// Madgex Limited
// Copyright (c) 2004 Madgex Limited. All Rights Reserved.
// Validation - Centaur Engineer
// 12 August 2004
// Version 1.0.0

function ValidateJobApplication()
{
	var empty_fields = "";
	var sTitle = document.ApplyOnline.sTitle.value.Trim();
	var sFirstName = document.ApplyOnline.sFirstName.value.Trim();
	var sLastName = document.ApplyOnline.sLastName.value.Trim();
	var sEmailAddress = document.ApplyOnline.sEmailAddress.value.Trim();
	var sMessage = document.ApplyOnline.sCoverMessage.value.Trim();
	var sCVName = document.ApplyOnline.CVFile.value.Trim();
	
	
	if ( sTitle == '' )
	{
		empty_fields += "\n        - Title";	
		document.ApplyOnline.sTitle.focus();
		document.ApplyOnline.sTitle.select();	
	}
	
	if ( sFirstName == '' )
	{
		empty_fields += "\n        - First Name";	
		if (! empty_fields)
		{
			document.ApplyOnline.sFirstName.focus();
			document.ApplyOnline.sFirstName.select();
		}	
	}
	
	if ( sLastName == '' )
	{
		empty_fields += "\n        - Last Name";	
		if (! empty_fields)
		{
			document.ApplyOnline.sLastName.focus();
			document.ApplyOnline.sLastName.select();
		}
	}
	
	if ( sEmailAddress == '' )
	{
		empty_fields += "\n        - Email Address";	
		if (! empty_fields)
		{
			document.ApplyOnline.sEmailAddress.focus();
			document.ApplyOnline.sEmailAddress.select();
		}
	}
	else
	{
		if ( !CheckValidEmail(sEmailAddress) )
		{
			empty_fields += "\n        - Invalid Email Address";
			if (! empty_fields)
			{
				document.ApplyOnline.elements.value.focus();
			}
		}
	}
	

	if ( sMessage == '' )
	{
		empty_fields += "\n        - Covering Message";	
		if (! empty_fields)
		{
			document.ApplyOnline.sCoverMessage.focus();
			document.ApplyOnline.sCoverMessage.select();
		}
	}	
	
	if (!document.ApplyOnline.bWorkInUK[0].checked && !document.ApplyOnline.bWorkInUK[1].checked)
	{
		empty_fields += "\n        - Eligibility to work in UK";	
	}
	
	if (sCVName == '' && empty_fields == '')
	{
		msg = "You have not added a CV to your application:\n";
		msg += " - Select OK to continue with your application\n";
		msg += " - Select CANCEL to add a CV\n";
		if (confirm(msg)) 
			return true;
		else
		{
			document.ApplyOnline.CVFile.focus();
			document.ApplyOnline.CVFile.select();
			return false;
		}
	}
	
	return alertUser(empty_fields);
}

