// JavaScript Document

$(function() {
//	$.datepick.setDefaults({useThemeRoller: true});
//	$('#arrival').datepick();

	$('#arrival').datepick({minDate: 0,  
    showOn: 'both', buttonImageOnly: true, buttonImage: 'images/calendar.gif', dateFormat: 'mm/dd/yy', appendText: '(mm/dd/yyyy)', mandatory: true, closeAtTop: false});

	$('#departure').datepick({minDate: 0,  
    showOn: 'both', buttonImageOnly: true, buttonImage: 'images/calendar.gif', dateFormat: 'mm/dd/yy', appendText: '(mm/dd/yyyy)', mandatory: true, closeAtTop: false});




$('#frmInsert').validate({ 

   meta: "validate",
   submitHandler: function() { 
		form.submit();
   		//alert("Submitted!") 
   },

	errorPlacement: $.datepick.errorPlacement,
	rules: { 
        arrival: {
			dpMinDate: [],
			required: true
		}, 
        departure: {
			dpMaxDate: [],
			required: true
		}, 
		
        nombre: "required",
		formtype: "required",
        apellido: "required",
        pais: "required",
        telefono: "required",
		email: {
			required: true,
			email: true
		},
		adultos: {
			required: function(element) {
			return $("#adultos").val() == 0
		   }
		}
    }, 
    messages: { 
        arrival: 'Please enter a valid date range', 
		formtype: 'Please select the type of request',
        departure: 
			{ 
				required: 'Please enter a valid date range'
			},
				
		nombre: 'Please, include your name',
		apellido: 'Also, it\'s important for us your last name',
		pais: 'We would like to know where you are comming from',
		telefono: 'Please, include your phone number',
		email: 'The email address is not valid... please check and try again',
		adultos: 'Check the number of people traveling'
    }

});


$("#arrival").change(function()
{
	ValidarDatos();
});

$("#departure").change(function()
{
	ValidarDatos();
});

$("#frmInsert").submit(function()
{
	ValidarDatos();	
});



function ValidarDatos()
{
	if ( ($("#departure").val() != "") && ( $("#arrival").val() != "") )
	{
		var fecha = $("#departure").val();
		fechaDeparture = Date.parse(fecha);
	
		var fecha2 = $("#arrival").val();
		fechaArrival = Date.parse(fecha2);
				
		if (fechaArrival == fechaDeparture)
		{
					$("#depError").show(500);
				    $("#depError").html("Dates can not be the same");
					return false;
		}
		else
		{
			if (fechaArrival > fechaDeparture)
				{
					$("#depError").show(500);
				    $("#depError").html("Arrival Date can not greater than Departure date");
					return false;
				}
			else
				{
					$("#depError").hide(1000);
				    $("#depError").html("");
					return true;
				}
		}
	}
}
// Final
});


