

function setCookie(c_name,value,expiredays,domain)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+";path=/;expires="+exdate.toGMTString()+";domain="+domain;
}

function setSessionCookie(c_name,value,domain)
{
document.cookie=c_name+ "=" +escape(value)+";path=/;;domain="+domain;
}

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 querySt(ji)
{
	hu = window.location.search.substring(1);
	gy = hu.split("&");
	for (i=0;i<gy.length;i++) 
	{
		ft = gy[i].split("=");
		if (ft[0] == ji) 
		{
		return ft[1];
		}
	}
	return "";
}

function getDomain()
{
    var strURL = document.URL;
    
    // Remove Blog Entry
    strURL = strURL.replace('blog.','www.');
    
    if (strURL != "") {
        return strURL.replace(/^http\:\/\/(www\.|)([^\/]+)(.*)$/,"$2");
    }
    else {
        return "";
    }
}

function getTrafficSourceIndex()
{
    var i;
    
    for (i = 0; i < arrTrafficSources.length; i++) {
        if (arrTrafficSources[i] == sTrafficSource) {

            return i;
        }
    }
        
    return 0;    
}

function cheekyPageOverride(bFirstPageHit,arrURL)
{
    var bOverrideFound = false;
    if (arrURL[0].toLowerCase() == 'dm' || arrURL[0].toLowerCase() == 'dm2')
    {
        sTrafficSource = 'dm';
        bOverrideFound = true;
    }
    else if (arrURL[0].toLowerCase() == 'tel')
    {
        sTrafficSource = 'tel';
        bOverrideFound = true;
    }
    if (bOverrideFound && bFirstPageHit)
    {
        setSessionCookie('calltrackerSession',sTrafficSource,sTropicalDomain);
        setCookie('calltracker',sTrafficSource,180,sTropicalDomain);
    }
    return bOverrideFound;
}

