$(function() {

	$("#departure").datepicker({minDate: 1, maxDate: '+3M'});
	$("#return").datepicker({minDate: 2, maxDate: '+3M'});
	
	var model = $("#model"), 
	name = $("#name"), 
	email = $("#email"),
	passengers = $("#passengers"),
	comments = $('#comments'),
	departure = $("#departure"), 
	freturn = $("#return"), 
	from = $("#from"), 
	to = $("#to"), 
	allFields = $([]).add(name).add(email).add(departure).add(freturn).add(from).add(to).add(passengers).add(model), 
	tips = $("#validateTips");

function updateTips(t) {
	tips.text(t).effect("highlight", {}, 1500);
}

function checkLength(o, n, min, max) {
	if (o.val().length > max || o.val().length < min) {
		o.addClass('ui-state-error');
		updateTips(n + " no es válido.");
		return false;
	} else {
		return true;
	}
}

function checkSelected(select) {
	var selectedIndex = select.find("option").index(select.find("option:selected"));
	if(selectedIndex == 0) {
		select.addClass('ui-state-error');
		updateTips("Tienes que seleccionar una opción.");
		return false;
	} else {
		return true;
	}
}

function checkRegexp(o, regexp, n) {
	if (!(regexp.test(o.val()))) {
		o.addClass('ui-state-error');
		updateTips(n);
		return false;
	} else {
		return true;
	}
}

$("#ok-message").dialog({ 
	autoOpen: false,
	buttons: {
		Close: function() {
			$(this).dialog('close');
		}
	}
});	

var center = screen.width / 2;

$("#dialog")
		.dialog(
				{
					bgiframe : true,
					autoOpen : false,
					draggable : false,
					resizable : false,
					height : 480,
					width: 200,
					title: 'Reservar vuelo',
					position: [center-285, 70],
					buttons : {
						'Reservar' : function() {
							var bValid = true;
							allFields.removeClass('ui-state-error');
							bValid = bValid && checkSelected(model);
							bValid = bValid && checkLength(name, "Name", 3, 100);
							bValid = bValid && checkLength(email, "Email", 6, 80);
							bValid = bValid && checkLength(passengers, "Passengers", 1, 3);
							bValid = bValid && checkLength(departure, "Departure", 10, 10);
							bValid = bValid && checkLength(freturn, "Return", 10, 10);
							bValid = bValid && checkLength(from, "From", 2, 20);
							bValid = bValid && checkLength(to, "To", 2, 20);

							// From jquery.validate.js (by joern),
							// contributed by Scott Gonzalez:
							// http://projects.scottsplayground.com/email_address_validation/
							bValid = bValid && checkRegexp(email,
											/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,
											"Email invalid");
							
							if (bValid) {
								var dataString = 'name=' + name.val() + '&email=' + email.val() + '&departure=' + departure.val() + '&freturn=' + freturn.val() + '&from=' + from.val() + '&to=' + to.val() + '&model=' + model.val() + '&passengers=' + passengers.val() + '&comments=' + comments.val();
								
								$.ajax({
									type : "POST",
									url : "contact.php",
										data : dataString,
										success : function() {
											$('#dialog').dialog('close');
											$('#ok-message').dialog('open');
										}
								});
							}
						},
						Cancel : function() {
							$(this).dialog('close');
						}
					},
					close : function() {
						allFields.val('').removeClass('ui-state-error');
					}
				});

$('#book2 img').click(function() {
	$('#dialog').dialog('open');
})
});

