function ValidatePostAJobForm()
{
	var sJobTitle = document.form1.JobTitle.value;
	var sDescription = document.form1.JobDescription.value;
//	var sCompanyID = document.form1.liCompanyID.value;
	var sCompanyName = document.form1.CompanyName.value;
	var sEmail = document.form1.Email.value;
	//var bFlexibleJob = document.form1.bFlexibleJob.checked;
	var dtValidFrom;
	
	var empty_fields = "";
	var invalid_Dates = "";
	var f = document.form1;
	var sName="";
	
	if ( sJobTitle == '' )
	{
		empty_fields += "\n        - Job title";
		document.form1.JobTitle.focus();
		document.form1.JobTitle.select();
	}
		
	if ( sDescription == '' )
	{
		empty_fields += "\n        - Description";
		if (! empty_fields)
		{
			document.form1.Description.focus();
			document.form1.Description.select();
		}
	}
	
	// Validate multi selects (EXCEPT location coz we need to link that with bFlexibleJob)
	for (var i = 0; i < f.length; i ++) {
		var e = f.elements[i];
		if (e.type == "checkbox" && (e.name != sName))
		{
			empty_fields += findChecked(e);
			sName=e.name;
		}
		if ((e.type == "select-one" || e.type == "select-multiple") && (e.name != "liJobRegionID"))
		{
			empty_fields += findSelected(e);
			sName=e.name;
		}
	}
	
	// Validate location and bFlexibleJob items. We only need to force a location selection If they have havent set the job as flexible
//	if ( bFlexibleJob != true )
	//{
		for (var i = 0; i < f.length; i ++) 
		{
			var e = f.elements[i];
			if ((e.type == "select-one" || e.type == "select-multiple") && (e.name == "liJobRegionID"))
			{
				empty_fields += findSelected(e);
				sName=e.name;
			}
		}
	//}
	
	
	dtValidFrom = document.form1.dtValidFrom.value;
	
	if ( dtValidFrom == '' )
	{
		empty_fields += "\n        - Start date";
		if (! empty_fields)
		{
			document.form1.dtValidFrom.focus();
			document.form1.dtValidFrom.select();
		}
	}
	
	if (GetDate(dtValidFrom) > DateAdd(new Date(),0,1,0))
	{
		invalid_Dates += "\n        - Start date cannot be more than 1 month from now";
		if (! invalid_Dates)
		{
			document.form1.dtValidFrom.focus();
			document.form1.dtValidFrom.select();
		}
	}
	
		//alert ( 'Date From: ' + GetDate(dtValidFrom) )
		//alert ( 'Date From: ' + Date() )
	if (GetDate(dtValidFrom) < Date())
	{
		
		invalid_Dates += "\n        - Start date cannot be in the past";
		if (! invalid_Dates)
		{
			document.form1.dtValidFrom.focus();
			document.form1.dtValidFrom.select();
		}
	}
	
//	if ( sCompanyID == '' && sCompanyName == '' )
//	{
		if ( sCompanyName == '' )
		{
			empty_fields += "\n        - You must enter a Company Name";
			document.form1.CompanyName.focus();
			document.form1.CompanyName.select();
		}
		
		if ( sEmail == '' )
		{
			empty_fields += "\n        - You must enter an Email address";
			document.form1.Email.focus();
			document.form1.Email.select();
		}
		
		if ( !CheckValidEmail(sEmail) )
		{
			empty_fields += "\n        - Valid Email address";
			document.form1.Email.focus();
			document.form1.Email.select();
		}
//	}
	
	
	if (invalid_Dates != '')
		return alertUserDates(invalid_Dates);		
	else if (empty_fields != '')
		return alertUser(empty_fields);
	
	return true;
			
}

