//------------------------------------------------------------------------------------------------------------------------
// Offset Calculator Functions
//------------------------------------------------------------------------------------------------------------------------

//------------------------------------------------------------------------------------------------------------------------
// Name: 			validateNumberTextBoxGeneric
// Purpose: 	Standard Form Numeric Textbox Validation
//
// Based on: 	validateNumberTextBox (validate_forms.js)
// Changes:		- takes text field as a parameter rather than hard-coding
// 						- is called on the onblur event rather than onsubmit  
// 						- removed alert on checking for empty string, this is dealt
// 							with by validateOffsetCalc
//------------------------------------------------------------------------------------------------------------------------
//
function validateNumberTextBoxGeneric(theField) {
var checkStr = new String(trim(theField.value));

var checkOK = "£,.0123456789 \t\r\n\f";
var allValid = true;
	for (i = 0;  i < checkStr.length;  i++) {
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++) 
			if (ch == checkOK.charAt(j))
				break;
				if (j == checkOK.length) {
					allValid = false;
					break;
				}
	}
	
	if (allValid == true) {
 		var checkStrFormat = checkStr.search(/£|,/);
 			if (checkStrFormat >=0 ) {
  				strFormatStr = checkStr.replace(/£/g,"");
  				strFormatStr = strFormatStr.replace(/,/g,"");
  				theField.value = strFormatStr;
 			}
	}

	if (!allValid) {
		alert("Please enter a numeric value.");
		theField.focus();
		return false;
	}
	
	return true;
}

//------------------------------------------------------------------------------------------------------------------------
// Name 	: validateOffsetCalc
// Purpose: Offset Mortgage Calculator Validation
//					- Check for empty form fields.
//					- Check mortgage terms is a whole number.
//					- Check all Mortgage conditions are valid - validateLoanAgainstProperty, validateLoanAmount, validateTermYears 
//------------------------------------------------------------------------------------------------------------------------
//
function validateOffsetCalc(theForm) {
	var str1 = new String(theForm.txt1.value);
	var str2 = new String(theForm.txt2.value);
	var str3 = new String(theForm.txt3.value);
	var str4 = new String(theForm.txt4.value);
	
	var checkOK = "0123456789-";
	var checkStr = theForm.txt4.value;
	var allValid = true;
	var decPoints = 0;
	var allNum = "";
	var strEmptyFieldError = "Please complete all mortgage fields before selecting the calculate button";
	
	if ((str1.length == "") || (str2.length == "")|| (str2 == "0") || (str3.length == "") || (str3 == "0") || (str4.length == "") || (str4 == "0"))
		{
			alert(strEmptyFieldError);
			theForm.txt1.focus();
			return false;
		}
	
	for (i = 0;  i < checkStr.length;  i++) {
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
			if (ch == checkOK.charAt(j))
			break;
			if (j == checkOK.length)
			{
				allValid = false;
				break;
			}
		allNum += ch;
	}

	if (!allValid) {
		alert("The loan term must be a whole number.");
		theForm.txt4.focus();
		return false;
	}

	if ((validateLoanAgainstProperty(theForm)) && (validateLoanAmount(theForm)) && (validateTermYears(theForm))) {
		return true;
	} else {
		return false;
	}
}
	
//------------------------------------------------------------------------------------------------------------------------
// Name		: validateLoanAgainstProperty
// Purpose: Offset Mortgage Calculator Validation
//					The Loan amount must be less than or equal to the the value of the property
//------------------------------------------------------------------------------------------------------------------------
//
function validateLoanAgainstProperty(theForm) {
		var intLoanAmount = parseInt(theForm.txt2.value);
		var intPropertyValue = parseInt(theForm.txt3.value);
		var strErrorText = "The loan amount must be less than or equal to 95% of the value of your home.";
	
		//if (intLoanAmount > intPropertyValue) {

		if ((intLoanAmount / intPropertyValue) > 0.95) {
			alert(strErrorText);
			theForm.txt2.focus();
			return false;
		} else {
			return true;
		}
}

//------------------------------------------------------------------------------------------------------------------------
// Name		: validateLoanAmount
// Purpose: Offset Mortgage Calculator Validation
//					The minimum loan amount for an offset Mortgage cannot be less than £30,000
//------------------------------------------------------------------------------------------------------------------------
//
function validateLoanAmount(theForm) {
	var intLoanAmount = parseInt(theForm.txt2.value);
	var strErrorText = "The minimum amount for an offset Flexible Mortgage is £30,000.";
	
	if (intLoanAmount < 30000) {
		alert(strErrorText);
		theForm.txt2.focus();
		return false;
	} else {
		return true;
	}
}

//------------------------------------------------------------------------------------------------------------------------
// Name		: validateTermYears
// Purpose: Offset Mortgage Calculator Validation
//					Check that loan terms is between 3 and 52 years
//------------------------------------------------------------------------------------------------------------------------
//
function validateTermYears(theForm) {
	var intLoanTerm = parseInt(theForm.txt4.value);
	var strErrorText = "The loan term must be within 3 and 52 years";
	
	if ((intLoanTerm < 3) || (intLoanTerm > 52)) {
		alert(strErrorText);
		theForm.txt4.focus();
		return false;
	} else {
		return true;
	}
}	