function ValidateJobSeekerProfile()
{
	var empty_fields = "";
	var sLabel = document.CandidateProfile.sLabel.value.Trim();
	var sEmailAddress = document.CandidateProfile.sEmailAddress.value.Trim();
	var sTitle = document.CandidateProfile.sTitle.value.Trim();
	var sFirstName = document.CandidateProfile.sFirstName.value.Trim();
	var sLastName = document.CandidateProfile.sLastName.value.Trim();
	var sJobFunctions = document.CandidateProfile.sJobFunctions.value.Trim();
	var sJobSectors = document.CandidateProfile.sJobSectors.value.Trim();
	var sJobQualifications = document.CandidateProfile.sJobQualifications.value.Trim();
	var liJobExperienceID = document.CandidateProfile.liJobExperienceID.value.Trim();
	var liCurrentJobLocationID = document.CandidateProfile.liCurrentJobLocationID.value.Trim();
	var sPreferredJobLocations = document.CandidateProfile.sPreferredJobLocations.value.Trim();
	var sJobPositionTypes = document.CandidateProfile.sJobPositionTypes.value.Trim();
	var sEmploymentHistory = document.CandidateProfile.sEmploymentHistory.value.Trim();
	var sSummaryStatement = document.CandidateProfile.sSummaryStatement.value.Trim();
	
	if ( sLabel == '' )
	{
		empty_fields += "\n        - Profile Label";	
		document.CandidateProfile.sLabel.focus();
		document.CandidateProfile.sLabel.select();
	}
	if ( sEmailAddress == '' )
	{
		empty_fields += "\n        - Email Address";
		if (! empty_fields)
		{
			document.CandidateProfile.sEmailAddress.focus();
			document.CandidateProfile.sEmailAddress.select();
		}
	}
	else
	{
		if ( !CheckValidEmail(sEmailAddress) )
		{
			empty_fields += "\n        - Invalid Email Address";
			if (! empty_fields)
			{
				document.CandidateProfile.sEmailAddress.focus();
				document.CandidateProfile.sEmailAddress.select();
			}
		}
	}
	if ( sTitle == '' )
	{
		empty_fields += "\n        - Title";	
		if (! empty_fields)
		{
			document.CandidateProfile.sTitle.focus();
			document.CandidateProfile.sTitle.select();
		}	
	}
	if ( sFirstName == '' )
	{
		empty_fields += "\n        - First Name";	
		if (! empty_fields)
		{
			document.CandidateProfile.sFirstName.focus();
			document.CandidateProfile.sFirstName.select();
		}
	}
	if ( sLastName == '' )
	{
		empty_fields += "\n        - Last Name";	
		if (! empty_fields)
		{
			document.CandidateProfile.sLastName.focus();
			document.CandidateProfile.sLastName.select();
		}
	}
	if ( sJobFunctions == '' )
	{
		empty_fields += "\n        - Preferred job functions";
		if (! empty_fields)
		{
			document.CandidateProfile.sJobFunctions.focus();
			document.CandidateProfile.sJobFunctions.select();
		}
	}
	if ( sJobSectors == '' )
	{
		empty_fields += "\n        - Industry sector";
		if (! empty_fields)
		{
			document.CandidateProfile.sJobSectors.focus();
			document.CandidateProfile.sJobSectors.select();
		}
	}
	if ( sJobQualifications == '' )
	{
		empty_fields += "\n        - Qualifications";
		if (! empty_fields)
		{
			document.CandidateProfile.sJobSectors.focus();
			document.CandidateProfile.sJobSectors.select();
		}
	}
	if ( liJobExperienceID == '' )
	{
		empty_fields += "\n        - Experience";
		if (! empty_fields)
		{
			document.CandidateProfile.liJobExperienceID.focus();
			document.CandidateProfile.liJobExperienceID.select();
		}
	}
	if ( liCurrentJobLocationID == '' )
	{
		empty_fields += "\n        - Current location";
		if (! empty_fields)
		{
			document.CandidateProfile.liCurrentJobLocationID.focus();
			document.CandidateProfile.liCurrentJobLocationID.select();
		}
	}
	if ( sPreferredJobLocations == '' )
	{
		empty_fields += "\n        - Where are you willing to work";
		if (! empty_fields)
		{
			document.CandidateProfile.sPreferredJobLocations.focus();
			document.CandidateProfile.sPreferredJobLocations.select();
		}
	}
	
	if ( sJobPositionTypes == '' )
	{
		empty_fields += "\n        - Preferred position types";
		if (! empty_fields)
		{
			document.CandidateProfile.sJobPositionTypes.focus();
			document.CandidateProfile.sJobPositionTypes.select();
		}
	}
	if ( sEmploymentHistory == '' )
	{
		empty_fields += "\n        - Employment history";
		if (! empty_fields)
		{
			document.CandidateProfile.sEmploymentHistory.focus();
			document.CandidateProfile.sEmploymentHistory.select();
		}
	}
	if ( sSummaryStatement == '' )
	{
		empty_fields += "\n        - Summary statement";
		if (! empty_fields)
		{
			document.CandidateProfile.sSummaryStatement.focus();
			document.CandidateProfile.sSummaryStatement.select();
		}
	}
	
	return alertUser(empty_fields);
	
}

