/******************************************************************************************************
' Generated by: Script Generator 2
' Written by: Ardeshir (Alex) G. Battye
' Email: agb@mangroveblue.com
' URL: www.mangroveblue.com
'******************************************************************************************************
'
'******************************************************************************************************
' Form.js
'******************************************************************************************************
' Copyright (c) 2003 : MangroveBlue.com
'
' This software is provided under the terms of a License Agreement and
' may only be used and/or copied in accordance with the terms of such an
' agreement.  Neither this software nor any copy thereof may be provided
' or otherwise made available to any other person.  No title or ownership
' of this software is hereby transferred.
'******************************************************************************************************
' CHANGE HISTORY 
'******************************************************************************************************
' 200xxxxx - Created by ScriptGenerator 
'*****************************************************************************************************/

//****************************************************************************************************/
// Global variable
//	Used in isSubmitted() and submitForm()
var blnSubmitted = false;

//****************************************************************************************************/
// Prevents form from being submitted twice. 
//	Returns FALSE if run more than once
function isSubmitted() {
	if (blnSubmitted == true) 
	{
		return true;
	} else {
		blnSubmitted = true;
		return false;
	}
}

//****************************************************************************************************/
// Returns TRUE if string looks like an email address
function IsEmail(astrEmailAddress) {
	var reEMail = /^\S+@\S+\.\S+$/;
	return reEMail.test(astrEmailAddress);
}

//****************************************************************************************************/
// Returns TRUE if string looks like an web address
// To be written //

//****************************************************************************************************/
// Approximate word count (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);
}

//****************************************************************************************************/
// Date validator
function IsValidDate (strFMode, theDate, blnMandatory) {

	if (strFMode == "TIME")
	{
		var theHour = theDate.substring(0,2);
		var theMin = theDate.substring(2,4);
		if (blnMandatory == false && theDate == "00000000")
		{
			return true;
		}
		else if(theDate == "00000000")
		{
			return false;
		}
	}
	else
	{

		var theDay = theDate.substring(6,9);
		var theMonth = theDate.substring(4,6);
		var theYear = theDate.substring(0,4);

		var correctDays		= 0;

		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;
		}
	}
}

//****************************************************************************************************/
// Limits number of characters in textarea.
// Returns charcters left to form field.
// Example: onKeyDown="limitText(this.form.strVCSummary,this.form.countdown,255);"
// The following input is needed under the relevant textarea
// <INPUT readonly type="text" name="countdown" size="3" value="255" id="Text1"> characters left

function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.value = limitNum - limitField.value.length;
	}
}