document.writeln('<div id="picabooLoggerDiv" style="visibility: hidden">');
document.writeln('<img id="picabooLogger" width="0" height="0">');
document.writeln('</div>');
function getPageParameter(vParam)
{
    vParam = vParam.toLowerCase();
    var query=this.location.search.substring(1);
    if (query.length > 0){
        var params=query.split("&");
        for (var i=0 ; i<params.length ; i++){
            var pos = params[i].indexOf("=");
            var name = (params[i].substring(0, pos)).toLowerCase();
            if (name == vParam) {
				return params[i].substring(pos + 1);
			}
        }
    }
    return "";
}

function getSource()
{
    vValue = getPageParameter('utm_source');
	if ( vValue == '') {
		vValue = cookie.source;
	}
	return vValue;
}


function metaCookie(document, name, expiration, path, domain)
{
    this.$document = document;
    this.$name = name;
    if (expiration)
        this.$expiration = expiration;
    else
        this.$expiration = null;

    if (path) this.$path = path; else this.$path = null;
    if (domain) this.$domain = domain; else this.$domain = null;
}

metaCookie.prototype.store = function () {
    var cookieval = "";
    for(var prop in this) {
        if ((prop.charAt(0) == '$') || ((typeof this[prop]) == 'function')) 
            continue;
        if (cookieval != "") cookieval += '&';
        cookieval += prop + ':' + escape(this[prop]);
    }

    var cookie = this.$name + '=' + cookieval;
    if (this.$expiration)
        cookie += '; expires=' + this.$expiration.toGMTString();
    if (this.$path)
       cookie += '; path=' + this.$path
    else
       cookie += '; path=/';
    if (this.$domain) cookie += '; domain=' + this.$domain;

    this.$document.cookie = cookie;
}


metaCookie.prototype.load = function() { 
    var allcookies = this.$document.cookie;
    if (allcookies == "") return false;

    var start = allcookies.indexOf(this.$name + '=');
    if (start == -1) return false;
    start += this.$name.length + 1;
    var end = allcookies.indexOf(';', start);
    if (end == -1) end = allcookies.length;
    var cookieval = allcookies.substring(start, end);

    var a = cookieval.split('&');
    for(var i=0; i < a.length; i++)
        a[i] = a[i].split(':');

    for(var i = 0; i < a.length; i++) {
        this[a[i][0]] = unescape(a[i][1]);
    }

    return true;
}

metaCookie.prototype.remove = function() {
    var cookie;
    cookie = this.$name + '=';
    if (this.$path) cookie += '; path=' + this.$path;
    if (this.$domain) cookie += '; domain=' + this.$domain;
    cookie += '; expires=Fri, 02-Jan-1970 00:00:00 GMT';

    this.$document.cookie = cookie;
}

function getUserId()
{
	return cookie.userId;
}

function getSiteID()
{
    vValue = getPageParameter('siteID');
    if ( vValue == '') {
	vValue = cookie.siteID;
    }
    return vValue;
}

function picabooGetSiteID()
{
        return cookie.siteID;
}



function getUserOID()
{
	return cookie.userOID;
}

function lGeneric(type)
{
  var str = new Array();
  logEvent(type, str);

}

function lGeneric(type, arg0, arg1)
{
  var str = new Array();
  var element = new Object();

  element = new Object();
  element.name = arg0;
  element.value = arg1;
  str[0] = element;

  logEvent(type, str);

}

function getTLD(domain) {
	var objRegExp = /[^.]*\.[a-zA-Z]{3}$/i;
	var result = objRegExp.exec(domain);
	var tld = "";
	if (result != null) {
		tld = result[0];
	}
	return tld;
	
}


function logEvent(eName, eData)
{
  var logImage = document.getElementById('picabooLogger');
  var fullURL = baseURL + "?event="+eName+"&userUID="+getUserId()+"&userOID="+getUserOID()+"&source="+getSource()+"&siteID="+getSiteID();
  fullURL +=  "&userAgent=" + escape(navigator.userAgent) + "&referrer=" + 

escape(document.referrer) + "&url=" + escape(document.URL);

  try {
	for (var i in eData)
	{
		var eEvent = eData[i];
			
		if (typeof eEvent == 'undefined' || eEvent.name == null) {
			continue;
		}
		if (typeof eEvent.value == 'undefined' || eEvent.value == null) {
			continue;
		}		
		
		fullURL = fullURL + "&" + eEvent.name+"="+eEvent.value;
	}

	logImage.src = fullURL;
  } catch (e)
   {
   }
}

var cookie = new metaCookie(document, "Picaboo", null, "/", getTLD(document.domain));
var baseURL = "/LoggerX/picaboo.aspx";

cookie.load();

if (typeof cookie.userId == "undefined") {
        var firstPart = getPageParameter('userID');
        if ( firstPart == '') {
	        var today = new Date ();
	        var firstPart = (today.getTime()) + (Math.floor(Math.random()*8999)+1000);
	    }
	    expDate = new Date();
	    expDate.setTime(expDate.getTime() + (10 * 365 * 24 * 60 * 60 * 1000)); 

	    cookie.userId = firstPart;
	    cookie.userOID = getPageParameter('installationID');

        if (getPageParameter('utm_source') != '') 
           cookie.source = getPageParameter('utm_source');
        else
 	    cookie.source = "";

        if (getPageParameter('siteID') != '') 
           cookie.siteID = getPageParameter('siteID');
        else
 	    cookie.siteID = "";

        cookie.store();	
} else {
        if (getPageParameter('utm_source') != '') {
           cookie.source = getPageParameter('utm_source');
       }
       if (getPageParameter('siteID') != '') {
           cookie.siteID = getPageParameter('siteID');
       }
       cookie.store();	
}

