var isFormSubmitted = false;

function checkFormSubmitted() {
	if(!isFormSubmitted) 
	{
		isFormSubmitted = true;
		return false;
	}
	else
	{
		return true;
	}
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function verifyAppForm(form){
	var message = "";
	// validation fails if the input is blank
	if(trim(form.strFullName.value) == '') { 
		message += "Name\n"; 
		form.strFullName.focus();
       	} 
	var emailre = /^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i; //should allow for basically any email address
	if(trim(form.strTelephoneNumber.value) == '') { 
		message += "Telephone number\n"; 
		form.strTelephoneNumber.focus();  
	} 
	if(trim(form.strContactEmail.value) == '' || !emailre.test(form.email.value)){
		message += "Email Address\n";
		form.strContactEmail.focus();
	}
	if(trim(form.strGradeQualification.value) == ''){
		message += "Grade/Qualification\n";
		form.strGradeQualification.focus();
	}
	if(trim(form.strSpeciality.value) == ''){
		message += "Speciality\n";
		form.strSpeciality.focus();
	}
	if(message!=""){
		//send email
		message = "Sorry, the following information is mandatory and has not been filled in: \n\n" + message + "\nPlease review the information and try again.";
		var mailMsg = "------------------------------\n";
		mailMsg += "Failed Job Application details:\n";
		mailMsg += "------------------------------\n";
	        mailMsg += "\n\n";
		mailMsg += "Applicant Name: " + form.strFullName.value + "\n";
		mailMsg += "Applicant Telephone: " + form.strTelephoneNumber.value + "\n";
		mailMsg += "Applicant Postal Address: " + form.strAddress.value + "\n";
		mailMsg += "Applicant Email Address: " + form.strContactEmail.value + "\n";
		mailMsg += "Applicant Enquiry text: " + form.strCoverLetter.value + "\n";
		mailMsg += "Applicant Grade/Qualification: " + form.strGradeQualification.value + "\n";
		mailMsg += "Applicant Speciality: " + form.strSpeciality.value + "\n";
		mailMsg += "Cover Letter: " + form.strCoverLetter.value + "\n";
		mailMsg += "Message to User: \n\n" + message;
		mailMsg = escape(mailMsg); //urlencode
		ajaxpage("/includes/sendemail.asp?message=" + mailMsg + "", "hiddenEmailConfirmation");
		get(form,'/includes/failedenquiry.asp');
		alert(message);
		return false;
	}
	
	if(checkFormSubmitted()) {
		alert('You have already submitted this form. Please wait...');
		return false;
	}
	return true;  
}

function verifyShortForm(form){
var message = "";
	// validation fails if the input is blank
	if(trim(form.txt_0_2053.value) == '') { 
		message += "First Name\n"; 
		form.txt_0_2053.focus();
       	} 
	if(trim(form.txt_0_2054.value) == '') { 
		message += "Last Name\n"; 
		form.txt_0_2053.focus();
       	} 
	var emailre = /^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i; //should allow for basically any email address
	if(trim(form.txt_0_2055.value) == '') { 
		message += "Address Line 1\n"; 
		form.txt_0_2055.focus();  
	} 
	if(trim(form.txt_0_2058.value) == '' && trim(form.txt_0_2506.value) == '') { 
		message += "Either Home Phone number or Mobile Phone number \n"; 
		form.txt_0_2058.focus();  
	} 
	if(trim(form.txt_0_2524.value) == '') { 
		message += "Town\n"; 
		form.txt_0_2524.focus();  
	} 
	if(form.gdl_0_2526.selectedIndex <= 0){
		message += "County\n";
		form.gdl_0_2526.focus();
	}
	if(trim(form.txt_0_2528.value) == '') { 
		message += "Postcode\n"; 
		form.txt_0_2528.focus();  
	}
	if(form.gdl_0_2507.selectedIndex <= 0){
		message += "Grade\n";
		form.gdl_0_2507.focus();
	}
	
	if(trim(form.txt_0_2056.value) == '' || !emailre.test(form.txt_0_2056.value)){
		message += "Email Address\n";
		form.txt_0_2056.focus();
	}

    if (form.mlt_0_2592_1)
    {
      if (form.q_2566_present.value == "1" && !form.mlt_0_2592_1.checked && !form.mlt_0_2592_4.checked)
		message += "NMC Registration Status\n";
    }

    if (form.mlt_0_2592_2)
    {
      if (form.q_2632_present.value == "1" && !form.mlt_0_2592_2.checked && !form.mlt_0_2592_5.checked)
		message += "HPC/GDC Registration Status\n";
    }

    if (form.mlt_0_2592_0)
    {    
      if (form.q_2592_present.value == "1" && !form.mlt_0_2592_0.checked && !form.mlt_0_2592_3.checked)
		message += "GMC Registration Status\n";
    }

    if (form.mlt_0_2591_0)
    {
      if (form.q_2591_present.value == "1" && !form.mlt_0_2591_0.checked && !form.mlt_0_2591_1.checked && !form.mlt_0_2591_2.checked && !form.mlt_0_2591_3.checked)
		message += "UK work experience\n";
    }

    if (form.mlt_0_2589.selectedIndex <= 0)
    {
		message += "Registration Status\n";
    }

	if(message!=""){
		//send email
		message = "Sorry, the following information is mandatory and has not been filled in: \n\n" + message + "\nPlease review the information and try again.";
		var mailMsg = "------------------------------\n";
		mailMsg += "Failed Enquiry details:\n";
		mailMsg += "------------------------------\n";
	        mailMsg += "\n\n";
		mailMsg += "Applicant First Name: " + form.txt_0_2053.value + "\n";
		mailMsg += "Applicant Last Name: " + form.txt_0_2054.value + "\n";
		mailMsg += "Applicant Telephone: " + form.txt_0_2058.value + "\n";
		mailMsg += "Applicant Email Address: " + form.txt_0_2056.value + "\n";
		mailMsg += "Applicant Address Line 1: " + form.txt_0_2055.value + "\n";
		mailMsg += "Applicant Address Line 2: " + form.txt_0_2523.value + "\n";
		mailMsg += "Applicant Town: " + form.txt_0_2524.value + "\n";
		mailMsg += "Applicant County: " + form.gdl_0_2526.selectedIndex.name + "\n";
		mailMsg += "Applicant Postcode: " + form.txt_0_2528.value + "\n";
		mailMsg += "Applicant Grade: " + form.gdl_0_2507.selectedIndex.name + "\n";
		mailMsg += "Applicant Enquiry text: " + form.tta_0_2520.value + "\n";
		mailMsg += "Message to User: \n\n" + message;
		mailMsg = escape(mailMsg); //urlencode
		ajaxpage("/includes/sendemail.asp?message=" + mailMsg + "", "hiddenEmailConfirmation");
		//get(form,'/includes/failedenquiry.asp');
		alert(message);
		return false;
	}

	if(checkFormSubmitted()) {
		alert('You have already submitted this form. Please wait...');
		return false;
	}

    try {
        category = 'Unknown';
        if ($F("q_2592_present") == "1")
            category = 'Doctor';
        else if ($F("q_2566_present") == "1")
            category = 'Nurse';
        else if ($F("q_2632_present") == "1")
            category = 'Allied Health';

        _gaq.push(['_trackEvent', category, 'CV Submission']);
    }
    catch (err)
    {}
	
	return true;  
}

function get(obj, url) {
   var getstr = "?";
   for (i=0; i<obj.elements.length; i++) {
      if (obj.elements[i].tagName == "INPUT") {
         if (obj.elements[i].type == "text") {
            getstr += obj.elements[i].name + "=" + escape(obj.elements[i].value) + "&";
         }
         if (obj.elements[i].type == "checkbox") {
            if (obj.elements[i].checked) {
               getstr += obj.elements[i].name + "=" + escape(obj.elements[i].value) + "&";
            } else {
               getstr += obj.elements[i].name + "=&";
            }
         }
         if (obj.elements[i].type == "radio") {
            if (obj.elements[i].checked) {
               getstr += obj.elements[i].name + "=" + escape(obj.elements[i].value) + "&";
            }
         }
      }   
      if (obj.elements[i].tagName == "SELECT") {
         var sel = obj.elements[i];
         getstr += sel.name + "=" + escape(sel.options[sel.selectedIndex].value) + "&";
      }
      if (obj.elements[i].tagName == "TEXTAREA") {
	 getstr += obj.elements[i].name + "=" + escape(obj.elements[i].value) + "&";
      }
      
   }
   ajaxpage(url + getstr, "hiddenEmailConfirmation");
}

function checkForm(form) { 
	var message = "";
	// validation fails if the input is blank
	if(trim(form.name.value) == '') { 
		message += "Name\n"; 
		form.name.focus();
       	} 
	var emailre = /^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i; //should allow for basically any email address
	if(trim(form.telephonenumber.value) == '') { 
		message += "Telephone number\n"; 
		form.telephonenumber.focus();  
	} 
	/*if(trim(form.postaladdress.value) == '') {
		message += "Postal Address\n";
		form.postaladdress.focus();
	}*/ //now optional
	if(trim(form.email.value) == '' || !emailre.test(form.email.value)){
		message += "Email Address\n";
		form.email.focus();
	}
	/*if(trim(form.message.value) == ''){
		message += "Enquiry message\n";
		form.message.focus();
	}*/ //now optional
	if(trim(form.gradequalification.value) == ''){
		message += "Grade/Qualification\n";
		form.gradequalification.focus();
	}
	if(trim(form.speciality.value) == ''){
		message += "Speciality\n";
		form.speciality.focus();
	}
	/*if(trim(form.attachcv.value) == ''){
		message += "CV\n";
	}*/ //CV now optional
	if(message!=""){
		//send email
		message = "Sorry, the following information is mandatory and has not been filled in: \n\n" + message + "\nPlease review the information and try again.";
		var mailMsg = "------------------------------\n";
		mailMsg += "Failed Enquiry details:\n";
		mailMsg += "------------------------------\n";
	        mailMsg += "\n\n";
		mailMsg += "Applicant Name: " + form.name.value + "\n";
		mailMsg += "Applicant Telephone: " + form.telephonenumber.value + "\n";
		mailMsg += "Applicant Postal Address: " + form.postaladdress.value + "\n";
		mailMsg += "Applicant Email Address: " + form.email.value + "\n";
		mailMsg += "Applicant Enquiry text: " + form.message.value + "\n";
		mailMsg += "Applicant Grade/Qualification: " + form.gradequalification.value + "\n";
		mailMsg += "Applicant Speciality: " + form.speciality.value + "\n";
		mailMsg += "Applicant CV filename: " + form.attachcv.value + " \n\n";
		mailMsg += "Message to User: \n\n" + message;
		mailMsg = escape(mailMsg); //urlencode
		//alert(unescape(mailMsg));
		ajaxpage("/includes/sendemail.asp?message=" + mailMsg + "", "hiddenEmailConfirmation");
		//ajaxpage("/includes/sendemail.asp?message=test", "hiddenEmailConfirmation");
		alert(message);
		get(form,'/includes/failedenquiry.asp');
		return false;
	}
	
	if(!checkFormSubmitted()) {
		alert('You have already submitted this form. Please wait...');
		return false;
	}
	
	return true;  
} 

