﻿//alert('add onload');
var autoLogoutEnabled = new Boolean(false);


function autoLogout() {

        if (autoLogoutEnabled == true) {
        
        //Log the user out of the system
        //alert('signing you out');
        Sys.Services.AuthenticationService.logout(null,
                null, null, null);
        //alert(window.location);
        }
    
}


function chain(oldFunc) {
    if (oldFunc) {
        return function() {
            //alert(oldFunc + "29090");
            autoLogoutEnabled = false;
            oldFunc.call(this, arguments);
        }
    } else {
        return function() {
            //alert('anon2');
            autoLogoutEnabled = false;
        }
    }
}


//Detect if the user clicked on a link on the page; if so don't autologout
//function detectAutoLogoutEvent(e) {
//    if (e) tg = e.target;
//    else tg = window.event.srcElement;
//    //    if (tg != undefined)
//        alert('href: ' + tg.href)
////    else
//    alert('body clicked');
//    autoLogoutEnabled = false;
//}

//function addEventSimple(obj, evt, fn) {
//    //alert(obj.innerHTML);
//    if (obj.addEventListener)
//        obj.addEventListener(evt, fn, false);
//    else if (obj.attachEvent) {
//        obj.onclick = chain(obj.onclick, fn);
//    }
//}

//Setup links on the page to trigger the "detect" event
function setupAutologout() {
    //Only enable Autosignout when the brower location is in the "intranet" tree OR it uses a getDoc
    if (window.top.location.toString().toLowerCase().match("intranet") != null || window.top.location.toString().toLowerCase().match("getdoc") != null) {
        autoLogoutEnabled = true;
        //alert('IE5');
    
        var a = document.getElementsByTagName('A');
        for (var i = 0; i < a.length; i++) {
            
            //If a link opens outside of the browser do not attach event in method chain OR if lin
            if (a[i].target != '_blank' && a[i].href.toString().match("javascript:__doPostBack") == null && a[i].href.toString().match("mailto:") == null) {
                //if(a[i].innerHTML.match("Now") != null)
                //alert(a[i].href.toString().match("javascript:__doPostBack"));
                a[i].onclick = chain(a[i].onclick);
            }
            else if (a[i].href.toString().match("javascript:__doPostBack") != null) {
                //alert(a[i].href.replace(/javascript:/i, "javascript:autoLogoutEnabled=false; "));
                a[i].href = a[i].href.replace(/javascript:/i, "javascript:autoLogoutEnabled=false; ");
            }
            
        }
        //alert('changed');
        var b = document.getElementsByTagName('INPUT');
        for (var i = 0; i < b.length; i++) {
            if (b[i].type == 'submit') {
                //alert(b[i].value);
                b[i].onclick = chain(b[i].onclick);
            }
        }

        var sel = document.getElementsByTagName('SELECT');
        for (var i = 0; i < sel.length; i++) {
            //alert(sel[i].onchange);
            if (sel[i].onchange)
                sel[i].onchange = chain(sel[i].onchange);
        }
    }    
}

function dumpProps(obj, parent) {
    // Go through all the properties of the passed-in object
    for (var i in obj) {
        // if a parent (2nd parameter) was passed in, then use that to
        // build the message. Message includes i (the object's property name)
        // then the object's property value on a new line
        if (parent) { var msg = parent + "." + i + "\n" + obj[i]; } else { var msg = i + "\n" + obj[i]; }
        // Display the message. If the user clicks "OK", then continue. If they
        // click "CANCEL" then quit this level of recursion
        if (!confirm(msg)) { return; }
        // If this property (i) is an object, then recursively process the object
        if (typeof obj[i] == "object") {
            if (parent) { dumpProps(obj[i], parent + "." + i); } else { dumpProps(obj[i], i); }
        }
    }
}

function addOnloadEvent(fnc) {
    if (typeof window.addEventListener != "undefined")
        window.addEventListener("load", fnc, 1);
    else if (typeof window.attachEvent != "undefined") {
        window.attachEvent("onload", fnc);
    }
    else {
        if (window.onload != null) {
            var oldOnload = window.onload;
            window.onload = function(e) {
                oldOnload(e);
                window[fnc]();
            };
        }
        else
            window.onload = fnc;
    }
}

addOnloadEvent(setupAutologout);

