/*************************************************************
* Page: job_postings.js v 1.8
* Description: Posting a Job, validate form
* 
* Hospitality Recruitment - Online Strategies
* Copyright (C) 2005 Online Strategies, All rights reserved
* Email  info@hospitalityonline.co.uk
* 
*************************************************************/
// Start Validation function
function validate_job_posting(e){
	if ((VALIDATE_JOB_POSTING == 0 && JAVASCRIPT_ON == 1) || (VALIDATE_JOB_POSTING == 0 && JAVASCRIPT_ON == 0)){ // override javascript
		// Send the form through without Javascript DEBUG
		return true;
	}
	else if ((VALIDATE_JOB_POSTING == 1 && JAVASCRIPT_ON == 1) || (VALIDATE_JOB_POSTING == 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 today = dateObj.getFullYear()+'-'+format_date((dateObj.getMonth(0)+1))+'-'+format_date(dateObj.getDate());
		
			//alert ("today:" +today+"\n"+"Start:"+e.start_date.value);
			// Must check atleast one Vacancy type
			group = group_it(e['anonymous']);
			if (is_minimum_checked(1,group))
			{
				msg += error[285];
				focus_el = focus_el || group[0];
			}
			// Vacancy Reference Empty
			
			e.vacancy_reference.value = e.vacancy_reference.value.trim();
			if (is_empty(e.vacancy_reference))
			{
				msg += error[271];
				focus_el = focus_el || e.vacancy_reference;
			}
			
			// Vacancy Title Empty
			if(document.getElementById('vacancy_title'))
			{
				e.vacancy_title.value = e.vacancy_title.value.trim();
				if (is_empty(e.vacancy_title))
				
				{
					msg += error[272];
					focus_el = focus_el || e.vacancy_title;
				}
			}
			if(document.getElementById('job_period')){
				// job_period not selected
				
				if (is_empty(e.job_period))
				{
					msg += error[273];
					focus_el = focus_el || e.job_period;
				}	
			}
			if (location.pathname == "/vacancy/new")
			{
				if(!is_empty(e.start_date))
				{
					if(e.start_date.value < today)
					{
						msg += error[284];
						focus_el = focus_el || e.start_date;
					}
				}
			}
			// online date empty
			e.online_date.value = e.online_date.value.trim();
			if (is_empty(e.online_date))
			{
				msg += error[275];
				focus_el = focus_el || e.online_date;
			}
			// Country is not selected
			e.country.value = e.country.value.trim();
			if (e.country.selectedIndex == 0)
			{
				msg += error[24];
				focus_el = focus_el || e.main_region;
			}
			// Country is not selected
			e.country.value = e.country.value.trim();
			e.main_region.value = e.main_region.value.trim();
			if (e.country.selectedIndex == 1)
			{
				if (e.main_region.selectedIndex == 0) {
					msg += error[61];
					focus_el = focus_el || e.main_region;
				}
			}
			// country must be selected
			if (e.country.selectedIndex != 0)
			{	
				
				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;
				}
			}
			// number employees not selected
			e.employer_type.value = e.employer_type.value.trim();
			if (e.employer_type.selectedIndex == 0)
			{
				msg += error[283];
				focus_el = focus_el || e.employer_type;
			}
	
			group = group_it(e['show_salary']);
			if (is_minimum_checked(1,group))
			{
				msg += error[287];
				focus_el = focus_el || group[0];
			}
			if (document.getElementById('show_salary_yes').checked)
			{
				// must selected preferred salary type
				if(e.salary_type.selectedIndex == 0)
				{
					msg += error[276];
					focus_el = focus_el || e.salary_type;
				}
				// base salary empty
				e.base_salary.value = e.base_salary.value.trim();
				if(is_empty(e.base_salary))
				{
					msg += error[277];
					focus_el = focus_el || e.base_salary;
				}
				// max salary empty
				e.max_salary.value = e.max_salary.value.trim();
				if(is_empty(e.max_salary))
				{
					msg += error[278];
					focus_el = focus_el || e.max_salary;
				}
			}
			// vacancy type not selected
			e.vacancy_type.value = e.vacancy_type.value.trim();
			if (e.vacancy_type.selectedIndex == 0)
			{
				msg += error[279];
				focus_el = focus_el || e.vacancy_type;
			}
			// education level not selected
			//e.education_level.value = e.education_level.value.trim();
			//if (e.education_level.selectedIndex == 0)
			//{
				//msg += error[280];
				//focus_el = focus_el || e.education_level;
			//}
			/* e.vacancy_description.value = e.vacancy_description.value.trim();
			if (is_empty(e.vacancy_description))
			{
				msg += error[281];
				focus_el = focus_el || e.vacancy_description;
			}*/
			// if there is a logo drop down (when logged)
			if (e.company_logo){
			var count = ( e.company_logo.getElementsByTagName('option').length -1 );
				if (e.company_logo.selectedIndex == count)
				{
					e.label.value = e.label.value.trim();
					if (is_empty(e.label))
					{
						msg += error[241];
						focus_el = focus_el || e.label;
					}
					
					// logo field is not empty
					//e.logo.value = e.logo.value.trim();
					if (is_empty(e.logo))
					{
						msg += error[242];
						focus_el = focus_el || e.logo;
					}
					
					// logo field is not empty
					//e.logo.value = e.logo.value.trim();
					if (!is_empty(e.logo))
					{
						if(!check_extention(e.logo, "img"))
						{
							msg += error[243];
							focus_el = focus_el || e.logo;
						}
					}
				}
			}
			if  (e.contact_email.style.display == "") {
				var mex = ( document.getElementById('contact_email').getElementsByTagName('option').length -1 )

				if (e.contact_email.selectedIndex == 0) {
					msg += error[248];
					focus_el = focus_el || e.contact_email;
				}
			}
				e.contact_email_txt.value = e.contact_email_txt.value.trim();
				if (e.contact_email.style.display == "") {
					if (e.contact_email.selectedIndex == mex) {
						if (is_empty(e.contact_email_txt)){
							msg += error[249];
							focus_el = focus_el || e.contact_email_txt;	
						}
					}				
				}
				if (e.contact_email.style.display == "none") {
					if (e.contact_email_txt)
					{
						if (is_empty(e.contact_email_txt)){
							msg += error[249];
							focus_el = focus_el || e.contact_email_txt;	
						}
					} 
				}

		// 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;
	}
}