function AddToValidateArray(Element)
{
    if (!document.ValidateArray) 
    {
        document.ValidateArray = new Array
    }
    document.ValidateArray[document.ValidateArray.length] = Element
}

function Validate(stopOnFailure) 
{
	errorMsg = ValidateAllItems(stopOnFailure);
	if (errorMsg != "")
	    alert("The form could not be submitted:" + errorMsg);

	return (errorMsg == ""); // false prevents form submission
} 

function ValidateAllItems(stopOnFailure)
{
	var i
	var msg
	var tofocus = true;
	var ErrorMsg = "";
	
	// Go through the Validate Array that may or may not exist
	// and call the Validate function for all elements that have one.
	// Set focus to the first failed item.
	
	if (document.ValidateArray)
	{
		for (i = 0; i < document.ValidateArray.length; i ++)
		{
			msg = document.ValidateArray[i].ValidationFunction()
			if (msg != "")
			{
				ErrorMsg += "\n\n" + msg;
				if (tofocus && document.ValidateArray[i].type != "hidden") 
				{
					if (document.ValidateArray[i].focus)
					{
						document.ValidateArray[i].focus()
						tofocus = false;
					}
				}
				
				if (stopOnFailure == "1") return ErrorMsg;
			}
  		}
	}
	document.ValidateArray = new Array
	return ErrorMsg;
}

function ValidateEMail()
{
	var msg = "";
	var val = this.value;
	var msgInvalid = "Please enter a valid e-mail address\n(a valid e-mail address contains the @ character)";
	if (this.ErrorMsg != "")
		msgInvalid = this.ErrorMsg

	//Required field validation
	var theLen = StripChars(" ",val).length
	if (theLen == 0)	
		if (!this.Required) return ""	
		else return "Required field.  " + msgInvalid
	
	//email syntax validation
	if (val.indexOf("@",0) < 0) msg = msgInvalid;
		
	if (RegExp)	
	{
		var re;
		re = /^[a-zA-Z0-9\.\-\_]+(\@)[a-zA-Z0-9\.\-\_]+(\.)[a-zA-Z]{2,3}$/
		if (!re.test(val)) msg = msgInvalid;
			
		re = /(\@)(\.)/;
		if (re.test(val))msg = msgInvalid;
		
		re = /(\.)(\@)/;
		if (re.test(val))msg = msgInvalid;
		
		re = /(\.)(\.)+/;
		if (re.test(val))msg = msgInvalid;
		
		re = /^[^a-zA-Z0-9]/;
		if (re.test(val))msg = msgInvalid;
	}
		
	return msg;
}

function ValidateRegExp()
{
	var msg = "";
	var val = this.value;
	var msgInvalid = "Invalid entry."
	if (this.ErrorMsg != "")
		msgInvlalid = this.ErrorMsg;
		
	var theLen = StripChars(" ", val).length;
	if (theLen == 0)
		if (!this.Required) return ""
		else return "Required field.  " + msgInvalid;
	
	var blnValid = true;
	if (RegExp)
	{
		var re;
		if (this.Custom1)
		{
			re = eval("/" + this.Custom1 + "/");
			blnValid = re.test(val);
			
			if (!blnValid)
			{
				if (this.Custom2)
				{
					re = eval("/" + this.Custom2 + "/");
					blnValid = re.test(val);
				}
			}
		}
	}
	if (!blnValid) return msgInvalid
	else return ""; 
}

function ValidateInteger()
{
	var msg = "";
	var val = this.value;
	var msgInvalid = "Please enter a number.";
	if (this.ErrorMsg != "")
		msgInvalid = this.ErrorMsg

	var theLen = StripChars(" \n\t\r",val).length
	if (theLen == 0)	
		if (!this.Required) return ""	
		else return "Required field.  " + msgInvalid

	theString = StripChars(" ",val);

	// remove leading zeros (zeros are only leading if there is more than one char)
	while (theString.length > 1 && theString.substring(0,1) == "0")
	{
		theString = theString.substring(1, theString.length);
	}

	var val = parseInt(theString);
	if (isNaN(val)) return msgInvalid;
	if (val < -32768) return msgInvalid;
	if (val > 32767) return msgInvalid;
	
	// check for non-digits (and minus sign). Do this by converting number
	// back to a string and comparing it to original string.
	if (val.toString() != theString) return msgInvalid;
	
	if (this.Custom1)
	{
		var intMin = parseInt(this.Custom1)
		if (val < intMin) return msgInvalid;
	}
	if (this.Custom2)
	{
		var intMax = parseInt(this.Custom2)
		if (val > intMax) return msgInvalid;
	}
	   
	// reset the entered string after removal of spaces and leading zeros.
	this.value = theString;
	return "";
}

