/* do browser check */
var isIE = (navigator.appName.indexOf("Explorer") >= 0) ? isIE = true : isIE = false;

/* Default color to highlight incorrect form fields */
highlightColor = "FFFF66"

/* Validate Required
§	Checks to make sure all the required elements of a form are filled in
§	Uses a list of required fields and labels provided by JSP tag
§	Contructs and displays a dialog of the unfilled required fields
§	Returns true or false
*/
function validateRequired(formObj) {
	if (!validPrivacyPolicy(formObj)) return false;
	if (formObj.required_fields.value != "") {
		var reqFields = formObj.required_fields.value;
		var reqFieldName = reqFields.split(",");
		var missingFields = "";
		var blankCount = 0;
		for (i=0; i<reqFieldName.length; i+=2) {
			var currField = formObj[reqFieldName[i]];
			var j = i+1;
			if (!currField) continue;
			if (reqFieldName[i] != "service_type") {
				//if (isIE) currField.style.backgroundColor = "";
				if (EFBrowser.ie || (EFBrowser.ns && !EFBrowser.ns4)) {currField.style.backgroundColor = "";};
			}
			if ((reqFieldName[i] == "service_type") && (formObj.service_fields.value != "")) {
				var srvcFields = formObj.service_fields.value;
				var srvcFieldName = srvcFields.split(",");
				var checkCount = 0;
				for (n=0; n<srvcFieldName.length; n+=2) {
					if (emailFilledRequired(formObj[srvcFieldName[n]])) checkCount ++;
				}
				if (checkCount == 0) {
					missingFields += '     * ' + reqFieldName[j] + '\n';
					blankCount++
				}
			} else {
				if ((!emailFilledRequired(formObj[reqFieldName[i]])) || (!validTextInput(formObj[reqFieldName[i]]))) {
					//if (isIE) currField.style.backgroundColor = highlightColor;
					if (EFBrowser.ie || (EFBrowser.ns && !EFBrowser.ns4)) {currField.style.backgroundColor = highlightColor;};
					
					missingFields += '     * ' + reqFieldName[j] + '\n';
					blankCount++
				}
			}
		}
		if (missingFields) {
			var isPlural = (blankCount > 1) ? 'fields are' : 'field is';
			var isAlsoPlural = (blankCount > 1) ? 'contain' : 'contains';
			alert ('The following required ' + isPlural + ' empty or ' + isAlsoPlural + ' invalid data:\n\n' + missingFields + '\n Please complete the required fields and re-send your request');
			return false;
		} else {
			formObj.submit();
		}
	} else {
		formObj.submit();
	}
}

/* validPrivacyPolicy
§	For the finance form only
§	Checks the privacy policy for a valid response.
*/
function validPrivacyPolicy(theForm) {
	if (((theForm.name == "frmRequestFinancing") || (theForm.name == "frmHoldRequest")) && (theForm.agree_checked)) {
		var agree = 0;
		var optout = 0;
		var optoutExists = 1;
		if (isChecked(theForm.agree_checked)) {	
			agree = 1;
		}
		if (theForm.opt_out_checked) {
			if (isChecked(theForm.opt_out_checked)) {
				optout = 1;
			}
		} else {
			optoutExists = 0;
		}
		if ((agree == 0) && (optoutExists == 0)){
			alert ("You must indicate agreement with the Privacy Policy");
			return false;
		} else if ((agree == 0) && (optout == 0)) {
			alert ("You must select a Privacy Option");
			return false;
		}
	}
	return true;
}

/* Required elements
§	Takes the required form element objects in a given form
§	Checks to see if the user has filled them all out
§	Highlights incompleted form fields
§	Returns true or false
*/
function emailFilledRequired() {
	var w = 0;
	for (var i=0; i<arguments.length; i++) {
		var currElem = arguments[i];
		//alert (currElem.name);
		var currElemType = currElem.type;
		switch (currElemType) {
			case "checkbox":
				if (!isChecked(currElem)) {
					w++;
				}
				break;
			case "text":
				if (currElem.value == "") {
					w++;
				}
				break;
			case "password":
				if (currElem.value == "") {
					w++;
				}
				break;
			case "textarea":
				if (currElem.value == "") {
					w++;
				}
				break;
			case "select-one":
				if (!isValidSelect(currElem)) {
					w++;
				}
				break;
			default:
		}
	}
	if (w <= 0) return true;
	else return false;
}

