// -----------------------------------------------------------------------------
// Generic Form Validation
//
// Copyright (C) 2000 Jacob Hage - [jacobhage@hotmail.com]
// Distributed under the terms of the GNU Library General Public License
// Modified by Rick Hays     - 11/05/2001 - Added "Alhpa" to allow only (A-Z) characters.
//             Rick Hays     - 11/08/2001 - Added "fcase" to allow Upper/Lower casing of strings.
//             Rick Hays     - 11/09/2001 - Added "Phone" to check for a valid phone number.
//             Rick Hays     - 11/09/2001 - Added routine to Strip spaces before it goes through validation.
//             Rick Hays     - 11/10/2001 - Added ".." Check in Email Address.
//             Rick Hays     - 03/21/2002 - Loosened Phone check to allow all numbers 0000 to 9999.
//             Rick Hays     - 08/27/2002 - Added Compair of 2 fields. To be equal (Like 2 Email Addresses).
//             Darren Klaudt - 05/29/2003 - Added " " Check in Email Address.
//             Jim Shea      - 06/28/2004 - Added to fcase the ability to 'proper' a string
//             Rick Hays     - 09/14/2004 - Added "emailarray" to allow more then one email in the string also
//                                          Added "parse" to the define to allow for different parse characters.
//             Rick Hays     - 09/23/2004 - Added "strinc" type to allow only characters placed in "parse" area.
//                                          If "parse" is blank then NO characters are allowed/excepted.
//                                          Added "strexc" type to allow only characters NOT placed in "parse" area.
//                                          If "parse" is blank then ALL characters are allowed/excepted.
//
// -----------------------------------------------------------------------------

// -----------------------------------------------------------------------------
// Initializing script  - setting global variables
// -----------------------------------------------------------------------------
var checkObjects		= new Array(); 	                         // Array containing the objects to validate.
var errors				= ""; 			                         // Variable holding the error message.
var returnVal			= false; 		                         // Return value. Form will only be submitted if true.
var language			= new Array(); 	                         // Language independent error messages!
var selectecLanguage	= "english";	                         // Choose Language
var invalid             = '1234567890-=+`!@#$%^&*()_;:,<.>/?"';  // 1234567890-=+`!@#$%^&*()_;:,<.>/?
var tmpstr;
var notAsc;

language.english		= new Array();

// Error messages in english:
	language.english.header		 = "The following error(s) occured:";
	language.english.start		 = "->";
	language.english.field		 = " Field ";
	language.english.both        = " Both ";
	language.english.require	 = " is required";
	language.english.equal       = " must be equal.";
	language.english.xmin		 = " and must consist of at least ";
	language.english.xmax		 = " and must not contain more than ";
	language.english.minmax		 = " and no more than ";
	language.english.doublequote = " and must not contain a double quote ";
	language.english.chars		 = " characters";
	language.english.num		 = " and must contain a number";
	language.english.lnum		 = " must consist of a number greater than ";
	language.english.hnum		 = " must consist of a number less than ";
	language.english.date		 = " must contain a valid date (mm/dd/yyyy)";
	language.english.ldate		 = " must consist of a date greater than ";
	language.english.hdate		 = " must consist of a date less than ";
	language.english.alpha       = " must be ALPHA characters (A-Z) only";
	language.english.strinc1     = " must contain only these ( "
	language.english.strinc2     = " ) charactes."
	language.english.strexc1     = " must NOT contain these ( "
	language.english.strexc2     = " ) charactes."
	language.english.phone		 = " must contain a valid phone number (xxx-xxx-xxxx)";
	language.english.areacode    = " invalid Telephone Number: Telephone Number's can't start with 000";
	language.english.prefix1	 = " invalid Telephone Number: Telephone Number's Prefix can not be 000";
	language.english.prefix2	 = " invalid Telephone Number: Telephone Number's Prefix can not be 555";
	language.english.areaprefix	 = " invalid Telephone Number: Area Code and Prefix can not be equal";
	language.english.payphone	 = " invalid Telephone Number: Telephone Number can not be greater then 8999";
	language.english.state		 = " must contain a valid 2 character state code";
	language.english.email		 = " must contain a valid e-mail address";

