﻿//function to validate clarendon contact form
function validateContact(aForm){
	// required fields:name,address,postcode,phone,email,services,
	// text
	if (aForm.name.value == ""){
	 alert("Please tell us your name.");
	 aForm.name.focus();
	 return false;
	}
	
	if (aForm.address.value == ""){
	 alert("Please tell us your address.");
	 aForm.address.focus();
	 return false;
	}
	
	if (aForm.postcode.value == ""){
	 alert("Please tell us your post code.");
	 aForm.postcode.focus();
	 return false;
	}
	
	if (aForm.phone.value == ""){
	 alert("Please tell us your telephone number.");
	 aForm.phone.focus();
	 return false;
	}
	
	if (!emailCheck(aForm.email.value)){
	 aForm.email.focus();
	 return false;
	}
	
	if (aForm.services.value == "0"){
	 alert("Please select a service from the list.");
	 aForm.services.focus();
	 return false;
	}
	
	return true;
}

//function to validate clarendon job application form
function validateJobForm(aForm){
	if (aForm.title.value == "0"){
	 alert("Please select a title from the list.");
	 aForm.title.focus();
	 return false;
	}
	
	if (aForm.forename.value == ""){
	 alert("Please tell us your first name.");
	 aForm.forename.focus();
	 return false;
	}
	
	if (aForm.surname.value == ""){
	 alert("Please tell us your last name.");
	 aForm.surname.focus();
	 return false;
	}
	
	if (aForm.phone.value == ""){
	 alert("Please tell us your telephone number.");
	 aForm.phone.focus();
	 return false;
	}
	
	/*if (aForm.ni_no.value == ""){
	 alert("Please tell us your National Insurance number.");
	 aForm.ni_no.focus();
	 return false;
	}*/
	
	if (aForm.mobile.value == ""){
	 alert("Please tell us your mobile telephone number.");
	 aForm.mobile.focus();
	 return false;
	}
	
	if (!emailCheck(aForm.email.value)){
	 aForm.email.focus();
	 return false;
	}
	
	if (aForm.cur_address.value == ""){
	 alert("Please tell us your current address.");
	 aForm.cur_address.focus();
	 return false;
	}
	
	if (aForm.cur_postcode.value == ""){
	 alert("Please tell us your current postcode.");
	 aForm.cur_postcode.focus();
	 return false;
	}
	
	if (aForm.cur_country.value == ""){
	 alert("Please tell us your current country.");
	 aForm.cur_country.focus();
	 return false;
	}
	
	/*if (aForm.dob.value == ""){
	 alert("Please tell us your date of birth.");
	 aForm.dob.focus();
	 return false;
	}*/
	
	/*if (validateButton(aForm.uk_born) == false){
	 alert("Please tell us if you were born in the UK.");
	 aForm.uk_born[0].scrollIntoView();
	 return false;
	} else {
	 if (getButtonValue(aForm.uk_born) == 'no'){
        if (aForm.birth_town.value == ""){
         alert("Please tell us your birth town.");
         aForm.birth_town.focus();
         return false;
        }
        if (aForm.birth_country.value == ""){
         alert("Please tell us your birth country.");
         aForm.birth_country.focus();
         return false;
        }
	 }
	}*/
	
	/*if (aForm.em_contact_name1.value == ""){
	 alert("Please tell us the name of your 1st emergency contact.");
	 aForm.em_contact_name1.focus();
	 return false;
	}*/
	
	/*if (aForm.em_contact_phone1.value == ""){
	 alert("Please tell us the telephone number of your 1st emergency contact.");
	 aForm.em_contact_phone1.focus();
	 return false;
	}*/
	
	/*if (aForm.em_contact_address1.value == ""){
	 alert("Please tell us the address of your 1st emergency contact.");
	 aForm.em_contact_address1.focus();
	 return false;
	}*/
	
	/*if (aForm.em_contact_rel1.value == ""){
	 alert("Please tell us your relationship to your 1st emergency contact.");
	 aForm.em_contact_rel1.focus();
	 return false;
	}*/
	
	if (aForm.start.value == ""){
	 alert("Please tell us when you could start work.");
	 aForm.start.focus();
	 return false;
	}
	
	if (validateButton(aForm.we_avail) == false){
	 alert("Please tell us if you can work weekends.");
	 aForm.we_avail[0].scrollIntoView();
	 return false;
	}
	
	if (aForm.hear.value == ""){
	 alert("Please tell us where you heard about us.");
	 /*if (aForm.hearselect.options[aForm.hearselect.selectedIndex].value == 'Other') {
	    aForm.hear.focus();
	 } else {
	    aForm.hearselect.focus();
	 }*/
	 return false;
	}
	
	if ((!aForm.fulltime.checked) && (!aForm.parttime.checked) && (!aForm.weekends.checked) && (!aForm.livein.checked) && (!aForm.evenings.checked)){
	 alert("Please tell us if you are applying for full-time, part-time, weekend, live-in or evening work (select at least one).");
	 aForm.fulltime.focus();
	 return false;
	}
	
	if (!atLeastOne(aForm.elements['school[]'])) {
	 alert("Please list at least one school.");
	 //aForm.elements['school[]'].focus();
	 location = "#school";
	 return false;
	}
	
	if (!atLeastOne(aForm.elements['employer[]'])) {
	 alert("Please list at least one employer.");
	 //aForm.elements['employer[]'].focus();
	 location = "#employer";
	 return false;
	}
	
	/*if (aForm.empref_name.value == ""){
	 alert("Please tell us the name of your work referee.");
	 aForm.empref_name.focus();
	 return false;
	}*/
	
	/*if (aForm.empref_co.value == ""){
	 alert("Please tell us your work referee's company.");
	 aForm.empref_co.focus();
	 return false;
	}*/
	
	/*if (aForm.empref_pos.value == ""){
	 alert("Please tell us your work referee's position.");
	 aForm.empref_pos.focus();
	 return false;
	}*/
	
	/*if (aForm.empref_addr.value == ""){
	 alert("Please tell us your work referee's address.");
	 aForm.empref_addr.focus();
	 return false;
	}*/
	
	/*if (aForm.empref_postcode.value == ""){
	 alert("Please tell us your work referee's postcode.");
	 aForm.empref_postcode.focus();
	 return false;
	}*/
	
	/*if (aForm.empref_phone.value == ""){
	 alert("Please tell us your work referee's telephone number.");
	 aForm.empref_phone.focus();
	 return false;
	}*/
	
	/*if (aForm.empref_note.value == ""){
	 alert("Please tell us how long you have known your work referee and in what capacity.");
	 aForm.empref_note.focus();
	 return false;
	}*/
	
	/*if (aForm.perref_name.value == ""){
	 alert("Please tell us the name of your personal referee.");
	 aForm.perref_name.focus();
	 return false;
	}*/
	
	/*if (aForm.perref_addr.value == ""){
	 alert("Please tell us your personal referee's address.");
	 aForm.perref_addr.focus();
	 return false;
	}*/
	
	/*if (aForm.perref_postcode.value == ""){
	 alert("Please tell us your personal referee's postcode.");
	 aForm.perref_postcode.focus();
	 return false;
	}*/
	
	/*if (aForm.perref_phone.value == ""){
	 alert("Please tell us your personal referee's telephone number.");
	 aForm.perref_phone.focus();
	 return false;
	}*/
	
	/*if (aForm.perref_note.value == ""){
	 alert("Please tell us how long you have known your personal referee and in what capacity.");
	 aForm.perref_note.focus();
	 return false;
	}*/
	
	if (validateButton(aForm.convict) == false){
	 alert("Please tell us if you've ever been convicted of a criminal offence.");
	 aForm.convict[0].scrollIntoView();
	 return false;
	} else {
	 if (getButtonValue(aForm.convict) == 'yes'){
        if (aForm.convict_details.value == ""){
         alert("Please give details of your conviction(s).");
         aForm.convict_details.focus();
         return false;
        }
	 }
	}
	
	if (validateButton(aForm.caution) == false){
	 alert("Please tell us if you've ever been cautioned.");
	 aForm.caution[0].scrollIntoView();
	 return false;
	} else {
	 if (getButtonValue(aForm.caution) == 'yes'){
        if (aForm.caution_details.value == ""){
         alert("Please give details of your caution(s).");
         aForm.caution_details.focus();
         return false;
        }
	 }
	}
	
	if (validateButton(aForm.uk_drvl) == false){
	 alert("Please tell us if you hold a UK driving licence.");
	 aForm.uk_drvl[0].scrollIntoView();
	 return false;
	}
	
	if (validateButton(aForm.car) == false){
	 alert("Please tell us if you have a car or have permanent access to a car for work purposes.");
	 aForm.car[0].scrollIntoView();
	 return false;
	}
	
	if (validateButton(aForm.work_permit_req) == false){
	 alert("Please tell us if you require a work permit.");
	 aForm.work_permit_req[0].scrollIntoView();
	 return false;
	}
	
	/*if (aForm.crb_name.value == ""){
	 alert("Please enter your name here.");
	 aForm.crb_name.focus();
	 return false;
	}*/
	
	/*
	if (validateButton(aForm.med_cond1) == false){
	 alert("Please answer all the medical questions.");
	 aForm.med_cond1[0].scrollIntoView();
	 return false;
	}
	
	if (validateButton(aForm.med_cond2) == false){
	 alert("Please answer all the medical questions.");
	 aForm.med_cond2[0].scrollIntoView();
	 return false;
	}
	
	if (validateButton(aForm.med_cond3) == false){
	 alert("Please answer all the medical questions.");
	 aForm.med_cond3[0].scrollIntoView();
	 return false;
	}
	
	if (validateButton(aForm.med_cond4) == false){
	 alert("Please answer all the medical questions.");
	 aForm.med_cond4[0].scrollIntoView();
	 return false;
	}
	
	if (validateButton(aForm.med_cond5) == false){
	 alert("Please answer all the medical questions.");
	 aForm.med_cond5[0].scrollIntoView();
	 return false;
	}
	
	if (validateButton(aForm.med_cond6) == false){
	 alert("Please answer all the medical questions.");
	 aForm.med_cond6[0].scrollIntoView();
	 return false;
	}
	
	if (validateButton(aForm.med_cond7) == false){
	 alert("Please answer all the medical questions.");
	 aForm.med_cond7[0].scrollIntoView();
	 return false;
	}
	
	if (validateButton(aForm.med_cond8) == false){
	 alert("Please answer all the medical questions.");
	 aForm.med_cond8[0].scrollIntoView();
	 return false;
	}
	
	if (validateButton(aForm.med_cond9) == false){
	 alert("Please answer all the medical questions.");
	 aForm.med_cond9[0].scrollIntoView();
	 return false;
	}
	
	if (validateButton(aForm.med_cond10) == false){
	 alert("Please answer all the medical questions.");
	 aForm.med_cond10[0].scrollIntoView();
	 return false;
	}
	
	if (aForm.doc.value == ""){
	 alert("Please tell us the name of your doctor.");
	 aForm.doc.focus();
	 return false;
	}
	
	if (aForm.doc_addr.value == ""){
	 alert("Please tell us the address of your doctor.");
	 aForm.doc_addr.focus();
	 return false;
	}
	
	if (aForm.doc_postcode.value == ""){
	 alert("Please tell us the postcode of your doctor.");
	 aForm.doc_postcode.focus();
	 return false;
	}
	*/
	
	return true;
}

