var validate_version = "2.0";
/**********************************************************
 * Form Validation library                                *
/**********************************************************

****************************
*  Change History          *
****************************
2.1		Shawn  10/27/2004
		Tooltips are now placed on invalid markers
		
2.0     Shawn  10/08/2004
		Dot net version started
		
1.2.8	Shawn  06/01/2004
		Added asGeac and asEmail

1.2.7	Shawn  03/8/2003
		Obsolete asTime with asTimeFmt

1.2.6	Luke  03/8/2003
		Make change to asTime to fix error when adding leading zeros
		 
1.2.5	Shawn 09/30/2003
		You can now use required="false"

1.2.4	Shawn 08/26/2003
		MAXLENGTH and MINLENGTH now work on textarea within checkForm function
		Added chkSze Function - onkeydown call this function on textarea inputs to 
		prevent them from typing more than they can.  You can circumvent with paste.

1.2.3	isdvlws 01/31/2003
		Now always passes objField to specific validation functions

1.2.2	isdvlws 01/31/2003
		added the "minlength" attribute

1.2.1	Started tracking changes


************
* SYNOPSIS *
************


It allows you to easily add simple validation to a form including the requirement 
for a field to be completed and validation as various data types including:
asDate
asTime
asTimeFmt must set FMT="AMPM" or FMT="24"
asPercentage
asCurrency
asNumber
asInteger
asPhone
asMMDDYYYY
asDayMMDDYYYY
asGeac
asEmail

To use the library simply include a client-side script reference to it in your page e.g.
	<SCRIPT LANGUAGE="JavaScript" SRC="/your_location/validate.js"></ SCRIPT>

Add validation to the form using ... (this causes validation on submission of the form)
	<FORM ONSUBMIT="return checkForm(this)" METHOD=POST ACTION="whatever.asp">

Add validation to the fields e.g. (use the onchange event if you would like to validate as fields are entered)
	<INPUT TYPE=TEXT required validate="asNumber" onchange="checkField(this)" NAME="Amount Ordered" 
	VALUE="" SIZE=10 MAXLENGTH=10 minlength="3">
	<INPUT TYPE=TEXT required validate="asDate" onchange="checkField(this)" NAME="Date Ordered" 
	VALUE="" SIZE=10 MAXLENGTH=10>
The "required" attribute is optional and is used to force the user to complete the field
The "minlength" attribute is used to force the user to enter a specific amount of characters

Example:
--------
	<FORM ONSUBMIT="return checkForm(this)" NAME="theForm">
	<INPUT
		TYPE="text"
		NAME="theField"
		TITLE="The Field"
		onchange="checkField(this);"  
		validate="asNumber"
		required
		minlength="3"
	>
	<INPUT  TYPE="submit">
	</FORM>

***********************************************************/

function switchMarker(obj, isValid, invalidMessage) {
	var fieldName = obj.id;
	//window.status = fieldName;
	if (fieldName != undefined && fieldName != "") {
		var markerName = "blankmarker" + fieldName;
		var marker = document.all(markerName);
		if (marker != undefined) {
			if (isValid) {
				var blankMarker = getBlankMarker(obj);
				marker.innerHTML = blankMarker;
				marker.title = invalidMessage;
			} else {
				marker.innerHTML = geInvalidMarker;
				marker.title = invalidMessage;
			}
		}
	}
}

function getBlankMarker(obj) {
	if (typeof(obj.blankmarker) == "string") {
		return obj.blankmarker;
	} else {
		return geBlankMarker;
	}
}

var geValidationSummaryClient = null;
function addSummaryMessage(objForm, strMessage) {
	// wingdings character considerations
	// 6 = hourglass
	// O = flag
	// x = checkbox
	// D = thumbs down
	
	strMessage = "<b><font face=wingdings size=5 color=#b22222>x</font> The following information could not be processed:</b><br><br>" + strMessage;
	// insert a validation summary if one does not exist
	if (geValidationSummaryClient == null) {
		var existingSummary = document.all("GEvalidationsummary");
		if (existingSummary != undefined) {
			geValidationSummaryClient = existingSummary;
		} else {
			var summaryObject = "<div id=\"GEvalidationsummary\" style=\"padding:5px;width:75%;border:solid black 1px;background:white;font-family:verdana;font-size:10pt;\">" + strMessage + "</div><br>";
			objForm.insertAdjacentHTML("beforeBegin", summaryObject);	
			geValidationSummaryClient = document.all("GEvalidationsummary");;
		}
		
	}

	geValidationSummaryClient.innerHTML = strMessage;
	
}

var fieldInvalidHtmlBullet = "<font face=wingdings size=1 color=#b22222>t</font> ";
var undefined; // = (void 0); in JavaScript 1.1 only

