
	var IsValid;
	var Errors;
	var xhttp = null;
	
	var focusField = "";
	
	function submitForm()
	{
		//window.alert("submitForm is called");
		if ( ValidateForm() )
		{	
			
			//Make xmlhttp working across browser
			//Checking if IE-specific document.all collectione exists
			//to see if we are running in IE
			if ( window.XMLHttpRequest )
			{
				xhttp = new XMLHttpRequest();
			}
			else if ( window.ActiveXObject )
			{
				xhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			
		    //hook the event handler
			xhttp.onreadystatechange = handleSubmitResult;
			var f = document.forms[0];
			
			//window.alert("CheckValidAmount is called");
			//window.alert("value is " + f.ctrlApply1_LoanAmount.value);
			xhttp.open("POST","/Artog/MortgageObserver.ashx" ,true);
			
			//Disable the submit button
			f.btnEarnMore.disabled = true;			
			
			var isBC = 0
			if(f.ctrlApply1_isBadCredit_0.checked){
				isBC = 1;			
			}else{
				isBC = 0;			
			}
			
			xhttp.send( "LoanAmount=" + escape(f.ctrlApply1_LoanAmount.value) + "&LoanPurpose=" + escape(f.ctrlApply1_LoanPurpose.value) + "&LoanType=" + escape(f.ctrlApply1_LoanType.value) + "&FirstName=" + escape(f.ctrlApply1_FirstName.value) + "&LastName=" + escape(f.ctrlApply1_LastName.value) + "&Phone=" + escape(f.ctrlApply1_Phone.value) + "&Mobile=" + escape(f.ctrlApply1_Mobile.value) + "&WorkNumber=" + escape(f.ctrlApply1_WorkPhone.value) + "&PostCode=" + escape(f.ctrlApply1_PostCode.value) + "&Email=" + escape(f.ctrlApply1_Email.value) + "&CurrentFormName=" + escape(f.ctrlHeader_CurrentFormName.value) + "&FormGuid=" + escape(f.ctrlHeader_FormGuid.value) + "&BadCredit=" + escape(isBC) + "&Comments=" + escape(f.ctrlApply1_Comments.value)  );

		}
	}
	
	
	//Asynchrnous event handler
	function handleSubmitResult()
	{
		//window.alert("xhttp status " + xhttp.readyState);
		// Complete
		if (xhttp.readyState == 4) 
		{
			if ( xhttp.status == 200 )
				{
					// XML Schema
					// <Response><requestid></requestid><trackingValue></trackingValue><trackingDomain></trackingDomain>
					// </Response>
					//Processing statement for xml returned back, I am talking about generic parsing for all the browsers
					//window.alert("inside");
					var response = xhttp.responseXML.documentElement;
					//window.alert( response.getElementsByTagName('loanAmount')[0].firstChild.data );
					var requestID = response.getElementsByTagName('requestid')[0].firstChild.data;
					//window.alert( requestID );
					var trackingValue = response.getElementsByTagName('trackingValue')[0].firstChild.data;
					var trackingDomain = response.getElementsByTagName('trackingDomain')[0].firstChild.data;
					var f = document.forms[0];
					var guidName = response.getElementsByTagName('MortgageFormGuid')[0].firstChild.data;
					//window.alert(guidName);
					var previousFormName = "";
					//window.alert("orevuiy" + previousFormName);
					var currentFormName = "ApplyStep1";
				
					trackingValue = f.ctrlHeader_CurrentFormName.value + "###" + f.ctrlHeader_FormGuid.value;
									
					agent_set_cookieByDomain( "ARTOG_STATE", trackingValue,1,trackingDomain);
					if ( requestID != "FAILED" )
					{
						window.location.href = "/Artog/Confirmation.aspx?uid=" + requestID + "&guidName=" + f.ctrlHeader_FormGuid.value + "&previousFormName=" + previousFormName + "&currentFormName=" + currentFormName ;
					}
					else if ( requestID == "CACHED" )
					{
						window.location.href = "/Artog/applystep1.aspx";
					}
					
				}			
		}
	}
	
	function ValidateForm(){
	
		try{
			var f = document.forms[0];
			IsValid = true;
			Errors = "";
			
			focusField = "";
			
			//do the loan amount field range validation here first, in case the blur event was missed
			//if (CheckValidAmount() == false)	{ return false; }
			
			ValidateRequiredField(f.ctrlApply1_LoanAmount, 'Loan amount');
			ValidateSelectedField(f.ctrlApply1_LoanPurpose, 'purpose of loan');
			ValidateSelectedField(f.ctrlApply1_LoanType, 'type of loan');
			
			ValidateRequiredField(f.ctrlApply1_FirstName, 'First name');
			ValidateRequiredField(f.ctrlApply1_LastName, 'Last name');
			ValidateRequiredField(f.ctrlApply1_Phone, 'Phone number');
			//ValidateRequiredField(f.ctrlApply1_Mobile, 'Mobile number');
			ValidateRequiredField(f.ctrlApply1_PostCode, 'Postcode');
			ValidateRequiredField(f.ctrlApply1_Email, 'Email');
		
			if(!f.ctrlApply1_isBadCredit_0.checked && !f.ctrlApply1_isBadCredit_1.checked){
				Errors = Errors + "\nPlease fill in required information [Would you like to speak to a bad credit expert]";
				IsValid = false;
			}		
			
			ValidateRequiredField(f.ctrlApply1_Comments, 'Additional details');
			
			
			if(f.ctrlApply1_Phone.value!="" && f.ctrlApply1_Phone.value.length<10){
				Errors = Errors + "\nPlease enter a valid Australian phone number including area code";
				IsValid = false;
			}
			if ( !ValidMobilePhone(f.ctrlApply1_Mobile.value) )
			{
				Errors = Errors + "\nPlease enter a valid Australian mobile phone number";
				IsValid = false;
			}
			
			if(f.ctrlApply1_WorkPhone.value!="" && f.ctrlApply1_WorkPhone.value.length<10){
				Errors = Errors + "\nPlease enter a valid Work phone number";
				IsValid = false;
			}
			if(f.ctrlApply1_PostCode.value!="" && f.ctrlApply1_PostCode.value.length<3){
				Errors = Errors + "\nPlease enter a valid postcode ";
				IsValid = false;
			}			
			if(f.ctrlApply1_Email.value!="" && !ValidEmail(f.ctrlApply1_Email.value)){
				Errors = Errors + "\nPlease enter valid Email Address ";
				IsValid = false;
			}
			
			
			if(Errors.length>0){
				alert(Errors);	
				if(focusField !=  "")
				{
					focusField.focus();
				}
			}
			return IsValid;
		}catch(e){
			alert("Error in ValidateSubFormEdit ["+e.Message+"]");
			return false;
		}
	}
	
	function IsValidLoanAmount(){
		var f = document.forms[0];
		IsInteger();
	}
		
	function ValidateRequiredField(Field, FieldDisplayName){
		if(Field.value==""){
			Errors = Errors + "\nPlease fill in required information [" + FieldDisplayName + "]";
			if(focusField ==  "")
			{
				focusField = Field;
			}
			IsValid = false;
		}
	}
	
	function ValidateSelectedField(Field, FieldDisplayName){
		if( Field.value==""){
			Errors = Errors + "\nPlease select your preferred " + FieldDisplayName;
			if(focusField ==  "")
			{
				focusField = Field;
			}
			IsValid = false;
		}
	}
	
	function popupLenderList()
	{
		var f = document.forms[0];
	//	window.alert("value:" + f.ctrlApply1_LoanAmount.value);
		PopUpLink("/Artog/LendersRefund.aspx?RequestAmount=" + f.ctrlApply1_LoanAmount.value , 700, 720, "LendersWillRefund");
	}
	
	
	//Developed by:James
	//Change this method
	//Instead of causing a postback which triggers double loading in the container wierdly
	//Using AJAX to create a page without postback (of course except the submit button )
	function CheckValidAmount(pageLoad) {
			
			//Make xmlhttp working across browser
			//Checking if IE-specific document.all collectione exists
			//to see if we are running in IE
			if ( window.XMLHttpRequest )
			{
				xhttp = new XMLHttpRequest();
			}
			else if ( window.ActiveXObject )
			{
				xhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			
			//hook the event handler
			xhttp.onreadystatechange = validateAmountResult;
			var f = document.forms[0];


			if ( pageLoad )
			{
				xhttp.open("GET","/Artog/MortgageObserver.ashx?pageLoad=true&amount=" + f.ctrlApply1_LoanAmount.value + "&loanpurpose=" ,true);
			
			}
			else
			{
				xhttp.open("GET","/Artog/MortgageObserver.ashx?pageLoad=false&amount=" + f.ctrlApply1_LoanAmount.value + "&loanpurpose=" ,true);
			}
			if ( window.XMLHttpRequest )
			{
				xhttp.send(null);
			}
			else if ( window.ActiveXObject )
			{
				xhttp.send();
			}
			
			if ( !pageLoad )
			{
				DisplayAmountWarning();
			}

	}
	
	//Asynchrnous event handler
	function validateAmountResult()
	{
		//window.alert("xhttp status " + xhttp.readyState);
		//1 loading
		//2 loaded
		//3 interactive
		//4 complete
		// Complete
		if (xhttp.readyState == 4) 
		{
			if ( xhttp.status == 200 )
			{
				var f = document.forms[0];
				//bypass
				if(false)
				{
					
					//Processing statement for xml returned back, I am talking about generic parsing for all the browsers
					var response = xhttp.responseXML.documentElement;
					//window.alert( response.getElementsByTagName('loanAmount')[0].firstChild.data );
					var loanAmount = response.getElementsByTagName('loanAmount')[0].firstChild.data;
					//window.alert('loanAmount:' + loanAmount );
					var Default = response.getElementsByTagName('Default')[0].firstChild.data;
					var DefaultAmount = response.getElementsByTagName('DefaultAmount')[0].firstChild.data;		
					var pageLoad = response.getElementsByTagName('pageLoad')[0].firstChild.data;
					var ArtogWillPayValue = response.getElementsByTagName('ArtogWillPay')[0].firstChild.data;
					var earnCashBack = response.getElementsByTagName('earnCashBack')[0].firstChild.data;
					var lpg = response.getElementsByTagName('lpg')[0].firstChild.data;
						
					
					//ctrlApply1_LoanAmount
					f.ctrlApply1_LoanAmount.value = loanAmount;
					// put default amount , hide "lpg" show "Vary loan amount"
					var artogWillPayHolder = document.getElementById("ctrlApply1_ArtogWillPay");
					artogWillPayHolder.innerHTML = "<label class='a_head3Yellow'>" + ArtogWillPayValue + "</label>";
					
					
					if ( Default == "yes" )
					{
					
						var objectlm = document.getElementById("lendermarketing");
						objectlm.style.display = 'none';
					
						var objectlnm = document.getElementById("lendernomarketing");
						objectlnm.style.display = 'block';
						
					}
					else
					{

					
						var objectlm = document.getElementById("lendermarketing");
						objectlm.style.display = 'block';
					
						var objectlnm = document.getElementById("lendernomarketing");
						objectlnm.style.display = 'none';
					}
					
					
					if ( pageLoad != "true" )
					{
						var objectEarn = document.getElementById("VaryLoanAmount");
						//objectEarn.style.visibility = 'visible';
						objectEarn.style.display = 'none';
					
						var objectLPG = document.getElementById("lpg");
						//objectLPG.style.visibility = 'hidden';
						objectLPG.style.display = 'block';
					}			
				}
				CheckRedirectAmount(f.ctrlApply1_LoanAmount);
			
			}
		}
	}
	
	function DisplayAmountWarning()
	{
		var f = document.forms[0];
	
		//read in values
		var intLowAmt = f.ctrlApply1_warningLow.value;
		var intHighAmt = f.ctrlApply1_warningHigh.value;
		var strMessage = f.ctrlApply1_warningMessage.value;
	
		if (ValidNumRange(f.ctrlApply1_LoanAmount.value,intLowAmt,intHighAmt) && strMessage!= "")
		{
			alert(strMessage);
		}
	}
	
	function CheckRedirectAmount(Field) {
		var minAmt = 1;
		var maxAmt = 99999;
		var fieldVal = Field.value;
		var fromApp = "Mortgage";
		var redirURL = "/ArtogPersonalLoan/ApplyStep1.aspx?loanAmount=" + fieldVal + "&loanPurpose=" + fromApp
		if (ValidNumRange(fieldVal,minAmt,maxAmt) == true)
		{
			var response=confirm("Artog - Mortgage\n\nFor loan amounts of this size, click 'OK' to submit your enquiry through our personal loans section. Alternatively click 'Cancel' to change the loan amount.");
			if (response==true)
			{
				window.location.href=redirURL;
				return false;
			}
			else
			{	
				Field.value="";
				Field.focus();
				return false;
			}
		}
		return true;
	}
	
	


