// "standard" read cookie function  (copied from Prusak's gwo_write.js)
function read_cookie(cookie_name) {
  var my_cookie=""+document.cookie;
  var ind=my_cookie.indexOf(cookie_name);
  if (ind==-1 || cookie_name=="") return ""; 
  var ind1=my_cookie.indexOf(';',ind);
  if (ind1==-1) ind1=my_cookie.length; 
  return unescape(my_cookie.substring(ind+cookie_name.length+1,ind1));
}

function superSetVar(appendValue) {
  var getVar = read_cookie('__utmv');       // read the __utmv cookie
  hasValue = getVar.indexOf(appendValue);   // does the cookie already have the value we want to append?
  if ( hasValue == -1 ) {                   // if the new value is not already in the cookie
    removePrefix = /^[0-9]*\.(.*)/.exec(getVar);  // __utmv cookie has the format 12345678.cookieValue - remove the ## prefix
    if (removePrefix && removePrefix[1]) {
      newVar = removePrefix[1] + appendValue; // append the value
    }else{
      newVar = appendValue;                 // this will execute if the cookie was not already set.
    }
      var superSetVarTracker = _gat._getTracker("UA-1-1");  // set up a tracker to call _setVar from
      superSetVarTracker._initData();  
      superSetVarTracker._setVar(newVar);                   // call _setVar with the new value
  }
}

function unSetVar(removeValue){
  var getVar = read_cookie('__utmv');         // read the __utmv cookie
  hasValue = getVar.indexOf(removeValue);     // does the cookie have the value we want to remove?
  if ( hasValue != -1 ) {                     // if the value is in the cookie then . . .  otherwise, there is nothing further to do.
    removePrefix = /^[0-9]*\.(.*)/.exec(getVar);  // __utmv cookie has the format 12345678.cookieValue - remove the ## prefix
    if (removePrefix && removePrefix[1]) {    // if we remove the prefix then. . .
      //
      var re = removeValue + '[^/]*';
      re = new RegExp(re,'g');
      newVar = removePrefix[1].replace(re,""); // newVar = removePrefix[1] MINUS removeValue
      //
      var superSetVarTracker = _gat._getTracker("UA-1-1");  // set up a tracker to call _setVar from
      superSetVarTracker._initData();  
      superSetVarTracker._setVar(newVar);                   // call _setVar with the new value
    }
    
  }
}


function parsedURL(myReferrer) {
  this.protocol = "";
  this.hostname="";
  this.pathname="";
  this.params = new Object();

  var expression;
  var paramstring;
  var parampair;
  var myarr;

  if (myReferrer) {
    expression = /(http|https|ftp):\/\/([-äÄöÖüÜß\.\w]+)\/?(.*)\??(.*)/;
    expression.exec(myReferrer);
    this.protocol = RegExp.$1;
    this.hostname=RegExp.$2;
    this.pathname=RegExp.$3;
    paramstring=RegExp.$4;

    if (paramstring) {
      parampair = paramstring.split(/\&/);
      for (var i = 0; i < parampair.length; i++) {
        myarr = parampair[i].split(/=/);
        this.params[myarr[0]]=myarr[1];
      }
    }
  }
}  

