//regular expressions for validating fields
var vfmtEntryRequired = ".+";				//entry of any length and character required
var vfmtNumberRequired = "^[\\d]+$";		//number of any length, must be at least one digit (can be 0)
var vfmtNumber = "^[\\d]*$";				//number of any length or can be blank (can be 0)
var vfmtEmail = "^[\\w\\.\\-]+@[\\w\\.\\-]+$";	//email address: name@domain.tld
var vfmtZip = "^\\d{5}$|^\\d{5}\\-\\d{4}$";	//zip code 12345 or 12345-1234
var vfmtPhoneParen = "^\\(\\d{3}\\)\\d{3}\\-\\d{4}$|^\\(\\d{3}\\)\\s\\d{3}\\-\\d{4}$" //phone number (123)123-1234 or (123) 123-1234
var vfmtPhone = "^\\d{3}\\-\\d{3}\\-\\d{4}$|^\\d{10}$|^\\d{3}\\.\\d{3}\\.\\d{4}$" //phone number 123-123-1234, 1231231234 or 123.123.1234
var vfmtPercent = "^\\d{0,3}\\.?\\d{0,2}$" //percentage with 2 decimal places
var vfmtPercentRequired = "^\\d{1,3}\\.?\\d{0,2}$" //percentage with 2 decimal places (entry required)
var vfmtTime = "^$|^\\d{1,2}:\\d{2} ?[aApP][Mm]$" //time in the format: HH:MM am
var vfmtTimeRequired = "^\\d{1,2}:\\d{2} ?[aApP][Mm]$" //time in the format: HH:MM am
var vfmtShiftDiffDollar = "^\\d?\\.?\\d{0,2}$"

function FormValidate(ValObjArray)
{
	var VName;
	for (VName in ValObjArray)
	{
		if(!ValObjArray[VName].ValidateField())
		{
			return false;
		}
	} 
	return true;
}

function ValidateField()
{
	var bOK = true;
	
	if(bOK && this.Required)
	{
		if(IsEmpty(this.Object.value))
		{
			alert("Please enter " + this.AAn + " " + this.Description + ".");
			bOK = false;
		}
	}
	if (bOK && this.RegExpr > "")
	{
		var re = new RegExp(this.RegExpr);
		if (!re.test(this.Object.value))
		{
			bOK = false;
			var strMsg = "Please enter " + this.AAn + " " + this.Description;
			if (this.Format > "")
			{
				strMsg += " using this format:\n" + this.Format;
			}
			else
			{
				strMsg += ".";
			}
			alert(strMsg);
		}
	}
	if (bOK && (this.Min != null || this.Max != null))
	{
		var Num = parseFloat(this.Object.value)
		if ((this.Min != null && Num < this.Min) || (this.Max != null && Num > this.Max))
		{
			var msg = "";
			if (this.RangeMessage.length > 0)
			{
				msg = this.RangeMessage.replace(/\[description\]/i, this.Description);
				if (this.Min != null)
				{
					msg = msg.replace(/\[minvalue\]/i, this.Min.toLocaleString());
				}
				if (this.Max != null)
				{
					msg = msg.replace(/\[maxvalue\]/i, this.Max.toLocaleString());
				}
			}
			else
			{
				if (this.Min != null && this.Max != null)
				{
					msg = "Please enter a value for " + this.Description + " that is between " + this.Min + " and " + this.Max + "."
				}
				else if (this.Min != null)
				{
					msg = "Please enter a value for " + this.Description + " that is equal to or greater than " + this.Min + "."
				}
				else
				{
					msg = "Please enter a value for " + this.Description + " that is less than or equal to " + this.Max + "."
				}
			}
			alert(msg);
			bOK = false;
		}
	}
	if (bOK && (this.ValidateMore != null))
	{
		//the function called is responsible to notifying the user of any problems encountered
		bOK = (this.ValidateMore());
	}
	if(!bOK)
	{
		this.Object.focus();
		this.Object.select();
	}
	return bOK;
}

function SetRange(Min, Max)
{
	this.Min = Min;
	this.Max = Max;
}

function ValidationObject(Obj, Desc, RegExpr, Fmt)
{
	this.Object = Obj;
	this.Description = Desc;
	this.RegExpr = RegExpr;
	this.Format = Fmt;

	this.Min = null;
	this.Max = null;
	this.SetRange = SetRange;
	this.RangeMessage = "";
			
	this.AAn = "a";
	this.ValidateField = ValidateField;
	this.ValidateMore = null;

	this.Required = false;
}

function IsEmpty(inputStr)
{
	if(inputStr == null || inputStr == "")
	{
		return true;
	} 
	return false;
} 

function KPNumeric(E)
{
	var CharCode = GetCharCodeFromEvent(E);
	if (CharCode > 0)
	{
		return IsValidChar("0123456789", CharCode);
	}
	return true;
}

function KPCurrency(E)
{
	var CharCode = GetCharCodeFromEvent(E);
	if (CharCode > 0)
	{
		return IsValidChar("0123456789.", CharCode);
	}
	return true;
}

function KPTime(E)
{
	var CharCode = GetCharCodeFromEvent(E);
	if (CharCode > 0)
	{
		return IsValidChar("0123456789apmAPM: ", CharCode);
	}
	return true;
}

function KPZip(E)
{
	var CharCode = GetCharCodeFromEvent(E);
	if (CharCode > 0)
	{
		return IsValidChar("0123456789-", CharCode);
	}
	return true;
}

function KPPhone(E)
{
	var CharCode = GetCharCodeFromEvent(E);
	if (CharCode > 0)
	{
		return IsValidChar("0123456789()-. ", CharCode);
	}
	return true;
}

function IsValidChar(Chars, Code)
{
	return Chars.indexOf(String.fromCharCode(Code)) >= 0;
}
function GetCharCodeFromEvent(evt)
{
	/* figures out in the mess of event codes
	// for different browsers which property
	// to use from the event and returns it
	*/
	if (navigator.userAgent.indexOf("MSIE") >= 0)
	{
		//if IE, use the keyCode property
		return evt.keyCode;
	}
	else
	{
		//if not IE (NN/Moz/FireFox/Opera), use the charCode property
		return evt.charCode;
	}
}
