/**
 * This library includes functions needed to verify field values on submission of various forms.
 *
 * @author Bobby Edge
 * @since 2006-11-23
 */

function returnObjById( id )
{
	if (document.getElementById)
		var returnVar = document.getElementById(id);
	else if (document.all)
		var returnVar = document.all[id];
	else if (document.layers)
		var returnVar = document.layers[id];

	return returnVar;
}

function returnObjByName( name )
{
	if (document.getElementByName)
		var returnVar = document.getElementByName(name);
	else if (document.all)
		var returnVar = document.all[name];
	else if (document.layers)
		var returnVar = document.layers[name];

	return returnVar;
}

function trim(theString)
{
	return theString.replace(/^\s+|\s+$/g,"");
}

function ltrim(theString)
{
	return theString.replace(/^\s+/,"");
}

function rtrim(theString)
{
	return theString.replace(/\s+$/,"");
}

function validateEmail (strEmail)
{

	return /^([0-9a-zA-Z]+([_.-]?[0-9a-zA-Z]+)*@[0-9a-zA-Z]+[0-9,a-z,A-Z,.,-]*(.){1}[a-zA-Z]{2,4})+$/.test(strEmail);
}

function validatePhone (strPhone)
{
	return /^(([0-9]{1})*[- .(]*([0-9a-zA-Z]{3})*[- .)]*[0-9a-zA-Z]{3}[- .]*[0-9a-zA-Z]{4})+$/.test(strPhone);
}

function validateUrl (strUrl)
{
	return /^(http[s]?:\/\/|ftp:\/\/)?(www\.)?[a-zA-Z0-9-\.]+\.[a-zA-Z]{2,4}$/.test(strUrl);
}

function validateDateMDY (strDate)
{
	return /^([\d]|1[0,1,2])\/([0-9]|[0,1,2][0-9]|3[0,1])\/\d{4}$/.test(strDate);
}

function validateDateYMD (strDate)
{
	return /^\d{4}-(0[0-9]|1[0,1,2])-([0,1,2][0-9]|3[0,1])$/.test(strDate);
}

function validateTime (strTime)
{
	return /^([0-1][0-9]|[2][0-3])(:([0-5][0-9])){1,2}(\s{1}[aApP]{1}[mM]{1})?$/.test(strTime);
}

function validateZipCodeUS (strZip)
{
	return /^[0-9]{5}(-[0-9]{4})?$/.test(strZip);
}

function validateZipCodeEU (strZip)
{
	return /^([A-Z][0-9]){3}$/.test(strZip);
}

function validateFileTypes (strFileName, strFileExt)
{
	if (strFileExt == undefined)
		strFileExt = "jpg|gif|png";

	return new RegExp("\.(" + strFileExt + ")$").test(strFileName);
}

function validateNumber (strNumber)
{
	return /^[1-9]{1}[0-9]*$/.test(strNumber);
}

function alertErrors (arrErrors, strHeader, strFooter)
{
	if (strHeader == undefined)
		strHeader = "Your form has the following error(s):";
	if (strFooter == undefined)
		strFooter = "Please correct and resubmit.     ";
	if (arrErrors == undefined)
		arrErrors = new Array();
	var strErrors = "";

	for (var i = 0; i < arrErrors.length; i ++)
		strErrors += "     -  " + arrErrors[i] + "     \n";

	if (strErrors)
		strErrors += "\n";

	window.alert(strHeader + "     \n\n" + strErrors + strFooter + "     ");
}