/**************************************************************************************/
// Functions used in the IIB calculators
/**************************************************************************************/
var bFullValidation =  false;


function IsALetter(c)
{
	return (((c>="a") && (c<="z")) || ((c>= "A") && (c<= "Z")))
}

function IsDigit(c)
{
	return ((c>="0") && (c<="9"))
}

function IsNumeric (oForm, oField)
{
	if(isNaN(document.forms[oForm].elements[oField].value))
	{
		alert("Please enter a valid amount");
		document.forms[oForm].elements[oField].focus();
	}
	return;
}


/**************************************************************************************/
//the following functions are used in the Approval in Principle calculator input page
/**************************************************************************************/

function VerifyApprPrincipleData(nMaxPerCentage)
	{		
		var nLoanAmt=0;
		var nMaxAmt=0;
		
						
		//var nMaxPerCentage=.92;
		
        
		//calculate max loan amount and round to two places of decimal
        nMaxAmt = document.ApprPrinciple.edHousePrice.value * nMaxPerCentage;
        nMaxAmt = Math.round((nMaxAmt) * Math.pow (10, 2))
        nMaxAmt =  nMaxAmt / Math.pow (10, 2);
    
        /*
		if (document.ApprPrinciple.edHousePrice.value == "") 		
		{
			alert("Please enter House Price");
			document.ApprPrinciple.edHousePrice.focus();
			return false;
		}

		if(isNaN(document.ApprPrinciple.edHousePrice.value))
		{
			alert("Please enter a valid amount for the House Price");
			document.ApprPrinciple.edHousePrice.focus();
			return false;
		}
        */
        
		if(document.ApprPrinciple.edHousePrice.value != "" && isNaN(document.ApprPrinciple.edHousePrice.value))
		{
			alert("Please enter a valid amount for the House Price");
			document.ApprPrinciple.edHousePrice.focus();
			return false;
		}
        
        
		//edLoanAmount
		if (document.ApprPrinciple.edLoanAmount.value == "") 		
		{
			alert("Please enter Loan Amount");
			document.ApprPrinciple.edLoanAmount.focus();
			return false;
		}

		if(isNaN(document.ApprPrinciple.edLoanAmount.value))
		{
			alert("Please enter a valid amount for the Loan Amount");
			document.ApprPrinciple.edLoanAmount.focus();
			return false;
		}

		//max loan amount is 92% of house value
		if (nMaxAmt >0){
		    nLoanAmt=Math.round(document.ApprPrinciple.edLoanAmount.value)				
		    if (nLoanAmt > nMaxAmt) 		
		    {
			    alert("The maximum loan amount you can request is "
			    +nMaxAmt);
			    document.ApprPrinciple.edLoanAmount.value = nMaxAmt;
			    document.ApprPrinciple.edLoanAmount.focus();
			    return false;
		    }
		 }

		//edSalarySole
		if (document.ApprPrinciple.edSalarySole.value == "") 		
		{
			alert("Please enter Basic Salary");
			document.ApprPrinciple.edSalarySole.focus();
			return false;
		}

        
		//contact name
		if (bFullValidation && document.ApprPrinciple.edName.value == "") 		
		{
			alert("Please enter a contact name");
			document.ApprPrinciple.edName.focus();
			return false;
		}
    	
    	
    	if (bFullValidation)
    	{
			//check it's a valid name
			var sName = ""
			sName = document.ApprPrinciple.edName.value
			if (sName != ""){
				for( i=0;i< sName.length; i ++)
				{
					//see that current character is a letter
					var c = sName.charAt(i);
					if (!IsALetter(c) && c!=" " && c!="'")
					{
						alert("Please enter a valid contact name.");
						document.ApprPrinciple.edName.focus();
						return false;
					}
				}		
			}

			//contact address
			if (document.ApprPrinciple.edAddLine1.value == "" && bFullValidation) 		
			{
				alert("Please enter a contact address");
				document.ApprPrinciple.edAddLine1.focus();
				return false;
			}
	    
			//contact phone
			if (document.ApprPrinciple.edPhone.value == ""  && bFullValidation) 		
			{
				alert("Please enter a contact phone number");
				document.ApprPrinciple.edPhone.focus();
				return false;
			}

			if(document.ApprPrinciple.edPhone.value != "" && isNaN(document.ApprPrinciple.edPhone.value))
			{
				alert("Please enter a valid phone number");
				document.ApprPrinciple.edPhone.focus();
				return false;
			}

			//occupation name
			if (document.ApprPrinciple.cbEmployTypeS.value != "Unemployed") 		
			{
				if (document.ApprPrinciple.edOccupationSole.value == "" && bFullValidation) 		
				{
					alert("Please enter your occupation");
					document.ApprPrinciple.edOccupationSole.focus();
					return false;
				}
			}	
	    
			//employers name
			if (document.ApprPrinciple.cbEmployTypeS.value != "Unemployed") 		
			{
				if (document.ApprPrinciple.edEmployerSole.value == "" && bFullValidation) 		
				{
					alert("Please enter employer's name");
					document.ApprPrinciple.edEmployerSole.focus();
					return false;
				}
			}

			//mobile number
			if (document.ApprPrinciple.edMobile.value != "") 		
			{
				if(isNaN(document.ApprPrinciple.edMobile.value))
				{
					alert("Please enter a valid mobile phone number");
					document.ApprPrinciple.edMobile.focus();
					return false;
				}
			}
	           

			if(isNaN(document.ApprPrinciple.edSalarySole.value))
			{
				alert("Please enter a valid amount for Basic Salary");
				document.ApprPrinciple.edSalarySole.focus();
				return false;
			}

			//edOvertimeSole
			if(isNaN(document.ApprPrinciple.edOvertimeSole.value))
			{
				alert("Please enter a valid amount for the Overtime Income");
				document.ApprPrinciple.edOvertimeSole.focus();
				return false;
			}

			//edBonusSole
			if(isNaN(document.ApprPrinciple.edBonusSole.value))
			{
				alert("Please enter a valid amount for Bonus Income");
				document.ApprPrinciple.edBonusSole.focus();
				return false;
			}

			//edCommSole
			if(isNaN(document.ApprPrinciple.edCommSole.value))
			{
				alert("Please enter a valid amount for Commission Income");
				document.ApprPrinciple.edCommSole.focus();
				return false;
			}

			//edOtherSole
			if(isNaN(document.ApprPrinciple.edOtherSole.value))
			{
				alert("Please enter a valid amount for Other Income");
				document.ApprPrinciple.edOtherSole.focus();
				return false;
			}

	//joint applicant
			//occupation name
			if (document.ApprPrinciple.edSalaryJoint.value != "") 		
			{
				if (document.ApprPrinciple.cbEmployTypeJ.value != "Unemployed") 		
				{
					if (document.ApprPrinciple.edOccupationJoint.value == "" && bFullValidation) 		
					{
						alert("Please enter your occupation");
						document.ApprPrinciple.edOccupationJoint.focus();
						return false;
					}
				}
			}
			
			//employers name
			if (document.ApprPrinciple.edSalaryJoint.value != "") 		
			{
				if (document.ApprPrinciple.cbEmployTypeJ.value != "Unemployed") 		
				{
					if (document.ApprPrinciple.edEmployerJoint.value == "" && bFullValidation) 		
					{
						alert("Please enter employer's name");
						document.ApprPrinciple.edEmployerJoint.focus();
						return false;
					}
				}
			}

			//edSalaryJoint
			if(isNaN(document.ApprPrinciple.edSalaryJoint.value))
			{
				alert("Please enter a valid amount for Basic Salary");
				document.ApprPrinciple.edSalaryJoint.focus();
				return false;
			}

			//edOvertimeJoint
			if(isNaN(document.ApprPrinciple.edOvertimeJoint.value))
			{
				alert("Please enter a valid amount for the Overtime Income");
				document.ApprPrinciple.edOvertimeJoint.focus();
				return false;
			}

			//edBonusJoint
			if(isNaN(document.ApprPrinciple.edBonusJoint.value))
			{
				alert("Please enter a valid amount for Bonus Income");
				document.ApprPrinciple.edBonusJoint.focus();
				return false;
			}

			//edCommJoint
			if(isNaN(document.ApprPrinciple.edCommJoint.value))
			{
				alert("Please enter a valid amount for Commission Income");
				document.ApprPrinciple.edCommJoint.focus();
				return false;
			}

			//edOtherJoint
			if(isNaN(document.ApprPrinciple.edOtherJoint.value))
			{
				alert("Please enter a valid amount for Other Income");
				document.ApprPrinciple.edOtherJoint.focus();
				return false;
			}
		}
		return true;
	}


