﻿jQuery.fn.resetForm = function () {
  $(this).each (function() { this.reset(); });
}
function restrictDates(dateStr) {
	if (this.id == 'datepicker_from') {
		$('#datepicker_to').datepicker('option', 'minDate', parseInt(days_between($('#datepicker_from').datepicker('getDate'), new Date())));
		$('#datepicker_to').datepicker('setDate', '+ '+parseInt(days_between($('#datepicker_from').datepicker('getDate'), new Date())+2));
	}else {
		$('#datepicker_from').datepicker('option', 'maxDate', days_between($('#datepicker_to').datepicker('getDate'), new Date()));
	}
}


function days_between(from_date, to_date) {
	var ONE_DAY = 1000 * 60 * 60 * 24;
	var from_ms = from_date.getTime();
	var to_ms = to_date.getTime();
	var difference_ms = from_ms - to_ms;
      //  return Math.round(difference_ms/ONE_DAY)+1;
	    return Math.floor(difference_ms/ONE_DAY)+1;
}
function toggleFlightDetails(elemid){
	$("#"+elemid).toggle(200);
}
function getTime(hours, minutes) {
	hours = hours + ""; 
	minutes = minutes + "";
	if (hours.length == 1) {
		hours = "0" + hours;
	}
	if (minutes.length == 1) {
		minutes = "0" + minutes;
	}
	return hours + ":" + minutes;
}
function checkPasNum(){
	if (parseInt($("#passengersAdult").val()) + parseInt($("#passengersChild").val()) + parseInt($("#passengersInfant").val()) > 9){
		
		alert('Maximum 9 passengers allowed.');
		
		return false;
	}else{
		if (parseInt($("#passengersAdult").val()) <  parseInt($("#passengersInfant").val())){
			
			alert('The number of infants can not transcend the number of adults');
			
			return false;	
		}else{
			if (($("#ap_from").val() == $("#ap_to").val())&&(($("input[name='isRoundtrip']:checked").val() == 0)||($("input[name='isRoundtrip']:checked").val() == 1))){
				
				alert('The departure and the arrival must be different'); // jp
				
				return false;	
			}else{
				return true;
			}
		}
	}
}




///gia iframes 
function calcHeight(ele)
{
var the_height= ele.contentWindow.document.body.scrollHeight;
ele.height=the_height;
}

function calcHeight2()
{
//find the height of the internal page
var the_height=
document.getElementById('the_iframe').contentWindow.
document.body.scrollHeight;

//change the height of the iframe
document.getElementById('the_iframe').height=
the_height;
}

   function addzero( value )
   {
      while( value.length<2 ) value = String("0") + value;
      return value;
   }

function checkDateOrder(frm, ci_day, ci_month_year, co_day, co_month_year) {
	if (document.getElementById) {
		var frm = document.getElementById(frm);
		// create date object from checkin values
		// set date to 12:00 to avoid problems with one
		// date being wintertime and the other summertime
		var my = frm[ci_month_year].value.split("-");
	    var ci = new Date (my[0], my[1]-1, frm[ci_day].value, 12, 0, 0, 0);

        // create date object from checkout values
	    my = frm[co_month_year].value.split("-");
	    var co = new Date (my[0], my[1]-1, frm[co_day].value, 12, 0, 0, 0);

		// if checkin date is at or after checkout date,
		// add a day full of milliseconds, and set the
		// selectbox values for checkout date to new value
	    if (ci >= co){
    	    co.setTime(ci.getTime() + 1000 * 60 * 60 * 24);
	        frm[co_day].value =  co.getDate();
    	    var com = co.getMonth()+1;
	        frm[co_month_year].value = co.getFullYear() + "-" + com;
    	}
	}
}