function GEcheckForm(objForm) {
	var boolThisField, intCounter;
	var objField, strName, fnType, typeVal, strVal;
	var aRadios = new Object();
	var strMessage = ""; var htmlMessage = "";
	var boolSubmit = true;
	for (intCounter = 0; intCounter < objForm.length; intCounter++) {
		objField = objForm(intCounter);
		
		if (!objField.disabled) {
			var isValid = true;
			var markerInvalidToolTip = "";
			if (objField.type == "text") {
				boolThisField = true;

				var tempLabel = objField.label;
				if (tempLabel != undefined && typeof(tempLabel) == "string" && tempLabel != "") {
					strName = objField.label;
				}
				else {
					strName = objField.name;
				}
				if (objField.required != undefined && isNull(objField)) {
					if (objField.required != "false") {
						boolThisField = false;
						markerInvalidToolTip = strName + isNull.message;
						strMessage = strMessage + "\n" + markerInvalidToolTip;
						htmlMessage = htmlMessage + fieldInvalidHtmlBullet + strName + isNull.message + "<BR>";
					}
				}
				else if (!isNull(objField) && objField.minlength) {
					var strValue = new String(objField.value);
					if (strValue.length < objField.minlength){
						boolThisField = false;
						markerInvalidToolTip = strName + " must contain at least " + objField.minlength + " characters.";
						strMessage = strMessage + "\n" + markerInvalidToolTip;
						htmlMessage = htmlMessage + fieldInvalidHtmlBullet + strName + " must contain atleast " + objField.minlength + " characters.<br>";
						
					}
				}
				else if (!isNull(objField) && typeof(eval(objField.validate)) == "function") {
					strVal = objField.value
					fnType = eval(objField.validate);
					// call specific validation function and send it value and the field object
					typeVal = fnType(strVal, objField);
					if (typeVal == undefined) {
						boolThisField = false;
						if (typeof(fnType.message == "string")) {
							markerInvalidToolTip = strVal + " in " + strName + " " + fnType.message;
							strMessage = strMessage + "\n" + markerInvalidToolTip;
							htmlMessage = htmlMessage + fieldInvalidHtmlBullet + strVal + " in " + strName + " " + fnType.message + "<BR>";
						}
						else {
							markerInvalidToolTip = strVal + " in " + strName + " failed " + objField.validate + "()";
							strMessage = strMessage + "\n" + markerInvalidToolTip;
							htmlMessage = htmlMessage + fieldInvalidHtmlBullet + strVal + " in " + strName + " failed " + objField.validate + "()<br>";
						}
					}
					//} else {
						// populate the field with the reformatted value.
						// ssn might change from 123456789 to 123-45-6789
					//	objField.value = typeVal;
					//}
				}
				if (boolSubmit && !boolThisField) {
					boolSubmit = false;
					selectField(objField);
					//objField.focus(); // seems to fire onblur for objField
					//objField.select(); // seems to fire onblur for objField
				}
				
				isValid = boolThisField;
			}
			else if (objField.type == "textarea") {
				var fieldInnerLength = parseInt(objField.innerHTML.length, 10);
				strName = objField.title;
				if (objField.MAXLENGTH != undefined) {
					if (fieldInnerLength >= parseInt(objField.MAXLENGTH, 10)) {
						markerInvalidToolTip = strName + " can not be longer than " + objField.MAXLENGTH + " characters.";
						strMessage = strMessage + "\n" + markerInvalidToolTip;
						htmlMessage = htmlMessage + fieldInvalidHtmlBullet + strName + " can not be longer than " + objField.MAXLENGTH + " characters.<br>";
						boolSubmit = false;
						isValid = false;
					}
				}
				if (objField.MINLENGTH != undefined) {
					if (fieldInnerLength <= parseInt(objField.MINLENGTH, 10)) {
						markerInvalidToolTip = strName + " must be at least " + objField.MINLENGTH + " characters.";
						strMessage = strMessage + "\n" + markerInvalidToolTip;
						htmlMessage = htmlMessage + fieldInvalidHtmlBullet + strName + " must be at least " + objField.MINLENGTH + " characters.<br>";
						boolSubmit = false;
						isValid = false;
					}
				}
				
				if (fieldInnerLength == 0) {
					if (objField.required != undefined || objField.required=="true") {
						boolSubmit = false;
						isValid = false;
						markerInvalidToolTip = strName + isNull.message;
						strMessage = strMessage + "\n" + markerInvalidToolTip;
						htmlMessage = htmlMessage + fieldInvalidHtmlBullet + strName + isNull.message + "<BR>";
					}
				}
				
			}
			else if (objField.type.slice(0,6) == "hidden") {
				boolThisField = true;
				if (typeof(objField.title == "string") && objField.title != "") {
					strName = objField.title;
				}
				else {
					strName = objField.name;
				}
				if (objField.required != undefined && isNull(objField)) {
					boolThisField = false;
					isValid = false;
					markerInvalidToolTip = strName + isNull.message;
					strMessage = strMessage + "\n" + markerInvalidToolTip;
					htmlMessage = htmlMessage + fieldInvalidHtmlBullet + strName + isNull.message + "<BR>";
				}
				if (boolSubmit && !boolThisField) {
					boolSubmit = false;
					isValid = false;
					//objField.focus(); // seems to fire onblur for objField
					//objField.select(); // seems to fire onblur for objField
				}
			}
			else if (objField.type.slice(0,6) == "select") {
				if (objField.required != undefined && objField(objField.selectedIndex).value == "") {
					if (objField.label != undefined && typeof(objField.label) == "string" && objField.label != "") {
						strName = objField.label;
					} else {
						strName = objField.name;
					}
					markerInvalidToolTip = "You must make a selection from " + strName;
					strMessage = strMessage + "\n" + markerInvalidToolTip;
					htmlMessage = htmlMessage + fieldInvalidHtmlBullet + "You must make a selection from " + strName + "<BR>";
					if (boolSubmit) {
						boolSubmit = false;
						isValid = false;
						//objField.focus(); // seems to fire onblur for objField
						selectField(objField);
					}			
				}
			}
			else if (objField.type == "radio") {
				if (aRadios[objField.name] == undefined) {
					var intCount2
					var boolReq = false;
					//var oRadio = eval(objForm.name + "." + objField.name);
					var oRadio = objForm[objField.name];
					aRadios[objField.name] = true;
					for (intCount2=0;intCount2<oRadio.length;intCount2++) if (oRadio(intCount2).required != undefined) boolReq = true;
					if (boolReq) {
						var boolCheck = false
						for (intCount2=0;intCount2<oRadio.length;intCount2++) if (oRadio(intCount2).checked) boolCheck = true;
						if (!boolCheck) {
							if (typeof(objField.title == "string") && objField.title != "") {
								strName = objField.title;
							}
							else {
								strName = objField.name;
							}
							markerInvalidToolTip = "You must make a selection in " + strName;
							strMessage = strMessage + "\n" + markerInvalidToolTip;
							htmlMessage = htmlMessage + fieldInvalidHtmlBullet + "You must make a selection in " + strName + "<BR>";
							if (boolSubmit) {
								boolSubmit = false;
								isValid = false;
								selectField(objField);
								//objField.focus();
							}
						}
					}
				}		
			}
			
			// switch the marker in front of the field
			switchMarker(objField, isValid, markerInvalidToolTip);
			
			
		} // field is not disabled
		
	} // for loop

	if (!boolSubmit) {
		addSummaryMessage(objForm, htmlMessage);
		alert("The following information could not be processed:" + strMessage);
	}
	else if (typeof(before_submit) == "function") {
		if (!before_submit(theForm)) {
			boolSubmit = false;
			// we rely on before_submit to alert with its own error message
		}
	}
	return boolSubmit;
}

