﻿var asCurrentModal = null;
var asModalTemp = null;
function asModal(id) {
    this.id = id;
    this.element = this.DomElement = document.getElementById(id);
    this.title = '';
    this.height = 250;
    this.width = 350;
    this.params = '';
    this.view = '';
    this.showMask = true;
    this.closeImage = '';
    this.position = 'middle';
    return this;
};
var timetoclose;
asModal.prototype.closeTimer = function() {
    timetoclose = setTimeout('asModalTemp.Close()', 2000);
};
function getDimensions(){
    var w = 0, h = 0;
    if (typeof (window.innerWidth) == 'number') {
        //Non-IE
        w = window.innerWidth;
        h = window.innerHeight;
    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        //IE 6+ in 'standards compliant mode'
        w = document.documentElement.clientWidth;
        h = document.documentElement.clientHeight;
    } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        //IE 4 compatible
        w = document.body.clientWidth;
        h = document.body.clientHeight;
    };

    return { h: h, w: w };
};
asModal.prototype.Show = function(params) {
    asCurrentModal = this.id;
    asModalTemp = this;
    var dimensions = getDimensions()
    var winWidth = dimensions.w;
    var divTop = 0;
    if (this.position == 'middle') {
        divTop = (asGetViewportHeight() / 2) - (this.height / 2);
        if (document.all) {
            divTop += top.document.documentElement.scrollTop;
        } else {
            divTop += top.pageYOffset;
        };
    } else {
        divTop = 200;
    };
    var divLeft = (winWidth / 2) - (this.width / 2);
    var shell = this.LoadShell();
    var modalBody = document.getElementById(this.id + 'ammodalbody');
    var div = document.getElementById(this.id);
    shell.style.position = 'absolute';
    try {
        shell.style.top = divTop + 'px';
        shell.style.left = divLeft + 'px';
        shell.style.display = '';
        div.style.display = '';
        div.style.width = this.width + 'px';
        div.style.height = this.height + 'px';
        div.style.zIndex = 500;
    } catch (ex) {
    
    };
    
    modalBody.appendChild(div);

    var cb = eval(this.id + '_CB');
    if (cb != null) {

        cb.Callback(this.view, this.params)
    };
};
asModal.prototype.Close = function() {
    var mm = document.getElementById('ammodalmask');
    if (mm != null || mm != undefined) {
        var tmp = mm.parentNode
        tmp.removeChild(mm);
    };
    var mTable = document.getElementById(this.id + 'ammodalshell');
    mTable.style.display = 'none';
    var bb = document.getElementById('ammodalbuttonbar');
    var modalBody = document.getElementById(this.id + 'ammodalbody');
    if (bb != null || bb != undefined) {
        modalBody.removeChild(bb);
    };
    clearTimeout(timetoclose);
    asCurrentModal = null;
    asModalTemp = null;

};
asModal.prototype.LoadShell = function() {
    var mTable;
    mTable = document.getElementById(this.id + 'ammodalshell');
    if (mTable == undefined) {
        mTable = document.createElement('table');
        mTable.setAttribute('cellpadding', '0');
        mTable.setAttribute('cellspacing', '0');
        var mDiv = document.createElement('div');
        mDiv.setAttribute('id', this.id + 'ammodalbody');
        mDiv.setAttribute('class', 'ammodalbody');
        mTable.setAttribute('id', this.id + 'ammodalshell');
        mTable.className = 'ammodalshell';
        var oTR = mTable.insertRow(0);
        var oTD = oTR.insertCell(0);
        oTD.className = 'ammodalUL';
        oTD = oTR.insertCell(1);
        oTD.className = 'ammodalUB';
        oTD = oTR.insertCell(2);
        oTD.className = 'ammodalUR';
        oTR = mTable.insertRow(1);
        oTD = oTR.insertCell(0);
        oTD.className = 'ammodalLB';
        oTD = oTR.insertCell(1);
        oTD.className = 'ammodalContent';
        var tDiv = document.createElement('div');
        tDiv.setAttribute('id', this.id + 'ammodaltitle');
        tDiv.innerHTML = this.title;
        tDiv.className = 'ammodaltitle';
        if (this.closeImage != '') {
            var cImg = new Image();
            cImg.src = this.closeImage;
            cImg.setAttribute('style', 'float:right;margin-top:3px;margin-right:3px;cursor:pointer;');
            cImg.style.cursor = 'pointer';
            cImg.onclick = function() { if (asModalTemp != null) { window.asModalTemp.Close(); }; };
            cImg.setAttribute('align', 'right');
            oTD.appendChild(cImg);
        };

        oTD.appendChild(tDiv);
        oTD.appendChild(mDiv);
        oTD = oTR.insertCell(2);
        oTD.className = 'ammodalRB';
        oTR = mTable.insertRow(2);
        oTD = oTR.insertCell(0);
        oTD.className = 'ammodalBL';
        oTD = oTR.insertCell(1);
        oTD.className = 'ammodalBB';
        oTD = oTR.insertCell(2);
        oTD.className = 'ammodalBR';
        document.body.appendChild(mTable);

    } else {
        var tDiv = document.getElementById(this.id + 'ammodaltitle');
        tDiv.innerHTML = this.title;
        var mDiv = document.getElementById(this.id + 'ammodalbody');
        mDiv.r
    };
    return mTable;

};

function closeOnTimer() {
    timetoclose = setTimeout('window.asModalTemp.Close()', 2000);
};
function amLoadMask() {
    var mDiv = document.createElement('div');
    mDiv.setAttribute('id', 'ammodalmask');
    mDiv.style.position = 'absolute';
    mDiv.style.top = '0px';
    mDiv.style.left = '0px';
    mDiv.style.width = getScrollWidth();
    mDiv.style.height = getScrollHeight();
    //mDiv.style.backgroundColor = 'Gray';
    //mDiv.style.filter = 'alpha(opacity=50)';
    //mDiv.style.opacity = '.5';
    mDiv.style.display = '';
    mDiv.style.zIndex = '1000';
    document.body.appendChild(mDiv);
};

function getScrollWidth() {
    var w = document.body.scrollWidth ||
           document.body.scrollLeft ||
           document.documentElement.scrollLeft;

    return w ? w : 0;
};

function getScrollHeight() {
    var h = document.body.scrollHeight ||
           document.body.scrollTop ||
           document.documentElement.scrollTop;

    return h ? h : 0;
};
function asgetTipPosition(e) {
    var left = 0;
    var top = 0;
    while (e.offsetParent) {
        left += e.offsetLeft;
        top += e.offsetTop;
        e = e.offsetParent;

    };
    left += e.offsetLeft;
    top += e.offsetTop;
    return { x: left, y: top };
};
function asGetViewportHeight() {
    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;
};