
/* 
function 1: checkvalid_blank (String Fieldname , String Description);
	checks to see if fieldname contains a value or not

function 2: checkvalid_zip (String Fieldname , String Description);
	checks to see if value in fieldname is 5 digits or not

function 3: checkvalid_ssn (String Fieldname , String Description);	
	checks to see if value in fieldname is 9 digits or not

function 4: checkvalid_password_match (String Fieldname1 , String Fieldname2 , String Description);	
	checks to see if two password fields have the same value
	returns true if yes or inserts description in fieldlist if false

function 5: checkvalid_ndigits (String Fieldname , Mixed NumberOfDigits , String Description);
	For checking a specified number of digits:
	return true if the NumberOfDigits is assigned to a integer value and the field contains the specified number of digits.
	
	For checking at least one digit (or digits only):
	return true if the NumberOfDigits is assigned to "any" and the field contains digits only.

function 6: checkvalid_email (String Fieldname , String Description);
	checks to see if value in fieldname is a valid email address or not
	
function 7: checkradio_value (String Fieldname , String SearchValue);	
	checks to see if the radio button value was selected or not (returns true or false)

function 8: checkselect_value (String Fieldname , String SearchValue);	
	checks to see if the select box value was selected or not (returns true or false)	
	
function 9: checkbox_selected (String Fieldname);	
	checks to see if the specified checkbox was checked or not (returns true or false)
	
function 10: checkvalid_digits_greaterthan (String Fieldname , Int Number , String Description);	
	checks to see if the fieldname value is a digit greater than Number passed in
	
function 11: checkvalid_digits_lessthan (String Fieldname , Int Number , String Description);
	checks to see if the fieldname value is a digit less than Number passed in	
			
function 12: checkvalid_digits_between (String Fieldname , Int Number1 , Int Number2 , String Description);	
	checks to see if the fieldname value is a digit between two specified Numbers passed in	(inclusive)	
	
function 13: checkradio_blank (String Fieldname , String Description); 
//checks for blank radio button arrays and sets the focus on the first element of the blank array.	
	
*/

/*
	The following two variables are utilized in the validation functions below to ensure the validation 
	alert box doesn't grow so long the user can't fit it within the window.  It seems that 25 items is 
	a good amount for a user with an 800x600 screen resolution.
*/
var failedValFieldCounter = 0; // reset this to 0 at the beginning of your validation function
var maxValFields = 9999999; // set to 9999999 for legacy purposes.  Change in your validation function (probably 25)

function setAvailableMethods()
{
	this.valid = true;
	this.fieldList = "";
	this.numericlist= "";
	this.focusField = null;
	
	this.checkvalid_blank = checkvalid_blank;
	this.checkvalid_email = checkvalid_email;
	this.checkvalid_zip = checkvalid_zip;
	this.checkvalid_ssn = checkvalid_ssn;
	this.checkvalid_password_match = checkvalid_password_match;
	this.checkvalid_3digits = checkvalid_3digits;
	this.checkvalid_4digits = checkvalid_4digits;
	this.checkvalid_digitsonly = checkvalid_digitsonly;
	this.checkvalid_digits_greaterthan = checkvalid_digits_greaterthan;
	this.checkvalid_digits_lessthan = checkvalid_digits_lessthan;
	this.checkvalid_digits_between = checkvalid_digits_between;
	this.checkradio_blank = checkradio_blank;
	this.checkradio_value = checkradio_value;
	this.checkselect_value = checkselect_value;
	this.checkbox_selected = checkbox_selected;
}

function checkvalid_blank(fieldname , description)
{
	if (failedValFieldCounter>=maxValFields) return;
	if (f.elements[fieldname].value=="") 
	{
		if (valid) focusField = f.elements[fieldname];
		this.valid = false;
		failedValFieldCounter++;
		if (description)
		{
			this.fieldList += description + "\n";
		}
		else
		{
			this.fieldList += fieldname + "\n";
		}
	}
} // checkvalid_blank()

	
function checkvalid_zip(fieldname , description)
{
	if (failedValFieldCounter>=maxValFields) return;
	if (!(/(^\d{5}$)|(^\d{5}-\d{4}$)/.test(f.elements[fieldname].value)) || f.elements[fieldname].value=="")
	{
		if (valid) focusField = f.elements[fieldname];
		this.valid = false;
		failedValFieldCounter++;
		if (description)
		{
			this.fieldList += description + "\n";
		}
		else
		{
			this.fieldList += fieldname + "\n";
		}
	}
} // checkvalid_zip()

