// JavaScript external functions
// Updated 4/9/2000

// A utility function that returns true if a string contains only
// whitespace characters
function isblank(s)
{
	for(var i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
	}
	return true;
}

// A utility function that returns true if a string contains only
// numeric characters
function isemail(s)
{
	if((s.length < 5) || (s.indexOf("@")== -1) || (s.charAt(0) == "@") || (s.indexOf(".") == -1) || (s.charAt(s.indexOf("@")+1) == ".") || (s.charAt(s.indexOf("@")-1) == ".")) return false;
	return true;
}


// This is the function that performs form verification. It will be invoked
// from the onSubmit() event handler.  The handler should return whatever
// value this function returns
function verify(f)
{
	var msg;
	var empty_fields = "";
	var errors = false;
	
	// Loop through the elements of the form, looking for all text
	// and textarea elements that don't have an "optional" property
	// defined. Then, check for fields that are empty and make a list
	// of them. 
	for(var i = 0; i < f.length; i++) {
		var e = f.elements[i];
		
		// Check for Required Fields
		//
		if(e.name.substring(0,4)=="req_" && !e.optional) {
			if((e.type=="text"||e.type=="textarea"||e.type == "password")&&(e.value=='' || isblank(e.value))) {
				empty_fields += "\n        " + e.name.substring(6,e.name.length);
				setColor(e, clr[2]);
				continue;
			}
		
			if(e.type=="select-one" || e.type=="select-multiple") {
				if(e.selectedIndex==-1 || e.options[e.selectedIndex].value=="") {
					empty_fields += "\n        " + e.name.substring(6,e.name.length);
					setColor(e, clr[2]);
					continue;
				}
			}
		}
		// Check for Valid Dates
		//
		if(e.name.substring(4,6)=="d_") {
			if(!checkdate(e)) {
				empty_fields += "\n        " + e.name.substring(6,e.name.length) + " - contains an invalid date";
				setColor(e, clr[2]);
				continue;
			}
		}
		// Check for Numerics only
		//
		if(e.name.substring(4,6)=="n_") {
			if(isNaN(e.value)) {
				empty_fields += "\n        " + e.name.substring(6,e.name.length) + " - contains a non-numeric character";
				setColor(e, clr[2]);
				continue;
			}
		}
		//Check for valid email addresses
		//
		if(e.name.substring(4,6)=="e_") {
			if((!isemail(e.value)) && (e.value)) {
				empty_fields += "\n        " + e.name.substring(6,e.name.length) + " - invalid email address";
				setColor(e, clr[2]);
				continue;
			}
		}	
	}
	
	// Now, if there were any errors, display the messages, and
	// return false to prevent the form from being submitted	.
	// Otherwise return true.
	if (!empty_fields && !errors) return true;

	msg  = "______________________________________________________\n\n"
	msg += "The form was not submitted because of the following errors(s).\n";
	msg += "Please correct these error(s) and re-submit.\n";
	msg += "______________________________________________________\n\n"

	if(empty_fields) {
		msg += "- The following field(s) are empty or invalid:" + empty_fields + "\n";
		
	}
	
	alert(msg);
	return false;
}

// Hilight every input field
// 
function highlight(state) {
	element=event.srcElement;
	if (element.tagName=='INPUT') {
		etype=element.type;
		if ((etype=='text' || etype=='password')==1) {
			element.style.backgroundColor=clr[state];
			//element.focus();
		}
		//element.focus();
	}
	if ((element.tagName=='TEXTAREA') || (element.tagName == 'SELECT')) {
		element.style.backgroundColor=clr[state];
		//if (element.tagName=='TEXTAREA') element.focus();
		
	}
}

// Set the background colour of a field
//
function setColor(el, bg) {
  if (el.style) el.style.backgroundColor = bg;
}


function checkdate(objName) {
	var datefield = objName;
	if (chkdate(objName) == false) {
		datefield.select();
		//alert("That date is invalid.  Please try again.");
		datefield.focus();
		return false;
	}
	else {
		return true;
	}
}

function chkdate(objName) {
	var strDate;
	var strDateArray;
	var strDay;
	var strMonth;
	var strYear;
	var intday;
	var intMonth;
	var intYear;
	var booFound = false;
	var datefield = objName;
	var strSeparatorArray = new Array("-"," ","/",".");
	var intElementNr;
	var err = 0;
	var strMonthArray = new Array(12);
	strMonthArray[0] = "Jan";
	strMonthArray[1] = "Feb";
	strMonthArray[2] = "Mar";
	strMonthArray[3] = "Apr";
	strMonthArray[4] = "May";
	strMonthArray[5] = "Jun";
	strMonthArray[6] = "Jul";
	strMonthArray[7] = "Aug";
	strMonthArray[8] = "Sep";
	strMonthArray[9] = "Oct";
	strMonthArray[10] = "Nov";
	strMonthArray[11] = "Dec";
	strDate = datefield.value;
	if (strDate.length < 1) {
		return true;
	}
	for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) {
		if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) {
			strDateArray = strDate.split(strSeparatorArray[intElementNr]);
			if (strDateArray.length != 3) {
				err = 1;
				return false;
			}
			else {
				strDay = strDateArray[0];
				strMonth = strDateArray[1];
				strYear = strDateArray[2];
				booFound = true;
			}
		}
	}
	if (booFound == false) {
		return false;
	}
	if (strYear.length == 2) {
		strYear = '20' + strYear;
	}
	//intday = parseInt(strDay, 10);
	intday = strDay;
	if (isNaN(intday)) {
		err = 2;
		return false;
	}
	//intMonth = parseInt(strMonth, 10);
	intMonth = strMonth;
	if (isNaN(intMonth)) {
		for (i = 0;i<12;i++) {
			if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase()) {
				intMonth = i+1;
				strMonth = strMonthArray[i];
				i = 12;
			}
		}
		if (isNaN(intMonth)) {
			err = 3;
			return false;
		}
	}
	//intYear = parseInt(strYear, 10);
	intYear = strYear;
	if (isNaN(intYear)) {
		err = 4;
		return false;
	}
	if (intMonth>12 || intMonth<1) {
		err = 5;
		return false;
	}
	if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) {
		err = 6;
		return false;
	}
	if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) {
		err = 7;
		return false;
	}
	if (intMonth == 2) {
		if (intday < 1) {
			err = 8;
			return false;
		}
		if (LeapYear(intYear) == true) {
			if (intday > 29) {
				err = 9;
				return false;
			}
		}
		else {
			if (intday > 28) {
				err = 10;
				return false;
			}
		}
	}
	if (strDay.length == 1) {
		strDay = '0' + strDay;
	}

	if (strMonth.length == 1) {
		strMonth = '0' + strMonth;
	}

	//datefield.value = intday + " " + strMonthArray[intMonth-1] + " " + strYear;
	datefield.value = strDay + "/" + strMonth + "/" + strYear;
	return true;
}

function LeapYear(intYear) {
	if (intYear % 100 == 0) {
		if (intYear % 400 == 0) { return true; }
	}
	else {
		if ((intYear % 4) == 0) { return true; }
	}
	return false;
}

function doDateCheck(from, to) {
	if (Date.parse(from.value) <= Date.parse(to.value)) {
		alert("The dates are valid.");
	}
	else {
		if (from.value == "" || to.value == "") 
			alert("Both dates must be entered.");
		else 
			alert("To date must occur after the from date.");
	}
}

function updateList(f, objname, i) {
	if (i) {
		f.options[0].text = objname;
		f.options[0].value = i;
		f.options[0].selected = true;
	}
}