/**************************************************************************************/
//the following functions are used in the Car loan quick quote input page
/**************************************************************************************/
function VerifyCarQuoteData()
	{		
		var nLoanAmt=0;
		var nDeposit=0;
				
		nLoanAmt=Math.round(document.CarQuote.edCarPrice.value)
		nDeposit=Math.round(document.CarQuote.edDeposit.value)
				
		if (nLoanAmt == "")  		
		{
			alert("Please enter a loan amount");
			document.CarQuote.edCarPrice.focus();
			return false;
		}

		if(isNaN(nLoanAmt))
		{
			alert("Please enter a valid loan amount");
			document.CarQuote.edCarPrice.focus();
			return false;
		}

		if(nDeposit != "")
			if(isNaN(nDeposit))
			{
				alert("Please enter a valid deposit amount");
				document.CarQuote.edDeposit.focus();
				return false;
			}
		
		return true;
	}

/**************************************************************************************/
//end of functions used in the Car loan quick quote calculator
/**************************************************************************************/


/**************************************************************************************/
//The following functions are used in the Commercial Mortgage calculator
/**************************************************************************************/

function VerifyCommMortgageData()
	{		
		var NP = 0;
		var IOP = 0;
		
		//edLoanReqd
		if (document.CommMortgage.edLoanReqd.value == "") 
		{
			alert("Please enter a Loan Amount");
			document.CommMortgage.edLoanReqd.focus();
			return false;
		}

		
			//check it's a valid name
		var sLoanAmt = ""
		sLoanAmt = document.CommMortgage.edLoanReqd.value
		for( i=0;i< sLoanAmt.length; i ++)
		{
			//see that current character is a letter
			var c = sLoanAmt.charAt(i);
			if (!IsDigit(c))
			{
				alert("Please enter a valid Loan Amount");
				document.CommMortgage.edLoanReqd.focus();
				return false;
			}
		}
	
		if (document.CommMortgage.cbInterest.value == "Yes") 
		{
			if (document.CommMortgage.cbIntOnlyPeriod.value == "") 
			{
				alert("Please select the Interest Only Period");
				document.CommMortgage.cbIntOnlyPeriod.focus();
				return false;
			}
		}
		
		if (document.CommMortgage.cbInterest.value == "Yes") 
		{
			NP = Math.round(document.CommMortgage.lbLoanTerm.value);
			NP = NP * 12;
			IOP = Math.round(document.CommMortgage.cbIntOnlyPeriod.value);
		
			if (IOP > NP)
			{
				alert("The Interest Only Period must be less than the Loan Term");
				document.CommMortgage.cbIntOnlyPeriod.focus();
				return false;
			}
		}	
		

		return true;
	}
		

