// Set Eloqua variables
var elqPPS = "70";

// Initialise the form
function initForm() {
	if ("string" == typeof formName) {
		if (document.forms[formName]) {
			// Set the  Eloqua UID field using GetElqCustomerGUID() if present
			if (document.forms[formName].elements["elqCustomerGUID"] && this.GetElqCustomerGUID) {
				document.forms[formName].elements["elqCustomerGUID"].value = GetElqCustomerGUID();
			}
		}
	}
}

// Checks that the form with the given ID validates correctly
// The user is notified of any errors via the alert() function
// Returns true if the form validates or false if not
function checkForm(formId) {
	var wf = document.getElementById(formId);
	if (wf == null)
	{
		return false;
	}
	for (var i=0;i<wf.elements.length;i++)
	{
		var result = checkFormElement(wf.elements[i]);
		if (result != null && result != "") {
			alert(result);
			return false;
		}
	}
	return true;
}

// Checks that a given form field meets the validation requirements
// Returns an error string if validation fails or an empty string otherwise
function checkFormElement(element) {
	if (isElementRequired(element)) {
		// Check for a custom function defined for this field
		if ("function" == typeof eval("window.checkFormElement_" + element.name)) {
			return eval("window.checkFormElement_" + element.name + "(element)");
		} else {
			return checkElementHasValue(element);
		}
	} else {
		return "";
	}
}

// Working example of a custom form validation function
// Algorithm based on example at http://www.w3schools.com/js/js_form_validation.asp
// Checks that a the value of a field is a valid email address
// Returns an error string if validation fails or an empty string otherwise
function checkFormElement_email(element) {
	if (elementHasValue(element)) {
		with (strTrim(element.value)) {
			apos = indexOf("@");
			dotpos = lastIndexOf(".");
			if (apos<1||dotpos-apos<2) {
		  		return "Please enter a valid e-mail address";
		  	} else {
		  		return "";
		  	}
		}
	} else {
		return "'" + getElementReference(element.id) + "' is a required field";
	}
}

// Checks whether the given form element is a required field or not
function isElementRequired(element) {
	return element.className == "req";
}

// Returns an error message if the form element has no value, or an empty string otherwise
function checkElementHasValue(element) {
	if (elementHasValue(element)) {
		return "";
	} else {
		return ("'" + getElementReference(element.id) + "' is a required field");
	}
}

// Returns a boolean value indicating whether or not the specified form element has a non-empty value
// The value is tested after trimming all white space off the start and end of the string
function elementHasValue(element) {
	if (element.type == "checkbox")
	{
		return element.checked;
	}
	else if (element.type == "select-one" || element.type == "select-multi") {
		var selectedValue = element.options[element.selectedIndex].value;
		return selectedValue != null && selectedValue != "";
	}
	return element.value != null && strTrim(element.value) != "";
}

function getElementReference(elementId) {
	var label = getElementLabel(elementId);
	var ref = elementId;
	if (label != null && label != "")
	{
		if (label.indexOf(":") != -1) {
			label = label.substring(0, label.indexOf(":"));
		}
		if (label.indexOf("<") != -1) {
			label = label.substring(0, label.indexOf("<"));
		}
		ref = label;
	}
	return strTrim(ref);
}

function getElementLabel(elementId) {
	var element = document.getElementById(elementId + '_label');
	if (element != null)
	{
		return element.innerHTML;
	}
	return null;
}

function strTrim(sString) {
	if (sString.length > 0)
	{
		while (sString.substring(0,1) == ' ')
		{
			sString = sString.substring(1, sString.length);
		}
		while (sString.substring(sString.length-1, sString.length) == ' ')
		{
			sString = sString.substring(0,sString.length-1);
		}
	}
	return sString;
}

