<!--
function testRequired(counter) {
	var thisReq = this.vReq;
	if (thisReq != true && thisReq != false) thisReq = eval(this.vReq);
	if (!thisReq) {
		return true;
	} else if (counter < 1) {
		this.par.errors[this.par.errors.length] =  '- '+this.desc+' - '+((this.objT == 'text' || this.objT == 'textarea' || this.objT == 'hidden' || this.objT == 'password') ? 'Entry' : 'Selection')+' Required.\n';
	} else {
		return true;
	}
}

function testMin(counter) {
	if (!this.vMin) {
		return true;
	} else if (counter > 0 && counter < this.vMin) {
		this.par.errors[this.par.errors.length] =  '- '+this.desc+' - '+this.vMin+' '+((this.objT == 'text' || this.objT == 'textarea' || this.objT == 'hidden' || this.objT == 'password') ? 'Character' : 'Selection')+' Minimum.\n';
		return false;
	} else {
		return true;
	}
}

function testMax(counter) {
	if (!this.vMax) {
		return true;
	} else if (counter > 0 && counter > this.vMax) {
		this.par.errors[this.par.errors.length] =  '- '+this.desc+' - '+this.vMax+' '+((this.objT == 'text' || this.objT == 'textarea' || this.objT == 'hidden' || this.objT == 'password') ? 'Character' : 'Selection')+' Maximum.\n';
		return false;
	} else {
		return true;
	}
}

function testRegExp(value) {
	var re, eTxt;
	var reTxt="A-Za-z", reTxt2=" \\._\\-'", reNum="0-9", reNum2="\\.,\\-\\(\\)", reCh2="\\?!@_#\\$&\\*\\(\\)\\-\\+=\\/\";:'\\/\\s";
	var thisReq = this.vReq;
	if (thisReq != true && thisReq != false) thisReq = eval(this.vReq);
	if (value == '' && thisReq == false) {
		return true;
	} else if (this.vType == 'vTxt') {
		re = "/["+reTxt+"]{"+value.length+",}/";
		eTxt = " - Alphabetic Characters Only.\n";
	} else if (this.vType == 'vTxt2') {
		re = "/["+reTxt+reTxt2+"]{"+value.length+",}/";
		eTxt = " - Alphabetic Characters and Common Punctuation Only.\n";
	} else if (this.vType == 'vNum') {
		re = "/["+reNum+"]{"+value.length+",}/";
		eTxt = " - Numbers Only.\n";
	} else if (this.vType == 'vNum2') {
		re = "/["+reNum+reNum2+"]{"+value.length+",}/";
		eTxt = " - Valid Numbers Only.\n";
	} else if (this.vType == 'vCh') {
		re = "/["+reTxt+reTxt2+reNum+reNum2+"]{"+value.length+",}/";
		eTxt = " - Standard Characters Only.\n";
	} else if (this.vType == 'vCh2') {
		re = "/["+reTxt+reTxt2+reNum+reNum2+reCh2+"]{"+value.length+",}/";
		eTxt = " - Valid Characters Only.\n";
	} else if (this.vType == 'none') {
		re = "/[^%]{"+value.length+",}/";
		eTxt = " - % Sign Not Allowed.\n";
	} else if (this.vType == 'vPhone') {
		re = "/[0-9\(\) \-]{"+value.length+",}/";
		eTxt = " - Valid Phone Number Only.\n";
	} else if (this.vType == 'vEmail') {
		re = "/^([^()<>@,;:\\\\\".\\s\\x00-\\x1F\\x7F\\[\\]]+|\"([^\\\\\"\\x0D]|\\\\.)*\")(\\.([^()<>@,;:\\\\\".\\s\\x00-\\x1F\\x7F\\[\\]]+|\"([^\\\\\"\\x0D]|\\\\.)*\"))*@([a-z0-9]([-a-z0-9]*[a-z0-9])?)(\\.([a-z0-9]([-a-z0-9]*[a-z0-9])?))*(\\.([a-z]{2}|com|edu|gov|int|mil|net|org|biz|info|name|pro|aero|coop|museum|cat|jobs|mobi|tel|travel|xxx))$/i";
		eTxt = " - Invalid Email Address.\n";
	}  else if (this.vType == 'vCCN') { 
          if (testCC(value)) { 
               return true; 
          } else { 
               this.par.errors[this.par.errors.length] =  '- '+this.desc+" - Invalid Credit Card Number.\n"; 
               return false; 
          }
	} else if (this.vType == 'vCCV') {
		re = /^\d{3,4}$/;
		eTxt = " - Invalid Credit Card Validation Number.\n";
	} else if (this.vType == 'vField') {
		re = /^\w{1,}$/;
		eTxt = " - Invalid Name.\n";
	} else if (this.vType == 'vURL') {
		re = /^http:\/\/[\w\.\/=\?\&]{1,}$|^https:\/\/[\w\.\/=\?\&]{1,}$/;
		eTxt = " - Invalid URL Format.\n";
	} else { return true;}
		// pattern for email address... returns TRUE if valid... that an incorrect response, others are exclusive validation
	if (eval(re+".test(value)")) { return true;}
	else {
		this.par.errors[this.par.errors.length] =  '- '+this.desc+eTxt;
		return false;
	}
}

