
if (!("logname" in window)) {
   if (window.document.forms.length > 0) {
      if ("logname" in window.document.forms[0]) {
         logname = window.document.forms[0].logname.value;
         //alert("logname: "+ logname);
      }
   }
   else {
     logname = "n/a";
     //alert("logname: "+ logname);
   }
}

var sessionid = "";

var start = 0;
var end = 0;

  var cookies = window.document.cookie;
 // alert("COOKIE in new: "+cookies);
  
  var pos = cookies.indexOf("RegalixUserId=");
  //alert("RegalixUserId pos: "+ pos);
  if (pos >= 0) {
    start = pos + 14;
    end = cookies.indexOf(";",start);
    if (end == -1) {
      end = cookies.length;
    }
    userid = cookies.substring(start,end);
  }
  
  pos = cookies.indexOf("SESSIONID=");
   //alert("SESSIONID pos: "+ pos);
    if (pos >= 0) {
      start = pos + 10;
      end = cookies.indexOf(";",start);
      if (end == -1) {
        end = cookies.length;
      }
      sessionid = cookies.substring(start,end);
  }
  
  pos = cookies.indexOf("regalix_tracking_id");
  //alert("regalix_tracking_id pos: "+ pos);
  if (pos >= 0) {
     Delete_Cookie("regalix_tracking_id","/",false);
  }

//UserID from the querystring
var queryObj = null;
if ("queryStr" in this) {
   queryObj = parseQueryString( queryStr );
}
else {
   queryObj = parseQueryString( location.search );
}
var uid = queryObj.uid

var userid = uid;
if ((uid == null) || (uid == "undefined"))
{
  uid = 1;
}

if (uid == 1) {
  var userid = getUserId();
 }
//alert("userid: "+ userid);

if(window.pUrl==null) pUrl="";
if(window.pCid==null)pCid="";

// get the user agent --------------------------------------
var rsRUA=navigator.userAgent;
var rsUA=navigator.appName+" "+navigator.appVersion;
var rsBV=navigator.appVersion.substring(0, 1);
var rsNN=(rsUA.indexOf('Netscape'));
var rsMC=(rsUA.indexOf('Mac'));
var rsIE=(rsUA.indexOf('MSIE'));
var rsXP=(rsUA.indexOf('NT 5.1'));
var rsCE=(rsUA.indexOf('Windows CE'));
var rsLX=(rsUA.indexOf('Linux'));
var rsOP=(rsRUA.indexOf('Opera'));
var rsIEV=(parseInt(rsUA.substr(rsIE+5)));
var rsMSIE=false;var rsIE6=false;var rsOSXP=false;var rsIE6XP=false;
var rsSW="na";var rsSH="na";var rsCD="na";var rsSR="na";var rsLG="na";var rsCT="na";var rsHP="na";var rsCK="na";var rsJE="na";
var rsJE=(navigator.javaEnabled()==true)?"y":"n";
var rsD= new Date();
var rsTZ=rsD.getTimezoneOffset()/-60;
if((rsNN!=-1)&&(rsBV >=4)||(rsOP>=0)){var rsLG=navigator.language;}if((rsIE!=-1)&&(rsBV >=4)&&(rsOP==-1)){var rsLG=navigator.userLanguage;}

// get the user agent name ----------------------------------
v = navigator.appName;

// get the screen resolution --------------------------------
var rsWS=window.screen;
if((typeof(rsWS)!="undefined")&&(rsWS!=null)){
rsSW=rsWS.width;
rsSH=rsWS.height;
rsCD=rsWS.colorDepth;
if((rsNN!=-1)&&(rsBV >=4)){
rsCD=rsWS.pixelDepth;}}

// are cookies enabled -------------------------------------
var rsCK=(navigator.cookieEnabled==true)?"y":"n";

// are cookies enabled -------------------------------------
var rsOS=navigator.platform

// get the screen size --------------------------------------
s = rsSW+"x"+rsSH;

// get the document's title ---------------------------------
t = escape(document.title);

// get the document's referrer -------------------------------
var f = "";

// if pp_frames is true then try getting the framed referral (without error checking)
if (typeof(pp_frames) != "undefined")
  if (pp_frames)
    f = top.document.referrer;

// get the referral for non-multi-domained-framed sites using a Netscape browser
if ((f == "") || (f == "[unknown origin]") || (f == "unknown") || (f == "undefined"))
  if (document["parent"] != null)
    if (parent["document"] != null) // ACCESS ERROR HERE!
      if (parent.document["referrer"] != null)
        if (typeof(parent.document) == "object")
          f = parent.document.referrer;

// get the referral for the current document if a framed referral wasn't found
if ((f == "") || (f == "[unknown origin]") || (f == "unknown") || (f == "undefined"))
  if (document["referrer"] != null)
    f = document.referrer;

// convert all the unknown's into blank
if ((f == "") || (f == "[unknown origin]") || (f == "unknown") || (f == "undefined"))
  f = "";

// escape the referral
f = escape(f);
if(f.length>=252)f=f.substring(0,252)+"...";

if (rsCK == "y") {
   //alert("Write: "+userid+" | "+sessionid);
   var expiration = new Date();
   expiration.setFullYear(expiration.getFullYear()+2);
   window.document.cookie = "RegalixUserId="+userid+"; expires="+expiration.toGMTString()+
   "; path=/";
   window.document.cookie = "RegalixTrackSessionId="+sessionid+"; expires="+expiration.toGMTString()+
   "; path=/";

}


