// BEGIN client-specific values


var clientID = "st";
var trackSession = true;  // Whether to set cookies and track session
var trackLinks = false;
var getMetaOK = false;     // Whether to send META keywords and description tags
// END client-specific values

//var trackScript="www.zinetool.com/webstats/buildstats.php";
var trackScript="www.roiwebsolutions.com/webstats/buildstats.php";

// TEMPORARY FOR TESTING
//var trackScript="localhost/zinetool.com/webstats/buildstats.php";


var today = new Date();
var year = today.getYear();
var month = today.getMonth() + 1;
var day = today.getDate();
var hours = today.getHours();
var minutes = today.getMinutes();
var seconds = today.getSeconds();
var string = today.toString();
var yyyy = year.toString();
var yearlength = yyyy.length;
switch (yearlength) {
  case 2:
    yyyy = "20" + yyyy;
    break;
    
  case 3:
    yyyy = "20" + yyyy.substr(1, 2);
    break;
}


var mm = ((month.toString().length < 2) ? "0" + month : month);
var dd = ((day.toString().length < 2) ? "0" + day : day);
var hh = ((hours.toString().length < 2) ? "0" + hours : hours);
var min2 = ((minutes.toString().length < 2) ? "0" + minutes : minutes);
var ss = ((seconds.toString().length < 2) ? "0" + seconds : seconds);
var datestamp = yyyy + "-" + mm + "-" + dd + " " + hh + "-" + min2 + "-" + ss;

var oneMinute = 60 * 1000  // milliseconds in a minute
var oneHour = oneMinute * 60
var oneDay = oneHour * 24
var oneWeek = oneDay * 7
var oneYear = oneDay * 365

var expireDateInMS = today.getTime() + (oneYear * 10)
var expireDate = new Date(expireDateInMS)

var zero_datestamp = "0000-00-00 00:00:00";;

var S_id = "";
var S_li = "";
var S_lt = "";
var ZT_id = "";
if (trackSession) {
  
  var thecookie = document.cookie;
  ZT_id = getCookie("ZT_id");
  ZT_lastdate = getCookie("ZT_ts");
  if (! ZT_lastdate) {
    ZT_lastdate = zero_datestamp;
  }
  S_id = getCookie("S_id");
  
  // The next two cookies will have been set by
  // the referring html page if (and only if) it has
  // specific link tracking enabled.
  // Link tracking uses the functions at end of this script
  if (trackLinks) {
    S_li = getCookie("S_li");
    S_lt = getCookie("S_lt");
  }
  if (! S_id) {
    // The id is randomly generated
    S_id = yyyy + "-" + mm + "-" + dd + "-" + Math.floor(Math.random()*1000000);
    setCookie("S_id", S_id);
    
    // Set a persistent cookie for the time this session started
    setCookie("ZT_ts", datestamp, expireDate);
    
    // If there is not a persistent user ID set on this client, create one now.
    if (ZT_id.length < 10) {
      ZT_id = S_id;
      setCookie("ZT_id", ZT_id, expireDate);
    }
  }
}


var gImages=new Array;
var gIndex=0;
var J=new Object();
var P=new Object();
var V=new Object();
var M=new Object();

var rTagPath=trackScript;

if (! S_id) {
  var S_id_string = "";
} else {
  var S_id_string = "S_id=" + S_id + "&";
}

if (! S_li) {
  var S_li_string = "";
} else {
  var S_li_string = "S_li=" + S_li + "&";
}

if (! S_lt) {
  var S_lt_string = "";
} else {
  var S_lt_string = "S_lt=" + S_lt + "&";
}

if (! ZT_id) {
  var ZT_id_string = "";
} else {
  var ZT_id_string = "ZT_id=" + ZT_id + "&";
}

if (typeof(ZT_lastdate)=="undefined") {
  var ZT_ts_string = "";
} else {
  var ZT_ts_string = "ZT_lastdate=" + ZT_lastdate + "&";
}

var addStrings = S_id_string + S_li_string + S_lt_string + ZT_id_string + ZT_ts_string;

getJPV();
if (getMetaOK) {
  getMeta();
}

buildQS(rTagPath, addStrings);

