
// 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 FireFox
	if(!XmlHttpObj && typeof XMLHttpRequest != "undefined") 
	{
		XmlHttpObj = new XMLHttpRequest();
	}
}

// called from onChange or onClick event of the state dropdown list
function citychange() 
{
    var stateList = document.getElementById("stateList");
    
    // get selected continent from dropdown list
    var selectedState = stateList.options[stateList.selectedIndex].value;
    
    // url of page that will send xml data back to client browser
    var requestUrl;
    
    requestUrl = "findcities.php" + "?filter=" + encodeURIComponent(selectedState);
    
	CreateXmlHttpObj();
	
	// verify XmlHttpObj variable was successfully initialized
	if(XmlHttpObj)
	{
        // assign the StateChangeHandler function 
        // to be called when the state of the XmlHttpObj changes
       
		XmlHttpObj.onreadystatechange = StateChangeHandler1;
		
		// define the iteraction with the server -- true is for asynchronous.
		XmlHttpObj.open("GET", requestUrl,  true);
		
		// send request to server, use null arg  when using "GET"
		XmlHttpObj.send(null);		
	}
}

function schoolchange() 
{
    var cityList = document.getElementById("cityList");
    
    // get selected city from dropdown list
    var selectedCity = cityList.options[cityList.selectedIndex].value;
    
    // url of page that will send xml data back to client browser
    var requestUrl;
    
    requestUrl = "findschools.php" + "?filter=" + encodeURIComponent(selectedCity);
    
	CreateXmlHttpObj();
	
	// verify XmlHttpObj variable was successfully initialized
	if(XmlHttpObj)
	{
        // assign the StateChangeHandler function
        // to be called when the state of the XmlHttpObj changes
      
		XmlHttpObj.onreadystatechange = StateChangeHandler2;
		
		// define the iteraction with the server -- true is for asynchronous.
		XmlHttpObj.open("GET", requestUrl,  true);
		
		// send request to server, use null arg  when using "GET"
		XmlHttpObj.send(null);		
	}
}

// function called when state of  XmlHttpObj changes

function StateChangeHandler1()
{
	// 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)
		{			
			PopulateCityList(XmlHttpObj.responseXML.documentElement);
		}
		else
		{
			alert("problem retrieving data from the server, status code: "  + XmlHttpObj.status);
		}
	}
}
function StateChangeHandler2()
{
	// 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)
		{	
			PopulateSchoolList(XmlHttpObj.responseXML.documentElement);
		}
		else
		{
			alert("problem retrieving data from the server, status code: "  + XmlHttpObj.status);
		}
	}
}
// populate the contents of the city dropdown list
function PopulateCityList(cityNode)
{
    var cityList = document.getElementById("cityList");
	var schoolList = document.getElementById("schoolList");
	// clear the city list 
	
	for (var count = cityList.options.length-1; count >-1; count--)
	{
		cityList.options[count]=null;
	}
	for (var count = schoolList.options.length-1; count >-1; count--)
	{
		schoolList.options[count]=null;
	}
	//reinit school dropdown
	schoolList.options[0] = new Option( "Select School", "-1", false, false);



	var cityNodes = cityNode.getElementsByTagName('city');
	
	var idValue;
	
	var textVal; 
	var optionItem;

	// populate the dropdown list with data from the xml doc
	for (var count = 0; count < cityNodes.length; count++)
	{
   		textValue = GetInnerText(cityNodes[count]);
		idValue = cityNodes[count].getAttribute("id");
		optionItem = new Option( textValue, idValue, false, false);
		cityList.options[cityList.length] = optionItem;
	}
}
// populate the contents of the school dropdown list
function PopulateSchoolList(schoolNode)
{
    var schoolList = document.getElementById("schoolList");
	// clear the school list 
	
	for (var count = schoolList.options.length-1; count >-1; count--)
	{
		schoolList.options[count]=null;
	}

	var schoolNodes = schoolNode.getElementsByTagName('school');
	var idValue;
	var textValue; 
	var optionItem;
	// populate the dropdown list with data from the xml doc
	for (var count = 0; count < schoolNodes.length; count++)
	{
   		textValue = GetInnerText(schoolNodes[count]);
		idValue = schoolNodes[count].getAttribute("id");
		optionItem = new Option( textValue, idValue,  false, false);
		schoolList.options[schoolList.length] = optionItem;
	}
}

// returns the node text value 
function GetInnerText (node)
{
	 return (node.textContent || node.innerText || node.text) ;
}