function getTrafficSource(){
//SET UP VARS AND GET SOME BASIC INFO WHICH CAN BE USED TO DETERMINE SOURCE
    var sLongTermCookie = "";
    var sSessionCookie = "";
    var sURL = document.URL;
    var sReferrer = document.referrer;
    if (sURL.charAt(sURL.length-1) == '/')
	{
	    sURL = sURL.substr(0,sURL.length-1);
	}
	if (sReferrer && sReferrer.charAt(sReferrer.length-1) == '/')
	{
	    sReferrer = sReferrer.substr(0,sReferrer.length-1);
	}
	var arrURL = sURL.split('/');
	arrURL.reverse();
	var arrRef = sReferrer.split('/');
	arrRef.reverse();
	
	


//WORKING ON LAST CLICK ATTRIBUTION MODEL NOW SO SEE IF HAVE THE NUMBER FOR THIS SESSION. IF SO THEN RETURN AND JOB DONE
    if (getCookie('calltrackerSession') != "" && getCookie('calltrackerSession') != null)
    {
        if (cheekyPageOverride(false,arrURL))
        {
            return sTrafficSource;
        }
        sSessionCookie = document.cookie.replace(/^(.*)calltrackerSession=([^\;]+)(.*)$/,"$2");
        if (sSessionCookie == 'setassamericaother')
        {
            //special case:- set serverside on first page hit by hotel/tour pages for all south american hotels/tours
            sSessionCookie = "samericaother";
            sLongTermCookie = sSessionCookie;
            setSessionCookie('calltrackerSession',sSessionCookie,sTropicalDomain);
            setCookie('calltracker',sLongTermCookie,180,sTropicalDomain);
        }
        return sSessionCookie;
    }
    
    
//CURRENT SESSION SOURCE HAS NOT BEEN DETERMINED SO THIS IS THE FIRST HIT ON THE SITE.
//TRY TO DETERMINE SOURCE...


    //CHECK FOR ANY PAGE OVERRIDES
    if (cheekyPageOverride(true,arrURL))
    {
        return sTrafficSource;
    }

    if (
            arrURL[0].toLowerCase() == 'south-america-holidays.htm' ||
            arrURL[0].toLowerCase() == 'argentina-holidays.htm' ||
            arrURL[0].toLowerCase() == 'brazil-holidays.htm' ||
            arrURL[0].toLowerCase() == 'chile-holidays.htm' ||
            arrURL[0].toLowerCase() == 'colombia-holidays.htm' ||
            arrURL[0].toLowerCase() == 'ecuador-and-galapagos-islands-holidays.htm' ||
            arrURL[0].toLowerCase() == 'peru-holidays.htm'
        )
        {
            sSessionCookie = "samericaother";
        }
	
	
    
	
	
	
	if (querySt("utm_source") == "eblast-worldwide")
	{  
       sSessionCookie = "eblast-worldwide";        
    }
    else if (querySt("utm_source") == "eblast-north-america")
    {  
       sSessionCookie = "eblast-north-america";        
    }
    else if (querySt("utm_source") == "eblast-row")
    {  
       sSessionCookie = "eblast-row";        
    }
    else if (querySt("utm_source") == "dealchecker")
    {  
       sSessionCookie = "dealchecker";        
    }
    else if (querySt("utm_source") == "cheapflights")
    {  
       sSessionCookie = "cheapflights";        
    }
    else if (querySt("utm_source") == "teletext")
    {  
       sSessionCookie = "teletext";        
    }
    else if (querySt("utm_source") == "e-media")
    {  
       sSessionCookie = "e-media";        
    }
    
	    	
	    	


	
	if (sReferrer.toLowerCase().match(/^(.*)travelzoo(.*)$/))
	{
        sSessionCookie = "tz";
	}
	else if (sReferrer.toLowerCase().match(/^(.*)facebook(.*)$/))
	{
        sSessionCookie = "facebook";
	}
	else if (sReferrer.toLowerCase().match(/^(.*)silvertraveladvisor(.*)$/))
	{
        sSessionCookie = "silvertraveladvisor";
	}
	else if (sReferrer.toLowerCase().match(/^(.*)twitter(.*)$/))
	{
        sSessionCookie = "twitter";
	}
	else if (arrURL[0].toLowerCase() == 'deals')
	{
	    if (arrRef[0].toLowerCase() == 'site-map.htm')
	    {
	        //alert('its the deals page BUT referred by the sitemap so no set cookie');
	    }
	    else
	    {
	        //alert('its the deals page');
            sSessionCookie = "pr";
        }
	}
	
	
    
	    	

    if (document.referrer != "")
    { 

        if (document.referrer.match(/^(.*)google(.*)$/))
        {

           if (document.URL.match(/^(.*)\?gclid\=(.*)$/) || document.URL.match(/^(.*)(\?|\&)utm\_term\=ppc(.*)$/) || document.cookie.match(/^(.*)utmcmd\=\(not\%20set\)(.*)$/) || document.cookie.match(/^(.*)utmcmd\=ppc(.*)$/) ||  document.referrer.match(/^(.*)sponsoredlinks\?(.*)$/))
           {
                sSessionCookie = "ppc";           
           }
           else
           {
                sSessionCookie = "organic";          
           } 
        }
    }
    
    if (document.referrer.match(/^(.*)google(.*)$/) != null && document.referrer.match(/^(.*)caribbean(\%2(0|B)|\+| )holidays(.*)$/) != null) {   
        if ( querySt("gclid").length > 0)
        {
            sSessionCookie = "carribeanppc";
        }
        else
        {
            sSessionCookie = "caribbeanorganic";  
        }
    }
    else if (document.URL.match(/^(.*)\/sundaytimes(.*)$/)) {   
        sSessionCookie = "sundaytimesdirect";        
    } 
    else if (document.URL.match(/^(.*)\/sunday\-times(.*)$/)) {   
        sSessionCookie = "sundaytimesurl";        
    }

    
    if (document.referrer.match(/^(.*)google(.*)$/) != null && document.referrer.match(/^(.*)south(\%2(0|B)|\+| )american(\%2(0|B)|\+| )holidays(.*)$/) != null) {   
        if ( querySt("gclid").length > 0)
        {
            sSessionCookie = "samericappc";
        }
        else
        {
            sSessionCookie = "samericaorganic";  
        }
    }


    
    if (sSessionCookie == "")
    {
        //DONT RECOGNISE SOURCE, DO WE HAVE A LONG TERM COOKIE?
        if (getCookie('calltracker') != "" && getCookie('calltracker') != null) {
            sLongTermCookie = document.cookie.replace(/^(.*)calltracker=([^\;]+)(.*)$/,"$2");
        }
        if (sLongTermCookie == "")
        {
            //NO LONG TERM COOKIE SO DEFAULT TO NORMAL NUM
            sSessionCookie = "normal";
            sLongTermCookie = sSessionCookie;
        }
        else
        {
            //USE LONG TERM COOKIE SOURCE
            sSessionCookie = sLongTermCookie;
        }
    }
    else
    {
        //NEW SOURCE RECOGNISED SO SWITCH LONG TERM COOKIE TO IT AS WE ARE NOW RUNNING A LAST CLICK ATTRIBUTION MODEL
        sLongTermCookie = sSessionCookie;
    }
    
    //WRITE COOKIES
    setSessionCookie('calltrackerSession',sSessionCookie,sTropicalDomain);
    setCookie('calltracker',sLongTermCookie,180,sTropicalDomain);

    return sSessionCookie.toLowerCase();
}