function checkvalid_ssn(fieldname , description)
{
	if (failedValFieldCounter>=maxValFields) return;
	if (!(/(^\d{9}$)|(^\d{3}-\d{2}-\d{4}$)/.test(f.elements[fieldname].value)) || f.elements[fieldname].value=="") 
	{
		if (valid) focusField = f.elements[fieldname];
		this.valid = false;
		failedValFieldCounter++;
		if (description)
		{
			this.fieldList += description + "\n";
		}
		else
		{
			this.fieldList += fieldname + "\n";
		}
	}
} // checkvalid_ssn()


function checkvalid_password_match(fieldname1 , fieldname2 , description)
{
	if (failedValFieldCounter>=maxValFields) return;
	if ((f.elements[fieldname1].value) != (f.elements[fieldname2].value)) 
	{
		if (valid) focusField = f.elements[fieldname1];
		this.valid = false;
		failedValFieldCounter++;
		if (description)
		{
			this.fieldList += description + "\n";
		}
		else
		{
			this.fieldList += fieldname1 + "does not match" + fieldname2 + "\n";
		}
	}
} // checkvalid_password_match()
	
	
function checkvalid_3digits(fieldname , description)
{
	checkvalid_ndigits(fieldname , "3" , description)
} // checkvalid_3digits()

function checkvalid_4digits(fieldname , description)
{
	checkvalid_ndigits(fieldname , "4" , description)
} // checkvalid_4digits()
	
function checkvalid_digitsonly(fieldname , description)
{
	checkvalid_ndigits(fieldname , "any" , description)
} // checkvalid_digitsonly()

	
function checkvalid_ndigits(fieldname , num_digits , description)
{
	if (failedValFieldCounter>=maxValFields) return;
	if (isNaN(num_digits))
	{
		pattern = eval("/^\\d+$/");
	}
	else
	{
		pattern = eval("/^\\d{"+num_digits+"}$/");
	}
	if (!(pattern.test(f.elements[fieldname].value)) || f.elements[fieldname].value=="")
	{
		if (valid) focusField = f.elements[fieldname];
		this.valid = false;
		failedValFieldCounter++;
		if (description)
		{
			this.fieldList += description + "\n";
		}
		else
		{
			this.fieldList += fieldname + "\n";
		}
	}
} // checkvalid_ndigits()

function checkvalid_email(fieldname , description)
{
	if (failedValFieldCounter>=maxValFields) return;
	if (!validemail(f.elements[fieldname].value)) 
	{
		if (valid) focusField = f.elements[fieldname];
		this.valid = false;
		failedValFieldCounter++;
		if (description)
		{
			this.fieldList += description + "\n";
		}
		else
		{
			this.fieldList += fieldname + "\n";
		}
	}
} // checkvalid_email()
	
	

	
function checkradio_blank(fieldname, description)
{
	if (failedValFieldCounter>=maxValFields) return;
	for (i = 0; i < f.elements[fieldname].length; i++)
	{
		var arraychecked=0;
		if (f.elements[fieldname][i].checked)
		{
			arraychecked++;
			break;
		}
	}
	if (arraychecked==0)
	{
		if (valid) focusField = f.elements[fieldname][0];
		this.valid = false;
		failedValFieldCounter++;
		if (description)
		{
			this.fieldList += description + "\n";
		}
		else
		{
			this.fieldList += fieldname + "\n";
		}
	}   
} // checkradio_blank()
	
	
function checkradio_value(fieldname , checkvalue)
{
	for (i = 0; i < f.elements[fieldname].length; i++)
	{
		if (f.elements[fieldname][i].checked)
		{
			if (f.elements[fieldname][i].value == checkvalue) 
			{
				return true;
			}
		}
	} 
	return false;	
} // checkradio_value()
		
		
function checkselect_value(fieldname , checkvalue)
{
	for (i = 0; i < f.elements[fieldname].length; i++)
	{
		if (f.elements[fieldname][i].selected)
		{
			if (f.elements[fieldname].options[i].value == checkvalue) 
			{
				return true;
			}
		}
	} 
	return false;	
} // checkselect_value()
	
