var clrError, clrRequired, clrOkay;

clrError = "#ffcccc";
clrRequired = "#ffcccc";
clrOkay = "#ffffff";

var agt=navigator.userAgent.toLowerCase();
var is_ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
var vistoken = (is_ie) ? "inline" : "table-row";

function stripcommas(y) {
	var x;
	x = String(y);
	for (i=0;(i<x.length);i++)
	{
	  if (x.charAt(i)==",") {
		x = x.substring(0,i) + x.substring(i+1,x.length);
	  }
	}
	return x;
}

function valInteger( fieldname, minval, maxval ) {
	var field, illegalChars;
	
	field = document.getElementById(fieldname);
	illegalChars = /\D/;

	
	if (field.value == "") { 
		field.style.backgroundColor=clrOkay;
		return true; 
	}
	
	if (field.value.indexOf(",")!=-1) {
		field.value = stripcommas(field.value)
	}
	//fixInt(field);
		
	if (illegalChars.test(field.value)) {
		field.style.backgroundColor=clrError;
		alert("Only whole numbers can be entered in this field.");
		field.value = "";
		field.focus(); //field.select();
		return false;
	} else {
		if ((field.value < minval) || (field.value > maxval))  {
			field.style.backgroundColor=clrError;
			
			var nicemin, nicemax;
			nicemin= new NumberFormat(minval);
			nicemax= new NumberFormat(maxval);			

			nicemin.setCommas(true);
			nicemin.setPlaces(0);
			nicemin.setCurrency(false);
			nicemax.setCommas(true);
			nicemax.setPlaces(0);
			nicemax.setCurrency(false);			

			alert("You must enter a whole number between "+nicemin.toFormatted()+" and "+nicemax.toFormatted() + ".");
			field.focus(); field.select();		
			return false;
		} else {
			field.style.backgroundColor=clrOkay;
			return true;
		}
	}
}
function valDecimel( fieldname, minval, maxval ) {
	var field, illegalChars;
	
	field = document.getElementById(fieldname);
	illegalChars = /[0-9]/;

	
	if (field.value == "") { 
		field.style.backgroundColor=clrOkay;
		return true; 
	}

	if (field.value.indexOf(",")!=-1) {
		field.value = stripcommas(field.value)
	}		
		
	if (!illegalChars.test(field.value)) {
		field.style.backgroundColor=clrError;
		alert("Only whole numbers can be entered in this field.");
		field.value = "";
		field.focus(); field.select();
		return false;
	} else {
		if ((field.value < minval) || (field.value > maxval))  {
			field.style.backgroundColor=clrError;

			var nicemin, nicemax;
			nicemin= new NumberFormat(minval);
			nicemax= new NumberFormat(maxval);			

			nicemin.setCommas(true);
			nicemin.setPlaces(2);
			nicemin.setCurrency(false);
			nicemax.setCommas(true);
			nicemax.setPlaces(2);
			nicemax.setCurrency(false);			
			
			alert("You must enter a number between "+nicemin.toFormatted()+" and "+nicemax.toFormatted()) + ".";
			field.focus(); field.select();	
			return false;
		} else {
			fixMoney(field);
			field.style.backgroundColor=clrOkay;
			return true;
		}
	}
}



function valPercent( fieldname ) {
	var field, illegalChars, total;
	
	field = document.getElementById(fieldname);
	illegalChars = /[0-9]/;

	
	if (field.value == "") { 
		field.style.backgroundColor=clrOkay;
		return true; 
	}
		
	if (!illegalChars.test(field.value)) {
		field.style.backgroundColor=clrError;
		alert("Only whole numbers can be entered in this field");
		field.focus(); field.select();
		return false;
	} else {
		if ((field.value < 0) || (field.value > 100))  {
			field.style.backgroundColor=clrError;
			alert("You must enter a number within the range 0-100.");
			field.focus(); field.select();
			return false;
		} else {
			field.style.backgroundColor=clrOkay;
			return true;
		}
	}
}


function ConsolidateLump( prefix )
{
	switch(document.getElementById("makeLumpSumRepayment"+prefix).value)
	{
		case "Yes":
			document.getElementById("rowLumpAmount"+prefix).style.display=vistoken;
			document.getElementById("rowLumpMonth"+prefix).style.display=vistoken;	
			break;
		case "No":
			document.getElementById("rowLumpAmount"+prefix).style.display="none";
			document.getElementById("rowLumpMonth"+prefix).style.display="none";
			break;			
	}
}


function ConsolidateFreq( prefix )
{
	switch(document.getElementById("repaymentFrequency"+prefix).value)
	{
		case "Weekly":
			document.getElementById("RepayLabel"+prefix).innerHTML = "per week"
			break;
		case "Fortnightly":
			document.getElementById("RepayLabel"+prefix).innerHTML = "per fortnight"
			break;
		case "Monthly":
			document.getElementById("RepayLabel"+prefix).innerHTML = "per month"
			break;			
	}	
}
 
