	// Insurance Rates - first one for 6 months, second for 12 months, third for 18 months etc.
	var InsuranceRates = new Array(0.06, 0.06, 0.06, 0.085, 0.105);
	
	// Booking Fee - currently $70.00
	var BookingFee = new Number (90.00);
	
	// GST Rate - currently 12.5%
	var GSTRate = new Number (0.125);
	
	// Interest Rate - currently 21.95%
	var InterestRate = new Number (0.238);

	
	// Calculates and displays the Monthly Payments, Weekly Payments, Booking Fee, Minimum Deposit,
	// Insurance, Finance Charges and Finace Rate of a loan from Gilrose Finance, based on the 
	// Cash Price, Deposit, Term, and Insurance options entered by the user. 	
	function Calculate()
	{
		
		var CashPriceIncGST = new Number (0);
		var CashPriceExcGST = new Number (0);
		var CashPrice = new Number (0);
		var Deposit = new Number (0);
		
		var InsuranceRate = new Number (0);
		var InsuranceAmount = new Number (0);
		var InsuranceAmountIncluded = new Number (0);
		var TotalGrossCost = new Number (0);
		var AmountFinanced = new Number (0);
		var MonthlyPayments = new Number (0);
		var WeeklyPayments = new Number (0);
		var TotalCharges = new Number (0);
		var FinanceCharge = new Number (0);
		var TotalCostOfCredit = new Number (0);
		var BalancePayable = new Number (0);
		var TotalCostOfTransaction = new Number (0);
		var AdditionalCostOfCashTransaction = new Number (0);
		var SuggestedMinimum = new Number (0);
		
		var Term = new Number (0);
		
		
		Term = Number(document.Loan.Term[document.Loan.Term.selectedIndex].value);
		// Deposit = Number(DollarToNumber(document.Loan.Deposit.value));
		CashPrice = Number(DollarToNumber(document.Loan.CashPrice.value));
		
		
		//if (document.Loan.GST.checked == true)
		//{
			CashPriceIncGST = CashPrice;
		//	CashPriceExcGST = CashPrice / (GSTRate + 1);
		//}
		//else
		//{
		//	CashPriceIncGST = CashPrice * (GSTRate + 1);
		//	CashPriceExcGST = CashPrice;
		//}
		
	
		//InsuranceRate = InsuranceRates[document.Loan.Term.selectedIndex];
		//InsuranceAmount = InsuranceRate * CashPriceIncGST;
		
		
		//if (document.Loan.HasInsurance.selectedIndex == 0)
		//{
		//	InsuranceAmountIncluded = InsuranceAmount;
		//}
		//else
		//{
			InsuranceAmountIncluded = 0;
		//}
		Deposit = 0;

		TotalGrossCost = CashPriceIncGST + BookingFee + InsuranceAmountIncluded;
		AmountFinanced =  TotalGrossCost - Deposit;

		MonthlyPayments = PaymentAmount(InterestRate / 12, Term, AmountFinanced * -1, 0)  + (0 / Term);
		WeeklyPayments = MonthlyPayments * 12 / 52;
		
		TotalCharges = MonthlyPayments * Term - AmountFinanced;
		FinanceCharge = TotalCharges - 0;
		
		BalancePayable = AmountFinanced  + TotalCharges;
		
		TotalCostOfTransaction = TotalGrossCost + TotalCharges;
		AdditionalCostOfCashTransaction = TotalCharges;

		SuggestedMinimum = CashPriceIncGST - CashPriceExcGST;
		
		document.Loan.FinanceCharge.value = NumberToDollar(  FinanceCharge);
		document.Loan.BookingFee.value = NumberToDollar(BookingFee);
		//document.Loan.Insurance.value = NumberToDollar(InsuranceAmountIncluded);
		document.Loan.MonthlyPayments.value = NumberToDollar(MonthlyPayments);
		document.Loan.WeeklyPayments.value = NumberToDollar(WeeklyPayments);
		//document.Loan.CashPriceIncGST.value = NumberToDollar(CashPriceIncGST);
		//document.Loan.CashPriceExcGST.value = NumberToDollar(CashPriceExcGST);
		//document.Loan.SuggestedMinimum.value = NumberToDollar(SuggestedMinimum);
		//document.Loan.FinanceRate.value = NumberToPercentage(InterestRate * 100);
		
		return true;		
	}
	

	// Calculates the payment for a loan based on constant payments and a constant interest rate, given the 
	// interest rate for the loan, the number of payments for the loan, the present value, and the future value.
	function PaymentAmount(IntrestRate, NumberOfPayments, PresentValue, FutureValue)
	{ 
		var PMT = new Number (0.000);		
		var Temp = new Number (0.000);		
			
		if(IntrestRate == 0.0) 
		{
			if(NumberOfPayments != 0) 
			{
				PMT = -(FutureValue + PresentValue) / NumberOfPayments;
			}
			else 
			{
				alert("Divide by zero error.");
			}
		}
		else 
		{
			Temp = Math.pow(1 + IntrestRate, NumberOfPayments);
			PMT = -((IntrestRate * (FutureValue + Temp * PresentValue)) /( -1 + Temp));
		}
		return PMT;
	} 
	
	
	// Converts a string in a '$999.00' format to a number value.
	function DollarToNumber(DollarValue)
	{
		var i = 1;
		var ReturnString = "";
		for (i = 0; i < DollarValue.length; i++)
		{
			if ((DollarValue.charAt(i) >= '0' && DollarValue.charAt(i) <= '9') || DollarValue.charAt(i) == '.')
			{
				ReturnString = ReturnString + DollarValue.charAt(i);
			}
		}
		return ReturnString;
	}

	// Converts a number value to a string in a '$999.00' format.
	function NumberToDollar(NumberValue) 
	{	
		NumberValue = Math.round(NumberValue * 100) / 100
		NumberValueAsString = "" + NumberValue;
    	if (NumberValueAsString.indexOf (".", 0) != -1) 
		{
    		Dollars = NumberValueAsString.substring(0, NumberValueAsString.indexOf (".", 0));
    		Cents = NumberValueAsString.substring(NumberValueAsString.indexOf(".", 0) + 1, NumberValueAsString.indexOf (".", 0) + 3);
    		
			if (Cents.length == 0) 
			{
				Cents = "00";
			}
			if (Cents.length == 1)			
			{
				Cents = Cents + "0";
			}
    	} 
		else 
		{
			Dollars = NumberValueAsString;		
			Cents = "00";
		}	
		ReturnString = "";	
		Index = Dollars.length;
    	if (Index >= 3) 
		{		
			while (Index > 0)
			{
				TempString = Dollars.substring(Index - 3, Index)
    			if (TempString.length == 3) 
				{
					ReturnString = "," + TempString + ReturnString;
    				Index = Index - 3;			
				}
				else 
				{				
					ReturnString = TempString + ReturnString;
					Index = 0;
				}
			}
    		if (ReturnString.substring(0, 1) == ",") 
			{
    			Dollars = ReturnString.substring(1, ReturnString.length);
			}
			else			
			{
				Dollars = ReturnString;
			}
    	} 	
		return ("$" + Dollars + "." + Cents);
	}

	// Converts a number value to a string in a '99.00%' format.
	function NumberToPercentage(NumberValue) 
	{	
		NumberValue = "" + NumberValue;
    	if (NumberValue.indexOf (".", 0) != -1) 
		{
    		IntegerValue = NumberValue.substring(0, NumberValue.indexOf (".", 0));
    		FractionValue = NumberValue.substring(NumberValue.indexOf(".", 0) + 1, NumberValue.indexOf (".", 0) + 3);
    		
			if (FractionValue.length == 0) 
			{
				FractionValue = "00";
			}
			if (FractionValue.length == 1)			
			{
				FractionValue = FractionValue + "0";
			}
    	} 
		else 
		{
			IntegerValue = NumberValue;		
			FractionValue = "00";
		}	
		ReturnString = "";	
		Index = IntegerValue.length;
    	if (Index >= 3) 
		{		
			while (Index > 0)
			{
				TempString = IntegerValue.substring(Index - 3, Index)
    			if (TempString.length == 3) 
				{
					ReturnString = "," + TempString + ReturnString;
    				Index = Index - 3;			
				}
				else 
				{				
					ReturnString = TempString + ReturnString;
					Index = 0;
				}
			}
    		if (ReturnString.substring(0, 1) == ",") 
			{
    			IntegerValue = ReturnString.substring(1, ReturnString.length);
			}
			else			
			{
				IntegerValue = ReturnString;
			}
    	} 	
		return (IntegerValue + "." + FractionValue + "%");
	}
	
