
InitAnalytics( "www.cosmoapps.com", "Prospecting" );

function InitAnalytics( asAnalyticsServer, asAnalyticsClient )
{

  try 
  {

    var lsVisitorGUID = "";
    var lsSessionGUID = "";
    var lsCookieSupport = CookieSupport();

    if ( lsCookieSupport == "Yes" || lsCookieSupport == "SessionOnly" )
    {

      lsVisitorGUID = ReadCookie( "VisitorGUID" );
      lsSessionGUID = ReadCookie( "SessionGUID" );

      if ( lsVisitorGUID == null || lsVisitorGUID == "" )
      {

        lsVisitorGUID = GetNewGuid();
        lsSessionGUID = GetNewGuid();

        if ( lsCookieSupport == "Yes" )
          SetCookie( "VisitorGUID", lsVisitorGUID, 180 );
        else
          SetCookie( "VisitorGUID", lsVisitorGUID, null );

        SetCookie( "SessionGUID", lsSessionGUID , null );

      }
      else if ( lsSessionGUID == null || lsSessionGUID == "" )
      {
        lsSessionGUID = GetNewGuid();
        SetCookie( "SessionGUID", lsSessionGUID , null );
      }

    }
    else
    {
        lsVisitorGUID = GetNewGuid();
        lsSessionGUID = GetNewGuid();
    }


    //3.  Build the URL to the analytics server.

    var lsAnalyticsUrl = "http://" + asAnalyticsServer + "/" + asAnalyticsClient + "/cosmo/cosmo.aspx?_CosmoComponent=Capture&_CosmoRequest=PageVisit"
                       + "&vg=" + encodeURIComponent( lsVisitorGUID )
                       + "&sg=" + encodeURIComponent( lsSessionGUID )
                       + "&cky=" + encodeURIComponent( lsCookieSupport )
                       + "&ref=" + encodeURIComponent( document.referrer )
                       + "&req=" + encodeURIComponent( document.location );


    //4.  Log the page view to the Analtics Server. 

    var elDivAnalytics = document.getElementById( "divAnalytics" );
  
    if ( elDivAnalytics != null ) 
      elDivAnalytics.innerHTML = "<img src=\"" + lsAnalyticsUrl + "\" alt=\".\" border=\"0\">"
    else
      alert("Error could not find Analytics DIV.");

  }
  catch (e)
  {
    alert("Error in InitAnalytics(): " + e.number);
  }
}




function CookieSupport()
{

  var lsCookieSupport = "No";

  SetCookie( "CookieSupport", "SessionOnly", null );

  if ( ReadCookie( "CookieSupport" ) == "SessionOnly" )
    lsCookieSupport = "SessionOnly";  

  SetCookie( "CookieSupport", "Yes", 5 );

  if ( ReadCookie( "CookieSupport" ) == "Yes" )
    lsCookieSupport = "Yes";

  SetCookie( "CookieSupport", "Remove", null );

  return lsCookieSupport;

}


function ReadCookie( asCookie ) 
{

  var lsCookies = "" + document.cookie;

  if ( asCookie == "" )
    return null

  var liStart = lsCookies.indexOf( asCookie );

  if ( liStart == -1 ) 
    return null;
 
  var liEnd = lsCookies.indexOf( ';', liStart );

  if ( liEnd == -1 ) 
    liEnd = lsCookies.length; 

  return unescape( lsCookies.substring( liStart + asCookie.length + 1, liEnd ) );

}

function SetCookie( asCookieName, asCookieValue, aiDays ) 
{

  var lsExpires = "";

  if ( aiDays != null )
  {
    var ldToday = new Date();
    var ldExpire = new Date();

    ldExpire.setTime( ldToday.getTime() + 3600000 * 24 * aiDays );

    lsExpires = ";expires=" + ldExpire.toGMTString();
  }

  document.cookie = asCookieName + "=" + escape( asCookieValue ) + lsExpires + ";path=/";

}

function GetNewGuid()
{

  var g = "";

  for( var i=0; i<32; i++ )
    g += Math.floor( Math.random() * 0xF ).toString( 0xF )

  return g;

}
