// VALIDATION CORE

function fcnValidateForm(thisform, strAction)
{
	// This function checks the entire form before it is submitted
	with (thisform)
	{
	if(valTextBlank(login,"Login")==false) {return false;};
	if(valTextBlank(password,"Password")==false) {return false;};
		action = strAction;
		method = "post";
		submit();
	}
}

function fcnCancel(strAction)
{
	document.location = strAction;
}

// CHECK FOR BLANK FIELDS

function valTextBlank(varFormVar, strFieldName)
{
	with (varFormVar)
	{
		var value = trim(value);
		if (value==null || value=="")
		{
			valAlertRequiredField(strFieldName);
			focus();
			return false;
		}
		else {return true;}
	}
}

function valPullDownBlank(varFormValue, strFieldName)
{
	with (varFormValue)
	{
		if (value == 0)
		{
				valAlertRequiredField(strFieldName);
				focus();
				return false;
		}
		else {return true;}
	}
}

function valAlertRequiredField(strFieldName)
{
	alert("'" + strFieldName + "' is a required field.\r\rPlease enter a value for '" + strFieldName + "'.");
}

// NUMERIC FUNCTIONS

function valNumeric(varFormValue, strFieldName) {

	with (varFormValue)
	{
		if (isNumeric(value)==false)
		{
			alert("'" + strFieldName + "' is a numeric field.\r\rPlease enter a numeric value for '" + strFieldName + "'.");
			value = '';
			focus();
			return false;
		}
	}
	return true;
}

function isNumeric(varValue) {
	var numdecs = 0;
	var mychar

	with (varValue)
	{
		for (i = 0; i < length; i++) {
			mychar = charAt(i)
			if ((mychar >= "0" && mychar <= "9") || mychar == ".")
			{
				if (mychar == "."){ numdecs++; }
			}
			else
			{return false;}
		}
		if (numdecs > 1) {return false;}
		return true;
	}
}

// TEXTBOX FUNCTIONS
function valTextBoxSize(varFormValue, maxlimit, strFieldName) {

	with (varFormValue)
	{
		if (value.length > maxlimit) // if too long...trim it!
		{
			value = value.substring(0, maxlimit);
			alert("'" + strFieldName + "' has exceeded the maximum text limit and has been trimmed to " + maxlimit + " characters.\r\rPlease review the '" + strFieldName + "' field for accuracy.");
			return false;
		}
		else
		{
			return true;
		}
	}
}
// TELEPHONE NUMBERS
function valTelephone(varFormValue, strFieldName)
{
	with (varFormValue)
	{
		if (funValidCharacters(varFormValue.value, "0123456789 -.()/+") == false)
		{
			alert("Invalid entry for the field '" + strFieldName + ".'\rThe values allowed are:'0123456789 -.()/+'\r\rPlease re-enter a value for '" + strFieldName + "'.");
			value='';
			focus();
			return false;
		}
	}
	return true;
}


// DATE FUNCTIONS
function valDate(varFormVar, strFieldName)
{
	with (varFormVar)
	{
		if (fcnValidDate(value)==false)
		{
			valAlertBadDate(strFieldName);
			select();
			focus();
			return false;
		}
		else {return true;}
	}
}

function valAlertBadDate(strFieldName)
{
	var sDefaultDateStyle = 'dd/mm/yy';
	// var sDefaultDateStyle = 'mm/dd/yy';
	var sDateStyle = (typeof(gsDateStyle) == 'string' ? gsDateStyle : sDefaultDateStyle);
	alert("'" + strFieldName + "' is either and invalid date, or is in an incorrect format.\r\rPlease re-enter a the date (Format: '" + sDateStyle.toUpperCase() + "').");
}

function fcnValidDate(dateStr) {
	// Checks for the following valid date formats:
	// if (gsDateStyle == 'mm/dd/yy')
	// MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY
	// else
	// DD/MM/YY   DD/MM/YYYY   DD-MM-YY   DD-MM-YYYY

	// Also separates date into month, day, and year variables

	var sDefaultDateStyle = 'dd/mm/yy';
	// var sDefaultDateStyle = 'mm/dd/yy';
	var sDateStyle = (typeof(gsDateStyle) == 'string' ? gsDateStyle : sDefaultDateStyle);

	if(dateStr != '')
	{
		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) {
			// alert("Date is not in a valid format.")
			return false;
		}

		if (sDateStyle == 'mm/dd/yy') {
			day = matchArray[3]; // parse date into variables
			month = matchArray[1];
			year = matchArray[4];		
		}
		else {
			day = matchArray[1]; // parse date into variables
			month = 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
	}
	else {
		return true;  // date is valid
	}
}

// GENERAL FUNCTIONS

function funValidCharacters(strFormValue, strValidCharacters)
{
	var j = 0;
	for( j = 0; j < strFormValue.length; j++ )
	{
		var thisChar  = strFormValue.charAt(j);
		if ( strValidCharacters.indexOf( thisChar ) == -1 )
		{
			return false;
		}
	} // end for
}

function valPasswordConfirm(varPwd, varConfirm)
{
	if (varPwd.value != varConfirm.value)
		{
			alert("The password confirmation is invalid.\r\rPlease try again.")
			varPwd.value="";
			varConfirm.value="";
			varPwd.focus();
			return false;	//Passwords are not identical!
		}
	else
		{
			return true;	//Passwords are identical!
		}
}

function valMinMaxFieldSize(varFormValue, strFieldName, lowNum, hiNum) {
	with (varFormValue)
	{
		if ( value.length < lowNum || value.length > hiNum )
		{
				alert("'" + strFieldName + "' must be between " + lowNum + " and " + hiNum + " characters.\r\rPlease modify the '" + strFieldName + "' field to meet this criteria.");
				focus();
				return false;
		}
	}
	return true;
}

function valAlphaNumericCharacters(varPwd, varConfirm, strFieldName)
{
	if (funValidCharacters(varConfirm.value, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") == false)
	{
		alert("Invalid entry '" + strFieldName + ".'\r\r\'" + strFieldName + "' must be alphanumeric.\rPlease re-enter a value for '" + strFieldName + "'.");
		varPwd.value="";
		varConfirm.value="";
		varPwd.focus();
		return false;
	}
	else
	{
	return true;
	}
}

function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue;
}

