/* Validate forms
Copyright Barry Scheepers 2007 <barry @e-smurf.com> All rights Reserved.
*/

$(document).ready(
	function()
	{
		$('#cnt_vcode').change( checkContactCode );
		$('#res_vcode').change( checkReservationCode );
	}
);

/* AJAX to dynamically check provided code...*/
function checkContactCode()
{
	//Get the entered code
	var sCodeField = document.getElementById('cnt_vcode');

	//Check/Validate the input.
	if (sCodeField.value == null || sCodeField.value == "" || /^\s+$/.test(sCodeField.value))
	{
		alert("Please enter security code shown in image.");
		sCodeField.focus();
		return false;
	}

	if (sCodeField.value.length < 5)
	{
		alert('The code entered does not match the code in the image. Please try again.');
		return false;
	}

	var sCryptField = document.getElementById('cnt_crypt_code');


	//Send AJAX request and get response...
	$.post("contact.php", {op: "val", code: sCodeField.value, crypt: sCryptField.value}, function(xml)
	{
	   // format and output result
	   //alert(xml);
	   var returnedStatus = $("status", xml).text();

	   if (returnedStatus != 'ok')
	   {
			//$('#cnt_code_status').val('error'); //???????????????????
			$('#cnt_vcode').css('color', '#FF0000');
			alert('The code entered does not match the code in the image. Please try again.');
			sCodeField.focus();
	   }
	   else
	   {
			// $('#code_status').val('ok'); //??????????????????????
			$('#cnt_vcode').css('color', '#0A8C16');
	   }

	 });
}

/* AJAX to dynamically check provided code...*/
function checkReservationCode()
{
	//Get the entered code
	var sCodeField = document.getElementById('res_vcode');

	//Check/Validate the input.
	if (sCodeField.value == null || sCodeField.value == "" || /^\s+$/.test(sCodeField.value))
	{
		alert("Please enter security code shown in image.");
		sCodeField.focus();
		return false;
	}

	if (sCodeField.value.length < 5)
	{
		alert('The code entered does not match the code in the image. Please try again.');
		return false;
	}

	var sCryptField = document.getElementById('res_crypt_code');


	//Send AJAX request and get response...
	$.post("contact.php", {op: "val", code: sCodeField.value, crypt: sCryptField.value}, function(xml)
	{
	   // format and output result
	   //alert(xml);
	   var returnedStatus = $("status", xml).text();

	   if (returnedStatus != 'ok')
	   {
			//$('#cnt_code_status').val('error'); //???????????????????
			$('#res_vcode').css('color', '#FF0000');
			alert('The code entered does not match the code in the image. Please try again.');
			sCodeField.focus();
	   }
	   else
	   {
			// $('#code_status').val('ok'); //??????????????????????
			$('#res_vcode').css('color', '#0A8C16');
	   }

	 });
}

function validateContact()
{
	var sFullName = document.getElementById("cnt_name");
	var sTelNumber = document.getElementById("cnt_tel");
	var sEmailAddress = document.getElementById("cnt_email");
	var sCode = document.getElementById("cnt_vcode");

	//Check Verification code field
	if (sCode.value == "" || /^\s+$/.test(sCode.value))
	{
		alert("Please enter security code shown in image.");
		sCode.focus();
		return false;
	}

	//Check Full Name field
	if (sFullName.value == "" || /^\s+$/.test(sFullName.value))
	{
	  alert("Please enter your full name.");
	  sFullName.focus();
	  return false;
	}

	//Check Telephone field
	if (sTelNumber.value == "" || /^\s+$/.test(sTelNumber.value))
	{
	  alert("Please enter your telephone number.");
	  sTelNumber.focus();
	  return false;
	}

	//Check Email field
	if (sEmailAddress.value == "" || /^\s+$/.test(sEmailAddress.value))
	{
		alert("Please enter your email address.");
		sEmailAddress.focus();
		return false;
	}

	//Check for valid email address field
	if (!/^[\w\.\-]+@([\w\-]+\.)+[a-zA-Z]+$/.test(sEmailAddress.value))
	{
		alert("Invalid email address supplied. Please provide a valid email address.");
		sEmailAddress.focus();
		return false;
	}

	return true;
}

function validateReservation()
{
	var sFullName = document.getElementById("res_name");
	var sTelNumber = document.getElementById("res_tel");
	var sEmailAddress = document.getElementById("res_email");
	var sCode = document.getElementById("res_vcode");

	//Check Verification code field
	if (sCode.value == "" || /^\s+$/.test(sCode.value))
	{
		alert("Please enter security code shown in image.");
		sCode.focus();
		return false;
	}

	//Check Full Name field
	if (sFullName.value == "" || /^\s+$/.test(sFullName.value))
	{
	  alert("Please enter your full name.");
	  sFullName.focus();
	  return false;
	}

	//Check Telephone field
	if (sTelNumber.value == "" || /^\s+$/.test(sTelNumber.value))
	{
	  alert("Please enter your telephone number.");
	  sTelNumber.focus();
	  return false;
	}

	//Check Email field
	if (sEmailAddress.value == "" || /^\s+$/.test(sEmailAddress.value))
	{
		alert("Please enter your email address.");
		sEmailAddress.focus();
		return false;
	}

	//Check for valid email address field
	if (!/^[\w\.\-]+@([\w\-]+\.)+[a-zA-Z]+$/.test(sEmailAddress.value))
	{
		alert("Invalid email address supplied. Please provide a valid email address.");
		sEmailAddress.focus();
		return false;
	}

	return true;
}
