function showHideExtra() {
	if (document.getElementById('application_form').source.selectedIndex == 1) {
		//document.getElementById('mail_extra').style.display = 'inline';
		document.getElementById('application_form_ref_popout').style.display = 'block';
	} else {
		//document.getElementById('mail_extra').style.display = 'none';
		document.getElementById('application_form_ref_popout').style.display = 'none';
	}
}
function IsNumericOnly(strString) {
	//  check for valid numeric strings
	var strValidChars = "0123456789";
	var strChar;
	var blnResult = true;
	
	if (strString.length == 0) return false; // not numeric, there is nothing here
	
	//  test strString consists of valid characters listed above
	for (i = 0; i < strString.length && blnResult == true; i++) {
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1) {
			blnResult = false;
		}
	}
	return blnResult;
}
function IsNumeric(strString) {
	//  check for valid numeric strings
	var strValidChars = "0123456789.-";
	var strChar;
	var blnResult = true;
	
	if (strString.length == 0) return false; // not numeric, there is nothing here
	
	//  test strString consists of valid characters listed above
	for (i = 0; i < strString.length && blnResult == true; i++) {
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1) {
			blnResult = false;
		}
	}
	return blnResult;
}
function IsNumericPhone(strString) {
	//  check for valid numeric strings with certain exceptions
	var strValidChars = "(0123456789.- extEXT+)/\\";
	var strChar;
	var blnResult = true;
	
	if (strString.length == 0) return false; // not numeric, there is nothing here
	
	//  test strString consists of valid characters listed above
	for (i = 0; i < strString.length && blnResult == true; i++) {
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1) {
			blnResult = false;
		}
	}
	return blnResult;
}


