var xmlHttp;
var idType = "";
var ROOT = "";

function fn_FindUrl()
{
	var trmpUrl = window.location;
	var str=new String(trmpUrl);
	arrUrl = str.split("/");
	
	domanName = arrUrl[2];

	ROOT = "http://"+domanName;
}

function ajaxSubCategory(id)
{
	idType = id; 
	
	var obj = document.getElementById("showDistIndustries");
	
	if(obj!=null && obj.style.display=='none'){
		obj.style.display='block';
	}
	xmlHttp=GetXmlHttpObject();
	
	var object = document.getElementById("ind");
	if(object!=""){
		
		if(object.style.display==='none'){
			object.style.display='block';
		}
	}

	if (xmlHttp==null)
	 {
		alert ("Browser does not support HTTP Request")
		return
	 }
	fn_FindUrl();
	var url = ROOT+"/showMoreIndustries.php?indType="+id;
	//alert(url);
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	setTimeout("getSupplier("+id+")",1000);
	
}

function stateChanged() 
{	
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	 { 
		var obj = document.getElementById("showDistIndustries");
		if(obj!=null){
			
			document.getElementById("showDistIndustries").innerHTML=xmlHttp.responseText ; 
		}
	 } 				
}

function getSupplier(id)
{
	//alert(id);
	idType = id; 
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	 {
		alert ("Browser does not support HTTP Request")
		return
	 }
	fn_FindUrl();
	var url = ROOT+"/ajax.php?indType="+id;
	//alert(url);
	xmlHttp.onreadystatechange=processSetSupplierRequest;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);				
}

function processSetSupplierRequest()
{
	
	if (xmlHttp.readyState == 4) 
	{
		
		if (xmlHttp.status == 200) 
		{
			//alert(xmlHttp.responseXML);
			showSupplier();
		} 
		else 
		{
		alert ( "Not able to retrieve data" );
		}
	}
}

function showSupplier()
{
	var counterObject = xmlHttp.responseXML.getElementsByTagName("countSupplier")[0];
	var objSelect=document.getElementById("selSupplierName");
	if(counterObject!=null){
		var counter = counterObject.childNodes[0].nodeValue;
		//alert(counter);
		if(counterObject != null && counter > 0) {
			
			objSelect.options.length = 1;
			
			if(objSelect!=null){
				var i;
				for(i=0;i<counter;i++)
				{
					var objSupplierId = xmlHttp.responseXML.getElementsByTagName("supplierId"+i)[0];
					var supplierId = objSupplierId.childNodes[0].nodeValue;
					var objsupplierName = xmlHttp.responseXML.getElementsByTagName("supplierName"+i)[0];
					var supplierName = objsupplierName.childNodes[0].nodeValue;
					//alert(supplierName);
					var newOpt = new Option(supplierName,supplierId);
					objSelect.options[objSelect.options.length] = newOpt;
				}
			}
		}
	}else{
		var i;
		for(i=objSelect.options.length-1;i>=0;i--){
			objSelect.remove(i);
		}
		var newOpt = new Option("--select Supplier--","");
		objSelect.options[objSelect.options.length] = newOpt;
	}
}

function GetXmlHttpObject()
{
	//var xmlHttp=null;
	try
	 {
	 // Firefox, Opera 8.0+, Safari
	 xmlHttp=new XMLHttpRequest();
	 }
	catch (e)
	 {
	 //Internet Explorer
	 try
	  {
	  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	  }
	 catch (e)
	  {
	  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	 }
	return xmlHttp;
}

function setHiddenValue(val){
	
	var object =document.getElementById("categoryId");
	if(object!=null){
		document.getElementById("categoryId").value=val;
	}
}