function ValidateEmailAlert()
{
	var empty_fields = "";
	var sJobAlertTitle = document.AddEmailAlert.sJobAlertTitle.value.Trim();
	var sEmailAddress = document.AddEmailAlert.sEmailAddress.value.Trim();
	var sJobDisciplines = document.AddEmailAlert.sJobDisciplines.value.Trim();
	var sJobFunctions = document.AddEmailAlert.sJobFunctions.value.Trim();
	
	if ( sJobAlertTitle == '' )
	{
		empty_fields += "\n        - Job alert title";	
		document.AddEmailAlert.sJobAlertTitle.focus();
		document.AddEmailAlert.sJobAlertTitle.select();
	}
	
	if ( sEmailAddress == '' )
	{
		empty_fields += "\n        - Email Address";
		if (! empty_fields)
		{
			document.AddEmailAlert.sEmailAddress.focus();
			document.AddEmailAlert.sEmailAddress.select();
		}
	}
	else
	{
		if ( !CheckValidEmail(sEmailAddress) )
		{
			empty_fields += "\n        - Invalid Email Address";
			if (! empty_fields)
			{
				document.AddEmailAlert.sEmailAddress.focus();
				document.AddEmailAlert.sEmailAddress.select();
			}
		}
	}

	return alertUser(empty_fields);
}

function ValidatePropertyAdEmailAlert()
{
	var empty_fields = "";
	var sJobAlertTitle = document.AddEmailAlert.sPropertyAdAlertTitle.value.Trim();
	var sEmailAddress = document.AddEmailAlert.sEmailAddress.value.Trim();
	
	
	if ( sJobAlertTitle == '' )
	{
		empty_fields += "\n        - Property alert title";	
		document.AddEmailAlert.sPropertyAdAlertTitle.focus();
		document.AddEmailAlert.sPropertyAdAlertTitle.select();
	}
	
	if ( sEmailAddress == '' )
	{
		empty_fields += "\n        - Email Address";
		if (! empty_fields)
		{
			document.AddEmailAlert.sEmailAddress.focus();
			document.AddEmailAlert.sEmailAddress.select();
		}
	}
	else
	{
		if ( !CheckValidEmail(sEmailAddress) )
		{
			empty_fields += "\n        - Invalid Email Address";
			if (! empty_fields)
			{
				document.AddEmailAlert.sEmailAddress.focus();
				document.AddEmailAlert.sEmailAddress.select();
			}
		}
	}

	return alertUser(empty_fields);
}


function ValidateSearchForm()
{
	var dtPublishedFrom = document.AdvancedSearch.dtPublishedFrom.value;
	var dtPublishedTo = document.AdvancedSearch.dtPublishedTo.value;
	var invalid_Dates = "";

	if (dtPublishedFrom.GetDate() > dtPublishedTo.GetDate())
	{
		invalid_Dates += "\n        - End date must be greater than start date";
		if (! invalid_Dates)
		{
			document.AdvancedSearch.dtPublishedTo.focus();
			document.AdvancedSearch.dtPublishedTo.select();
		}
	}
	
	if (invalid_Dates != '')

		return alertUserDates(invalid_Dates);	
}

function ValidateFeedForm()
{
	var liArticleTypeID = document.FeedSearch.liArticleTypeID.value;
	var liChannelID = document.FeedSearch.liChannelID.value;
	var lWidthOfTable = document.FeedSearch.lWidthOfTable.value;
	var lSizeOfLink = document.FeedSearch.lSizeOfLink.value;
	var lSizeOfFont = document.FeedSearch.lSizeOfFont.value;
	var empty_fields = "";
	
	if ( liArticleTypeID == '' )
	{
		empty_fields += "\n        - Article Type";	
		document.FeedSearch.liArticleTypeID.focus();
	}
	
	if ( liChannelID == '' )
	{
		empty_fields += "\n        - Channel";	
		document.FeedSearch.liChannelID.focus();
	}
	
	if ( lWidthOfTable == '' )
	{
		empty_fields += "\n        - Width of table in pixels";	
		document.FeedSearch.lWidthOfTable.focus();
	}
	
	if ( lSizeOfLink == '' )
	{
		empty_fields += "\n        - Size of link in pixels";	
		document.FeedSearch.lSizeOfLink.focus();
	}
	
	if ( lSizeOfFont == '' )
	{
		empty_fields += "\n        - Size of font in pixels";	
		document.FeedSearch.lSizeOfFont.focus();
	}
	
	return alertUser(empty_fields);
}