function findSelected(e)
{
	var sField="";
	var sName="";
	var sum=0;
	
	switch(e.name){
		case "liJobTypeID":
			sField = "Job sector";
			sName = "liJobTypeID";
			break;
		case "liEmployerTypeID":
			sField = "InPR Job sector"; // Relabeled to "Job sector" as per categorisation (JD 17/10/2006)
			sName = "liEmployerTypeID";
			break;
//		case "liIndustrySkillID":
//			sField = "Industry skill";
//			sName = "liIndustrySkillID";
//			break;
		case "liJobRegionID":
			sField = "Location"; // Relabeled to Location" as per categorisation (JD 17/10/2006)
			sName = "liJobRegionID";
			break;
//		case "liJobPositionTypeID":
//			sField = "Job term"; // Relabeled to "Job term" as per categorisation (JD 17/10/2006)
//			sName = "liJobPositionTypeID";
//			break;
		case "liSalaryRateID":
			sField = "Salary"; 
			sName = "liSalaryRateID";
			break;
//		case "liHoursID":
//			sField = "Travel"; 
//			sName = "liHoursID";
//			break;
		//Removed because it was impossible to "Post a Job" while registering a company for the first time. BD 10 jan 07
		/*case "liCompanyID":
			sField = "Employer";
			sName = "liCompanyID";
			break;*/
		}

	if (sField != "")
	{	
		if ( document.form1.elements[sName].value==0 )
			return "\n        - " + sField;
	}
	return "";

}

function DateAdd(startDate, numDays, numMonths, numYears)
{
	var returnDate = new Date(startDate.getTime());
	var yearsToAdd = numYears;
	
	var month = returnDate.getMonth()	+ numMonths;
	if (month > 11)
	{
		yearsToAdd = Math.floor((month+1)/12);
		month -= 12*yearsToAdd;
		yearsToAdd += numYears;
	}
	returnDate.setMonth(month);
	returnDate.setFullYear(returnDate.getFullYear()	+ yearsToAdd);
	
	returnDate.setTime(returnDate.getTime()+60000*60*24*numDays);
	
	return returnDate;

}

function GetDate(Text)
{
	var sText = Text;
	var sMsg = ''
	var sRes = ''
	
	if ( sText != '' )
	{
		var d = sText.split('/')

		if ( d.length == 2 )
		{
			var d = new Date()
			d[2] = d.getFullYear()
		}
		
		if ( d.length != 3 )
		{
			sMsg = 'Invalid date format - ' + sText
		}
		else
		{
			if ( isNaN(parseInt(d[0],10)) || d[0] > 31 || d[0] < 1 )
			{
				sMsg = 'Invalid day of month - ' + d[0]
			}
			else if ( isNaN(parseInt(d[1],10)) || d[1] > 12 || d[1] < 1 )
			{
				sMsg = 'Invalid month - ' + d[1]			
			}
			else if ( isNaN(parseInt(d[2],10)) )
			{
				sMsg = 'Invalid day of year - ' + d[2]			
			}
			else if ( d[2] > -1 && d[2] < 100 )
			{
				d[2] = parseInt(d[2],10) + 2000
			}
			
			if ( d[2] < 1900 || d[2] > 3000 )
			{
				sMsg = 'Invalid year - ' + d[2]
			}
			
			var day, month
			
			day = parseInt(d[0],10)
			if (day<10)
				day = '0' + day
			
			month = parseInt(d[1],10)
			if ( month<10 )
				month = '0' + month
			
			//sRes = day + '/' + month  + '/' + parseInt(d[2],10)
			sRes =  month + '/' +  day + '/' + parseInt(d[2],10)  
		}
	}
	
	if ( sMsg != '' )
	{
		return sRes;
	}
	else
	{
		var dRes = new Date( Date.parse( sRes ) );
		return dRes;
	}
}

function CheckValidEmail( sText )
{
	// Assumes sText is not empty
	var d = sText.split('@')

	if ( d.length != 2 )
	{
		return false;
	}
	else
	{
		var dom = d[1].split('.') ;
		
		if ( dom.length < 2 )
			return false;
	}
	
	return true;
}


function alertUser(empty_fields)
{
	var msg="";
	
	if (empty_fields){
		msg += "The following fields are mandatory:"
				+ empty_fields + "\n";
		alert(msg);	
		return false;
	}else{ 
		return true;	
	}
}