    $(function(){

        
        $("#SearchDepartureDateVisual").datepicker({
            numberOfMonths:  2,
            minDate:         3,
                altField:        '#SearchDepartureDate',
                altFormat:       'yy-mm-dd',
            showButtonPanel: true,
            beforeShowDay:   disableSpecificDays_departure,
            onSelect: function(date, ctxt) {
                $(this).css('color','#555');
                $('#SearchComebackDateVisual').css('color','#555');
                 var sDate = $(this).datepicker('getDate');
                
                if($('#SearchComebackDate').val()){
                    selectDeparture(sDate, false);
                }
                else{
                    selectDeparture(sDate, true);
                }
                return true;
            }
        });
        
        $("#SearchComebackDateVisual").datepicker({
            numberOfMonths:  2,
            minDate:         3,
            showButtonPanel: true,
                    altField:        '#SearchComebackDate',
                    altFormat:       'yy-mm-dd',
            beforeShowDay:   disableSpecificDays_arrival,
            onSelect: function(date){
                $(this).css('color','#1A1A1A');
                return true;
            }
          });
          
          show_waytype();
    });
    
   function selectDeparture(date, set_comeback)
   {
       if (date == '') {
       date = $("#SearchDepartureDate").val();
       }
       if (!date) {
       return false;
       }
       var day = null;
       if (typeof date == "object") {
           day = new Date(date.setHours(23));
       } else {
           date = date.replace(/-/gi,'/');
           day = new Date(date);
           day.setDate(day.getDate());
       }

       if (set_comeback) {
           var offsetDay = new Date(day.getTime());
           var offsetCount = 2;
           if (offsetDay.getDay() > 2) {
               offsetCount = offsetDay.getDay() - 2;
           }
           offsetDay.setDate(offsetDay.getDate() + offsetCount);
       $('#SearchComebackDateVisual').datepicker('setDate', offsetDay);
       }

       $("#SearchComebackDateVisual").datepicker("option", "minDate", day);
   }
   
    function disableSpecificDays_departure(date) {
          td_class='';
          return [true,td_class];
  }
      
    function disableSpecificDays_arrival(date) {
      td_class='';
      return [true,td_class];
  }
  
  function search_submit(){

    var way_type = $('input:radio[name="data[Search][way_type]"]:checked').val();

    var departure = true;
    var destination = true;
    var departure_date = true;
    var destination_date = true;

    if(!$('#SearchDepartureId').val()){
      departure = false;
      $('#SearchDeparture').addClass('error');
    }
    else{
      $('#SearchDeparture').removeClass('error');
    }

    if(!$('#SearchArrivalId').val()){
      destination = false;
      $('#SearchArrival').addClass('error');
    }
    else{
      $('#SearchArrival').removeClass('error');
    }

    if(!$('#SearchDepartureDate').val()){
      departure_date = false;
      $('#SearchDepartureDate').addClass('error');
    }
    else{
      $('#SearchDepartureDate').removeClass('error');
    }

    var departuredate = $('#SearchDepartureDate').val();
    var comebackdate = $('#SearchComebackDate').val();

    var timestamp = Number(new Date(departuredate));
    var timestamp2 = Number(new Date(comebackdate));

    if(way_type != 'one_way' && !comebackdate){
      destination_date = false;
      $('#SearchComebackDate').addClass('error');
    }
    else{
      $('#SearchComebackDate').removeClass('error');
    }

    if(!departure){
		if (language=="lav")
			{alert('Lūdzu izvēlieties virzienu no saraksta vai izmantojiet atlantu!');
		}
		else {alert('Выберите направление из предложенных или воспользуйтесь атласом!');}
      
    }
    else if(!destination){
      	if (language=="lav")
			{alert(' Lūdzu izvēlieties virzienu no saraksta vai izmantojiet atlantu!');
		}
		else {alert('Выберите направление из предложенных или воспользуйтесь атласом!');}
      
    }
    else if(!departure_date){
        if (language=="lav")
			{alert('Izvēlieties izlidošanas datumu!');
		}
		else {alert('Выберите дату вылета!');}
      
    }
    else if(!destination_date){
        if (language=="lav")
			{alert('Izvēlieties atgriešanās datumu!');
		}
		else {alert('Выберите дату возвращения!');}
      
    }
    else if(way_type != 'one_way' && timestamp2 < timestamp){
        if (language=="lav")
			{alert('Izvēlieties atgriešanās datumu!');
		}
		else {alert('Выберите дату возвращения!');}
      
    }
    else{
      $("#searchform").submit();
      return true;
    }

    return false;
  }
  
  function show_destinations(dest, lang){
     window.open("http://eurotravel-rezervet.eturas.lt/flights/search/destinations/"+dest+"/?LANGUAGE="+lang,"Window1","menubar=no,width=660,height=560,scrollbars=yes,toolbar=no");
  }
  
    var way_type = '';

  function show_waytype(){

    $('div.waytypes label, div.waytypes1 label').removeClass('bold');

    var selected = $('input:radio[name="data[Search][way_type]"]:checked');

    selected.next().addClass('bold');

    var val = selected.val();
    way_type = val;

    if (val == 'round_trip' || val == 'weekends' || val == 'multi') {
      $('#comeback_td').show();
    }
    else{
      $('#comeback_td').hide();
    }

    if(val == 'multi') {
        $('#departure2').show();
        $('#comeback2').show();
        $('#multi-destinations').show();
    }
    else{
        $('#departure2').hide();
        $('#comeback2').hide();
        $('#multi-destinations').hide();
    }
   }
   
   $(function(){
        $('input[default_text]').each(function(){
            jQuery(this).bind('focus', function(){
                if(jQuery(this).val() == jQuery(this).attr('default_text')){
                    jQuery(this).val('');
                }
            });
            jQuery(this).bind('blur', function(){
                if(jQuery(this).val() == ''){
                    jQuery(this).val(jQuery(this).attr('default_text'));
                }
            });
            if(jQuery(this).val() == ''){
                jQuery(this).val(jQuery(this).attr('default_text'));
            }
        });
        
    });
    
    // filter destinations popup
