var frmValInfo = {
	frmID: null,
	checkFor: ['EMAIL', 'FIRSTNAME', 'LASTNAME', 'ADDRESS1', 'CITY', 'STATE', 'ZIPCODE', 'BMONTH', 'BDAY', 'BYEAR', 'GENDER'],
	inputID: ['email_address', 'first_name', 'last_name', 'address_line1', 'address_city', 'address_state', 'address_zip', 'bdate_month', 'bdate_day', 'bdate_year', 'g_selection'],
	dateChk: new Object(),
	errMsgType: 2,
	errClassName: "highlightERR",
	errTagsReset: ['label', 'input', 'textarea'],
	defaultBGColor: "#ffffff"
};

var checkResult = [false, false, false, false, false, false, false, false, false, false, false];

var validateForm = {

	init: function(frm) {
	
		var v = frmValInfo;
		v.frmID = frm.id;
		resetStuff();
		return (this.checkForm());
	
	},
	
	checkForm: function() {
	
		var v = frmValInfo;

		for (var x = 0; x < (v.checkFor).length; x++) {
			
			validateFunc(v.checkFor[x], x);
			
		}
		
		return(this.isSubmit());
		
	},
	
	isSubmit: function() {
		
		var ok = true;
		var x = 0;
		
		for (var x = 0; x < checkResult.length; x++) {
			if (ok) {
				ok = (!checkResult[x])?false:ok;
			}
		}
		
		if (!ok) {
			checkForBDate();
			setErrorMsgs(frmValInfo.errMsgType);
		}
		else {
			ok = checkForBDate();
			if (!ok)
				setErrorMsgs(frmValInfo.errMsgType);
		}
		/*
		for (var x = 0; x < checkResult.length; x++) {
			console.log(checkResult[x]);
		}*/
	
		return ok;
		
	}

};

function validateFunc(input, idNum) {

	var regEx = null;
	var str = null;
	var type = 'string';
	var v = frmValInfo;
	var chkIt = false;
	
	if (input != 'GENDER')
		str = document.getElementById(v.inputID[idNum]).value;
	
	if (input == 'EMAIL') {
	
		type = 'regex';
		regEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		chkIt = true;
	
	}
	
	if (input == 'FIRSTNAME' || input == 'LASTNAME' || input == 'ADDRESS1' || input =='CITY') {
		
		type = 'string';
		chkIt = true;
		
	}
	
	if (input == 'GENDER') {
		chkGenderCustom();
	}
	
	if (input == 'STATE') {
		
		str = str.toUpperCase();
		document.getElementById(v.inputID[idNum]).value = str;
		type = 'regex';
		regEx = /^(A[LKSZRAEP]|C[AOT]|D[EC]|F[LM]|G[ANU]|HI|I[ADLN]|K[SY]|LA|M[ADEHINOPST]|N[CDEHJMVY]|O[HKR]|P[ARW]|RI|S[CD]|T[NX]|UT|V[AIT]|W[AIVY])$/;
		chkIt = true;
	
	}
	
	if (input == 'ZIPCODE') {
		
		type = 'regex';
		regEx = /^((\d{5}([- ])\d{4})|(\d{5})|([AaBbCcEeGgHhJjKkLlMmNnPpRrSsTtVvXxYy]\d[A-Za-z]\s?\d[A-Za-z]\d))$/;
		chkIt = true;
		
	}
	
	if (input == 'BMONTH' || input == 'BDAY' || input == 'BYEAR') {
		type = 'number';
		//regEx = /^[-]?\d+(\.\d+)?$/
		chkIt = true;
	}
	

	
	if (chkIt) {
		
		inputCheck(type, idNum, regEx, str, input);
	}
	
}

function chkGenderCustom() {
	 checkResult[10] = (!document.getElementById('g_male').checked && !document.getElementById('g_female').checked)?false:true;
}

