/*
   name - name of the cookie
   value - value of the cookie
   [expires] - expiration date of the cookie
     (defaults to end of current session)
   [path] - path for which the cookie is valid
     (defaults to path of calling document)
   [domain] - domain for which the cookie is valid
     (defaults to domain of calling document)
   [secure] - Boolean value indicating if the cookie transmission requires
     a secure transmission
   * an argument defaults when it is assigned null as a placeholder
   * a null placeholder is not required for trailing omitted arguments
*/

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


/*
  name - name of the desired cookie
  return string containing value of specified cookie or null
  if cookie does not exist
*/

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


/*
   name - name of the cookie
   [path] - path of the cookie (must be same as path used to create cookie)
   [domain] - domain of the cookie (must be same as domain used to
     create cookie)
   path and domain default if assigned null or omitted if no explicit
     argument proceeds
*/

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

// date - any instance of the Date object
// * hand all instances of the Date object to this function for "repairs"

function fixDate(date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    date.setTime(date.getTime() - skew);
}

function URLEncode( URL )
{
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var plaintext = URL;
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	return encoded;
};

function URLDecode( URL )
{
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef"; 
   var encoded = URL;
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
	   if (ch == "+") {
	       plaintext += " ";
		   i++;
	   } else if (ch == "%") {
			if (i < (encoded.length-2) 
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
		   plaintext += ch;
		   i++;
		}
	} // while
   return plaintext;
};

function getURLParam(strParamName){
  var strReturn = "";
  var strHref = window.location.href;
  if ( strHref.indexOf("?") > -1 ){
    var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
    var aQueryString = strQueryString.split("&");
    for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
      if (aQueryString[iParam].indexOf(strParamName + "=") > -1 ){
        var aParam = aQueryString[iParam].split("=");
        strReturn = aParam[1];
        break;
      }
    }
  }
  return strReturn;
}

function getSite() {
	return getURLParam("utm_source");
}

function getCampaign() {
  return getURLParam("utm_campaign");
}

function getKeyword() {
  return getURLParam("utm_term");
}

function getReferrer() {
  return URLEncode(document.referrer);
}

function _uVoid() { return; }

function getHash() {
 var str = document.location.href;
 hash = 0;
 for (i=0; i<str.length; i++) 
   hash = hash + str.charCodeAt(i);

 return hash;
}


function trackVisit() {

 var tag = 0;
 if (getCookie('_tag')) tag = 1;

 if (!getCookie('_site')) {
	 site = getSite();
	 if (!site) { 
		url = document.referrer;
		if (url) {
			url1 = url.split("?");
			if (url1) {
				url2 = url1[0];
				if (url2) {
					url3 = url2.split("//");
					if (url3) {
						url4 = url3[1];
						if (url4) {
							url5 = url4.split("/");
							if (url5) 
								site = url5[0];
						}
					}
				}
			}
		}
	}

  setCookie('_site', site, 0, '/');
 }

 if (!getCookie('_campaign'))
   setCookie('_campaign', getCampaign(), 0, '/');

 if (!getCookie('_keyword'))
   setCookie('_keyword', getKeyword(), 0, '/');

 var i=new Image(1,1);
 i.src = _trackpath + tracker_id + "?utm_source=" + getSite() + "&utm_campaign=" + getCampaign() +
   '&utm_term=' + getKeyword() + '&utm_medium=' + getReferrer() + '&tag=' + tag;

 return;
}

function trackAction() {
  d = new Date();
  d.setDate(d.getDate()+90)

  var tag = 0;
  var hash = getHash();
  if (getCookie('_url' + hash)) tag = 1; else tag = 0;

	url = document.location.href;
	url1 = url.split("?");
	url2 = url1[0];

  if (getCookie('_site'))
   _site = getCookie('_site');
  else _site = getSite();

  if (getCookie('_campaign'))
   _campaign = getCookie('_campaign');
  else _campaign = getCampaign();

  if (getCookie('_keyword'))
   _keyword = getCookie('_keyword');
  else _keyword = getKeyword();

  var i=new Image(1,1);
  i.src = _trackpath + tracker_id + "?url=" + URLEncode(url2) +
   "&utm_source=" + _site + "&utm_campaign=" + _campaign +
   "&utm_term=" + _keyword + '&utm_medium=' + getReferrer() + "&tag=" + tag;

//  setCookie('_url' + hash, document.location.href, d, '/');

  return;
}

function tracker() {
  d = new Date();
  d.setDate(d.getDate()+90)
 
  _trackpath = 'http://www.statnip.com/';
  //_trackpath = 'http://et/';
  trackVisit();
  trackAction();
  setCookie('_tag', 1, d, '/');
  deleteCookie('_url', '/');
}