
function sendCV() {
 oForm = document.submitCV;
 oForm2 = document.submitCVCV;
 
 if (oForm.name.value == "") {
 	alert("Please enter your name")
	oForm.name.focus();
 } else if (oForm.email.value == "" || !validateEmail(oForm.email.value)) {
 	alert("Please enter a valid email address")
	oForm.email.focus();
 } else if (oForm.division.value == "") {
 	alert("Please select the division you are intersted in")
	oForm.division.focus();
 } else if (oForm2.file.value == "") {
 	alert("Please press browse and select your C.V.")
	oForm2.file.focus();
 } else if (oForm.h_formValidated.value != "true") {
 	alert("To prevent SPAM we require you to manually enter your name")
	oForm.name.focus();
 } else {
 		oForm2.action += "?h_formValidated=" + oForm.h_formValidated.value +
			"&name=" + oForm.name.value +
			"&email=" + oForm.email.value +
			"&telephone=" + oForm.telephone.value +
			"&division=" + oForm.division[oForm.division.selectedIndex].value;
		
		$("#loadingAni").show().next("form").css("opacity","0.35");
		// return false;

		oForm2.submit();
 }
}

function verifyAppForm() {
 oForm = document.appForm;
 oForm2 = document.appFormCV;
 
 if (oForm.name.value == "") {
 	alert("Please enter your name")
	oForm.name.focus();
 } else if (oForm.email.value == "" || !validateEmail(oForm.email.value)) {
 	alert("Please enter a valid email address")
	oForm.email.focus();
 } else if (oForm2.file.value == "") {
 	alert("Please press browse and select your C.V.")
	oForm2.file.focus();
 } else if (oForm.h_formValidated.value != "true") {
 	alert("To prevent SPAM we require you to manually enter your name")
	oForm.name.focus();
 } else {
 		oForm2.action += "?h_formValidated=" + oForm.h_formValidated.value +
			"&contact=" + oForm.contact.value +
			"&vacancyTitle=" + oForm.vacancyTitle.value +
			"&vacancyRef=" + oForm.vacancyRef.value +
			"&name=" + oForm.name.value +
			"&email=" + oForm.email.value +
			"&telephone=" + oForm.telephone.value +
			"&coverNote=" + oForm.coverNote.value
		
		$("#loadingAni").show().next("form").css("opacity","0.35");
		// return false;

		oForm2.submit();
 }
}


function register() {
	oForm = document.regForm;
	oForm2 = document.regFormCV;

	types = "";
	locations = "";

	for (var i=0; i<oForm.elements.length; i++) {
	//Loop through the form elements to get all the selected types + locations
		if ((oForm.elements[i].name == "types" || oForm.elements[i].name == "typeId") && oForm.elements[i].value != '') {
			types	+=  ", " + oForm.elements[i].value
		}
		if ((oForm.elements[i].name == "locations" || oForm.elements[i].name == "locationId") && oForm.elements[i].value != '') {
			locations	+=  ", " + oForm.elements[i].value
		}
	}
	types = Right(types,(types.length - 2));
	locations = Right(locations,(locations.length - 2));

	if (oForm.fname.value == "") {
		alert("You must enter your forename(s).");
		oForm.fname.focus();
	} else if (oForm.sname.value == "") {
		alert("You must enter your surname.");
		oForm.sname.focus();
	} else if (!validateEmail(oForm.email.value)) {
		alert("You must enter a valid email address");
		oForm.email.focus();
	} else if (oForm.emailAlerts.checked && oForm.salaryId.value == "") {
		alert("Please select a salary band are interested in");
		oForm.salaryId.focus();	
	} else if (oForm.emailAlerts.checked && typeof(oForm.types) == "undefined") {
		alert("Please select a job type you are interested in");
		oForm.typeId.focus();	
	} else if (oForm.emailAlerts.checked && typeof(oForm.locations) == "undefined") {
		alert("Please select a job location you are interested in");
		oForm.locationId.focus();	
	} else if (oForm.password.value == "") {
		alert("Please enter a password.");
		oForm.password.focus();
	} else if (oForm.password.value != oForm.password2.value) {
		alert("Your Passwords do not match, please re-type them.");
		oForm.password.focus();
	} else {

		oForm2.action += "?h_formValidated=" + oForm.h_formValidated.value +
			"&fname=" + oForm.fname.value +
			"&sname=" + oForm.sname.value +
			"&Address=" + oForm.Address.value +
			"&postcode=" + oForm.postcode.value +
			"&tel=" + oForm.tel.value +
			"&email=" + oForm.email.value +
			"&emailAlerts=" + oForm.emailAlerts.value +
			"&types=" +  types +
			"&locations=" +  locations + 
			"&salaryId=" + oForm.salaryId.value +
			"&password=" + oForm.password.value

		$("#loadingAni").show().next("form").css("opacity","0.35");
		// return false;

		oForm2.submit();
	}
}


function validateEmail(email_address) {
	if (/\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email_address)){
		return true;
	}
	return false;
}

function Left(str, n) {
	if (n <= 0) {
		return "";
	} else if (n > String(str).length) {
		return str;
	} else {
		return String(str).substring(0,n);
	}
}

function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}