function FValidateControl(control) {
  if (control.value=="") {
    alert(control.name+": this is a required field.")
    control.focus()
    return false }
  return true }

function FSubmitValidation(form) {
	
if (!FValidateControl(form.Deposit)) return false
if (!FValidateControl(form.Given_Names)) return false
if (!FValidateControl(form.Surname)) return false
if (form.NZ_Citizen[0].checked || form.NZ_Citizen[1].checked){
}else{ 
	alert("New Zealand Citizen is a required field")
	return false
}
if(form.NZ_Citizen[1].checked)
	if (!FValidateControl(form.Citizenship)) return false

if (form.Permanent_Resident[0].checked || form.Permanent_Resident[1].checked){
}else{ 
	alert("Permanent Resident is a required field")
	return false
}
if (!FValidateControl(form.Facsimile)) return false
if (!FValidateControl(form.DOB)) return false
if (!FValidateControl(form.Address)) return false
if (!FValidateControl(form.Address_Time_There)) return false
if (!FValidateControl(form.Home_Phone)) return false
if (form.Number_Listed[0].checked || form.Number_Listed[1].checked){
}else{ 
	alert("Number Listed is a required field")
	return false
}
if (!FValidateControl(form.Previous_Address)) return false
if (!FValidateControl(form.Previous_Address_Time_There)) return false

if (form.employed[0].checked || form.employed[1].checked){
}else{ 
	alert("Are you currently in full or part-time employment?")
	return false
}

if (!FValidateControl(form.Business_Phone)) return false
if (!FValidateControl(form.Occupation)) return false
if (!FValidateControl(form.Employer)) return false
if (!FValidateControl(form.Employer_Time_There)) return false
if (!FValidateControl(form.Position)) return false
if (!FValidateControl(form.Immediate_Superior)) return false
if (!FValidateControl(form.NOK_Name_Address)) return false
if (form.Marital_Status[0].checked || form.Marital_Status[1].checked || form.Marital_Status[2].checked){
}else{ 
	alert("Marital Status is a required field")
	return false
}
if (form.Weekly_Income[0].checked || form.Weekly_Income[1].checked || form.Weekly_Income[2].checked || form.Weekly_Income[3].checked || form.Weekly_Income[4].checked){
}else{ 
	alert("Weekly Income is a required field")
	return false
}
if (form.Home_Owner[0].checked || form.Home_Owner[1].checked){
}else{ 
	alert("HomeOwner is a required field")
	return false
}
if (form.Home_Owner[0].checked){
	if (!FValidateControl(form.Home_Value)) return false
	if (!FValidateControl(form.Mortgage_Balance)) return false
}

if(!form.no_credit_ref.checked){
	if (!FValidateControl(form.Company)) return false
	if (!FValidateControl(form.Branch)) return false
	if (!FValidateControl(form.Original_Balance)) return false
	if (!FValidateControl(form.Balance_Owing)) return false
	if (!FValidateControl(form.Monthly_Payments)) return false
}
if(!form.no_credit_outstanding.checked){
	if (!FValidateControl(form.Company_Other)) return false
	if (!FValidateControl(form.Branch_Other)) return false
	if (!FValidateControl(form.Original_Balance_Other)) return false
	if (!FValidateControl(form.Balance_Owing_Other)) return false
	if (!FValidateControl(form.Monthly_Payments_Other)) return false
}

if (form.Accepted.checked){
}else{
	alert("You must accept the privacy act before applying")
	return false
}
   return true }
   
   
   
function employedP(v){
	switch(v){
	
	case "yes":
		window.finance.Business_Phone.value = "";
		window.finance.Occupation.value = "";
		window.finance.Employer.value = "";
		window.finance.Employer_Time_There.value = "";
		window.finance.Position.value = "";
		window.finance.Immediate_Superior.value = "";
	break;
	
	case "no":
		window.finance.Business_Phone.value = "N/A";
		window.finance.Occupation.value = "N/A";
		window.finance.Employer.value = "N/A";
		window.finance.Employer_Time_There.value = "N/A";
		window.finance.Position.value = "N/A";
		window.finance.Immediate_Superior.value = "N/A";
	break;
	
	
	}
 }
   