$(document).ready(function() {


	$('a[@rel*=lightbox]').lightBox();
	
});

function open_page(url) {
	window.open(url);
	return false;
}

function open_popup(url) {
	window.open(url, 'bmpp', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=300,height=300,left=570,top=300');
	return false;
}



function send_form_check(idf) {
        $("#form_loading"+idf).slideDown();

	var nm = $("#nome"+idf).attr("value");
	if(nm=='Name') {
		alert("You must enter your Name");
		$("#form_loading"+idf).slideUp();
		return false;
	}
	
	var cgnm = $("#cognome"+idf).attr("value");
	if(cgnm=='Lastname') {
		alert("You must enter your Lastname");
		$("#form_loading"+idf).slideUp();
		return false;
	}
	
	
	var em = $("#email"+idf).attr("value");
	if(!echeck(em)) {
		$("#form_loading"+idf).slideUp();
		return false;
	}
	
	var ai = $("#aeroporto_in"+idf).val();
        if(ai=='0') {
		alert("You must choose the airport where you rent");
		$("#form_loading"+idf).slideUp();
		return false;
	}
        var ai = $("#aeroporto_in"+idf+" option:selected").text();


	var ao = $("#aeroporto_out"+idf).val();
        if(ao=='0') {
		alert("You must choose the airport where you return the car");
		$("#form_loading"+idf).slideUp();
		return false;
	}
        var ao = $("#aeroporto_out"+idf+" option:selected").text();
	
		
	var s = $("#checkin"+idf).attr("value");
	if(s=='Check-in') {
		alert("You must fill in the check-in field");
		$("#form_loading"+idf).slideUp();
		return false;
	}

	var e = $("#checkout"+idf).attr("value");
	if(e=='Check-out') {
		alert("You must fill in the check-out field");
		$("#form_loading"+idf).slideUp();
		return false;
	}

	var s_ary = s.split("/");
	var e_ary = e.split("/");
	if( s_ary[2] > e_ary[2] ){
		alert("L'ANNO inserito in checkin &egrave; maggiore di quello inserito in checkout");
		$("#form_loading"+idf).slideUp();
		return false;
        } else if( s_ary[1] > e_ary[1] ) {
		alert("Il MESE inserito in checkin &egrave; maggiore di quello inserito in checkout");
		$("#form_loading"+idf).slideUp();
		return false;
        } else if( s_ary[2] == e_ary[2] && s_ary[1] == e_ary[1] && s_ary[0] > e_ary[0] ) {
		alert("Il GIORNO inserito in checkin, a parit&agrave� di anno e mese, &egrave; maggiore di quello inserito in checkout");
		$("#form_loading"+idf).slideUp();
		return false;
        }

	var oi = $("#orario_in"+idf).val();
        if(oi=='0') {
		alert("You must choose the starting time of hire");
		$("#form_loading"+idf).slideUp();
		return false;
	}
        var oi = $("#orario_in"+idf+" option:selected").text();


	var oo = $("#orario_out"+idf).val();
        if(oo=='0') {
		alert("You must choose the end time of hire");
		$("#form_loading"+idf).slideUp();
		return false;
	}
        var oo = $("#orario_out"+idf+" option:selected").text();
	
		
	if(!$("#trattamento"+idf).get(0).checked) {
		alert("You must accept the Privacy Policy");
		$("#form_loading"+idf).slideUp();
		return false;
	}


	var co = $("#codice"+idf).attr("value");
	var rr = $("#richiesta"+idf).attr("value");
	rr = (rr && rr != 'Notes' && rr != 'undefined')?rr:"Non specificato";
	var ls = $("#lang"+idf).attr("value");
        ls = ( ls && ls != 'undefined')?ls:"-1";
	
        query = {
		nome: nm,
		cognome: cgnm,
		email: em,
		aeroporto_in: ai,
		aeroporto_out: ao,
		checkin: s,
		checkout: e,
		orario_in: oi,
		orario_out: oo,
		richiesta: rr,
		lang: ls,
		codice: co
	};
	$.post("sendmail.php", query, function(data) {
	
			form_alert(""+data, idf);
	
		
		$("#form_loading"+idf).slideUp();
	});
	
}


function form_alert(msg, idf) {
	$("#form_loading"+idf).fadeOut(function() {
		var a = $("#form_alert"+idf);
		a.html("<font size=\"1\">"+msg+"</font>");
		a.fadeIn();
		setTimeout(function() {
			a.fadeOut(function() {
				$("#form_content"+idf).fadeIn();
			});
		}, 3000);
	});
}

function echeck(str) {

	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   alert("Enter a valid email address")
	   return false
	}

	var invalid = "Enter a valid email address";

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   alert(invalid)
	   return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	    alert("Enter a valid email address")
	    return false
	}

	 if (str.indexOf(at,(lat+1))!=-1){
	    alert("Enter a valid email address")
	    return false
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	    alert("Enter a valid email address")
	    return false
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
	    alert("Enter a valid email address")
	    return false
	 }
	
	 if (str.indexOf(" ")!=-1){
	    alert("Enter a valid email address")
	    return false
	 }

	 return true					
}