/* validTextInput
§	validates data in text input fields
*/
function validTextInput(textObj) {
	if ((textObj.type != "text") && (textObj.type != "textarea")) return true;
	if (textObj.value == "") return false;
	if ((textObj.name == "contact:email") || (textObj.name == "joint_email")) {
		if (!isEFEmail(textObj.value)) return false;
	} else if (textObj.name.substr(0,13) == "contact:phone") {
		if (!isPhoneFax(textObj.value)) return false;
	} else if ((textObj.name == "emp_phone") || (textObj.name == "joint_emp_phone")) {
		if (!isPhoneFax(textObj.value)) return false;
	} else if ((textObj.name == "joint_home_phone") || (textObj.name == "joint_day_phone") || (textObj.name == "joint_fax") || (textObj.name == "joint_cell_phone")) {
		if (!isPhoneFax(textObj.value)) return false;
	} else if ((textObj.name == "contact:address:home:postalcode") || (textObj.name == "contact:address:work:zip") || (textObj.name == "joint_contact:address:work:zip") || (textObj.name == "joint_zip")) {
		if (!isZip(textObj.value)) return false;
	} else if ((textObj.name == "vehicle:buy:year") || (textObj.name == "vehicle:trade-in:year")) {
		if (!isNum(textObj.value)) return false;
	} else if ((textObj.name == "vehicle:buy:odometer:mi") || (textObj.name == "vehicle:trade-in:odometer:mi")) {
		if (!isAlphaNum(textObj.value)) return false;
	} else if ((textObj.name == "ssn") || (textObj.name == "joint_ssn")) {
		if (!isSSN(textObj.value)) return false;
	} else if ((textObj.name == "residence_monthly_pay") || (textObj.name == "joint_residence_monthly_pay")) {
		if (!isMoney(textObj.value)) return false;
	} else if ((textObj.name == "monthly_income") || (textObj.name == "joint_monthly_income") || (textObj.name == "other_monthly") || (textObj.name == "joint_other_monthly")) {
		if (!isMoney(textObj.value)) return false;
	} else if ((textObj.name == "down_pay") || (textObj.name == "amount_req")) {
		if (!isMoney(textObj.value)) return false;
	} else if ((textObj.name.substr(0,3) == "dob") || (textObj.name.substr(0,9) == "joint_dob")) {
		if (!isDOB(textObj)) return false;
	} else {
		if (!isAlphaNum(textObj.value)) return false;
	}
	return true;
}

/* isSSN
§	Takes a string
§	Checks to see if it is a valid Social Security Numbwer.
§	Returns true or false
*/
function isSSN(str) {
	var okChars = "-1234567890";
	for (var ssnCount=0; ssnCount<str.length; ssnCount++) {
		if (okChars.indexOf(str.charAt(ssnCount)) < 0) return false;
	}
	return true;
}

/* validates money fields
§	Takes a string
§	Checks to see if it is valid USD format currency.
§	Returns true or false
*/
function isMoney(str) {
	var okChars = "$,.kK1234567890";
	for (var mnyCount=0; mnyCount<str.length; mnyCount++) {
		if (okChars.indexOf(str.charAt(mnyCount)) < 0) return false;
	}
	return true;
}

/* validates DOB fields
§	Takes a text field object
§	Checks to see if it is a date of birth -month, day, and year
§	Returns true or false
*/
function isDOB(dobObj) {
	var dobName = dobObj.name;
	var dobVal = dobObj.value;
	if (isNaN(dobVal)) return false;
	if ((dobName == "dob_day") || (dobName == "joint_dob_day")) {
		if ((dobVal < 1) || (dobVal > 31)) return false;
	}
	if ((dobName == "dob_mon") || (dobName == "joint_dob_mon")) {
		if ((dobVal < 1) || (dobVal > 12)) return false;
	}
	if ((dobName == "dob_year") || (dobName == "joint_dob_year")) {
		if ((dobVal < 1900) || (dobVal > 2000)) return false;
	}
	return true;
}

/* isValidSelect
§	Takes a select object and checks to make sure it's valid.
§ It's capable of checking selects with a default "choose one" or nondefault first entry.
§ This is a capability needed by email forms -which does selects either way.
*/
function isValidSelect(dropdownObj) {
	for (z=0; z<dropdownObj.length; z++) {
		var currOpt = dropdownObj.options[z];
		if (currOpt.selected == true) {
			if (currOpt.value == "") return false;
		}
	}
	return true;
}

/* isValidDateSelect
§	Takes a select object and checks to make sure it's valid.
§ It's capable of checking selects with a default "choose one" or nondefault first entry.
§ This is a capability needed by email forms -which does selects either way.
*/
function isValidSelect(dropdownObj) {
	for (z=0; z<dropdownObj.length; z++) {
		var currOpt = dropdownObj.options[z];
		if (currOpt.selected == true) {
			if (currOpt.value == "") return false;
		}
	}
	return true;
}

/* isEFEmail ...special hackish version...erg
§	Takes a string
§	Checks to see if it is a valid email address (exact specs to be determined later)
§	Returns true or false
*/
function isEFEmail(str) {
	if (str.length == 0) return true;
	var badChars = " /:,!;\"'`";
	for (efi=0; efi<badChars.length; efi++) {
		var badChar = badChars.charAt(efi);
		if (str.indexOf(badChar,0) != -1) return false;
	}
	var atPos = str.indexOf("@",1);
	if (atPos == -1) return false;
	if (str.indexOf("@",atPos+1) != -1) return false;
	var periodPos = str.indexOf(".",atPos);
	if (periodPos == -1) return false;
	if (periodPos+3 > str.length)	return false;
	return true;
}

// checks privacy policy checkboxes for finance and hold forms
function onlyOne(thisCheck) {
	if (thisCheck.checked == true) {
		if ((thisCheck.name == "agree_checked") && (document.forms[0].opt_out_checked)) {
			document.forms[0].opt_out_checked.checked = false;
		}
		if (thisCheck.name == "opt_out_checked") {
			document.forms[0].agree_checked.checked = false;
		}
	}
}