//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
/*
validate.js
Contains standard validation functions for HTML forms
*/

//Initialize the validation message
var valMsg = "";

function isNumberKey(evt)
{
	//Used in the onKeyPress event of a form field, only allows the user to type numbers

	evt = (evt) ? evt : event;
	var theKey = (evt.which) ? evt.which : event.keyCode;
	var theChar = String.fromCharCode(theKey);
	
	if ((theChar != "." && isNaN(theChar)) || theChar == " ")
		return false;
	else
		return true;
}
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
function isIntNumberKey(evt)
{
	//Used in the onKeyPress event of a form field, only allows the user to type positive integers
	
	evt = (evt) ? evt : event;
	var theKey = (evt.which) ? evt.which : event.keyCode;
	var theChar = String.fromCharCode(theKey);
	
	if ((isNaN(theChar)) || theChar == " ")
		return false;
	else
		return true;
}
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
function isIntNumberKeyNeg(evt)
{
	//Used in the onKeyPress event of a form field, only allows the user to type positive and negative integers

	evt = (evt) ? evt : event;
	var theKey = (evt.which) ? evt.which : event.keyCode;
	var theChar = String.fromCharCode(theKey);
	
	if ((theChar != "-" && isNaN(theChar)) || theChar == " ")
		return false;
	else
		return true;
}
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
function isPhoneKey(evt)
{
	//Used in the onKeyPress event of a form field, only allows the user to type numbers

	evt = (evt) ? evt : event;
	var theKey = (evt.which) ? evt.which : event.keyCode;
	var theChar = String.fromCharCode(theKey);
	
	if ((theChar != "-" && isNaN(theChar)) || theChar == " ")
		return false;
	else
		return true;
}
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
function isDateKey(thisField,evt)
{
	//Used in the onKeyPress event of a form field, only allows the user to type numbers and slashes in the correct position

	evt = (evt) ? evt : event;
	var theKey = (evt.which) ? evt.which : event.keyCode;
	var theChar = String.fromCharCode(theKey);
	
	if (((thisField.value.length < 2) || (thisField.value.length < 5 && thisField.value.length > 2) || (thisField.value.length > 5)) && (isNaN(theChar)) || theChar == " ")
		return false;
	else if (((thisField.value.length == 2) || (thisField.value.length == 5)) && theChar != "/")
		return false;
	else
		return true;
}
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
function moveNextFld(thisField, fieldLen, nextField)
{
	//This function moves the focus to the next field if the current field is full
	if (thisField.value.length == fieldLen)
		nextField.focus();
}
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
function isBlank(theField,fieldName)
{
	//Used to check for empty fields
	if (theField.value.length < 1 || theField.value == "")
		valMsg = valMsg + "You must provide a value for " + fieldName + ".\n";
}
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
function isChecked(theField,fieldName)
{
	//Used to check for empty checkboxes
	if (!theField.checked)
		valMsg = valMsg + "You must check the " + fieldName + " box.\n";
}
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
function isRange(theField,fieldName,fromNo,toNo)
{
	//Used to check for numeric ranges in fields, does not check for blanks
	if ((theField.value.length > 0 || theField.value != "") && (theField.value < fromNo || theField.value > toNo))
		valMsg = valMsg + "The " + fieldName + " must be between " + fromNo + " and " + toNo + ".\n";
}
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
function mustChoose(theField,fieldName)
{
	//Used to force a selection from a drop-down (with a default value of 0)
	if (theField.value == 0)
		valMsg = valMsg + "You must choose " + fieldName + ".\n";
}
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
function confirmAction(entName,entValue,actionName)
{
	//Used to confirm the deletion of an item
	return confirm("Are you sure you want to " + actionName + "\n" + entName + ": " + entValue);
}
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
function checkVal()
{
	//Used in the onSubmit event to check for any error messages
	if (valMsg != "")
	{
		alert(valMsg);
		return false;
	}
	else
		return true;
}
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
function getToday(theField)
{
	//Used to populate a form field with today's date
	var theDate = new Date();
	theField.value =  (theDate.getMonth()+1) + "/" + theDate.getDate() + "/" + theDate.getFullYear();
}
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
function textAreaMaxLength(theField,maxLength)
{
	//Used in the onKeyPress event of a textarea field, to provide a maxlength capability
	if (theField.value.length > maxLength - 1)
	{
		alert("You have reaced the limit of " + maxLength + " characters allowed in this field");
		return false;
	}
	else
		return true;
}
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
function updateMessage(entType,entId)
{
	// Display a message informing the user that an update has been performed
	alert("Thank you. " + entType + " " + entId + " has been updated.")
}
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
function isDependBlank(Determiner,theField,fieldName)
{
	if (Determiner != '0' && Determiner != 0)
	{
		if (theField.value.length < 1 || theField.value == "") valMsg = valMsg + "You must provide a value for " + fieldName + ".\n";
	}

}
function checkPanelMaterial(theMaterial,theThickness)
{
	if(theMaterial.value == "OSB"){
		if(theThickness.value == 7/16 || theThickness.value == 15/32 || theThickness.value == 19/32 || theThickness.value == 23/32){
			valMsg = valMsg + "OSB Pallets can only have thickness of 1/2, 5/8, 3/4.\n";
		}
	}
	if(theMaterial.value == "Plywood"){
		if(theThickness.value == 1/2 || theThickness.value == 5/8 || theThickness.value == 3/4){
			valMsg = valMsg + "Plywood Pallets can only have thickness of 7/16, 15/32, 19/32, 23/32.\n";
		}
	}
}

//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->
//--- ****************************************************************************************************************** --->