
// 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();
	}
}

// called from onChange or onClick event of the continent dropdown list
function StateListOnChange() 
{
    var StateList = document.getElementById("autostate");
	var CountryList = document.getElementById("Country");
    
    // get selected continent from dropdown list
    var selectedState = StateList.options[StateList.selectedIndex].value;
    var selectedCountry = CountryList.options[CountryList.selectedIndex].value;	
    
    // url of page that will send xml data back to client browser
    var requestUrl;
    // use the following line if using asp
    requestUrl = "modules/xml_data_provider_jw.asp" + "?state=" + encodeURIComponent(selectedState) + "&country=" + encodeURIComponent(selectedCountry);
    // use the following line if using php
    // requestUrl = "xml_data_provider.php" + "?filter=" + encodeURIComponent(selectedContinent);
    
	CreateXmlHttpObj();
	
	// verify XmlHttpObj variable was successfully initialized
	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 = 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);		
	}
}

function CountryListOnChange() 
{
	var CountryList = document.getElementById("Country");
   
    // get selected continent from dropdown list
    var selectedCountry = CountryList.options[CountryList.selectedIndex].value;	
     //alert(selectedCountry)
    // url of page that will send xml data back to client browser
    var requestUrl;
    // use the following line if using asp
    requestUrl = "modules/xml_data_provider_states.asp" + "?country=" + encodeURIComponent(selectedCountry);
    // use the following line if using php
    // requestUrl = "xml_data_provider.php" + "?filter=" + encodeURIComponent(selectedContinent);
    
	CreateXmlHttpObj();
	
	// verify XmlHttpObj variable was successfully initialized
	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 = CountryStateChangeHandler;
		
		// 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 StateChangeHandler()
{
		//alert("valled");
	// 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)
		{			
			PopulateCountryList(XmlHttpObj.responseXML.documentElement);
		}
		else
		{
			//alert("problem retrieving data from the server, status code: "  + XmlHttpObj.status);
		}
	}
}

function CountryStateChangeHandler()
{
	// 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)
		{			
			PopulateStateList(XmlHttpObj.responseXML.documentElement);
		}
		else
		{
			//alert("problem retrieving data from the server, status code: "  + XmlHttpObj.status);
		}
	}
}

function PopulateCountryList(townNode)
{
    var TownList = document.getElementById("autotown");
	// clear the country list 
	for (var count = TownList.options.length-1; count >-1; count--)
	{
		TownList.options[count] = null;
	}

	var townNodes = townNode.getElementsByTagName('town');
	var idValue;
	var textValue; 
	var optionItem;
	var cookieTown = get_cookie ("Town");
	// populate the dropdown list with data from the xml doc
	for (var count = 0; count < townNodes.length; count++)
	{
   		textValue = GetInnerText(townNodes[count]);
		idValue = townNodes[count].getAttribute("id");
		matchValue = idValue.replace(/ /g, "+");
		if (cookieTown==matchValue){selected=true;}else{selected=false;}
		optionItem = new Option( textValue, idValue,  selected, selected);
		TownList.options[TownList.length] = optionItem;
	}
	//alert(get_cookie ("Town"));
	//alert(townNodes.length)
	if (townNodes.length==1){
		document.getElementById("townrow").style.display = 'none';
	}else{
		document.getElementById("townrow").style.display = '';
		
	}
}

function PopulateStateList(stateNode)
{
	//alert(StateNode);
    var stateList = document.getElementById("autostate");
	// clear the state list 
	for (var count = stateList.options.length-1; count >-1; count--)
	{
		stateList.options[count] = null;
	}

	var stateNodes = stateNode.getElementsByTagName('state');
	var idValue;
	var textValue; 
	var optionItem;
	var cookiestate = get_cookie("State");
	
	if (document.getElementById("selstate")!=null){
		cookiestate=document.getElementById("laststate").innerHTML;
	}
	
	// populate the dropdown list with data from the xml doc
	//alert(stateNodes.length + "jj");
	optionItem = new Option( "Select state / county", "",  false, false);
	stateList.options[stateList.length] = optionItem;
	for (var count = 0; count < stateNodes.length; count++)
	{
   		textValue = GetInnerText(stateNodes[count]);
		idValue = stateNodes[count].getAttribute("id");
		matchValue = idValue.replace(/ /g, "+");
		//alert((matchValue==cookiestate) + " " + matchValue + " " + cookieState);
		if (cookiestate==matchValue){selected=true;}else{selected=false;}
		//alert(selected);
		optionItem = new Option( textValue, idValue,  selected, selected);
		stateList.options[stateList.length] = optionItem;
	}
	if (document.getElementById("autotown")!=null){
		StateListOnChange();
	}
}

function get_cookie ( cookie_name )
{
  var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );

  if ( results )
    return ( unescape ( results[2] ) );
  else
    return null;
}


// returns the node text value 
function GetInnerText (node)
{
	 return (node.textContent || node.innerText || node.text) ;
}










