
// ------------------------------------------------------------------------------
// ----------   PPC Cookie stuff --------------------------------------------


var CookieLifeTime = 1800;  // Cookie life time is 1800 seconds / 30 minutes


function setCookie(cookieName,cookieValue,lifeTime,path,domain,isSecure)
{
     var d = new Date();
     var time = d.getDate();

     document.cookie=escape( cookieName ) + "=" + escape( cookieValue ) +
		( lifeTime ? ";expires=" + ( new Date( ( new Date() ).getTime() + ( 1000 * lifeTime ) ) ).toGMTString() : "" ) +
		( path ? ";path=" + path : "") + ( domain ? ";domain=" + domain : "") + 
		( isSecure ? ";secure" : "");     
}

function getCookie(name)
{
  var nameEQ = name + "=";
	var ca = document.cookie.split(';');

	for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);   
		if (c.indexOf(nameEQ) == 0) {
            var cookieValue = c.substring(nameEQ.length,c.length);
            return c.substring(nameEQ.length,c.length);
        }
	}
	return null;
}

function extendCookie(name,value,lifetime)
{
    setCookie(name,value,lifetime);
}


function deleteCookie(name)
{
    setCookie(name,"",-10);  
}

function getKey(key) {
    var ss = window.location.search;
    if (ss.charAt(0)=='?') ss = ss.substring(1,ss.length);
    var keys = ss.split('&');   
    var key = key + "=";	
    for(var i=0;i<keys.length;i++) {
        var c = keys[i];
        if (c.indexOf(key) == 0) { 
          return c.substring(key.length,c.length); 
        }
    }
    return false;
}

var SEHost = escape(window.location.hostname);

if (getKey("WT.srch") && getKey("srch")) {

    if (!getCookie("srch")) {
        var CookieValue = getKey("srch");
        setCookie("srch",CookieValue,CookieLifeTime,"/");  
    }
	
    if (!getCookie("SEHost")) {
        var CookieValue = SEHost;
        setCookie("SEHost",CookieValue,CookieLifeTime,"/"); 
    }
	
    if (!getCookie("WT.srch")) {
        var CookieValue = getKey("WT.srch");
        setCookie("WT.srch",CookieValue,CookieLifeTime,"/"); 
    }
    if (!getCookie(SEHost)) {
        var CookieValue = "WT.srch=" + getKey("WT.srch") + ";srch=" + getKey("srch") + ";SEHost=" + SEHost + ";";
        setCookie(SEHost,CookieValue,CookieLifeTime,"/"); 
    }
    
}

var addPPC = function () {
  var anchors;
  var srch = getKey("srch") ? getKey("srch") : getCookie("srch");
  var wtsrch = getKey("WT.srch") ? getKey("WT.srch") : getCookie("WT.srch");
  
  if (srch && wtsrch) {
  
    anchors = document.getElementsByTagName("A");
    for ( a0 in anchors ) {
      if ( anchors[a0].href ) { /* && anchors[a0].href.lastIndexOf(window.location.hostname) < 0 */
        if ( anchors[a0].href.lastIndexOf("http://") == 0 || anchors[a0].href.lastIndexOf("https://") == 0) {
          if ( anchors[a0].href.lastIndexOf("?") > 0) {
              anchors[a0].href = anchors[a0].href + "&WT.srch="+encodeURIComponent(wtsrch)+"&srch="+encodeURIComponent(srch)+"&ref=" + encodeURIComponent(window.location.hostname);
          } else {
              anchors[a0].href = anchors[a0].href + "?WT.srch="+encodeURIComponent(wtsrch)+"&srch="+encodeURIComponent(srch)+"&ref=" + encodeURIComponent(window.location.hostname);
          }
        }
      }
    }
  }
};


var init = function() {
  addPPC();
};

if(window.addEventListener) {
  window.addEventListener("load", init, false);
} else {
  window.attachEvent("onload", init);
}