function checkField(objField, blnHandleInvalid) {
	if (blnHandleInvalid == undefined) {
		blnHandleInvalid = true;
	}
	
	if (!isNull(objField) && objField.minlength && (objField.type == "text")) {
		var strValue = new String(objField.value);
		if (strValue.length < objField.minlength){
			if (blnHandleInvalid) {
				var strName = "";
				if (typeof(objField.title == "string") && objField.title != "") {
					strName = objField.title;
				}
				else {
					strName = objField.name;
				}
				alert(objField.title + " must contain atleast " + objField.minlength + " characters.");
				selectField(objField, '#FF0101');
				//objField.focus(); // this fires onblur for the field being left too??
				//objField.select();
				//objField.style.color='#FF0101';
			}
			return false;
		} 
		else
		{		
			// Changes color back if it was previously an error
			if (blnHandleInvalid) {
				if (objField.style.color == '#ff0101') { // only change color if it was previously invalid
					objField.style.color='';
				}
			}
			return true;
		}
				
	}
	
	if (!isNull(objField) && typeof(eval(objField.validate)) == "function") {
		var strVal = objField.value;
		var fnType = eval(objField.validate);
		var typeVal;
		// call specific validation function and send it value and the field object
		typeVal = fnType(strVal, objField);
		if (typeVal == undefined) {
			if (blnHandleInvalid) {
				if (typeof(fnType.message == "string")) {
					alert(strVal + fnType.message);
				}
				else {
					alert(strVal + " failed " + objField.validate + "()");
				}
			
				selectField(objField, '#FF0101');
				//objField.focus(); // this fires onblur for the field being left too??
				//objField.select();
				//objField.style.color='#FF0101';
			}
			return false;
		}
		else {
			objField.value = typeVal;
			if (blnHandleInvalid) {
				// for some reason it stores the style.color value in lowercase letters?!
				if (objField.style.color == '#ff0101') { // only change color if it was previously invalid
					objField.style.color='';
				}
			}
			return true;
		}
	}
	else {
		// Check if the field is required
		if (objField.required != undefined && isNull(objField)) {
			if (blnHandleInvalid) {
				alert(objField.title + isNull.message);
				selectField(objField, '#FF0101');
				//objField.focus(); // this fires onblur for the field being left too??
				//objField.select();
				//objField.style.color='#FF0101';
			}
			return false;
		}
		// Changes color back if it was previously an error
		if (objField.required != undefined) {
			if (blnHandleInvalid) {
				if (objField.style.color == '#ff0101') { // only change color if it was previously invalid
					objField.style.color='';
				}
			}
		}
		return true;
	}
}

function selectField(o, color) {
	// If a field is disabled it will not be able to accept focus
	try {
		objField.focus(); // this fires onblur for the field being left too??
		objField.select();
		if (color != undefined) {
			objField.style.color=color;
		}
	} catch(exception) {}
}

/* 
/////////////////////////////////////////////////////////////////////////////
To validate email address.  Just checks for @ symbol
///////////////////////////////////////////////////////////////////////////// 
*/
function asEmail(emailAddress) {
		
	if (emailAddress.length < 4) {
		return undefined;
	}
	
	if (emailAddress.indexOf("@") < 1) {
		return undefined;
	}
	
	if (emailAddress.indexOf("@") == (emailAddress.length - 1)) {
			return undefined;
	}
	
	return emailAddress;
	
}
asEmail.message = " is not a valid email address.";