function inputCheck(type, idNum, regEx, str, input) {

	switch(type) {
	
		case 'string' : {
			checkResult[idNum] = (str.length<1)?false:true;
			//console.log(input + " - " + str.length);
			break;
		}
		
		case 'regex' : {
			checkResult[idNum] = regEx.test(str);
			
			if (checkResult[idNum] && input == 'BMONTH' || input == 'BDAY' || input == 'BYEAR') {
				frmValInfo.dateChk[input] = str;
			}
			
			break;
		}
		
		case 'number' : {
			str = parseFloat(str);
			checkResult[idNum] = (isNaN(str))?false:true;
			if (checkResult[idNum] && input == 'BMONTH' || input == 'BDAY' || input == 'BYEAR') {
				frmValInfo.dateChk[input] = str;
			}
			break;
		}
		
	}

}

function setErrorMsgs(type) {
	
	/*
		types:
		1 - highlight input box
		2 - highlight input box, highlight label
	*/
	
	for (var x = 0; x < checkResult.length; x++) {

		if (!checkResult[x]) {
		
			v = frmValInfo;
			id = v.inputID[x];


			switch(type) {
			
				case 1 : {
					errDisplayTypes('highlightBox', id);
					break;
				}
				
				case 2 : {
					errDisplayTypes('label', id);
					break;
				}
				
				case 3 : {
					errDisplayTypes('highlightBox', id);
					errDisplayTypes('label', id);
					break;
				}
			
			}
		
		}
		
	}
	
}

function errDisplayTypes(type, id) {
	
	v = frmValInfo;
	err = v.errClassName;
	
	switch(type) {
	
		case 'highlightBox' : {
			var tmp = document.getElementById(id).className;

			if (tmp.indexOf(err)<0) {
				tmp = tmp + ((tmp.length>0)?' ':'') + err;
				document.getElementById(id).className = tmp;
			}
			
			break;
		}
		
		case 'label' : {
		
			var tags = document.getElementsByTagName('label');
			for (var x = 0; x < tags.length; x++) {
				if (tags[x].htmlFor == id) {
					var tmp = tags[x].className;
					
					if (tmp.indexOf(err)<0) {
						tmp = tmp + ((tmp.length>0)?' ':'') + err; 
						tags[x].className = tmp;
					}
					
				}
			}
		
		}
	
	}

}

function resetStuff() {
	
	var v = frmValInfo;
	var tags = v.errTagsReset;
	var css = v.errClassName;
	
	for (var x = 0; x < checkResult.length; x++) {
		checkResult[x] = false;
	}
	
	for (var t = 0; t < tags.length; t++) {
		//alert(ttype[t]);
		var tg = document.getElementsByTagName(tags[t]);
		for (var x = 0; x < tg.length; x++) {
			if ((tg[x].className).indexOf(css)>-1) {
				var rep = tg[x].className.replace(/highlightERR/, '');
				tg[x].className = rep;
				
				if (tags[t] == 'input' || tags[t] == 'select') {
					
				}
				
			}
			
		}

	}
	
	document.getElementById('errMsgTxt').style.display = 'none';
	

}

/* check for valid date entry */
function checkForBDate() {
	var v = frmValInfo;
	var mm = parseFloat(v.dateChk['BMONTH']);
	var dd = parseFloat(v.dateChk['BDAY']);
	var yy = parseFloat(v.dateChk['BYEAR']);
	var isOK = false;
	
	if ((dd > 0) && (yy >= 1900 ) && (mm > 0 && mm < 13)) {
		
		if (mm == 1 || mm == 3 || mm == 5 || mm == 7 || mm == 8 || mm == 10 || mm == 12) { /* 31 day months */
			isOK = (dd<32)?true:false;
		}
		else {
			if (mm == 2 && (yy%4) == 0) { /* leap year */
				isOK = (dd<30)?true:false;
			}
			else if (mm == 2) {/* regular feb */
				isOK = (dd<29)?true:false;
			}
			else { /* 30 day months  */
				isOK = (dd<31)?true:false;
			}
		}
	
	}
	
	if (!isOK) {
		checkResult[7] = false;
		checkResult[8] = false;
		checkResult[9] = false;
		return false;
	}
	else {
		return true;
	}
}

