

var gXmlHttp;
var gStateProvElement = ""; //values to be filled by retrieving data from server
var gCurrencyElement = "";
var gErrMsgElement = "";
var gRequestData = ""; //Reuest data type, either 'stateprov' or 'currency'
var gSelectedCountry = "";
var gNumber = "";


function InvokeWServiceCall(sRequestData, sCountryDropdownElement, sStateProvElement, sCurrencyElement, sRemotingMsgElement, sMessage, sErrMsgElement, sNumber)
{
	if(sRemotingMsgElement != null && sRemotingMsgElement != '')
		DisplayRemoteCallMsg(sRemotingMsgElement, sMessage);
	
	gStateProvElement = sStateProvElement;
	
	gCurrencyElement = sCurrencyElement;
	gRequestData = sRequestData;
	gErrMsgElement = sErrMsgElement;
	gNumber = sNumber;
	gSelectedCountry = document.getElementById(sCountryDropdownElement).options[document.getElementById(sCountryDropdownElement).selectedIndex].value;
	
		
	ProcessWServiceCall(gSelectedCountry);

	if(sRemotingMsgElement != null && sRemotingMsgElement != '')	
		HideRemoteCallMsg(sRemotingMsgElement)
}


function ProcessWServiceCall(sCountry) 
{
	var sXml;
	var requestUrl;
	
	requestUrl = "http://www.landbluebookinternational.com/Webservice/RemoteCallReceiver.aspx" + "?Country=" + encodeURIComponent(sCountry) +"&Number=" + gNumber;
//requestUrl = "http://localhost/LandBlueBookInternational/Webservice/RemoteCallReceiver.aspx" + "?Country=" + encodeURIComponent(sCountry) +"&Number=" + gNumber;
		
	CreateXmlHttp();
	
	if(gXmlHttp) // If browser supports XMLHTTPRequest object
	{		
		try
		{
			gXmlHttp.onreadystatechange = HandleStateChange;
			gXmlHttp.open("GET", requestUrl);
			//alert(requestUrl);

			gXmlHttp.send(null);	

		}
		catch(ex)
		{
			DisplayRemoteCallMsg(gErrMsgElement, 'Error retriving state/province data.')
		}

	}
	else //no support for XMLHTTPRequest
	{
		DisplayRemoteCallMsg(gErrMsgElement, 'Browser error retriving state/province data.')
	}

}

function HandleStateChange()
{
	//alert(gDropdownElement);
	
	// To make sure receiving response data from server is completed
	if(gXmlHttp.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(gXmlHttp.status == 200)
		{			
			//alert(gXmlHttp.responseXML.getElementsByTagName("StateProvName").length);
			
			ProcessResponseMsg(gXmlHttp.responseXML);
		}
		else
		{
			DisplayRemoteCallMsg(gErrMsgElement, 'Error retriving state/province data. status= ' + gXmlHttp.status + ", " + gXmlHttp.statusText)
		}
	}
}

//Clears the contents of state combo box and adds the states of currently selected country
function ProcessResponseMsg(xmDocReturn)
{

	if(gNumber == "1AND2")
	{
		var DropDownList = document.getElementById(gStateProvElement);
	
		//Clears the dropdown list contents.
		for (var i = DropDownList.options.length-1; i > -1; i--)
		{
			DropDownList.options[i] = null;
		}
		
		PopulateStateProvList(DropDownList, xmDocReturn);

		var DropDownList = document.getElementById(gCurrencyElement);
		//Clears the dropdown list contents.
		for (var i = DropDownList.options.length-1; i > -1; i--)
		{
			DropDownList.options[i] = null;
		}

		PopulateCurrencyList(DropDownList, xmDocReturn);
	}
	else if (gNumber == "1")
	{
			var DropDownList = document.getElementById(gStateProvElement);
		//Clears the dropdown list contents.
		for (var i = DropDownList.options.length-1; i > -1; i--)
		{
			DropDownList.options[i] = null;
		}
		
		PopulateStateProvList(DropDownList, xmDocReturn);
	}
	else if (gNumber == "2")
	{
		var DropDownList = document.getElementById(gCurrencyElement);
		//Clears the dropdown list contents.
		for (var i = DropDownList.options.length-1; i > -1; i--)
		{
			DropDownList.options[i] = null;
		}

		PopulateCurrencyList(DropDownList, xmDocReturn);
	}
	
}

