﻿
//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();
	}
}

//Gets called when country combo box selection changes
function CountriesList()
{	
    countryList = document.getElementById("lstBusinessType");
    
	//Getting the selected country from country combo box.
	var selectedCountry = countryList.options[countryList.selectedIndex].value;
	
	// URL to get states for a given country
	//var requestUrl = AjaxServerCountry + "?SelectedCountry=" + encodeURIComponent(selectedCountry);
	var requestUrl = AjaxServerCountry + "?businesstype=" + encodeURIComponent(selectedCountry);
	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);
	}
}

//Gets called when country combo box selection changes
function ClassifiedStatesList()
{	
    classifiedList = document.getElementById("lstClassifiedType");
    
	//Getting the selected classified from classified combo box.
	var selectedClassified = classifiedList.options[classifiedList.selectedIndex].value;
	
	// URL to get states for a given classified	
	var requestUrl = AjaxServerClassified + "?classifiedtype=" + encodeURIComponent(selectedClassified);
	CreateXmlHttp();
	
	// If browser supports XMLHTTPRequest object
	if(XmlHttp)
	{
		//Setting the event handler for the response
		XmlHttp.onreadystatechange = HandleClassifiedResponse;
		
		//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);
	}
}

//Called when response comes back from server
function HandleClassifiedResponse()
{
	// 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)
		{
			ClearAndSetClassifiedListItems(XmlHttp.responseXML.documentElement);		
		}
		else
		{
			alert("There was a problem retrieving data from the server.");
		}
	}
}


//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)
		{
			ClearAndSetListItems(XmlHttp.responseXML.documentElement);		
		}
		else
		{
			alert("There was a problem retrieving data from the server.");
		}
	}
}

//Clears the contents of state combo box and adds the states of currently selected country
function ClearAndSetClassifiedListItems(stateNode)
{   
    //Clears the Country combo box contents.    
    stateList = document.getElementById("lstClassifiedState");    
    clearSelect(stateList);	
    var countryNodes = stateNode.getElementsByTagName('sstatename');    
    var countryValue = stateNode.getElementsByTagName('nstateid');	
    
    textName = "---- Select ----";
    textValue = "0";

    optionItem = new Option(textName, textValue, false, false);
    stateList.options[stateList.length] = optionItem;
    
    stateList.disabled = 1;     
    
    var textValue; 
    var optionItem;
    //Add new states list to the state combo box.
    for (var count = 0; count < countryNodes.length; count++)
    {
	    textName = GetInnerText(countryNodes[count]);
	    textValue = GetInnerText(countryValue[count]);
   		
	    optionItem = new Option(textName, textValue, false, false);
	    stateList.options[stateList.length] = optionItem;
	    stateList.disabled = 0;
    }    
}

//Clears the contents of state combo box and adds the states of currently selected country
function ClearAndSetListItems(stateNode)
{   
    //Clears the Country combo box contents.    
    stateList = document.getElementById("lstState");    
    clearSelect(stateList);	
    var countryNodes = stateNode.getElementsByTagName('sstatename');    
    var countryValue = stateNode.getElementsByTagName('nstateid');	
    
    textName = "---- Select ----";
    textValue = "0";

    optionItem = new Option(textName, textValue, false, false);
    stateList.options[stateList.length] = optionItem;
    
    stateList.disabled = 1;     
    
    var textValue; 
    var optionItem;
    //Add new states list to the state combo box.
    for (var count = 0; count < countryNodes.length; count++)
    {
	    textName = GetInnerText(countryNodes[count]);
	    textValue = GetInnerText(countryValue[count]);
   		
	    optionItem = new Option(textName, textValue, false, false);
	    stateList.options[stateList.length] = optionItem;
	    stateList.disabled = 0;
    }    
}

//Returns the node text value 
function GetInnerText (node)
{
	return (node.textContent || node.innerText || node.text) ;
}

function appendToSelect(select, value, content)
{    
	var opt;
	opt = document.createElement("option");//Create an Element of type option
	opt.value = value;//Set the option's value
	opt.appendChild(content);//Attach the text content to the option
	select.appendChild(opt);//Append the option to the referenced [Territory] select box
	status.innerText = value;
}

function clearSelect(select)
{
	select.options.length = 0; //Set the select box's length to 1 so only "--Select--" is availale in the selection on calling this function.    
}