/* 
/////////////////////////////////////////////////////////////////////////////
To validate Giant Eagle Advantage Cards
///////////////////////////////////////////////////////////////////////////// 
*/
function asGeac(geacNumber) {
	if (geacNumber.length != 12) {
		return undefined;
	}
	
	if (geacNumber.substr(0,1) != "4") {
		return undefined;
	}
	
	if (!onlyContainsNumbers(geacNumber)) {
		return undefined;
	}
	
	return geacNumber;
}
asGeac.message = " is not a valid Giant Eagle Advantage Card number.  Make sure it is the number from the back of the card, is 12 digits and starts with the number 4.";

/* 
/////////////////////////////////////////////////////////////////////////////
To Social Security Number
///////////////////////////////////////////////////////////////////////////// 
*/
function asSSN(strSSN) {
	var newSSN = "";
	var parts = strSSN.split("-");
	var length = parts.length;
	for (var n = 0; n < length; n++) {
		newSSN += parts[n];		
	}
	
	// 123456789
	if (newSSN.length != 9) { 
		return undefined;
	}
	
	if (isNaN(newSSN)) {
		return undefined;
	}
	
	strSSN = newSSN.substr(0,3) + "-" + newSSN.substr(3,2) + "-" + newSSN.substr(5,4);
	return strSSN;
}
asSSN.message = " is not a valid SSN (use 123-45-6789)";

/* 
/////////////////////////////////////////////////////////////////////////////
To validate phone
///////////////////////////////////////////////////////////////////////////// 
*/
function asPhone(strPhone) {
// 412 area code assumed when not entered		
// accepts:										
//	(412) 555-1212								
//	4125551212									
//	412-555-1212								
//	5551212										
//	555-1212									
//	any delimiter in place of the - on the above
//												
//	@returns (412) 555-1212

	// (412) 821-1405
	if (strPhone.length == 14) { 
		strPhone = strPhone.substr(1,3) + strPhone.substr(6,3) + strPhone.substr(10,4);
	}

	// all 10 digits with area code and delimiter
	if (strPhone.length == 12) { 
		strPhone = strPhone.substr(0,3) + strPhone.substr(4,3) + strPhone.substr(8,4);
	}

	// 7 digits with delimiter, but no area code (assume 412)
	if (strPhone.length == 8) {
		strPhone = "412" + strPhone.substr(0,3) + strPhone.substr(4,4);
	}

	
	// all digits with area code, no delimiter
	if (strPhone.length == 10) { 
		if (isNaN(strPhone)) {
			return undefined;
		}
	}
	
	// digits without area code, no delimiter
	if (strPhone.length == 7) { 
		if (isNaN(strPhone)) {
			return undefined;
		}
		// on 7 digit # assume 412 area code
		strPhone = "412" + strPhone;
	}
	
	strPhone = "(" + strPhone.substr(0,3) + ") " + strPhone.substr(3,3) + "-" + strPhone.substr(6,4);
	
	// now double check the parse
	var strCheck = strPhone.substr(1,3) + strPhone.substr(6,3) + strPhone.substr(10,4);
	if (isNaN(strCheck)) {
		return undefined;
	}
	
	return strPhone;
	
}
asPhone.message = " is not a valid phone number (use 412-555-1212)";

function asGeacHouseholdNumber(strGeacHouseholdNumber) {
// 00010005
//												

	if (strGeacHouseholdNumber.length != 8) { 
		return undefined;
	}
	
	if (isNaN(strGeacHouseholdNumber)) {
		return undefined;
	}
	
	return strGeacHouseholdNumber;
	
}
asGeacHouseholdNumber.message = " is not a valid household number";

/* 
/////////////////////////////////////////////////////////////////////////////
To validate dates
///////////////////////////////////////////////////////////////////////////// 
*/
function asDate(strDate) {
	var valDate = parseDate(strDate);
	if (isNaN(valDate)) {
		return undefined;
	}
	else {
		return valDate;
	}
}
asDate.message = " is not a valid date (use MM/DD/YYYY)";

function asZipCode(strZipCode) {
	//if (!(strZipCode.length == 5 || strZipCode.length == 9 || strZipCode.length == 10)) {
	if (!(strZipCode.length == 5 || strZipCode.length == 10)) {
		return undefined;
	}
	
	if (strZipCode.length == 5) {
		return asNumber(strZipCode);
	}

	/*if (strZipCode.length == 9) {
		if (asNumber(strZipCode) == undefined) {
			return undefined;
		}
		var s1 = strZipCode.substr(0,5);
		var s2 = strZipCode.substr(5,4);
		
		return s1 + "-" + s2;		
	}*/
	
	if (strZipCode.length == 10) {
		var dash = strZipCode.substr(5,1);
		if (dash != "-") {
			return undefined;		
		}
		
		var s1 = strZipCode.substr(0,5);
		var s2 = strZipCode.substr(6,4);
		
		var s1Result = asNumber(s1);
		var s2Result = asNumber(s2);
		
		if (s1Result == undefined || s2Result == undefined) {
			return undefined;		
		}
		
		return s1 + "-" + s2;
	}
	
	return strZipCode;
	
	
}
asZipCode.message = " is not a valid zip code (use xxxxx or xxxxx-xxxx)";


