// NARZ validation | 25/07/2005
// In-use as data is posted via SalesForce.
// AP.
//
// Jon Crawley 21/06/06
// Revised this so that if validation.js is included on a given page and setFormOnSubmit() is called from the given page's body onload 
// it will look for the first form on the page and set the onSubmit to return the validateForm function
// validateForm contains every unique form question across the narz site as the form element name should always be the same e.g. first name is always "first_name" etc
// validateForm will validate the form on the page and therefore no separate form validation need be implemented
//
<!--
	// check required fields are not empty
	function isNotEmpty(element, fieldname) {
	    elem = document.forms[0].elements[element];
	    if (elem != null) {
	        var error = "";
	        var str = elem.value;
	        var re = /.+/;
	        if(!str.match(re)) {
		        error = "* " + fieldname + ".\n";
	        }
	        else {
		        error = "";
	        }
	        return error;
	    }
    }

    function isNumeric(element, fieldname) {
	    //  check for valid numeric strings
	    elem = document.forms[0].elements[element];
	    var error = "";
	    var str = elem.value;
	    var strValidChars = "0123456789.-";
	    var strChar;
	    if (str.length != 0) {
	        //  test str consists of valid characters listed above
	        for (i = 0; i < str.length; i++) {
                strChar = str.charAt(i);
		        if (strValidChars.indexOf(strChar) == -1) {
    		        error = "* " + fieldname + ".\n";
		        }
		        else {
    		        error = "";
		        }
	        }
	    }
	    return error;
	}

	// validate email address
	function isEmailAddr(element, fieldname) {
	    elem = document.forms[0].elements[element];
		var error = "";
		var str = elem.value;
		var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
		if (str == "") {
			error = "* Email address is required.\n";
			return error;
		}
		if (!str.match(re)) {
			error = "* Email address is not valid.\n";
		}
		else {
			error = "";
		}
		return error;
	}

	function isUndefined(a) {
		return typeof a == 'undefined';
	}

	//get the referrer and populate the hidden referrer field on the form
	function getReferrer() {
		var strReferrer = '';
		var strQS = String(window.location);
		var s = strQS.split("referrer=");
		strReferrer = s[1];
		if (isUndefined(strReferrer)==false) {
			document.frmMain.lead_source.value = strReferrer;
		}
	}

    //find the form on the page and set its onSubmit property to validateForm;
    function setFormOnSubmit() {       
        document.forms[0].onsubmit = validateForm;
        /*document.forms[0].onsubmit = function() {
            return validateForm();
        }*/
    }

    function elemExists(elem) {
        if (document.forms[0].elements[elem] == null) {
            return false;
        }
        return true;
    }

	function validateForm() {
        var form = document.forms[0];
		var strReason = "";
		if (elemExists('first_name')) strReason += isNotEmpty('first_name', "Please enter your first name");
		if (elemExists('last_name')) strReason += isNotEmpty('last_name', "Please enter your surname");
		if (elemExists('company')) strReason += isNotEmpty('company', "Please enter your company name");
		if (elemExists('title')) strReason += isNotEmpty('title', "Please enter your job title");
		if (elemExists('email')) strReason += isEmailAddr('email', "Please enter a valid email address");
		if (elemExists('phone')) strReason += isNotEmpty('phone', "Please enter your telephone number");
		if (elemExists('00N20000000gzYK')) strReason += isNotEmpty('00N20000000gzYK', "Please select your industry sector");
		//Do you recruit internationally? is a radio button and is set to No by default. Since it can't be deselected there is no need to validate.
		if (elemExists('00N20000000h02H')) strReason += isNotEmpty('00N20000000h02H', "Please select your recruiter type");
		if (elemExists('00N20000000lbVr')) strReason += isNotEmpty('00N20000000lbVr', "Please enter the number of offices");
		if (elemExists('00N20000000lbVr')) strReason += isNumeric('00N20000000lbVr', "Please enter the number of offices as numerical values only");
		if (elemExists('employees')) strReason += isNotEmpty('employees', "Please enter the number of employees");
        if (elemExists('employees')) strReason += isNumeric('employees', "Please enter the number of employees as numerical values only");
		if (elemExists('00N20000000l6Dm')) strReason += isNotEmpty('00N20000000l6Dm', "Please select your nearest office");
		if (elemExists('00N20000000ljgJ')) strReason += isNotEmpty('00N20000000ljgJ', "Please select where you heard about totaljobs.com");
		if (elemExists('00N20000000l5Vu')) strReason += isNotEmpty('00N20000000l5Vu', "Please select your industry report type");
		if (elemExists('message')) strReason += isNotEmpty('message', "Please enter your message");
		if (elemExists('00N20000000ktmj')) strReason += isNotEmpty('00N20000000ktmj', "Please enter your reason for contacting us");
        
		if (strReason != "") {
			alert("We were unable to process your details\nbecause of the following:\n\n" + strReason);
		    return false;
		}
		
		// ContactUs.aspx and TheNetwork.aspx require the message form entry to populate the hidden description field before posting
		// For this to work those forms that require this- their form names must be put in the below string
        var str = "frmContactUs, frmTheNetwork";
        if (str.indexOf(form.name) != -1) {
            form.description.value = form.message.value;
        }
        
	    return true;
	}
//-->