
function populateSearchBox(){
    //var sp_searchName = $('select#searchName').val();
    var sp_county = gup('county');
    var sp_checkinDD = gup('ad');
    var sp_checkinMMYYYY = unescape(gup('am'));
    var sp_numNights= gup('numNights');
    var sp_numAdults = gup('numAdults');
    var sp_numRooms = gup('numRooms');
    var sp_numChildren = gup('numChildren');
    var sp_propertyType = gup('propertyType');
    var sp_sortMethod = gup('sortMethod');
    var sp_currency = gup('currency');
    var sp_priceRange = gup('priceRange');
    var sp_starRating = gup('starRating');
    var sp_propertyByCounty = gup('propertyByCounty');
    var sp_hotelGroupId = gup('hotelGroupId');
    //var sp_suburbs = gup('suburb');


    var data=  'currency=' + sp_currency +
        '&county=' + sp_county +
        '&ad=' + sp_checkinDD +
        '&am=' + sp_checkinMMYYYY +
        '&numNights=' + sp_numNights +
        '&numRooms=' + sp_numRooms +
        '&numAdults=' + sp_numAdults +
        '&numChildren=' + sp_numChildren +
        '&propertyType=' + sp_propertyType +
        '&sortMethod=' + sp_sortMethod +
        '&priceRange=' + sp_priceRange +
        '&starRating=' + sp_starRating +
        '&propertyByCounty=' + sp_propertyByCounty +
        '&hotelGroupId=' + sp_hotelGroupId;
    

if (sp_checkinDD!="" && sp_checkinMMYYYY!=""){
        populateArrival(sp_checkinDD,unescape(sp_checkinMMYYYY));
    }
    else {
        var arrDate = $('#datepicker').datepicker('getDate');
        $('#am').val((arrDate.getMonth()+1)+':'+arrDate.getFullYear());
        $('#ad').val(arrDate.getDate());

    }



        if (sp_numAdults !=""){
            $('#numAdults').val(sp_numAdults);
        }
    
        if (sp_numRooms !=""){
            
            $('#numRooms').val(sp_numRooms);
        }

        if (sp_sortMethod !=""){

            $('#sortMethod').val(sp_sortMethod);
        }
    
        if (sp_propertyType !=""){

            $('#propertyType').val(sp_propertyType);
        }
        if (sp_numChildren !=""){
            $('#numChildren').val(sp_numChildren);
        }


    

        if (sp_numNights !=""){
            $('#numNights').val(sp_numNights);
        }

        var searchDate = new Date();

        if (sp_checkinDD !=""){
            $('#ad').val(sp_checkinDD);
            searchDate.setDate(sp_checkinDD);
            $('#datepicker').datepicker( "setDate" , searchDate );
        }


        if (sp_checkinMMYYYY !=""){
            $('#am').val(sp_checkinMMYYYY);
            var temp = new Array();
            temp = sp_checkinMMYYYY.split(":");

            searchDate.setMonth(parseInt(temp[0])-1);
            searchDate.setFullYear(parseInt(temp[1]));
            $('#datepicker').datepicker( "setDate" , searchDate );
        }
}


    function gup( name )
    {
        name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
        var regexS = "[\\?&]"+name+"=([^&#]*)";
        var regex = new RegExp( regexS );
        var results = regex.exec( window.location.href );
        if( results == null )
            return "";
        else
            return results[1];
    }


function populateArrival(dd, mmyyyy) {
    if (dd.length==1){
        dd = "0"+dd;
    }
    var startDate = new Date();
    startDate.setDate(dd);
    var temp = new Array();
    temp = mmyyyy.split(":");
    startDate.setMonth(temp[0]-1);
    startDate.setFullYear(temp[1]);

    $('#datepicker').datepicker('setDate',startDate);
    $('#checkinDD').val(dd);
    $('#checkinMMYYYY').val(mmyyyy);
}

    $(document).ready(function() {

    


        $('#datepicker').datepicker({
            showOn: "button",
            buttonImage: "http://search.bookin1.com/images/buttons/calendar.png",
            buttonImageOnly: true,
            buttonText: 'Select an Arrival Date',
            closeText: 'Close',
            minDate: 0,
            maxDate: 365,
            firstDay: 1,
            showAnim: false,
            dateFormat: "dd/mm/yy",
            defaultDate: +0,
            onSelect: function(dateText, inst) {
                var month = inst.currentMonth+1;
                $('#am').val(month+':'+inst.currentYear);
                $('#ad').val(inst.currentDay);

            },
            showButtonPanel: true
        });
        $('#datepicker').datepicker( "setDate" , new Date() );
        populateSearchBox();
        
    });




