var blnOverrideValidation = false;   
var mstrCurrentPage = "";
var marrOnLoads = new Array();
var marrOnSubmits = new Array();
var marrOnSubmitted = new Array();
var marrDisplayMessages = new Array();

marrOnLoads.push(window.onload);
window.onload = INGDirectCommonOnLoad;

function $E(id)
{
	return document.getElementById(id);
}

function $V(id)
{
	if ($E(id))
		return $E(id).value;
	else
		return null;
}

function $U(control)
{
	control.value = control.value.toUpperCase();
}

function INGDirectCommonOnLoad()
{
	try
	{
		try
		{
			marrOnSubmits.push(window.document.forms[0].onsubmit);
			window.document.forms[0].onsubmit = INGDirectCommonOnSubmit;    
		} catch(ex) {}
				
		for(var i = 0; i < marrOnLoads.length; i++)
		{
			try
			{
				marrOnLoads[i]();
			} catch(ex) {}
		}			
	} 
	catch(e)
	{
		PublishError(e);
		return false;  
	}
}

function INGDirectCommonOnSubmit()
{
	try
	{				
		var blnReturn = true;
	
		for(var i = 0; i < marrOnSubmits.length; i++)
		{
			if(blnReturn)
			{
				try
				{
					blnReturn = marrOnSubmits[i]();
				} catch(ex) {}
			}
			else
				break;
		}
		
		if(blnReturn)
		{
			for(var i = 0; i < marrOnSubmitted.length; i++)
			{
				try
				{
					marrOnSubmitted[i]();
				} catch(ex) {}	
			}	
		}
						
		return blnReturn;
	} 
	catch(e)
	{
		PublishError(e);
		return false;  
	}
}

// This function can be overriden to provide custom client side display message by, for example,
// DisplayMessages = function(messages) { dhtmlDisplay(messages); }
function DisplayMessages(messages)
{
	for (var i = 0; i < messages.length; i++)
		alert(messages[i]);
}

marrOnLoads.push(DisplayClientMessages);

function DisplayClientMessages()
{
	DisplayMessages(marrDisplayMessages);
}
	
function SetCurrentPage(Page)
{
	mstrCurrentPage = Page;
}

function GetCurrentPage()
{
	return mstrCurrentPage;
}

function CheckPath(path)
{			
	var strDomain = document.domain.toString().toLowerCase();
	var strProtocol;
	var pos;
	
	if(strDomain == "localhost" || strDomain.indexOf("ws") >= 0)
		strProtocol = "http://";
	else
		strProtocol = "https://";
		
	if(strDomain == "ingdirect.com.au")
	{
		pos = document.location.toString().toLowerCase().indexOf("www");
		if(pos < 0)
		{
			if(path == '' || path == null || path == 'undefined')
				document.location.href = "http://www." + strDomain;
			else
				document.location.href = strProtocol + "www." + strDomain + path;
		}
	}
}

function BypassValidation(strCtrl)     
{		    
	try    
	{    
		blnOverrideValidation = true;    
		return true;    
	}     
	catch(e)    
	{    
		PublishError(e);
		return false;    
	}    
}    

function PublishError(e, Page)
{
	if(Page+"" == "undefined")
		Page = "abort.aspx";

	var objForm = document.createElement("form");    
	objForm.id = "ErrorForm";    
	objForm.method = "post";    
	objForm.action = Page;

	var objInupt1 = document.createElement("input");    
	objInupt1.name = "ErrorPage";
	objInupt1.id = "ErrorPage";
	try
	{
		objInupt1.type = "hidden";    
	} catch(e) {}
	objInupt1.value = window.document.location.href;
	
	var objInupt2 = document.createElement("input");    
	objInupt2.name = "ErrorMessage";
	objInupt2.id = "ErrorMessage";
	try
	{
		objInupt2.type = "hidden";    
	} catch(e) {}
	objInupt2.value = e.name + " - " + e.message;
		
	document.body.appendChild(objForm);  
	document.getElementById("ErrorForm").appendChild(objInupt1);
	document.getElementById("ErrorForm").appendChild(objInupt2);
	document.getElementById("ErrorForm").submit(); 
}

function ArrayAdd(ArrayToAdd, Value)
{
	var arrStub = new Array(ArrayToAdd.length + 1);
	for(var i = 0; i < ArrayToAdd.length; i++)
	{
		arrStub[i] = ArrayToAdd[i];
	}
	arrStub[ArrayToAdd.length] = Value;
	return arrStub;
}

function ArrayRemove(ArrayToRemove, Index)
{
	var arrStub = new Array();
	for(var i = 0; i < ArrayToRemove.length; i++)
	{
		if(i != Index)
			arrStub = ArrayAdd(arrStub, ArrayToRemove[i]);
	}
	return arrStub;
}