function ValidateEmailToAFriend()
{
	var empty_fields = "";
	var sRecipientEmail = document.EmailToAFriend.sRecipientEmail.value.Trim();
	var sSenderEmail = document.EmailToAFriend.sSenderEmail.value.Trim();
	var sMessage = document.EmailToAFriend.sMessage.value.Trim();
	
	if ( sRecipientEmail == '' )
	{
		empty_fields += "\n        - Recipient Email Address";
		if (! empty_fields)
		{
			document.EmailToAFriend.sRecipientEmail.focus();
			document.EmailToAFriend.sRecipientEmail.select();
		}
	}
	else
	{
		if ( !CheckValidEmail(sRecipientEmail) )
		{
			empty_fields += "\n        - Invalid Recipient Email Address";
			if (! empty_fields)
			{
				document.EmailToAFriend.sRecipientEmail.focus();
				document.EmailToAFriend.sRecipientEmail.select();
			}
		}
	}
	
	if ( sSenderEmail == '' )
	{
		empty_fields += "\n        - Sender Email Address";
		if (! empty_fields)
		{
			document.EmailToAFriend.sSenderEmail.focus();
			document.EmailToAFriend.sSenderEmail.select();
		}
	}
	else
	{
		if ( !CheckValidEmail(sSenderEmail) )
		{
			empty_fields += "\n        - Invalid Sender Email Address";
			if (! empty_fields)
			{
				document.EmailToAFriend.sSenderEmail.focus();
				document.EmailToAFriend.sSenderEmail.select();
			}
		}
	}
	
	if ( sMessage == '' )
	{
		empty_fields += "\n        - Message";
		if (! empty_fields)
		{
			document.EmailToAFriend.sMessage.focus();
			document.EmailToAFriend.sMessage.select();
		}
	}

	return alertUser(empty_fields);
}

function ValidatePostAComment()
{
	var empty_fields = "";
	var sRecipientEmail = document.PostComment.sAuthorName.value.Trim();
	var sSenderEmail = document.PostComment.sAuthorEmail.value.Trim();
	var sComment = document.PostComment.sComment.value.Trim();
	
	if ( sRecipientEmail == '' )
	{
		empty_fields += "\n        - Author Name";
		if (! empty_fields)
		{
			document.PostComment.sAuthorName.focus();
			document.PostComment.sAuthorName.select();
		}
	}
	
	if ( sSenderEmail == '' )
	{
		empty_fields += "\n        - Email Address";
		if (! empty_fields)
		{
			document.PostComment.sAuthorEmail.focus();
			document.PostComment.sAuthorEmail.select();
		}
	}
	else
	{
		if ( !CheckValidEmail(sSenderEmail) )
		{
			empty_fields += "\n        - Invalid Email Address";
			if (! empty_fields)
			{
				document.PostComment.sAuthorEmail.focus();
				document.PostComment.sAuthorEmail.select();
			}
		}
	}
	
	if ( sComment == '' )
	{
		empty_fields += "\n        - Comment";
		if (! empty_fields)
		{
			document.PostComment.sComment.focus();
			document.PostComment.sComment.select();
		}
	}
	
	return alertUser(empty_fields);
	
}

function DeleteJobSeekerProfile()
{
	var msg;
	
	msg = "Are you sure you want to delete your profile?";
	
	if (confirm(msg)) 
	{
		return true;
	}
	else
		return false;
	
}

//Functions used by Validation scripts

	function alertUser(empty_fields)
	{
		var msg="";
		
		if (empty_fields){
			msg += "Please enter the following required information\n in order to submit your application:"
					+ empty_fields + "\n";
			alert(msg);	
			return false;
		}else{ 
			return true;	
		}
	}

	function alertUserDates(invalid_Dates)
	{
		var msg="";
		
		if (invalid_Dates){
			msg += "Invalid date range:"
					+ invalid_Dates + "\n";
			alert(msg);	
			return false;
		}else{ 
			return true;	
		}
	}


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;
}

String.prototype.IsWhiteSpace = function()
{
	return this == ' ' || this == '\t';
}

String.prototype.TrimLeft = function()
{
	var i=0;
	
	while (this.charAt(i).IsWhiteSpace())
		i++;
		
	return this.substr(i)
}

String.prototype.TrimRight = function()
{
	var i=this.length;
	
	while (this.charAt(i).IsWhiteSpace())
		i--;
		
	return this.substr(0,i+1)
}

String.prototype.Trim = function()
{
	return this.TrimLeft().TrimRight();
}

