var trk = {
    defaultCookie : '',

    init : function(defaultCookie){ //public call//
    /*-------------------------------------
    Description:
    Init function to setup the tracking stuff
    Author: JL Jan 10
    -------------------------------------*/
        if(!this._allModulesIn()){
            //not all modules present
            return false;
        }

        if(this._GetParameterFromURL("A8ID")){
             //set the PPC tracking code cookie at the root level
            $.cookie('A8ID',this._GetParameterFromURL("A8ID"),{path: '/'})
        }
        else{
            $.cookie('A8ID',defaultCookie);
        }

        if(!$.cookie('a8.referer')){//if referrer not set
            $.cookie('a8.referer',document.referrer,{path: '/'});
        }
        return true;
    },

    getTrackingWithDcStorm : function(){
    /*-------------------------------------
    Description:
    Concatenates a8id, delimiter and dc storm id. We are doing this as the regular
    dc storm scripts dont work with expedia little widget

    Author: JL Aug '09
    Dependencies: jquery.js, jq.cookie.js
    -------------------------------------*/
        var delimiter ='~~';//change this if the delimiter changes on DC storm
        var DcStormCookie = ''; // the dc storm cookie which is set

        if(!$.cookie('_#clkid')){//if no dc storm id defined for what ever reason
            DcStormCookie = 'NoDcStormCookieSet';
        }
        else{
            DcStormCookie = $.cookie('_#clkid');
        }
        return $.cookie('A8ID') + delimiter + DcStormCookie;
    },

    getTrackingWithDcsAndCustomAppend : function(appendString){
    /*-------------------------------------
    Description:
    Concatenates a8id, delimiter and dc storm id. We are doing this as the regular
    dc storm scripts dont work with expedia little widget

    Author: JL Aug '09
    Dependencies: jquery.js, jq.cookie.js
    -------------------------------------*/
        var delimiter ='~~';//change this if the delimiter changes on DC storm
        var DcStormCookie = ''; // the dc storm cookie which is set

        if(!$.cookie('_#clkid')){//if no dc storm id defined for what ever reason
            DcStormCookie = 'NoDcStormCookieSet';
        }
        else{
            DcStormCookie = $.cookie('_#clkid');
        }
        return $.cookie('A8ID') + appendString + delimiter + DcStormCookie;
    },
    
    _allModulesIn : function(){
        //chk if jquery present
        if (!jQuery) {
           alert('Jquery not loaded!!! - Fix this before putting live');
           return false;
        }
        if(!jQuery.cookie){
            //alert('jquey cookie plugin not installed - Add it or fix the reference');
            return false;
        }
        return true
    },

    _GetParameterFromURL : function(name){
    /*-------------------------------------
    Description:
    Extracts a parameter from the url. eg xyz.com/test.php?msg=hello
    GetParameterFromURL('msg') will get 'hello'
    Author: http://www.netlobo.com/url_query_string_javascript.html
    Dependencies:
    Updates:
    @Parameters: name = name of the parameter to extract
    -------------------------------------*/
      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];
    }
}