function ToInt(strInt)     
{    
	try    
	{    
		var intInt = 0;    
		if((!CheckNumeric(strInt)) && (strInt.length > 0))    
		{    
			var strLength = "";    
			for(var i = 0; i < strInt.length; i++)    
			{    
				strLength += "0";    
			}    
			var intInt = parseInt("1" + strInt, 10);    
			intInt -= parseInt("1" + strLength, 10);    
		}    
		return intInt;     
	}     
	catch(e)    
	{    
		PublishError(e);
		return false;    
	}    
}   

function fixup(f)     
{    
	try    
	{    
		if((f.name != "__VIEWSTATE") && (f.name != "__INGSUBMITIMAGEBUTTON"))
		{
			var s = f.value;    
			s = ltrim(s);    
			s = rtrim(s);    
			var a = s.charAt(0);    
			var b = a.toUpperCase();    
			if(b != a)     
			{    
				f.value = b + s.slice(1);    
				return;    
			}    
			f.value = s;
		}
	}     
	catch(e)    
	{    
		PublishError(e);
		return false;    
	}    
}    

function ltrim(String)     
{    
	try    
	{    
		if(String+"" == "undefined")
			String = "";
		while ((String.length > 0) && (String.charAt(0) == " "))    
			String = String.slice(1);    
		return String;     
	}     
	catch(e)    
	{    
		PublishError(e);
		return false;    
	}    
}	    

function rtrim(String)     
{    
	try    
	{    
		if(String+"" == "undefined")
			String = "";
		var i = String.length - 1;    
		while (i >= 0)     
		{    
			if(String.charAt(i) != " ")    
			{    
				String = String.substring(0, i+1);    
				break;    
			}    
			i--;    
		}    
		return String;     
	}     
	catch(e)    
	{    
		PublishError(e);
		return false;    
	}    
}    

function CheckNumeric(str)    
{    
	try    
	{    
		var len = str.length;    
		if(len == 0)    
			return true;    
		for(var z = 0; z < len; z++)     
		{    
				var ch = str.substring(z, z + 1)    
				if(((ch < "0") || ("9" < ch)))    
					return true;    
		}    
		return false;     
	}     
	catch(e)    
	{    
		PublishError(e);
		return false;    
	}    
}    

function NumericOnly(ev)    
{    
	try    
	{    
		var intKeyCode = 13;
		if(navigator.appName == "Netscape")
			intKeyCode = ev.which;
		else
			intKeyCode = window.event.keyCode;
		
		if(((intKeyCode < 48) || (intKeyCode > 57)) && (intKeyCode > 31) && (intKeyCode < 256))
			return false;     
		return true;    
	}     
	catch(e)    
	{    
		return true;    
	}    
}    

function setYear(objControl)
{
	var yr = eval(objControl.value);
	if(yr < 20)
	{
		yr += 2000;
	}	
	else
	{
		if(yr < 100)
			yr += 1900;
	}
	objControl.value = yr;
}

function NumberCount(strValue)
{
	var intNumberCount = 0;
	for(var i = 0; i < strValue.length; i++) 
	{
		var chrStub = strValue.substring(i, i + 1);
		if((chrStub >= "0") && (chrStub <= "9"))
			intNumberCount++;
	}
	return intNumberCount;
}

function AlphaCount(strValue)
{
	var intAlphaCount = 0;
	for(var i = 0; i < strValue.length; i++) 
	{
		var chrStub = strValue.substring(i, i + 1);
		if((chrStub < "0") || (chrStub > "9"))
			intAlphaCount++;
	}
	return intAlphaCount;
}

function IsAllZeros(strValue)
{
	var intIsAllZeros = 0;  
	for(var i = 0; i < strValue.length; i++) 
	{
		var chrStub = strValue.substring(i, i + 1);
		if((chrStub != "0"))
			intIsAllZeros++;
	}
	
	if(intIsAllZeros > 0)
		return false;
	else
		return true; 
}

