
function addTrackToFormAction(actionUrl){
/*
 * @desc takes the URL and places the tracker into it
 * @author Joe Lee
 * @changelog
 * @param string actionUrl - the url the form posts to
 * @return string modified URL
*/	
	//eg http://www.awin1.com/awclick.php?gid=79977&mid=1963&id=63715&clickref=&p=http://www.macdonaldhotels.co.uk/bookonline/Default.aspx

	var theA8id = getTrackingWithDcStorm();
	var trackingParam = 'clickref=';
	var trackCharIndex = -1;
	var firstPart = '';
	var lastPart = '';
	var newActionUrl ='';

	//get trackingParam start point from actionUrl
	trackCharIndex  = actionUrl.indexOf(trackingParam);
	
	//break apart the URL
	firstPart = actionUrl.substring(0,trackCharIndex-1);
	lastPart = actionUrl.substring(trackCharIndex+10, actionUrl.length+1);

	//put them together
	newActionUrl = firstPart + trackingParam + theA8id + "&"+ lastPart;
	
	return newActionUrl;
}

function GetParameterFromURL(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];
}

function setCookie(c_name,value,expiredays)
{
     var exdate=new Date();
     exdate.setDate(exdate.getDate()+expiredays);
     document.cookie=c_name+ "=" +escape(value)+
     ((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
}

function getCookie(c_name)
{
     if (document.cookie.length>0){
          c_start=document.cookie.indexOf(c_name + "=");
          if (c_start!=-1){
              c_start=c_start + c_name.length+1;
              c_end=document.cookie.indexOf(";",c_start);

              if (c_end==-1) c_end=document.cookie.length;
              return unescape(document.cookie.substring(c_start,c_end));
          }
     }
     return "";
}

function submitForm(formId){
/*
 * @desc called on submission of the search form, this includes a hack which seems to be in jquery when trying
 * 		to mod the action attr of form.
 * @author Joe Lee
 * @changelog
 * @param string formId - css id of form
*/
	var form_url = $(formId).attr("action");
    //alert("Before - action=" + form_url);       
    //
    $(formId).attr("action",addTrackToFormAction($(formId).attr("action")));
    
    $('#bookOnline').html($('#bookOnline').html()+'<input type="hidden" name="action" value="SEARCH" id="action"/>')    
    //submit the form
    $(formId).submit();
}

var macDon = {
    updateFormAction : function(){
        /*-------------------------------------
        Description:
        Gets the A8id from a cookie or returns the default

        Author: JL Aug '09
        Dependencies: jquery.js, jq.cookie.js
        Updates:
        -------------------------------------*/

        //updates the awin form action to include a tracking code

        //prepare the tracking code;
        var ck = ''; //tmp var for cookie
        var trckCode =''; // the ppc or natural trk 
        var finalTrkCode = '';
        var dcStormCode ='' ;

        //chk if new ppc traffic and set the a8id cookie
        if(GetParameterFromURL('A8ID')){
            setCookie('A8ID', GetParameterFromURL('A8ID'), 365);
        }

        ck = getCookie('A8ID');
        if(ck == ''){
            //no a8id set so assuming natural
            trckCode = 'a08_macD_bookinFom';
        }
        else{
            trckCode = ck;
        }

        //dc storm
        ck = getCookie('_#clkid');
        if(ck == ''){
            dcStormCode = 'chkDcStorm';
        }
        else{
            dcStormCode = ck;
        }
            //put it all together
        finalTrkCode = trckCode + '~~' + dcStormCode;

        var t = $('#frmSimpleSearch').attr('action');
        var newUrl = t.match(/clickref=(.*)&p=/, 'test');
        newUrl = t.replace(newUrl[1],finalTrkCode);
        $('#frmSimpleSearch').attr('ACTION',newUrl);


    }
}

/************************************* */	
/*-------------------------------------
Description:
The code to get floating calendar stuff   

Author: JL Oct '09
Dependencies: jquery.js, jq.cookie.js
Updates:
-------------------------------------*/
$(document).ready(function() {
	//$('.defaultPopupDatepicker').datepick({dateFormat: 'dd/mm/yy'});		
	
	//remove the entry allowing people to search by any macD hotel and default to the first one
	$('#propertyCode option:nth-child(1)').remove();
        
        //replace the action with the correct tracking code
        //setTimeout('macDon.updateFormAction()',1000);
	
});