var frmSrc = { //built but not used :( maybe in the future
    Initialise : function() {
        var numModes = 6;
        for (i = 0; i < numModes; i++) {
            if (document.getElementById("exp_SearchType" + i) != null) {
                document.getElementById("exp_SearchType" + i).checked = true;
                this.exp_showMode(i);
                return;
            }
        }
    },

    exp_showMode : function(whichMode) {
        var numModes = 6;
        for (i = 0; i < numModes; i++) {
            if (typeof(document.getElementById("exp-wiz-" + i) == 'object')) document.getElementById("exp-wiz-" + i).style.display = (i == whichMode) ? "block" : "none";
        }
    },

    exp_showRooms : function(roomNo) {
        document.getElementById("exp-wiz-room2").style.display = (roomNo == 2) ? "block" : "none";
    },

    exp_MoreOptions : function() {

        //FLIGHT + HOTEL
        if ($("#exp_SearchType0").attr('checked')) {
            document.location = "http://clk.tradedoubler.com/click?p=21874&a=1681740&g=17638408&epi=" + trk.getTrackingWithDcsAndCustomAppend('_srchM') +"&url=/pubspec/scripts/eap.asp" + escape("?GOTO=PKGLAUNCH&DestName=" + encodeURI(document.getElementById("exp_FH_DestName").value) + "&FrAirport=" + encodeURI(document.getElementById("exp_FH_FrAirport").value) + "&FromDate=" + document.getElementById("exp_FH_FromDate").value + "&ToDate=" + document.getElementById("exp_FH_ToDate").value + "&FromTime=" + document.getElementById("exp_FH_FromTime").value + "&ToTime=" + document.getElementById("exp_FH_ToTime").value + "&NumAdult=" + document.getElementById("exp_FH_NumAdult").value + "&NumChild=" + document.getElementById("exp_FH_NumChild").value + "&NumSenior=" + document.getElementById("exp_FH_NumSenior").value + "&NumRoom=1");
        }

        //HOTEL
        if ($("#exp_SearchType2").attr('checked')) {
            document.location = "http://clk.tradedoubler.com/click?p=21874&a=1681740&g=17638408&epi=" + trk.getTrackingWithDcsAndCustomAppend('_srchM') +"&url=/pubspec/scripts/eap.asp" + escape("?GOTO=HOTWIZ&MoreOptions=1&cityname=" + encodeURI(document.getElementById("exp_H_CityName").value) + "&indate=" + document.getElementById("exp_H_InDate").value + "&outdate=" + document.getElementById("exp_H_OutDate").value + "&numadult=" + document.getElementById("exp_H_NumAdult").value + "&numchild=" + document.getElementById("exp_H_NumChild").value + "&numroom=" + document.getElementById("exp_H_NumRoom").value);
        }

        if ($("#exp_SearchType5").attr('checked')) {
            document.location = "http://clk.tradedoubler.com/click?p=21874&a=1681740&g=17638408&epi=" + trk.getTrackingWithDcsAndCustomAppend('_srchM') +"&url=/pubspec/scripts/eap.asp" + escape("?http://booking.expedia.co.uk");
        }

        if ($("#exp_SearchType4").attr('checked')) {
            document.location = "http://clk.tradedoubler.com/click?p=21874&a=1681740&g=17638408&epi=" + trk.getTrackingWithDcsAndCustomAppend('_srchM') +"&url=/pubspec/scripts/eap.asp" + escape("?GOTO=TSHOPSSEARCH&LocationName=" + encodeURI(document.getElementById("exp_A_LocationName").value) + "&StartDate=" + document.getElementById("exp_A_StartDate").value + "&EndDate=" + document.getElementById("exp_A_EndDate").value);
        }
    },

    exp_SubmitWiz : function() {
            if  ($("#exp_SearchType0").attr('checked'))
            {
                document.location = "http://clk.tradedoubler.com/click?p=21874&a=1681740&g=17638408&epi="+trk.getTrackingWithDcsAndCustomAppend('_srch')+"&url=/pubspec/scripts/eap.asp" + escape("?GOTO=PACKAGEWIZ&DestName=" + encodeURI(document.getElementById("exp_FH_DestName").value) +"&FrAirport="+ encodeURI(document.getElementById("exp_FH_FrAirport").value) +"&FromDate="+ document.getElementById("exp_FH_FromDate").value +"&ToDate="+ document.getElementById("exp_FH_ToDate").value +"&FromTime="+ document.getElementById("exp_FH_FromTime").value +"&ToTime="+ document.getElementById("exp_FH_ToTime").value +"&NumAdult="+ document.getElementById("exp_FH_NumAdult").value +"&NumChild="+ document.getElementById("exp_FH_NumChild").value +"&NumSenior="+ document.getElementById("exp_FH_NumSenior").value +"&NumRoom=1");
            }


        //FLIGHT

            if  ($("#exp_SearchType1").attr('checked'))
            {
                document.location = "http://clk.tradedoubler.com/click?p=21874&a=1681740&g=17638408&epi=" + trk.getTrackingWithDcsAndCustomAppend('_srch') +"&url=/pubspec/scripts/eap.asp" + escape("?GOTO=EXPFLTWIZ&FrAirport=" + encodeURI(document.getElementById("exp_F_FrAirport").value) +"&ToAirport="+ encodeURI(document.getElementById("exp_F_ToAirport").value) +"&FromDate="+ document.getElementById("exp_F_FromDate").value +"&ToDate="+ document.getElementById("exp_F_ToDate").value +"&FromTime="+ document.getElementById("exp_F_FromTime").value +"&ToTime="+ document.getElementById("exp_F_ToTime").value +"&NumAdult="+ document.getElementById("exp_F_NumAdult").value +"&NumChild="+ document.getElementById("exp_F_NumChild").value +"&NumSenior="+ document.getElementById("exp_F_NumSenior").value);
            }

        //HOTEL
            if($("#exp_SearchType2").attr('checked'))
            {
                var redirectURL;
                redirectURL = "http://clk.tradedoubler.com/click?p=21874&a=1681740&g=17638408&epi=" + trk.getTrackingWithDcsAndCustomAppend('_srch') +"&url=/pubspec/scripts/eap.asp" + escape("?GOTO=HOTSEARCH&cityname=" + encodeURI(document.getElementById("exp_H_CityName").value) + "&indate=" + document.getElementById("exp_H_InDate").value +"&outdate="+ document.getElementById("exp_H_OutDate").value +"&numadult="+ document.getElementById("exp_H_NumAdult").value +"&numchild="+ document.getElementById("exp_H_NumChild").value +"&numroom="+ document.getElementById("exp_H_NumRoom").value);
                if ( document.getElementById("exp_H_NumRoom").value == 2 ) {
                    redirectURL = redirectURL + escape("&numadult2="+ document.getElementById("exp_H_NumAdult2").value +"&numchild2="+ document.getElementById("exp_H_NumChild2").value);
                }
                document.location = redirectURL;
            }
        //CAR
            if($("#exp_SearchType3").attr('checked'))
            {
                document.location = "http://clk.tradedoubler.com/click?p=21874&a=1681740&g=17638408&epi=" + trk.getTrackingWithDcsAndCustomAppend('_srch') +"&url=/pubspec/scripts/eap.asp" + escape("?GOTO=CARSEARCH&pickuploc=" + encodeURI(document.getElementById("exp_C_PickupLoc").value) +"&FromDate="+ document.getElementById("exp_C_FromDate").value +"&ToDate="+ document.getElementById("exp_C_ToDate").value +"&FromTime="+ document.getElementById("exp_C_FromTime").value +"&ToTime="+ document.getElementById("exp_C_ToTime").value +"&Class=" + document.getElementById("exp_C_CarClass").value);
            }

        //ACTIVITES
            if  ($("#exp_SearchType4").attr('checked'))
            {
                document.location = "http://clk.tradedoubler.com/click?p=21874&a=1681740&g=17638408&epi=" + trk.getTrackingWithDcsAndCustomAppend('_srch') +"&url=/pubspec/scripts/eap.asp" + escape("?GOTO=TSHOPSSEARCH&LocationName=" + encodeURI(document.getElementById("exp_A_LocationName").value) +"&StartDate="+ document.getElementById("exp_A_StartDate").value +"&EndDate="+ document.getElementById("exp_A_EndDate").value);
            }

        //3PP
        //UK 3

            if($("#exp_SearchType5").attr('checked'))
            {
                document.location = "http://clk.tradedoubler.com/click?p=21874&a=1681740&g=17638408&epi=" + trk.getTrackingWithDcsAndCustomAppend('_srch') +"&url=http://booking.expedia.co.uk/tt.aspx?" + escape("Departure=" + encodeURI(document.getElementById("exp_UK3PP_LeavingFrom").value) +"&Region="+ encodeURI(document.getElementById("exp_UK3PP_GoingTo").value) +"&Duration="+ document.getElementById("exp_UK3PP_Duration").value +"&FromDate="+ document.getElementById("exp_UK3PP_FromDate").value +"&ToDate="+ document.getElementById("exp_UK3PP_ToDate").value +"&NumAdults="+ document.getElementById("exp_UK3PP_NumAdults").value +"&Child1="+ document.getElementById("exp_UK3PP_Child1").value +"&Child2="+ document.getElementById("exp_UK3PP_Child2").value);
            }
    }
}

/************************************* */	
	
/*-------------------------------------
Description:
The code to get floating calendar stuff   

Author: JL Aug '09
Dependencies: jquery.js, jq.cookie.js
Updates:
-------------------------------------*/
$(document).ready(function() {
    //date picker
    $('.defaultPopupDatepicker').datepick({dateFormat: 'dd/mm/yy'});
    $('.buttonDisable').toggle(function() {
            $(this).text('Enable').siblings('.datepicker').datepick('disable');
        },
        function() {
            $(this).text('Disable').siblings('.datepicker').datepick('enable');
        }
    );

    $('.removeDatepicker').toggle(function() {
            $(this).text('Re-attach').siblings('.datepicker').datepick('destroy');
        },
        function() {
            $(this).text('Remove').siblings('.datepicker').datepick();
        }
    );

    //tracking
    trk.init('a08_expedia_default');

    frmSrc.Initialise();

});

