//main validation function
function validate(form,id,id2,numLength,realName){
	this.form = form;
	this.id = id;
	this.id2 = id2;
	this.numLength = numLength;
	this.realName = realName;
	this.requiredField = function (e){
		isRequired = new isEmpty(this.form,this.id)
		var errortext = isRequired.checkText()
		var error = "";
		if (!(errortext == "")){
			error = "<li>"+this.realName + " " + errortext + "</li>";
			mark = new highlight(this.id)
		  	mark.markInput()
		  	mark.markLabel()
		}
	  return error;
	}
	this.requiredRadio = function (e){
		isRequired = new isEmpty(this.form,this.id)
		var errortext = isRequired.checkRadioGroup()
		var error = "";
		if (!(errortext == "")){
			error = "<li>"+this.realName + " " + errortext + "</li>";
			mark = new highlight(this.id)
		  	mark.markLabel()
		}
	  return error;
	}
	this.checkNum = function (e) {
		isNumber = new isNum(this.form,this.id,this.numLength)
		var errortext = isNumber.validateNum()
		var error = "";
		if (!(errortext == "")){
			error = "<li>"+this.realName + " " + errortext + "</li>";
			mark = new highlight(this.id)
			mark.markInput()
		  	mark.markLabel()
		}
	  return error;
	}
	this.checkIsSame = function (e) {
		isTheSame = new isSame(this.form,this.id,this.id2)
		var errortext = isTheSame.validateSame()
		var error = "";
		if (!(errortext == "")){
			error = "<li>"+this.id + " and " + this.id2 + " " + errortext + "</li>";
			mark1 = new highlight(this.id)
			mark1.markInput()
		  	mark1.markLabel()
			mark2 = new highlight(this.id2)
			mark2.markInput()
		  	mark2.markLabel()
		}
		return error;
	}
	this.checkEmail = function (e) {
		isValidEmail = new isEmail(this.form,this.id)
		var errortext = isValidEmail.validateEmail()
		var error = "";
		if (!(errortext == "")){ 
			error = "<li>"+this.id + " " + errortext + "</li>";
			mark1 = new highlight(this.id)
			mark1.markInput()
		  	mark1.markLabel()
		}
		return error;
	}
	
}
function customValidate1 (form,id,id2,id3){
	this.form = form;
	this.id = id;
	this.id2 = id2;
	this.id3 = id3;
	this.checkRadioGroup = function(e) {//checks if radio button selection has been made in a specified radio group.
			var checkvalue = ""
			for (i=0, n=this.form[this.id].length; i<n; i++) {
					if (this.form[this.id][i].checked) {
					checkvalue = this.form[this.id][i].value;
					break;
				} 
		   }
		   var error="";
		   switch (checkvalue){
		   case "":
		   	error  = "<li>Visitor Type is a required selection.</li>\n";
		   	mark = new highlight(this.id)
		  	mark.markLabel()
		   break
		   case "Broker":
		   	isRequired = new isEmpty(this.form,this.id2)
			var errortext = isRequired.checkText()
				if (!(errortext == "")){
				error = "<li>Group Size is a required field.</li>\n";
				mark = new highlight(this.id2)
				mark.markInput()
				mark.markLabel()
				}
			break
		   case "Employer":
		   	isRequired = new isEmpty(this.form,this.id2)
			var errortext = isRequired.checkText()
				if (!(errortext == "")){
				error = "<li>Group Size is a required field.</li>\n";
				mark = new highlight(this.id2)
				mark.markInput()
				mark.markLabel()
				}
			break
			case "Member_Employee":
		   	isRequired = new isEmpty(this.form,this.id3)
			var errortext = isRequired.checkText()
				if (!(errortext == "")){
				error = "<li>Group Number is a required field.</li>\n";
				mark = new highlight(this.id3)
				mark.markInput()
				mark.markLabel()
				}
			break
		   }
		return error;
		}
}
		
	
//Checks for required fields	
function isEmpty(form,id) {
	this.form = form;
	this.id = id;
	this.errortext = "";
	this.checkText = function (e) {//check if value of text input or select input is empty.
			var strng = this.form[this.id].value;
	  if ( strng == 0) {
		  this.errortext = "is a required field.";
	  }
	return this.errortext;	  
	}
	this.checkRadioGroup = function(e) {//checks if radio button selection has been made in a specified radio group.
			for (i=0, n=this.form[this.id].length; i<n; i++) {
					if (this.form[this.id][i].checked) {
					var checkvalue = this.form[this.id][i].value;
					break;
				} 
		   }
		   if (!(checkvalue)) {
			   this.errortext = "is a required selection.";
			}
		return this.errortext;
		}
}
//check if field is a number or check field is a number and a specified length.
function isNum (form,id,numLength){
	this.form = form;
	this.id = id;
	this.numLength = numLength;
	this.errortext = "";
	this.validateNum = function ( e ){
		var strng = this.form[this.id].value;
		if (!(strng == "")) {
			var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters for phone numbers and SSN numbers.
				if (isNaN(stripped)) {
				   this.errortext = "contains illegal characters.";
				}else {
						if (!(this.numLength == 0)){
							if (!(stripped.length == this.numLength)) {
								this.errortext = "is the wrong length.";
							}
						}
				}
		}
		return this.errortext;
	}
}
//compare 2 fields and verify if they match.
function isSame(form,id,id2) {
	this.form = form;
	this.id = id;
	this.id2 = id2;
	this.errortext = "";
	this.validateSame = function (e) {
		var strng1 = this.form[this.id].value;
		var strng2 = this.form[this.id2].value;
		if (!(strng1 == '')&& !(strng2 == "")){
			if (!(strng1 == strng2)){
				this.errortext = "must match.";
			}
		}
		return this.errortext;
	}
}	
//checks for valid email address
function isEmail(form,id) {
	this.form = form;
	this.id = id;
	this.errortext = "";
	this.validateEmail = function (e) {
		var emailFilter=/^.+@.+\..{2,3}$/;
		var strng = this.form[this.id].value;
			if (!(strng == "")){	
				if (!(emailFilter.test(strng))) { 
				   this.errortext += "Is not a valid e-mail address. \n";
				} 
					//test email for illegal characters
				var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/;
				if (strng.match(illegalChars)) {
						this.errortext += "Contains illegal characters. \n";
					}	
			}return this.errortext;
	}
}
function resetValidation(form){
	this.form = form;
	this.checkState = function (e) {
		for (var i = 0; i < this.form.length; i++) {
			if(this.form.elements[i].className == 'error'){
				newClass = new highlight(this.form.elements[i].name)
				newClass.unMarkInput()
				newClass.unMarkLabel()
			}
			if(this.form.elements[i].type == 'radio') {
				newClass = new highlight(this.form.elements[i].name)
				newClass.unMarkLabel()
			}
		}
	}
}
	
//used to change styles on <label> and <input> tags to mark the non validating form fields
function highlight(formElement) {
	this.formElement = formElement;
	this.markInput = function(e) {
		document.getElementById(this.formElement).className='error';
	}
	this.unMarkInput = function(e) {
		document.getElementById(this.formElement).className='clear';
	}
	this.markLabel = function(e) {
		document.getElementById("lbl" + this.formElement).className='error';
	}
	this.unMarkLabel = function(e) {
		document.getElementById("lbl" + this.formElement).className='clear';
	}
}