var ak_sid;
var ak_intervalo;

var WrCommon =
{
    includeJS: function(sSource)
    {
    
        if(!WrCommon.checkExistScript(sSource))
        {
            var oScript  = document.createElement('script');
      
            oScript.type = "text/javascript";
            oScript.src  = sSource;
            document.getElementsByTagName('head')[0].appendChild(oScript);
        }
    },
  
    checkExistScript: function(sSource)
    {
        var oScripts  = document.getElementsByTagName('script');
        var rePattern = new RegExp(sSource, "g");
    
        for(var i=0; i<oScripts.length; i++)
            if(rePattern.test(oScripts[i].src))
                return true;
  
        return false;
    }
};

function WrLoad()
{
    var ak_wrscript = "http://wr.akna.com.br/webreport/js/?id=" + ak_sid;
    var url = document.location.toString();
    
    if(url.search(/__akacao/) != -1 && url.search(/__akcnt/) != -1 && url.search(/__akvkey/) != -1)
    {
        var key = WrGetKey();
        WrSetCookie("key", key);
    }
    
    if(WrGetCookie("key"))
    {
        WrCommon.includeJS(ak_wrscript);    
        ak_intervalo = window.setInterval("WrCheck()", 1000);
    }
}

function WrCheck()
{
    try
    {
        if(wrload == 1)
        {
            WrSend(WrParameters);
            clearInterval(ak_intervalo);
        }
    } catch(e) {

    }
}

function WrGetCookie (name) 
{
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen)
    {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg)
            return WrGetCookieVal (j);
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) break;
    }
    return false;
}


function WrGetCookieVal (offset) 
{
    var endstr = document.cookie.indexOf (";", offset);
    if (endstr == -1)
        endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));
}

function WrSetCookie (name, value) 
{
    var argv = WrSetCookie.arguments;
    var argc = WrSetCookie.arguments.length;
    var expires = (argc > 2) ? argv[2] : null;
    var path = (argc > 3) ? argv[3] : null;
    var domain = (argc > 4) ? argv[4] : null;
    var secure = (argc > 5) ? argv[5] : false;
    document.cookie = name + "=" + escape (value) +
    ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
    ((path == null) ? "" : ("; path=" + path)) +
    ((domain == null) ? "" : ("; domain=" + domain)) +
    ((secure == true) ? "; secure" : "");
}

function WrGetKey()
{
    var dCur = new Date();
    var co_f = "2";
    var cur = dCur.getTime().toString();
    for (var i=2; i<=(32-cur.length); i++)
    {
        co_f += Math.floor(Math.random()*16.0).toString(16);
    }
    co_f += cur;

    return co_f;
}