var mandatory = new Array("nid","keyword","channel","camp","adkey","adgroup","debug");
var additional = new Object();

var nid = queryObj.nid
//alert("nid: "+ nid);
if ((nid == null) || (nid == "undefined")){
  nid = 1;
}

var keyword = queryObj.keyword;
if ((keyword == null) || (keyword == "undefined")){
  keyword = "n/a";
}
//alert(keyword);

var channel = queryObj.channel;
if ((channel == null) || (channel == "undefined")){
  channel = "n/a";
}

var camp = queryObj.camp;
if ((camp == null) || (camp == "undefined")){
  camp = "n/a";
}
//alert(camp);

var adkey = queryObj.adkey;
if ((adkey == null) || (adkey == "undefined")){
  adkey = "n/a";
}
//alert(adkey);

var adgroup= queryObj.adgroup;
if ((adgroup == null) || (adgroup == "undefined")){
  adgroup = "n/a";
}
//alert(adgroup);

var type= queryObj.type;
if ((type == null) || (type == "undefined")){
  type = "null";
}
//alert(type);

var debug= queryObj.debug;
if ((debug == null) || (debug == "undefined")){
  debug = "false";
}

searchparam:
for (var name in queryObj) {
   for (i = 0; i < mandatory.length; i++) {
     if (name.toLowerCase() == mandatory[i]) {
       this[name.toLowerCase()] = queryObj[name];
       delete queryObj[name];
       continue searchparam;
     }
   }
   additional[name] = queryObj[name];
}

if (companyid == undefined) {
  var companyid = window.document.title.split("|")[0];
}


if ("convertor" in this) {
   for (var name in additional) {
     for (j = 0; j < convertor.length; j +=2) {
       if (name.toLowerCase() == convertor[j]) {
            this[convertor[j+1]] = additional[name];
            delete additional[name];
       }
     }
   }
}

r="?userid="+userid+
   "&pageaccessed="+t+
   "&logname="+logname+
   "&remotebrowser="+rsUA+
   "&remoteos="+rsOS+
   "&deviceuseragent="+rsRUA+
   "&reffererurl="+f+
   "&windowdimensions="+s+
   "&userlanguage="+rsLG+
   "&iscookieenabled="+rsCK+
   "&keyword="+keyword+
   "&channel="+channel+
   "&camp="+camp+
   "&adkey="+adkey+
   "&adgroup="+adgroup+
   "&type="+type+
   "&project="+companyid+
   "&sessionid="+sessionid+
   "&debug="+debug;
  
  
  if(window['custom2'] != undefined){
    r += "&custom2="+custom2;
    window.document.cookie = "refererIdCookie="+custom2+"; expires="+expiration.toGMTString()+"; path=/";
  }
  

  if(window['custom3'] != undefined){	  
    r += "&custom3="+custom3;
	    window.document.cookie = "regPageCookie="+custom3+"; expires="+expiration.toGMTString()+"; path=/";
  }

  if(window['custom4'] != undefined){
    r += "&custom4="+custom4;
  }
  
  if(window['custom5'] != undefined){
    r += "&custom5="+custom5;
  }

   for (var name in additional) {
      r += "&"+name+"="+additional[name];
   }
//alert("r: "+r);
// adding logid if called by st='phpjs'

if(jslogid==null) var jslogid = 0;
else r = r + "&jslogid="+jslogid;



if (v != "Microsoft Internet Explorer") {
  r = r+"&url="+document.URL; // $HTTP_REFERER problem with NS,...
}
document.open();
//alert("before: "+ getcookie("regalix_tracking_id"));
//document.write("\<script language=\"JavaScript\" type=\"text/javascript\" src=\"http://tr.regalix.com/tracking/track"+r+"\"\>\</script\>");
document.write('\<script language="JavaScript" type="text/javascript" src="http://tr.regalix.com/tracking/track'+r+'"\></script\>');
//alert("after: " + getcookie("regalix_tracking_id"));
document.close();

function parseQueryString(queryString) {
  var queryObject = new Object();
  queryString = queryString.replace(/^.*\?(.+)$/,'$1');

  while ((pair = queryString.match(/([^=]+)=\'?([^\&\']*)\'?\&?/)) && pair[0].length) {
    queryString = queryString.substring( pair[0].length );

    if (/^\-?\d+$/.test(pair[2])) pair[2] = parseInt(pair[2]);
    queryObject[pair[1].toLowerCase()] = pair[2];
  }

  return queryObject;
}

function getcookie(cookiename) {
 var cookiestring=""+document.cookie;
 var index1=cookiestring.indexOf(cookiename);
 if (index1==-1 || cookiename=="") return ""; 
 var index2=cookiestring.indexOf(';',index1);
 if (index2==-1) index2=cookiestring.length; 
 return unescape(cookiestring.substring(index1+cookiename.length+1,index2));
}

function getUserId() {
  return Math.ceil(Math.random()*1000)+"_"+(new Date()).getTime();
}

function Delete_Cookie( name, path, domain) {
     
        document.cookie = name + "=" +
        ( ( path ) ? ";path=" + path : "") +
        ( ( domain ) ? ";domain=" + domain : "" ) +
        ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}