/******************************************************************************
**	Validates a date given as a 8 character string
******************************************************************************/

function IsValidDate (strFMode, theDate, blnMandatory) {
	
	var theDay = theDate.substring(6,9);
	var theMonth = theDate.substring(4,6);
	var theYear = theDate.substring(0,4);

	var correctDays		= 0;

	//alert (theDay); 
	//alert (theMonth); 
	//alert (theYear); 	

	if (blnMandatory == false && theDate == "00000000") {
		return true;
	}
	else {

		//Check that all have value other than "00", "00, "0000"
			if (theDay == "00") {
				if (strFMode != "MONTHYEAR") {			
					return false;
				}		
			}
			
			if (theMonth == "00") {
				return false;
			}

			if (theYear == "0000") {
				return false;
			}


		// 30 days has September,April, June and November
		if ((theMonth == 4) || (theMonth == 6)|| (theMonth == 9)|| (theMonth ==11)) {
			correctDays = 30;
		}
		
		else { // All the rest have 31 except February
			if((theMonth == 2)) {	
				if (( theYear % 4 == 0 && theYear % 100 != 0) || theYear % 400 == 0 ) {
					correctDays = 29;
				}
				else {
					correctDays = 28;
				}
			}
			else {
				correctDays = 31;
			}

		}					

		if (theDay > correctDays) {
			return false;
		}

		if (blnMandatory == true && theDate == "00000000") {
			return false;
		}
	return true;
	}

}

/******************************************************************************
**	Validates a date given integer Year, Month & Day
******************************************************************************/
function ValidateDate(aintYear, aintMonth, aintDay) {
	var intCorrectDays	= 0;
	
	if ((aintYear == 0) || (aintMonth == 0) || (aintDay == 0)) {
		return false;
	} else {
		// 30 days has September,April, June and November
		if ((aintMonth == 4) || (aintMonth == 6)|| (aintMonth == 9)|| (aintMonth ==11)) {
			intCorrectDays = 30;
		} else {
			// All the rest have 31 except February
			if((aintMonth == 2)) {		
				if (( aintYear % 4 == 0 && aintYear % 100 != 0) || aintYear % 400 == 0 ) {
					intCorrectDays = 29;
				} else {
					intCorrectDays = 28;
				}
			} else {
				intCorrectDays = 31;
			}
		}
	
		if (aintDay > intCorrectDays) {
			return false;
		}
	
		return true;
	
	}
}

function CalcAge(aintYear, aintMonth, aintDay) {
	var today = new Date();
	var intAge = 0;
	var iYearNow = today.getFullYear();
	var iMonthNow = (today.getMonth() +1);
	var iDayNow = today.getDate();
	
	intAge = iYearNow - aintYear;
	
	if (iMonthNow == aintMonth) {
		if (iDayNow >= aintDay) {
			
			intAge++;
		}
	} else {
		if (iMonthNow > aintMonth) {
			intAge++;
		}
	}	
	return intAge;
}

/******************************************************************************
**	Validates an e-mail address for format
******************************************************************************/
function IsEmail(astrEmailAddress) {

	var reEMail = /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{1,7}|[0-9]{1,3})(\]?)$/;
	return reEMail.test(astrEmailAddress);
}

/******************************************************************************
**	Validates an input
**  Modified 20050420 Paul S Now allows greater range of punctuation characters
******************************************************************************/
function IsValidString(astrInput) {
	//OLD VERSION:
	//var reInput = /^([0-9A-Za-z@\.\'\,\ ]{1,255})$/;

	//allows alpha-numeric . ' , [space] / : ; " ( ) [ ] # { } ~ | ? + = * & _
	var reInput = /^([0-9A-Za-z@\.\'\,\ \/\-\:\;\"\(\)\[\]\#\!\{\}\~\|\?\+\=\*\&\_]{1,255})$/;
	return reInput.test(astrInput);
}

/******************************************************************************
**	Removes leading and trailing spaces from a string
******************************************************************************/
function trim(astrString) {
	var i;
	var j;
	var strNew;
	var intStartIndex = 0;
	var intFinishIndex = astrString.length;
	var blnFoundChar = false;

	// if the string has a length then
	if (intStartIndex != intFinishIndex) {
		//loop through string from the front counting how many spaces occur before the first character
		for (i = 0; i <= astrString.length - 1; i++) {
			if ((astrString.charAt(i) == ' ') && (blnFoundChar == false)) {
				intStartIndex = i + 1;
			} else {
				blnFoundChar = true;
			}
		}
		
		blnFoundChar = false;
		
		//loop through string from the end counting how many spaces occur before the last character
		for (j = astrString.length - 1; j >= 0; j--) {
			if ((astrString.charAt(j) == ' ') && (blnFoundChar == false)) {
				intFinishIndex = intFinishIndex - 1;
			} else {
				blnFoundChar = true;
			}
		}
	
		strNew = astrString.substr(intStartIndex, intFinishIndex - intStartIndex);
	} else {
		strNew = astrString;
	}
	
	return strNew;
}

/******************************************************************************
**	Approximate word count function; mimics MS Word.
******************************************************************************/
function countWords(strTarget) {
	var arrWords = strTarget.match(/\S+/g);
	if (arrWords) return arrWords.length;
	return 0;
}

/******************************************************************************
**	Returns true if argument is an integer.
******************************************************************************/
function isInteger(intTarget) {
	var reInteger = /^[0-9]+$/;
	return reInteger.test(intTarget);
}

/******************************************************************************
**	Returns true if first date is earlier. Handles dates with or without day.
******************************************************************************/
function isFirstDateEarlier() {
	if (arguments.length == 4) {    // Month1,Year1,Month2,Year2
		if (arguments[1] < arguments[3]) return true;
		if (arguments[1] > arguments[3]) return false;
		if (arguments[0] <= arguments[2]) return true;    // Allows identical months when not using days
		if (arguments[0] > arguments[2]) return false;
	}
	else if (arguments.length == 6) {    // Date1,Month1,Year1,Date1,Month2,Year2
		if (arguments[2] < arguments[5]) return true;
		if (arguments[2] > arguments[5]) return false;
		if (arguments[1] < arguments[4]) return true;
		if (arguments[1] > arguments[4]) return false;
		if (arguments[0] < arguments[3]) return true;
		if (arguments[0] >= arguments[3]) return false;    // Does not allow identical days
	}
	else {
		window.alert("Function isFirstDateEarlier requires 4 or 6 arguments:\n\nd1,m1,y1,d2,m2,y2 or\nm1,y1,m2,y2");
		return false;
	}
}

/******************************************************************************
**	Prevents form from being submitted twice. Used as part of a checkForm
**  function, returns false if run more than once.
******************************************************************************/
function isSubmitted() {
	if (blnSubmitted == true) return true;
	else {
		blnSubmitted = true;
		return false;
	}
}