function validate() {
	var strMessage = '';
	//var f = document.forms['theForm'];
	var f = document.getElementById('application_form');
	// remove commas and dollar signs from the HV, mortgage and mortgage 2 fields
	regExp = /\$/gi;
	/*f.EstHomeValue.value = f.EstHomeValue.value.replace(regExp, '');
	f.EstMortgageBalance.value = f.EstMortgageBalance.value.replace(regExp, '');
	f.EstMortgageBalance2.value = f.EstMortgageBalance2.value.replace(regExp, '');
	*/
	regExp = /,/gi;
	//f.EstHomeValue.value = f.EstHomeValue.value.replace(regExp, '');
	//f.EstMortgageBalance.value = f.EstMortgageBalance.value.replace(regExp, '');
	//f.EstMortgageBalance2.value = f.EstMortgageBalance2.value.replace(regExp, '');
	f.Direct_Mail_Reference_ID.value = f.Direct_Mail_Reference_ID.value.replace(regExp, '');
	
	if (f.primary_first.value.length < 1) {
		strMessage = 'Applicant First Name is a required field. Please enter the applicant\'s first name.';
		f.primary_first.focus();
	} else if (f.primary_last.value.length < 1) {
		strMessage = 'Applicant Last Name is a required field. Please enter the applicant\'s last name.';
		f.primary_last.focus();
	} else if ((f.Phone_areacode.value.length < 1) || (f.Phone_first3.value.length < 1) || (f.Phone_last4.value.length < 1)) {
		strMessage = 'Home Phone is a required field. Please enter the applicant\'s home phone number.';
		f.Phone_areacode.focus();
	} else if (!IsNumericPhone(f.Phone_areacode.value)) {
		strMessage = 'Home Phone Area Code must be numeric. Please enter the applicant\'s home phone number area code using numbers only.';
		f.Phone_areacode.focus();
	} else if (!IsNumericPhone(f.Phone_first3.value)) {
		strMessage = 'Home Phone must be numeric. Please enter the applicant\'s home phone number using numbers only.';
		f.Phone_first3.focus();
	} else if (!IsNumericPhone(f.Phone_last4.value)) {
		strMessage = 'Home Phone must be numeric. Please enter the applicant\'s home phone number using numbers only.';
		f.Phone_last4.focus();
	} else if ((f.primary_dob_m.selectedIndex == 0 ) || (f.primary_dob_d.selectedIndex == 0) || (f.primary_dob_y.selectedIndex == 0)) {
		strMessage = 'Applicant Date of Birth is a required field. Please enter the applicant\'s date of birth.';
		f.primary_dob_m.focus();
	} else if (f.isCoApp.selectedIndex == 0) {
		strMessage = "Please select an answer to the \'Is there a co-applicant?\' question.";
		f.isCoApp.focus();
	} else if ((f.isCoApp.selectedIndex == 2) && (f.secondary_first.value.length < 1)) {
		strMessage = 'Co-Applicant First Name is a required field. Please enter the co-applicant\'s first name.';
		f.secondary_first.focus();
	} else if ((f.isCoApp.selectedIndex == 2) && (f.secondary_last.value.length < 1)) {
		strMessage = 'Co-Applicant Last Name is a required field. Please enter the co-applicant\'s last name.';
		f.secondary_last.focus();
	} else if (f.Street_Address.value.length < 1) {
		strMessage = 'Street Address is a required field. Please enter the applicant\'s street address.';
		f.Street_Address.focus();
	} else if (f.City.value.length < 1) {
		strMessage = 'City is a required field. Please enter the applicant\'s city.';
		f.City.focus();
	} else if (f.State.selectedIndex == 0) {
		strMessage = 'State is a required field. Please enter the applicant\'s state.';
		f.State.focus();
	} else if (f.Zip.value.length < 1) {
		strMessage = 'Zip Code is a required field. Please enter the applicant\'s zip code.';
		f.Zip.focus();
	} else if (!IsNumeric(f.Zip.value)) {
		strMessage = 'Zip Code must be numeric. Please enter the applicant\'s zip code using numbers only.';
		f.Zip.focus();
	} else if (f.hometype.selectedIndex == 0) {
		strMessage = 'Home Type is a required field. Please select the type of home.';
		f.hometype.focus();
	} else if (f.EstHomeValue.selectedIndex == 0) {
		strMessage = 'Estimated Home Value is a required field. Please enter the applicant\'s estimated home value.';
		f.EstHomeValue.focus();
	/*} else if (!IsNumeric(f.EstHomeValue.value)) {
		strMessage = 'Estimated Home Value must be numeric. Please enter the applicant\'s estimated home value using numbers only.';
		f.EstHomeValue.focus();
	} else if ((f.EstMortgageBalance.value.length > 0) && (!IsNumeric(f.EstMortgageBalance.value))) {
		strMessage = 'Estimated Mortgage Balance must be numeric. Please enter the estimated mortgage balance using numbers only.';
		f.EstMortgageBalance.focus();
	} else if ((f.EstMortgageBalance2.value.length > 0) && (!IsNumeric(f.EstMortgageBalance2.value))) {
		strMessage = 'Estimated Second Mortgage Balance must be numeric. Please enter the estimated second mortgage balance using numbers only.';
		f.EstMortgageBalance2.focus();*/
	} else if (f.isPrimary.selectedIndex == 0) {
		strMessage = "Please select an answer to the \'Is the homeowner also the primary contact?\' question.";
		f.isPrimary.focus();
	}  
	if ((strMessage == '') && (f.isCoApp.selectedIndex == 2)) {
		// co-applicant entered -- check birthday
		if ((f.secondary_dob_m.selectedIndex == 0) || (f.secondary_dob_d.selectedIndex == 0) || (f.secondary_dob_y.selectedIndex == 0)) {
			strMessage = 'Co-Applicant Date of Birth is required.  Please enter the co-applicant\'s date of birth.';
			f.secondary_dob_m.focus();
		}
	}
	
	if ((strMessage == '') && (f.isPrimary.selectedIndex == 2)) {
		//If collecting alternate contact info then these are required fields
		if (f.contact_first.value < 1) { 
			strMessage = 'Please enter the name of the primary contact.';
			f.contact_first.focus();
		} else if (f.contact_last.value < 1) {
			strMessage = 'Please enter the last name of the primary contact.';
			f.contact_last.focus();
		} else if (f.contact_relationship.selectedIndex == 0) {
			strMessage = 'Please select the relationship the primary contact has to the applicant.';
			f.contact_relationship.focus();
		} else if(f.pc_phone_areacode.value.length < 3) {
			strMessage = 'Please enter the phone number for the primary contact.';
			f.pc_phone_areacode.focus();
		} else if(f.pc_phone_first3.value.length < 3) {
			strMessage = 'Please enter the phone number for the primary contact.';
			f.pc_phone_first3.focus();
		} else if(f.pc_phone_last4.value.length < 3) {
			strMessage = 'Please enter the phone number for the primary contact.';
			f.pc_phone_last4.focus();
		} else if (!IsNumericPhone(f.pc_phone_areacode.value)) {
			strMessage = 'The primary contact\'s phone number area code must be numeric. Please enter the phone number area code for the primary contact using numbers only.';
			f.pc_phone_areacode.focus();
		} else if (!IsNumericPhone(f.pc_phone_first3.value)) {
			strMessage = 'The primary contact\'s phone number must be numeric. Please enter the phone number for the primary contact using numbers only.';
			f.pc_phone_first3.focus();
		} else if (!IsNumericPhone(f.pc_phone_last4.value)) {
			strMessage = 'The primary contact\'s phone number must be numeric. Please enter the phone number for the primary contact using numbers only.';
			f.pc_phone_last4.focus();
		}
	}
	
	if (strMessage == '') {
		if (f.source.selectedIndex == 0) {
			strMessage = 'Please select how you first heard about Live Well Financial.';
			f.source.focus();
		} else if ((f.source.selectedIndex == 1) && (f.Direct_Mail_Reference_ID.value.length < 1)) {
			strMessage = 'Direct Mail Reference ID is a required field. Please enter your direct mail reference ID';
			f.Direct_Mail_Reference_ID.focus();
		} else if ((f.source.selectedIndex == 1) && (!IsNumericOnly(f.Direct_Mail_Reference_ID.value))) {
			strMessage = 'Please enter your direct mail reference ID using numbers only.';
			f.Direct_Mail_Reference_ID.focus();
		} else if ((f.source.selectedIndex == 1) && ((parseInt(f.Direct_Mail_Reference_ID.value) < 1000000) || (parseInt(f.Direct_Mail_Reference_ID.value) > 8999999))) {
			strMessage = 'Invalid direct mail reference ID.  Please re-enter your direct mail reference ID using numbers only.';
			f.Direct_Mail_Reference_ID.focus();
		}
	}
	
	if (strMessage == '') {
		return true;
	} else {
		//	This will post the errors of the form to a php file when then emails it to lwf.
		$.post("util/send_errors.php", { error: strMessage } );
		//	Alert the user
		alert(strMessage);
		//	Kill the form send
		return false;
	}
}
function toggleCoApp(caller, target) {
	ca = document.getElementById(caller);
	caInfo = document.getElementById(target);
	
	if(ca.value == "yes") {
		caInfo.style.display = "block";
	}
	else {
		caInfo.style.display = "none";
	}
}
function togglePrimary() {
	isPri = document.getElementById("isPrimary");
	pri = document.getElementById("application_form_primary_popout");
	
	if(isPri.value == "no") {
		pri.style.display = "block";
	}
	else {
		pri.style.display = "none";
	}
}