$(document).ready(function() {
$("#Rent_Car_li").css("background-color","#fff");//for mytrips search sub menu
$("#Rent_Car").css("color","#000");
$("#itnerary_item").css("color","#fff");
$("#itnerary_item_li").css("background-color","#1C86C2");//end

if($("#country").val()!="00")      //pick-up country
{
$("#pickupdiv").load(local+"/car/getlocations",{'country':$("#country").val(),'page':pagetype,'type':"pickup"},function(){
      $("#pickup").attr("value",pickupCode);              
      $("#fullPickup").val($('#pickup :selected').text());
      changelocation();
     });
}

$("#residencyId").change(function(){   //residency
  $("#residency").attr("value",$("#residencyId :selected").text());
                     })
$("#country").change(function(){   //pick-up country

   $("#pickup").attr('disabled', 'disabled'); 
   $("#pickupdiv").load(local+"/car/getlocations",{'country':$("#country").val(),'page':pagetype,'type':"pickup"},function(){
   if(!$("#pickup").val()) 
   {
       $('#alertErrorText').text("No locations are available for this country.");
       errormsg("alertError");
       $("#pickup").attr('disabled', 'disabled');
   }
   else
   { 
      $("#fullPickup").val($('#pickup :selected').text());
      changelocation();
   }  
    });
})

if($("#dropoffCountry").val()!="00")          //drop-of country
{
$("#dropoffdiv").load(local+"/car/getlocations",{'country':$("#dropoffCountry").val(),'page':pagetype,'type':"dropoff"},function(){
      
      $("#dropoff").attr("value",dropoffCode);              
      $("#fullDropoff").val($('#dropoff :selected').text());
      changelocation();
     if($("input[@name='carround']:checked").val()=="1")
     {
        $("#dropoff").attr('disabled', 'disabled');
        $("#dropoff").css('background-color','#ECE9D8');
     }   
     });
}

$("#dropoffCountry").change(function(){     //drop-off country

   $("#dropoff").attr('disabled', 'disabled');
   $("#dropoffdiv").load(local+"/car/getlocations",{'country':$("#dropoffCountry").val(),'page':pagetype,'type':"dropoff"},function(){
   if(!$("#dropoff").val()) 
   {
       $('#alertErrorText').text("No locations are available for this country.");
       errormsg("alertError");
       $("#dropoff").attr('disabled', 'disabled');
   }
   else
   {   
    $("#fullDropoff").val($('#dropoff :selected').text());
     changelocation();
     }  
    });
})


//$("#t_returndate").datepicker("disable");
$("input[@name='carround']").click(function() {
    if($("input[@name='carround']:checked").val()=="0")
    {
        $("#dropoffCountry").removeAttr('disabled');
        $("#dropoffCountry").css('background-color','#fff');
        if($("#dropoffCountry").val()!="00")
        {
            $("#dropoff").removeAttr('disabled');
            $("#dropoff").css('background-color','#fff');
        }     
    }
    else
    {
        $("#dropoffCountry").attr('disabled', 'disabled');
        $("#dropoffCountry").css('background-color','#ECE9D8');
        $("#dropoff").attr('disabled', 'disabled');
        $("#dropoff").css('background-color','#ECE9D8');
    } 
});

 
$("#formsubmit").submit(function()
{
    if(!$("#pickup").val())
    {
       $('#alertErrorText').text("No locations are available for this country.");
       errormsg("alertError");
       return false;
    } 
    if(!$("#dropoff").val()&& $("input[@name='carround']:checked").val()=="0")
    {
       $('#alertErrorText').text("No locations are available for this country.");
       errormsg("alertError");
       return false;
    } 
      if($("#t_pickuphome").val().length<1)
      {
        //if($("#alertErrorSearch").height()) 
          //hideerrormsg('alertErrorSearch');
        $('#alertErrorText').text("Please enter pick-up date.");
        errormsg("alertError");
        return false;
      }

      if($("#t_dropoff").val().length<1)
      {
        //if($("#alertErrorSearch").height()) 
          //hideerrormsg('alertErrorSearch');
        $('#alertErrorText').text("Please enter drop-off date.");
        errormsg("alertError");
        return false;
      }
      var pickupdate = new Date($("#t_pickuphome").val());
      var dropdate = new Date($("#t_dropoff").val());
      if(pickupdate > dropdate)
      {
        //if($("#alertErrorSearch").height()) 
          //hideerrormsg('alertErrorSearch');
        $('#alertErrorText').text("Pickup time must be before  dropoff time.");
        errormsg("alertError");
        return false;
      }
})
$("#t_pickuphome").change(function() {
        var date1=$("#t_pickuphome").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));
        myday.setDate(myday.getDate()+1);
        var month = myday.getMonth()+1;
        if(month<10)
            month="0"+month;
         var dat=myday.getDate();
         if(dat<10)
            dat="0"+dat;         
       //$("#t_dropoff").datepicker('enable');
       $("#arrChoice").removeAttr('disabled');
      var pickupdate = new Date($("#t_pickuphome").val());
      var dropdate = new Date($("#t_dropoff").val());

       if( $("#t_dropoff").val() == "" || pickupdate > dropdate )
       {
         $("#t_dropoff").attr("value",month+"/"+dat+"/"+myday.getFullYear()) ;
       }
});

$("#t_pickup").change(function() {
        var date1=$("#t_pickup").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));
        myday.setDate(myday.getDate()+1);
        var month = myday.getMonth()+1;
        if(month<10)
            month="0"+month;
         var dat=myday.getDate();
         if(dat<10)
            dat="0"+dat;         
       //$("#t_dropoff").datepicker('enable');
       $("#arrChoice").removeAttr('disabled');
       $("#t_dropoff").attr("value",month+"/"+dat+"/"+myday.getFullYear()) 
});
$(".linksearch").click(function() {
         var id=this.id;
         id="#"+id.replace("hidd",'');
         var obj=$(id);
         obj.submit();
         return false;
      });
});

function changelocation()
{
$("#dropoff").change(function(){
  $("#fullDropoff").val($('#dropoff :selected').text());
});
$("#pickup").change(function(){
  $("#fullPickup").val($('#pickup :selected').text());
});
}