function generateVar(refererURLunparsed) {
  var documentURL = new parsedURL(document.URL);
  var refererURL = new parsedURL(refererURLunparsed);

  var result="";
  
  if ( refererURL.hostname != documentURL.hostname ) {
    if (refererURL.hostname.indexOf("buergerkarte.at") >= 0) {result = result + "/referer=buergerkarte.at/reftype=public"; }
    if (refererURL.hostname.indexOf("bürgerkarte.at") >= 0) {result = result + "/referer=buergerkarte.at/reftype=public"; }
    if (refererURL.hostname.indexOf("sozialversicherung.at") >= 0) {result = result + "/referer=Sozialversicherung/reftype=public"; }
    if (refererURL.hostname.indexOf("bgkk.at") >= 0) {result = result + "/referer=Sozialversicherung/reftype=public"; }
    if (refererURL.hostname.indexOf("bva.at") >= 0) {result = result + "/referer=Sozialversicherung/reftype=public"; }
    if (refererURL.hostname.indexOf("kgkk.at") >= 0) {result = result + "/referer=Sozialversicherung/reftype=public"; }
    if (refererURL.hostname.indexOf("noegkk.at") >= 0) {result = result + "/referer=Sozialversicherung/reftype=public"; }
    if (refererURL.hostname.indexOf("ooegkk.at") >= 0) {result = result + "/referer=Sozialversicherung/reftype=public"; }
    if (refererURL.hostname.indexOf("sgkk.at") >= 0) {result = result + "/referer=Sozialversicherung/reftype=public"; }
    if (refererURL.hostname.indexOf("stgkk.at") >= 0) {result = result + "/referer=Sozialversicherung/reftype=public"; }
    if (refererURL.hostname.indexOf("sva.at") >= 0) {result = result + "/referer=Sozialversicherung/reftype=public"; }
    if (refererURL.hostname.indexOf("svb.at") >= 0) {result = result + "/referer=Sozialversicherung/reftype=public"; }
    if (refererURL.hostname.indexOf("tgkk.at") >= 0) {result = result + "/referer=Sozialversicherung/reftype=public"; }
    if (refererURL.hostname.indexOf("vgkk.at") >= 0) {result = result + "/referer=Sozialversicherung/reftype=public"; }
    if (refererURL.hostname.indexOf("digitales.oesterreich.gv.at") >= 0) {result = result + "/referer=digitalaustria/reftype=public"; }
    if (refererURL.hostname.indexOf(".gv.at") >= 0) {result = result + "/reftype=public"; }
    if (refererURL.hostname.indexOf("wko.at") >= 0) {result = result + "/referrer=wko.at/reftype=public"; }

    if (refererURL.hostname.indexOf("easybank.at") >= 0) {result = result + "/referer=BAWAG-PSK-easybank/reftype=banking"; }
    if (refererURL.hostname.indexOf("pskbank.at") >= 0) {result = result + "/referer=BAWAG-PSK-easybank/reftype=banking"; }
    if (refererURL.hostname.indexOf("bawag.com") >= 0) {result = result + "/referer=BAWAG-PSK-easybank/reftype=banking"; }
    if (refererURL.hostname.indexOf("verkehrskreditbank.at") >= 0) {result = result + "/referer=BAWAG-PSK-easybank/reftype=banking"; }
    if (refererURL.hostname.indexOf("sparkassen.at") >= 0) {result = result + "/referer=Sparkassen/reftype=banking"; }
    if (refererURL.hostname.indexOf("spardawien.at") >= 0) {result = result + "/referer=Sparda/reftype=banking"; }
    if (refererURL.hostname.indexOf("elba.at") >= 0) {result = result + "/referer=elba/reftype=banking"; }
	
    if (refererURL.hostname.indexOf("a-trust.at") >= 0) {result = result + "/referer=a.trust/reftype=trustcenter"; }

    if (refererURL.hostname.indexOf("geizhals.at") >= 0) {result = result + "/referer=geizhals.at/reftype=listig"; }

    if (refererURL.hostname.indexOf("herold.at") >= 0) {result = result + "/referer=herold/reftype=register"; }

    if (refererURL.hostname.indexOf("pressetext.at") >= 0) {result = result + "/referer=pressetext.at/reftype=media"; }
    if (refererURL.hostname.indexOf("openpr.de") >= 0) {result = result + "/referer=openpr.de/reftype=media"; }
    if (refererURL.hostname.indexOf("derstandard.at") >= 0) {result = result + "/referer=pressetext.at/reftype=media"; }
    if (refererURL.hostname.indexOf("itmedia.at") >= 0) {result = result + "/referer=pressetext.at/reftype=media"; }
  }

  return result;
}

function checkReferrer() {
	var myVar=generateVar(document.referrer);
	if (myVar!="") {superSetVar(myVar);}
}