function ToggleHowItWorks()
{

	if (document.getElementById( 'HowItWorks' ).style.display == 'none'	)
	{
		document.getElementById( 'HowItWorks' ).style.display='block';
	}
	else
		document.getElementById( 'HowItWorks' ).style.display='none';
		
	//style=\"display:none;\"
}

function SetHowItWorks()
{
	if (document.getElementById( 'HowItWorks' ))
		document.getElementById( 'HowItWorks' ).style.display='none';
}


function IsNumeric(sText)
{	
	var ValidChars = "0123456789." ;
	var IsNumber = true;
	var Char;
	
	for (i = 0; i < sText.length && IsNumber == true; i++) 
	{
    	Char = sText.charAt(i);
    	if (ValidChars.indexOf(Char) == -1)
    	{
    		IsNumber = false;
    	}
    }
    return IsNumber;
 
}  		


function ValidatePropertySearchForm()
{		
	if (document.PropertySearch.mAvailableSizeSqMtrsMin.value != "")
	{
		if (!IsNumeric(document.PropertySearch.mAvailableSizeSqMtrsMin.value)) 
		{
			alert('Please only enter numbers in the Min and Max fields'); 
			document.PropertySearch.mAvailableSizeSqMtrsMin.focus(); 
			return false;
		}
	}
	if (document.PropertySearch.mAvailableSizeSqMtrsMax.value != "")
	{
		if (!IsNumeric(document.PropertySearch.mAvailableSizeSqMtrsMax.value)) 
		{
			alert('Please only enter numbers in the Min and Max fields'); 
			document.PropertySearch.mAvailableSizeSqMtrsMax.focus(); 
			return false;
		}
	}	
	if (document.PropertySearch.mSalePricePerSqMtrMin.value != "")
	{
		if (!IsNumeric(document.PropertySearch.mSalePricePerSqMtrMin.value)) 
		{
			alert('Please only enter numbers in the Min and Max fields');
			document.PropertySearch.mSalePricePerSqMtrMin.focus(); 
			return false;
		}
	}	 
	if (document.PropertySearch.mSalePricePerSqMtrMax.value != "")
	{
		if (!IsNumeric(document.PropertySearch.mSalePricePerSqMtrMax.value)) 
		{
			alert('Please only enter numbers in the Min and Max fields'); 
			document.PropertySearch.mSalePricePerSqMtrMax.focus(); 
			return false;
		}
	} 
 	
	if (parseInt(document.PropertySearch.mAvailableSizeSqMtrsMax.value) < parseInt(document.PropertySearch.mAvailableSizeSqMtrsMin.value))
	{	
		alert('The Max value should be greater than the Min value');
		document.PropertySearch.mAvailableSizeSqMtrsMax.focus();
		return false;
	}					 			


	if (parseInt(document.PropertySearch.mSalePricePerSqMtrMax.value) < parseInt(document.PropertySearch.mSalePricePerSqMtrMin.value))
	{	
		alert('The Max value should be greater than the Min value');
		document.PropertySearch.mSalePricePerSqMtrMax.focus();
		return false;
	}	
	
	return true;
}		

function ValidateEnquiryForm()
{
	var sName = document.EnquiryForm.sName.value.Trim();
	var sCompany = document.EnquiryForm.sCompany.value.Trim();
	var sTel = document.EnquiryForm.sTel.value.Trim();
	var sEmail = document.EnquiryForm.sEmail.value.Trim();
	
	if ( sName == '' )
	{
		alert( "You must enter a name" );
		document.EnquiryForm.sName.focus();
		document.EnquiryForm.sName.select();
		return false;	
	}
	
	if ( sCompany == '' )
	{
		alert( "You must enter a company name" );
		document.EnquiryForm.sCompany.focus();
		document.EnquiryForm.sCompany.select();
		return false;	
	}
	
	if ( sTel == '' )
	{
		alert( "You must enter a telephone number" );
		document.EnquiryForm.sTel.focus();
		document.EnquiryForm.sTel.select();
		return false;	
	}
	
	if ( sEmail == '' )
	{
		alert( "You must enter an email" );
		document.EnquiryForm.sEmail.focus();
		document.EnquiryForm.sEmail.select();
		return false;	
	}
	
	if ( !CheckValidEmail(sEmail) )
	{
		alert( "Please enter a valid email address" );
		document.EnquiryForm.sEmail.focus();
		document.EnquiryForm.sEmail.select();
		return false;	
	}
	
	return true;
}	

