// --------------------------------------------------------------------------------
// cf_Application.js
// Simon Anderson 03-Aug-2006
// Contains Javascript relevant to the Contact Forms application.
// --------------------------------------------------------------------------------

var cf_fieldPrefix = "tmpl_cfApp_"

// cancel flag
var cf_cancelled = false;

// --- Attach JS to document elements and execute simple statements. ---
ow_f_AppendLoadEvent(
function() {

	if (document.getElementById(cf_fieldPrefix + "cf_btnOK") != null) ow_f_AddEvent(document.getElementById(cf_fieldPrefix + "cf_btnOK"), "click", cf_formSubmitClicked, false);
	if (document.getElementById(cf_fieldPrefix + "cf_btnOKImage") != null) ow_f_AddEvent(document.getElementById(cf_fieldPrefix + "cf_btnOKImage"), "click", cf_formSubmitClicked, false);

	if (document.getElementById("cf_form") != null) {
		var inp = document.getElementById("cf_form").getElementsByTagName("input");
		for (var i = 0; i < inp.length; i++) {
			if (inp[i].type == "text")
				ow_f_AddEvent(inp[i], "keypress", cf_formTextSubmit, false);
				
			// add hidden input element for spambot-prevention after a short (500 ms) timeout
			setTimeout(function () {
				var cf_chkNotSpam = document.getElementById(cf_fieldPrefix + "cf_chkNotSpam");
				if (cf_chkNotSpam == null && document.createElement) {
					var cf_chkNotSpam = document.createElement("input");
					if (cf_chkNotSpam) {
						cf_chkNotSpam.setAttribute("type", "checkbox");
						cf_chkNotSpam.setAttribute("id", cf_fieldPrefix+"cf_chkNotSpam");
						cf_chkNotSpam.setAttribute("name", cf_fieldPrefix+"cf_chkNotSpam");
						cf_chkNotSpam.setAttribute("value","notspam");
						cf_chkNotSpam.style.display="none";
						document.getElementById("cf_form").appendChild(cf_chkNotSpam);
						cf_chkNotSpam.checked=true;
					}
				}
			}, 500);
			

		}
	}
}
);

// --------------------------------------------------------------------------------
// cf_formTextSubmit()
// Fires when a textfield had focus and ENTER was pressed.
// --------------------------------------------------------------------------------
// Arguments:
//	- none
// Returns:
//	- Nothing
// --------------------------------------------------------------------------------
function cf_formTextSubmit(e) {

	var code;
	if (!e) var e = window.event;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;

	if (code == 13) {
		document.getElementById(cf_fieldPrefix + "cf_btnOK").click();
		if (e.preventDefault) e.preventDefault(); else e.returnValue = false;
	}
}

// --------------------------------------------------------------------------------
// cf_formSubmitClicked()
// Fires when the submit button was clicked.
// --------------------------------------------------------------------------------
// Arguments:
//	- none
// Returns:
//	- Nothing
// --------------------------------------------------------------------------------
function cf_formSubmitClicked(e) {

	if (!cf_checkRequestData()) {
		if (!e) var e = window.event;
		if (e.preventDefault) e.preventDefault(); else e.returnValue = false;
	} else {
		return;
	}
}

// --------------------------------------------------------------------------------
// cf_checkRequestData()
// Validates the data entered on the order page.
// --------------------------------------------------------------------------------
// Arguments:
//	- none
// Returns:
//	- the results of the validation [boolean]
// --------------------------------------------------------------------------------

