
//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 ButtonOnClick()
{

    var countryInput = document.getElementById("countryInput").value;
    var brandInput = document.getElementById("brandInput").value;

    var vehicleIndex = document.getElementById("vehicleList").selectedIndex;
	var selectedTechSuit = document.getElementById("vehicleList").options[vehicleIndex].value;

    var vehicleInput = document.getElementById("veicoloInput");
    vehicleInput.value = document.getElementById("vehicleList").options[vehicleIndex].text;
    
    var provinceIndex = document.getElementById("ProvinceList").selectedIndex;
	var provinceSelected = document.getElementById("ProvinceList").options[provinceIndex].value;

    var provinceInput = document.getElementById("provinceInput");
    provinceInput.value = provinceSelected;	
	
}


function ButtonZipOnClick()
{


    var countryInput = document.getElementById("countryInput").value;
    var brandInput = document.getElementById("brandInput").value;

    var vehicleIndex = document.getElementById("vehicleList").selectedIndex;
	var selectedTechSuit = document.getElementById("vehicleList").options[vehicleIndex].value;

    var vehicleInput = document.getElementById("veicoloInput");
    vehicleInput.value = document.getElementById("vehicleList").options[vehicleIndex].text;
    
	var zipcode = document.getElementById("txtZipCode").value;

    var provinceInput = document.getElementById("provinceInput");
    provinceInput.value = zipcode;	
	
}



function ProvinceListOnChange() 
{

	var provinceList = document.getElementById("ProvinceList");
    var provinceInput = document.getElementById("provinceInput");


	//Getting the selected country from country combo box.
	var selectedProv = provinceList.options[provinceList.selectedIndex].value;

    provinceInput.value = selectedProv;	
}

function VehicleListZIPOnChange()
{

	var vehicleList = document.getElementById("vehicleList");
    var countryInput = document.getElementById("countryInput").value;
    var brandInput = document.getElementById("brandInput").value;

    var ideoneitaInput = document.getElementById("idoneitaInput");


	//Getting the selected country from country combo box.
	var selectedTechSuit = vehicleList.options[vehicleList.selectedIndex].value;
	
	ideoneitaInput.value = selectedTechSuit;
	
}

//Gets called when country combo box selection changes
function VehicleListOnChange() 
{

	var vehicleList = document.getElementById("vehicleList");
    var countryInput = document.getElementById("countryInput").value;
    var brandInput = document.getElementById("brandInput").value;

    var ideoneitaInput = document.getElementById("idoneitaInput");


	//Getting the selected country from country combo box.
	var selectedTechSuit = vehicleList.options[vehicleList.selectedIndex].value;
	
	ideoneitaInput.value = selectedTechSuit;
	
	// URL to get states for a given country
	var requestUrl = AjaxServerPageName + "?idoneita=" + encodeURIComponent(selectedTechSuit) 
	                    + "&country=" + encodeURIComponent(countryInput) 
	                    + "&brand=" + encodeURIComponent(brandInput);
	                    

    var loadimg = document.getElementById("loadingimg");
    if (loadimg)
        loadimg.style.display = "block";
                    
	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);		
	}
}


//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)
		{			
	        var loadimg = document.getElementById("loadingimg");
	        if (loadimg)
	            loadimg.style.display = "none";
	        
			ClearAndSetProvinceListItems(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 ClearAndSetProvinceListItems(countryNode)
{

    var ProvinceList = document.getElementById("ProvinceList");
	//Clears the state combo box contents.
	for (var count = ProvinceList.options.length-1; count >-1; count--)
	{
		ProvinceList.options[count] = null;
	}

	var provinceNodes = countryNode.getElementsByTagName('province');
	var textValue;
	var opValue; 
	var optionItem;
	//Add new states list to the state combo box.
	
//	optionItem = new Option( " --- ", "",  false, false);
//	ProvinceList.options[ProvinceList.length] = optionItem;
	
	for (var count = 0; count < provinceNodes.length; count++)
	{
	    opValue = provinceNodes[count].attributes.getNamedItem("value").value;
   		textValue = provinceNodes[count].attributes.getNamedItem("text").value;
		optionItem = new Option( textValue, opValue,  false, false);
		ProvinceList.options[ProvinceList.length] = optionItem;
	}
}

//Returns the node text value 
function GetInnerText (node)
{
	 return (node.textContent || node.innerText || node.text) ;
}