function SetIntOnly()
{
//	If interest only is 'no' then clear the interest only period text box
	if (document.CommMortgage.cbInterest.value == "No") 
	{
		document.CommMortgage.cbIntOnlyPeriod.value = ""; 	
		document.CommMortgage.cbIntOnlyPeriod.disabled= true;
	}
	else
		document.CommMortgage.cbIntOnlyPeriod.disabled= false; 	
	return;
}

/**************************************************************************************/
//end of functions used in the Commercial Mortgage calculator
/**************************************************************************************/



/**************************************************************************************/
//The following functions are used in the Quick Quote Homeloan calculator
/**************************************************************************************/


function CalcMaxLoan(nMaxLoanPC)
{
    
	var nLoanAmount = 0;
	var nMaxLoanAmount = 0;
		
	if (nMaxLoanPC == null)
		nMaxLoanPC = .92;
		
	//nLoanAmount = document.QuickQuoteHL.edHousePrice.value;
	nLoanAmount = (getObj ('edHousePrice')).value;
	
	if (nLoanAmount > 0)
	{                        
		//calculate max loan and round to two places of decimal
        nMaxLoanAmount =(getObj ('edHousePrice')).value * nMaxLoanPC;
        nMaxLoanAmount = Math.round((nMaxLoanAmount) * Math.pow (10, 2));                
        (getObj ('edMaxLoan')).value =  nMaxLoanAmount / Math.pow (10, 2);
    }
    
}

