var XmlHttpObj; // declare a global  XMLHTTP Request object
var domain_name = document.URL;
if (domain_name.match("www")){
	var myUrl = "http://www."+domain_name.substring(11,domain_name.lastIndexOf('/'));
}
else{
	var myUrl = "http://"+domain_name.substring(7,domain_name.lastIndexOf('/'));
}

function CreateXmlHttpObj(){ // create an instance of XMLHTTPRequest Object, varies with browser type, try for IE first then Mozilla
	try{ 	// try creating for IE (note: we don't know the user's browser type here, just attempting IE first.)
		XmlHttpObj = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e){
		try{
			XmlHttpObj = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(oc){
			XmlHttpObj = null;
		}
	}
	if(!XmlHttpObj && typeof XMLHttpRequest != "undefined"){ // if unable to create using IE specific code then try creating for Mozilla (FireFox) 
		XmlHttpObj = new XMLHttpRequest();
	}
}

function GetInnerText (node){ // returns the node text value
	 return (node.textContent || node.innerText || node.text) ;
}
// All function of "StateChangeHandler" called when state of  XmlHttpObj changes
// we're interested in the state that indicates data has been
// received from the server
function StateChangeHandler() {
	if(XmlHttpObj.readyState == 4){	// state ==4 indicates receiving response data from server is completed
		if(XmlHttpObj.status == 200){// To make sure valid response is received from the server, 200 means response received is OK
			PopulateCountryList(XmlHttpObj.responseXML.documentElement);
		}
		else{
			alert("problem retrieving data from the server, status code: "  + XmlHttpObj.status);
		}
	}
}

function OnCountryLoad() { // On Page Load
	var countryList = document.main.cntry;
    var requestUrl; // url of page that will send xml data back to client browser
	requestUrl = myUrl+"/ip_country_xml.php";
	CreateXmlHttpObj();	
	if(XmlHttpObj){ // verify XmlHttpObj variable was successfully initialized
        // assign the StateChangeHandler function ( defined below in this file)
        // to be called when the state of the XmlHttpObj changes        
		XmlHttpObj.onreadystatechange = StateChangeHandler; // receiving data back from the server is one such change			
		XmlHttpObj.open("GET", requestUrl,  true); // define the iteraction with the server -- true for as asynchronous.		
		XmlHttpObj.send(null); // send request to server, null arg  when using "GET"
	}
}

function PopulateCountryList(countryNode){	
	var countryList = document.main.cntry;
	for (var count = countryList.options.length-1; count >-1; count--){ // clear the country list 
		countryList.options[count] = null;
	}

	var countryNodes = countryNode.getElementsByTagName('country');
	var idValue ;
	var textValue; 
	var optionItem;
	var curr_selected;
	for (var count = 0; count < countryNodes.length; count++){ 	// populate the dropdown list with data from the xml doc
   		textValue = GetInnerText(countryNodes[count]);
		if (textValue.length < 2)
			continue;
		idValue = countryNodes[count].getAttribute("id");
		curr_selected = countryNodes[count].getAttribute("default");
		if (curr_selected == 1)
			optionItem = new Option( textValue, idValue,  false, true);
		else
			optionItem = new Option( textValue, idValue,  false, false);
		countryList.options[countryList.length] = optionItem;
	}	
}
