﻿var reqXML;

function LoadXMLDoc(url) {
    if (window.XMLHttpRequest) { //Mozilla, Firefox, Opera 8.01, Safari
        reqXML = new XMLHttpRequest();
        reqXML.onreadystatechange = BuildXMLResults;
        reqXML.open("GET", url, true);
        reqXML.send(null);
    }
    else if (window.ActiveXObject) { //IE
        reqXML = new ActiveXObject("Microsoft.XMLHTTP");
        if (reqXML) {
            reqXML.onreadystatechange = BuildXMLResults;
            reqXML.open("GET", url, true);
            reqXML.send();
        }
    }
    else { //Older Browsers
        alert("Din browser er desværre ikke understøttet!");
    }
}

function BuildXMLResults() {
    if (reqXML.readyState == 4) { //completed state
        if (reqXML.status == 200) { //We got a sucess page back

            //Check to verify the message from the server
            if (reqXML.responseText.indexOf("renew") != -1) {
                ResetSessionWarningTimer(); //restart timer
            }
            else {
                //display that that session expired
                alert("BEC:\nDin Session er desværre allerede udløbet.");
                window.location.href = '/default.aspx';
            }
        }
        else {
            //display server code not be accessed
            alert("BEC:\nDer var problemer med at hente XML data for sessionen. Data:\n" + reqXML.statusText);
            window.location.href = '/default.aspx';
        }
    }
}

function ConfirmUpdate() {
    //Ask them to extend
    if (confirm("BEC:\nDin session udløber om " + _WarnMinutesBefore + " minutter.\n\nData der ikke er gemt vil gå tabt,\nhvis din session ikke bliver fornyet senest " + _ExpireTimeString + ".\n\nTryk 'OK' for at forny din session.\n\nMed venlig hilsen\nByggeriets Evaluerings Center")) {
        //load server side page if ok
        LoadXMLDoc('/sessionUpdater.aspx');
    }
}

var timerObj;
var _SessionTimeOut = 0;
var _WarnMinutesBefore = 0;
var _Delay = 0;
var _Initialized = false;
var _ExpireTimeString = '';
function InitializeSessionTimeoutTimer(SessionTimeOut, WarnMinutesBefore) {
    var now = new Date();
    _SessionTimeOut = SessionTimeOut;
    _WarnMinutesBefore = WarnMinutesBefore;
    _Delay = 1000 * 60 * (_SessionTimeOut - _WarnMinutesBefore);
    _Initialized = true;
    ResetSessionWarningTimer();
}
function ResetSessionWarningTimer() {

    if (!_Initialized) {
        alert("Session Expiration Script:Must call InitializeSessionTimeoutTimer() on page initialization!");
        return;
    }

    timerObj = setTimeout("ConfirmUpdate()", _Delay);
    var date = new Date();
    date = date.AddMinutes(_SessionTimeOut);
    _ExpireTimeString = getFormattedDate(date);
}
//InitializeSessionTimeoutTimer(20, 2);
function getFormattedDate(date) {
    var d = new Date();
    return PadDigits(date.getDate(), 2) + "/" + PadDigits(date.getMonth() + 1, 2) + "/" + PadDigits(date.getFullYear(), 4) + " klokken " + PadDigits(date.getHours(), 2) + ":" + PadDigits(date.getMinutes(), 2) + ":" + PadDigits(date.getSeconds(), 2);
}
function PadDigits(n, totalDigits) {
    n = n.toString();
    var pd = '';
    if (totalDigits > n.length) {
        for (i = 0; i < (totalDigits - n.length); i++) {
            pd += '0';
        }
    }
    return pd + n.toString();
}
// Date functions
Date.prototype.AddDays = function(days) {
    this.setDate(this.getDate() + days);
    return this;
}

Date.prototype.AddHours = function(hours) {
    this.setHours(this.getHours() + hours);
    return this;
}

Date.prototype.AddMilliseconds = function(milliseconds) {
    this.setMilliseconds(this.getMilliseconds() + milliseconds);
    return this;
}

Date.prototype.AddMinutes = function(minutes) {
    this.setMinutes(this.getMinutes() + minutes, this.getSeconds(), this.getMilliseconds());
    return this;
}

Date.prototype.AddMonths = function(months) {
    this.setMonth(this.getMonth() + months, this.getDate());
    return this;
}

Date.prototype.AddSeconds = function(seconds) {
    this.setSeconds(this.getSeconds() + seconds, this.getMilliseconds());
    return this;
}

Date.prototype.AddYears = function(years) {
    this.setFullYear(this.getFullYear() + years);
    return this;
}


