$(document).ready(function() {
	
	/* Suggestion event */
	$("#dp_city").suggest("lib/ctl/airportSuggest.php");
	$("#ar_city").suggest("lib/ctl/airportSuggest.php");
	$("#airline2").suggest("lib/ctl/airlineSuggest.box2.php");
	
	/* Event to change Search default values */
	$("#dp_city").click(function() { if($(this).val() == "Departure...") { $(this).val(""); } });
	$("#dp_city").blur(function() { if($(this).val() == "") { $(this).val("Departure..."); } });
	$("#ar_city").click(function() { if($(this).val() == "Arrival...") { $(this).val(""); } });
	$("#ar_city").blur(function() { if($(this).val() == "") { $(this).val("Arrival..."); } });
	
	/* Event to change Search default values */
	$("#airline2").click(function() { if($(this).val() == "Airline...") { $(this).val(""); } });
	$("#airline2").blur(function() { if($(this).val() == "") { $(this).val("Airline..."); } });
	$("#flightnumber").click(function() { if($(this).val() == "Flight Number...") { $(this).val(""); } });
	$("#flightnumber").blur(function() { if($(this).val() == "") { $(this).val("Flight Number..."); } });
	
	/* Search BOX 1 */
	$("#search_box").bind('submit', function() {
		
		var form = 0;

		if($("#dp_city").val() != "Departure..." || $("#ar_city").val() != "Arrival..." ) {
			form = 1;
		}
		
		if($("#airline2").val() != "Airline..." || $("#flightnumber").val() != "Flight Number..." ) {
			form = 2;
		}
		

		if(form==1) {
		
			if($("#dp_city").val() == "Departure..." || $("#ar_city").val() == "Arrival..." ) {

				$("#dp_city").focus();
				return false;
			}
			$("#loading").show();
			$.post("lib/ctl/setSearch.php", { dateBox1: $("#dateBox1").val(), box1: "ok", dp_city: $("#dp_city").val(), airline1: $("#airline1").val(), ar_city: $("#ar_city").val(), sitekey: $("#sitekey").val() } ,
				function(data) { 	
					if(data=="4") {
						window.top.location.href='./search-result';
					} else {
						$("#loading").hide();
					}
				}
			);
		} else if(form==2) {
			
			if($("#airline2").val() == "Airline..." || $("#flightnumber").val() == "Flight Number..." ) {
				$("#airline2").focus();
				return false;
			}
			$("#loading").show();	
			$.post("lib/ctl/setSearch.php", { dateBox2: $("#dateBox2").val(), box2: "ok", sitekey: $("#sitekey").val(),  airline2: $("#airline2").val(), flightnumber: $("#flightnumber").val() } ,
				function(data) { 

					if(data=="4") {
						window.top.location.href='./search-result';
					} else {
						$("#loading").hide();
						$("#flightnumber").focus();
					}
				
				}
			);
		} else {
			$("#dp_city").focus();
		}
		
		return false;
	});
	
	/* Search BOX 2 */
	$("#search_box2").bind('submit', function() {
		if($("#airline2").val() == "Airline...") {
			 $("#airline2").addClass("formContent_search_al");
			 $("#airline2").focus();
			 return false;
		} else if($("#flightnumber").val() == "Flight Number...") {
			 $("#airline2").removeClass("formContent_search_al");
			 $("#flightnumber").addClass("formContent_search_al");
			 $("#flightnumber").focus();
			 return false;
		}
		$('#search_box1_load').css('visibility','visible');
		$("#airline2").removeClass("formContent_search_al");
		$("#flightnumber").removeClass("formContent_search_al");
		$.post("core/setSearch.php", { dateBox2: $("#dateBox2").val(), box2: $("#box2").val(), sitekey: $("#sitekey").val(),  airline2: $("#airline2").val(), flightnumber: $("#flightnumber").val() } ,
				function(data) { 	
					if(data=="4") {
						window.top.location.href='./Search-Result';
					} else { 
						
						$('#form_login_load').css('visibility','hidden');
					}
				}
		);
		return false;
	});	
	

	/* Auth User */
	$("#authUser").bind('submit', function() {

		if($("#authEmail").val() == "" || /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test($("#authEmail").val()) != "1" ) {
		 	$("#authEmail").focus();
		 	return false;
		}
		if($("#authPassword").val() == "") {
		 	$("#authPassword").focus();
		 	return false;
		}

		$.post("lib/ctl/authUser.php", { redirect: $("#redirect").val(),  authEmail: $("#authEmail").val(), authPassword: $("#authPassword").val() } ,	
			function(data) { 		
				if(data == "0") {
					alert("The email or password you entered is incorrect. Please, try again!");
				} else  { 
					window.top.location.href= './'+data;
				}
			}
		);
		return false;
	});
	
	/* Menu Uer */
	$("#bottomSearch").click(function() { window.top.location.href='./'; });
	$("#personalInfo").click(function() { window.top.location.href='register'; });
	$("#signature").click(function() { window.top.location.href='register'; });
	$("#myContacts").click(function() { 
		CB_Open('href=lib/ctl/myContact.php?dest=dp_city,,width=690,,height=300,,closeonh=off,, ...'); 
		//document.getElementById("CB_CloseWindow").style.visibility = 'hidden';
		document.getElementById("CB_TG").style.visibility = 'hidden';
 	});
	
	$("#myAlerts").click(function() { window.top.location.href='notification'; });
	$("#exit").click(function() { $.post("lib/ctl/exit.php",function(){ window.top.location.href='./'; }); });
	
	
	/* Select FLights */
	$('[id^=sel]').click(function() {
		id = this.id;
		idc = id.replace("sel","");
		flightid = $('#FlightId'+idc).val();
		action = $('#FlightIdAction'+idc).val();
		$(this).html('<img src="lib/tpl/'+tpl+'/images/loading2.gif" border="0" >');
		$.post("lib/ctl/setFlight.php", { flightid: flightid, action: action, arrayid: idc } ,
			function(data) {
				if(data=="1") {
					if(action == "add") {
						$('#FlightIdAction'+idc).val("del");
						$("#"+id).html('<img src="lib/tpl/'+tpl+'/images/selected.png" border="0">');
					} else if(action == "del") {
						$('#FlightIdAction'+idc).val("add");
						$("#"+id).html('<img src="lib/tpl/'+tpl+'/images/select.png" border="0">');
					}
				}
		});
	});
	
	$('span, a').hoverbox();
	
	$("#goHome").click(function() { window.top.location.href='./'; });
	$("#goAlerts").click(function() { window.top.location.href='./notification'; });
	
	
	/* Page notification bottom remove */
	$("[id^=remove_]").click( function() {
		id = this.id.replace("remove_","");
		$.post("lib/ctl/setFlight.php", { flightid: id, action: "del" } ,
				function (data) {
					window.top.location.href = './notification';
		});
	});
	
	$('[id^=earlyOpCK_]').click(function() { 
		id = this.id.replace("earlyOpCK_","");
		value = $(this).is(':checked');
		$.post("lib/ctl/setCheckbox.php?a=earlyOpCK", { flightid: id, value: value } ,
				function(data) {
					return false;
					
		});
	});
	
	$('[id^=lateOpCK_]').click(function() { 
		id = this.id.replace("lateOpCK_","");
		value = $(this).is(':checked');
		$.post("lib/ctl/setCheckbox.php?a=lateOpCK", { flightid: id, value: value } ,
				function(data) {
					return false;
					
		});
	});
	
	$('[id^=cancelOpCK_]').click(function() { 
		id = this.id.replace("cancelOpCK_","");
		value = $(this).is(':checked');
		$.post("lib/ctl/setCheckbox.php?a=cancelOpCK", { flightid: id, value: value } ,
				function(data) {
					return false;
					
		});
	});
	
	$('[id^=arrivalOpCK_]').click(function() { 
		id = this.id.replace("arrivalOpCK_","");
		value = $(this).is(':checked');
		$.post("lib/ctl/setCheckbox.php?a=arrivalOpCK", { flightid: id, value: value } ,
				function(data) {
					return false;
					
		});
	});
	
	$('[id^=idcontact_]').click(function() { 
		id = this.id.replace("idcontact_","");
		value = $(this).val();
		$.post("lib/ctl/setCheckbox.php?a=idcontact", { flightid: id, value: value } ,
				function(data) {
					return false;
					
		});
	});
	
	/* Save notification */
	$('#saveNotification').click( function() {
		$.post("lib/ctl/saveNotification.php", {  action: "del" } ,
				function (data) {
					window.top.location.href = '?message=saved#alert';
		});		
	});
	

	
	/* Notification page */
	$("#accordion").accordion();

	$('[id^=startDate]').editable('lib/ctl/setDate.php?a=startDate', {
	     type: 'datepicker',
	     tooltip: 'CLick to edit...',
	     event: 'click',
	     width: '90px',
	     style: 'inherit'
	});

	$('[id^=endDate]').editable('lib/ctl/setDate.php?a=endDate', {
	     type: 'datepicker',
	     tooltip: 'CLick to edit...',
	     event: 'click',
	     width: '90px',
	     style: 'inherit'
	});
	 

	  $("[id^=earlyOp]").editable("lib/ctl/setTime.php?a=earlyOp", { 
		    indicator : '<img src="lib/ctl/'+tpl+'/images/loading2.gif">',
		    data   : "{'20 Min':'20 Min','40 Min':'40 Min','1 Hour':'1 Hour','2 Hours':'2 Hours','> 2 Hours':'> 2 Hours'}",
		    type   : "select",
		    submit : "OK",
		    style  : "inherit",
		    submitdata : function() {
		      
		    }
		 });

	  $("[id^=lateOp]").editable("lib/ctl/setTime.php?a=lateOp", { 
		    indicator : '<img src="lib/ctl/'+tpl+'/images/loading2.gif">',
		    data   : "{'20 Min':'20 Min','40 Min':'40 Min','1 Hour':'1 Hour','2 Hours':'2 Hours','> 2 Hours':'> 2 Hours'}",
		    type   : "select",
		    submit : "OK",
		    style  : "inherit",
		    submitdata : function() {
		      
		    }
		 });
	  
	  $("[id^=arrivalOp]").editable("lib/ctl/setTime.php?a=arrivalOp", { 
		    indicator : '<img src="lib/ctl/'+tpl+'/images/loading2.gif">',
		    data   : "{'20 Min':'20 Min','40 Min':'40 Min','1 Hours':'1 Hours','2 Hours':'2 Hours','3 Hours':'3 Hours'}",
		    type   : "select",
		    submit : "OK",
		    style  : "inherit",
		    submitdata : function() {
		     
		    }
		 });
		
	$("#contentSearchResult ul li:last-child").addClass("last-item");
	
	
	/* If the user wants to import contacts */ 
	$('#import_msn').bind("click", function() {
		$("#imp_login").css("visibility","visible");
		$("#imp_from_label").html('"MSN"');
		$("#imp_from").val("msn");
	});
	$('#import_gmail').bind("click", function() {
		$("#imp_login").css("visibility","visible");
		$("#imp_from_label").html('"GMail"');
		$("#imp_from").val("gmail");
	});
	$('#import_linkedin').bind("click", function() {
		$("#imp_login").css("visibility","visible");
		$("#imp_from_label").html('"LinkedIN"');
		$("#imp_from").val("linkedin");
	});
	$('#import_yahoo').bind("click", function() {
		$("#imp_login").css("visibility","visible");
		$("#imp_from_label").html('"Yahoo"');
		$("#imp_from").val("yahoo");
	});
	$('#import_facebook').bind("click", function() {
		$("#imp_login").css("visibility","visible");
		$("#imp_from_label").html('"Facebook"');
		$("#imp_from").val("facebook");
	});
	$('#import_myspace').bind("click", function() {
		$("#imp_login").css("visibility","visible");
		$("#imp_from_label").html('"MySpace"');
		$("#imp_from").val("myspace");
	});
	
	/* Submit import contacts */
	$("#imp_submit").bind("click", function() {
		$("#imp_result").html("<img src='../images/loading.gif'>");
		imp_from = $("#imp_from").val();
		imp_email = $("#imp_email").val();
		imp_password = $("#imp_password").val();
		$.post("importContact.php",{
			imp_from: imp_from, imp_email: imp_email, imp_password: imp_password
		},function(data) {
			result = $("#imp_result").load("importContact.results.php");
		});
	});
	
	
	/* Save page contacts */
	$("[id^=confirm_]").click(function() {
		// get the looping id
		id = this.id.replace("confirm_","");
		
		// get the id from db
		id_db_input = $("[id^=name__"+id+"]").attr("id");
		id_db = id_db_input.replace("name__"+id+"__","");
		//get the values
		name = $("[id^=name__"+id+"]").val();
		type = $("[id^=type__"+id+"]").val();
		content = $("[id^=content__"+id+"]").val();
		// validation
		if(name == "") {
			$("[id^=name__"+id+"__]").addClass("contact_p_al");
			$("[id^=name__"+id+"__]").focus();
		} else if(type == "email" && /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(content) == false) {
			$("[id^=name__"+id+"__]").removeClass("contact_p_al");
			$("[id^=content__"+id+"__]").addClass("contact_p_al");
			$("[id^=content__"+id+"__]").focus();		
		} else if(content == "") {
			$("[id^=name__"+id+"__]").removeClass("contact_p_al");
			$("[id^=content__"+id+"__]").addClass("contact_p_al");
			$("[id^=content__"+id+"__]").focus();	
		} else { 
			$("[id^=name__"+id+"__]").removeClass("contact_p_al");
			$("[id^=content__"+id+"__]").removeClass("contact_p_al");
			$.post("myContactSave.php?action=addContact", { id_db: id_db, name: name, type: type, content: content },
					function(data) { 
						window.location.reload();
				
			});
		}
	});
	/* Option del contact */
	$("[id^=del__]").click(function() {
		// get the id from db
		id_db = this.id;
		id_db = id_db.replace("del__","");
		if(confirm("Are you sure?")) { 
			$.post("myContactSave.php?action=delContact", { id_db: id_db },
					function(data) { 
						window.location.reload();
				
			});
		}
	});
	/* code inputy */ 
	$("[id^=code_]").click(function() { if($(this).val() == "Code...") { $(this).val(""); } });
	$("[id^=code_]").blur(function() { if($(this).val() == "") { $(this).removeClass("code_al"); $(this).val("Code..."); } });
	$("[id^=code_]").keyup(function(event) {
		id = this.id.replace("code_","");
		content = $(this).val();
		if(content.length == "4") {
			$(this).removeClass("code_al");
			$(this).addClass("code");
			code = $(this).val();
			$.post("myContactSave.php?action=valCode",{ code: code, id: id}, function(data) { 
				if(data == 1) {
					window.location.reload();
				} else if(data == 2) {
					$("#code_"+id).addClass("code_al");
				}
			});
		} else if(content.length > "4") {
			$(this).addClass("code_al");
		}
	});
	
	/* Validation BOX contact */
	$('#cssdropdown li.headlink').hover(
			function() { $('ul', this).css('display', 'block'); },
			function() { $('ul', this).css('display', 'none'); });

	/* Set mask by selected type contact */
	$("[id^=type__]").change(function() { 
		type = $("#"+this.id+" option:selected").val();
		id = this.id.replace("type__","");
		if(type == "email") {
			$("#content__"+id).unsetMask().val(''); 
			$("#content__"+id).focus();
		} else {
			$("#content__"+id).setMask("999-999-9999");  	
			$("#content__"+id).focus();  	
		}
	});
	


	/* LINK PAYMENT */
	$("#pay").click(function() {
		CB_Open('href=lib/ctl/payment.php?action=pay,,width=780,,height=400,,closeonh=off,, ...'); 
		document.getElementById("CB_CloseWindow").style.visibility = 'hidden';
		document.getElementById("CB_TG").style.visibility = 'hidden';
	});
	
	
	
	/* Validate Add User */
	$("#formUser").bind('submit', function() { 	
		if($("#name").val() == "") {
			$("#name").addClass("formUserReg_al");
			$("#name").focus();
			return false;
		} else if($("#last_name").val() == "") {
				$("#name").removeClass("formUserReg_al");
				$("#last_name").addClass("formUserReg_al");
				$("#last_name").focus();
				return false;
		} else if(($("#email").val() == "" || /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test($("#email").val()) != "1") && $("#action").val() == "insert") {
			$("#name").removeClass("formUserReg_al");
			$("#last_name").removeClass("formUserReg_al");
			$("#email").addClass("formUserReg_al");
			$("#email").focus();
			return false;
		} else if($("#email_confirm").length == 1 && ($("#email").val() != $("#email_confirm").val())) {
			$("#email_confirm").addClass("formUserReg_al");
			$("#email_confirm").focus();
			return false;
		} else if($("#password").val() == "" && $("#action").val() == "insert") {
			$("#name").removeClass("formUserReg_al");		
			$("#last_name").removeClass("formUserReg_al");
			$("#email").removeClass("formUserReg_al");
			$("#email_confirm").removeClass("formUserReg_al");
			$("#password").addClass("formUserReg_al");
			$("#password").focus();
			return false;
		} else if($("#password_confirm").val() == "" && $("#action").val() == "insert") {
			$("#name").removeClass("formUserReg_al");	
			$("#last_name").removeClass("formUserReg_al");
			$("#email").removeClass("formUserReg_al");
			$("#password").removeClass("formUserReg_al");
			$("#password_confirm").addClass("formUserReg_al");
			$("#password_confirm").focus();
			return false;
		} else if($("#password").val() != $("#password_confirm").val() && $("#action").val() == "insert") {
			$("#password").addClass("formUserReg_al");
			$("#password_confirm").addClass("formUserReg_al");
			$("#password_match").html("The password is different!");
			$("#password_confirm").focus();
			return false;
		} else if($("#password").val() != "" && $("#action").val() == "update") {
			if($("#password").val() != $("#password_confirm").val()) {
				$("#password").addClass("formUserReg_al");
				$("#password_confirm").addClass("formUserReg_al");
				$("#password_confirm").focus();
				return false;
			}
		} else if($("#address1").val() == ""  ) {
			$("#name").removeClass("formUserReg_al");	
			$("#last_name").removeClass("formUserReg_al");
			$("#email").removeClass("formUserReg_al");
			$("#password").removeClass("formUserReg_al");
			$("#password_confirm").removeClass("formUserReg_al");
			$("#address1").addClass("formUserReg_al");
			$("#address1").focus();
			return false;
		} else if($("#city").val() == ""  ) {
			$("#name").removeClass("formUserReg_al");
			$("#last_name").removeClass("formUserReg_al");
			$("#email").removeClass("formUserReg_al");
			$("#password").removeClass("formUserReg_al");
			$("#password_confirm").removeClass("formUserReg_al");
			$("#address1").removeClass("formUserReg_al");
			$("#zipcode").removeClass("formUserReg_al");
			$("#city").addClass("formUserReg_al");
			$("#city").focus();
			return false;
		} else if($("#state").val() == ""  ) {
			$("#name").removeClass("formUserReg_al");	
			$("#last_name").removeClass("formUserReg_al");
			$("#email").removeClass("formUserReg_al");
			$("#password").removeClass("formUserReg_al");
			$("#password_confirm").removeClass("formUserReg_al");
			$("#address1").removeClass("formUserReg_al");
			$("#zipcode").removeClass("formUserReg_al");
			$("#city").removeClass("formUserReg_al");
			$("#state").addClass("formUserReg_al");
			$("#state").focus();
			return false;
		} else if($("#zipcode").val() == ""  ) {
			$("#name").removeClass("formUserReg_al");
			$("#last_name").removeClass("formUserReg_al");
			$("#email").removeClass("formUserReg_al");
			$("#password").removeClass("formUserReg_al");
			$("#password_confirm").removeClass("formUserReg_al");
			$("#address1").removeClass("formUserReg_al");
			$("#zipcode").addClass("formUserReg_al");
			$("#zipcode").focus();
			return false;
		} else if($("#cell_phone").val() == "") {
			$("#name").removeClass("formUserReg_al");
			$("#last_name").removeClass("formUserReg_al");
			$("#email").removeClass("formUserReg_al");
			$("#password").removeClass("formUserReg_al");
			$("#password_confirm").removeClass("formUserReg_al");
			$("#address1").removeClass("formUserReg_al");
			$("#zipcode").removeClass("formUserReg_al");
			$("#cell_phone").addClass("formUserReg_al");
			$("#cell_phone").focus();
			return false;
		}
		if($("#action").val() == "insert") {
			file = "addUser.php";
		} else {
			file = "upUser.php";
		}
		
		$.post("lib/ctl/"+file, { 
			id_package: $("#package").val(), cell_phone: $("#cell_phone").val(), 
			auth: $("#auth").val(), name: $("#name").val(), last_name: $("#last_name").val(), 
			email: $("#email").val(), password: $("#password").val(),
			city: $("#city").val(),state: $("#state").val(),
			address1: $("#address1").val(), address2: $("#address2").val(), zipcode: $("#zipcode").val()
		},function(data) {
			if(data == 10) { // if email already used
				$("#email").addClass("formUserReg_al");
				$("#email_used").html("This email is already in use!");
				return false;
			} else if(data == 1) { // if all is ok
				$("#name").removeClass("formUserReg_al");
				$("#last_name").removeClass("formUserReg_al");
				$("#email").removeClass("formUserReg_al");
				$("#email_confirm").removeClass("formUserReg_al");
				$("#password").removeClass("formUserReg_al");
				$("#password_confirm").removeClass("formUserReg_al");
				$("#address1").removeClass("formUserReg_al");
				if($("#action").val() == "insert") {
					//$("#alert").addClass("alert_red");
		
					$("#alert span").html("Ok, you have been registered with us. Please Log In using your credentials. Thank You!");
					$("#prevnext").html("");

					
					// Clear all fields
					$("#name").val('');
					$("#last_name").val('');
					$("#email").val('');
					$("#email_confirm").val('');
					$("#password").val('');
					$("#password_confirm").val('');
					$("#address1").val('');
					$("#address2").val('');
					$("#city").val('');
					$("#state").val('');
					$("#zipcode").val('');
					$("#cell_phone").val('');
					$("#email_used").html("");
					window.top.location.href='./register#alert';
				}
			} else {
				window.top.location.href="./register";		
				return false;
			}
			return false;
		});
		return false;
	});
	$('#formUserSubmit').click(function() { $('#formUser').submit(); });
	
	// Send new password
	$('#forget').click(function() { 
		if($("#authEmail").val() == "") {
			alert("Please, enter your email!");
			$("#authEmail").focus();
		} else {
			email = $("#authEmail").val();
			$.post("lib/ctl/setPassword.php",{ email: email }, function(data) {
				alert("Please, check your email. We sent the instructions!")
			});
		}
	});

});



// Open Map real status
function openMap(map_url,sdep_time,sarr_time,rdep_time,rarr_time,dep_gate,arr_gate) {
	CB_Open('href=lib/ctl/trackRealTime.php?map='+map_url+'&sdep_time='+sdep_time+'&sarr_time='+sarr_time+'&rdep_time='+rdep_time+'&rarr_time='+rarr_time+'&dep_gate='+dep_gate+'&arr_gate='+arr_gate+',,width=505,,height=425,,closeonh=off,, ...'); 
	//document.getElementById("CB_CloseWindow").style.visibility = 'hidden';
	document.getElementById("CB_TG").style.visibility = 'hidden';		
}