function PopulateCurrencyList(DropDownList, xmDocReturn)
{
	var newOpt = new Option("--- Selection ---",""); 
	DropDownList.options[DropDownList.options.length] = newOpt;
	
	var sCurrencyCode = "";
	var sCurrencyDes = "";
	
	if(xmDocReturn.getElementsByTagName("CurrencyDes").length > 0)
		sCurrencyCode = xmDocReturn.getElementsByTagName("CurrencyCode")[0].childNodes[0].nodeValue;
	if(xmDocReturn.getElementsByTagName("CurrencyDes").length > 0)
		sCurrencyDes = xmDocReturn.getElementsByTagName("CurrencyDes")[0].childNodes[0].nodeValue;
			
	if (sCurrencyCode.length > 0 && sCurrencyDes.length > 0)
	{
		var newOpt = new Option(sCurrencyDes, sCurrencyCode);
		DropDownList.options[DropDownList.options.length] = newOpt;
		if(sCurrencyCode != "EUR")
		{
			var newOpt = new Option("Euro", "EUR");
			DropDownList.options[DropDownList.options.length] = newOpt;
		}
		if(sCurrencyCode != "USD")
		{
			var newOpt = new Option("US Dollar", "USD");
			DropDownList.options[DropDownList.options.length] = newOpt;
		}
	}
	else
	{
		var newOpt = new Option("US Dollar", "USD");
		DropDownList.options[DropDownList.options.length] = newOpt;
		var newOpt = new Option("Euro", "EUR");
		DropDownList.options[DropDownList.options.length] = newOpt;

	}


}


function PopulateStateProvList(DropDownList, xmDocReturn)
{
	var newOpt = new Option("--- Selection ---",""); 
	DropDownList.options[DropDownList.options.length] = newOpt;
				
	if(xmDocReturn.getElementsByTagName("StateProvName").length > 0)
	{
		for(i = 0; i < xmDocReturn.getElementsByTagName("StateProvName").length; i++)
		{
			var sNodeValue = xmDocReturn.getElementsByTagName("StateProvName")[i].childNodes[0].nodeValue;
			
			if (sNodeValue == null || sNodeValue == "")
			{
				var newOpt = new Option(gSelectedCountry, gSelectedCountry);
				DropDownList.options[DropDownList.options.length] = newOpt;
			}
			else
			{
				var newOpt = new Option(sNodeValue, sNodeValue);
				DropDownList.options[DropDownList.options.length] = newOpt;
			}
		}
	}
	else
	{
		var newOpt = new Option(gSelectedCountry, gSelectedCountry);
		DropDownList.options[DropDownList.options.length] = newOpt;

	}

}

//Creating and setting the instance of appropriate XMLHTTP Request object  
function CreateXmlHttp()
{
	try
	{
		gXmlHttp = new ActiveXObject(GetProgID(["Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"]));
	}
	catch(Ex)
	{
		gXmlHttp = null;
	}
	
	if(!gXmlHttp && typeof XMLHttpRequest != "undefined") 
	{
		gXmlHttp = new XMLHttpRequest();
	}
	
}


function DisplayRemoteCallMsg(sMessageElement, sMessage)
{
	try
	{
		//return;
		var el = document.getElementById(sMessageElement);
		el.innerHTML = sMessage;
		//el.style.display = 'inline'
		el.style.display = 'block'
	}
	catch(ex)
	{/*trap*/}
}

function HideRemoteCallMsg(sMessageElement)
{
	try
	{
		//return;
		var el = document.getElementById(sMessageElement);
		el.style.display = 'none';
	}
	catch(ex)
	{/*trap*/}
}


	function GetProgID(IDs)
	{
		var blnFound = false;
		for(var i=0; i < IDs.length && !blnFound; i++)
		{
			try
			{
				var objDoc = new ActiveXObject(IDs[i]);
				ProdId = IDs[i];
				
				blnFound = true;
			}
			catch (objException)
			{
				// trap; try next progID
			}
		}
		if (!blnFound)
			throw "A valid progID not found: " + IDs[IDs.length-1] + ". (Exception: " + e + ")";
		IDs = null;
		return ProdId;
		
	}

