function ProposalCalc() {
	this.formObj = false;
	this.capPopupUrl = '';
	this.capPopupWidth = 100;
	this.capCount = 0;
	this.useFlatRate = false;		// Default to Compound APR Calculations
	this.inputType = '';			// Enter % in flat rate or APR
	this.lastBalance = 0;
	this.creditAppForm = false;
	this.financeQuoteForm = false;
	this.minDepositRate = false;
	this.vatRate = 0;
	this.incVat = true;
	
	this.FocusStore = FocusStore;
	this.CorrectNaN = CorrectNaN;
	this.CorrectFloat = CorrectFloat;
	this.Calc = Calc;
	this.GetQuote = GetQuote;
	this.SetForm = SetForm;
	this.SetCalcCreditForm = SetCalcCreditForm;
	this.CalcCreditApplication = CalcCreditApplication;
	this.SetCapPopupProperties = SetCapPopupProperties;
	this.CapLookup = CapLookup;
	this.SetPxPrice = SetPxPrice;
	this.SetPxReg = SetPxReg;
	this.AddCAPCount = AddCAPCount;
	this.SetFinaceQuoteForm = SetFinaceQuoteForm;
	this.CalcFinanceQuote = CalcFinanceQuote;
	this.SetMinDepositRate = SetMinDepositRate;
	this.DepositCheck = DepositCheck;
	this.SetVatRate = SetVatRate;
	this.SetIncVat = SetIncVat;
}

function FocusStore(inputObj) {
	inputObj.var_focus_value = inputObj.value;
}

// Correct Raw Value without replace
function CorrectFloat(inputVal, dplaces)
{
	if (!dplaces && dplaces != 0) {
		dplaces = 2;
	}
	
	var thisValue = inputVal;
	if (thisValue == '') thisValue = 0;
	else
	{
		thisValue = parseFloat(thisValue);
		if (isNaN(thisValue)) thisValue = 0;
	}
	thisValue = parseFloat(thisValue.toFixed(dplaces));
	return thisValue;
}

function CorrectNaN(inputObj, dplaces) {
	if (!dplaces && dplaces != 0) {
		dplaces = 2;
	}
		
	if (inputObj.tagName == 'INPUT') {
		var this_value = inputObj.value.replace('£', '');
		
		if (this_value == '') {
			var temp1 = 0;
			inputObj.value = '£'+temp1.toFixed(dplaces);
			return 0;
		} else {
			var this_value = parseFloat(this_value);
			if (isNaN(this_value)) {
				inputObj.value = inputObj.var_focus_value;
				return 0;
			} else {
				inputObj.value = '£'+this_value.toFixed(dplaces);
				return this_value;
			}
		}
	} else {
		var this_value = parseFloat(inputObj.value);
		return this_value.toFixed(dplaces);
	}
}

function Calc(inputObj, submit) {
	if (inputObj) {
		if ((inputObj.name != 'hp_apr_rate') && (inputObj.name != 'hp_flat_rate'))
		{
			this.CorrectNaN(inputObj);
		}
	}
	
	var balanceValue = this.CorrectNaN(this.formObj.cash_price);
	balanceValue -= this.CorrectNaN(this.formObj.deposit);
	balanceValue -= this.CorrectNaN(this.formObj.part_exchange);
	balanceValue += this.CorrectNaN(this.formObj.settlement);
	this.formObj.balance.value = balanceValue;
	this.CorrectNaN(this.formObj.balance);
	
	if (!this.incVat) {
		var vatAmount = this.CorrectFloat((this.CorrectNaN(this.formObj.cash_price) / 100) * this.vatRate);
		this.formObj.vat_amount.value = vatAmount;
		this.CorrectNaN(this.formObj.vat_amount);
		balanceValue += vatAmount;
	}
	
	// Adjust the hire purchase
	this.formObj.hire_purchase.value = balanceValue;
	
	var hpMonths = CorrectNaN(this.formObj.hp_period);
	var hpYears = hpMonths / 12;
	
	var amountBorrowed = CorrectNaN(this.formObj.hire_purchase);
	
	var aprRate = 0;
	var flatRate = 0
	
	// Input Type Conversions (APR and Flat Rate)
	if (this.inputType == 'FLAT')
	{
		// Flat Rate to APR Conversion
		flatRate = this.CorrectFloat(this.formObj.hp_flat_rate.value);
		if (flatRate <= 0) {
			aprRate = 0;
			flatRate = 0;
		}
		else aprRate = ((flatRate * 2) - 0.1);
		this.formObj.hp_apr_rate.value = this.CorrectFloat(aprRate);
	}
	else if (this.inputType == "APR")
	{
		// APR to Flat Rate
		aprRate = this.CorrectFloat(this.formObj.hp_apr_rate.value);
		if (aprRate <= 0) {
			aprRate = 0;
			flatRate = 0;
		}
		else flatRate = ((aprRate + 0.1) / 2);
		this.formObj.hp_flat_rate.value = this.CorrectFloat(flatRate);
	}
	else
	{
		// No Conversion, just get values and store
		aprRate = this.CorrectFloat(this.formObj.hp_apr_rate.value);
		flatRate = this.CorrectFloat(this.formObj.hp_flat_rate.value);
		this.formObj.hp_apr_rate.value = this.CorrectFloat(aprRate);
		this.formObj.hp_flat_rate.value = this.CorrectFloat(flatRate);
	}
	
	// Use either base rate or APR calculations
	if (this.useFlatRate == true)
	{
		// Flat Rate Calculation	
		var hpInterest = (amountBorrowed * (flatRate / 100)) * hpYears;
		var hpTotal = amountBorrowed + hpInterest; 
		this.formObj.hp_total.value = hpTotal;
		CorrectNaN(this.formObj.hp_total);
		var hpMonthly = hpTotal / hpMonths;
		this.formObj.hp_monthly.value = hpMonthly;
		CorrectNaN(this.formObj.hp_monthly);
		
		var totalInterest = hpTotal - amountBorrowed;
		this.formObj.total_interest.value = totalInterest;
		CorrectNaN(this.formObj.total_interest);
	}
	else
	{
		// Compound APR Calculation	
		var P = 0.00;
		
		if (aprRate > 0) {
			P = amountBorrowed * aprRate * Math.pow(1+aprRate/1200,hpMonths) 
							/ (1200 * (Math.pow(1+aprRate/1200,hpMonths) - 1));
		}
		else	P = amountBorrowed / hpMonths;				

		var hpMonthly = P;
		this.formObj.hp_monthly.value = hpMonthly;
		CorrectNaN(this.formObj.hp_monthly);
		
		var hpTotal = P*hpMonths; 
		this.formObj.hp_total.value = hpTotal;
		CorrectNaN(this.formObj.hp_total);
		
		var totalInterest = hpTotal - amountBorrowed;
		this.formObj.total_interest.value = totalInterest;
		CorrectNaN(this.formObj.total_interest);
	}
	
	if (submit) {
		this.formObj.submit();
	}
}

