jQuery(function($) {
	
	// Adding validation to the registration form basic step
	$("#frmContactUs").validate({
		   
		   submitHandler: function(form) {
		   var options = { 

				target: '',   // target element(s) to be updated with server response 	
				beforeSubmit: showContactRequest,
				success: showContactSuccessResponse,  // post-submit callback 
				url: 'travel_ajax.php',         // override for form's 'action' attribute 
				type: 'post',        // 'get' or 'post', override for form's 'method' attribute 
				clearForm: false        // clear all form fields after successful submit 
			}; 
			$(form).ajaxSubmit(options);
		}
	});
	
	// Adding validation to the registration form basic step
	$("#frmNewsletterSubscription").validate({
		   
		   submitHandler: function(form) {
		   var options = { 

				target: '',   // target element(s) to be updated with server response 	
				beforeSubmit: showRequest,
				success: showSuccessResponse,  // post-submit callback 
				url: 'subscription_ajax.php',         // override for form's 'action' attribute 
				type: 'post',        // 'get' or 'post', override for form's 'method' attribute 
				clearForm: false        // clear all form fields after successful submit 
			}; 
			$(form).ajaxSubmit(options);
		}
	});
	
	
});



// pre-submit callback 
function showRequest(formData, jqForm, options) 
{
	
	$("#emaillNL").html('');
	$("#country").html('');
	$("#msg").html('');	
	var Email 		= $("#frmEmailNL").val();
	var Country 	= $("#frmCountry").val();
	var Operation  	= $("#frmOperation").val();
	var bool = true;
	var regEmail = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
	if(Operation == 'unsub')
	{
		if(Email == '')
		{
			$("#emaillNL").html('Please enter email.');
			bool = false;
		}
		else if(!regEmail.test(Email))	
	    {
			$("#emaillNL").html('Please enter a valid email address.');
			bool = false;
		}
	}
	else
	{
		if(Email == '')
		{
			$("#emaillNL").html('Please enter email.');
			bool = false;
		}
		else if(!regEmail.test(Email))	
	    {
			$("#emaillNL").html('Please enter a valid email address.');
			bool = false;
		}
		else
		{
			$("#emaillNL").html('');	
		}
		if(Country == '')
		{
			$("#country").html('Please enter country.');
			bool = false;
		}
		else
		{
			$("#country").html('');	
		}
		
	}
	$("#frmOperation").val('sub');
	if(bool)
	{
		$("#ajax_image").show();
	}
	return bool;
} 

// post-submit callback 
function showSuccessResponse(responseText, statusText)  
{ 
	
	$("#ajax_image").hide();
	$("#frmOperation").val('sub');
	$("#emaillNL").html('');
	$("#country").html('');
	var arrResponseDetail = responseText.split('|');
	if(arrResponseDetail[1] == 'err')
	{
		$("#emaillNL").html(arrResponseDetail[0]);	
	}
	else
	{
		$("#frmEmailNL").val('');
		$("#frmCountry").val('');
		/*$("#msg").html(arrResponseDetail[0]);	*/
		$("#msg").html('You have subscribed successfully.');
	}
	
} 



