 //JavaScript Document
 //$(function() {
   
$(document).ready(function() {
	updatePredictedFare(); // grab an initial fare prediction
$("#Search_Flights_li").css("background-color","#fff");//for mytrips search sub menu
$("#Search_Flights").css("color","#000");
$("#itnerary_item").css("color","#fff");
$("#itnerary_item_li").css("background-color","#1C86C2");//end

function format(airport) {
      var str;
      str = airport.apName + " (" + airport.code + ")";
      if(airport.city != null && airport.city != '')
      str += ", "+airport.city;
      if(airport.state != null && airport.state != '')
      str += ", "+airport.state;
      return str;  
    }
    function parseData(row)
    {
        var str;
        str = row.apName + " (" + row.code + ")";
        if(row.city != null && row.city != '')
        str += ", "+row.city;
        if(row.state != null && row.state != '')
        str += ", "+row.state;
        return {
          data: row,
          value: str,
          result: str
        }
    }
    $("#name").autocomplete(local+'/user/searchairport', {
      minChars: 3,
      matchContains: true,
      max: autoCompBoxLimit,
      selectFirst: true,
      pare:'nameh',
       parse: function(data) {
        return $.map(eval(data), function(row) {
                     return parseData(row);
        });
      },
      formatItem: function(item) {
      return format(item);
      }
    });
	$("#name, #airport, .passengerclass").change(function () {
		updatePredictedFare();
	});
	$("#RoundTrip, #OneWay, #nonStopSearch, #restricting, #restricting1, #restrictinghome").click (function () {
		updatePredictedFare();
	});
    $("#airport").autocomplete(local+'/user/searchairport', {
      selectFirst: true,
      minChars: 3,
      matchContains: true,
      max: autoCompBoxLimit,
      pare:'airporth',
      parse: function(data) {
        return $.map(eval(data), function(row) {
                     return parseData(row);
        });
      },
      formatItem: function(item) {
      return format(item);
      }
    });
  });
  
$(document).ready(function() {
//$("#restricting1").datepicker("disable");

                  $("#morefares").click(function() {
                  $(".nondisplayleftinner").css("display","block");
                  $(".moredetails").css("display","none");
                  return false;
           });
         $(".linksearch").click(function() {
                 var id=this.id;            
                 id="#"+id.replace("hidd",'');
                 var obj=$(id);
                 obj.submit();
                 return false;
           });
  $("#flightsubmit").submit(function()
  {
     if(($("#name").val().length<1||$("#nameh").val()=="0")&&($("#name").val()!=to||$("#name").val().length<1))
     {

       $('#alertErrorText').text("Please select a departure city or airport from the list.");
       errormsg("alertError");
       return false;
     }
     if(($("#airport").val().length<1||$("#airporth").val()=="0")&&($("#airport").val().length<1||$("#airport").val()!=from))
     {
       $('#alertErrorText').text("Please select a destination city or airport from the list.");
       errormsg("alertError"); 
       return false; 
     }

     if($("#restrictinghome").val().length<1)
     {
       $('#alertErrorText').text("Please enter a departure date.");
       errormsg("alertError");       
       return false;
     }
     if($("input[@name='round']:checked").val()==1)
     {  
       if($("#restricting1").val().length<1)
       {
         $('#alertErrorText').text("Please enter a return date.");
         errormsg("alertError");
         return false;
       }
       var mydate1 = new Date($("#restrictinghome").val()) ;
       var mydate2 = new Date($("#restricting1").val());
       if(mydate1 > mydate2 )
       {
         $('#alertErrorText').text("Departure date must be before return date.");              
         errormsg("alertError"); 
         return false;
       }
     } 
  });

$("input[@name='round']").click(function() {
  if($("input[@name='round']:checked").val()=="0")
  {
     $("#arrChoice").attr('disabled', 'disabled');
     $("#arrTime").attr('disabled', 'disabled');
     $("#restricting1").datepicker("disable");
  }
  else
  {
     $("#arrChoice").removeAttr('disabled');
     $("#arrTime").removeAttr('disabled');
     $("#restricting1").datepicker('enable');
  }  
});   
$("#restrictinghome").change(function() {
         var date1=$("#restrictinghome").val();
         var myday = new Date(date1);
         var today=  new Date();
         var one_day=1000*60*60*24;
         chan_date=Math.ceil((myday.getTime()-today.getTime())/(one_day))+0;
         myday.setDate(myday.getDate()+3);
         var month = myday.getMonth()+1;
         if(month<10)
            month="0"+month;
         var dat=myday.getDate();
         if(dat<10)
            dat="0"+dat;        
    if($("input[@name='round']:checked").val()=="1"&&($("#restricting1").val()==""||dateconvert($("#restrictinghome").val())>dateconvert($("#restricting1").val())))
         $("#restricting1").attr("value",month+"/"+dat+"/"+myday.getFullYear()) 
      });

 });
function dateconvert(datechange)
{
  newdate=new Date(datechange);
  newdate=newdate.getTime()/1000; 
  return newdate;
}
function updatePredictedFare() {
	// extract airport code from between parenthesis
	//if (search_source.search(/\(/)) var search_source=from_search.split('(')[1].split(')')[0];
	//if (search_dest.search(/\(/)) var search_dest=to_search.split('(')[1].split(')')[0];
	
	$.ajax({
			type: "POST",
			url: local+"/flight/predict",
			data: $('#flightsubmit').serialize(), //+"&source="+search_source+"&dest="+search_dest,
			success: function(result){
				if (result > 0) {
					$('#predictedfare').html('Predicted Lowest Fare: <span id=predictedfareprice>$' + result + '</span>'); // + ' ('+search_source+' to '+search_dest+')');
					$('#predictedfare').fadeIn('.4');
				} else {
					$('#predictedfare').fadeOut('.4');
				}
			}
		});
}
