// Form Validation Script

// Required fields name, phone number
// Phone number must be valid format
// El Centro office visits must verify that they agree to $50 charge

// Ensures SD office is always checked at page refresh
function onLoad()
{
	document.getElementById("sdOffice").checked = true;
}

// Displays checkbox field for $50 El Centro Charge
function onECChecked()
{
	document.getElementById("ecCharge").innerHTML = '<table><tr><td>I understand that there is a $50 charge for all consultations performed in the El Centro office and that this payment will be due at the time of my consultation appointment.  I agree to this charge.</td></tr><tr><td colspan="2" style="text-align: center"><input type="checkbox" id="ecChargeOk" value="I agree" /><label for="ecChargeOK">I agree to this charge.</label></td></tr></table>';
}

// Removes checkbox field for $50 El Centro Charge
function onSDChecked()
{
	document.getElementById("ecCharge").innerHTML = "";
}

// Checks to ensure that the EC Charge OK checkbox is checked, Form does not validate if not
// @Location id of the field that asks user to pick the office locations of their consultation 
// @ecCharge id of the field that asks user to confirm they are ok with the charge
// @ecChargeDisplay id of the field that displays the ecCharge message

function isECCChargeOk()
{
	if (document.getElementById("ecOffice").checked && 
		!(document.getElementById("ecChargeOk").checked))
	{
		document.getElementById("ecCharge").innerHTML = '<table><tr><td>I understand that there is a $50 charge for all consultations performed in the El Centro office and that this payment will be due at the time of my consultation appointment.  I agree to this charge.</td></tr><tr><td colspan="2" style="text-align: center"><input type="checkbox" id="ecChargeOk" value="I agree" /><label for="ecChargeOK">I agree to this charge.</label></td></tr></table>	<p class="error">We&apos;re sorry.  We cannot schedule an El Centro Consultation via the internet without your confirmation that you agree to the consultation fee.  Please select "yes," select San Diego as the office for your consultation, or call the El Centro office at (760) 352-2800 to schedule your consultation.</p>'
		document.getElementById("ecChargeOK").focus();
		return false;	
	}
	
	document.getElementById("ecCharge").innerHTML = '<table><tr><td>I understand that there is a $50 charge for all consultations performed in the El Centro office and that this payment will be due at the time of my consultation appointment.  I agree to this charge.</td></tr><tr><td colspan="2" style="text-align: center"><input type="checkbox" id="ecChargeOk" value="I agree" checked="checked"/><label for="ecChargeOK">I agree to this charge.</label></td></tr></table>';
	return true;	
}

// Checks to see whether the given field was left blank
// @param fieldID the id of the field to be checked
// @param fieldName the name of the field that the user sees
// @param errorFieldID the id of the field where the error message will be written
// @return false will prevent the form from validating
function isFieldBlank(fieldID, fieldName, errorFieldID)
{
	if (fieldID == null)
	{
		return false;
	}

	var whiteSpace = /\s+/;
	var twowhiteSpace = /\w+\s?\w+/  // letters whitespace letters
	
	if (fieldID.match("clientName"))
	{
		if (!document.getElementById(fieldID).value.match(twowhiteSpace))
		{
			document.getElementById(errorFieldID).innerHTML += "Please fill out the " + fieldName + " field. <br />";
			document.getElementById(fieldID).focus();
			return false;
		} 
	}
	else if (document.getElementById(fieldID).value.match(whiteSpace))
	{
		document.getElementById(errorFieldID).innerHTML += "Please fill out the " + fieldName + " field. <br />";
		document.getElementById(fieldID).focus();
		return false;
	}
	
	return true;
}

// Checks whether the phone number was submitted in a valid format
// @param areaCodeID ID of the area code of the phone number field to be checked
// @param phonePrefixID ID of the phone prefix field to be checked
// @param phoneSuffixID ID of the phone suffix field to be checked
// @param errorFieldID the id of the field where the error message will be written
// @return false will prevent the form from validating
function isPhoneValid(areaCodeID, phonePrefixID, phoneSuffixID, errorFieldID)
{
	
	if (areaCodeID == null || phonePrefixID == null || phoneSuffixID == null)
	{
		return false;
	}
	
	// set up valid regular expression forms to check against
	var phoneFormat1 = /\d{3}/; // 3 digits
	var phoneFormat2 = /\d{4}/; // 4 digits

	var fakeAreaCode = "555";
	
	if (!document.getElementById(areaCodeID).value.match(phoneFormat1) || 
		document.getElementById(areaCodeID).value.match(fakeAreaCode) ||
		!document.getElementById(phonePrefixID).value.match(phoneFormat1) ||
		!document.getElementById(phoneSuffixID).value.match(phoneFormat2))
	{
		document.getElementById(errorFieldID).innerHTML += "Please submit a valid phone number, including area code";
		document.getElementById(areaCodeID).focus();
		return false;
	}
	
	return true;
}

// checks to see whether the form validates, and submits it if so
// @param formID the id of the form to be checked
// @param submit the submit information

function onSubmitValidate()
{	
	// clears any name/phone error messages from previous submission
	document.getElementById("clientNamePhoneError").innerHTML = "";
	
	// sets variable for checking of form, if at any time returns false, form will not validate
	var success = true;
	
	if (document.getElementById("ecOffice").checked)
	{
		success = isECCChargeOk() && success;
	}
	
	// checks if name & phone are blank, holds all phone fields in their own variables
	success = isFieldBlank("clientName", "Name", "clientNamePhoneError") && success;
	var areaSuccess = isFieldBlank("clientAreaCode", "Phone Area Code", "clientNamePhoneError");
	success = areaSuccess && success;
	var prefixSuccess = isFieldBlank("clientPhonePrefix", "Phone Prefix", "clientNamePhoneError");
	success = prefixSuccess && success;
	var suffixSuccess = isFieldBlank("clientPhoneSuffix", "Phone Suffix", "clientNamePhoneError");
	success = suffixSuccess && success;
	
	// if all phone fields are filled out, checks for valid phone format
	if (areaSuccess && prefixSuccess && suffixSuccess)
	{
		success = isPhoneValid("clientAreaCode", "clientPhonePrefix", "clientPhoneSuffix", "clientNamePhoneError") && success;
	}
	
	if (success)
	{
		document.forms.consultationRequest.submit();
	}

}