﻿
//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();
	}
}

function handleDivTag(divid) 
{ 
   var divid;    
   return divid; 
} 

// Create the Divtag Handler -- Mainly an IE 6 Fix 
var divhandler = new handleDivTag(null); 

function CountryListLoad(divid)
{
	// URL to get countries
	var requestUrl = "/diyGetCountries.aspx";
	CreateXmlHttp();
	divhandler.divid = divid;

	// If browser supports XMLHTTPRequest object
	if(XmlHttp)
	{
		//Setting the event handler for the response
		XmlHttp.onreadystatechange = HandleCountries;
		
		//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);		
	}
}


function HandleCountries()
{
	// 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)
		{			
		    //var imgLoading_Countries = document.getElementById("imgLoading_Countries");
            //imgLoading_Countries.style.visibility = 'hidden';
            //imgLoading_Countries.style.display = 'none';
            var countryList = document.getElementById(divhandler.divid);
            //countryList.disabled = ''
	        //Clears the location combo box contents.
	        for (var count = countryList.options.length-1; count >-1; count--)
	        {
		        countryList.options[count] = null;
	        }
	        //var locationNodes = cityNode.getElementsByTagName('ctl00$locations');
	        var countryNodes = XmlHttp.responseXML.documentElement.getElementsByTagName('country');
	        var optionText;
	        var optionValue;
	        var optionItem;
	        //Add new locations list to the locations combo box.
	        //optionItem = new Option('All locations', '',  false, false);
	        //countryList.options[countryList.length] = optionItem;
	        for (var count = 0; count < countryNodes.length; count++)
	        {
   		        optionValue = countryNodes[count].attributes.getNamedItem("id").value;
   		        optionText = GetInnerText(countryNodes[count]);
   		        if (optionText != '')
   		        {
		        optionItem = new Option(optionText, optionValue,  false, false);
		        countryList.options[countryList.length] = optionItem;
		        }
		    }
		    // Test start
		    var qs = new Querystring()
		    if (qs.contains('CountryRes')) {
		        document.getElementById('ddlCountryRes').value = qs.get('CountryRes');
		    }
            // Test end 
		}
		else
		{
			alert("There was a problem retrieving country list from the server." );
    	    //var imgLoading_Countries = document.getElementById("imgLoading_Countries");
            //imgLoading_Countries.style.visibility = 'hidden';
            //imgLoading_Countries.style.display = 'none';
		}
	}
	else
        {
       	    //var imgLoading_Countries = document.getElementById("imgLoading_Countries");
			//imgLoading_Countries.style.visibility = 'visible';
            //imgLoading_Countries.style.display = 'inline';
            var countryList = document.getElementById(divhandler.divid);
		    //countryList.disabled = 'true';
            //var cityList = document.getElementById("ctl00_mainContentPlaceHolder_cityList");
		    //cityList.disabled = 'true';
		    //var locationList = document.getElementById("ctl00_mainContentPlaceHolder_locationList");
		    //locationList.disabled = 'true';
		    var optionItem;
	        optionItem = new Option('Loading...', '',  false, false);
	        //optionItem = new Option('Australia', 'Australia',  false, false);
	        countryList.options[countryList.length] = optionItem;
        }
}


//Returns the node text value 
function GetInnerText (node)
{
	 return (node.textContent || node.innerText || node.text) ;
}

