/*
example:
onchange="ClearList(document.getElementById('lstCity')); return lstOnChange('../../Xml/ZoneProvinceCity/ZoneProvinceXML.aspx', 'lstZone', 'lstProvince', 'id_province', 'name', 'Province')"
*/
var _value
var _text
var _lstSubList
var _node

//Global XMLHTTP Request object
var XmlHttp;

//Creating and setting the instance of appropriate XMLHTTP Request object to a “XmlHttp” variable  
function CreateXmlHttp()
{
	//Creating object of XMLHTTP in IE
	try
	{
		XmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(oc)
		{
			XmlHttp = null;
		}
	}
	//Creating object of XMLHTTP in Mozilla and Safari 
	if(!XmlHttp && typeof XMLHttpRequest != "undefined") 
	{
		XmlHttp = new XMLHttpRequest();
	}
}

//Called when response comes back from server
function HandleResponse()
{
	// To make sure receiving response data from server is completed
	if(XmlHttp.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttp.status == 200)
		{			
			ClearAndSetSubListItems(XmlHttp.responseXML.documentElement);
		}
		else
		{
			alert("There was a problem retrieving data from the server." );
		}
	}
}

//Returns the node text value 
function GetInnerText (node)
{
	 return (node.textContent || node.innerText || node.text) ;
}

//Gets called when combo box selection changes
function lstOnChange(AjaxServerPageName, lstMainList, lstSubList, value, text, node) 
{
	_value = value;
	_text = text;
	_node = node;
	_lstSubList = lstSubList;
	var lstMain = document.getElementById(lstMainList);
	
	//Getting the selected value from combo box.
	var selected = lstMain.options[lstMain.selectedIndex].value;

	// URL to get sub items for a given value
	var requestUrl = AjaxServerPageName;
	if (requestUrl.indexOf("?") < 0)
		requestUrl += "?";
	else
		requestUrl += "&";
		
	requestUrl += "SelectedId=" + encodeURIComponent(selected);
	CreateXmlHttp();

	// If browser supports XMLHTTPRequest object
	if(XmlHttp)
	{
		//Setting the event handler for the response
		XmlHttp.onreadystatechange = HandleResponse;
		
		//Initializes the request object with GET (METHOD of posting), 
		//Request URL and sets the request as asynchronous.
		XmlHttp.open("GET", requestUrl,  true);
		
		//Sends the request to server
		XmlHttp.send(null);		
	}
}

//Clears the contents of the list
function ClearList(lst)
{
	for (var count = lst.options.length-1; count >-1; count--)
	{
		lst.options[count] = null;
	}
}

//Clears the contents of the sub combo box and adds the sub items of currently selected value
function ClearAndSetSubListItems(mainNode)
{
    var lstSub = document.getElementById(_lstSubList);
	//Clears the sub items combo box contents.
	ClearList(lstSub);
	
	var subNodes = mainNode.getElementsByTagName(_node);
	var id;
	var textValue;
	var optionItem;
	//Add blank value
	optionItem = new Option('', '-1',  false, false);
	lstSub.options[lstSub.length] = optionItem;
	//Add new sub items list to the sub items combo box.
	for (var count = 0; count < subNodes.length; count++)
	{
   		id = GetInnerText(subNodes[count].getElementsByTagName(_value)[0]);
   		textValue = GetInnerText(subNodes[count].getElementsByTagName(_text)[0]);
		optionItem = new Option(textValue, id,  false, false);
		lstSub.options[lstSub.length] = optionItem;
	}
}