$(document).ready(function() {
    $.tabs("GSWcontainer");              
    $('#flights').addClass('on');
    $('.anchor .gsw_submit_but').click(makeURL);
    $('.anchor form').submit(function(){return false;});
    $("#depart_date, #return_date").datepicker({beforeShow: customRange});    
    $("#check_in, #check_out").datepicker({beforeShow: customRange2});    
    $("#v_depart_date, #v_return_date").datepicker({beforeShow: customRange3});    
    $('#depart_date, #return_date, #check_in, #check_out, #v_depart_date, #v_return_date').unbind('keyup').unbind('keydown');
    var as_json = new AutoSuggest('flights_from', autosuggest_options);
    var as_json2 = new AutoSuggest('flights_to', autosuggest_options); 
       
    $('#flights_from').blur(function(){as_json.clearSuggestions()});
    $('#flights_to').blur(function(){as_json2.clearSuggestions()});       
});
   
function customRange(input) { 
    var mDate = (input.id == "depart_date" ? new Date() : $("#depart_date").datepicker("getDate"));
    return { minDate: mDate}; 
}

function customRange2(input) { 
    var mDate = (input.id == "check_in" ? new Date() : $("#check_in").datepicker("getDate"));
    return { minDate: mDate}; 
}

function customRange3(input) { 
    var mDate = (input.id == "v_depart_date" ? new Date() : $("#v_depart_date").datepicker("getDate"));
    return { minDate: mDate}; 
}

function makeURL(){


    a = $(this).attr('id').replace($(this).attr('class')+'_','');

    if (a == "Hotels"){
        var destination = document.widget_hotels.city.value;
        var dep_date = document.widget_hotels.check_in.value;
        var ret_date = document.widget_hotels.check_out.value;
        var adults = document.widget_hotels.adults.value;
        var rooms = document.widget_hotels.rooms.value;
        // end of ret date calcs
        var URL = "http://www.bookingwiz.com/hotels.asp?searchType=hotel&process=referrer&mic=" + mic + "&fm_City=" + destination + "&fm_hotel_checkin_date=" + dep_date + "&fm_hotel_checkout_date=" + ret_date + "&fm_Adults=" + adults + "&fm_children=0&fm_creativeid=I005&fm_xml=&fm_Rooms=" + rooms;
        
    }
    else
    if (a == "Flights"){        
        var departure = $('#flights_from').val();
        var destination = $('#flights_to').val();
        var dep_date = $('#depart_date').val();
        var ret_date = $('#return_date').val();
        
        departure = departure.replace(/\(([^)]+)\)(.+)/,'$1');
        destination = destination.replace(/\(([^)]+)\)(.+)/,'$1');
        
        val_err = false;
        mult_from = mult_to = false;
        fr_fix = to_fix = false;
        $('#flights_to, #flights_from, #depart_date, #return_date').css('background-color','#ffffff');    
        $.ajaxSetup({async: false});
        $.getJSON(josnx_url + '?' + 'check_air='+departure+'|'+destination + '&jsoncallback=?', function(msg){        
            spl = msg.result.split('|');
            if(spl[0]=='0'){
                if(!spl[1]){
                    $('#flights_from').css('background-color','#ffd3d3');
                    val_err = true;
                }else if(spl[1].charAt(0)=='@'){    
                    mult_from = spl[1].substr(1);                
                }else{
                    $('#flights_from').attr('value',spl[1]);
                    fr_fix = true;
                }
            }
            if(spl[2]=='0'){
                if(!spl[3]){
                    $('#flights_to').css('background-color','#ffd3d3');
                    val_err = true;
                }else if(spl[3].charAt(0)=='@'){
                    mult_to = spl[3].substr(1);                
                }else{
                    $('#flights_to').attr('value',spl[3]);
                    to_fix = true;
                }
            } 

            var val_err_date = false;
            var now = new Date();
            var ef_dep = new Date();
            var ef_ret = new Date();
            var spl_d = dep_date.split('/');
            var spl_r = ret_date.split('/');    
            ef_dep.setFullYear(parseInt(spl_d[2]),parseInt(spl_d[0].replace(/^0/,''))-1,parseInt(spl_d[1].replace(/^0/,'')));
            ef_ret.setFullYear(parseInt(spl_r[2]),parseInt(spl_r[0].replace(/^0/,''))-1,parseInt(spl_r[1].replace(/^0/,'')));        
            if(ef_dep < now || ef_dep > ef_ret || !dep_date.match(/\d{1,2}\/\d{1,2}\/\d{4}/)){
                $('#depart_date').css('background-color','#ffd3d3');
                val_err_date = true;
            }
            if(ef_ret < now || ef_dep > ef_ret || !ret_date.match(/\d{1,2}\/\d{1,2}\/\d{4}/)){
                $('#return_date').css('background-color','#ffd3d3');
                val_err_date = true;
            }    
            
            if(val_err){
                alert('The city or airport code(s) in the highlighted fields are not recognized. Please try again and select from one of the suggested locations that appear when typing.');
                return false;
            }
            if(val_err_date){
                alert('Please enter correct departure and return dates'); 
                return false;
            }    
            
            var URL = "http://www.whygo.com/gsw2/results.php?display_inline=0&affiliate=whygocom&action=search_flights&oneway=0&to=" + destination + "&from=" + departure + "&depart=" + dep_date + "&return=" + ret_date + "&mic=" + mic;    
            if(typeof(cvar)!='undefined'){
                URL += '&c='+cvar;
            }
                   
            if(mult_from){
                URL = URL + '&mult_from=' + mult_from;
            }    
            if(mult_to){
                URL = URL + '&mult_to=' + mult_to;
            } 
            
            if(fr_fix){
                URL = URL.replace(/from=([^&]+)&/,'from='+$('#flights_from').val().replace(/\(([^)]+)\)(.+)/,'$1')+'&');
            }           
            if(to_fix){
                URL = URL.replace(/to=([^&]+)&/,'to='+$('#flights_to').val().replace(/\(([^)]+)\)(.+)/,'$1')+'&');
            }                           
                
            $.ajaxSetup({async: false});
            this.req = jQuery.getJSON(josnx_trk, {ajx_action:'add_record',domain:location.host,page:location.pathname}, function(){
                top.location.href=URL;    
            });        
            $.ajaxSetup({async: true});
                    
            
        });
        

                                 
    }
    else
    if (a == "Vacations"){
        var departure = document.widget_vacations.city1.value;
        var destination = document.widget_vacations.city2.value;

        var dep_date = document.widget_vacations.v_depart_date.value;
        var ret_date = document.widget_vacations.v_return_date.value;
        var package_type = document.widget_vacations.package_type.value;

        var URL = "http://www.bookingwiz.com/air.asp?searchType=" + package_type + "&process=referrer&mic=" + mic + "&fm_type_trip=2&fm_air_Departing_Location=" + departure + "&fm_air_Destination_Location=" + destination + "&fm_air_departure_full_date=" + dep_date + "&fm_air_return_full_date=" + ret_date + "&fm_air_Adults=1&fm_air_Children=0&fm_creativeid=I005&fm_xml=";

        
    }
    
    if (a != "Flights"){
        $.ajaxSetup({async: false});
        this.req = jQuery.getJSON(josnx_trk, {ajx_action:'add_record',domain:location.host,page:location.pathname}, function(){
            top.location.href=URL;
        });        
        $.ajaxSetup({async: true});    
    }
    
}