// -----------------------------------------------------------------------------
// define - Call this function in the beginning of the page. I.e. onLoad.
//
// n     = name of the input field (Required)
// type  = string     (any ascii character less the Double Quotes Character)  *** Not Complete ***
//         num        (numeric characters only)
//         alpha      (A-Z only)
//         phone      (format of xxx-xxx-xxxx only)
//         email      (valid email address)
//		   emailarray (valid email array, delimited by parse (see below) character.)
//         state      (valid 2 character state abbrv.) - Currently 50 states and DC active
//         date       (valid date field)
//         compair    (validate 2 fields - First is n, Second is in xmin)
//         strinc     (excepts only chars in the array. Place array in parse field.)
//                    [Blank array = nothing excepted.]
//         strexc     (excepts anything but chars in the array. Place array in parse field.)
//                    [Blank array = everything excpted.]
// xmin  = the value must have at least [min] characters [SET TO NULL for NOT REQUIRED]
// xmax  = the value must have maximum [max] characters
// lnum  = lowest value allowed, used in NUM, DATE Fields
// hnum  = Highest value allowed, used in NUM, DATE fields
// fcase = (upper,lower) set case of field
// parse = Parse Character for Arrays (Currently only used in emailarray, strinc, strexc).
// d     = (Optional)
// -----------------------------------------------------------------------------
function define(n,type,HTMLname,xmin,xmax,lnum,hnum,fcase,parse,d)
{
	var p;
	var i;
	var j;
	var x;
	if(!d) d=document;

	if((p=n.indexOf("?"))>0&&parent.frames.length)
	{
		d=parent.frames[n.substring(p+1)].document;
		n=n.substring(0,p);
	}

	if(!(x=d[n])&&d.all) x=d.all[n];

	for (i=0;!x&&i<d.forms.length;i++)
	{
		x=d.forms[i][n];
	}

	for(i=0;!x&&d.layers&&i<d.layers.length;i++)
	{
		x=define(n,type,HTMLname,xmin,xmax,lnum,hnum,fcase,parse,d.layers[i].document);
		return x;
	}

	// Create Object. The name will be "V_something" where something is the "n" parameter above.
	eval("V_"+n+" = new formResult(x,type,HTMLname,xmin,xmax,lnum,hnum,fcase,parse);");
	checkObjects[eval(checkObjects.length)] = eval("V_"+n);
}

// -----------------------------------------------------------------------------
// formResult - Used internally to create the objects
// -----------------------------------------------------------------------------
function formResult(form,type,HTMLname,xmin,xmax,lnum,hnum,fcase,parse)
{
	this.form     = form;
	this.type     = type;
	this.HTMLname = HTMLname;
	this.xmin     = xmin;
	this.xmax     = xmax;
	this.lnum     = lnum;
	this.hnum     = hnum;
	this.fcase    = fcase;
	this.parse    = parse;
}

