
// declare a global  XMLHTTP Request object
var XmlHttpObj;

// create an instance of XMLHTTPRequest Object, varies with browser type, try for IE first then Mozilla
function CreateXmlHttpObj()
{
	// try creating for IE (note: we don't know the user's browser type here, just attempting IE first.)
	try
	{
		XmlHttpObj = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			XmlHttpObj = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(oc)
		{
			XmlHttpObj = null;
		}
	}
	// if unable to create using IE specific code then try creating for Mozilla (FireFox) 
	if(!XmlHttpObj && typeof XMLHttpRequest != "undefined") 
	{
		XmlHttpObj = new XMLHttpRequest();
	}
}
//*************************  COUNTRY SECTION ***********************************///
// called from onChange or onClick event of the country dropdown list
function OnclickSubzone(area_id,language_id,country_id,count) 
{
    // url of page that will send xml data back to client browser
    var requestUrl;

    requestUrl = "xml_zone_subzone.php" + "?areas_id=" + encodeURIComponent(area_id) + "&language_id=" + encodeURIComponent(language_id) + "&country_id=" + encodeURIComponent(country_id) + "&count=" + encodeURIComponent(count);
	CreateXmlHttpObj();
	//alert(requestUrl);
	if(XmlHttpObj)
	{
        // assign the StateChangeHandler function ( defined below in this file)
        // to be called when the state of the XmlHttpObj changes
        // receiving data back from the server is one such change
		XmlHttpObj.onreadystatechange = zoneChangeHandler;
		//XmlHttpObj.onreadystatechange = StateChangeHandler;
		
		// define the iteraction with the server -- true for as asynchronous.
		XmlHttpObj.open("GET", requestUrl,  true);
		
		// send request to server, null arg  when using "GET"
		XmlHttpObj.send(null);		
	}
}

// this function called when state of  XmlHttpObj changes
// we're interested in the state that indicates data has been
// received from the server
function zoneChangeHandler()
{
	// state ==4 indicates receiving response data from server is completed
	if(XmlHttpObj.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttpObj.status == 200)
		{			
			PopulatesubzoneList(XmlHttpObj.responseXML.documentElement);
			//PopulateSubCatList(XmlHttpObj.responseXML.documentElement);
		}
		else
		{
			alert("problem retrieving data from the server, status code: "  + XmlHttpObj.status);
		}
	}
}

// populate the contents of the country dropdown list
function PopulatesubzoneList(subzoneNode)
{
    //var subzoneList = document.getElementById("subzoneList");
	//document.getElementById("subzoneList").style.display="block";
	// clear the country list 

	var subzone1Node = subzoneNode.getElementsByTagName('subarea');
	var totalzoneNode = subzoneNode.getElementsByTagName('totalzone');
	var totalzone = totalzoneNode[0].getAttribute("id");
	var countidNode = subzoneNode.getElementsByTagName('countid');
	var countid = countidNode[0].getAttribute("id");

	var idValue;
	var textValue; 
	if(subzone1Node[0].getAttribute("id") != '0'){
		var str = '<table cellpadding="1" cellspacing="1"><tbody><tr><td  valign="top">';
		// populate the dropdown list with data from the xml doc
		for (var count = 0; count < subzone1Node.length; count++)
		{
			textValue = GetInnerText(subzone1Node[count]);
			//alert(textValue);
			textValue = replace_spl_char(textValue);
			idValue = replace_spl_char(subzone1Node[count].getAttribute("id"));
			idValue = idValue.split("_");
			var idValdiv = idValue[1];
			str += '<a href="index.php?area_id='+idValdiv+'&type=2">'+textValue+'</a><br>';
		}
		str += '</td></tr></tbody></table>';
		for(var i=1; i<=totalzone; i++){
			if(i == countid){
				document.getElementById("subzoneList_" + i).style.display="block";
				document.getElementById("subzoneList_" + i).innerHTML=str;
			}else{
				document.getElementById("subzoneList_" + i).style.display="none";
			}	
		}
	}else{
		for(var i=1; i<=totalzone; i++){
			document.getElementById("subzoneList_" + i).style.display="none";
		}	
	}	
}
	//alert(str);
// returns the node text value 
function GetInnerText (node)
{
	 return (node.textContent || node.innerText || node.text) ;
}
