var USER_ID = "CelebrosAnalyticsUserId";

function createAnalyticsImage (sSrc)
{
    var jsElement = document.getElementById('anlximg');
    if (jsElement)
    {
        jsElement.parentNode.removeChild(jsElement);        
    }
    // Use the same protocol as the window.
    if (window.location.protocol == "http:")
    {
        sSrc = "http:" + sSrc;
    }
    else if (window.location.protocol == "https:")
    {
        sSrc = "https:" + sSrc;
    }
    else
    {
        return;
    }           
          
    var anlxImg = new Image();
    anlxImg.src = sSrc;
//    anlxImg.setAttribute("id","anlximg");
//    anlxImg.setAttribute("src",sSrc);
//    anlxImg.setAttribute("height",0);
//    anlxImg.setAttribute("display","none");
//    anlxImg.setAttribute("width",0);
//    document.getElementById("divSuggestions").insertAdjacentElement("afterEnd",anlx);
}

function generateGuid()
{
    var result, i, j;
    result = '';
    for(j=0; j<32; j++)
    {
        if( j == 8 || j == 12|| j == 16|| j == 20)
            result = result + '-';
        i = Math.floor(Math.random()*16).toString(16).toUpperCase();
        result = result + i;
    }
    return result
}

function createCookie (name,value,days,domain) {    
    var sCookieString;
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	sCookieString = name+"="+value+expires+"; path=/";
	if (domain)
	{
	    sCookieString = sCookieString + "; domain="+domain;
	}
	document.cookie = sCookieString
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie (name) {
	createCookie(name,"",-1);
}

function GetBaseAnalyticsImage(sSrc, sCustomerId)
{
    var sReferrer = "";
    if (document.referrer)
    {
        sReferrer = document.referrer;
    }
    
    var sUserId = readCookie(USER_ID)
    if (sUserId == null)
    {
        sUserId = generateGuid();
    }    
    //Re-Create cookie to update expired date
    createCookie(USER_ID,sUserId,30);
    
    var theDate = new Date();		// create Date object with the current date;
    var sGMT = theDate.toGMTString();	// convert date to a GMT string;
        
    sSrc += "?CustomerId="+escape(sCustomerId);   
    sSrc += "&UserId=" +escape(sUserId);
    sSrc += "&ReferringUrl=" + escape(sReferrer);
    sSrc += "&PageUrl=" + escape(document.URL);
    sSrc += "&GMTTime=" + escape(sGMT);
    
    return sSrc;
}

function celebrosAnalytics(sCustomerId)
{
    sAnalyticsLocation = "//"  +sCustomerId + "-aca.celebros-analytics.com/AutoCompleteAnalytics/tbanalytics.htm";
    //sAnalyticsLocation = "//207.97.232.85/AutoCompleteAnalytics/tbanalytics.htm";
    sSrc = GetBaseAnalyticsImage(sAnalyticsLocation, sCustomerId);
    createAnalyticsImage(sSrc);    
}