destinations=function() {
    destinations.parent_url=document.referrer;
    destinations.opener=window.opener;
    destinations.cookie_name='ff_referrer';
    destinations.saveReferer=function(){
        var exdate=new Date();
        exdate.setDate(exdate.getDate() + 60);
        var c_value=escape(destinations.parent_url) + ";";
        document.cookie=destinations.cookie_name+"=" + c_value;
        return true;
    };
    destinations.setReferer=function(){
        var i,x,y,ARRcookies=document.cookie.split(";");
        for (i=0;i<ARRcookies.length;i++)
        {
          x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
          y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
          x=x.replace(/^\s+|\s+$/g,"");
          if (x==destinations.cookie_name)
            {
                destinations.parent_url=unescape(y)
            return unescape(y);
            }
        }
        return null;
    };
    destinations.add_destination=function(dest,name,key) {
        ref_domain=destinations.parent_url.replace(/http\:\/\/([a-zA-Z.]+)\/(.+)/,/$1/).replace(/\//g,'');
        my_domain=window.location.href.replace(/http\:\/\/([a-zA-Z.]+)\/(.+)/,/$1/).replace(/\//g,'');
        if(ref_domain!=my_domain) {
            send='add_destination/'+dest+'/'+name+'/'+key;
            if(typeof(window.postMessage)=='undefined') {
                destinations.opener.location.hash=send;
            } else {
                destinations.opener.postMessage(send,destinations.parent_url);
            }
        } else {
            destinations.opener.add_destination(dest,name,key);
        }
    }
    return destinations;
}
listenHash = function() {
    listenHash.listen_functions={'add_destination':function(h){
        params=h.replace(/add_destination\/(.+)\//,/$1/).split('/');
        eval('add_destination("'+params[1]+'","'+params[2]+'","'+params[3]+'");');
    }};
    listenHash.getHashValue = function() {
        var arr = window.location.hash.split("#");
        var hasValue = arr[1];
        if (typeof hasValue == "undefined") {
            return '';
        }
        return hasValue;
    };
    listenHash.change = function(hash) {
        for(nm in listenHash.listen_functions) {
            if(hash.substring(0,nm.length)==nm) {
                listenHash.listen_functions[nm](hash);
                break;
            }
        }
        listenHash.lastHash = hash;
        return true;
    };
    listenHash.lastHash = listenHash.getHashValue();
    listenHash.watchHash = function(){
        var hash = listenHash.getHashValue();
        if (hash !== listenHash.lastHash) {
            listenHash.change(hash);
        }
        return true;
    };
    listenHash.stop = function(){
        clearInterval(listenHash.listen);
        return true;
    };
    listenHash.messages = function(e){
        listenHash.change(e.data);
    };
    listenHash.start = function(){
        if(typeof(window.postMessage)=='undefined') {
            listenHash.listen = setInterval(listenHash.watchHash,100);
        } else {
            if(typeof window.addEventListener != 'undefined') {
                window.addEventListener('message', listenHash.messages, false);
            }
            else if(typeof window.attachEvent != 'undefined') {
                window.attachEvent('onmessage', listenHash.messages);
            }
        }
        return true;
    };
    return listenHash.start();
};
listenHash();

function add_destination(dest,name,key){

  if(dest == 'from'){
    $('#SearchDeparture').val(name);
    $('#SearchDepartureId').val(key);
    $('#SearchDepartureSelect').hide();
    $('#SearchDeparture').show();
  }
  else if(dest == 'from2'){
    $('#SearchDeparture2').val(name);
    $('#SearchDepartureId2').val(key);
  }
  else if(dest == 'to'){
    $('#SearchArrival').val(name);
    $('#SearchArrivalId').val(key);
    $('#SearchArrivalSelect').hide();
    $('#SearchArrival').show();
  }
  else if(dest == 'to2'){
    $('#SearchArrival2').val(name);
    $('#SearchArrivalId2').val(key);
  }

}
function changeDeparture(val){
    if(val == 'other'){
        $('#SearchDepartureSelect').hide();
        $('#SearchDeparture').show();
        return;
    }
    else if(val.length == 3){
        $('#SearchDepartureId').val('cityCode_'+val);
    }
}
function changeArrival(val){
    if(val == 'other'){
        $('#SearchArrivalSelect').hide();
        $('#SearchArrival').show();
        return;
    }
    else if(val.length == 3){
        $('#SearchArrivalId').val('cityCode_'+val);
    }
}

