// JavaScript Document
// Get products which related to the company through the ID of products

function getProducts(comId, elementId)
{
	var req; //定义变量，用来创建xmlhttprequest对象
	creatReq();
	
	function creatReq() // 创建xmlhttprequest,ajax开始
	{
		var url = "getProducts.php?comId="+comId; //要请求的服务端地址
		if(window.XMLHttpRequest) //非IE浏览器，用xmlhttprequest对象创建
		{
			req=new XMLHttpRequest();
		}
		else if(window.ActiveXObject) //IE浏览器用activexobject对象创建
		{
			req=new ActiveXObject("Microsoft.XMLHttp");
		}
		
		if(req) //成功创建xmlhttprequest
		{
			req.open("GET",url); //与服务端建立连接(请求方式post或get，地址,true表示异步)
			req.onreadystatechange = callback; //指定回调函数
			req.send(null); //发送请求
		}
	}
	
	//alert("req.status"+req.status);
	function callback() //回调函数，对服务端的响应处理，监视response状态
	{
		if(req.readyState==4) //请求状态为4表示成功
		{
			if(req.status==200) //http状态200表示OK
			{
				Dispaly(); //所有状态成功，执行此函数，显示数据
			}
		}
	}
	
	function Dispaly() //接受服务端返回的数据，对其进行显示
	{
		returnValue = req.responseText;
		updatemenu(elementId,returnValue);
	}
}

function updatemenu(elementId,text){
	//对产品下拉框的初始化
	document.getElementById(elementId).options.length = 1;
	textArray = text.split("|");
	//alert(text);
	//alert(textArray.length);
	for(var i=0;i<textArray.length-1;i++){
		with(document.getElementById(elementId)){
			titleUrl = textArray[i].split("**");
			options[options.length]=new Option(titleUrl[0],titleUrl[1]);
		}
	}
}

// 根绝用户选择的产品项，跳转到对应的产品内容页面。
function getProURL(textUrl)
{
	//alert(textUrl);
	self.location = textUrl;
}


//根据用户选择的公司名称来确定下载的pdf列表
function getDoc(companyId,catId, companyName)
{
	var req; //定义变量，用来创建xmlhttprequest对象
	createXmlHttp();
	
	function createXmlHttp() // 创建xmlhttprequest,ajax开始
	{
		var url = "getProducts.php?companyId="+companyId+"&catId="+catId+"&companyName="+companyName; //要请求的服务端地址
		if(window.XMLHttpRequest) //非IE浏览器，用xmlhttprequest对象创建
		{
			req=new XMLHttpRequest();
		}
		else if(window.ActiveXObject) //IE浏览器用activexobject对象创建
		{
			req=new ActiveXObject("Microsoft.XMLHttp");
		}
		
		if(req) //成功创建xmlhttprequest
		{
			req.open("GET",url); //与服务端建立连接(请求方式post或get，地址,true表示异步)
			req.onreadystatechange = callback; //指定回调函数
			req.send(null); //发送请求
		}
	}
	
	//alert("req.status"+req.status);
	function callback() //回调函数，对服务端的响应处理，监视response状态
	{
		if(req.readyState==4) //请求状态为4表示成功
		{
			if(req.status==200) //http状态200表示OK
			{
				downloadPDF(); //所有状态成功，执行此函数，显示相应公司中所有的pdf文档
			}
		}
	}
	
	function downloadPDF() //接受服务端返回的数据，对其进行显示
	{
		returnValue = req.responseText;
		//alert(returnValue);
		self.location = returnValue;
	}
}