﻿// JScript File

var stateDropDownControl;
var countryDropDownControl;
var stateResponse = "";


function getStateOptions(stateControlName, countryControlName)
{
	stateDropDownControl = document.getElementById(stateControlName);
	var country = getCountry(countryControlName);
	
	if (country == "US" || country == "CA" || country == "AU")
	{
		stateDropDownControl.disabled = false;
		stateDropDownControl.style.backgroundColor = "white";
		getStates(country, processStateResponse);
	}
	else
	{
		if (stateDropDownControl.length > 0)
		{
			clearStateListBox(stateDropDownControl, stateDropDownControl.length);
			addNotListedItem(stateDropDownControl, "", "na");
			stateDropDownControl.disabled = true;
			stateDropDownControl.style.backgroundColor = "darkgray";
		}
		
		if (stateResponse != "")	// Clear out the state response back to "" since this country doesn't have anything
		{
			stateResponse = "";
		}
	}
}


function getCountryOption(stateControlName, countryControlName)
{
	countryDropDownControl = document.getElementById(countryControlName);
	var state = getState(stateControlName);
	
	if (state == "" || state == "na" || state == "AS" ||
		state == "FM" || state == "GU" || state == "MH" ||
		state == "MP" || state == "N/A" || state == "PW" || 
		state == "VI")
	{
		return;
	}
	else
	{
		getCountryServer(state, processCountryResponse);
	}
}


// Callback for the ajax method
function processStateResponse()
{
	if (xmlHTTP.readyState == 4)
	{
		stateResponse = xmlHTTP.responseText;
		generateStateOptions(stateResponse, stateDropDownControl);
	}
}


// Callback for the ajax method
function processCountryResponse()
{
	if (xmlHTTP.readyState == 4)
	{
		countryResponse = xmlHTTP.responseText;
		
		// Just loop through the options to find the country that matches the return
		for (var i = 0; i < countryDropDownControl.length; i++)
		{
			if (countryDropDownControl.options[i].value == countryResponse)
			{
				// Once we have a match select that country
				countryDropDownControl.selectedIndex = i;
				break;
			}
		}
	}
}


function popupKM(URL,wWidth,wHeight)
{
	myWindow = window.open(URL,'EstWin','toolbar=no,scrollbars=yes,location=no,statusbar=no,menubar=no,resizable=yes,width='+wWidth+',height='+wHeight+',left=50,top=50');
	myWindow.focus();
}


function checkForIreland(countryControlName, postalCodeControlName, cityMessageDivElementName)
{
	var country = getCountry(countryControlName);
	if (country == "IE")
	{
		disablePostalCode(postalCodeControlName, true);
		displayIrelandCityMessage(cityMessageDivElementName);
	}
	else if (country == "HK") // Added Hong Kong check since they don't have a zip code
	{
		disablePostalCode(postalCodeControlName, true);
		hideIrelandCityMessage(cityMessageDivElementName);
	}
	else
	{
		disablePostalCode(postalCodeControlName, false);
		hideIrelandCityMessage(cityMessageDivElementName);
	}
}


function displayIrelandCityMessage(cityMessageDivElementName)
{
	var cityDiv = document.getElementById(cityMessageDivElementName);
	cityDiv.style.display = "block";
}


function hideIrelandCityMessage(cityMessageDivElementName)
{
	var cityDiv = document.getElementById(cityMessageDivElementName);
	cityDiv.style.display = "none";
}


function disablePostalCode(postalCodeControlName, isDisabled)
{
	var postalCodeControl = document.getElementById(postalCodeControlName);
	postalCodeControl.disabled = isDisabled;
	
	if (isDisabled == true)
	{
		postalCodeControl.value = "";
		postalCodeControl.style.backgroundColor = "darkgray";
	}
	else
	{
		postalCodeControl.style.backgroundColor = "white";
	}
}


// this function trims leading and trailing spaces from the
// value property of any specified object
function TrimValue(str) {
  str = str.replace(/^\s*/, '').replace(/\s*$/, '');
  return str;
}


function Validate(theForm)
{
	var pstrCountry = theForm.DestinationCountryAbb.options[theForm.DestinationCountryAbb.selectedIndex].value;
	var pstrCity = theForm.CityDestination.value;
	var pstrZip = theForm.ZipDestination.value;
	
	if (TrimValue(pstrCity) == "")
	{
		alert("Please enter a City.");
		theForm.CityDestination.focus();
		return false;
	}

	if (TrimValue(pstrZip) == "" && pstrCountry != "IE" && pstrCountry != "HK")
	{
		alert("Please enter a ZIP code.");
		theForm.ZipDestination.focus();
		return false;
	}

	return true;
}


function clearMessage()
{
	var objWaitMessage;

	if (ns4)
		objWaitMessage=document.waitMessage;
	else if (ns6)
		objWaitMessage=document.getElementById("waitMessage").style;
	else if (ie4)
		objWaitMessage=document.all.waitMessage.style;

	if(ns4)
	{
		objWaitMessage.visibility="hidden";
	}
	else if (ns6||ie4)
	{
		objWaitMessage.display = "none";
	}
}


var mstrWaitMessage = '<center><h4>Please wait while we obtain your rates . . .</h4></center>';
var ns4=document.layers;
var ns6=document.getElementById && !document.all;
var ie4=document.all;

if (ns6 || ie4)
{
	document.write('<div id="waitMessage" style="display:">' + mstrWaitMessage + '</div>')
}
else if (ns4)
{
	document.write('<layer name="waitMessage" visibility="visible"><div id="waitMessage" style="display:">' + mstrWaitMessage + '</div></layer>')
}


