/********************************************************/
/********************************************************/
//Check Date
function CheckDate(datedata){
var dateStr = datedata
// Checks for the following valid date formats:
// MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY
// Also separates date into month, day, and year variables

var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;

// To require a 4 digit year entry, use this line instead:
//var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;

var matchArray = dateStr.match(datePat); // is the format ok?
if (matchArray == null) {
return false;
}
month = matchArray[1]; // parse date into variables
day = matchArray[3];
year = matchArray[4];
if (month < 1 || month > 12) { // check month range
alert("Month must be between 1 and 12.");
return false;
}
if (day < 1 || day > 31) {
alert("Day must be between 1 and 31.");
return false;
}
if ((month==4 || month==6 || month==9 || month==11) && day==31) {
alert("Month "+month+" doesn't have 31 days!")
return false;
}
if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day>29 || (day==29 && !isleap)) {
alert("February " + year + " doesn't have " + day + " days!");
return false;
   }
} return true;  // date is valid








} //End function CheckDate()
/********************************************************/
/********************************************************/


/********************************************************/
/********************************************************/
//***************************************************************/
//Changed code to accept hh:mm:ss AM/PM
//Changed max length to 11 instead of 8
//Added in a case of character 5 or 6 being : instead of a space.
//Tested 8/15/2002
//Nick Smith
//***************************************************************/
//  Check Time
function CheckTime(TimeStr){
	var zero = "0";

	if (TimeStr=="") { return (false); }
	if (TimeStr.length > 11) { return (false); }

	if (TimeStr.substring(1,2) == ":") { TimeStr = zero + TimeStr; }

	if (TimeStr.substring(2,3) != ":" && TimeStr.substring(3,4) != ":") { return (false); }

	if (isNaN(TimeStr.substring(0,2))) {
		return (false); 
	}
	else {
		if (TimeStr.substring(0,2) > 12) { return (false); }

		if (isNaN(TimeStr.substring(3,5))) {
			return (false);
		}
		else {
			if (TimeStr.substring(3,5) > 59) { return (false); }
		}

	}

	TimeStr = TimeStr.toUpperCase();

	switch(TimeStr.substring(5,6))
	{   
	case " ":
			if (TimeStr.substring(5,6) == "." || TimeStr.substring(6,7) == "." || TimeStr.substring(7,8) == "." || TimeStr.substring(8,9) == ".")
			{
				return (false);
			}

	case "A":
			if (TimeStr.substring(5,6) == "." || TimeStr.substring(6,7) == "." || TimeStr.substring(7,8) == "." || TimeStr.substring(8,9) == ".")
			{
				return (false);
			}
	case "P":
			if (TimeStr.substring(5,6) == "." || TimeStr.substring(6,7) == "." || TimeStr.substring(7,8) == "." || TimeStr.substring(8,9) == ".")
			{
				return (false);
			}

	case " ":
	    	if (TimeStr.substring(6,7) == "A" || TimeStr.substring(6,7) == "P")
	       	{
                	if (TimeStr.substring(6,7) == "A")
		   		TimeStr = TimeStr.substring(0,6) + "AM";
			else
		   		TimeStr = TimeStr.substring(0,6) + "PM";
			
			break;
	    	}
	    	else {
	        	return (false);
		}

	case "A":
		TimeStr = TimeStr.substring(0,5) + " " + "AM";
	    	break;
	case "P":
		TimeStr = TimeStr.substring(0,5) + " " + "PM";
	    	break;
	case ":":
   			if (TimeStr.substring(9,10) == "." || TimeStr.substring(10,11) == "." || TimeStr.substring(11,12) == ".")
			{
				return (false);
			}
	    	if (TimeStr.substring(9,10) == "A" || TimeStr.substring(9,10) == "P")
	       	{
                	if (TimeStr.substring(9,10) == "A")
		   		TimeStr = TimeStr.substring(0,9) + "AM";
			else
		   		TimeStr = TimeStr.substring(0,9) + "PM";
			
			break;
	    	}
	    	else {
	        	return (false);
	        }

	default:
		return (false);
	}

	return (true);

} 
//End function CheckTime()
/********************************************************/
/********************************************************/