function checkbox_selected(fieldname,description )
{
	if (failedValFieldCounter>=maxValFields) return;
	if (f.elements[fieldname].checked)
	{
		return true;
	}
	else
	{
		if (valid) focusField = f.elements[fieldname];
		this.valid = false;
		failedValFieldCounter++;
		if (description)
		{
			this.fieldList += description + "\n";
		}	
		return false;		
	}
	return false;		
} // checkbox_selected()

var fpu = eval("/^(\\-)?(\\d)+(\\.)?(\\d)*$/");
function checkvalid_digits_greaterthan(fieldname , numvalue , description)
{
	if ( !fpu.test(f.elements[fieldname].value) || parseFloat(f.elements[fieldname].value) <= numvalue ||
			f.elements[fieldname].value == "") 
	{
		if (valid) focusField = f.elements[fieldname];
		this.valid = false;
		failedValFieldCounter++;
		if (description)
		{
			this.fieldList += description + "\n";
		}
		else
		{
			this.fieldList += fieldname + "\n";
		}
	}
} // checkvalid_digits_greaterthan()

function checkvalid_digits_lessthan(fieldname , numvalue , description)
{
	if (failedValFieldCounter>=maxValFields) return;
	if ( !fpu.test(f.elements[fieldname].value) || parseFloat(f.elements[fieldname].value) >= numvalue ||
			f.elements[fieldname].value == "") 
	{
		if (valid) focusField = f.elements[fieldname];
		this.valid = false;
		failedValFieldCounter++;
		if (description)
		{
			this.fieldList += description + "\n";
		}
		else
		{
			this.fieldList += fieldname + "\n";
		}
	}
} // checkvalid_digits_lessthan()

function checkvalid_digits_between(fieldname , numvalue1 , numvalue2 , description)
{
	if (failedValFieldCounter>=maxValFields) return;
	if ( !fpu.test(f.elements[fieldname].value) || parseFloat(f.elements[fieldname].value) < numvalue1 ||
			parseFloat(f.elements[fieldname].value) > numvalue2 || f.elements[fieldname].value == "") 
	{
		if (valid) focusField = f.elements[fieldname];
		this.valid = false;
		failedValFieldCounter++;
		if (description)
		{
			this.fieldList += description + "\n";
		}
		else
		{
			this.fieldList += fieldname + "\n";
		}
	}
} // checkvalid_digits_between()

function isnumeric(n)
{
	nums = /[\\0-9]/;
	if (nums.test(n)) return true;
} // isnumeric()


function validemail(c)
{
	invalidChars = " /:,;"
	if (c == "")
	{
		return false;
	}
	for (i=0; i<invalidChars.length; i++)
	{
		badChar = invalidChars.charAt(i)
		if (c.indexOf(badChar,0) != -1)
		{
			return false;
		}
	}
	atPos = c.indexOf("@",1)
	if (atPos == -1) 
	{
		return false;
	}
	if (c.indexOf("@",atPos+1) != -1) 
	{
		return false;
	}
	periodPos = c.indexOf(".",atPos)
	if (periodPos == -1)
	{
		return false;
	}
	if (periodPos+3 > c.length)
	{
		return false;
	}
	return true;
} // validemail()
	
	
// Trim leading spaces from a string
String.prototype.ltrim = function () 
{
	var s=this;
	while (s.charAt(0)==" ") s=s.substring(1,s.length);
	return s;
} // String.prototype.ltrim()

// Trim trailing spaces from a string
String.prototype.rtrim = function () 
{
	var s=this;
	while (s.charAt(s.length-1)==" ") s=s.substring(0, s.length-1);
	return s;
} // String.prototype.rtrim()

// Return the rightmost n characters of a string
String.prototype.left = function (n)
{
	if (n < this.length) {
		return this.substring(0, n);
	} 
	else
	{
		return this;
	}
} // String.prototype.left()

// Return the rightmost n characters of a string
String.prototype.right = function (n) {
	if (n < this.length) 
	{
		return this.substring(this.length-n, this.length);
	} 
	else
	{
		return this;
	}
} // String.prototype.right()