// -----------------------------------------------------------------------------
// validate - Call this function onSubmit and return the "returnVal". (onSubmit="validate();return returnVal;")
// -----------------------------------------------------------------------------
function validate()
{
	if(checkObjects.length>0)
	{
		errorObject = "";

		for(i=0;i<checkObjects.length;i++)
		{
			validateObject 			= new Object();
			validateObject.form 	= checkObjects[i].form;
			validateObject.name     = checkObjects[i].name;
			validateObject.HTMLname = checkObjects[i].HTMLname;
			validateObject.val 		= checkObjects[i].form.value;
			validateObject.len 		= checkObjects[i].form.value.length;
			validateObject.xmin 	= checkObjects[i].xmin;
			validateObject.xmax 	= checkObjects[i].xmax;
			validateObject.lnum		= checkObjects[i].lnum;
			validateObject.hnum		= checkObjects[i].hnum;
			validateObject.fcase	= checkObjects[i].fcase;
			validateObject.parse    = checkObjects[i].parse;
			validateObject.type 	= checkObjects[i].type;

			// ***********************************************
			// *** STRIP SPACES FROM FIELD BEFORE VALIDATION *
			// ***********************************************
			var tmp = "";
			var item_length = validateObject.len;
			var item_length_minus_1 = validateObject.len - 1;
			for (index = 0; index < item_length; index++)
			{
				if (validateObject.form.value.charAt(index) != ' ')
				{
					tmp += validateObject.form.value.charAt(index);
				}
				else
				{
					if (tmp.length > 0)
					{
						if (validateObject.form.value.charAt(index+1) != ' ' && index != item_length_minus_1)
						{
							tmp += validateObject.form.value.charAt(index);
						}
					}
				}
			}
			validateObject.form.value = tmp;
			// --- END STRIP SPACES --------------------------------------

			// ***********************************************************
			// *** UPPER / LOWER CASE CONVERSION                         *
			// *** START - RLH - 11/08/2001 - Set the case for the field *
			// ***********************************************************
			if (validateObject.fcase == "lower")
			{
				validateObject.form.value = validateObject.form.value.toLowerCase();
			}

			if (validateObject.fcase == "upper")
			{
				validateObject.form.value = validateObject.form.value.toUpperCase();
			}

			if (validateObject.fcase == "proper")
			{
				validateObject.form.value = PCase(validateObject.form.value.toString())
			}
			// --- END of UPPER / LOWER / PROPER CASE --------------------------

			// ##########################################################################
			// # CHECK FOR DOUBLE QUOTE IN FIELD (UNLESS FIELD TYPE IS strinc OR strexc #
			// ##########################################################################
			if ((validateObject.val.indexOf('"') > -1) && (validateObject.type != "strinc") && (validateObject.type != "strexc"))
			{
				errors+=language[selectecLanguage].start+language[selectecLanguage].field+validateObject.HTMLname+language[selectecLanguage].doublequote+"\n";
			}
			// --- END of DOUBLE QUOTE CHECK --------------------------

			// #######################################
			// # CHECK FOR MIN / MAX LENGTH OF FIELD #
			// #######################################
			if (validateObject.xmin != null || (validateObject.xmin == null && validateObject.len >= 0))
			{
				if (validateObject.xmin && validateObject.xmax && (validateObject.len < validateObject.xmin || validateObject.len > validateObject.xmax))
				{
					errors+=language[selectecLanguage].start+language[selectecLanguage].field+validateObject.HTMLname+language[selectecLanguage].require+language[selectecLanguage].xmin+validateObject.xmin+language[selectecLanguage].minmax+validateObject.xmax+language[selectecLanguage].chars+"\n";
				}
				else if (validateObject.xmin && !validateObject.xmax && (validateObject.len < validateObject.xmin))
				{
					errors+=language[selectecLanguage].start+language[selectecLanguage].field+validateObject.HTMLname+language[selectecLanguage].require+language[selectecLanguage].xmin+validateObject.xmin+language[selectecLanguage].chars+"\n";
				}
				else if (validateObject.xmax && !validateObject.xmin &&(validateObject.len > validateObject.xmax))
				{
					errors+=language[selectecLanguage].start+language[selectecLanguage].field+validateObject.HTMLname+language[selectecLanguage].require+language[selectecLanguage].xmax+validateObject.xmax+language[selectecLanguage].chars+"\n";
				}
				else if (!validateObject.xmin && !validateObject.xmax && validateObject.len <= 0)
				{
					errors+=language[selectecLanguage].start+language[selectecLanguage].field+validateObject.HTMLname+language[selectecLanguage].require+"\n";
				}
			}
			// --- END of MIN / MAX LENGTH OF FIELDS --------------------------

			// ##########################################
			// ### START - VALIDATION STRING FIELD TYPE #
			// ##########################################
			if (validateObject.type == "string")
			{

			}
			// --- END of STRING VALIDATION --------------------------

			// ##########################################
			// ### START - VALIDATION STRINC FIELD TYPE #
			// ##########################################
			if (validateObject.type == "strinc")
			{
				//notAsc = true;
				for (var j = 0; j < validateObject.len; j++)
				{
					tmpstr = validateObject.val.substring(j, j+1);
					//alert(tmpstr);
					//alert(validateObject.parse.indexOf(tmpstr));
					if (validateObject.parse.indexOf(tmpstr) == -1)
					{
						notAsc = true;
					}
				}
				if (notAsc)
				{
					errors+=language[selectecLanguage].start+language[selectecLanguage].field+validateObject.HTMLname+language[selectecLanguage].strinc1+validateObject.parse+language[selectecLanguage].strinc2+"\n";
					notAsc = false;
				}
			}
			// --- END of STRINC VALIDATION --------------------------

			// ##########################################
			// ### START - VALIDATION STREXC FIELD TYPE #
			// ##########################################
			if ((validateObject.type == "strexc") && (validateObject.parse.length > 0))
			{
				for (var j = 0; j < validateObject.len; j++)
				{
					tmpstr = validateObject.val.substring(j, j+1);
					//alert(tmpstr);
					//alert(validateObject.parse.indexOf(tmpstr));
					if (validateObject.parse.indexOf(tmpstr) > -1)
					{
						notAsc = true;
					}
				}
				if (notAsc)
				{
					errors+=language[selectecLanguage].start+language[selectecLanguage].field+validateObject.HTMLname+language[selectecLanguage].strexc1+validateObject.parse+language[selectecLanguage].strexc2+"\n";
					notAsc = false;
				}
			}
			// --- END of STREXC VALIDATION --------------------------

			// #######################################
			// ### START - VALIDATION NUM FIELD TYPE #
			// #######################################
			if (validateObject.type == "num")
			{
				if (validateObject.xmin != null || (validateObject.xmin == null && validateObject.len > 0))
				{
					if (isNaN(validateObject.val))
					{
						errors+=language[selectecLanguage].start+language[selectecLanguage].field+validateObject.HTMLname+language[selectecLanguage].num+"\n";
					}
					if (validateObject.val < validateObject.lnum && validateObject.len > 0)
					{
						errors+=language[selectecLanguage].start+language[selectecLanguage].field+validateObject.HTMLname+language[selectecLanguage].lnum+validateObject.lnum+"\n";
					}
					if (validateObject.val > validateObject.hnum && validateObject.len > 0)
					{
						errors+=language[selectecLanguage].start+language[selectecLanguage].field+validateObject.HTMLname+language[selectecLanguage].hnum+validateObject.hnum+"\n";
					}
				}
			}
			// --- END of NUM VALIDATION --------------------------

			// #########################################
			// ### START - VALIDATION ALPHA FIELD TYPE #
			// #########################################
			if (validateObject.type == "alpha")
			{
				if (validateObject.xmin != null || (validateObject.xmin == null && validateObject.len > 0))
				{
					//alert("Alpha - Not Null");
					if (validateObject.len == 1)
					{
						if(invalid.indexOf(validateObject.val) > -1)
						{
							errors+=language[selectecLanguage].start+language[selectecLanguage].field+validateObject.HTMLname+language[selectecLanguage].alpha+"\n";
						}
					}
					else if (validateObject.len > 1)
					{
						//alert(validateObject.len);
						for (var j = 0; j < validateObject.len; j++)
						{
							tmpstr = validateObject.val.substring(j, j+1);
							//alert(tmpstr);
							//alert(invalid.indexOf(tmpstr));
							if (invalid.indexOf(tmpstr) > -1)
							{
								notAsc = true;
							}
						}
						if (notAsc){
							errors+=language[selectecLanguage].start+language[selectecLanguage].field+validateObject.HTMLname+language[selectecLanguage].alpha+"\n";
							notAsc = false;
						}
					}
				}
			}
			// --- END of ALPHA VALIDATION --------------------------

			// #########################################
			// ### START - VALIDATION PHONE FIELD TYPE #
			// #########################################
			if (validateObject.type == "phone")
			{
				if (validateObject.xmin != null || (validateObject.xmin == null && validateObject.len > 0))
				{
					//alert("Phone - Not Null");
					var matchArr = validateObject.form.value.match(/^(\d{3})-?(\d{3})-?(\d{4})$/);
					var numDashes = validateObject.form.value.split('-').length - 1;

					if (matchArr == null || validateObject.len != 12 || numDashes != 2)
					{
						errors+=language[selectecLanguage].start+language[selectecLanguage].field+validateObject.HTMLname+language[selectecLanguage].phone+"\n";
					}
					else if (matchArr != null)
					{
						// *** Area Code can not be Zeros
						if (parseInt(matchArr[1],10) == 0)
						{
							errors+=language[selectecLanguage].start+language[selectecLanguage].field+validateObject.HTMLname+language[selectecLanguage].areacode+"\n";
						}
						// *** Prefix can not be zeros
						if (parseInt(matchArr[2],10) == 0)
						{
							errors+=language[selectecLanguage].start+language[selectecLanguage].field+validateObject.HTMLname+language[selectecLanguage].prefix1+"\n";
						}
						// *** Prefix can not be 555
						if (parseInt(matchArr[2],10) == 555)
						{
							errors+=language[selectecLanguage].start+language[selectecLanguage].field+validateObject.HTMLname+language[selectecLanguage].prefix2+"\n";
						}
						// *** Area Code and Prefix can not be equal
						if (parseInt(matchArr[1],10) == parseInt(matchArr[2],10))
						{
							errors+=language[selectecLanguage].start+language[selectecLanguage].field+validateObject.HTMLname+language[selectecLanguage].areaprefix+"\n";
						}
						// *** Phone Number can not be greater then 8999 -> 9000 and up are pay phones
// RLH - 03/21/2002			if (parseInt(matchArr[3],10) > 8999) {
						if (parseInt(matchArr[3],10) > 9999)
						{
							errors+=language[selectecLanguage].start+language[selectecLanguage].field+validateObject.HTMLname+language[selectecLanguage].payphone+"\n";
						}
					} // (matchArr != null)
				}
			}
			// --- END of PHONE VALIDATION --------------------------

			// #########################################
			// ### START - VALIDATION EMAIL FIELD TYPE #
			// #########################################
			if (validateObject.type == "email")
			{
				if (validateObject.xmin != null || (validateObject.xmin == null && validateObject.len > 0))
				{
					if((validateObject.val.indexOf("@") == -1) || (validateObject.val.charAt(0) == ".") || (validateObject.val.charAt(0) == "@") ||(validateObject.len < 6) || (validateObject.val.indexOf(".") == -1) || (validateObject.val.indexOf("..") > -1) || (validateObject.val.charAt(validateObject.val.indexOf("@")+1) == ".") || (validateObject.val.indexOf(" ") > -1) || (validateObject.val.charAt(validateObject.val.indexOf("@")-1) == "."))
					{
						errors+=language[selectecLanguage].start+language[selectecLanguage].field+validateObject.HTMLname+language[selectecLanguage].email+"\n";
					}
				}
			}
			// --- END of EMAIL VALIDATION --------------------------

			// ##############################################
			// ### START - VALIDATION EMAILARRAY FIELD TYPE #
			// ##############################################
			if (validateObject.type == "emailarray")
			{
				if (validateObject.xmin != null || (validateObject.xmin == null && validateObject.len > 0))
				{
					emailArray = validateObject.val.split(validateObject.parse);

					for(k=0;k<emailArray.length;k++)
					{
						if ((k > 0) && (emailArray[k].length <= 0))
						{
							// alert('Extra parse character at the end!');
						}
						else
						{
							// ***********************************************
							// *** STRIP SPACES FROM FIELD BEFORE VALIDATION *
							// ***********************************************
							var tmp = "";
							var item_length = emailArray[k].length;
							var item_length_minus_1 = emailArray[k].length - 1;

							for (index = 0; index < item_length; index++)
							{
								if (emailArray[k].charAt(index) != ' ')
								{
									tmp += emailArray[k].charAt(index);
								}
								else
								{
									if (tmp.length > 0)
									{
										if ((emailArray[k].charAt(index+1) != ' ') && (index != item_length_minus_1))
										{
											tmp += emailArray[k].charAt(index);
										}
									}
								}
							}
							emailArray[k] = tmp;
							// --- END STRIP SPACES --------------------------------------

							if((emailArray[k].indexOf("@") == -1) || (emailArray[k].charAt(0) == ".") || (emailArray[k].charAt(0) == "@") || (emailArray[k].len < 6) || (emailArray[k].indexOf(".") == -1) || (emailArray[k].indexOf("..") > -1) || (emailArray[k].charAt(emailArray[k].indexOf("@")+1) == ".") || (emailArray[k].indexOf(" ") > -1) || (emailArray[k].charAt(emailArray[k].indexOf("@")-1) == "."))
							{
								errors+=language[selectecLanguage].start+language[selectecLanguage].field+validateObject.HTMLname+' '+emailArray[k]+' '+language[selectecLanguage].email+"\n";
							}
						}
					}
				}
			}
			// --- END of EMAIL VALIDATION --------------------------

			// #########################################
			// ### START - VALIDATION STATE FIELD TYPE #
			// #########################################
			if (validateObject.type == "state")
			{
				var states = "AK AL AR AZ CA CO CT DC DE FL GA HI IA ID IL IN KS KY LA MA MD ME MI MN MO MS MT NC ND NE NH NJ NM NV NY OH OK OR PA RI SC SD TN TX UT VA VT WA WI WV WY";
				// Missing States: AS CN FC FM GU IQ IT MH MP MQ PR PW VI WQ
				// RLH - 11/10/2001 - Added toUpperCase to catch lower case state values
				if (validateObject.xmin != null || (validateObject.xmin == null && validateObject.len > 0))
				{
					if (states.indexOf(validateObject.form.value.toUpperCase()) == -1)
					{
						errors+=language[selectecLanguage].start+language[selectecLanguage].field+validateObject.HTMLname+language[selectecLanguage].state+"\n";
					}
				}
			}
			// --- END of STATE VALIDATION --------------------------

			// ########################################
			// ### START - VALIDATION DATE FIELD TYPE #
			// ########################################
			if (validateObject.type == "date")
			{
				if (validateObject.xmin != null || (validateObject.xmin == null && validateObject.len > 0))
				{
					if (validateObject.len < 8 || validateObject.len > 10)
					{
						errors+=language[selectecLanguage].start+language[selectecLanguage].field+validateObject.HTMLname+language[selectecLanguage].date+"\n";
					}
					else
					{
						if (!dateValid(validateObject.val))
						{
							errors+=language[selectecLanguage].start+language[selectecLanguage].field+validateObject.HTMLname+language[selectecLanguage].date+"\n";
						}
						else
						{
							var tmpDate = new Date(validateObject.val);
							var lDate   = new Date(validateObject.lnum);
							var hDate   = new Date(validateObject.hnum);

							if (tmpDate < lDate)
							{
								errors+=language[selectecLanguage].start+language[selectecLanguage].field+validateObject.HTMLname+language[selectecLanguage].ldate+validateObject.lnum+"\n";
							}
							if (tmpDate > hDate)
							{
								errors+=language[selectecLanguage].start+language[selectecLanguage].field+validateObject.HTMLname+language[selectecLanguage].hdate+validateObject.hnum+"\n";
							}
						}
					}
				}
			}
			// --- END of DATE VALIDATION --------------------------

			// #############################################
			// ### START - COMPAIR VALIDATION FIELD TYPE ###
			// #############################################
			if (validateObject.type == "compair")
			{
				for(j=0;j<checkObjects.length;j++)
				{
					if (checkObjects[j].form.name == validateObject.xmin)
					{
						if (validateObject.val != checkObjects[j].form.value)
						{
							errors+=language[selectecLanguage].start+language[selectecLanguage].both+validateObject.HTMLname+language[selectecLanguage].equal+"\n";
						}
					}
				}
			}
			// --- END of COMPAIR VALIDATION -----------------------
		}
	}

	// Used to set the state of the returnVal. If errors -> show error messages in chosen language
	if(errors)
	{
		alert(language[selectecLanguage].header.concat("\n"+errors));
		errors = "";
		returnVal = false;
	}
	else
	{
		returnVal = true;
	}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////

// ############################
// # DATE VALIDATION FUNCTION #
// ############################
function dateValid(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;
	strDate = objName;
	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)
	{
		if (strDate.length>5)
		{
			strDay = strDate.substr(0, 2);
			strMonth = strDate.substr(2, 2);
			strYear = strDate.substr(4);
		}
	}

	//Adjustment for short years entered
	if (strYear.length == 2)
	{
		strYear = '20' + strYear;
	}

	strTemp = strDay;
	strDay = strMonth;
	strMonth = strTemp;
	intday = parseInt(strDay, 10);
	if (isNaN(intday))
	{
		err = 2;
		return false;
	}

	intMonth = parseInt(strMonth, 10);
	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);
	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;
			}
		}
	}
	return true;
}

