<!--
//************************************************************
// JAVASCRIPT LIBRARY
// For form entry / submission v3.0
//************************************************************
//
// NOTE: To surpress alerts for all these functions, global variable
// 		 surpressAlerts must be set to TRUE on the function call page.
//		 This will allow the controlling page to handle its own validation
//		 alerts.
//		 Otherwise, surpressAlerts is assumed to be FALSE.
//
//************************************************************
// Function checks the mandatory fields on a form and alerts the
// user if any are not entered. Called from the OnClick event of
// the forms Submit button. Cancels submission if any fields are
// not supplied. Arguments must be passed in the format:
//		(FormObject, 'Fieldname1', 'Fieldlabel1', 'Fieldname2', 'Fieldlabel2'......)
//************************************************************
function checkFormMandatoryFields() 
{
	
	var loop=0;
	var res='';
	var daform = arguments[0];
	
	if (typeof surpressAlerts != 'boolean') {var surpressAlerts = false;}
	
	if (daform != null) 
	{

		for (loop = 1; loop <= arguments.length-1; loop++)
		{
			var daelement = daform.elements[arguments[loop]];
			
			loop += 1
			
			if (daelement != null) 
				if (daelement.value == '') 
					res += '\n  - ' + arguments[loop];	
		}
		
		if (res != '')
		{
			if (!surpressAlerts) {alert('Please complete the following fields: \n' + res);}
			return false;
		}
		else
			return true;
	}
}


//************************************************************
// Function checks the supplied fields on a form and alerts the
// user if any are not as specified. The values of formatCodes should be:
// A : value must be alpha only
// N : value must be numeric only
// AN : value must be alphanumeric
// Arguments must be passed in the format:
//		(FormObject, formatCode, 'Fieldname1', 'Fieldlabel1', 'Fieldname2', 'Fieldlabel2'......)
//************************************************************
function checkIsAlphaNumeric()
{
	var loop=0,loop2=0;
	var res='';
	var daform = arguments[0];
	var formatCode = arguments[1];
	var daString;
	var isAlpha, isNumeric, alertWord;
	
	if (typeof surpressAlerts != 'boolean') {surpressAlerts = false;}
	
	if (daform != null) 
	{

		for (loop = 2; loop <= arguments.length-1; loop++)
		{
			var daelement = daform.elements[arguments[loop]];
			
			loop += 1;
			
			isAlpha = false;
			isNumeric = false;
			
			if (daelement != null){
				daString = daelement.value.toUpperCase();
				for (loop2 = 0; loop2 <= daString.length-1; loop2++)
				{	
					if ((daString.charCodeAt(loop2) >=65) && (daString.charCodeAt(loop2) <= 90)) {isAlpha = true}
					if ((daString.charCodeAt(loop2) >=48) && (daString.charCodeAt(loop2) <= 57)) {isNumeric = true;}
				}
				
				if (formatCode == "A") {
					if (isNumeric) {res += '\n  - ' + arguments[loop];}
					alertWord = "letters";
				} else if (formatCode == "N") {
					if (isAlpha) {res += '\n  - ' + arguments[loop];}
					alertWord = "numbers";
				} else if (formatCode == "AN") {
					if ((!isAlpha) && (!isNumeric)){res += '\n  - ' + arguments[loop];}
					alertWord = "numbers and letters";
				}
			}	
		}
		
		if (res != '')
		{
			if (!surpressAlerts) {alert('The following fields are not ' + alertWord + ' only: \n' + res);}
			return false;
		}
		else
			return true;
	}
}


//************************************************************
// Function checks a field on a form and alerts the
// user if it is of an illegal length. Maximum length can be
// restricted by the maxlength value in original form.
// Arguments must be passed in the format:
//	(field object reference (eg. document.form.fieldname), 'Fieldlabel', minimum length......)
//************************************************************
function checkFieldLength(daelement, daelementlabel, minlength) 
{
	if (typeof surpressAlerts != 'boolean') {var surpressAlerts = false;}
	
	if (daelement.value.length < minlength) {
		if (!surpressAlerts) {alert("The field '"+daelementlabel+"' must be over "+minlength+" characters long.");}
		return false;}
		
	return true;
}


