﻿Object.prototype.extend = function (o) {
    for (var _p in o.prototype) {
        this.prototype[_p] = o.prototype[_p];
    }
}
Object.extendto = function (destination, source) {
    for (var property in source) {
        destination[property] = source[property];
    }
    return destination;
}
var oFun =
{
    bgId: ["_tansar_pop_bg_id", "pop_bg"],
    dialogId: ["_tansar_pop_dialog_id"],
    dialogType: "DIV",
    themeType: "default",
    rootPath: "/theme/pop/"
}
var odFun =
{
    bgId: ["_tansar_pop_bg_ids", "pop_bg"],
    dialogId: ["_tansar_pop_alert_id"],
    themeType: "pop_alert",
    rootPath: "/theme/pop/",
    dialogType: "DIV"
}
var gPopupMask = null;
var gPopupContainer = null;
var gPopFrame = null;
var gReturnFunc;
var gPopupIsShown = false;
var gDefaultPage = "/loading.html";
var gHideSelects = false;
var gReturnVal = null;
var gPopContent = null;
var gTabIndexes = new Array();
var gTabbableTags = new Array("A", "BUTTON", "TEXTAREA", "INPUT", "IFRAME");

if (!document.all) {
    document.onkeypress = keyDownHandler;
}

function RemoveId(id) {
    var d = document;
    var o = d.getElementById(id);
    if (o != null || o != undefined)
        d.body.removeChild(o);
}
function clearPop() {
    RemoveId(oFun.bgId[0]);
    RemoveId(oFun.dialogId[0]);

    removeEvent(window, "resize", centerPopWin, false);
    removeEvent(window, "scroll", centerPopWin, false);
}
function clearAlert() {
    RemoveId(odFun.dialogId[0]);
    RemoveId(odFun.bgId[0]);
    removeEvent(window, "resize", centerPopwins, false);
    removeEvent(window, "scroll", centerPopwins, false);
}
function addEvent(obj, evType, fn) {
    if (obj.addEventListener) {
        obj.addEventListener(evType, fn, false);
        return true;
    } else if (obj.attachEvent) {
        var r = obj.attachEvent("on" + evType, fn);
        return r;
    } else {
        return false;
    }
}
function removeEvent(obj, evType, fn, useCapture) {
    if (obj.removeEventListener) {
        obj.removeEventListener(evType, fn, useCapture);
        return true;
    } else if (obj.detachEvent) {
        var r = obj.detachEvent("on" + evType, fn);
        return r;
    } else {
        alert("Handler could not be removed");
    }
}
function setMaskSize() {
    var theBody = document.getElementsByTagName("BODY")[0];

    var fullHeight = getViewportHeight();
    var fullWidth = getViewportWidth();

    // Determine what's bigger, scrollHeight or fullHeight / width
    if (fullHeight > theBody.scrollHeight) {
        popHeight = fullHeight;
    } else {
        popHeight = theBody.scrollHeight;
    }

    if (fullWidth > theBody.scrollWidth) {
        popWidth = fullWidth;
    } else {
        popWidth = theBody.scrollWidth;
    }

    gPopupMask.style.height = popHeight + "px";
    gPopupMask.style.width = popWidth + "px";
}
function getViewportHeight() {
    if (window.innerHeight != window.undefined) return window.innerHeight;
    if (document.compatMode == 'CSS1Compat') return document.documentElement.clientHeight;
    if (document.body) return document.body.clientHeight;

    return window.undefined;
}
function getViewportWidth() {
    var offset = 17;
    var width = null;
    if (window.innerWidth != window.undefined) return window.innerWidth;
    if (document.compatMode == 'CSS1Compat') return document.documentElement.clientWidth;
    if (document.body) return document.body.clientWidth;
}

function getScrollTop() {
    if (self.pageYOffset) // all except Explorer
    {
        return self.pageYOffset;
    }
    else if (document.documentElement && document.documentElement.scrollTop)
    // Explorer 6 Strict
    {
        return document.documentElement.scrollTop;
    }
    else if (document.body) // all other Explorers
    {
        return document.body.scrollTop;
    }
}
function getScrollLeft() {
    if (self.pageXOffset) // all except Explorer
    {
        return self.pageXOffset;
    }
    else if (document.documentElement && document.documentElement.scrollLeft)
    // Explorer 6 Strict
    {
        return document.documentElement.scrollLeft;
    }
    else if (document.body) // all other Explorers
    {
        return document.body.scrollLeft;
    }
}
function $(id) {
    return document.getElementById(id);
}
// Tab key trap. iff popup is shown and key was [TAB], suppress it.
// @argument e - event - keyboard event that caused this function to be called.
function keyDownHandler(e) {
    if (gPopupIsShown && e.keyCode == 9) return false;
}

// For IE.  Go through predefined tags and disable tabbing into them.
function disableTabIndexes() {
    if (document.all) {
        var i = 0;
        for (var j = 0; j < gTabbableTags.length; j++) {
            var tagElements = document.getElementsByTagName(gTabbableTags[j]);
            for (var k = 0; k < tagElements.length; k++) {
                gTabIndexes[i] = tagElements[k].tabIndex;
                tagElements[k].tabIndex = "-1";
                i++;
            }
        }
    }
}

// For IE. Restore tab-indexes.
function restoreTabIndexes() {
    if (document.all) {
        var i = 0;
        for (var j = 0; j < gTabbableTags.length; j++) {
            var tagElements = document.getElementsByTagName(gTabbableTags[j]);
            for (var k = 0; k < tagElements.length; k++) {
                tagElements[k].tabIndex = gTabIndexes[i];
                tagElements[k].tabEnabled = true;
                i++;
            }
        }
    }
}


/**
* Hides all drop down form select boxes on the screen so they do not appear above the mask layer.
* IE has a problem with wanted select form tags to always be the topmost z-index or layer
*
* Thanks for the code Scott!
*/
function hideSelectBoxes() {
    var x = document.getElementsByTagName("SELECT");

    for (i = 0; x && i < x.length; i++) {
        x[i].style.visibility = "hidden";
    }
}

/**
* Makes all drop down form select boxes on the screen visible so they do not 
* reappear after the dialog is closed.
* 
* IE has a problem with wanting select form tags to always be the 
* topmost z-index or layer.
*/
function displaySelectBoxes() {
    var x = document.getElementsByTagName("SELECT");

    for (i = 0; x && i < x.length; i++) {
        x[i].style.visibility = "visible";
    }
}
