//+---------------------------------------------------------------------------------------------
//| OBJECT: 		frmValidate.js
//| PURPOSE:		This page contains java script code for validation purposes.
//+---------------------------------------------------------------------------------------------
//| --- (c) Copyright 2008, All Rights Reserved ---
//| --- Written by: Derek Paul                  ---
//| --- Contact: 0402 283871                    ---
//| --- Email: obiwan@senet.com.au              ---
//+---------------------------------------------------------------------------------------------
//|
//| NOTES:	
//|
//| CHANGE HISTORY:
//| DATE           WHO            DESCRIPTION OF CHANGE
//| ------------  ------------   ---------------------------------------------------------------
//| 23-Apr-2008    Derek Paul     Original
//+---------------------------------------------------------------------------------------------


function isCurrency(passVal){ 
	isPrice = /^\d{1,3}(,\d{3})*(\.\d{2})?$/
	return isPrice.test( passVal ); 
} 

function isBlank(passedVal){
    return /^\s*$/.test(passedVal);
}

function isNum(passedVal){
 	for(i=0; i<passedVal.length; i++){
 		if(passedVal.charAt(i) <"0"){
 			return false
 		} 
 		if(passedVal.charAt(i) >"9"){
 			return false
 		} 
 	}
return true
}

function dp(price) {
   string = "" + price;
   number = string.length - string.indexOf('.');
   if (string.indexOf('.') == -1)
      return string + ''; /* .00 */
   if (number == 1)
      return string + ''; /* .00 */
   if (number == 2)
      return string + '0';
   if (number > 3)
      return string.substring(0,string.length-number+3);

return string;
}

function validateForm(form) {
	if (isBlank(form.frmCurrentLoanAmount.value)){
		alert("Your current loan amount is required.")
		form.frmCurrentLoanAmount.select();
		return false
	} 

	if (!isBlank(form.frmCurrentLoanAmount.value)){
		if (isNaN(form.frmCurrentLoanAmount.value)){
			if (!isNum(form.frmCurrentLoanAmount.value)){
 				if (!isCurrency(form.frmCurrentLoanAmount.value)){
 					alert("Your current loan amount must be a number.")
 					form.frmCurrentLoanAmount.select();
 					return false
 				}
			}
		}	
	}
	
	if ((form.frmCurrentLoanAmount.value) < 0.01 ) {
 		alert("Your current loan amount must be greater than zero.")
 		form.frmCurrentLoanAmount.select();
 		return false
 	}

	if (isBlank(form.frmCurrentLoanRepayments.value)){
		alert("Your current loan repayment is required.")
		form.frmCurrentLoanRepayments.select();
		return false
	} 

	if (!isBlank(form.frmCurrentLoanRepayments.value)){
		if (isNaN(form.frmCurrentLoanRepayments.value)){
			if (!isNum(form.frmCurrentLoanRepayments.value)){
 				if (!isCurrency(form.frmCurrentLoanRepayments.value)){
 					alert("Your current loan repayment must be a number.")
 					form.frmCurrentLoanRepayments.select();
 					return false
 				}
			}
		}	
	}

	if ((form.frmCurrentLoanRepayments.value) < 0.01 ) {
 		alert("Your current loan repayment must be greater than zero.")
 		form.frmCurrentLoanRepayments.select();
 		return false
 	}

	if (isBlank(form.frmHouseholdGrossIncome.value)){
		alert("Your household income is required.")
		form.frmHouseholdGrossIncome.select();
		return false
	} 

	if (!isBlank(form.frmHouseholdGrossIncome.value)){
		if (isNaN(form.frmHouseholdGrossIncome.value)){
			if (!isNum(form.frmHouseholdGrossIncome.value)){
 				if (!isCurrency(form.frmHouseholdGrossIncome.value)){
 					alert("Your household income must be a number.")
 					form.frmHouseholdGrossIncome.select();
 					return false
 				}
			}
		}	
	}
	
	if ((form.frmHouseholdGrossIncome.value) < 0.01 ) {
 		alert("Your household income must be greater than zero.")
 		form.frmHouseholdGrossIncome.select();
 		return false
 	}

//	form.frmCurrentLoanAmount.value = dp((form.frmCurrentLoanAmount.value)*1)
//	form.frmCurrentLoanRepayments.value = dp((form.frmCurrentLoanRepayments.value)*1)
//	form.frmHouseholdGrossIncome.value = dp((form.frmHouseholdGrossIncome.value)*1)
	return true
}