//************************************************************
// Function checks date parts and returns TRUE if a valid date
// arguments are as follows:
// formObj, fieldPrefix, groupName[, allowBlank]
// where allowBlank is boolean - when TRUE, function returns TRUE 
// if all fields have no values.
//************************************************************

function verifDate()
{
	var validDate = true;
	
	var formObj = arguments[0], fieldPrefix = arguments[1], groupName = arguments[2];
	
	var dateObj = new getDateParts(formObj,fieldPrefix);
	
	var allowBlank = (arguments.length == 4 ? arguments[3] : false);
	
	if (typeof surpressAlerts != 'boolean') {var surpressAlerts = false;}
	
	if ((dateObj.valid == -1) || ((dateObj.valid == 0) && (!allowBlank))) {
		// date is invalid, or blank when not allowed
		validDate = false;
	} else {
		// all dateparts are valid, so check to see if date as a whole is OK
		if ((dateObj.month > 12) || (dateObj.day > 31)) {validDate=false;}
		
		if ((dateObj.day=="" || dateObj.month=="" || dateObj.year=="")) {validDate=false;}
		
		if (dateObj.day>29 && dateObj.month==2 && ((dateObj.year % 4)==0)) {validDate=false;}
		
		if (dateObj.day>28 && dateObj.month==2 && ((dateObj.year % 4)!=0)) {validDate=false;}
		
		if (dateObj.day==31 && (dateObj.month==4 || dateObj.month==6 || dateObj.month==9 || dateObj.month==11)){validDate=false;}
	}

	if (!validDate) {
		if (!surpressAlerts) {alert("You have not entered a valid date for the '"+groupName+"' field!");}
		return false;
	} else {
		return true;
	}
}

//************************************************************
// Function checks date is within valid range
//************************************************************
// If 6-7 arguments are found:
//
// formObject, fieldPrefix, groupName, Lday, Lmonth, Lyear[, allowBlank]
// Date must be on or after Lday/Lmonth/Lyear
//
// If 9-10 arguments are found:
//
// formObject, fieldPrefix, groupName, Lday, Lmonth, Lyear, Hday, Hmonth, Hyear[,allowBlank]
// Date must be between or on Lday/Lmonth/Lyear - Hday/Hmonth/Hyear
//************************************************************

function verifDateRange()
{
	if (typeof surpressAlerts != 'boolean') {var surpressAlerts = false;}
	
	var formObj = arguments[0], fieldPrefix = arguments[1], groupName = arguments[2];
	
	var dateObj = new getDateParts(formObj, fieldPrefix);
	
	var allowBlank = (arguments.length == 7 ? arguments[6] : false);
	allowBlank = (arguments.length == 10 ? arguments[9] : allowBlank);
	
	var inRange = true;
	
	if (verifDate(formObj,fieldPrefix, groupName,allowBlank)) {
		//date is valid, so we can perform range check
		
		if (dateObj.valid==0) {return true; } //date is valid blank so no check needed
		
		// perform lower range check
		
		var LDay = arguments[3], LMonth = arguments[4], LYear = arguments[5];
		
		if (dateObj.year < LYear) {inRange = false;}
		
		if (dateObj.year == LYear) {
			if (dateObj.month < LMonth) {inRange = false;}
			if (dateObj.month == LMonth) {
				if (dateObj.day < LDay) {inRange = false;}
			}
		}
		
		if (!inRange) {
			if (!surpressAlerts) {alert("The date specified in '"+groupName+"' occurs too early!");}
			return false;
		} else {
			if (arguments.length < 8) {return true;} // we can return if no upper range to check.
		}
		
		
		// perform upper range check if arguments are present
		
		var HDay = arguments[6], HMonth = arguments[7], HYear = arguments[8];
		 
		if (dateObj.year > HYear) {inRange = false;}
		
		if (dateObj.year == HYear) {
			if (dateObj.month > HMonth) {inRange = false;}
			if (dateObj.month == HMonth) {
				if (dateObj.day > HDay) {inRange = false;}
			}
		}
	
	if (!inRange) {
			if (!surpressAlerts) {alert("The date specified in '"+groupName+"' occurs too late!");}
			return false;
		} else {
			return true;
		}
	}
}