// Test a string to see if it's entirely digits
String.prototype.isNumeric = function() 
{
	for (var i=0; i<this.length; ++i)
	{
		if ("0123456789".indexOf(this.charAt(i)) == -1) return false;
	}
	return true;
} // String.prototype.isNumeric()


function formatMoney(fld, show_error) 
{
	var amt=fld.value.ltrim().rtrim();
	var moneyChars="0123456789";
	var cents="00";
	var formatAmt="";
	var errorDesc="";

	// If there is a dollar sign, remove it.
	if (amt.charAt(0) == "$") amt = amt.substring(1,amt.length).ltrim();
	
	// Check for a decimal, and set a flag if there is one in the right place.
	if (amt.indexOf(".") != -1 ) 
	{
		if (amt.indexOf(".") == amt.lastIndexOf(".") && amt.indexOf(".") >= amt.length-3) {
		if (amt.indexOf(".") < amt.length-1) cents = amt.substring(amt.indexOf(".")+1, amt.length);
		if (cents.length==1) cents=cents+"0";
			amt = amt.substring(0,amt.indexOf("."));
		}
		else
			errorDesc = "Decimal placement error";
	}
	
	// Remove any commas.
	if (errorDesc.length == 0) 
	{
		while (amt.indexOf(",") != -1) 
		{
			amt = amt.substring(0,amt.indexOf(","))+amt.substring(amt.indexOf(",")+1, amt.length);
		}
	}
	
	// Now, check for any invalid characters.  There should not be any characters other than digits.  
	for (var i=0; i<amt.length && errorDesc.length==0; ++i) 
	{
		if (moneyChars.indexOf(amt.charAt(i)) == -1) 
		{
			errorDesc = "Dollar amount contains at least one illegal character.";
		}
	}
	
	if (errorDesc.length==0) 
	{
		// The dollar amount now contains only digits, so we can format them.
		if (amt.length == 0) amt="0";
		formatAmt = "."+cents;
		while (amt.length > 3) 
		{
			formatAmt = ","+amt.substring(amt.length-3,amt.length)+formatAmt;
			amt=amt.substring(0,amt.length-3);
		}
		formatAmt = "$"+amt+formatAmt;
		//    formatAmt = amt+""+formatAmt;
	} 
	else 
	{
		if (show_error) 
		{
			alert(errorDesc);
			fld.focus();
		}
		else
		{
			return false;
		}
	}
	fld.value = formatAmt;
	return true;
} // formatMoney()

function checkDate(dateVal)
{
	if (/^(\d{2})([\/\.-]?)(\d{2})(\2)(\d{2}|\d{4})$/.test(dateVal) ||
		/^(\d{1,2})([\/\.-])(\d{1,2})(\2)(\d{2}|\d{4})$/.test(dateVal)) 
	{
		this.month   = RegExp.$1 * 1;
		this.date    = RegExp.$3 * 1;
		this.year    = RegExp.$5 * 1;
		this.isValid = true;
	}
	else
	{
		this.isValid = false;
	} // end if-else
	return (this.isValid);
} // checkDate()

function parseDateOb2Str(dateOb)
//# returns date as a string
{
	var sMonth = String(dateOb.getMonth() + 1);
	var sDate = String(dateOb.getDate());
	var sYear = String(dateOb.getFullYear());
	
	if (sMonth.length == 1) sMonth = '0' + sMonth;
	if (sDate.length == 1) sDate = '0' + sDate;
	
	var newDate = sMonth + '/' + sDate + '/' + sYear;
	return   String(newDate);
} // parseDateOb2Str()