/* display numbers only */
function keyNumbersOnly (e) {
	var keyCode = (e.keyCode)?e.keyCode:(e.charCode)?e.charCode:e.which;
	var isOk = false;
	
	keyCode = parseFloat(keyCode);
	
	/* key codes allowed: backspace, del, end, home, tab */	
	if (keyCode == 8 || keyCode == 46 || keyCode == 9 || keyCode == 35 || keyCode == 36 || keyCode == 37 || keyCode == 39) {
		isOk = true;
	}
	else {
		isOk =  (keyCode >= 48 && keyCode <=57)?true:false;
	}
	
	return isOk;
}

/* custom key restrictions on keypress */
function restrictKey (e, type) {
	var keyCode = (e.keyCode)?e.keyCode:(e.charCode)?e.charCode:e.which;
	var isOk = false;
	var allow = "";
	
	switch (type) {
		case 'string': { 
			allow = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; break;
		}
		
		case 'num' : {
			allow = '0123456789'; break;
		}
	}
	
	keyCode = parseFloat(keyCode);
	
	/* key codes allowed: backspace, del, end, home, tab */	
	if (keyCode == 8 || keyCode == 46 || keyCode == 9 || keyCode == 35 || keyCode == 36 || keyCode == 37 || keyCode == 39) {
		isOk = true;
	}
	else {
		isOk = (allow.indexOf(String.fromCharCode(keyCode))!=-1)?true:false;
	}
	
	return isOk;
}

/* removes input field value on focus */
function removeValOnFocus(i) {
	var v = parseFloat(i.value);
	if (isNaN(v)) i.value = "";
}

/* disable google input completion background color on IE*/
if(window.attachEvent)
    window.attachEvent("onload",setListeners);

  function setListeners(){
    inputList = document.getElementsByTagName("INPUT");
    for(i=0;i<inputList.length;i++){
      inputList[i].attachEvent("onpropertychange",restoreStyles);
      inputList[i].style.backgroundColor = "";
    }
    selectList = document.getElementsByTagName("SELECT");
    for(i=0;i<selectList.length;i++){
      selectList[i].attachEvent("onpropertychange",restoreStyles);
      selectList[i].style.backgroundColor = "";
    }
  }

  function restoreStyles(){
    if(event.srcElement.style.backgroundColor != "")
      event.srcElement.style.backgroundColor = "";
  }
  
function bypass() {

	if (GETVars.GET["override"]) {
		 if (GETVars.GET["override"] == "true")
		 	return true;
	}
	
	return false;
  
}

function stepOneOK(id) {
	if (document.getElementById(id).value != 0) {
		return true;
	}
	return false;
}

function checkOnlyOne(check, other) {
	
	if (document.getElementById(check).checked)
		document.getElementById(check).value = 'y'
	else
		document.getElementById(check).value = 'n';
		
	document.getElementById(other).value = 'n';
	document.getElementById(other).checked = (document.getElementById(other).checked)?false:document.getElementById(other).checked;
	
	/*
	console.log(other + " = " + document.getElementById(other).value);
	console.log(check + " = " + document.getElementById(check).value);
	*/
	
}

function checkIfChecked(id)
{
	document.getElementById(id).value = (document.getElementById(id).checked)?'y':'n';
}


function checkBoxValidate(id) {
	for (x = 0; x < 7; x++) {
		if (eval("document.extraInfo.q2[" + x + "].checked") == true) {
			document.extraInfo.q2[x].checked = false;
			if (x == id) {
				document.extraInfo.q2[x].checked = true;
         	}
      	}
   	}
}