/* 
/////////////////////////////////////////////////////////////////////////////
To validate US dates in the format MM/DD/YYYY
///////////////////////////////////////////////////////////////////////////// 
*/
function asMMDDYYYY(strDate) {
	// check to see if they just entered numbers
	if (!isNaN(strDate)) {
		if (strDate.length == 8) {
			strDate = strDate.substr(0,2) + "/" + strDate.substr(2,2) + "/" + strDate.substr(4,4);
		}
	}
	
	// Make sure the date is delimited properly.
	// If the user entered all numbers the above code adds the delimiter	
	var strDelimiter = getDelimiter(strDate);
	if (strDelimiter == null) {
		return undefined;
	} 
	
	var dateParts = strDate.split(strDelimiter);
	if (strDelimiter != "/") { // If they used a different delimiter replace it with / so Date.parse below works
		strDate = dateParts[0] + "/" + dateParts[1] + "/" + dateParts[2];
	}
	
	// We need to save the last 4 digits of the year from the string for use later
	//var strYear = strDate.substr(strDate.length - 4 ,4); // I commented this out as the last 4 digits is not always the year 1/1/03

	var valDate = new Date(Date.parse(strDate));
	
	if (isNaN(valDate)) {
		return undefined;
	}
	else {
		// Put together date in the format MM/DD/YYYY
		var strDD = new String(valDate.getDate());
		if (strDD.length < 2) { strDD = "0" + strDD; }
		var strMM = new String(valDate.getMonth()+1);
		if (strMM.length < 2) { strMM = "0" + strMM; }

		// We'll just do a split and grab the last element
		var strYear = dateParts[2];
		var nYears = parseInt(strYear);
		if (strYear.length < 4) {
			if (nYears < 50) {
				nYears += 2000;
			} else {
				nYears += 1900;
			}
		}
		/*
		var nYears = valDate.getYear(); // Only returns 2 digit date for < 2000!! aarghh!!
										// only returns 2 digit year for anythin 19xx - sap 7/26/2002
		if (nYears < 50) {
			if (strYear.length == 4) {
				nYears = parseInt(strYear);
			} else {
				nYears += 2000;
			}
		} else if (nYears < 100) {
			nYears += 1900;
		}
		//Try use some logic to make year entry easier!
		if (nYears > 100 && nYears < 2000) { nYears += 2000; }
		if (nYears < 100 && nYears > 50) { nYears += 1900; }
		if (nYears < 50) { nYears += 2000; }
		*/

		var strYYYY = new String(nYears);
		return strMM + "/" + strDD + "/" + strYYYY;
	}
}
asMMDDYYYY.message = " is not a valid date (use MM/DD/YYYY)";

/* 
/////////////////////////////////////////////////////////////////////////////
To validate US dates in the format MM/DD/YYYY adn also specify valid days they can choose
Specify on the input which dates are invalid.
Properties include: SU, MO, TU, WE, TH, FR, SA
SU=FALSE
///////////////////////////////////////////////////////////////////////////// 
*/
function asDayMMDDYYYY(strDate, objField) {
	// check to see if the just entered numbers
	if (!isNaN(strDate)) {
		if (strDate.length == 8) {
			strDate = strDate.substr(0,2) + "/" + strDate.substr(2,2) + "/" + strDate.substr(4,4);
		}
	}
	
	// Make sure the date is delimited properly.
	// If the user entered all numbers the above code adds the delimiter	
	var strDelimiter = getDelimiter(strDate);
	if (strDelimiter == null) {
		return undefined;
	} 
	
	var dateParts = strDate.split(strDelimiter);
	if (strDelimiter != "/") { // If they used a different delimiter replace it with / so Date.parse below works
		strDate = dateParts[0] + "/" + dateParts[1] + "/" + dateParts[2];
	}
	
	// We need to save the last 4 digits of the year from the string for use later
	//var strYear = strDate.substr(strDate.length - 4 ,4); // I commented this out as the last 4 digits is not always the year 1/1/03

	var valDate = new Date(Date.parse(strDate));
	if (isNaN(valDate)) {
		return undefined;
	}
	else {
		// make sure that the date is an allowed day
		var iDay = valDate.getDay();
		switch (iDay) {
			case 0:
				// see if Sunday is allowed
				var d = objField.SU;
				if (d == undefined) {
					d = objField.su; // case insensitive
				}								
				if (d != undefined) {
					if (d == "FALSE" || d == "false") {
						asDayMMDDYYYY.message = " is a Sunday and is not valid (use MM/DD/YYYY)";
						return undefined;
					}
				}
				break;
			case 1:
				// see if Monday is allowed
				var d = objField.MO;
				if (d == undefined) {
					d = objField.mo; // case insensitive
				}								
				if (d != undefined) {
					if (d == "FALSE" || d == "false") {
						asDayMMDDYYYY.message = " is a Monday and is not valid (use MM/DD/YYYY)";
						return undefined;
					}
				}
				break;
			case 2:
				// see if Tuesday is allowed
				var d = objField.TU;
				if (d == undefined) {
					d = objField.tu; // case insensitive
				}								
				if (d != undefined) {
					if (d == "FALSE" || d == "false") {
						asDayMMDDYYYY.message = " is a Tuesday and is not valid (use MM/DD/YYYY)";
						return undefined;
					}
				}
				break;
			case 3:
				// see if Wednesday is allowed
				var d = objField.WE;
				if (d == undefined) {
					d = objField.we; // case insensitive
				}								
				if (d != undefined) {
					if (d == "FALSE" || d == "false") {
						asDayMMDDYYYY.message = " is a Wednesday and is not valid (use MM/DD/YYYY)";
						return undefined;
					}
				}
				break;
			case 4:
				// see if Thursday is allowed
				var d = objField.TH;
				if (d == undefined) {
					d = objField.th; // case insensitive
				}								
				if (d != undefined) {
					if (d == "FALSE" || d == "false") {
						asDayMMDDYYYY.message = " is a Thursday and is not valid (use MM/DD/YYYY)";
						return undefined;
					}
				}
				break;
			case 5:
				// see if Friday is allowed
				var d = objField.FR;
				if (d == undefined) {
					d = objField.fr; // case insensitive
				}								
				if (d != undefined) {
					if (d == "FALSE" || d == "false") {
						asDayMMDDYYYY.message = " is a Friday and is not valid (use MM/DD/YYYY)";
						return undefined;
					}
				}
				break;
			case 6:
				// see if Saturday is allowed
				var d = objField.SA;
				if (d == undefined) {
					d = objField.sa; // case insensitive
				}								
				if (d != undefined) {
					if (d == "FALSE" || d == "false") {
						asDayMMDDYYYY.message = " is a Saturday and is not valid (use MM/DD/YYYY)";
						return undefined;
					}
				}
				break;
				
			default:
				break;
		}
	
		asDayMMDDYYYY.message = " is not a valid date or you did not select an allowed day of the week (use MM/DD/YYYY)";
	
		// Put together date in the format MM/DD/YYYY
		var strDD = new String(valDate.getDate());
		if (strDD.length < 2) { strDD = "0" + strDD; }
		var strMM = new String(valDate.getMonth()+1);
		if (strMM.length < 2) { strMM = "0" + strMM; }
		
		// We'll just do a split and grab the last element
		var strYear = dateParts[2];
		var nYears = parseInt(strYear);
		if (strYear.length < 4) {
			if (nYears < 50) {
				nYears += 2000;
			} else {
				nYears += 1900;
			}
		}

		var strYYYY = new String(nYears);
		
		return strMM + "/" + strDD + "/" + strYYYY;
	}
}
asDayMMDDYYYY.message = " is not a valid date or you did not select an allowed day of the week (use MM/DD/YYYY)";