function testCC(value) {
	var visa = /^4\d{3}-?\d{4}-?\d{4}-?\d{4}$/;
	var visa2 = /^4\d{3}-?\d{4}-?\d{4}-?\d{1}$/;
	var mc = /^5[1-5]\d{2}-?\d{4}-?\d{4}-?\d{4}$/;
	var disc = /^6011-?\d{4}-?\d{4}-?\d{4}$/;
	var amex = /^3[4,7]\d{13}$/;
	var diners = /^3[0,6,8]\d{12}$/;
	var jcb = /(^(352)[8-9](\d{11}$|\d{12}$))|(^(35)[3-8](\d{12}$|\d{13}$))/;
	var auto = /^x{11,12}\d{4}$/;
	
	if(auto.test(value)) return true;
	else if(!visa.test(value) && !visa2.test(value) && !mc.test(value) && !disc.test(value) && !amex.test(value) && !diners.test(value) && !jcb.test(value)) return false;

	var checksum = 0;
   	for (var i=(2-(value.length % 2)); i<=value.length; i+=2) {
    	checksum += parseInt(value.charAt(i-1));
   	}
   	// Analyze odd digits in even length strings or even digits in odd length strings.
   	for (var i=(value.length % 2) + 1; i<value.length; i+=2) {
    	var digit = parseInt(value.charAt(i-1)) * 2;
      	if (digit < 10) { checksum += digit; } else { checksum += (digit-9); }
   	}
   	if ((checksum % 10) == 0) return true; else return false;
}

function testText() {
	for (var i=0; i<this.objL; i++) {
		var obj = this.objArray[i];
		obj.value = obj.value.replace(/^ {1,}/,"");
		obj.value = obj.value.replace(/ {1,}$/,"");
		if (!this.testRequired(obj.value.length)) {}
		else if (!this.testMin(obj.value.length)) {}
		else if (!this.testMax(obj.value.length)) {}
		else if (!this.testRegExp(obj.value)) {}
		else { 
			this.value[i] = obj.value;
			this.counter[i] = obj.value.length;
		}
	}
}

function testTextarea() {
	this.testText();
}

function testHidden() {
	this.testText();
}

function testPassword() {
	this.testText();
}

function testSelectOne() {
	for (var i=0; i<this.objL; i++) {
		var counter = 0;
		var obj = this.objArray[i];
		for (var j=0; j<obj.length; j++) {
			if (obj.options[j].selected == true && obj.options[j].value != '') {
				this.value[i] = obj.options[j].value;
				counter++;
			}
		}
		if (!this.testRequired(counter)) {}
		else if (!this.testMin(counter)) {}
		else if (!this.testMax(counter)) {}
		else { this.counter[i] = counter;}
	}
}

function testSelectMultiple() {
	this.testSelectOne();
}

function testCheckbox() {
	var counter = 0;
	for (var i=0; i<this.objL; i++) { 
		var obj = this.objArray[i];
		if (obj.checked) {
			this.value[i] = obj.value;
			this.counter[i] = 1;
			counter++;
		}
	}
	if (!this.testRequired(counter)) {}
	else if (!this.testMin(counter)) {}
	else if (!this.testMax(counter)) {}
}

function testRadio() {
	this.testCheckbox();
}

function Field(par,name,desc,vReq,vType,vMin,vMax){
	// properties
	this.par=par;
	this.name=name;
	this.desc=desc;
	this.vReq=vReq;
	this.vType=vType;
	this.vMin=(vMin) ? vMin : null;
	this.vMax=(vMax) ? vMax : null;
	this.obj=par.obj[name];
		if (!this.obj) alert("Error Found;  "+this.name+"\nYou are attempting to validate a field that has not been created.");
	this.objL=(/\d/.test(this.obj.length)) ? (this.obj.options) ? 1 : this.obj.length : 1;
	this.objArray=new Array();
		if (this.objL > 1) { for (var i=0; i<this.objL; i++) this.objArray[i] = this.obj[i];}
		else { this.objArray[0] = this.obj;}
	this.objT=null;
		for (i=0; i<this.objL; i++) {
			if (this.objT && this.objT != this.objArray[i].type) alert("Error Found;  "+this.name+"\nYou are attempting to validate fields of the same name described with multiple types.");
			this.objT=this.objArray[i].type;
		}
	this.value=new Array();
	this.counter=new Array();
	// methods
	this.testText=testText;
	this.testTextarea=testTextarea;
	this.testHidden=testHidden;
	this.testPassword=testPassword;
	this.testSelectOne=testSelectOne;
	this.testSelectMultiple=testSelectMultiple;
	this.testRadio=testRadio;
	this.testCheckbox=testCheckbox;
	this.testRequired=testRequired;
	this.testMin=testMin;
	this.testMax=testMax;
	this.testRegExp=testRegExp;
	// assign validation method
	this.test = (this.objT=='text') ? testText : (this.objT=='textarea') ? testTextarea : (this.objT=='hidden') ? testHidden : (this.objT=='password') ? testPassword : (this.objT=='select-one') ? testSelectOne : (this.objT=='select-multiple') ? testSelectMultiple : (this.objT=='radio') ? testRadio : (this.objT=='checkbox') ? testCheckbox : alert("Error Found;  "+this.name+"\nYou are attempting to validate a field created with an unexpected type.");
}