$(document).ready(function() {
	///autocomplete
	$('#ap_from').focus();
	$('#ap_from').autocomplete({
		serviceUrl:'/qsfnew/query.php?l=en',
    	minChars:3, 
    	maxHeight:400,
		width:300,
		zIndex: 9999,
		onSelect: function(value, data){ $('#depval').val(data); $('#ap_to').focus(); }
	})
	$('#ap_to').autocomplete({
		serviceUrl:'/qsfnew/query.php?l=en',
    	minChars:3, 
    	maxHeight:400,
		width:300,
		zIndex: 9999,
		onSelect: function(value, data){ $('#arrval').val(data); }
	})
	$( "#datepicker_from" ).datepicker({
		showOtherMonths: true,
		selectOtherMonths: true, 
		numberOfMonths: 2,
		showButtonPanel: true,
		minDate: 0,
		showOn: "both",
		buttonImage: "/qsfnew/images/calendar.png",
		buttonImageOnly: true,
		onSelect: restrictDates
	});
	$( "#datepicker_from" ).datepicker( "setDate" , +2 );
	$( "#datepicker_to" ).datepicker({
		showOtherMonths: true,
		selectOtherMonths: true,
		numberOfMonths: 2,
		minDate: 0,
		showOn: "both",
		buttonImage: "/qsfnew/images/calendar.png",
		buttonImageOnly: true,
		showButtonPanel: true,
		onSelect: restrictDates
	});
	$( "#datepicker_to" ).datepicker( "setDate" , "+9d" );
	$('.ui-datepicker-trigger').addClass('calendar');
		
	///hide/show if get_isRoundtrip aplo
	
		$("input[name='isRoundtrip']").change(function(){
		
	///aplo
		if ($("#simple").attr("checked") && $("#simple").attr("checked") != 'undefined'){
			$("#returnDate").hide(100);
			$("#three").attr("disabled", "disabled");
		}else{
			//met' epistrofhs
			$("#returnDate").show(100);
			$("#showextendedDates").show(100);
			$("#three").removeAttr("disabled");
		}
		$.uniform.update();
	});
	$("input[name='trip']").change(function(){
		if ($("#three").attr("checked") && $("#three").attr("checked") != 'undefined'){
			$("#extendedDates").attr("value", "1");
		}else{
			$("#extendedDates").attr("value", "0");
		}
	});
	///modal gia dep
	/*
	$('#airDepList').click(function() {
		var dialog = $('<div style="display:hidden" id="modalwin"></div>').appendTo('body');
			dialog.load(
					'/qsfnew/query.php?airp=all&la=2', 
					{},
					function (responseText, textStatus, XMLHttpRequest) {
						dialog.dialog({modal: true,width: 800,height: 600});
					}
			);
	});
	*/
	$('.destAirmodal').live('click',function(){
		$('#ap_from').val($(this).html());
		$('#depval').val($(this).attr('title'));
		$('#modalwin').remove();
		$.uniform.update();
	});
	///modal gia arr
	/*
	$('#airArrList').click(function() {
		var dialog = $('<div style="display:hidden" id="modalwin"></div>').appendTo('body');
		dialog.load(
				'/qsfnew/query.php?airp=all&la=2&arr=true', 
				{},
				function (responseText, textStatus, XMLHttpRequest) {
					dialog.dialog({modal: true,width: 800,height: 600});
				}
		);
	});
	*/
	$('.arrAirmodal').live('click',function(){
			$('#ap_to').val($(this).html());
			$('#arrval').val($(this).attr('title'));
			$('#modalwin').remove();
			$.uniform.update();
	});
	///check if passenger number <= 9
	$("#passengersAdult").change(checkPasNum);
	$("#passengersChild").change(checkPasNum);
	$("#passengersInfant").change(checkPasNum);
	var validator = $("#flightSearchQuery").validate({
		rules: {
			dep1: "required",
			arr1: "required",
			dep: "required",
			arr: "required",
			obDate: "required"
		},
		messages: {
			dep1: "Fill from",
			arr1: "Fill to",
			dep: "Fill from",
			arr: "Fill to",
			obDate: "Fill the date"
		},
		submitHandler: function(form) {
			if ($('#directFlightsOnly:checked').val() !== undefined) {
				directflights_val = 1;
			}else{
				directflights_val = 0;
			}
			$.cookie('airsearch', 'dep1:'+$( "#ap_from" ).val()+'&dep:'+$( "#depval" ).val()+'&arr1:'+$( "#ap_to" ).val()+'&arr:'+$( "#arrval" ).val()+'&obDate:'+$( "#datepicker_from" ).val()+'&ibDate:'+$( "#datepicker_to" ).val()+'&extendedDates:'+$( "#extendedDates" ).val()+'&passengersAdult:'+$( "#passengersAdult" ).val()+'&passengersChild:'+$( "#passengersChild" ).val()+'&passengersInfant:'+$( "#passengersInfant" ).val()+'&directFlightsOnly:'+directflights_val+'&isRoundtrip:'+$( "input[name=isRoundtrip]:checked" ).val()+'&class='+$( "#class1" ).val(), {path:'/'});
			form.submit();
		},
		errorPlacement: function(error, element) {
					error.appendTo( element.parent().last() );
			}
		});
		$.validator.addMethod("return_date_air", function(value, element) {
			return ($("#simple").attr("checked") &&  $("#simple").attr("checked") != 'undefined') || (element.value != '') ;
		}, "Fill the date");	
		///end of form validation
});///end domready


$(function() {
	$('no#infant_flight_info').tooltip({
		left:-22,
		fade: 250,
		showURL: false
	});
});

$(function() {
	$('no#adult_flight_info').tooltip({
		left:-22,
		fade: 250,
		showURL: false
	});
});

	$(function() {
		$('no#child_flight_info').tooltip({
			left:-22,
			fade: 250,
			showURL: false
		});
	});