/* 
/////////////////////////////////////////////////////////////////////////////
To validate times! - very basic validation
///////////////////////////////////////////////////////////////////////////// 
*/
function asTime(strTime) {
	var rtnVal,strTmp,strHours,strMins;
	strTmp = strTime.replace(/:/g,"");
	rtnVal = asNumber(strTmp);
	if (rtnVal == undefined) {
		return undefined;
	} else {
		if (strTmp.length > 4) {
			strTmp = strTmp.substring(0,4);
		} else {
			while (strTmp.length < 4) {
				//strTmp = strTmp + "0";
				strTmp = "0" + strTmp;
			}
		}
		strMins = strTmp.substring(2,4);
		strHours = strTmp.substring(0,2);
		if (strHours > "23")	{ strHours = "23";}
		if (strMins > "59")		{ strMins = "59";}
		strTmp = strHours + ":" + strMins;
		return strTmp
	}
}
asTime.message = " is not a valid time (use HH:MM)";

function asTimeFmt(strTime, objField) {
	var fmt = objField.FMT;
	if (fmt == undefined) {
		fmt = objField.fmt; // case insensitive
	}
	
	strTime = strTime.toUpperCase();
	
	if (fmt == "AMPM") {
		asTimeFmt.message = " must be in the format hh:mi AMPM and between 12:00 AM and 11:59 PM";
		
		var AMPM = "";
		if (strTime.indexOf(" ") < 0) {
			var pos = strTime.indexOf("AM");
			if (pos < 0) {
				pos = strTime.indexOf("PM");
			}
			if (pos < 0) {
				AMPM = "AM";
			} else {
				AMPM = strTime.substring(pos, pos + 2);
				strTime.substring(0, strTime.length - 2);
			}
		} else { 
			var parts = strTime.split(" ");
			strTime = parts[0];
			AMPM = parts[1];
		}

		var timeParts = strTime.split(":");
		var hour = parseInt(timeParts[0]);
		if (isNaN(hour)) {
			return undefined;
		}
		
		var minute = parseInt(timeParts[1]);
		if (timeParts[1] == undefined) {
			minute = 0;
		}
		
		if (isNaN(minute)) {
			return undefined;
		}
		if (hour > 12) {
			if (AMPM == "AM" && hour < 25) {
				hour = hour - 12;
				AMPM = "PM";
			} else {
				return undefined;
			}
		} else if (hour == 0 && AMPM == "AM") {
			hour = 12;
		}
		
		if (hour > 12 || hour < 1) {
			return undefined;
		}
		
		if (minute > 59 || minute < 0) {
			return undefined;
		}
		if (!(AMPM == "AM" || AMPM == "PM")) {
			return undefined;
		}
		
		return hour + ":" + padZero(minute) + " " + AMPM;
	} else if (fmt == "24") {
		asTimeFmt.message = " must be between 0:00 and 24:00";
		
		var blnPM = false;
		if (strTime.indexOf("AM") > 0) {
			strTime.replace("AM", "");
		} else if (strTime.indexOf("PM") > 0) {
			blnPM = true;
			strTime.replace("PM", "");
		}
	
		var timeParts = strTime.split(":");
		var hour = parseInt(timeParts[0]);
		if (blnPM) {
			hour = hour + 12;
		}
		var minute = parseInt(timeParts[1]);
		if (timeParts[1] == undefined) {
			minute = 0;
		}
		if (hour > 24) {
			return undefined;
		} else if (hour == 24 && minute > 0) {
			return undefined;
		}
		if (minute > 59) {
			return undefined;
		}
		
		return padZero(hour) + ":" + padZero(minute);
		
	} else {
		alert("FMT must either be 24 or AMPM.  Also make sure your attribute is in quotes");
		return undefined;
	}
	
}
asTimeFmt.message = " is not a valid time";