/********************************************************/
/********************************************************/
//***************************************************************/
//Added code to force time as hh:mm:ss AM/PM
//Seconds cannot be "00"
//Scott Harper
//02/27/2004
//***************************************************************/
//  Check Long Time
function CheckLongTime(TimeStr){
	var zero = "0";

	if (TimeStr=="") { return (false); }
	if (TimeStr.length > 11) { return (false); }

	if (TimeStr.substring(1,2) == ":") { TimeStr = zero + TimeStr; }

	if (TimeStr.substring(2,3) != ":" && TimeStr.substring(3,4) != ":") { return (false); }

	if (isNaN(TimeStr.substring(0,2))) {
		return (false); 
	}
	else {
		if (TimeStr.substring(0,2) > 12) { return (false); }

		if (isNaN(TimeStr.substring(3,5))) {
			return (false);
		}
		else {
			if (TimeStr.substring(3,5) > 59) { return (false); }
		}

	}

	TimeStr = TimeStr.toUpperCase();

	if ((TimeStr.substring(5,6)) == ":") {
		if (isNaN(TimeStr.substring(6,8))) {
			return (false);
		}
		else {
			if(TimeStr.substring(6,8) > 59 || TimeStr.substring(6,8) < 1) {return (false); }
		}
	
   			if (TimeStr.substring(9,10) == "." || TimeStr.substring(10,11) == "." || TimeStr.substring(11,12) == ".")
			{
				return (false);
			}
	    	if (TimeStr.substring(9,10) == "A" || TimeStr.substring(9,10) == "P")
	       	{
				if (TimeStr.substring(9,10) == "A")
		   			TimeStr = TimeStr.substring(0,9) + "AM";
				else
		   			TimeStr = TimeStr.substring(0,9) + "PM";
	    	}
	    	else {
	        	return (false);
	        }
	}

	else {
		return (false);
	}

	return (true);

} 
//End function CheckLongTime()
/********************************************************/
/********************************************************/

function VerifyData(objField, blnRequired, strFormat, intMax) {
	var blnReturn
	
	blnReturn = true;
	
	switch(strFormat) {
		case "Date":
			if (CheckDate(objField.value) == false) {
				if (objField.value == "") {
					if (blnRequired) {
						alert("You must enter all required Date fields in the format: (mm/dd/yyyy)  After clicking OK, the cursor will be placed in the field needing corrected.");
						blnReturn = false;
					}
				}
				else {
					alert("Date must be in the format: (mm/dd/yyyy)  After clicking OK, the cursor will be placed in the field needing corrected.");
					blnReturn = false;
				}
			}

			break;

		case "Time":
			if (CheckTime(objField.value) == false) {
				if (objField.value == "") {
					if (blnRequired) {
						alert("You must enter all required Time fields in the format: hh:mm AM -or- h:mm PM *You must include a space between the time and AM or PM.");
						blnReturn = false;
					}
				}
				else {
					alert("Time must be in the format: hh:mm AM -or- h:mm PM *You must include a space between the time and AM or PM.");
					blnReturn = false;
				} //end if-else
			}

			break;
			
		case "LongTime":
			if (CheckLongTime(objField.value) == false) {
				if (objField.value == "") {
					if (blnRequired) {
						alert("You must enter all required Time fields in the format: hh:mm:ss AM -or- h:mm:ss PM *You must include a space between the time and AM or PM.");
						blnReturn = false;
					}
				}
				else {
					alert("Time must be in the format: hh:mm:ss AM -or- h:mm:ss PM *You must include a space between the time and AM or PM.");
					blnReturn = false;
				} //end if-else
			}

			break;

		case "Memo":
			if (blnRequired) {
				if (objField.value == "") {
					alert("Please fill out all necessary comments and explanations.  After clicking OK, the cursor will be placed in the field needing corrected.");
					blnReturn = false;
				}
			}

			if (intMax == 0) { intMax=1000; }
			if (intMax > 8000) { intMax=8000; }

			if (objField.value.length > intMax) {
				alert("Comment text is too long.  You entered " + objField.value.length + " characters.  (Maximum: " + intMax + " characters)  After clicking OK, the cursor will be placed in the field needing corrected.");
				blnReturn = false
			}
		
			break;

		case "Text":
			if (blnRequired) {
				if (objField.value == "") {
					alert("Please fill out all necessary information and explanation fields.  After clicking OK, the cursor will be placed in the field needing corrected.");
					blnReturn = false;
				}
			}

			if ((intMax == 0) || (intMax > 255)) { intMax=255; }

			if (objField.value.length > intMax) {
				alert("Text entered is too long.  You entered " + objField.value.length + " characters.  (Maximum: " + intMax + " characters)  After clicking OK, the cursor will be placed in the field needing corrected.");
				blnReturn = false
			}

			break;
		
		
		case "Number":
			if (blnRequired) {
				if (objField.value == "") {
					alert("Please fill out all necessary information and explanation fields.  After clicking OK, the cursor will be placed in the field needing corrected.");
					blnReturn = false;
				}
			}
			
			if (intMax == 0) { intMax=99 }
			
			if (objField.value != "") {
				if (isNaN(objField.value)) {
					alert("Value must be a number between 0 and " + intMax + ".  After clicking OK, the cursor will be placed in the field needing corrected.");
					blnReturn = false;
				}
				else if ((objField.value < 0) || (objField.value > intMax)) {
					alert("Value must be a number between 0 and " + intMax + ".  After clicking OK, the cursor will be placed in the field needing corrected.");
					blnReturn = false;
				}
			}
			
			break;

	} //End switch

	if (blnReturn == false) {
		objField.focus();
		objField.select();
	}	

	return (blnReturn)
} //End function VerifyData()
/********************************************************/
/********************************************************/


