var strBorderColor;
var strIncomeCalcBorderColor;

/**
* Calculate the income required for a specified mortgage
*
* @param			frmTarget			The form containing the required information
*/
function calculateIncome(frmTarget) {
	var strMortgage;
	var intMortgage, intIncome;
	
	// Reset the form
	resetIncomeCalculator(frmTarget);	
	
	strMortgage = frmTarget.txtMortgage.value;
	
	if (!validateNumber(strMortgage, 5000, 10000000)) {
	
		// Highlight the problem field
		frmTarget.txtMortgage.style.borderColor = "#F00";
		
		// Display the warning message
		document.getElementById("incomeCalcFormWarning").style.display = "block";
		
	} else {
	
		intMortgage	= Number(strMortgage);
		intIncome	= intMortgage / 3.5;
		frmTarget.txtIncome.value = formatCurrency(intIncome);
		
	} // if - else
} // calculateIncome



/**
* Reset the form border colours
* @param		frmTarget			A reference to the form
*/
function resetIncomeCalculator(frmTarget) {
	if (strIncomeCalcBorderColor == undefined) {
		strIncomeCalcBorderColor = frmTarget.txtMortgage.style.borderColor;
	} // if
	
	frmTarget.txtMortgage.style.borderColor	= strIncomeCalcBorderColor;
	frmTarget.txtIncome.value						= "";
	
	// Hide the warning message
	document.getElementById("incomeCalcFormWarning").style.display = "none";
} // resetIncomeCalculator









/**
* Compute the mortgage repayments based on the information
* entered into the form
*
* @param			frmTarget			The form containing the information
* @return		Boolean value of "false" (in order to prevent the form being submitted)
*/
function calculateMortgage(frmTarget) {
	var intRequired, intPeriod, intRate;
	var intCounter, intLength;
	var intRepayment, intRepayment8, intInterest, intInterest8;
	var arrInvalid;
	
	// Reset the form
	resetForm(frmTarget);
	
	arrInvalid		= new Array();
	
	intRequired		= frmTarget.txtMortgage.value;
	intPeriod		= frmTarget.txtPeriod.value;
	intRate			= frmTarget.txtRate.value;
	
	// Check that the user has filled in all the fields
	// Mortgage Required
	if (!validateMortgage(intRequired)) {
		arrInvalid.push(frmTarget.txtMortgage);
	} // if
	
	// Repayment Period
	if (!validatePeriod(intPeriod)) {
		arrInvalid.push(frmTarget.txtPeriod);
	} // if
	
	// Interest Rate
	if (!validateRate(intRate)) {
		arrInvalid.push(frmTarget.txtRate);
	} // if
	
	// Check if we have any invalid data
	if (arrInvalid.length > 0) {
		
		// Highlight the offending form fields
		intLength = arrInvalid.length;
		for (intCounter = 0; intCounter < intLength; intCounter++) {
			arrInvalid[intCounter].style.borderColor = "#F00";
		} // for
		
		// Display the warning message
		document.getElementById("mortgageFormWarning").style.display = "block";
		
	} else {
	
		// Calculate the repayments
		intRequired		= parseFloat(intRequired);
		intPeriod		= parseFloat(intPeriod);
		intRate			= parseFloat(intRate);
		
		intRate			/= 100;
		intRepayment	= ((intRequired * intRate) / 12) * (1 / (1 - (Math.pow(1 / (1 + intRate), intPeriod))));
		intRepayment8	= ((intRequired * 0.08) / 12) * (1 / (1 - (Math.pow(1 / 1.08, intPeriod))));
		intInterest		= (intRequired * intRate) / 12;
		intInterest8	= (intRequired * 0.08) / 12;
		
		// Populate the form fields
		frmTarget.txtMonthRepay.value		= formatCurrency(intRepayment);
		frmTarget.txtIntOnly.value			= formatCurrency(intInterest);
		frmTarget.txt8pcMonthRepay.value	= formatCurrency(intRepayment8);
		frmTarget.txt8pcIntOnly.value		= formatCurrency(intInterest8);
		
	} // if - else
	
	return (false);
} // calculateMortgage



/**
* Reset the form border colours
* @param		frmTarget			A reference to the form
*/
function resetForm(frmTarget) {
	if (strBorderColor == undefined) {
		strBorderColor = frmTarget.txtMortgage.style.borderColor;
	} // if
	
	frmTarget.txtMortgage.style.borderColor	= strBorderColor;
	frmTarget.txtPeriod.style.borderColor		= strBorderColor;
	frmTarget.txtRate.style.borderColor			= strBorderColor;
	
	frmTarget.txtMonthRepay.value				= "";
	frmTarget.txtIntOnly.value					= "";
	frmTarget.txt8pcMonthRepay.value			= "";
	frmTarget.txt8pcIntOnly.value				= "";
	
	// Hide the warning message
	document.getElementById("mortgageFormWarning").style.display = "none";
} // resetForm



/**
* Validate the requested mortgage amount
*
* @param			strCheck			The amount to check
* @return		A boolean value indicating the validity of the requested mortgage amount
*/
function validateMortgage(strCheck) {
	if (strCheck == undefined || strCheck == null || strCheck.length == 0) {
		return (false);
	} else {
		return (validateNumber(strCheck, 5000, 10000000));
	} // if - else
} // validateMortgage



/**
* Validate the requested repayment period
*
* @param			strCheck			The period to check
* @return		A boolean value indicating the validity of the repayment period
*/
function validatePeriod(strCheck) {
	if (strCheck == undefined || strCheck == null || strCheck.length == 0) {
		return (false);
	} else {
		return (validateNumber(strCheck, 5, 25));
	} // if - else
} // validatePeriod



/**
* Validate the requested interest rate
*
* @param			strCheck			The rate to check
* @return		A boolean value indicating the validity of the interest rate
*/
function validateRate(strCheck) {
	if (strCheck == undefined || strCheck == null || strCheck.length == 0) {
		return (false);
	} else {
		return (validateNumber(strCheck, 1, 100));
	} // if - else
} // validateRate



/**
* Validate a string as a number using the supplied arguments
*
* @param		intCheck			The number string to check
* @param		intMin			The minimum value
* @param		intMax			The maximum value
*/
function validateNumber(strCheck, intMin, intMax) {
	var intCheck;
	var rexPattern;
	
	rexPattern = new RegExp("^[0-9\.]+$");
	
	if (!rexPattern.test(strCheck)) {
		return (false);
	} else {
		intCheck = parseFloat(strCheck);
		if (intCheck < intMin || intCheck > intMax) {
			return (false);
		} else {
			return (true);
		} // if - else
	} // if - else
} // validateNumber



/**
* Format a number as currency (i.e. with two decimal places)
*
* @param		intSource			The number to format
* @return	A formatted "number string"
*/
function formatCurrency(intSource) {
	var intPeriod;
	var strSource, strWhole, strFraction;
	
	strSource = intSource.toString();
	intPeriod = strSource.indexOf(".");
	
	if (intPeriod >= 0) {
		strWhole			= strSource.substr(0, intPeriod);
		strFraction		= strSource.substr(intPeriod + 1);
		
		if (strFraction.length > 2) {
			strFraction = strFraction.substr(0, 2);
		} else {
			if (strFraction.length < 2) {
				while (strFraction.length < 2) {
					strFraction += "0";
				} // while
			} // if
		} // if - else
		
		strSource = "£" + strWhole + "." + strFraction;
	} else {
		strSource = "£" + strSource + ".00";
	} // if - else
	
	return (strSource);
} // formatCurrency