function GetQuote() {
	this.formObj.pcp_quote.value = 1;
	this.Calc(false, true);
}

function SetForm(formObj) {
	this.formObj = formObj
}

function SetCalcCreditForm(formObj) {
	this.creditAppForm = formObj;
}

function CalcCreditApplication() {
	var financeDetails = "";
	financeDetails += this.formObj.deposit.value+" deposit";
	financeDetails += ", "+this.formObj.hp_period.options[this.formObj.hp_period.selectedIndex].value+" Months";
	financeDetails += ", "+this.formObj.hp_apr_rate.value+"% APR";
	financeDetails += ", "+this.formObj.hp_monthly.value+" p/m";
	this.creditAppForm.finance_selected.value = financeDetails;
	this.creditAppForm.submit();
}

function SetCapPopupProperties(url, width) {
	this.capPopupUrl = url;
	this.capPopupWidth = width;
}

function CapLookup() {
	capWin = currentPage.OpenPopup(this.capPopupUrl, this.capPopupWidth);
	capWin.proposalGadgetId = this.gadgetId;
}

function SetPxPrice(pxPrice) {
	this.formObj.part_exchange.value = pxPrice;
	this.Calc();
}

function SetPxReg(regNo, pxDetail) {
	document.getElementById("regNoCell").innerHTML 	  = regNo;
	document.getElementById("pxDetailCell").innerHTML = '<b>My Current Car</b><br>'+pxDetail;
	this.formObj.px_detail.value 	= pxDetail;
	this.formObj.regno.value 	= regNo;
}

function AddCAPCount() {
	this.capCount++;
	if (this.capCount > 0) {
		document.getElementById("capButtonCell").style.visibility = 'hidden';
	}
}

function SetFinaceQuoteForm(formObject) {
	this.financeQuoteForm = formObject;
}

function CalcFinanceQuote() {
	this.financeQuoteForm.deposit.value = this.formObj.deposit.value;
	this.financeQuoteForm.period.value = this.formObj.hp_period.value;
	this.financeQuoteForm.submit();
}

function SetMinDepositRate(depositRate) {
	this.minDepositRate = depositRate;
}

function DepositCheck(depositAmount) {
	if (this.minDepositRate !== false) {
		var priceValue = this.CorrectNaN(this.formObj.cash_price);
		var depositValue = this.CorrectNaN(this.formObj.deposit);
		var depositCheck = this.CorrectFloat((priceValue / 100) * this.minDepositRate);
		if (depositValue < depositCheck) {
			alert('A minimum deposit of '+this.minDepositRate+'% is required');
			this.formObj.deposit.value = depositCheck;
			this.CorrectNaN(this.formObj.deposit);
		}
	}
}

function SetVatRate(vatRate) {
	this.vatRate = vatRate;
}

function SetIncVat(incVat) {
	this.incVat = (incVat == 0) ? false : true;
}