// Validate and format date. Begin and end dates must be javascript date objects
// e.g. <input type="text" name="date" onchange="valDate(this, 'Date of Foo')">
function valDate(f, desc) 
{
	var dateFlag, errorDesc = '';
	if (2 > arguments.length || arguments.length > 6) return;        
	dateFlag = new checkDate(f.value);
	if (dateFlag.isValid)
	{
		var M = dateFlag.month;
		var D = dateFlag.date;
		var Y = dateFlag.year;
		if (Y < 100) Y = (Y <= 50) ? Y + 2000 : Y + 1900;
		if (M < 1 || M > 12) 
		{
			errorDesc = " The month portion of the date you entered is invalid.  Please choose a number from 1 to 12.";
		} 
		else 
		{
			var ld = (Y % 400 == 0 || Y % 100 != 0 && Y % 4 == 0) ? "9" : "8";
			var maxdays = "312" + ld + "31303130313130313031";
			if (D < 1 || D > maxdays.substr ((M - 1) * 2, 2)) errorDesc = " The day portion of the date you entered is invalid.  Please enter a number from 1 to " + (maxdays.substr ((M - 1) * 2, 2)) + ".";
		} // end if-else
		var nDate = new Date(Y,(parseInt(M)-1),D);
		if (errorDesc.length == 0) 
		{
			// Date is valid and in range; format it.
			f.value =  parseDateOb2Str(nDate);
		} // end if
	} 
	else 
	{
		errorDesc = "You have entered an invalid date. Please re-enter the date in an accepted format, " + 
				"e.g. 01/01/2000 for January 1, 2000.";
	} // end if-else
	
	
	if (errorDesc.length > 0) 
	{
		f.focus();
		f.select();
		alert(desc + ": " + errorDesc);
		return false;
	} // end if
	return true;
} // valDate()
  
  

// Validate and format phone number.
// e.g. <input type="text" name="phoneNum" onchange="valPhone(this, 'Foo Phone');">
function valPhone (f, desc)
{
	if (/^\((\d{3})\)\s?(\d{3})[\s|\.|-](\d{4})((\s+|\s?)([eE][xX][tT]|[xX]?|[eE][xX][tT]\.)(\d{0,5}))?$/.test(f.value) ||
		/^(\d{3})[\s|\/|\.|-]?(\d{3})[\s|\.|-]?(\d{4})((\s+|\s?)([eE][xX][tT]|[xX]?|[eE][xX][tT]\.)(\d{0,5}))?$/.test(f.value))
	{
		if (RegExp.$7) f.value="("+RegExp.$1+")"+" "+RegExp.$2+"-"+RegExp.$3+" ext."+RegExp.$7;
		else f.value="("+RegExp.$1+")"+" "+RegExp.$2+"-"+RegExp.$3;
	}
	else
	{
		f.focus ();
		f.select ();
		
		var x10tionMesg;
		if (desc == "Fax Number") 
		{
			x10tionMesg = "";
		}
		else
		{
			x10tionMesg = "\n If you wish to enter an extension (up to five digits), place it at the end of the " + 
					"main telephone number, and precede it with an 'x'.";
		}	
		
		alert (desc + ": " + " You have entered an invalid number.  The number must be 10 digits in length and " + 
				"contain the 3 digit area code.  Please re-enter, formatting will be completed automatically.\n " + x10tionMesg);       
		return false;
	} // end if-else
	
	return true;
} // valPhone()	



// Validate and format a Social Security Number
// e.g. <input type="text" name="ssn" onchange="valSSN(this, 'Social Security Number');">
// note: the SSN field should have a maxlength of 11 characters
function valSSN(f, desc)
{	
	var output = "";
	var letter = "";
	var output_counter = -1;
	var failed_validation = false;
	if (!f) alert("Could not find the specified field in valSSN");
	if (!f.value) return true;
	if (f.value.length<9)
	{
		alert (desc + ": " + " You have entered an invalid SSN.  The number must be at least 9 digits in length.  Please re-enter, formatting will be completed automatically.\\n");       
		return false;
	}
	for (var i=0; i<11; i++)
	{
		letter = f.value.charAt(i);
		if (letter.charCodeAt(0)>="0".charCodeAt(0) && letter.charCodeAt(0)<="9".charCodeAt(0))
		{
			output += letter;
			output_counter++;
			if (output_counter==2 || output_counter==5)
			{
				output += "-";
				output_counter++;
			}
			if (output_counter==10)
			{
				f.value = output;
				return true;
			}
		}
	}
	if (output_counter<11)
	{
		alert (desc + ": " + " You have entered an invalid SSN.  The number must be at least 9 digits in length.  Please re-enter, formatting will be completed automatically.\\n");       
		return false;
	}
} // valSSN()