function ValidateFloat()
{
	var msg = "";
	var val = this.value;
	var msgInvalid = "Please enter a number.";
	if (this.ErrorMsg != "")
		msgInvalid = this.ErrorMsg

	var theLen = StripChars(" \n\t\r",val).length
	if (theLen == 0)	
		if (!this.Required) return ""	
		else return "Required field.  " + msgInvalid

	theString = StripChars(" ",val);

	// remove leading zeros (zeros are only leading if there is more than one char)
	while (theString.length > 1 && theString.substring(0,1) == "0")
	{
		theString = theString.substring(1, theString.length);
	}

	var val = parseFloat(theString);
	if (isNaN(val)) return msgInvalid;
	
	// check for non-digits (and minus sign). Do this by converting number
	// back to a string and comparing it to original string.
	if (val.toString() != theString) return msgInvalid;
	
	if (this.Custom1)
	{
		var fltMin = parseFloat(this.Custom1)
		if (val < fltMin) return msgInvalid;
	}
	if (this.Custom2)
	{
		var fltMax = parseFloat(this.Custom2)
		if (val > fltMax) return msgInvalid;
	}
	   
	// reset the entered string after removal of spaces and leading zeros.
	this.value = theString;
	return "";
}

function ValidateLong()
{
	var msg = "";
	var val = this.value;
	var msgInvalid = "Please enter a number.";
	if (this.ErrorMsg != "")
		msgInvalid = this.ErrorMsg

	var theLen = StripChars(" \n\t\r",val).length
	if (theLen == 0)	
		if (!this.Required) return ""	
		else return "Required field.  " + msgInvalid

	theString = StripChars(" ",val);

	// remove leading zeros (zeros are only leading if there is more than one char)
	while (theString.length > 1 && theString.substring(0,1) == "0")
	{
		theString = theString.substring(1, theString.length);
	}

	var val = parseInt(theString);
	if (isNaN(val)) return msgInvalid;
	if (val < -2147483648) return msgInvalid;
	if (val > 2147483647) return msgInvalid;
	
	// check for non-digits (and minus sign). Do this by converting number
	// back to a string and comparing it to original string.
	if (val.toString() != theString) return msgInvalid;
	
	if (this.Custom1)
	{
		var intMin = parseInt(this.Custom1)
		if (val < intMin) return msgInvalid;
	}
	if (this.Custom2)
	{
		var intMax = parseInt(this.Custom2)
		if (val > intMax) return msgInvalid;
	}
	   
	// reset the entered string after removal of spaces and leading zeros.
	this.value = theString;
	return "";
}

function ValidateNonBlank()
{	
	var msg = "";
	var val = new String(); 
	if (this.type)
	{
		if (this.type == "select-one")
		{
			val = this.options[this.selectedIndex].value;
		}
		else
		{
			if(this.value)
			{
				val = this.value; 
			}
		}
	}
	else
	{
		if (this.length)
		{
			for(var i = 0 ; i < this.length; i++)
			{
				if (this[i].checked) {val = "1"; break;}
			}
		}
		else
		{
			if (this.value)
			{
				val = this.value; 
			}
		}
	}
	
	var msgInvalid = "Please enter a non-blank character.";
	if (this.ErrorMsg != "")
		msgInvalid = this.ErrorMsg
	//Required field validation
	var theLen = StripChars(" ",val).length
	if (theLen == 0)	
		if (!this.Required) return ""	
		else return "Required field.  " + msgInvalid
	
	if (val.length > 0)
		if (StripChars(" \n\t\r",val).length == 0)
			msg = msgInvalid;
	
	return msg;
}

function ValidateDate()
{
	var msg = "";
	var val = this.value;
	var msgInvalid = "Please enter a valid date (for example\n5/3/97 or May 3, 1997 or 3 May 1997).";
	if (this.ErrorMsg != "")
		msgInvalid = this.ErrorMsg

	//Required field validation
	var theLen = StripChars(" ",val).length
	if (theLen == 0)	
		if (!this.Required) return ""	
		else return "Required field.  " + msgInvalid
	
	var dateVar = new Date(val);

	if (isNaN(dateVar.valueOf()) || (dateVar.valueOf() == 0))
		return msgInvalid;
		
	if (this.Custom1)
	{
		var minDate = new Date(this.Custom1);
		if (dateVar.valueOf() < minDate.valueOf()) msg = msgInvalid;
	}
	if (this.Custom2)
	{
		var maxDate = new Date(this.Custom2);
		if (dateVar.valueOf() > maxDate.valueOf()) msg = msgInvalid;
	}

	return msg;
}

function ValidateTime()
{
	var msg = "";
	var val = this.value;
	var msgInvalid = "Please enter a valid time (for example\n13:30:00 or 13:30 or 1:30 pm)";
	if (this.ErrorMsg != "")
		msgInvalid = this.ErrorMsg

	//Required field validation
	var theLen = StripChars(" ",val).length
	if (theLen == 0)	
		if (!this.Required) return ""	
		else return "Required field.  " + msgInvalid

	var result = Date.parse("1/1/1970 " + val);
	if (isNaN(result))
		msg = msgInvalid
	
	if (this.Custom1)
	{
		var minDate = Date.parse("1/1/1970 " + this.Custom1);
		if (result.valueOf() < minDate.valueOf()) msg = msgInvalid;
	}
	if (this.Custom2)
	{
		var maxDate = Date.parse("1/1/1970 " + this.Custom2);
		if (result.valueOf() > maxDate.valueOf()) msg = msgInvalid;
	}	
		
	return msg;
}

////////////////////////////////
// Support functions

function StripChars(theFilter,theString)
{
	var strOut,i,curChar

	strOut = ""	
	for (i=0;i < theString.length; i++)
	{		
		curChar = theString.charAt(i)
		if (theFilter.indexOf(curChar) < 0)	// if it's not in the filter, send it thru
			strOut += curChar		
	}	
	return strOut
}


