/*
FUNCTIONS IN THIS FILE:
	DigitValidation
	EmailAddressValidation
	EmptyValidation
	IsValidDate
	LengthValidation
	NewWindow
	PreventEnterKeySubmit
	SubmitEnter
	SubmitFormOnlyOnce
	ToggleDiv
	Trim
*/
var submitCount = 0;

function DigitValidation(entered, lenMin, lenMax, alertText) {
/*verifies numeric value was entered and that it's length is between the passed min and max*/
	var strValue = Trim(entered.value);	
	var checkvalue=parseFloat(strValue);
	if ( (parseFloat(lenMin)==lenMin && strValue.length<lenMin) || (parseFloat(lenMax)==lenMax && strValue.length>lenMax) || strValue!=checkvalue || (strValue.indexOf(".")>0) )
	{
		if (alertText) 
			alert(alertText);
		entered.focus();
		entered.select();
		return false;
	}
	else 
		return true;
}

function EmailAddressValidation(entered, alertText) {
/*shows a message (alertText) and returns false if no e-mail address has been specified in the 
input text box (entered) or if the address appears invalid due to a number of conditions checked*/
	with (entered)
	{
		var len=value.length
		var apos=value.indexOf("@");
		var dotpos=value.lastIndexOf(".");
		var lastpos=value.length-1;
		// if length = 0
		// or "@" is the first character or doesn't exist
		// or "." doesn't follow "@" with at least 2 spaces between them
		// or there are more than 3 characters following the "."
		// or there are less than 2 characters following the "."
		if (len==0 || apos<1 || dotpos-apos<2 || lastpos-dotpos>4 || lastpos-dotpos<2) 
		{
			if (alertText) 
				alert(alertText);
			entered.focus();
			return false;
		}
		else 
			return true;
	}
}

function EmptyValidation(entered, alertText) {
/*shows a message (alertText) and returns false if no data has been entered in the input text box (entered)*/
	var strValue = Trim(entered.value);
	if (strValue == null || strValue == "" || strValue == "undefined")
	{
		if (alertText)
		{
			alert(alertText);
		} 
		entered.focus();
		entered.select();
		return false;
	}
	else 
		return true;
}

function IsValidDate(dateStr) {
/*Checks for the following valid date formats: MM/DD/YYYY OR MM-DD-YYYY 
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
		return false;
	}
	if (day < 1 || day > 31) {
		return false;
	}
	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		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)) {
			return false;
   		}
	}
	return true;  // date is valid
}

function LengthValidation(entered, lenMin, lenMax, alertText) {
/*shows a message (alertText) and returns false if the length of the input text (entered) is too short or too long*/
	var strValue = Trim(entered.value);
	if (strValue.length < lenMin || strValue.length > lenMax)
	{
		if (alertText) 
		{
			alert(alertText);
		} 
		entered.focus();
		entered.select();
		return false;
	}
	else 
		return true;
}

function NewWindow(URL, name, specs) {
/*opens a new browser window with the specified url, window name and other parameter specs*/
	var anon_win = window.open(URL, name, specs);
}

function PreventEnterKeySubmit() {
/*used to prevent enter key in onkeypress events.  used to prevent forms from being submitted, 
bypassing form validation*/
  return !(window.event && window.event.keyCode == 13); 
}

function SubmitEnter(myfield,e) {
/*used in onkeypress event of form inputs to allow enter key to kick off form submission process.  works with ie and netscape.*/
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return false;
	
	if (keycode == 13)
	   return true;
	else
	   return false;
}

function SubmitFormOnlyOnce() {
/*once the form's submit button is clicked, prevents the form from being submitted more than once.  
NOTE: Make sure to call this function AFTER all validation functions*/
   if (submitCount == 0)
   {
      submitCount++;
      return true;
   }
   else 
   {
      alert("This form has already been submitted.  Thanks!");
      return false;
   }
}

function ToggleDiv(divID, divState) 
/* Sets the visible state [divState = 0 (hide) or 1 (show)] of the div corresponding to the id passed in via divID */
{
	divState == 1 ? divDisplay = "block" : divDisplay = "none";
	document.getElementById(divID).style.display=divDisplay;
	//alert(divID + ': ' + document.getElementById(divID).style.display);
}

function Trim(sData) {
/*Trims leading/trailing spaces and tabs*/
	var sTrimmed = String(sData);
	sTrimmed = sTrimmed.replace(/(^[ |\t]+)|([ |\t]+$)/g, '');
	return sTrimmed;
}
