// JavaScript Document
// Written by Jesse Teal
var output	=	"The following must be corrected in order to submit this form:" + '\n';

// CONSTANT declarations
var vNotNull		=	1;		//text field has a value
var vIsNumeric		=	2;		//value is pure numeric (integer) 0-9
var vIsReal			=	4;		//value is a real number, decimal allowed
var vIsMoney		=	8;		//value is a real number, decimal and $ allowed, $ stripped prior to submit
var vIsAlphaNumeric	=	16;		//value is letters and numbers only a-z,A-Z,0-9
var vIsSize			=	32;		//value length is X characters long
var vIsFilename		=	64;		//value does NOT contain # % * ? < > |
var vIsDate			=	128;	//value is a valid date in m/d/yy or m-d-yy format
var vNotDefault		=	256;	//select value is not -1
var vIsChecked		=	512;	//radio value IS selected
var vNotEmpty		=	1024;	//multiple select has at least 1 selected entry
var vIsPhone		=	2048;	//formatted: (xxx) xxx-xxxx
var vIsSSN			=	4096;	//formatted: xxx-xx-xxxx
var vIsEmail		=	8192;	//formatted: email@domain.xxx

function validateObject()
{
	if(arguments.length >= 3)
	{
		this.fieldname	=	arguments[0];		//[0] : input element
		this.method		=	arguments[1];		//[1] : method: bitMask Field
		this.name		=	arguments[2];		//[2] : Name output in Alert box "First Name" for Name_First input
	}
	if(arguments.length > 3)
		this.size		=	arguments[3];		//[3] : (optional) number for minsize check
	this.validate		=	Validate;	//callback function
	this.setField		=	_setField;
	this.setMethod		=	_setMethod;
	this.setName		=	_setName;
}
function _setField(){this.field = arguments[0];}
function _setMethod(){this.method=arguments[0];}
function _setName(){this.name=arguments[0];}
//==========================================================
// Validate()
//===========================================================
function Validate()
{
	var returnValue	=	false;
	if(!this.field)
		this.field 	=	eval(this.fieldname);
	if(this.method & vIsPhone)//moved above NotNull because it was wiping the field of bogus char AFTER the not null test
	{
		var rxp	=	/[^\d]/g;
		var TestValue	=	this.field.value.replace(rxp,"");//remove non-digits
		if(TestValue.length != 10 && TestValue.length > 0)
		{
			this.field.style.borderColor='red';
			this.field.style.borderStyle='solid';
			output += "* " + this.name + " needs to be a phone number like: (nnn) nnn-nnnn\n";
			returnValue = true;
		}
		else if(TestValue.length == 0)
		{
			this.field.value = "";
		}
		else
		{
			this.field.value = "(" + TestValue.slice(0,3) + ") " + TestValue.slice(3,6) + "-" + TestValue.slice(6,10);
		}
	}
	if((this.method & vNotNull) && this.field.value.length == 0)
	{
		this.field.style.borderColor='red';
		this.field.style.borderStyle='solid';
		output += "* " + this.name + " must be filled with some value" + '\n';
		returnValue = true;
	}
	if((this.method & vIsNumeric) && this.field.value.search(/^[0-9]*$/) == -1)
	{
		this.field.style.borderColor='red';
		this.field.style.borderStyle='solid';
		output += "* " + this.name +" Must be a Number" + '\n';
		returnValue = true;
	}
	if((this.method & vIsReal) && this.field.value.search(/^[0-9]*\.?[0-9]*$/) == -1)
	 {
		this.field.style.borderColor='red';
		this.field.style.borderStyle='solid';
		output += "* " + this.name +" Must be a Real Number" + '\n';
		returnValue = true;
	 }
	if(this.method & vIsMoney)
	{
		var rxp	=	/,/g;
		this.field.value	=	this.field.value.replace(rxp,"");//remove commas
		this.field.value	=	this.field.value.replace("$","");//remove dollar sign
		
		if(this.field.value.search(/^-?([0-9]*\.?[0-9]{0,2})$/))
		{
			this.field.style.borderColor='red';
			this.field.style.borderStyle='solid';
			output +=	"* " + this.name+ " Must be a Dollar Amount" + '\n';
			returnValue = true;
		}
	}
	if((this.method & vIsAlphaNumeric) && this.field.value.search(/^[-|(a-z)|(0-9)|(A-Z)]*$/) == -1)
	{
		this.field.style.borderColor='red';
		this.field.style.borderStyle='solid';
		output += "* " + this.name + " Must be Alphanumeric" + '\n';
		returnValue = true;
	}
	if((this.method & vIsSize) && this.field.value.length != this.size && this.field.value != "")
	{
		this.field.style.borderColor='red';
		this.field.style.borderStyle='solid';
		output	+= "* " + this.name + " Must be " + this.size + " Characters Long" + '\n';
		returnValue = true;
	}
	if((this.method & vIsFilename) && this.field.value.search(/[#%\*\?<>\|";]/) > -1)
	{
		this.field.style.borderColor='red';
		this.field.style.borderStyle='solid';
		output += "* " + this.name + " Must be a valid filename. Filenames cannot contain special characters. Please rename the file." + '\n';
		returnValue = true;
	}
	if((this.method & vIsDate) && this.field.value != "")
	{
		var error	=	false;
		this.field.value	=	this.field.value.replace(/-/g, "/");//put 1-1-00 into 1/1/00 format
		var re_date = /^(\d+)\/(\d+)\/(\d+)$/;
		var mil_date = /^(\d+) ([a-zA-Z]+) (\d+)$/;
		if(!re_date.exec(this.field.value) && !mil_date.exec(this.field.value))
			error = true;//neither formats found
/*		var arrayDate	=	this.field.value.split("/");
		if(arrayDate.length != 3)
			error=true;
		else//valid lengths
		{
			for(index=0;index < arrayDate.length;index++)
			{
				if(arrayDate[index].length < 1)//empty field
					error = true;
				if(isNaN(arrayDate[index].valueOf()))//not a number
					error=true;
			}
		}
*/
		if(error)
		{
			this.field.style.borderColor='red';
			this.field.style.borderStyle='solid';
			output	+= "* " + this.name + " needs to be a date in a MM/DD/YY" + '\n';
			returnValue = true;
		}	
	}
	if((this.method & vNotDefault) && (this.field.value == -1 || this.field.value == ""))
	{
		this.field.style.color='red';
		output += "* " + this.name + " requires a Selection" + '\n';
		returnValue = true;
	}
	else if(this.method & vNotDefault)
	{
		this.field.style.color=''; //standard ClearForm doesn't cover selection color
	}
	if(this.method & vIsChecked)
	{
		var isSel	=	false;
		for(index=0;index < this.field.length;index++)
		{
				if(this.field[index].checked == true)
				{
					isSel = true;
				}
		}
		if(!isSel)
		{
			output += "* " + this.name + " requires a Selection\n";
			returnValue = true;
		}
	}
	if((this.method & vNotEmpty) && this.field.selectedIndex == -1)
	{
		this.field.style.color='red';
		output += "* " + this.name + " requires a Selection" + '\n';
		returnValue = true;
	}
	else if(this.method & vNotEmpty)
	{
		this.field.style.color=''; //standard ClearForm doesn't cover selection color
	}
	if(this.method & vIsSSN)
	{
		var rxp	=	/[^\d|\.]/g;
		var TestValue	=	this.field.value.replace(rxp,"");//remove non-digits
		if(TestValue.length != 9 && TestValue.length > 0)
		{
			this.field.style.borderColor='red';
			this.field.style.borderStyle='solid';
			output += "* " + this.name + " needs to be a number formatted like: nnn-nn-nnnn\n";
			returnValue = true;
		}
		else if(TestValue.length == 0)
		{
			this.field.value = "";
		}
		else
		{
			this.field.value = TestValue.slice(0,3) + "-" + TestValue.slice(3,5) + "-" + TestValue.slice(5,9);
		}
	}
	if(this.method & vIsEmail)
	{//\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
		var rxp	=	/\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/g;
		var TestValue	=	this.field.value.replace(rxp,"");//remove non-email
		if(TestValue.length != 0)
		{
			this.field.style.borderColor='red';
			this.field.style.borderStyle='solid';
			output += "* " + this.name + " needs to be a valid formatted email address\n";
			returnValue = true;
		}
	}
	return returnValue;
}
//=============================================================
// ClearForm(form)
//=============================================================
function ClearForm(frm)
{
	for(i=0;i<frm.elements.length;i++)
	{
		frm.elements[i].style.borderColor='';
		frm.elements[i].style.borderStyle='';
	}
}
//=============================================================
//
// ValidateForm()
// Requires: formObject
//			arguments[1] -> arguments[n]
//
//=============================================================
function ValidateForm(formObject)
{
	var isInvalid = false;
	ClearForm(formObject);
	for(i=1;i<arguments.length;i++)//loop through all passed validation Objects
	{
		isInvalid |= arguments[i].validate();
	}
	if(isInvalid)
	{
		alert(output);
		output	=	"The following must be corrected in order to submit this form:" + '\n';
		for(lp=1;lp<arguments.length;lp++)
		{
			if(arguments[lp].type == "text" || arguments[lp].type == "select-one")
				arguments[1].field.focus();
		}
//		if(formObject.PostAction.length==1);//if a save|PostAction button is defined, disable it
//			formObject.PostAction.disabled = true;//will do nothing if there are more than one postactions
		return false;
	}
	else
	{
//		if(formObject.PostAction.length==1);//if a save|PostAction button is defined, disable it
//			formObject.PostAction.disabled = false;//will do nothing if there are more than one postactions
		return true;
	}	
}