function getJPV(){
	V.tz=today.getTimezoneOffset()/60*-1;
	V.ul=navigator.appName=="Netscape"?navigator.language:navigator.userLanguage;
	if (typeof(screen)=="object"){
		V.cd=screen.colorDepth;
		V.sr=screen.width+"x"+screen.height;
	}
	if (typeof(navigator.javaEnabled())=="boolean"){
		J.jd=navigator.javaEnabled()?"Yes":"No";
	}
	J.js="Yes";
	if (typeof(gVersion)!="undefined"){
		J.jv=gVersion;
	}

	P.path=window.location.pathname;
  var qslength = window.location.search.length;
  if (qslength > 0) {
	  P.qs=window.location.search.substr(1, qslength-1);
  }
  
	if ((window.document.referrer!="")&&(window.document.referrer!="-")){
		if (!(navigator.appName=="Microsoft Internet Explorer"&&parseInt(navigator.appVersion)<4)){
			V.ref=window.document.referrer;
      // Strip off http://
      if (V.ref.length > 7) {
        if (V.ref.substr(0, 7) == "http://") {
          V.ref = V.ref.substr(7);
        }
      }
      // Strip off https://
      if (V.ref.length > 8) {
        if (V.ref.substr(0, 8) == "https://") {
          V.ref = V.ref.substr(8);
        }
      }
		}
	}
	P.host=window.location.hostname;
  
	V.ts=datestamp;
}

function getMeta(){
	var myDocumentElements;
	if (document.all){
		myDocumentElements=document.all.tags("meta");
	}
	else if (document.documentElement){
		myDocumentElements=document.getElementsByTagName("meta");
	}
	if (typeof(myDocumentElements)!="undefined"){
		for (var i=1;i<=myDocumentElements.length;i++){
			myMeta=myDocumentElements.item(i-1);
			if (myMeta.name){
        metatype = myMeta.name;
        eval("M." + metatype + " = myMeta.content");
      }
    }
	}
	M.title=document.title;
}

function buildQS(TagImage, addStrings){
  
  QS = addStrings + "clientID=" + clientID;
	for (N in J){
		if (J[N]) {
      QS+=A("J_"+N,J[N]);
		}
	}
	for (N in P){
		if (P[N]) {
			QS+=A("P_"+N,P[N]);
		}
	}
	for (N in V){
		if (V[N]) {
			QS+=A("V_"+N,V[N]);
		}
	}
	for (N in M){
		if (M[N]) {
			QS+=A("M_"+N,M[N]);
		}
	}
	if (QS.length>2048&&navigator.userAgent.indexOf('MSIE')>=0){
		QS=QS.substring(0,2040)+"&MS.tu=1";
	}
   
  var SRC="http"+(window.location.protocol.indexOf('https:')==0?'s':'')+"://"+TagImage+"?" + QS;

	CreateImage(SRC);
}

function A(Tag,Var){
	return "&"+Tag+"="+escape(Var);
}

function CreateImage(SRC){

	// Debug
  /*
  src_length = SRC.length;
  for (i=0; i<src_length; i=i+50) {
    debug_string = SRC.substr(i,50);
    //alert("i=" + i + " debug_string=" + debug_string);
    alert("i=" + i);
    alert("debug_string=" + debug_string);
  }
  */
  
  if (document.images){
    gImages[gIndex]=new Image;
		gImages[gIndex].src=SRC;
		gIndex++;
	}
	else{
    document.write('<IMG BORDER="0" NAME="TRACKIMG" WIDTH="1" HEIGHT="1" SRC="'+SRC+'">');
	}

}

function setCookie(name, value, expires, path, domain, secure) {
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

function getCookie(name) {
    var prefix = name + "=";
    var begin = thecookie.indexOf("; " + prefix);
    if (begin == -1) {
        begin = thecookie.indexOf(prefix);
        if (begin != 0) return "";
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = thecookie.length;
    }
    return unescape(thecookie.substring(begin + prefix.length, end));
}

////////////////////////////////////////////////////////////////////////////////

// The following code set cookies to track
// which link was clicked to leave a page

// Variables used for link tracking by the onClick and onUnload of the page this is in
var linktext = "";
var linkid = "";


// onClick for individual links
function trackLink(anchortext, id) {
  linkid = id;
  linktext = anchortext;
}

// onUnload for checking which outgoing link was clicked
function checkLink() {
  setCookie("S_li", linkid);
  setCookie("S_lt", linktext);
}



