// --------------------------------------------------------------------------------
// auto_Contact.js
// Travis Musika Aug 18, 2005
// Contains Javascript relevant to the AutoDirectory contact form.
// --------------------------------------------------------------------------------

var auto_contactFieldPrefix = "tmpl_autoInventoryContactForm_aC_";
var auto_contactFieldPrefix2 = "tmpl_autoCatalogueContactForm_aC_";

// cancel flag
var auto_contactCancelled = false;

// --- Attach JS to document elements and execute simple statements. ---
ow_f_AppendLoadEvent(
    function() {

		if (document.getElementById(auto_contactFieldPrefix + "auto_btnSubmit") != null) 
			ow_f_AddEvent(document.getElementById(auto_contactFieldPrefix + "auto_btnSubmit"), "click", auto_btnSubmitClicked, false);
			
		if (document.getElementById(auto_contactFieldPrefix2 + "auto_btnSubmit") != null) {
			auto_contactFieldPrefix = auto_contactFieldPrefix2;
			ow_f_AddEvent(document.getElementById(auto_contactFieldPrefix + "auto_btnSubmit"), "click", auto_btnSubmitClicked, false);
		}
    }
);

// --------------------------------------------------------------------------------
// auto_cancelForm()
// Sets a page variable flagging the click of the Clear button.
// --------------------------------------------------------------------------------
// Arguments:
//	- none
// Returns:
//	- nothing
// --------------------------------------------------------------------------------

function auto_cancelForm() {
	auto_contactCancelled = true;
}

// --------------------------------------------------------------------------------
// auto_textSubmit()
// Fires when a textfield had focus and ENTER was pressed.
// --------------------------------------------------------------------------------
// Arguments:
//	- none
// Returns:
//	- Nothing
// --------------------------------------------------------------------------------
function auto_textSubmit(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) {
		mc_imgCalculateClicked(e);
		if (e.preventDefault) e.preventDefault(); else e.returnValue = false;
	}
}

// --------------------------------------------------------------------------------
// auto_btnSubmitClicked()
// Fires when the submit button was clicked.
// --------------------------------------------------------------------------------
// Arguments:
//	- none
// Returns:
//	- Nothing
// --------------------------------------------------------------------------------
function auto_btnSubmitClicked(e) {
	if (!auto_ContactValid()) {
		//alert("Contact FORM IS INVALID.");
		if (!e) var e = window.event;
		if (e.preventDefault) e.preventDefault(); else e.returnValue = false;
	} else {
		return true;
	}
}

// --------------------------------------------------------------------------------
// mc_Valid()
// Validates the data entered in the mortgage calculator.
// --------------------------------------------------------------------------------
// Arguments:
//	- none
// Returns:
//	- the results of the validation [boolean]
// --------------------------------------------------------------------------------

function auto_ContactValid() {

   	// return if the Clear button is clicked
	if (auto_contactCancelled) return true;

	var auto_txtFirstName = document.getElementById(auto_contactFieldPrefix + "auto_txtFirstName");
	if (auto_txtFirstName != null)
		if (auto_txtFirstName.value.length == 0) {
			alert(document.getElementById(auto_contactFieldPrefix + "auto_rfvFirstName").value);
			auto_txtFirstName.focus();
			return false;
		}

	var auto_txtLastName = document.getElementById(auto_contactFieldPrefix + "auto_txtLastName");
	if (auto_txtLastName != null)
		if (auto_txtLastName.value.length == 0) {
			alert(document.getElementById(auto_contactFieldPrefix + "auto_rfvLastName").value);
			auto_txtLastName.focus();
			return false;
		}

	var auto_txtPhone = document.getElementById(auto_contactFieldPrefix + "auto_txtPhone");
	if (auto_txtPhone != null) {
		var auto_radPhone = document.getElementById(auto_contactFieldPrefix + "auto_radPhone");
		if (auto_txtPhone.value.length == 0 && auto_radPhone != null && auto_radPhone.checked) {
			alert(document.getElementById(auto_contactFieldPrefix + "auto_rfvPhone").value);
			auto_txtPhone.focus();
			return false;
		} else if (auto_txtPhone.value.length > 0) {
			var ph_re = /^[0-9]{3}\-[0-9]{3}\-[0-9]{4}$/;
			if (!auto_txtPhone.value.match(ph_re))
			{
				alert(document.getElementById(auto_contactFieldPrefix + "auto_rxvPhone").value);
				auto_txtPhone.focus();
				return false;
			}
		}
	}

	var auto_txtEmail = document.getElementById(auto_contactFieldPrefix + "auto_txtEmail");
	if (auto_txtEmail != null) {
		var auto_radEmail = document.getElementById(auto_contactFieldPrefix + "auto_radEmail");
		if (auto_txtEmail.value.length == 0 && auto_radEmail != null && auto_radEmail.checked)
		{
			alert(document.getElementById(auto_contactFieldPrefix + "auto_rfvEmail").value);
			auto_txtEmail.focus();
			return false;
		} else if (auto_txtEmail.value.length > 0) {
			var em_re = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
			if (!auto_txtEmail.value.match(em_re))
			{
				alert(document.getElementById(auto_contactFieldPrefix + "auto_rxvEmail").value);
				auto_txtEmail.focus();
				return false;
			}
		}
	}

	//google code
	//will be called only if all applicable validation is passed
	var inp = document.getElementById(auto_contactFieldPrefix + "GoogleFunnelTrackingURL");
	if (inp != null) {
	    if (inp.value != '') {
	        OneWeb.Analytics.track(inp.value);
	    }
	}

	return true;

}