﻿

function DisableControls() {
    for (var i = 0; i < document.forms[0].elements.length; i++) {
        var obj = document.forms[0].elements[i];
        obj.disabled = true;
    }
}
function EnableControls() {
    for (var i = 0; i < document.forms[0].elements.length; i++) {
        var obj = document.forms[0].elements[i];
        obj.disabled = false;
    }
}
function GetXmlHttpObject() {
    var xmlHttp = null;
    try {
        // Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
    }
    catch (e) {
        // Internet Explorer
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp;
}
function findPos(obj) {

    var curleft = curtop = 0;

    if (obj.offsetParent) {
        curleft = obj.offsetLeft

        curtop = obj.offsetTop

        while (obj = obj.offsetParent) {
            curleft += obj.offsetLeft

            curtop += obj.offsetTop

        }

    }
    return [curleft, curtop];
}
function getCookie(name) {
    var start = document.cookie.indexOf(name + "=");
    var len = start + name.length + 1;
    if ((!start) && (name != document.cookie.substring(0, name.length))) {
        return null;
    }
    if (start == -1) return null;
    var end = document.cookie.indexOf(';', len);
    if (end == -1) end = document.cookie.length;
    return unescape(document.cookie.substring(len, end));
}

function setCookie(name, value, expires, path, domain, secure) {
    var today = new Date();
    today.setTime(today.getTime());
    if (expires) {
        expires = expires * 1000 * 60 * 60 * 24;
    }
    var expires_date = new Date(today.getTime() + (expires));
    document.cookie = name + '=' + escape(value) +
		((expires) ? ';expires=' + expires_date.toGMTString() : '') + //expires.toGMTString()
		((path) ? ';path=' + path : '') +
		((domain) ? ';domain=' + domain : '') +
		((secure) ? ';secure' : '');
}

function deleteCookie(name, path, domain) {
    if (getCookie(name)) document.cookie = name + '=' +
			((path) ? ';path=' + path : '') +
			((domain) ? ';domain=' + domain : '') +
			';expires=Thu, 01-Jan-1970 00:00:01 GMT';
}


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 getScrollXY() {
    var scrOfX = 0, scrOfY = 0;
    if (typeof (window.pageYOffset) == 'number') {
        //Netscape compliant
        scrOfY = window.pageYOffset;
        scrOfX = window.pageXOffset;
    } else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
        //DOM compliant
        scrOfY = document.body.scrollTop;
        scrOfX = document.body.scrollLeft;
    } else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
        //IE6 standards compliant mode
        scrOfY = document.documentElement.scrollTop;
        scrOfX = document.documentElement.scrollLeft;
    }
    return [scrOfX, scrOfY];
}

function getInnerSize() {
    var myWidth = 0, myHeight = 0;
    if (typeof (window.innerWidth) == 'number') {
        //Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }
    return [myWidth, myHeight];
}


function ScrollToElement(theElement) {

    var selectedPosX = 0;
    var selectedPosY = 0;

    while (theElement != null) {
        selectedPosX += theElement.offsetLeft;
        selectedPosY += theElement.offsetTop;
        theElement = theElement.offsetParent;
    }

    window.setTimeout("window.scrollTo(" + selectedPosX + "," + selectedPosY + ")", 100);

}

function GetCenteredXY(w, h) {
    var ps = getScrollXY();
    var sz = getInnerSize();
    var Left = (sz[0] - w) / 2 + ps[0];
    var Top = (sz[1] - h) / 2 + ps[1];
    return [Math.ceil(Left), Math.ceil(Top)];
}

function dateToString(d) {
    return d.getDate() + '/' + (d.getMonth() + 1) + '/' + d.getFullYear() + ' ' + d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds();
}

function dx_OpenPopup(s, e, popup, width, height, url, HeaderText) {
    var ps = GetCenteredXY(width, height);
    var popupInstance = eval('window.' + popup);
    popupInstance.SetContentHTML('');
    popupInstance.SetHeaderText(HeaderText);
    popupInstance.ShowAtPos(ps[0], ps[1]);
    if (url.indexOf('?') == -1)
        url += '?'; else
        url += '&';
    popupInstance.SetContentUrl(url + 'rand=' + Math.random() + '&instance=' + popup);
    popupInstance.SetSize(width, height);
    if (e) e.processOnServer = false;
}
function ShowWindow(s, e, popup) {
    var popupInstance = eval('window.' + popup);
    popupInstance.PopUp();
}
function HideWindow(s, e, popup) {
    var popupInstance = eval('window.' + popup);
    popupInstance.CloseUp();
}

function OpenHelp(context) {
    //TODO: WebMailUILanguage
    window.open('help.htm#' + context, 'webmailhelp', 'left=50,top=100,width=400,height=300,scrollbars=yes');
}

function AvoidCache(el) {
    var rand_no = Math.ceil(Math.random() * 100000);
    el.href = el.href.replace(/R=\d+/i, 'R=' + rand_no);
    if (el.href.indexOf('R=') == -1)
        if (el.href.indexOf('?') == -1)
        el.href = el.href + '?R=' + rand_no; else
        el.href = el.href + '&R=' + rand_no;
}