function validateButton(button) {
    var count = -1;
    for (var i=button.length-1; i > -1; i--) {
        if (button[i].checked) {
        	count = i; i = -1;
        }
    }
    if (count > -1) {
    	return true;
    } else {
    	return false;
    }
}

function getButtonValue(button) {
    var value = "";
    for (var i=button.length-1; i > -1; i--) {
        if (button[i].checked) {
        	value = button[i].value; i = -1;
        }
    }
    return value;
}

function atLeastOne(arr) {
    var count = -1;
    for (var i=arr.length-1; i > -1; i--) {
        if (arr[i].value != '') {
            return true;
        }
    }
    return false;
}

function emailCheck (emailStr) {
	/* The following pattern is used to check if the entered e-mail address
	   fits the user@domain format.  It also is used to separate the username
	   from the domain. */
	var emailPat=/^(.+)@(.+)$/
	/* The following string represents the pattern for matching all special
	   characters.  We don't want to allow special characters in the address. 
	   These characters include ( ) < > @ , ; : \ " . [ ]    */
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	/* The following string represents the range of characters allowed in a 
	   username or domainname.  It really states which chars aren't allowed. */
	var validChars="\[^\\s" + specialChars + "\]"
	/* The following pattern applies if the "user" is a quoted string (in
	   which case, there are no rules about which characters are allowed
	   and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
	   is a legal e-mail address. */
	var quotedUser="(\"[^\"]*\")"
	/* The following pattern applies for domains that are IP addresses,
	   rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
	   e-mail address. NOTE: The square brackets are required. */
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	/* The following string represents an atom (basically a series of
	   non-special characters.) */
	var atom=validChars + '+'
	/* The following string represents one word in the typical username.
	   For example, in john.doe@somewhere.com, john and doe are words.
	   Basically, a word is either an atom or quoted string. */
	var word="(" + atom + "|" + quotedUser + ")"
	// The following pattern describes the structure of the user
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	/* The following pattern describes the structure of a normal symbolic
	   domain, as opposed to ipDomainPat, shown above. */
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	
	
	/* Finally, let's start trying to figure out if the supplied address is
	   valid. */
	
	/* Begin with the coarse pattern to simply break up user@domain into
	   different pieces that are easy to analyze. */
	var matchArray=emailStr.match(emailPat)
	if (matchArray==null) {
	  /* Too many/few @'s or something; basically, this address doesn't
		 even fit the general mould of a valid e-mail address. */
		alert("Email address seems incorrect (check @ and .'s)")
		return false
	}
	var user=matchArray[1]
	var domain=matchArray[2]
	
	// See if "user" is valid 
	if (user.match(userPat)==null) {
		// user is not valid
		alert("The username doesn't seem to be valid.")
		return false
	}
	
	/* if the e-mail address is at an IP address (as opposed to a symbolic
	   host name) make sure the IP address is valid. */
	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) {
		// this is an IP address
		  for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				alert("Destination IP address is invalid!")
			return false
			}
		}
		return true
	}
	
	// Domain is symbolic name
	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
		alert("The domain name doesn't seem to be valid.")
		return false
	}
	
	/* domain name seems valid, but now make sure that it ends in a
	   three-letter word (like com, edu, gov) or a two-letter word,
	   representing country (uk, nl), and that there's a hostname preceding 
	   the domain or country. */
	
	/* Now we need to break up the domain to get a count of how many atoms
	   it consists of. */
	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 || 
		domArr[domArr.length-1].length>3) {
	   // the address must end in a two letter or three letter word.
	   alert("The address must end in a three-letter domain, or two letter country.")
	   return false
	}
	
	// Make sure there's a host name preceding the domain.
	if (len<2) {
	   var errStr="This address is missing a hostname!"
	   alert(errStr)
	   return false
	}
	
	// If we've gotten this far, everything's valid!
	return true;
}
//  End