function getFormTerm( prefix ) {
	years = document.getElementById("loanTermYears"+prefix).value;
	months = document.getElementById("loanTermMonths"+prefix).value;
	
	CLT = 0
	if (isFinite(years) && !isNaN(years) && years != "") CLT += parseInt(years)*12;
	if (isFinite(months) && !isNaN(years) && months != "") CLT += parseInt(months);
	
	//alert("years: "+years+", months:"+months+", CLT: "+CLT);
	
	if (isNaN(CLT)) return 0;
	return CLT;
}


function validateForm( prefix )
{
	var calculate;
	radios = document.forms[0].calculate;
	for(var i= 0; i < radios.length; i++)
		if(radios[i].checked) calculate = radios[i].value;

	if (1==1) {
		if (calculate != "amount" && valInteger("loanAmount"+prefix, 1, 10000000) == false) return false;
		if (calculate != "repayment" && valInteger("loanRepayments"+prefix, 1, 10000000) == false) return false;
		if (calculate != "term" && valInteger("loanTermYears"+prefix, 1, 30) == false) return false;
		if (calculate != "term" && valInteger("loanTermMonths"+prefix, 0, 11) == false) return false;
		if (!valDecimel("interestRate"+prefix, 0.01, 100)) return false;
		if (!valDecimel("maturityInterestRate"+prefix, 0.01, 100)) return false;
		if (brand=="cba") {
			if (!valInteger("interestOnlyPeriod"+prefix, 0, 15)) return false;
		}
		if (brand=="homepath") {
			if (!valInteger("interestOnlyPeriod"+prefix, 0, 5)) return false;
		}
		if (!valInteger("monthlyFee"+prefix, 0, 100)) return false;
	}
	
	if (calculate != "repayment" && document.getElementById("loanRepayments"+prefix).value =="") {
		alert("Please enter the loan repayments.");
		return false;
	}
	if (calculate != "amount" && document.getElementById("loanAmount"+prefix).value =="") {
		alert("Please enter the loan amount.");
		return false;
	}
	if (calculate != "term" && getFormTerm(prefix) < 12) {
		alert("Please enter the loan term. The loan term must be at least 1 year.");
		return false;
	}


	if (document.getElementById("interestRate"+prefix).value =="") {
		alert("Please enter the interest rate.");
		return false;
	}
	if (document.getElementById("maturityInterestRate"+prefix).value =="") {
		alert("Please enter the maturity interest rate.");
		return false;
	}	
	if (document.getElementById("makeLumpSumRepayment"+prefix).value == "Yes") {
		
		if (calculate != "amount" && (parseFloat(document.getElementById("lumpSumRepaymentAmount"+prefix).value) > parseFloat(document.getElementById("loanAmount"+prefix).value))) {
			alert("Lump sum repayment must not exceed the loan amount.");
			return false;
		}
		if (document.getElementById("lumpSumRepaymentAmount"+prefix).value == "") {
			alert("Please enter the lump sum amount.");
			return false;
		}
		if (document.getElementById("lumpSumRepaymentMonth"+prefix).value == "") {
			alert("Please enter the month you would like to make the lump sum repayment.");
			return false;
		}		
	}
	
	if ( document.getElementById("repaymentFrequency"+prefix).value !="Monthly" && document.getElementById("interestOnlyPeriod"+prefix).value > 0) {
		alert("Loans with an Interest Only period must have a monthly repayment frequency. However it is possible to make weekly or fortnightly repayments afterwards.")
		return false;
	}
	
	if (brand == "cba") {
		// CBA Only
		if ( document.getElementById("loanType"+prefix).value =="Owner Occupied" && parseInt(document.getElementById("interestOnlyPeriod"+prefix).value) > 10) {
			alert("Maximum interest only period on Owner Occupied loans is 10 years.")
			return false;
		}

		if ( document.getElementById("loanType"+prefix).value =="Investment" && parseInt(document.getElementById("interestOnlyPeriod"+prefix).value) > 15) {
			alert("Maximum interest only period on Investment loans is 15 years.")
			return false;
		}
	}

	if (brand == "homepath") {
		if ( document.getElementById("interestOnlyPeriod"+prefix).value > 5) {
			alert("Maximum interest only period is 5 years.")
			return false;
		}
	}
	
	
	if (calculate != "term" && getFormTerm(prefix) > (30*12)) {
		alert("The loan term cannot be more than 30 years.");
		return false;
	}

	if (calculate != "amount" && document.getElementById("loanAmount"+prefix).value < 1) {
		alert("The minimum loan amount is $1.");
		return false;
	}
		
	if (calculate != "term") {

		if (getFormTerm(prefix) <= document.getElementById("interestOnlyPeriod"+prefix).value * 12) {
			alert("The interest only period must be less than the loan term.");
			return false;			
		}

		if (getFormTerm(prefix) < getLoanTerm(document.getElementById("product"+prefix).value)) {
			alert("The product you have selected matures after the loan term.  Please select a product that does not exceed the loan term.");
			return false;			
		}

		if (getFormTerm(prefix) < document.getElementById("lumpSumRepaymentMonth"+prefix).value) {
			alert("The month of the lump sum repayment is after the loan term.  Please enter an earlier month.");
			return false;			
		}		
	}
	return true;
}



