/*------------------------------------------------------------------------------*\
' Function:     numberOnly
' Description:  Function used to only permit users to enter numbers as valid 
'               input in a given textfield.
' Arguments:    myEvent - an event object
'               formElement - the element on the form this function is to 
'               interact with.
' Return Value: Boolean
\*------------------------------------------------------------------------------*/
function numberOnly(myEvent, formElement, blnAllowDecimal){
	var intPressedVal = event.keyCode;
	
	// alert("Char (" + String.fromCharCode(intPressedVal) + "): " + intPressedVal);
	
	return validKey("n", intPressedVal, blnAllowDecimal);
}


/*------------------------------------------------------------------------------*\
' Function:     alphaOnly
' Description:  Function used to only permit users to enter letters as valid 
'               input in a given textfield.
' Arguments:    myEvent - an event object
'               formElement - the element on the form this function is to 
'               interact with.
' Return Value: Boolean
\*------------------------------------------------------------------------------*/
function alphaOnly(myEvent, formElement){
	var intPressedVal = event.keyCode;

	if (validKey("a", intPressedVal) || intPressedVal == 222 || intPressedVal == 32)
		return true;
	else
		return false;
}


/*------------------------------------------------------------------------------*\
' Function:     validDate
' Description:  Function used to validate that the user has supplied a valid date 
'               for a particular field.
' Arguments:    formElement - the element on the form this function is to 
'               interact with.
' Return Value: Boolean
\*------------------------------------------------------------------------------*/
function validDate(formElement){
	var intMonthDays = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	var intMonth, intDay, intYear;
	var intSepCount = 0; 
	var intStartVal = 0;
	var blnIsLeapYear;
	
	for (var i=0; i < formElement.value.length; i++){
		if (formElement.value.charAt(i) == '/')
			intSepCount++;
	}
	
	if (intSepCount != 2){
		alert ("Invalid date format, please enter the date as DD/MM/YYYY");

		formElement.focus();
		return false;
	}
	
	// extract the various sections from the string
	intDay = parseInt(formElement.value.substr(intStartVal, formElement.value.indexOf("/")));
	intStartVal = formElement.value.indexOf("/") + 1;
	intMonth = parseInt(formElement.value.substr(intStartVal, formElement.value.indexOf("/", intStartVal) - intStartVal),10);
	intStartVal = formElement.value.indexOf("/", intStartVal) + 1;
	intYear = parseInt(formElement.value.substr(intStartVal));
	
	if (String(intYear).length != 4){
		alert ("Invalid date format, please enter the date as DD/MM/YYYY");

		formElement.focus();
		return false;
	}
	
	// determine if the year is a leap year
	if (intYear % 400 == 0)
		blnIsLeapYear = true;
	else {
		if (intYear % 100 == 0)
			blnIsLeapYear = false;
		else if (intYear % 4 == 0)
			blnIsLeapYear = true;
		else
			blnIsLeapYear = false;
	}
	
	// make sure we have a valid month
	if (intMonth < 1 || intMonth > 12 || isNaN(intMonth)){
		alert ("The month you supplied is invalid, please enter a month between 1 and 12");

		formElement.focus();
		return false;
	}
	
	// Make sure the supplied day is valid
	if (blnIsLeapYear && intMonth == 2){
		if ((intMonthDays[intMonth-1] + 1) < intDay){
			alert ("The day you supplied is invalid, please enter a day between 1 and " + (intMonthDays[intMonth-1] + 1));
	
			formElement.focus();
			return false;
		}
	}
	else{
		if (intMonthDays[intMonth-1] < intDay){
			alert ("The day you supplied is invalid, please enter a day between 1 and " + intMonthDays[intMonth-1]);
	
			formElement.focus();
			return false;
		}
	}
	
	return true;
}


/*------------------------------------------------------------------------------*\
' Function:     dateOnly
' Description:  Function used to only permit users to enter numbers and the slash
'               (/) key as valid input in a given textfield.
' Arguments:    myEvent - an event object
'               formElement - the element on the form this function is to 
'               interact with.
' Return Value: Boolean
\*------------------------------------------------------------------------------*/
function dateOnly(myEvent, formElement){
	var intPressedVal = event.keyCode;
	
	return (validKey("n", intPressedVal) || intPressedVal == 191 || intPressedVal == 111);
}


/*------------------------------------------------------------------------------*\
' Function:     validKey
' Description:  
' Arguments:    fldType - String containing the type of textfield to be updated,
'               the valid values are:
'                  * "a" - Alphabetic
'                  * "n" - Numeric
'               intKeyCode - The corresponding ASCII code of the pressed key
'               blnSpecialCase - Boolean indicating a special exception
' Return Value: Boolean
\*------------------------------------------------------------------------------*/
function validKey(fldType, intKeyCode, blnSpecialCase){
	var blnRetVal = false;
	switch (fldType){
		case "a":
			if (intKeyCode >= 65 && intKeyCode <= 90)
				blnRetVal = true;
			break;
		case "n":
			if (intKeyCode >= 48 && intKeyCode <= 57 ||
			    intKeyCode >= 96 && intKeyCode <= 105){
				blnRetVal = true;
			}
			else if(blnSpecialCase){
				if (intKeyCode == 190 || intKeyCode == 110)
					blnRetVal = true;
			}
			break;
	}
	

	// Now check other keys (home, end, cursor, delete, backspace, tab, enter)
	if ((intKeyCode >= 35 && intKeyCode <= 40) ||
	    intKeyCode == 8 ||
	    intKeyCode == 9 ||
	    intKeyCode == 46 ||
	    intKeyCode == 13){
		blnRetVal = true;
	}

	return blnRetVal;
}


/*
'********************************************************************************
' PROCEDURE   : Functions checkFloat
' PARAMETERS  : none
' DESCRIPTION : Only period dot and numerical figures are allowed (Interest Rate fields)
' RETURNS     : true or false
'               
********************************************************************************/
function checkFloat(){
	var ascii0 = 48;
	var ascii9 = 57;
	var asciiPeriod = 46;
	var asciiMinus = 45;
	var asciiPlus = 43;
	var asciiSpace = 32;



	if (((window.event.keyCode < ascii0) || (window.event.keyCode > ascii9))
		&& (window.event.keyCode != asciiPeriod) && (window.event.keyCode != asciiMinus))
		return false
	else
		return true;
}


/*
'********************************************************************************
' PROCEDURE   : Functions ValidateFloatFld
' PARAMETERS  : el (element value)
' DESCRIPTION : Only one period dot is allowed (Interest Rate fields)
' RETURNS     : element value
'               
********************************************************************************/
function ValidateFloatFld(el){
	var sVal = el.value;
	if (sVal != '') {
		if (isNaN(sVal)) {
			alert("Please enter a valid number");
			el.focus();
			return false;
		}
	}
	return true;
}