function padZero(s) {
	s = new String(s);
	if (s.length == 1) {
		return "0" + s;
	} else {
		return s;
	}
}


/* 
/////////////////////////////////////////////////////////////////////////////
To allow for very basic validation of percentages. (uses asNumber)
///////////////////////////////////////////////////////////////////////////// 
*/
function asPercentage(strPercentage) {
	var nPosition,strTmp;
	var NumOfPercentageDecimals = 4; // Determines num of decimals to format to ...
	nPosition = strPercentage.indexOf("%");
	if (nPosition == -1) {
		strTmp = asNumber(strPercentage);
	} else {
		strTmp = asNumber(strPercentage.slice(0,(nPosition)));
	}
	if (strTmp == undefined) {
		return strTmp;
	} else {
		strTmp = NumDecimalPlaces("" + strTmp,NumOfPercentageDecimals) + "%";
		return strTmp;
	}
}
asPercentage.message = " is not a valid percentage";


/* 
/////////////////////////////////////////////////////////////////////////////
To allow for very basic validation of money amounts. (uses asNumber)
///////////////////////////////////////////////////////////////////////////// 
*/
function asCurrency(strCurrency) {
	var nPosition,strTmp;
	var NumOfMoneyDecimals = 2; // Determines num of decimals to format to ...
	var NumOfDigitsBetweenCommas = 3; // Determines num of digits between commas
	var strCurrency = new String(strCurrency);
	var strNewCurrency = new String("");
	//Remove any ,s present 
	for (var n=0; n < strCurrency.length; n++ ) {
		if (strCurrency.charAt(n) != ",") { strNewCurrency += strCurrency.charAt(n); }
	}
	// Get rid of $ sign
	nPosition = strNewCurrency.indexOf("$");
	if (nPosition == -1) {
		strTmp = asNumber(strNewCurrency);
	} else {
		strTmp = asNumber(strNewCurrency.slice((nPosition+1)));
	}
	
	if (strTmp == undefined) {
		return strTmp;
	} else {
		// Add commas
		strTmp = AddCommas(strTmp,NumOfDigitsBetweenCommas);
		strTmp = "$" + NumDecimalPlaces("" + strTmp,NumOfMoneyDecimals);
		return strTmp;
	}
}
asCurrency.message = " is not a valid money amount";

/* 
/////////////////////////////////////////////////////////////////////////////
Helper functions for the currency validation
///////////////////////////////////////////////////////////////////////////// 
*/
function AddCommas(strNumber,nDigits) {
		var strNumber = new String(strNumber);
		var strPrefix = new String("");
		var strDecimals = new String("");
		var strReturn = new String("");
		if (strNumber.indexOf(".") == -1) {
			strPrefix = strNumber;
			strDecimals = "00";
		} else {
			strPrefix   = strNumber.substr(0,strNumber.indexOf("."));
			strDecimals = strNumber.substr((strNumber.indexOf(".")+1));
		}
		// run thru the prefix adding commas where necessary
		var nCount = 0;
		for (var n=strPrefix.length-1; n >= 0; n-- ) {
			if (nCount == nDigits) {
				strReturn = "," + strReturn;
				nCount = 0;
			}
			nCount++;
			strReturn = strPrefix.charAt(n) + strReturn;
		}
		return strReturn + "." + strDecimals;
}
function NumDecimalPlaces(strNumber,NumDecimals) {
	if (strNumber.indexOf(".") == -1) {
		strNumber += ".";
		var n;
		for (n=0; n < NumDecimals; n++) {
			strNumber += "0"
		}
		return strNumber;
	} else {
		var strPrefix,strDecimals;
		strPrefix   = strNumber.substr(0,strNumber.indexOf("."));
		strDecimals = strNumber.substr((strNumber.indexOf(".")+1));
		while (strDecimals.length != NumDecimals) {
			if (strDecimals.length > NumDecimals) {
				strDecimals = strDecimals.slice(0,-1);
			} else {
				strDecimals += "0";
			}
		}
		return strPrefix + "." + strDecimals;
	}
}

/* 
/////////////////////////////////////////////////////////////////////////////
To validate numbers
///////////////////////////////////////////////////////////////////////////// 
*/
function asNumber(strNumber) {
	var intCounter = 0;
	if (strNumber == "0") {
		return 0;
	}
	else {
		while (strNumber.charAt(intCounter) == "0") {
			strNumber = strNumber.slice(1);
		}
		var valNumber = parseFloat(strNumber);
		if (valNumber == strNumber) {
			return valNumber;
		}
		else {
			return undefined;
		}
	}
}
asNumber.message = " is not a valid number";