/********************************************************/
/********************************************************/
function TextLimit(field, maxlimit) {

	if (field.value.length > maxlimit) // if too long...trim it!
		field.value = field.value.substring(0, maxlimit);

} //End function TextLimit()
/********************************************************/
/********************************************************/



/********************************************************/
/********************************************************/
function TimeDiff(time1,time2,description,blnRequired2) {
	var blnReturn2
	var blnOkay
	var HourDifference
	var DateDifference
	
	blnReturn2 = true;
	blnOkay = true;
	HourDifference = 0;
	
	if (blnRequired2) {
		if (time1.value == "") {
			alert("Please fill out all necessary time fields.  After clicking OK, the cursor will be placed in the field needing corrected.");
			blnReturn = false;
			blnOkay = false;
		}
		if (time1.value != "") {
			if (time2.value == "") {
				alert("Please fill out all necessary time fields.  After clicking OK, the cursor will be placed in the field needing corrected.");
				blnReturn = false;
				blnOkay = false;
			}			
		}
	}
	if (blnOkay) {
		if (time1.value != "") {
			date1 = new Date();
			date2 = new Date();

			date1temp = new Date("01/01/2001 " + time1.value);
			date1.setTime(date1temp.getTime());

			date2temp = new Date("01/01/2001 " + time2.value);
			date2.setTime(date2temp.getTime());

			if (date1.getTime() > date2.getTime())
				{alert(description);
				blnReturn2 = false;}

			DateDifference = date1temp-date2temp ;
    
			HourDifference = parseInt(DateDifference / 3600000 ) ;
				
			if (HourDifference <= -6 || HourDifference >= 6)
				{alert("You have entered an invalid time. Please check the times entered.");
				blnReturn2 = false;}
		}
		if (blnReturn2 == false) {
				time2.focus();
				time2.select();
		}
	}
	return (blnReturn2);
	
} //End function TimeDiff()
/********************************************************/
/********************************************************/





//-- Function to force the cursor to move to the next field on the form within an Input Field or Textarea
function EnterMove (field, event) {
		var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
		if (keyCode == 13) {
			var i;
			for (i = 0; i < field.form.elements.length; i++)
				if (field == field.form.elements[i])
					break;
			i = (i + 1) % field.form.elements.length;
			field.form.elements[i].focus();
			return false;
		} 
		else
		return true;
	}      
//-- End Enter Key Move Function
/********************************************************/
/********************************************************/



//-- Function to Disable the Enter Key within an Input Field or Textarea
function EnterDisable (field, event) {
		var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
		if (keyCode == 13) {
			return (false);
		} 
	}      
//-- End Enter Key Disable Function
/********************************************************/
/********************************************************/



//-- Function to Validate the form the Enter Key within an Input Field or Textarea
function EnterValidate (field, event) {
		var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
		if (keyCode == 13) {
			Validate();
			return (false);
		} 
	}      
//-- End Enter Key Validate Function
/********************************************************/
/********************************************************/

