var g_oHttp = false;
var g_sStartElementId = "start";
var g_sEndElementId = "destination";
var g_sResultElementId = "result";
var g_sPostStartVarName = "start";
var g_sPostEndVarName = "destination";
var g_sServerSideScriptToCall = "ratelookupajax.php";
var g_sLoadingHtml = "<font class=\"loading\">Loading...</loading>";


var g_sStartLocation = "";
var g_sEndLocation = "";

// hide the submit button to make it clear that AJAX is in use!
window.addEventListener
	? window.addEventListener("load",HideSubmitButton,false)
	: window.attachEvent("onload",HideSubmitButton);

function HideSubmitButton()
{
    HideElement('submitbutton');
}

if (navigator.appName == "Microsoft Internet Explorer")
{
	var g_iMaxHide = 22;
	g_oHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else
{
	var g_iMaxHide = 20;
	g_oHttp = new XMLHttpRequest();
}

function ShowRate()
{
	var oStartPullDown = document.getElementById(g_sStartElementId);
	var oEndPullDown = document.getElementById(g_sEndElementId);

	// DO NOT include the default item (index=0) in the search
	for (i=1; i < oStartPullDown.options.length; i++)
	{
		if ((oStartPullDown.options[i].selected) && GetMyMax(i))
		{
			g_sStartLocation = oStartPullDown.options[i].text;
			break;	// exit the loop for efficiency
		}
	}

	// DO NOT include the default item (index=0) in the search
	for (i=1; i < oEndPullDown.options.length; i++)
	{
		if (GetMyMax(i) && (oEndPullDown.options[i].selected))
		{
			g_sEndLocation = oEndPullDown.options[i].text;
			break;  // exit the loop for efficiency
		}
	}

	if ((g_sStartLocation.length > 0) && (g_sEndLocation.length > 0))
	{
		document.getElementById(g_sResultElementId).innerHTML = g_sLoadingHtml;

		g_oHttp.abort();
		g_oHttp.open("GET", g_sServerSideScriptToCall + "?" + g_sPostStartVarName + "=" + g_sStartLocation + "&" + g_sPostEndVarName + "=" + g_sEndLocation, true);
		g_oHttp.onreadystatechange=function()
		{
			if(g_oHttp.readyState == 4)
			{
				document.getElementById(g_sResultElementId).innerHTML = g_oHttp.responseText;
			}
		}

		g_oHttp.send(null);
	}
	else
	{
		document.getElementById(g_sResultElementId).innerHTML = "";
	}
}

function GetMyMax(i)
{
   if (i > g_iMaxHide-11)
    {
		return true; // false
	}
	else
	{
		return true;
    }
}

function HideElement(oElementId)
{
	// safe function to hide an element with a specified ID
	if (document.getElementById)  // DOM3 = IE5, NS6
	{ 
		document.getElementById(oElementId).style.display = 'none';
	}
	else
	{
		if (document.layers) // Netscape 4
		{
			document.oElementId.display = 'none';
		}
		else  // IE 4
		{
			document.all.oElementId.style.display = 'none';
		}
	}
}
