function xor_str(to_enc){
	var xor_key=6
	var the_res="";
	
	for(i=0;i<to_enc.length;++i){
		the_res+=String.fromCharCode(xor_key^to_enc.charCodeAt(i));
	}
	
	return the_res;
}

function decrypt_str(to_dec){
	var xor_key=6
	var the_res="";
	
	for(i=0;i<to_dec.length;i++){
		the_res+=String.fromCharCode(xor_key^to_dec.charCodeAt(i));
	}
	
	return the_res;
}

function highlightField(field) {
	field.style.background = 'LemonChiffon';
	field.focus();
}

function checkEnquiryForm(enquiryForm) {

	try{
		enquiryForm.firstName.style.background = 'White';
		enquiryForm.lastName.style.background = 'White';
		enquiryForm.dayPhone.style.background = 'White';
		enquiryForm.mobilePhone.style.background = 'White';
		enquiryForm.contactMethod.style.background = 'White';
		enquiryForm.postcode.style.background = 'White';
		enquiryForm.email.style.background = 'White';
		enquiryForm.freeText.style.background = 'White';
		enquiryForm.departureDay.style.background = 'White';
		enquiryForm.departureMonthYear.style.background = 'White';
		enquiryForm.returnDay.style.background = 'White';
		enquiryForm.returnMonthYear.style.background = 'White';
	}catch (e){}

	if (enquiryForm.firstName.value.length == 0) {
		alert("Please enter your first name.");
		highlightField(enquiryForm.firstName);
		return false;
	}

	if (enquiryForm.lastName.value.length == 0) {
		alert("Please enter your surname.");
		highlightField(enquiryForm.lastName);
		return false;
	}
	
	if (enquiryForm.organisation.value.length == 0) {
		alert("Please enter your organisation.");
		highlightField(enquiryForm.organisation);
			return false;
		}
	
	if (enquiryForm.position.value.length == 0) {
	alert("Please enter your position.");
	highlightField(enquiryForm.position);
		return false;
	}

	if (enquiryForm.dayPhone.value.length == 0) {
	alert("Please enter your phone (daytime).");
	highlightField(enquiryForm.dayPhone);
		return false;
	}

	
	try{
		if (!isValidPostcode(enquiryForm.postcode.value)) {
			highlightField(enquiryForm.postcode);
			return false;
		}
	}catch (e){}

	if (!isValidEmail(enquiryForm.email.value)) {
		highlightField(enquiryForm.email);
		return false;
	}
	
	if( ( enquiryForm.existingClient[0].checked == false )
    && ( enquiryForm.existingClient[1].checked == false ) )
    {
        alert ( "Please select whether you have booked with Campus Travel before." );
		highlightField(enquiryForm.existingClient[0]);
        return false;
    }

	// Assume that month and year will be a string in the format of MM/yyyy
	// Also remember that months range from 0..11
	month = new Number(enquiryForm.departureMonthYear.value.substring(0,2))-1;
	year = enquiryForm.departureMonthYear.value.substring(3,7);

	// Assume that day is an integer in the form of a string
	day = enquiryForm.departureDay.value;
	if (!isValidDate(year, month, day)) {
		highlightField(enquiryForm.departureDay);
		highlightField(enquiryForm.departureMonthYear);
		return false;
	}
	departureDate = new Date(year, month, day, 11, 59, 59);

	// Departure date must be after today.
	var date = new Date();
	var fullYear = date.getFullYear();
	var month = date.getMonth()+1;
	var day = date.getDate();
	if (day <= 9) {
		day="0"+day;
	}
	if (month <= 9) {
		month="0"+month;
	}
	var todaysDate = day+"/"+month+"/"+fullYear;
	if (departureDate <= todaysDate) {
		alert('The departure date must be after today.');
		highlightField(enquiryForm.departureDay);
		highlightField(enquiryForm.departureMonthYear);
		return false;
	}

	// If a return date has been given, check its validity.
	if (enquiryForm.returnMonthYear != null) {

		// Assume that month and year will be a string in the format of MM/yyyy
		// Also remember that months range from 0..11
		month = new Number(enquiryForm.returnMonthYear.value.substring(0,2))-1;
		year = enquiryForm.returnMonthYear.value.substring(3,7);

		// Assume that day is an integer in the form of a string
		day = enquiryForm.returnDay.value;
		if (!isValidDate(year, month, day)) {
			highlightField(enquiryForm.returnDay);
			highlightField(enquiryForm.returnMonthYear);
			return false;
		}
		returnDate = new Date(year, month, day, 11, 59, 59);

		// Return date must be AFTER departure date
		if (returnDate <= departureDate) {
			alert('The return date must be after the departure date.');
			highlightField(enquiryForm.returnDay);
			highlightField(enquiryForm.returnMonthYear);
			return false;
		}
	}


	
	if (enquiryForm.freeText.value.length >= 1) {
	var swear_words_arr=new Array(decrypt_str('`sem'), decrypt_str('unor'), decrypt_str('eshr'), decrypt_str('doren'), decrypt_str('qnitc'), decrypt_str('guunijc'), decrypt_str('ujstt'), decrypt_str('ujsr'), decrypt_str('hoaact'), decrypt_str('hoaag'), decrypt_str('aiim'), decrypt_str('dogren'), decrypt_str('dogten'), decrypt_str('boemncgb'), decrypt_str('dsrrncgb'), decrypt_str('eiemncgb'), decrypt_str('`gaair'), decrypt_str('qn6tc'), decrypt_str('eiih'), decrypt_str('riuuct'), decrypt_str('riuug'), decrypt_str('boemqgb'), decrypt_str('ujgvvct'));
	var swear_alert_arr=new Array;
	var swear_alert_count=0;
	
	function reset_alert_count()
	{
	swear_alert_count=0;
	}
	
	reset_alert_count();
	var compare_text=document.enquiryForm.freeText.value;
	for(var i=0; i<swear_words_arr.length; i++)
	{
	 for(var j=0; j<(compare_text.length); j++)
	 {
	  if(swear_words_arr[i]==compare_text.substring(j,(j+swear_words_arr[i].length)).toLowerCase())
	  {
	   swear_alert_arr[swear_alert_count]=compare_text.substring(j,(j+swear_words_arr[i].length));
	   swear_alert_count++;
	  }
	 }
	}
	var alert_text="";
	for(var k=1; k<=swear_alert_count; k++)
	{
	 alert_text+="\n" + "(" + k + ")  " + swear_alert_arr[k-1];
	}
	if(swear_alert_count>0)
	{
	 alert("Please refrain from using bad language");
	 highlightField(enquiryForm.freeText);
	 return false;
	}
	
	window.onload=reset_alert_count;
	}	


	return true;
}