//************************************************************
// Function takes date pulldown fields from formObj and returns
// object with integer properties day, month and year.
// valid properties:
// -1 = invalid (some dateparts were not set)
//	0 = blank (no dateparts were set)
//	1 = valid (all dateparts were set)
//************************************************************

function getDateParts(formObj,fieldPrefix)
{
	
	if (!document.all) {
		// long-winded syntax for NN
		this.day = parseInt(formObj.elements[fieldPrefix+"day"][formObj.elements[fieldPrefix+"day"].selectedIndex].value);
		this.month = parseInt(formObj.elements[fieldPrefix+"month"][formObj.elements[fieldPrefix+"month"].selectedIndex].value);
		this.year = parseInt(formObj.elements[fieldPrefix+"year"][formObj.elements[fieldPrefix+"year"].selectedIndex].value);

	} else {
		this.day = parseInt(formObj.elements[fieldPrefix+"day"].value);
		this.month = parseInt(formObj.elements[fieldPrefix+"month"].value);
		this.year = parseInt(formObj.elements[fieldPrefix+"year"].value);	
	}
	
	this.valid = 1;
	
	if (isNaN(this.day) || isNaN(this.month) || isNaN(this.year)) {
		if (isNaN(this.day) && isNaN(this.month) && isNaN(this.year)) {
			this.valid = 0;
		} else {
			this.valid = -1;
		}
	}
}


//************************************************************
// Function checks a field on a form and alerts the
// user if it is not a valid e-mail address.
// 3 Arguments must be passed in the format:
//	(form object, field name, 'Fieldlabel')
// OR with 2 arguments:
// (field value, 'Fieldlabel')
//************************************************************
function checkIsEMail() 
{
	if (arguments.length == 3) {
		var davalue = arguments[0].elements[arguments[1]].value, fieldlabel = arguments[2];
	} else {

		var davalue = arguments[0], fieldlabel = arguments[1];
	}
	
	var atPos
	

			
	if (typeof surpressAlerts != 'boolean') {var surpressAlerts = false;}
	
	if (davalue.length > 8) {
		
		atPos = davalue.indexOf("@");
		
		if (atPos > 0) {
			if ((davalue.indexOf(".",atPos) != -1) && (davalue.indexOf(".",atPos) != davalue.length-1)) {return true}	
		}
	}
	
	if (!surpressAlerts) {alert("'"+fieldlabel+"' is not a valid e-mail address!");}
	return false;
}

//************************************************************
// Function checks a field on a form and alerts the
// user if it is not a list of valid e-mail addresses separated by ';'.
// Arguments must be passed in the format:
//	('field object reference (eg. document.form.fieldname), 'Fieldlabel')
//************************************************************
function checkMultipleEmails(formObj, fieldname, fieldlabel) 
{
	var addressString = formObj.elements[fieldname].value;
	var addresses = new Array();
	addresses[0] = "";
	var pos = 0, separatorAt, offset, offset2;
	
	if (typeof surpressAlerts != 'boolean') {var surpressAlerts = false;}
	
	while((separatorAt = addressString.indexOf(";")) != -1) {
		//remove extra space if present around semi-colon
		if (addressString.charAt(0) == " ") {offset = 1; } else {offset = 0;}
		if (addressString.charAt(separatorAt-1) == " ") {offset2 = 1; } else {offset2 = 0;}
		addresses[pos] = addressString.substring(offset, separatorAt-offset2);
		addressString = addressString.substring(separatorAt+1);
		pos ++;
	}
	
	if (addressString != "") { addresses[pos] = addressString; }
	
	for (loop = 0; loop < addresses.length; loop++) {
		if (!checkIsEMail(addresses[loop], addresses[loop])) {return false;}
	}
	return true;
}

//************************************************************
// Function checks number of words in a field.
//************************************************************
function checkNoOfWords(formObj,fieldname,fieldlabel,maxWords) {
	
	if (typeof surpressAlerts != 'boolean') {var surpressAlerts = false;}
	
	var noOfWords = formObj.elements[fieldname].value.split(" ").length;
	
	if (noOfWords > maxWords) {
		if (!surpressAlerts) {alert("There is a limit of "+maxWords+" word"+(maxWords>1 ? "s " : " ")+"in the '"+fieldlabel+"' field (you have entered "+noOfWords+").");}
		return false;
	} else {
		return true;
	}
}
// -->