function CallDefaultButton()
{
	try    
	{
		if(document.getElementById("__INGSUBMITIMAGEBUTTON") != null)
		{
			if(document.getElementById(mobjINGSubmitImageButtonID) != null)
			{
				document.getElementById("__INGSUBMITIMAGEBUTTON").value = mobjINGSubmitImageButtonID;
				try   
				{  
					if(document.forms[0].onsubmit())    
					{
						var objInupt1 = document.createElement("input");    
						objInupt1.name = document.getElementById("__INGSUBMITIMAGEBUTTON").value + ".x";    
						objInupt1.id = document.getElementById("__INGSUBMITIMAGEBUTTON").value + ".x";    
						try    
						{    
							objInupt1.type = "hidden";    
						} catch(e) {}    
						objInupt1.value = "1";    
						var objInupt2 = document.createElement("input");    
						objInupt2.name = document.getElementById("__INGSUBMITIMAGEBUTTON").value + ".y";    
						objInupt2.id = document.getElementById("__INGSUBMITIMAGEBUTTON").value + ".y";    
						try    
						{    
							objInupt2.type = "hidden";    
						} catch(e) {}    
						objInupt2.value = "1";    
						document.forms[0].appendChild(objInupt2)    
						document.forms[0].appendChild(objInupt1) 
						document.forms[0].submit();   
						return true;
					}
					else  
						document.getElementById("__INGSUBMITIMAGEBUTTON").value = "";  
				}  
				catch(e)  
				{ 
					document.forms[0].submit();   
					return true;
				} 
			}
		}
	} catch(e) {}  
	return false;
}

function Check_btnClose()
{
	try    
	{
		if(document.getElementById("__INGSUBMITIMAGEBUTTON").value == "btnClose")
			top.window.close();
		return false;
	}     
	catch(e)    
	{    
		PublishError(e);
		return false;    
	}   
}

function ChangeRadioDisplay(arrRadio, strDiv, strTextReq, display)
{
	if (display == null) display = 'block';
		
	try    
	{    
		for(var i = 0; i < arrRadio.length; i++)
		{
			if(i == arrRadio.length - 1)
			{
				if (strDiv != null)
					document.getElementById(strDiv).style.display = "none";
				if (strTextReq != null)
					document.getElementById(strTextReq).value = "False";
			}
			else
			{
				if(document.getElementById(arrRadio[i]).checked)
				{
					if (strDiv != null)
						document.getElementById(strDiv).style.display = display;
					if (strTextReq != null)
						document.getElementById(strTextReq).value = "True";
					break;
				}
			}
		}
	}     
	catch(e)    
	{    
		PublishError(e);
		return false;    
	}    
}
	
function Calculate(arrFields, strTotal)
{
	try    
	{
		var intReturn = 0;
		for(var i = 0; i < arrFields.length; i++)
		{
			if(! CheckNumeric(document.getElementById(arrFields[i]).value))
				intReturn += parseInt(document.getElementById(arrFields[i]).value, 10);
		}
		document.getElementById(strTotal).value = intReturn;
		return intReturn;
	}     
	catch(e)    
	{    
		PublishError(e);
		return false;    
	}  
}

function INGDirectValidationGetAttributeValue(objControl, AttributeName, DefaultValue)
{
	try
	{
		var strMessage = objControl[AttributeName];
		if((strMessage == null) || (strMessage == "") || (strMessage+"" == "undefined"))
			strMessage = objControl.getAttribute(AttributeName);
		if((strMessage == null) || (strMessage == "") || (strMessage+"" == "undefined"))
			strMessage = DefaultValue;
		return strMessage;
	} 
	catch(e)
	{
		return DefaultValue;  
	}
}

function INGDirectValidationSetAttributeValue(objControl, AttributeName, Value)
{
	try
	{
		objControl[AttributeName] = Value;
		if(INGDirectValidationGetAttributeValue(objControl, AttributeName, "") == "")
			objControl.setAttribute(AttributeName, Value);
	} catch(e) {}
}

function Trim(String)
{    
	try    
	{    
		while ((String.length > 0) && (String.charAt(0) == " "))    
			String = String.slice(1);    
			
		var i = String.length - 1;    
		while (i >= 0)     
		{    
			if(String.charAt(i) != " ")    
			{    
				String = String.substring(0, i+1);    
				break;    
			}    
			i--;    
		}   
		
		return String;     
	}     
	catch(e)    
	{    
		PublishError(e);
		return false;    
	}    
}	    

function HideDiv(objControl)
{
	try
	{
		document.getElementById(objControl).style.display = 'none';
	}
	catch(e)
	{
		PublishError(e);
		return false;
	}
}

function ShowDiv(objControl)
{
	try
	{
		document.getElementById(objControl).style.display = 'block';
	}
	catch(e)
	{
		PublishError(e);
		return false;
	}
}

function ConfirmCancel()
{
	return (confirm("Please confirm you wish to cancel this request"))
}

function ConfirmCancelAboutYou()
{
	return (confirm("Please confirm you wish to cancel this request. If you  want to continue at a later time, select Cancel and then Save your application."))
}