function cf_checkRequestData()
{
	// return if the cancel button is clicked
	if (cf_cancelled)
		return true;

	var cf_lstTitle = document.getElementById(cf_fieldPrefix + "cf_lstTitle");
	if (cf_lstTitle != null)
		if (cf_lstTitle.value == 0) {
			// see if title is required
			if (document.getElementById(cf_fieldPrefix + "lblTitleRequired") != null) {
				alert(document.getElementById(cf_fieldPrefix + "cf_rfvTitle").value);
				cf_lstTitle.focus();
				return false;
			}	
		}

	var cf_txtFirstName = document.getElementById(cf_fieldPrefix + "cf_txtFirstName");
	if (cf_txtFirstName  != null)
		if (cf_txtFirstName.value.length == 0) {
			alert(document.getElementById(cf_fieldPrefix + "cf_rfvFirstName").value);
			cf_txtFirstName.focus();
			return false;
		}

	var cf_txtLastName = document.getElementById(cf_fieldPrefix + "cf_txtLastName");
	if (cf_txtLastName != null)
		if (cf_txtLastName.value.length == 0) {
			alert(document.getElementById(cf_fieldPrefix + "cf_rfvLastName").value);
			cf_txtLastName.focus();
			return false;
		}

	var cf_txtCompany = document.getElementById(cf_fieldPrefix + "cf_txtCompany");
	if (cf_txtCompany != null)
		if (cf_txtCompany.value.length == 0) {
			// see if company is required
			if (document.getElementById(cf_fieldPrefix + "lblCompanyRequired") != null) {
				alert(document.getElementById(cf_fieldPrefix + "cf_rfvCompany").value);
				cf_txtCompany.focus();
				return false;
			}	
		}


	var em_re = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	var cf_txtEmail = document.getElementById(cf_fieldPrefix + "cf_txtEmail");
	if (cf_txtEmail != null)
		if (cf_txtEmail.value.length == 0) {
			alert(document.getElementById(cf_fieldPrefix + "cf_rfvEmail").value);
			cf_txtEmail.focus();
			return false;
		} else {
			if (!cf_txtEmail.value.match(em_re)) {
				alert(document.getElementById(cf_fieldPrefix + "cf_regvEmail").value);
				cf_txtEmail.focus();
				cf_txtEmail.select();
				return false;
			}
		}

	//determine if we need to verify re-enter email field
	var cf_txtEmailConfirmation = document.getElementById(cf_fieldPrefix + "cf_txtEmailConfirmation");
	if (cf_txtEmailConfirmation != null)
		if (cf_txtEmailConfirmation.value.length == 0) {
			alert(document.getElementById(cf_fieldPrefix + "cf_rfvEmailConfirmation").value);
			cf_txtEmailConfirmation.focus();
			return false;
		} else if (cf_txtEmailConfirmation.value != cf_txtEmail.value) {
			alert(document.getElementById(cf_fieldPrefix + "cf_cvEmailConfirmation").value);
			cf_txtEmailConfirmation.focus();
			return false;
		}
		
 	// determine if we need to check for a required phone field
	var ph_re = /^(((\(\d{3}\))|((1[\- ]?)?\d{3}))[\- ]?)?\d{3}[\- ]?\d{4}$/;
	var cf_txtPhone = document.getElementById(cf_fieldPrefix + "cf_txtPhone");
	if (cf_txtPhone != null) {
	    if (cf_txtPhone.value.length == 0) {
	        // see if phone is required
			if (document.getElementById(cf_fieldPrefix + "lblPhoneRequired") != null) {
				alert(document.getElementById(cf_fieldPrefix + "cf_rfvPhone").value);
				cf_txtPhone.focus();
				return false;
			}
	    } else {
			if (!cf_txtPhone.value.match(ph_re)) {
				alert(document.getElementById(cf_fieldPrefix + "cf_regvPhone").value);
				cf_txtPhone.focus();
				cf_txtPhone.select();
				return false;
			}
		}
    }


 	// determine if we need to check for a required address1 field
	var cf_txtAddress1 = document.getElementById(cf_fieldPrefix + "cf_txtAddress1");
	if (cf_txtAddress1 != null) {
	    if (cf_txtAddress1.value.length == 0) {
	        // see if address1 is required
			if (document.getElementById(cf_fieldPrefix + "lblAddress1Required") != null) {
				alert(document.getElementById(cf_fieldPrefix + "cf_rfvAddress1").value);
				cf_txtAddress1.focus();
				return false;
			}
	    }
    }

 	// determine if we need to check for a required city field
	var cf_txtCity = document.getElementById(cf_fieldPrefix + "cf_txtCity");
	if (cf_txtCity != null)
	    if (cf_txtCity.value.length == 0) {
	        // see if city is required
			if (document.getElementById(cf_fieldPrefix + "lblCityRequired") != null) {
				alert(document.getElementById(cf_fieldPrefix + "cf_rfvCity").value);
				cf_txtCity.focus();
				return false;
			}
	    }

 	// determine if we need to check for a required province selection
	var cf_lstProvince = document.getElementById(cf_fieldPrefix + "cf_lstProvince");
	if (cf_lstProvince != null)
		if (cf_lstProvince.value == "") {
		    // see if it's required
		    if (document.getElementById(cf_fieldPrefix + "lblProvinceRequired") != null) {
    			alert(document.getElementById(cf_fieldPrefix + "cf_rfvProvince").value);
    			cf_lstProvince.focus();
    			return false;
			}
		}

 	// determine if we need to check for a required country selection
	var cf_lstCountry = document.getElementById(cf_fieldPrefix + "cf_lstCountry");
	if (cf_lstCountry != null)
		if (cf_lstCountry.value == "") {
		    // see if it's required
		    if (document.getElementById(cf_fieldPrefix + "lblCountryRequired") != null) {
    			alert(document.getElementById(cf_fieldPrefix + "cf_rfvCountry").value);
    			cf_lstCountry.focus();
    			return false;
			}
		}

 	// determine if we need to check for a required postal code
	var cf_txtPostalCode = document.getElementById(cf_fieldPrefix + "cf_txtPostalCode");
	if (cf_txtPostalCode != null)
	    if (cf_txtPostalCode.value.length == 0) {
	        // see if postal code is required
			if (document.getElementById(cf_fieldPrefix + "lblPostalCodeRequired") != null) {
				alert(document.getElementById(cf_fieldPrefix + "cf_rfvPostalCode").value);
				cf_txtPostalCode.focus();
				return false;
			}
	    }

 	// determine if we need to check for a required web address field
    var web_re = /^http[s]?:\/\/\w+[\w\-\.]+\.[\w]{2,5}(\/[\w\-\.\/%+]*)?(\?[\w\-\.=&\/%+]*)?(#[\w\-\.\/%+]*)?$/i;
	var cf_txtURL = document.getElementById(cf_fieldPrefix + "cf_txtURL");
	if (cf_txtURL != null) {
	    if (cf_txtURL.value.length == 0) {
	        // see if URL is required
			if (document.getElementById(cf_fieldPrefix + "lblURLRequired") != null) {
				alert(document.getElementById(cf_fieldPrefix + "cf_rfvURL").value);
				cf_txtURL.focus();
				return false;
			}
	    } else {
			if (!cf_txtURL.value.match(web_re)) {
				alert(document.getElementById(cf_fieldPrefix + "cf_regvURL").value);
				cf_txtURL.focus();
				cf_txtURL.select();
				return false;
			}
		}
    }

 	// determine if we need to check for a required category
	var cf_lstCategory = document.getElementById(cf_fieldPrefix + "cf_lstCategory");
	if (cf_lstCategory != null)
		if (cf_lstCategory.value == "") {
		    // see if it's required
		    if (document.getElementById(cf_fieldPrefix + "lblCategoryRequired") != null) {
    			alert(document.getElementById(cf_fieldPrefix + "cf_rfvCategory").value);
    			cf_lstCategory.focus();
    			return false;
			}
		}

 	// determine if we need to check for a required comment field
	var cf_txtComment = document.getElementById(cf_fieldPrefix + "cf_txtComment");
	if (cf_txtComment != null) {
	    if (cf_txtComment.value.length == 0) {
	        // see if comment is required
			if (document.getElementById(cf_fieldPrefix + "lblCommentRequired") != null) {
				alert(document.getElementById(cf_fieldPrefix + "cf_rfvComment").value);
				cf_txtComment.focus();
				return false;
			}
	    }
    }
	
	var ow_txtCustomField = null;
	for (var i = 1; i <= 10; i++) {
		ow_txtCustomField = document.getElementById(cf_fieldPrefix + "cf_txtCustomField"+i);
		if (ow_txtCustomField != null) {
			if (ow_txtCustomField.value.length == 0) {
				// see if the custom field is required
				if (document.getElementById(cf_fieldPrefix + "lblCustomField"+i+"Required") != null) {
					alert(document.getElementById(cf_fieldPrefix + "cf_rfvCustomField"+i).value);
					ow_txtCustomField.focus();
					return false;
				}
			}
		}
	}

	return true;

}	
