var iCallID4;
var bCityOrPostalCodeChanged;
var AjaxMode = true;

function initializeWebServicePopIATA()
{
	if (AjaxMode == false)
	{
		try
		{
			if (!isFireFox())
			{
			//popIATA.useService("http://www.sysdma.com/wsUtilityEngine/eswUtilityEngine.asmx?WSDL","XMLFriendlyName4");
				popIATA.useService("http://www.sysdma.com/wsIATASearch/wsIATASearch.asmx?WSDL","XMLFriendlyName4");
			}
		}
		catch (e)
		{
			alert(e.message)
		}
	}
}
function updateChangeFlag()
{
		bCityOrPostalCodeChanged = true;
		try{ 
			if (isFireFox())
			{
				document.getElementById("lblVIATA").textContent = '';
			}
			else
			{
				document.getElementById("lblVIATA").innerText = '';
			}
		}
		 catch (e){}
}
function updateCustomChangeFlag()
{
	var anIATASelector = document.getElementById("cboIATASelector");
	if (anIATASelector.length == 0)
	{
		bCityOrPostalCodeChanged = true;
		try{ 
			if (isFireFox())
			{
				document.getElementById("lblVIATA").textContent = '';
			}
			else
			{
				document.getElementById("lblVIATA").innerText = '';
			}
		}
		 catch (e){}
	}
	else
	{
		alreadyAlert = false;
	}
}
function doPopIATA()
{
	var localProvince = document.getElementById("txtCANProvinces");
	var currentLoadedCountry = document.getElementById("cboCountry");
	if (localProvince == null)
	{
		bCityOrPostalCodeChanged = true;
	}
	try{ 
		if (!bCityOrPostalCodeChanged){return;}
		if (isFireFox())
		{
			document.getElementById("lblVIATA").textContent = '';
		}
		else
		{
			document.getElementById("lblVIATA").innerText = '';
		}
	}
	catch (e){}
	var currentLoadedCity = document.getElementById("txtCity");
	var currentLoadedPostalCodeValue;
	try
	{
		currentLoadedPostalCodeValue = igedit_getById("txtPostalCode").getValue();
		currentLoadedPostalCodeValue = currentLoadedPostalCodeValue.replace(/ /g,"");
		currentLoadedPostalCodeValue = currentLoadedPostalCodeValue.toUpperCase();
		igedit_getById("txtPostalCode").setValue(currentLoadedPostalCodeValue)
	}
	catch (e)
	{
		currentLoadedPostalCodeValue = document.getElementById("txtPostalCode").value;
		currentLoadedPostalCodeValue = currentLoadedPostalCodeValue.replace(/ /g,"");
		currentLoadedPostalCodeValue = currentLoadedPostalCodeValue.toUpperCase();
		document.getElementById("txtPostalCode").value = currentLoadedPostalCodeValue;
	}
	
	currentLoadedCity.value = currentLoadedCity.value.toUpperCase();
	if (currentLoadedPostalCodeValue !='' || currentLoadedCity.value !='')
	{
		var sCity = currentLoadedCity.value;
		sCity = sCity.replace(/'/g,"''");
		if (AjaxMode == false)
		{
			if (!isFireFox())
			{
				iCallID4 = popIATA.XMLFriendlyName4.callService(displayIATAResults1,"getIATACity2",currentLoadedCountry.value,currentLoadedPostalCodeValue,sCity);
			}
			else
			{
				var mySOAPCall = new SOAPCall();
				mySOAPCall.transportURI = "http://www.sysdma.com/wsIATASearch/wsIATASearch.asmx";
				mySOAPCall.actionURI = "http://www.sysdma.com/wsIATASearch/wsIATASearch/getIATACity2";
				var parameters = new Array();
				parameters[0] = new SOAPParameter(currentLoadedCountry.value,"Country");
				parameters[1] = new SOAPParameter(currentLoadedPostalCodeValue,"PostalCode");
				parameters[2] = new SOAPParameter(currentLoadedCity.value,"City");

				if (!mySOAPCall.verifySourceHeader)netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
				mySOAPCall.encode(0,"getIATACity2","http://www.sysdma.com/wsIATASearch/wsIATASearch",0,null,parameters.length,parameters);
				mySOAPCall.asyncInvoke(displayIATAResults2);
			}
		}
		else
		{
			var aResult; 
			aResult = DHLWebSite.ModAjaxEngine.GetIATACity(currentLoadedCountry.value,currentLoadedPostalCodeValue,sCity).value;
			displayAjaxIATAResults(aResult);		
		}

		bCityOrPostalCodeChanged = false;
	}
}
function doCustomPopIATA()
{
	var localProvince = document.getElementById("txtCANProvinces");
	var currentLoadedCountry = document.getElementById("cboCountry");
	var anIATASelector = document.getElementById("cboIATASelector");
	if (anIATASelector.length == 0)
	{
		if (localProvince == null)
		{
			bCityOrPostalCodeChanged = true;
		}
		try{ 
			if (!bCityOrPostalCodeChanged){return;}
			if (isFireFox())
			{
				document.getElementById("lblVIATA").textContent = '';
			}
			else
			{
				document.getElementById("lblVIATA").innerText = '';
			}
		}
		catch (e){}
		var currentLoadedCity = document.getElementById("txtCity");
		var currentLoadedPostalCodeValue;
		try
		{
			currentLoadedPostalCodeValue = igedit_getById("txtPostalCode").getValue();
		}
		catch (e)
		{
			currentLoadedPostalCodeValue = document.getElementById("txtPostalCode").value;
		}
		
		currentLoadedPostalCodeValue = currentLoadedPostalCodeValue.toUpperCase();
		currentLoadedCity.value = currentLoadedCity.value.toUpperCase();
		if (currentLoadedPostalCodeValue !='' || currentLoadedCity.value !='')
		{
			var sCity = currentLoadedCity.value;
			sCity = sCity.replace(/'/g,"''");
			if (AjaxMode == false)
			{
				if (!isFireFox())
				{
					iCallID4 = popIATA.XMLFriendlyName4.callService(displayIATAResults1,"getIATACity2",currentLoadedCountry.value,currentLoadedPostalCodeValue,sCity);
				}
				else
				{
					var mySOAPCall = new SOAPCall();
					mySOAPCall.transportURI = "http://www.sysdma.com/wsIATASearch/wsIATASearch.asmx";
					mySOAPCall.actionURI = "http://www.sysdma.com/wsIATASearch/wsIATASearch/getIATACity2";
					var parameters = new Array();
					parameters[0] = new SOAPParameter(currentLoadedCountry.value,"Country");
					parameters[1] = new SOAPParameter(currentLoadedPostalCode.value,"PostalCode");
					parameters[2] = new SOAPParameter(currentLoadedCity.value,"City");

					if (!mySOAPCall.verifySourceHeader)netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
					mySOAPCall.encode(0,"getIATACity2","http://www.sysdma.com/wsIATASearch/wsIATASearch",0,null,parameters.length,parameters);
					mySOAPCall.asyncInvoke(displayIATAResults2);
				}
			}
			else
			{
				displayAjaxIATAResults(DHLWebSite.ModAjaxEngine.GetIATACity(currentLoadedCountry.value,currentLoadedPostalCodeValue,sCity).value);		
			}

			bCityOrPostalCodeChanged = false;
		}
	}
	
}

function displayAjaxIATAResults(result)
{

	var aTempResultString = result;
	var a;
	var b;
	var c;
	var iIndexFoundCity  = -1;
	var i;
	var aIATASelector = document.getElementById("cboIATASelector");
	var currentLoadedCity = document.getElementById("txtCity");
	var currentLoadedProvince = document.getElementById("cboProvince");
	var alblIATA = document.getElementById("lblVIATA");
	var aProvinceCode = document.getElementById("txtProvince");
	var aCitySelector  = document.getElementById("citySelector");

	aIATASelector.length = 0;
	try{ 
		if (isFireFox())
		{
			document.getElementById("lblVIATA").textContent = '';
		}
		else
		{
			document.getElementById("lblVIATA").innerText = '';
		}
	}
	catch (e){}
	updateCurrentIATA('');
	if (aTempResultString != null && aTempResultString != '')
	{
		a = aTempResultString.split("|");
		aIATASelector.options[0] = new Option("Select One...","Select One...");
		aIATASelector.options[0].selected = true;
		if (a.length == 1)
		{
			c = a[0].split(";");
			b = c[0].split("=");
			aIATASelector.options[1] = new Option(c[0],c[0]);
			if (currentLoadedCity.value == '' || currentLoadedCity.value == b[1])
			{
				alreadyAlert = true;
				if (isFireFox())
				{
					alblIATA.textContent = b[0];
				}
				else
				{
					alblIATA.innerText = b[0];
				}
				setBigIATA(b[0]);
				updateCurrentIATA(b[0]);
				currentLoadedCity.value  = b[1];
				aIATASelector.options[1].selected = true;
				if (c[1] != '')
				{
					setDropDownListDefaultItem(currentLoadedProvince,c[1]);
					if (currentLoadedProvince.length > 0)
					{
						aProvinceCode.value = currentLoadedProvince.options[currentLoadedProvince.selectedIndex].value;
					}
				}
			}
		}
		else
		{
			for (i=0;i<a.length;i++)
			{
				c = a[i].split(";");
				b = c[0].split("=");
				aIATASelector.options[i+1] = new Option(c[0],c[0]);
				if (currentLoadedCity.value != '') 
				{
					if (currentLoadedCity.value == b[1])
					{ 
						alreadyAlert = true;
						if (isFireFox())
						{
							alblIATA.textContent = b[0];
						}
						else
						{
							alblIATA.innerText = b[0];
						}
						setBigIATA(b[0]);
						updateCurrentIATA(b[0]);
						currentLoadedCity.value  = b[1];
						aIATASelector.options[i+1].selected = true;
						if (c[1] != '')
						{
							setDropDownListDefaultItem(currentLoadedProvince,c[1]);
							if (currentLoadedProvince.length > 0)
							{
								aProvinceCode.value = currentLoadedProvince.options[currentLoadedProvince.selectedIndex].value;
							}
						}
					}
				}
			}
		}
		populateResultCity(aCitySelector,aTempResultString);
	}
	else
	{
		aIATASelector.options[0] = new Option("No IATA matched...","No IATA matched...");
	}
}	
function displayIATAResults1(result)
{

	var aTempResultString = HandleDisplayResults(result,iCallID4);
	var a;
	var b;
	var c;
	var iIndexFoundCity  = -1;
	var i;
	var aIATASelector = document.getElementById("cboIATASelector");
	var currentLoadedCity = document.getElementById("txtCity");
	var currentLoadedProvince = document.getElementById("cboProvince");
	var alblIATA = document.getElementById("lblVIATA");
	var aProvinceCode = document.getElementById("txtProvince");
	var aCitySelector  = document.getElementById("citySelector");

	aIATASelector.length = 0;
	try{ 
		if (isFireFox())
		{
			document.getElementById("lblVIATA").textContent = '';
		}
		else
		{
			document.getElementById("lblVIATA").innerText = '';
		}
	}
	catch (e){}
	updateCurrentIATA('');
	if (aTempResultString != null && aTempResultString != '')
	{
		a = aTempResultString.split("|");
		aIATASelector.options[0] = new Option("Select One...","Select One...");
		aIATASelector.options[0].selected = true;
		if (a.length == 1)
		{
			c = a[0].split(";");
			b = c[0].split("=");
			aIATASelector.options[1] = new Option(c[0],c[0]);
			if (currentLoadedCity.value == '' || currentLoadedCity.value.toUpperCase() == b[1].toUpperCase())
			{
				alreadyAlert = true;
				if (isFireFox())
				{
					alblIATA.textContent = b[0];
				}
				else
				{
					alblIATA.innerText = b[0];
				}
				setBigIATA(b[0]);
				updateCurrentIATA(b[0]);
				currentLoadedCity.value  = b[1];
				aIATASelector.options[1].selected = true;
				if (c[1] != '')
				{
					setDropDownListDefaultItem(currentLoadedProvince,c[1]);
					if (currentLoadedProvince.length > 0)
					{
						aProvinceCode.value = currentLoadedProvince.options[currentLoadedProvince.selectedIndex].value;
					}
				}
			}
		}
		else
		{
			for (i=0;i<a.length;i++)
			{
				c = a[i].split(";");
				b = c[0].split("=");
				aIATASelector.options[i+1] = new Option(c[0],c[0]);
				if (currentLoadedCity.value != '') 
				{
					if (currentLoadedCity.value.toUpperCase() == b[1].toUpperCase())
					{
						alreadyAlert = true;
						if (isFireFox())
						{
							alblIATA.textContent = b[0];
						}
						else
						{
							alblIATA.innerText = b[0];
						}
						setBigIATA(b[0]);
						updateCurrentIATA(b[0]);
						currentLoadedCity.value  = b[1];
						aIATASelector.options[i+1].selected = true;
						if (c[1] != '')
						{
							setDropDownListDefaultItem(currentLoadedProvince,c[1]);
							if (currentLoadedProvince.length > 0)
							{
								aProvinceCode.value = currentLoadedProvince.options[currentLoadedProvince.selectedIndex].value;
							}
						}
					}
				}
			}
			populateResultCity(aCitySelector,aTempResultString);
		}
	}
	else
	{
		aIATASelector.options[0] = new Option("No IATA matched...","No IATA matched...");
	}
}	
function displayIATAResults2(resp,call,status)
{

	var aTempResultString = resp.body.firstChild.firstChild.firstChild.data;
	var a;
	var b;
	var c;
	var iIndexFoundCity  = -1;
	var i;
	var aIATASelector = document.getElementById("cboIATASelector");
	var currentLoadedCity = document.getElementById("txtCity");
	var currentLoadedProvince = document.getElementById("cboProvince");
	var alblIATA = document.getElementById("lblVIATA");
	var aProvinceCode = document.getElementById("txtProvince");
	var aCitySelector  = document.getElementById("citySelector");

	aIATASelector.length = 0;
	if (isFireFox())
	{
		document.getElementById("lblVIATA").textContent = '';
	}
	else
	{
		document.getElementById("lblVIATA").innerText = '';
	}
	updateCurrentIATA('');
	if (aTempResultString != null && aTempResultString != '')
	{
		a = aTempResultString.split("|");
		aIATASelector.options[0] = new Option("Select One...","Select One...");
		aIATASelector.options[0].selected = true;
		if (a.length == 1)
		{
			c = a[0].split(";");
			b = c[0].split("=");
			aIATASelector.options[1] = new Option(c[0],c[0]);
			if (currentLoadedCity.value == '' || currentLoadedCity.value.toUpperCase() == b[1].toUpperCase())
			{
				alreadyAlert = true;
				if (isFireFox())
				{
					alblIATA.textContent = b[0];
				}
				else
				{
					alblIATA.innerText = b[0];
				}
				setBigIATA(b[0]);
				updateCurrentIATA(b[0]);
				currentLoadedCity.value  = b[1];
				aIATASelector.options[1].selected = true;
				if (c[1] != '')
				{
					setDropDownListDefaultItem(currentLoadedProvince,c[1]);
					if (currentLoadedProvince.length > 0)
					{
						aProvinceCode.value = currentLoadedProvince.options[currentLoadedProvince.selectedIndex].value;
					}
				}
			}
		}
		else
		{
			for (i=0;i<a.length;i++)
			{
				c = a[i].split(";");
				b = c[0].split("=");
				aIATASelector.options[i+1] = new Option(c[0],c[0]);
				if (currentLoadedCity.value != '') 
				{
					if (currentLoadedCity.value.toUpperCase() == b[1].toUpperCase())
					{
						alreadyAlert = true;
						if (isFireFox())
						{
							alblIATA.textContent = b[0];
						}
						else
						{
							alblIATA.innerText = b[0];
						}
						setBigIATA(b[0]);
						updateCurrentIATA(b[0]);
						currentLoadedCity.value  = b[1];
						aIATASelector.options[i+1].selected = true;
						if (c[1] != '')
						{
							setDropDownListDefaultItem(currentLoadedProvince,c[1]);
							if (currentLoadedProvince.length > 0)
							{
								aProvinceCode.value = currentLoadedProvince.options[currentLoadedProvince.selectedIndex].value;
							}
						}
					}
				}
			}
			populateResultCity(aCitySelector,aTempResultString);
		}
	}
	else
	{
		aIATASelector.options[0] = new Option("No IATA matched...","No IATA matched...");
	}
}	
function populateResultCity(aCityObj, aResult)
{
	var i;
	var b;
	var c;
	aCityObj.length = 0;
	var getCityName = aResult.split("|");
	for (i=0;i<getCityName.length;i++)
	{
		c = getCityName[i].split(";");
		b = c[0].split("=");
		aCityObj.options[i+1] = new Option(b[1],b[0]);
	}

}					
function doSelectIATA()
{
	var aIATASelector = document.getElementById("cboIATASelector");
	if (aIATASelector.selectedIndex > 0)
	{
		var b;
		var s = aIATASelector.value;
		b = s.split("=");
		if (isFireFox())
		{
			document.getElementById("lblVIATA").textContent = b[0];
		}
		else
		{
			document.getElementById("lblVIATA").innerText = b[0];
		}
		setBigIATA(b[0]);
		updateCurrentIATA(b[0]);
		document.getElementById("txtCity").value = b[1];
		bCityOrPostalCodeChanged = true;
		document.getElementById("cboIATASelector").length = 0;
		doCustomPopIATA();
	}
}

function checkIATA()
{
	try
	{
		var mConsigneeTelephone = igedit_getById("uwmPhone");
	}
	catch (e)
	{}
	var aCurrentCountry = document.getElementById("cboCountry");

	doPopIATA();
	
	try
	{
		setMaskForTelephoneByCode(mConsigneeTelephone, aCurrentCountry);
	}
	catch (e)
	{
	}
}
function customCheckIATA()
{
	try
	{
		var mConsigneeTelephone = igedit_getById("uwmPhone");
	}
	catch (e)
	{}
	var aProvinceCode = document.getElementById("txtProvince");
	var currentLoadedCity = document.getElementById("txtCity");
	var aCurrentCountry = document.getElementById("cboCountry");
	var aIATASelector = document.getElementById("cboIATASelector");
	var aLanguage = document.getElementById("btnLanguage");
	var currentLanguage =""
	if (aIATASelector.length == 0 || bCityOrPostalCodeChanged == true)
	{
		doPopIATA();
	}
	else
	{
		if (alreadyAlert == false)
		{
		
		   /*currentLanguage=aLanguage.parentElement.innerText
		   if (currentLanguage == "English"){
		      alert("La ville que vous avez indiquée n’existe pas dans notre table des destinations internationales. Si vous gardez cette même ville, le calculateur ne pourra pas vous fournir le prix de transport ni le délai d’acheminement. De plus, votre envoi pourrait subir des retards en cours de route.");
		   }
		   else{
				alert("The city you have specified does not exist in our global table. If you choose to use the city you have specified, we will not be able to calculate an estimated rate or transit time for your shipment. Your shipment may also experience routing/service delays.");
		   }*/
		   showErrorCityPrompt("ch05020ShowCityErrorMsg", "txt0505ShowCityErrorMsg")
		   alreadyAlert = true;
		}
	}
	try
	{
		setMaskForTelephoneByCode(mConsigneeTelephone, aCurrentCountry);
	}
	catch (e)
	{
	}
}
function resetConsigneeIATA()
{
	if (isFireFox())
	{
		document.getElementById("txtCurrentIATA").textContext = "";
		document.getElementById("lblVIATA").textContext = "";
	}
	else
	{
		document.getElementById("txtCurrentIATA").innerText = "";
		document.getElementById("lblVIATA").innerText = "";
	}
	document.getElementById("cboIATASelector").length = 0;
}
function updateCurrentIATA(v)
{
	try
	{
		document.getElementById("txtCurrentIATA").value = v;
	}
	catch (e)
	{
	}
}
function setBigIATA(v)
{
	try
	{
		if (isFireFox())
		{
			document.getElementById("_05020lblBigToIATA").textContent = v;
		}
		else
		{
			document.getElementById("_05020lblBigToIATA").innerText = v;
		}
	}
	catch (e)
	{
	}
}
function switchService(aServiceDDL)
{
	/*var aService = document.getElementById(aServiceDDL);
	var currentLoadedCountry = document.getElementById("cboCountry");
	
	if ((currentLoadedCountry.options.value != "CA") && (currentLoadedCountry.options.value != "US"))
	{
		setDropDownListDefaultItem(aService,"E");
	}*/
}
function setPostalCodeMask()
{
	var currentLoadedCountry = document.getElementById("cboCountry");
	var editor = igedit_getById("txtPostalCode");
	if (currentLoadedCountry.value == "CA")
	{
		editor.setValue('');
		editor.setInputMask(">?#?-#>?#");
	}
	else
	{
		editor.setValue('');
		editor.setInputMask("CCCCCCCCCC");
	}
}
