// AJAX specific functions
		
var _xmlHttp = null; // This is the XMLHttp Object...
var _timeoutAdjustment = 0; // timeout adjustment... gets adjusted over time...

// returns an XMLHttp object... gets it in an IE/Mozilla friendly way..
function getXMLHTTP()
{
	var oAX = null;
	try
	{
		oAX = new ActiveXObject("Msxml2.XMLHTTP")
	}
	catch(e)
	{
		try
		{
			oAX = new ActiveXObject("Microsoft.XMLHTTP")
		}
		catch(oc)
		{
			oAX = null
		}
	}
	if(!oAX && typeof XMLHttpRequest != "undefined")
	{
		oAX = new XMLHttpRequest()
	}
	return oAX
}
	
function doAJAXCall(sURL)
{
	if(_xmlHttp&&_xmlHttp.readyState != 0)
	{
		_xmlHttp.abort();
	}		
	
	_xmlHttp=getXMLHTTP();
		
	if(_xmlHttp)
	{
		_xmlHttp.open("GET", sURL, true);
		
		// Note that this function will ONLY be called when we get a complete response...
		_xmlHttp.onreadystatechange = function()
		{
			
			if(_xmlHttp.readyState == 4 && _xmlHttp.responseText)
			{
				
				//var frameElement=B;
				if(_xmlHttp.responseText.charAt(0) == "<")
				{
					_timeoutAdjustment--;
				}
				else
				{
				// The response text gets executed as javascript...
					//alert(_xmlHttp.responseText);
					eval(_xmlHttp.responseText);
				}
			}
		};
		
		// ARE YOU LOCAL?...
		// Comment out when running from a local file...
		_xmlHttp.send(null)
	}
}

// function that is called when the funder is changed...
function Calculate(iCalculatorID, bShowErrorMessages, e)
{
	//tell me what key was just pressed
	var code;
	if (!e) var e = window.event;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	//if it was a tab (a 9) then return - don't recalculate
	if (code == 9)
	{
		return;	
	}	
	
	sNameValuePairs = "msg=" + new Date().getTime().toString() + "&CalcID=" + iCalculatorID; 
	
	var aUnCheckedRadio = new Array();
	var aCheckedRadio = new Array();
	
	//find me all the inputs
	var aInputElements = document.getElementsByTagName('input');
	
	for(var i = 0; i < aInputElements.length; i++)
	{		
		if (!(aInputElements[i].name == undefined))
		{
			if (aInputElements[i].name.substring(0, 8) == 'CalcVar_')
			{
				if (aInputElements[i].type == 'radio')
				{
					if (aInputElements[i].checked)
					{
						aCheckedRadio.push(aInputElements[i].name);
						//loop through and remove all instances of commas
						var sValue = aInputElements[i].value;
						while (sValue.indexOf(",") != -1)
						{
							sValue = sValue.replace(",", "");
						}

						sNameValuePairs += "&" + aInputElements[i].name + "=" + sValue;						
					}
					else
					{
						if (!RadioExists(aUnCheckedRadio, aInputElements[i].name) && !RadioExists(aCheckedRadio, aInputElements[i].name))
						{
							aUnCheckedRadio.push(aInputElements[i].name);
						}
					}
				}
				else
				{
					//loop through and remove all instances of commas
					var sValue = aInputElements[i].value;
					while (sValue.indexOf(",") != -1)
					{
						sValue = sValue.replace(",", "");
					}
					
					sNameValuePairs += "&" + aInputElements[i].name + "=" + sValue;
				}
			}
		}
	}
	
	//repeat the process for selects (combo boxes)
	var aInputElements = document.getElementsByTagName('select');
	
	for(var i = 0; i < aInputElements.length; i++)
	{		
		if (!(aInputElements[i].name == undefined))
		{
			if (aInputElements[i].name.substring(0, 8) == 'CalcVar_')
			{
				if (aInputElements[i].type == 'radio')
				{
					if (aInputElements[i].checked)
					{
						aCheckedRadio.push(aInputElements[i].name);
						//loop through and remove all instances of commas
						var sValue = aInputElements[i].value;
						while (sValue.indexOf(",") != -1)
						{
							sValue = sValue.replace(",", "");
						}
						
						sNameValuePairs += "&" + aInputElements[i].name + "=" + sValue;
					}
					else
					{
						if (!RadioExists(aUnCheckedRadio, aInputElements[i].name) && !RadioExists(aCheckedRadio, aInputElements[i].name))
						{
							aUnCheckedRadio.push(aInputElements[i].name);
						}
					}
				}
				else
				{
					//loop through and remove all instances of commas
					var sValue = aInputElements[i].value;
					while (sValue.indexOf(",") != -1)
					{
						sValue = sValue.replace(",", "");
					}
						
					sNameValuePairs += "&" + aInputElements[i].name + "=" + sValue;
				}
			}
		}
	}
	
	
	for(i = 0; i < aUnCheckedRadio.length; i++)
	{
		if (!RadioExists(aCheckedRadio, aUnCheckedRadio[i]))
		{	
			sNameValuePairs += "&" + aUnCheckedRadio[i] + "=";
		}
	}	
	
	//finally add on whether the user has finished inputting text - if not, don't validate
	sNameValuePairs += "&ShowErrorMessages=" + bShowErrorMessages;
	
        //***************************************************************************
        //alert(SiteRoot + "/calculators/calculator-display.aspx?" + sNameValuePairs);	
        //natb 28 may 2007 - incite 26876 fixing javascript error
        //The following lines need to be swapped for the AJAX to work in different environments RP Nov08
                //This line is for Live
	doAJAXCall("/calculators/calculator-display.aspx?" + sNameValuePairs);
        //***************************************************************************
}

function RadioExists(aRadio, sRadioName)
{
	var bReturn = false;
	for(i = 0; i < aRadio.length; i++)
	{
		bReturn = (aRadio[i] == sRadioName)
	}
	return bReturn;
}