/* 
/////////////////////////////////////////////////////////////////////////////
To validate integers
///////////////////////////////////////////////////////////////////////////// 
*/
function asInteger(strInteger) {
	var intCounter = 0;
	if (strInteger == "0") {
		return 0;
	}
	else {
		while (strInteger.charAt(intCounter) == "0") {
			strInteger = strInteger.slice(1);
		}
		var valInteger = parseInt(strInteger);
		if (valInteger == strInteger) {
			return valInteger;
		}
		else {
			return undefined;
		}
	}
}	
asInteger.message = " is not a valid whole number";

function isNull(objField) {
	return (objField.value == "");
}
isNull.message = " must contain a value"

var strDelims = ", ./-";
function parseDate(strDate) {
	var dateVal;
	var intCounter;
	var items = new Array();
	items[0] = "";
	for (intCounter = 0; intCounter < strDate.length; intCounter++) {
		if (strDelims.indexOf(strDate.charAt(intCounter)) > -1) {
			intCounter++; // what if seperator is at end of strDate??
			if (items[items.length - 1] != "") {
				// only add new item if last item is not null
				items[items.length] = "";
			}
		}
		else if ((items[items.length] == "") && (strDate.charAt(intCounter) == "0")) { // remove leading zeros
			intCounter++;
		}
		items[items.length-1] += strDate.charAt(intCounter);
	}
	// Check for leading zeroes and remove them!
	for (var n=0; n < items.length; n++) {
		var strNew = "";
		var blnReachedNumber = false;
		for (var j=0; j < items[n].length; j++) {
			if (items[n].charAt(j) != "0") {
				blnReachedNumber = true;
			}
			if (blnReachedNumber) {
				strNew = items[n].substring(j,items[n].length);
				break;
			} 
		}
		if (strNew == "") {
			items[n] = "0";
		} else {
			items[n] = strNew;
		}
	}
	// when using New Date("m d y") in Explorer
	// a two figure year between 70 and 99 is interpreted as 1900 + y
	// numbers over 99 are interpreted as their full date value
	// numbers less than 70 produce NaN	
	if (parseInt(items[2]) < 70) items[2] = parseInt(items[2]) + 2000;
	if (parseInt(items[1]) == items[1]) {
		dateVal = new Date(items[0] + "/" + items[1] + "/" + items[2]);
	} else {
		dateVal = new Date(items[0] + " " + items[1] + " " + items[2]);
	}
	//if (typeof(dateVal.getTime) == "function") {
	//	dateVal.toString = _dateToString;
	//}	// older JavaScript would have to do it this way rather than via the prototype
	return dateVal;
}
Date.prototype.toString = _dateToString;

function _dateToString() {
	// may not need all the type checking
	//if (typeof(dateVal.getTime) == "function") {
		return this.getDate() + " " + formatMonth(this.getMonth() + 1) + " " + formatYear(this.getYear());
	//}
	//else {
	//	return this.toString();
	//}
}

function formatMonth(intMonth) {
	// javaScript 1.2 could use a switch statement
	if (intMonth == 1) return "Jan";
	else if (intMonth == 2) return "Feb";
	else if (intMonth == 3) return "Mar";
	else if (intMonth == 4) return "Apr";
	else if (intMonth == 5) return "May";
	else if (intMonth == 6) return "Jun";
	else if (intMonth == 7) return "Jul";
	else if (intMonth == 8) return "Aug";
	else if (intMonth == 9) return "Sep";
	else if (intMonth == 10) return "Oct";
	else if (intMonth == 11) return "Nov";
	else if (intMonth == 12) return "Dec";
	else return "n/a";
}

// we have to use this because the Date.getYear() is bad
// with Explorer getYear returns a two figure date if it is 19xx
// it returns a four figure date otherwise. Officially (Netscape?)
// the date function should return the number of years since 1900
function formatYear(intYear) {
	if (intYear < 100) {
		return 1900 + intYear;
	}
	else {
		return intYear;
	}
}

// Determine the delimiter they used, checks for / . -
// used when determing the date delimiter
function getDelimiter(str) {
	if (str.indexOf("/") > 0) {
		return "/";
	} else if (str.indexOf(".") > 0) {
		return "."
	} else if (str.indexOf("-") > 0) {
		return "-"
	} else {
		return null;
	}
}

// onkeydown call this function on textarea inputs
// onkeydown="return chkSze(this);"
function chkSze(obj) {
	var k = window.event.keyCode;
	if (k == 8 || k == 9 || k == 16 || k == 17 || 
		k == 20 || k == 35 || k == 36 || k == 37 || k == 38 || k == 39 || 
		k == 40  || k == 46 || k == 116) {
		return true;
	}
	// validate text areas
	if (obj.type == 'textarea') {
		if (obj.MAXLENGTH != undefined) {
			var fieldLength = parseInt(obj.innerHTML.length,10);
			if (fieldLength >= parseInt(obj.MAXLENGTH,10)) {
				window.status = "This field can not be longer than " + obj.MAXLENGTH + " characters.";
				return false;
				//obj.innerHTML = obj.innerHTML.substr(0, obj.MAXLENGTH);
				
				//return true;
			} else {
				return true;
			}
		}
	}
}

// return true if the string only contains numbers
// return false if any of the items are not digits
function onlyContainsNumbers(s) {

	var cn_iSize = s.length;
	for (var cn_n=0; cn_n < cn_iSize; cn_n++ ) {
		var cn_place = s.charAt(cn_n);
		if (isNaN(cn_place)) {
			return false;
		}
	}
	return true;
}


//</SCRIPT>