function autoReject(type) {
    
    // special case where applicant clicked for live-in position, then clicked 'no' for driving licence then unclicked live-in button
    if ((type == "livein") && (document.getElementById("livein").checked == false) && (document.getElementById("uk_drvln").checked == true)) {
        var outstr = "You have indicated that you do not hold a UK or EU driving licence, which means you can only apply for live-in positions.\n\nClick OK to apply for live-in positions or Cancel to change your driving licence status.";
        var r=confirm(outstr);
        if (r==true) {
            document.getElementById("livein").checked = true;
        } else {
            document.getElementById("uk_drvln").checked = false;
            document.getElementById("uk_drvly").focus();
        }
        return;
    }

    // allow applicant to select 'no' driving licence if they already selected for live-in positions
    if ((type == "uk_drvln") && (document.getElementById("livein").checked == true)) {
        alert("Please note that without a driving licence you may only apply for live-in positions.");
        return;
    }
    
    // otherwise...
    switch(type) {
        case "uk_drvln":
            var outstr = "If you do not hold a UK or EU driving licence you cannot apply (except for live-in positions).\n\nClick OK to select again or Cancel to abort your application and go back to the home page.";
            break;
        case "we_availn":
            var outstr = "You must be available to work at weekends.\n\nClick OK to select again or Cancel to abort your application and go back to the home page.";
            break;
        default:
            return true;
    }
    var r=confirm(outstr);
    if (r==true) {
        document.getElementById(type).checked = false;
    } else {
        location="http://"+location.hostname;
    }
}

function wherehearSelect(select) {
    form = select.form;
    //alert(select.options[select.selectedIndex].value);
    switch(select.options[select.selectedIndex].value) {
        case 'Choose...':
            document.getElementById('whereheardiv').style.display = "none";
            form.hear.value = "";
            break;
        case 'Other':
            document.getElementById('whereheardiv').style.display = "block";
            form.hear.value = "";
            form.hear.focus();
            break;
        default:
            document.getElementById('whereheardiv').style.display = "none";
            form.hear.value = select.options[select.selectedIndex].value;
    }
}

function checkDateFormat(dateobj) {
    var re = /[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{4}/;
    if (!re.test(dateobj.value)) {
        alert("Please enter a date in the format dd/mm/yyyy e.g. 31/03/2010 for 31 March 2010");
        dateobj.focus();
        dateobj.select();
    }
}