function LeapYear(intYear)
{
	if (intYear % 100 == 0)
	{
		if (intYear % 400 == 0)
		{
			return true;
		}
	}
	else
	{
		if ((intYear % 4) == 0)
		{
			return true;
		}
	}
	return false;
}


//  06/28/2004 JFS Added routine (copied from http://www.apriori-it.co.uk/PCase.asp#)
//                 Adjusted it to work with apostrophes.
function PCase(STRING){
var strReturn_Value = "";
var iTemp = STRING.length;

  if(iTemp==0){
    return "";
  }

  var UcaseNext = false;
  strReturn_Value += STRING.charAt(0).toUpperCase();   // 1st char upper case

  for(var iCounter=1;iCounter < iTemp;iCounter++){
    if(UcaseNext == true){
      strReturn_Value += STRING.charAt(iCounter).toUpperCase();
    }
    else{
      strReturn_Value += STRING.charAt(iCounter).toLowerCase();
    }
    var iChar = STRING.charCodeAt(iCounter);

    // space, hyphen, period, or apostrophe (any of the 3 versions)
    if(iChar == 32 || iChar == 45 || iChar == 46 || iChar == 39 || iChar == 44 || iChar == 96){
      UcaseNext = true;
    }
    else{
      UcaseNext = false
    }

	//  also a c (99 and 67) preceeded by an m (77 or 109) preceeded by a space or start of string (0-based)
    if(iChar == 99 || iChar == 67){
      if(STRING.charCodeAt(iCounter-1)==77 || STRING.charCodeAt(iCounter-1)==109){
		if(iCounter==1 || STRING.charCodeAt(iCounter-2)==32){
          UcaseNext = true;
		}
      }
    }

  } //End For

return strReturn_Value;
} //End Function



//  End -->