// checks whether the specified element is contained by an element with given id
function ContainsElement(containerId, element)
{
	if (!element) return false;
	if (!element.parentNode) return false;
	var curNode = element.parentNode;
	while (true)
	{
		if (curNode.id && curNode.id == containerId) return true;
		if (!curNode.parentNode) return false;
		if (curNode == curNode.parentNode) return false;
		curNode = curNode.parentNode;
	}
}

// gets all form elemens for give form id
// optional parameter containerId filters only elements which are nested inside the element with containerId
function GetFormElements(formId, containerId)
{
	var elems = new Array();
	var form = document.forms[0];
	if (formId != null) form = $E(formId);
	if (form.elements)
		for (var i = 0; i < form.elements.length; i++)
			if (containerId == null || ContainsElement(containerId, form.elements[i]))
				elems.push(form.elements[i]);
	return elems;
}

// clears elements of form
// optional containerId - only clears information inside element with contanerId
// optional applyTriggers - onclick, onchange functions will be invoked on applying items.
var clearing = false;
function ClearFormElements(containerId, applyTriggers, formId)
{
	if (clearing) return;
	try
	{
		clearing = true;
		var elems = GetFormElements(formId, containerId);
		for (var i = 0; i < elems.length; i++) {
			var elem = elems[i];
			if (elem.tagName)
			{
				if (elem.tagName.toLowerCase() == "input")
				{
					switch (elem.type.toLowerCase()) {
						case 'text':
						case 'hidden':
							elem.value = '';
						case 'radio':
						case 'checkbox':
							elem.checked = false;
					}
				}
				else if (elem.tagName.toLowerCase() == "select")
				{
					elem.selectedIndex = 0;
				}
				if (applyTriggers) {
					if (elem.onclick) elem.onclick();
					if (elem.onchange) elem.onchange();
				}
			}
		}
		clearing = false;
	}
	catch(e)
	{
		PublishError(e);
		return false;
	}
}

// hide section, clears all its form elements and update the required fields to false
// if no formId give, the first available form will be used
function HideSection(containerId, clearFields, formId)
{
	if (clearFields == null || clearFields) ClearFormElements(containerId, true, formId);
	var elems = GetFormElements(formId, containerId);
	var reqs = new Array();
	for (var i = 0; i < elems.length; i++)
	{
		if (INGDirectValidationGetAttributeValue(elems[i], 'Required', null) != null)
			reqs.push(INGDirectValidationGetAttributeValue(elems[i], 'Required', null));
	}
	for (var i = 0; i < arrDocumentAll.length; i++)
	{
		if (ContainsElement(containerId, $E(arrDocumentAll[i])) && INGDirectValidationGetAttributeValue($E(arrDocumentAll[i]), 'Required', null) != null)
			reqs.push(INGDirectValidationGetAttributeValue($E(arrDocumentAll[i]), 'Required', null));
	}
	for (var i = 0; i < reqs.length; i++)
		try { $E(reqs[i]).value = "False"; } catch (e) {}
	if ($E(containerId))
		$E(containerId).style.display = "none";
}

// shows a section and update the required fields to true
// if no formId give, the first available form will be used
function ShowSection(containerId, clearFields, formId)
{
	if (clearFields) ClearFormElements(containerId, true, formId);
	var elems = GetFormElements(formId, containerId);
	var reqs = new Array();
	for (var i = 0; i < elems.length; i++)
	{
		if (INGDirectValidationGetAttributeValue(elems[i], 'Required', null) != null)
			reqs.push(INGDirectValidationGetAttributeValue(elems[i], 'Required', null));
	}
	for (var i = 0; i < arrDocumentAll.length; i++)
	{
		if (ContainsElement(containerId, $E(arrDocumentAll[i])) && INGDirectValidationGetAttributeValue($E(arrDocumentAll[i]), 'Required', null) != null)
			reqs.push(INGDirectValidationGetAttributeValue($E(arrDocumentAll[i]), 'Required', null));
	}
	for (var i = 0; i < reqs.length; i++)
		try { $E(reqs[i]).value = "True"; } catch (e) {}
	if ($E(containerId))
		$E(containerId).style.display = "";
}

// changes visibility (and requiredness where necessary) of section based on radio selection
// arrShowOptions contains radio buttons that needs to be checked for section to be visible
function ChangeVisibilityFromRadio(arrShowOptions, containerId)
{
	var show = false;
	try    
	{    
		for (var i = 0; i < arrShowOptions.length; i++)
		{
			if ($E(arrShowOptions[i]).checked) show = true;			
		}
		
		if (show)
			ShowSection(containerId);
		else
			HideSection(containerId);
	}     
	catch(e)    
	{    
		PublishError(e);
		return false;    
	}   
}

function ChangeVisibility(visible, containerId) {
	if (visible)
		ShowSection(containerId);
	else
		HideSection(containerId);
}