//by Andres Castiglia - 04-Feb-2008
var perfTimer;
var wReceive = CusNow(); //at start
var idReq = "";
var wSend = "";
var wComplete = "";
var pPerf = "";
function getCookie(c_name)
{
    if (document.cookie.length>0)
    {
        c_start=document.cookie.indexOf(c_name + "=");
        if (c_start!=-1)
        { 
            c_start=c_start + c_name.length+1; 
            c_end=document.cookie.indexOf(";",c_start);
            if (c_end==-1) c_end=document.cookie.length;
            return unescape(document.cookie.substring(c_start,c_end));
        } 
    }
    return "";
} 
function setCookie(c_name,value,expiredays)
{
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}
function NewGUID()
{
    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 CusNow()
{
    var date = new Date();
    var d  = date.getDate();
    var day = (d < 10) ? '0' + d : d;
    var m = date.getMonth() + 1;
    var month = (m < 10) ? '0' + m : m;
    var yy = date.getYear();
    var year = (yy < 1000) ? yy + 1900 : yy;
    var h  = date.getHours();
    var hour = (h < 10) ? '0' + h : h;
    var n = date.getMinutes();
    var minute = (n < 10) ? '0' + n : n;
    var s = date.getSeconds();
    var second = (s < 10) ? '0' + s : s;
    var ms = date.getMilliseconds();
    var millisecond = (ms < 10) ? '00' + ms : ((ms < 100) ? '0' + ms : ms);
    return year+month+day+hour+minute+second+millisecond;
}
function customload()
{
    //wReceive = CusNow(); //at start (dejar comentado)
    wComplete = CusNow(); //at finish
    idReq = getCookie("cIdReq");
    wSend = getCookie("cSend");
    pPerf = getCookie("pPerf");
    setCookie("cIdReq","",-1);
    setCookie("cSend","",-1);
    setCookie("pPerf","",-1);
    if (idReq!="" && wSend!="" && wReceive!="" && wComplete!="")  
    {
        var img=document.createElement("img");
        img.setAttribute("border","0");img.setAttribute("width","1");img.setAttribute("height","1");
        img.setAttribute("src","/PerformanceHandler.ashx?id="+escape(idReq)+"&snd="+escape(wSend)+"&rec="+escape(wReceive)+"&fin="+escape(wComplete)+"&asp="+escape(pPerf));
        document.lastChild.appendChild(img);
    }
}
function customonbeforeunload()
{
    setCookie("cIdReq",NewGUID(),1);
    setCookie("cSend",CusNow(),1);
}
if( window.addEventListener ) {
window.addEventListener('load',customload,false); //legacy
} else if( document.addEventListener ) {
document.addEventListener('load',customload,false); //proper
} else if( window.attachEvent ) {
window.attachEvent("onload", customload); //IE only
} else {
window.onload = customload;
}
if( window.addEventListener ) {
window.addEventListener('onbeforeunload',customonbeforeunload,false); //legacy
} else if( document.addEventListener ) {
document.addEventListener('onbeforeunload',customonbeforeunload,false); //proper
} else if( window.attachEvent ) {
window.attachEvent("onbeforeunload", customonbeforeunload); //IE only
} else {
window.onbeforeunload = customonbeforeunload;
}