// pre-submit callback  
function showContactRequest(formData, jqForm, options) 
{
	$("#city").html('');	
    $("#ddate").html('');
	$("#destination").html('');
	$("#rdate").html('');
	$("#hotel").html('');
	$("#passengers").html('');
	$("#acity").html('');
	$("#address").html('');
	$("#acountry").html('');
	$("#zipCode").html('');
	$("#phone").html('');
	$("#email").html('');
	$("#acountrys").html('');
	var City 	            = $("#frmCity").val();
 	var DepartureDay 		= $("#frmDepartureDay").val();
	var DepartureMonth 		= $("#frmDepartureMonth").val();
	var DepartureYear 		= $("#frmDepartureYear").val();
	var Destination 		= $("#frmDestination").val();
	var ReturnDay 		    = $("#frmReturnDay").val();
	var ReturnMonth 		= $("#frmReturnMonth").val();
	var ReturnYear		    = $("#frmReturnYear").val();
	var Hotel		        = $("#frmPHotel").val();
	var Passenger		    = $("#frmNumberOfTravellers").val();
	var ACity		        = $("#frmACity").val();
	var Adress		        = $("#frmAddress").val();
	var Country	            = $("#frmACountry").val();
	var Zipcode	            = $("#frmZipCode").val();
	var PhoneNumber	        = $("#frmPhone").val();
	var Email	        = $("#frmEmail").val();
	
	//CODE FOR CURRENT DATE
	var currentDate  = document.getElementById('date').value.split("-");
    //alert(FromDate);
	/*********************** From Date *****************/
	var cY = currentDate[0];  //Year
	var cM = currentDate[1];  //Month
	var cD = currentDate[2];  //Date

	for(i=1;i<=Passenger;i++)
	{
		$("#name_"+i).html('');
		$("#dob_"+i).html('');
	}
	
	var bool = true;
	var regEmail = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
	var regNumeric  = /^([0-9]+)$/;
    var regAlphaNum = /^([a-zA-Z0-9_#@]+)$/;  
	if(City == '')
	{
		$("#city").html('Please enter departure city.');
		bool = false;
	}
	if(!(DepartureDay != '' && DepartureMonth !='' && DepartureYear != ''))
	{
		$("#ddate").html('Please enter departure date.');
		bool = false;
	}
	else
	{
		
		if(DepartureYear<cY) 
		{
			$("#ddate").html('Departure date should not be previous date.');
			bool = false;
		}
		else if(cM==DepartureMonth && DepartureDay<cD && cY==DepartureYear) 
		{ 
			$("#ddate").html('Departure date should not be previous date.');
			bool = false;	
		}
		else if(DepartureMonth<cM && DepartureYear==cY)
		{ 
			$("#ddate").html('Departure date should not be previous date.');
			bool = false;		
		}
	}
	
	if(Destination == '')
	{
		$("#destination").html('Please enter Destination.');
		bool = false;
	}
	if(!(ReturnDay != '' && ReturnMonth !='' && ReturnYear != ''))
	{
		$("#rdate").html('Please enter return date.');
		bool = false;
	}
	else
	{
		if(ReturnYear<DepartureYear) 
		{
			$("#rdate").html('Return date should be greater than departure date.');
			bool = false;
		}
		else if(DepartureMonth==ReturnMonth && ReturnDay<DepartureDay && DepartureYear==ReturnYear) 
		{ 
			$("#rdate").html('Return date should be greater than departure date.');
			bool = false;	
		}
		else if(ReturnMonth<DepartureMonth && DepartureYear==ReturnYear)
		{ 
			$("#rdate").html('Return date should be greater than departure date.');
			bool = false;	
		}	
		
	}
	
	if(Hotel == '')
	{
		$("#hotel").html('Please enter hotel.');
		bool = false;
	}
	if(Passenger == '')
	{
		$("#passengers").html('Please select number of travellers.');
		bool = false;
	}
	if(Passenger != '')
	{
		for(i=1;i<=Passenger;i++)
		{
			var name = '#frmName_'+i;
			var bd   = '#frmPasDD_'+i;
			var bm   = '#frmPasMonth_'+i;
			var by   = '#frmPasYear_'+i;
			var BirthdayDay   = $(bd).val();
			var BirthdayMonth = $(bm).val();
			var BirthdayYear  = $(by).val();
			
			if($(name).val() =='')
			{
				$("#name_"+i).html('Please enter name.');
				bool = false;	
			}
			if($(bd).val() !='' || $(bm).val() !='' || $(by).val() !='')
			{
				
				if(!($(bd).val() !='' && $(bm).val() !='' || $(by).val() !=''))
				{
					$("#dob_"+i).html('Please enter date.');
					bool = false;		
				}
				else if(BirthdayYear>cY) 
				{
					$("#dob_"+i).html('Date of Birth should be less than current date.');
					bool = false;	
				}
				else if(cM==BirthdayMonth && BirthdayDay>cD && cY==BirthdayYear) 
				{ 
					$("#dob_"+i).html('Date of Birth should be less than current date.');
					bool = false;	
				}
				else if(BirthdayMonth>cM && BirthdayYear==cY)
				{ 
					$("#dob_"+i).html('Date of Birth should be less than current date.');
					bool = false;	
				}
			}
		}
	}
	/*if(ACity == '')
	{
		$("#acity").html('Please enter city.');
		bool = false;
	}
	if(Adress == '')
	{
		$("#address").html('Please enter address.');
		bool = false;
	}
	*/
	if(Country == '')
	{
		
		$("#acountrys").html('Please select a country.');
		bool = false;
	}
	
	if(Zipcode != '' && !regAlphaNum.test(Zipcode))
	{
		$("#zipCode").html('Zip Code should be alpha-numeric.');
		bool = false;
	}
	if(!regNumeric.test(PhoneNumber))
	{
		$("#phone").html('Phone Number should be numeric.');
		bool = false;	
	}
	if(PhoneNumber == '')
	{
		$("#phone").html('Please enter phone number.');
		bool = false;
	}
	else if(!regNumeric.test(PhoneNumber))
	{
		$("#phone").html('Phone Number  should be numeric.');
		bool = false;	
	}
	if(Email == '')
	{
		$("#email").html('Please enter email.');
		bool = false;
	}
	else if(!regEmail.test(Email))	
	{
			$("#email").html('Please enter a valid email address.');
			bool = false;
	}
	if(bool)
	{
		$("#ajax_image").show();
	}
	return bool;
}


function showContactSuccessResponse(responseText, statusText)
{
	
	$("#ajax_image").hide();
	var City 	            = $("#frmCity").val('');
 	var DepartureDay 		= $("#frmDepartureDay").val('');
	var DepartureMonth 		= $("#frmDepartureMonth").val('');
	var DepartureYear 		= $("#frmDepartureYear").val('');
	var Destination 		= $("#frmDestination").val('');
	var ReturnDay 		    = $("#frmReturnDay").val('');
	var ReturnMonth 		= $("#frmReturnMonth").val('');
	var ReturnYear		    = $("#frmReturnYear").val('');
	var Hotel		        = $("#frmPHotel").val('');
	var ACity		        = $("#frmACity").val('');
	var Adress		        = $("#frmAddress").val('');
	var Country	            = $("#frmACountry").val('');
	var Zipcode	            = $("#frmZipCode").val('');
	var PhoneNumber	        = $("#frmPhone").val('');
	var Email	        = $("#frmEmail").val('');
	
	$("#frmQuestion").val('');
	$("#city").html('');	
    $("#ddate").html('');
	$("#destination").html('');
	$("#rdate").html('');
	$("#hotel").html('');
	$("#passengers").html('');
	$("#acity").html('');
	$("#address").html('');
	$("#acountry").html('');
	$("#zipCode").html('');
	$("#phone").html('');
	$("#email").html('');
	$("#acountrys").html('');
	$("#passenger").html('');
	$("#frmNumberOfTravellers").val('');
	$("#cmsg").html('Your quote details have been sent successfully.');
}

function validateEnquiryForm() 
{
	$("#city").html('');	
    $("#ddate").html('');
	$("#destination").html('');
	$("#rdate").html('');
	$("#hotel").html('');
	$("#passengers").html('');
	$("#acity").html('');
	$("#address").html('');
	$("#acountry").html('');
	$("#zipCode").html('');
	$("#phone").html('');
	$("#email").html('');
	$("#acountrys").html('');
	var City 	            = $("#frmCity").val();
 	var DepartureDay 		= $("#frmDepartureDay").val();
	var DepartureMonth 		= $("#frmDepartureMonth").val();
	var DepartureYear 		= $("#frmDepartureYear").val();
	var Destination 		= $("#frmDestination").val();
	var ReturnDay 		    = $("#frmReturnDay").val();
	var ReturnMonth 		= $("#frmReturnMonth").val();
	var ReturnYear		    = $("#frmReturnYear").val();
	var Hotel		        = $("#frmPHotel").val();
	var Passenger		    = $("#frmNumberOfTravellers").val();
	var ACity		        = $("#frmACity").val();
	var Adress		        = $("#frmAddress").val();
	var Country	            = $("#frmACountry").val();
	var Zipcode	            = $("#frmZipCode").val();
	var PhoneNumber	        = $("#frmPhone").val();
	var Email	        = $("#frmEmail").val();
	
	//CODE FOR CURRENT DATE
	var currentDate  = document.getElementById('date').value.split("-");
    //alert(FromDate);
	/*********************** From Date *****************/
	var cY = currentDate[0];  //Year
	var cM = currentDate[1];  //Month
	var cD = currentDate[2];  //Date

	for(i=1;i<=Passenger;i++)
	{
		$("#name_"+i).html('');
		$("#dob_"+i).html('');
	}
	
	var bool = true;
	var regEmail = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
	var regNumeric = /^([0-9]+)$/; 
     var regAlphaNum = /^([a-zA-Z0-9_#@]+)$/;   
	if(City == '')
	{
		$("#city").html('Please enter departure city.');
		bool = false;
	}
	if(!(DepartureDay != '' && DepartureMonth !='' && DepartureYear != ''))
	{
		$("#ddate").html('Please enter departure date.');
		bool = false;
	}
	else
	{
		
		if(DepartureYear<cY) 
		{
			$("#ddate").html('Departure date should not be previous date.');
			bool = false;
		}
		else if(cM==DepartureMonth && DepartureDay<cD && cY==DepartureYear) 
		{ 
			$("#ddate").html('Departure date should not be previous date.');
			bool = false;	
		}
		else if(DepartureMonth<cM && DepartureYear==cY)
		{ 
			$("#ddate").html('Departure date should not be previous date.');
			bool = false;		
		}
	}
	
	if(Destination == '')
	{
		$("#destination").html('Please enter Destination.');
		bool = false;
	}
	if(!(ReturnDay != '' && ReturnMonth !='' && ReturnYear != ''))
	{
		$("#rdate").html('Please enter return date.');
		bool = false;
	}
	else
	{
		if(ReturnYear<DepartureYear) 
		{
			$("#rdate").html('Return date should be greater than departure date.');
			bool = false;
		}
		else if(DepartureMonth==ReturnMonth && ReturnDay<DepartureDay && DepartureYear==ReturnYear) 
		{ 
			$("#rdate").html('Return date should be greater than departure date.');
			bool = false;	
		}
		else if(ReturnMonth<DepartureMonth && DepartureYear==ReturnYear)
		{ 
			$("#rdate").html('Return date should be greater than departure date.');
			bool = false;	
		}	
		
	}
	
	if(Hotel == '')
	{
		$("#hotel").html('Please enter hotel.');
		bool = false;
	}
	if(Passenger == '')
	{
		$("#passengers").html('Please select number of travellers.');
		bool = false;
	}
	if(Passenger != '')
	{
		for(i=1;i<=Passenger;i++)
		{
			var name = '#frmName_'+i;
			var bd   = '#frmPasDD_'+i;
			var bm   = '#frmPasMonth_'+i;
			var by   = '#frmPasYear_'+i;
			var BirthdayDay   = $(bd).val();
			var BirthdayMonth = $(bm).val();
			var BirthdayYear  = $(by).val();
			
			if($(name).val() =='')
			{
				$("#name_"+i).html('Please enter name.');
				bool = false;	
			}
			if($(bd).val() !='' || $(bm).val() !='' || $(by).val() !='')
			{
				
				if(!($(bd).val() !='' && $(bm).val() !='' || $(by).val() !=''))
				{
					$("#dob_"+i).html('Please enter date.');
					bool = false;		
				}
				else if(BirthdayYear>cY) 
				{
					$("#dob_"+i).html('Date of Birth should be less than current date.');
					bool = false;	
				}
				else if(cM==BirthdayMonth && BirthdayDay>cD && cY==BirthdayYear) 
				{ 
					$("#dob_"+i).html('Date of Birth should be less than current date.');
					bool = false;	
				}
				else if(BirthdayMonth>cM && BirthdayYear==cY)
				{ 
					$("#dob_"+i).html('Date of Birth should be less than current date.');
					bool = false;	
				}
			}
		}
	}
	/*if(ACity == '')
	{
		$("#acity").html('Please enter city.');
		bool = false;
	}
	if(Adress == '')
	{
		$("#address").html('Please enter address.');
		bool = false;
	}
	*/
	if(Country == '')
	{
		
		$("#acountrys").html('Please select a country.');
		bool = false;
	}
	
	if(Zipcode != '' && !regAlphaNum.test(Zipcode))
	{
		$("#zipCode").html('Zip Code should be alpha-numeric.');
		bool = false;
	}
	if(!regNumeric.test(PhoneNumber))
	{
		$("#phone").html('Phone Number should be numeric.');
		bool = false;	
	}
	if(PhoneNumber == '')
	{
		$("#phone").html('Please enter phone number.');
		bool = false;
	}
	else if(!regNumeric.test(PhoneNumber))
	{
		$("#phone").html('Phone Number  should be numeric.');
		bool = false;	
	}
	if(Email == '')
	{
		$("#email").html('Please enter email.');
		bool = false;
	}
	else if(!regEmail.test(Email))	
	{
			$("#email").html('Please enter a valid email address.');
			bool = false;
	}
	if(bool)
	{
		$("#ajax_image").show();
	}
	return bool;
}

function ValidateOurBrochure()
{
 

    $("#name").html('');
    $("#address").html('');
    $("#city").html('');
    $("#country").html('');
    $("#zipcode").html('');
    $("#email").html('');
    var name                    =       $("#frmname").val();
    var city                    =       $("#frmcity").val();
    var address                 =       $("#frmaddress").val();
    var country                 =       $("#frmcountry").val();
    var zipcode                 =       $("#frmzipcode").val();
    var email                   =       $("#frmemail").val();
    var bool = true;
    var regEmail = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
  if(name == '')
    {
        $("#name").html('Please enter name.');
        bool = false;
    }
  
  if(city == '')
    {
        $("#city").html('Please enter city.');
        bool = false;
    }
  if(address == '')
  {
        $("#address").html('Please enter address.');
        bool = false;
  }
  if(country == '')
  {
        $("#country").html('Please select country.');
        bool = false;
  }
 if(zipcode == '')
  {
        $("#zipcode").html('Please enter zipcode.');
        bool = false;
  }
  
  if(email == '')
    {
        $("#email").html('Please enter email.');
        bool = false;
    }
    else if(!regEmail.test(email))    
    {
            $("#email").html('Please enter a valid email address.');
            bool = false;
    }
    return bool;
}
