var http_request = false;
    
    function makeRequest(url, parameters) {
      http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/xml');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
      http_request.onreadystatechange = alertContents;
      http_request.open('GET', url + parameters, true);
      http_request.send(null);
}

function makeRequest2(url) 
{
      http_request = false;
      if (window.XMLHttpRequest)
      { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/xml');
         }
      } else if (window.ActiveXObject) { // IE
         try
         {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         }
         catch (e)
         {
            try{
               http_request = new ActiveXObject("Microsoft.XMLHTTP");} 
            catch (e) {}}
		}
      if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
      http_request.onreadystatechange = managestatechange;
      http_request.open('GET', url, false);
      http_request.send(null);
      window.setTimeout("waiting('divProgress', false)", 2000);
   }
   function managestatechange() 
   {
		switch (http_request.readyState)
		{
		case 2, 3:
			waiting("divProgress",false);
			break;
		case 4:
			//waiting("divProgress", false);
			break;
		}	
	}
		// Hide the progress indicator for now
		

	function getdata2(url)
	{
		makeRequest2(url);
		if (http_request.readyState == 4)
		{
         //if (http_request.status == 200)
         //{
			var xmldoc = http_request.responseText;
			return xmldoc;
		//	}
		}	
	}
	function setdata(url, DataToSend)
	{
	http_request.Open("POST",url,false);
	http_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	try
	{
		http_request.send(DataToSend);
	}
	catch(e)
	{
		alert(e);
	}

	}
	function buildPOST(theFormName) {
		theForm = document.forms[theFormName];
		var qs = ''
		for (e=0;e<theForm.elements.length;e++) 
		{
			if (theForm.elements[e].name!='') 
			{
			var name = theForm.elements[e].name;
			qs+=(qs=='')?'':'&'
			qs+= name+'='+escape(theForm.elements[e].value);
			}
		}
		qs+="\n";
		return qs;
	}

   function alertContents() {
      if (http_request.readyState == 4) {
         if (http_request.status == 200) {

				var xmldoc = http_request.responseXML;
				var root = xmldoc.getElementsByTagName('NewDataSet').item(1);
				
				for (var iNode = 0; iNode < root.childNodes.length; iNode++) 
				{
					var node = root.childNodes.item(iNode);
					alert(node);
					for (i = 0; i < node.childNodes.length; i++) 
					{
						var sibl = node.childNodes.item(i);
						var len = parseInt(sibl.childNodes.length / 2);
						var arr = new Array(len);
						var cnt = 0;
						for (x = 0; x < sibl.childNodes.length; x++) 
						{
							var sibl2 = sibl.childNodes.item(x);
							var sibl3;
							if (sibl2.childNodes.length > 0) 
							{
								sibl3 = sibl2.childNodes.item(0);
								arr[cnt] = sibl3.data;	
								cnt++;
							}
						}
						addrow("mytable", arr);
					}
				}
         } else {
            alert('There was a problem with the request.');
         }
      }
   }
   function do_xml() {
		makeRequest('/howto-ajax-xml.php', '');
   }
   function addrow(tablename, arr) {
   var tbl = document.getElementById(tablename);
   var lastRow = tbl.rows.length;
	var row = tbl.insertRow(lastRow);
		for (r = 0; r < arr.length; r++) {	
			var cell = row.insertCell(r);
			cell.innerHTML = arr[r];
   	}
   }
  
function waiting(where, show)
{
	if(show || show == null)
	{
		getobj(where).innerHTML='<center><img src=images/loading-circ.gif align=absmiddle> Loading content...</center>';
	}
	else
	{
		//document.all.item(where).style.display = "none";	
		if(where == null || where =='undefined')
			getobj(where).style.display="none";
	}
	window.setTimeout("", 2000);
}

function dynamiccombo(alphaid, currlevel)
	{
		
		var childDiv;
		var parentDiv;
		var maxlevel = 0;
		if(document.getElementById){
			maxlevel = parseInt(document.getElementById("maxlevel").value);}
		else{
			maxlevel = parseInt(document.all("maxlevel").value);}
	
		if(document.getElementById){
			parentDiv=document.getElementById("div" + currlevel);
			document.getElementById("maxlevel").value = maxlevel;	}
		else{
			parentDiv = document.all("div"+currlevel);
			document.all("maxlevel").value = maxlevel ;	}
		
		if (parentDiv == null){
			if(document.getElementById)
				parentDiv = document.getElementById("div0");
			else
				parentDiv = document.all("div0");	}
		
		if(parentDiv.hasChildNodes()==true || alphaid=="")
			{
				//var removed = parentDiv.removeChild(childDiv);
				childDiv=document.getElementById("div"+ parseInt(currlevel+1));
				if(childDiv!=null)
				{
					childDiv.innerHTML='';
					var removed = childDiv.parentNode.removeChild(childDiv);
				}
			}
				
		
		// Get new div + select data which depends on category ID
		waiting('divProgress', true);
		var data = getdata2('../dynamiccombo_ajax.asp?catid=' + alphaid + '&level=' + parseInt(currlevel+1));
		if(data.length>0)
		{
			/* Create the child inner nodes */ 
			maxlevel = currlevel+1;
			
			childDiv = document.createElement('div'); 
			childDiv.setAttribute('id', "div"+ parseInt(maxlevel));
			childDiv.setAttribute('style', "position:relative");
			childDiv.innerHTML=data;
			parentDiv.appendChild(childDiv);
			childDiv.focus();
		}
		else
		{
			maxlevel = currlevel;
		}

		// Setting Max level in hidden field for selecting last level cat ID
		updatefield('maxlevel', maxlevel);
		//alert(parentDiv.innerHTML);
		
		childDiv = null;
		parentDiv = null;
		
	}
	
	function updatefield(fld, fldvalue)
	{
		getobj(fld).value = fldvalue;
	}
	function setfocus(fld) {
		getobj(fld).focus();
	}
	function getvalue(fld)	{
		return getobj(fld).value;
	}
	function getobj(fld)
	{
		
		if(document.getElementById && !document.all)	// FireFox
		{
			//alert(document.getElementById('txtname').value);
			return document.getElementById(fld);
		}
		else if(document.getElementById)
		{
			return document.getElementById(fld);
		}
		else
		{
			return document.all(fld);
		}
	}
	
	function getcatvalue()
	{
		var cboLast = null;
		var maxlevel= 0;
		
		if(document.getElementById)
		{
			maxlevel = document.getElementById("maxlevel").value;
			cboLast=document.getElementById("cbo" + maxlevel);
		}
		else
		{
			maxlevel = document.all("maxlevel").value ;
			cboLast = document.all("cbo"+maxlevel);
		}
		return cboLast.value;
	}
	
	
var JSdocs = ["main1.js","main2.js"];
function loadJSFiles() {
	var head = document.getElementsByTagName("head")[0];
	for (var i=0; i<JSdocs.length; i++) {
		var script = document.createElement("script");
		script.setAttribute("type","text/javascript");
		script.setAttribute("src",JSdocs[i]);
		head.appendChild(script);
	}
}	