function addField(name,desc,vReq,vType,vMin,vMax) {
	this.fields[this.fields.length] = new Field(this,name,desc,vReq,vType,vMin,vMax);
}

function getProperty(fieldName,property) {
	for (var i=0; i<this.fields.length; i++) { if (fieldName == this.fields[i].name) return eval("this.fields[i]."+property+";");}
}

function preOps() {}

function finalOps() {}

if (typeof bam != "undefined") {
	if (typeof bam.tracking == "undefined") {
		$.ajax({type:"GET", async:false, cache:true, url:"/shared/scripts/bam.tracking.js", dataType:"script"});
	}
}

function validateForm() {
	this.errors=new Array();
	if (this.submitOnce == false) {
		this.submitOnce = true;
		this.preOps();
		for (var i=0; i<this.fields.length; i++) this.fields[i].test();
		if (this.errors.length < 1) this.finalOps();
		if (this.errors.length > 0) {
			if (this.errors.length > 30) {
				for (var i = this.errors.length-1; i >= 30; i--) this.errors.pop();
			}
			var thisError = '________________________________________________________\n\n';
			thisError += '        The form was not submitted because of the following error(s).\n';
			thisError += '________________________________________________________\n\n';
			thisError += this.errors.join('');
			alert(thisError);
			this.submitOnce = false;
			return false;
		} else {
			if (this.obj.format && this.obj.email_type) {
				if (this.obj.format.checked) this.obj.email_type.value = "html_email";
				else this.obj.email_type.value = "text_email";
			}
			if (this.obj.mobile_tel && this.obj.mobile_opt) {
				if (this.obj.mobile_tel.value != "") this.obj.mobile_opt.value = "yes";
				else this.obj.mobile_opt.value = "";
			}
			this.submitOnce = true;
			if (typeof bam != "undefined") {
				if (typeof bam.tracking != "undefined") {
					if (typeof bam.tracking.track != "undefined" && typeof s != "undefined") {
						if (typeof this.obj.FORM_CODE != "undefined") {
							bam.tracking.track({formSubmit:{formID:this.obj.FORM_CODE.value}});
						}
					}
				}
			}
			if (this.obj.method == "get") {
				return true;
			} else if (/formCustomSurveyServlet/gi.test(this.obj.action) || /formtool.do/gi.test(this.obj.action)) {
				this.obj.action = "/s/FormService/FormSubmitServlet";
			}
			return true;
		}
	} else {
		var thisError = '________________________________________________________\n\n';
		thisError += 'The form was submitted, please wait while we process your submission.\n';
		thisError += '________________________________________________________\n\n';
		alert(thisError);
		return false;
	}
}

function Validator(obj) {
	// properties
	this.obj=obj;
	this.fields=new Array();
	this.errors=new Array();
	this.submitOnce = false;
	// methods
	this.addField=addField;
	this.getProperty=getProperty;
	this.preOps=preOps;
	this.finalOps=finalOps;
	this.validateForm=validateForm;
}

function removeOption(obj,str) {
	for (var i = obj.length-1; i >= 1; i--) { 
		if (obj.options[i].value == str || obj.options[i].text == str) obj.options[i] = null;
	}
}

function selectAutoRemove(obj, d, h) {
	var today = new Date(),
	gD = new Date();
	if (typeof d != "undefined") today.setDate(today.getDate()+parseInt(d));
	if (typeof h != "undefined") today.setHours(today.getHours()+parseInt(h));
	for (var i=(obj.length-1); i>=1; i--) {
		var gA = obj.options[i].value.substr(0,10).split("/");
			gD.setFullYear(parseInt(gA[0]));
			gD.setMonth((parseInt(gA[1],10)-1));
			gD.setDate(parseInt(gA[2],10));
			gD.setHours(0,0,0,0);
			if (gD <= today) obj.options[i] = null;
	}
}
//-->