function VerifyQQHLData()
	{		
		var nLoanAmt=0;
		var nMaxAmt=0;
		
		//edHousePrice
		if (document.QuickQuoteHL.edHousePrice.value == "") 		
		{
			alert("Please enter the House Price");
			document.QuickQuoteHL.edHousePrice.focus();
			return false;
		}
	
		if(isNaN(document.QuickQuoteHL.edHousePrice.value))
		{
			alert("Please enter a valid House Price");
			document.QuickQuoteHL.edHousePrice.focus();
			return false;
		}

		//edLoanAmount
		if (document.QuickQuoteHL.edLoanAmount.value == "") 		
		{
			alert("Please enter Loan Amount Required");
			document.QuickQuoteHL.edLoanAmount.focus();
			return false;
		}
	
		if(isNaN(document.QuickQuoteHL.edLoanAmount.value))
		{
			alert("Please enter a valid Loan Amount");
			document.QuickQuoteHL.edLoanAmount.focus();
			return false;
		}

		nLoanAmt=Math.round(document.QuickQuoteHL.edLoanAmount.value)
		nMaxAmt=Math.round(document.QuickQuoteHL.edMaxLoan.value)
				
		if (nLoanAmt > nMaxAmt) 		
		{
			alert ("The maximum loan you can request is " + nMaxAmt);
			document.QuickQuoteHL.edLoanAmount.value = nMaxAmt;
			document.QuickQuoteHL.edLoanAmount.focus();
			return false;
		}
		return true;
	}
		
/**************************************************************************************/
//end of functions used in the Quick Quote Homeloan calculator
/**************************************************************************************/


/**************************************************************************************/
//The following functions are used in the Quick Quote Residential Investment calculator
/**************************************************************************************/

function CalcQQRIMaxLoan()
{
	var nLoanAmount = 0;
	var nMaxLoanAmount = 0;
	var nMultiple = 0;
	var sFirstInvestment = "";

	sFirstInvestment = document.QuickQuoteRI.cbFirstInvestment.value;
	if (sFirstInvestment == "Yes")
		nMultiple = 0.90;
	else
		nMultiple = 0.80;
			
	nLoanAmount = document.QuickQuoteRI.edHousePrice.value;
	
	if (nLoanAmount > 0)
	{                        
		//calculate max loan and round to two places of decimal
        nMaxLoanAmount =document.QuickQuoteRI.edHousePrice.value * nMultiple;
        nMaxLoanAmount = Math.round((nMaxLoanAmount) * Math.pow (10, 2))
        document.QuickQuoteRI.edMaxLoan.value =  nMaxLoanAmount / Math.pow (10, 2);
    }
}

function VerifyQQRIData()
	{		
		var nLoanAmt=0;
		var nMaxAmt=0;
		
		//edHousePrice
		/*
		if (document.QuickQuoteRI.edHousePrice.value == "") 		
		{
			alert("Please enter the House Price");
			document.QuickQuoteRI.edHousePrice.focus();
			return false;
		}
	    */
		if(document.QuickQuoteRI.edHousePrice.value != "" && isNaN(document.QuickQuoteRI.edHousePrice.value))
		{
			alert("Please enter a valid House Price");
			document.QuickQuoteRI.edHousePrice.focus();
			return false;
		}

		//edLoanAmount
		if (document.QuickQuoteRI.edLoanAmount.value == "") 		
		{
			alert("Please enter Loan Amount Required");
			document.QuickQuoteRI.edLoanAmount.focus();
			return false;
		}
	
		if(isNaN(document.QuickQuoteRI.edLoanAmount.value))
		{
			alert("Please enter a valid Loan Amount");
			document.QuickQuoteRI.edLoanAmount.focus();
			return false;
		}


        if (document.QuickQuoteRI.edMaxLoan.value != ""){
		    nLoanAmt=Math.round(document.QuickQuoteRI.edLoanAmount.value)
		    nMaxAmt=Math.round(document.QuickQuoteRI.edMaxLoan.value)

		    if (nLoanAmt > nMaxAmt) 		
		    {
			    alert ("The maximum loan you can request is " + nMaxAmt);
			    document.QuickQuoteRI.edLoanAmount.value = nMaxAmt;
			    document.QuickQuoteRI.edLoanAmount.focus();
			    return false;
		    }
		}
		
		return true;
	}
				
/**************************************************************************************/
//end of functions used in the Quick Quote Residential Investment calculator
/**************************************************************************************/
