/*************************************************************
* Page: jobseekers.js v 1.2  
* Description: jobseekers registration & edit validation
* 
* Hospitality Recruitment - Online Strategies
* Copyright (C) 2005 Online Strategies, All rights reserved
* Email  info@hospitalityonline.co.uk
*
* Note: Commented out secret question and answer validation as per changes for new application process
*       21.02.2005
*************************************************************/
function validate(e){
	var path = location.pathname;
	
	if ((VALIDATE_JOBSEEKERS == 0 && JAVASCRIPT_ON == 1) || (VALIDATE_JOBSEEKERS == 0 && JAVASCRIPT_ON == 0)){ // override javascript
		
		return true; // Send the form through without Javascript DEBUG
	
	}
	else if ((VALIDATE_JOBSEEKERS == 1 && JAVASCRIPT_ON == 1) || (VALIDATE_JOBSEEKERS == 1 && JAVASCRIPT_ON == 0)){ // Validate the form
		// Trim the data to make sure there is no white space
		String.prototype.trim = function()
		{
			return this.replace(/^\s*|\s*$/g, '');
		}
		
		var focus_el = null, msg = '';
		var dateObj = new Date();
		var d = format_date(dateObj.getDate());
		var m = format_date((dateObj.getMonth(0)+1));
		var y = dateObj.getFullYear();
		// title must be filled in
		e.title.value = e.title.value.trim();
		if (e.title.selectedIndex == 0)
		{
			msg += error[0];
			focus_el = focus_el || e.title;
		}		
		// First Name is_empty
		e.first_name.value = e.first_name.value.trim();
		if (is_empty(e.first_name))
		{
			msg += error[1];
			focus_el = focus_el || e.first_name;
		}				

		// Surname is empty
		e.surname.value = e.surname.value.trim();
		if (is_empty(e.surname))
		{
			msg += error[2];
			focus_el = focus_el || e.surname;
		}

		// EMAIL *********************************************
		// Must give a valid email
		e.email.value = e.email.value.trim();
		if (is_empty(e.email))
		{
			msg += error[33];
			focus_el = focus_el || e.email;
		}

		// This code is left out for the editing
		if (location.pathname == "/jobseekers_registration/"){

			// Must fill in confirm email 
			e.email.value = e.email.value.trim();
			if (!is_empty(e.email) && is_empty(e.confirm_email))
			{
				msg += error[53];
				focus_el = focus_el || e.confirm_email;
			}

			// Must fill in confirm email 
			e.email.value = e.email.value.trim();
			if (!is_empty(e.email) && !is_empty(e.confirm_email))
			{
				if (e.email.value != e.confirm_email.value)
				{
					msg += error[4];
					focus_el = focus_el || e.email;
				}
				// Must be correct format
				e.email.value = e.email.value.trim();
				if (!is_empty(e.email))
				{
					if(!is_valid_email(e.email))
					{
						msg += error[33];
						focus_el = focus_el || e.email;
					}
				}
			}
		}
		// Must select male or female
		if (!is_radio_selected(e.gender))
		{
			msg += error[60];
			focus_el = focus_el || e.gender[0];
		}
		
		// nationality not selected
		e.nationality.value = e.nationality.value.trim();
		if (e.nationality.selectedIndex == 0)
		{
			msg += error[54];
			focus_el = focus_el || e.nationality;
		}
		
		// Make sure the date is filled in
		e.days.value = e.days.value.trim();
		if ((!is_number(e.days) && !is_number(e.months) && !is_number(e.years)) || 
			(!is_number(e.days) && !is_number(e.months)) || 
			(!is_number(e.months) && !is_number(e.years)) || 
			(!is_number(e.days) && !is_number(e.years)) || 
			(!is_number(e.days)) || 
			(!is_number(e.months)) || 
			(!is_number(e.years)))
		{
		    msg += error[34];
			focus_el = focus_el || e.days;
		}
		if ((is_number(e.days) && is_number(e.months) && is_number(e.years)) || 
			(is_number(e.days) && is_number(e.months)) || 
			(is_number(e.months) && is_number(e.years)) || 
			(is_number(e.days) && is_number(e.years)) || 
			(is_number(e.days)) || 
			(is_number(e.months)) || 
			(is_number(e.years)))
		{
			if(e.days.value > 31){
		    	msg += error[51];
				focus_el = focus_el || e.days;
			}
			if(e.months.value > 12)
			{
		    	msg += error[52];
				focus_el = focus_el || e.months;
			}
			if(e.years.value >= y)
			{
		    	msg += error[59];
				focus_el = focus_el || e.years;
			}
			if(e.years.value < '1946')
			{
		    	msg += error[62];
				focus_el = focus_el || e.years;
			}
		}
		// marital status must be selected
		e.marital_status.value = e.marital_status.value.trim();
		if (e.marital_status.selectedIndex == 0)
		{
			msg += error[35];
			focus_el = focus_el || e.marital_status;
		}
		// if telephone field is filled in make sure its a number
		e.telephone.value = e.telephone.value.trim();
		if (!is_empty(e.telephone))
		{
			if (!is_valid_phone1(e.telephone))
				{
					msg += error[36];
					focus_el = focus_el || e.telephone;
				}
		}
		// if mobile field is filled in make sure its a number
		e.mobile.value = e.mobile.value.trim();
		if (!is_empty(e.mobile))
		{
			if (!is_number(e.mobile))
				{
					msg += error[37];
					focus_el = focus_el || e.mobile;
				}
		}
		// Address line 1 is empty
		e.address_line_1.value = e.address_line_1.value.trim();
		if (is_empty(e.address_line_1))
		{
			msg += error[39];
			focus_el = focus_el || e.address_line_1;
		}
		// Town is empty
		e.town.value = e.town.value.trim();
		if (is_empty(e.town))
		{
			msg += error[40];
			focus_el = focus_el || e.town;
		}
		// Postcode is empty or more than 10 chars
		e.postcode.value = e.postcode.value.trim();
		if ((is_empty(e.postcode)) || (e.postcode.value.length > 10))
		{
			msg += error[41];
			focus_el = focus_el || e.postcode;
		}
		// country must be selected
		e.country.value = e.country.value.trim();
		if (e.country.selectedIndex == 0)
		{
			msg += error[48];
			focus_el = focus_el || e.country;
		}
		
		// country must be selected
		if (e.country.selectedIndex != 0)
		{
			e.region.value = e.region.value.trim();
			if (e.country.selectedIndex == 1 && e.region.selectedIndex == 0)
			{
				msg += error[49];
				focus_el = focus_el || e.region;
			}
			e.t_regions.value = e.t_regions.value.trim();
			if (e.country.selectedIndex != 1 && e.country.selectedIndex != 0 && is_empty(e.t_regions)) 
			{
				msg += error[50];
				focus_el = focus_el || e.t_regions;
			}
		}
		// where did you hear about us, not selected
			e.marketing.value = e.marketing.value.trim();
			if (e.marketing.selectedIndex == 0)
			{
				msg += error[57];
				focus_el = focus_el || e.marketing;
			}
			
			// where did you hear about us, not selected
			e.marketing_other.value = e.marketing_other.value.trim();
			if (e.marketing.selectedIndex != 0 && e.marketing.selectedIndex == 10)
			{
				if(is_empty(e.marketing_other))
				{
					msg += error[58];
					focus_el = focus_el || e.marketing_other;					
				}
			}
		// This code is left out for the editing
		if (location.pathname == "/jobseekers_registration/"){
			// Username is empty
			e.username.value = e.username.value.trim();
			if (is_empty(e.username))
			{
				msg += error[25];
				focus_el = focus_el || e.username;
			}
			// Username is empty
			e.username.value = e.username.value.trim();
			if (!is_empty(e.username) && (e.username.value.length < USERNAME_MIN || e.username.value.length > USERNAME_MAX))
			{
				msg += error[26];
				focus_el = focus_el || e.username;
			}
			/* PASSWORD VALICATION START HERE */
			// password empty
			e.password.value = e.password.value.trim();
			if (is_empty(e.password))
			{
				msg += error[55];
				focus_el = focus_el || e.password;
			}
			// password empty
			e.password.value = e.password.value.trim();
			if (!is_empty(e.password) && is_empty(e.confirm_password))
			{
				msg += error[56];
				focus_el = focus_el || e.password;
			}
			
			// password empty
			e.password.value = e.password.value.trim();
			if (!is_empty(e.password) && !is_empty(e.confirm_password))
			{
				// password's dont match
				e.password.value = e.password.value.trim();
				if (e.password.value != e.confirm_password.value)
				{
					msg += error[27];
					focus_el = focus_el || e.password;
				}
			
				// password incorrectly formattted
				e.password.value = e.password.value.trim();
				if (e.password.value.length < PASSWORD_MIN || e.password.value.length > PASSWORD_MAX)
				{
					msg += error[28];
					focus_el = focus_el || e.password;
				}
			}

		}
		// Secret question not selected
		/*e.secret_question.value = e.secret_question.value.trim();
		if (e.secret_question.selectedIndex == 0)
		{
			msg += error[29];
			focus_el = focus_el || e.secret_question;
		}
		// Secret answer left blank
		e.secret_answer.value = e.secret_answer.value.trim();
		if (is_empty(e.secret_answer))
		{
			msg += error[30];
			focus_el = focus_el || e.secret_answer;
		}
		// Answer but no Question
		e.secret_question.value = e.secret_question.value.trim();
		if (!is_empty(e.secret_answer) && e.secret_question.selectedIndex == 0)
		{
			msg += error[31];
			focus_el = focus_el || e.secret_answer;
		}*/
		// This code is left out for the editing
		if (location.pathname == "/jobseekers_registration/"){
			// Terms and conditions must be checked
			e.terms.value = e.terms.value.trim();
			if (is_checked(e.terms))
			{
				msg += error[32];
				focus_el = focus_el || e.terms;
			}
		}
		// If there is an error then send through an alert (var msg) and return false
		if (msg != '')
		{
			var prefix = "The form has been incorrectly completed:\n\n";
			alert(prefix + msg);
			if (focus_el.focus)
			{
				focus_el.focus();
			}
			return false;
		}
		// If there are no errors send through the form and process it.
		if (CHANGE_PROCESS == 1){
			showprocess('show','hide');
		}
		else {
			document.getElementbyId('sub').disabled = true;
		}
		return true;
	}
	else {
		// There has been an error
		alert ("Error reported to: " + ADMIN_EMAIL);
		return false;
	}
}