﻿var asCurrentModal = 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(this.Close(), 3000);
};

asModal.prototype.ShowModal = function(params) {
if (asModal.showMask) {
    //    amLoadMask();
};
asCurrentModal = this.id;
var winHeight = document.body.clientHeight;
var winWidth = document.body.clientWidth;
var divTop = (winHeight / 2) - (this.height / 2);
var divLeft = (winWidth / 2) - (this.width / 2);
var shell = amLoadShell(this.title, this.closeImage, divTop, divLeft, this.height, this.width);
var modalBody = document.getElementById('ammodalbody');
var div = document.getElementById(this.id);


shell.style.position = 'absolute';
shell.style.top = divTop + 'px';
//shell.style.cssFloat = 'left';
shell.style.left = divLeft + 'px';

//shell.style.zIndex = '2000';
shell.style.display = '';
div.style.display = '';
div.style.width = this.width + 'px';
div.style.height = this.height + 'px';
div.style.zIndex = 500;
modalBody.appendChild(div);
//    var btnDiv = document.createElement('div');
//    btnDiv.setAttribute('id', 'ammodalbuttonbar');
//    btnDiv.className = 'ammodalbuttonbar';
//    var btn = document.createElement('input');
//    btn.setAttribute('type', 'button');
//    btn.setAttribute('value', 'Save');
//    btn.className = 'ambutton';
//    if (saveFunction != undefined) {
//        btn.setAttribute('onclick', saveFunction);
//        btnDiv.appendChild(btn);
//    };
//    btn = document.createElement('input');
//    btn.setAttribute('type', 'button');
//    btn.setAttribute('value', 'Cancel');
//    btn.className = 'ambutton';
//    btn.setAttribute('onclick', 'amCloseModal(' + modalDiv + ');');
//    btnDiv.appendChild(btn);
//    modalBody.appendChild(btnDiv);
//    var cb = eval(div.firstChild.id);

var cb = eval(this.id);
if (cb != null) {
    cb.Callback(this.view, this.params)
};
};
asModal.prototype.Show = function(params) {

    if (asModal.showMask) {
        //    amLoadMask();
    };
    asCurrentModal = this.id;
    var winHeight = document.body.clientHeight;
    var winWidth = document.body.clientWidth;
    var divTop;
    if (this.position == 'middle') {
        divTop = (winHeight / 2) - (this.height / 2);
    } else {
        divTop = 200;
    };
    var divLeft = (winWidth / 2) - (this.width / 2);
    var shell = amLoadShell(this.title, this.closeImage, divTop, divLeft, this.height, this.width);
    var modalBody = document.getElementById('ammodalbody');
    var div = document.getElementById(this.id);


    shell.style.position = 'absolute';
    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;
    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('ammodalshell');
    mTable.style.display = 'none';
    var bb = document.getElementById('ammodalbuttonbar');
    var modalBody = document.getElementById('ammodalbody');
    if (bb != null || bb != undefined) {
        modalBody.removeChild(bb);
    };
    
};
function closeOnTimer() {
    timetoclose = setTimeout('amClose()', 2000);
};
asModal.prototype.amClose = function() {
    var mm = document.getElementById('ammodalmask');
    if (mm != null || mm != undefined) {
        var tmp = mm.parentNode
        tmp.removeChild(mm);
    };
    var mTable = document.getElementById('ammodalshell');
    mTable.style.display = 'none';
    var bb = document.getElementById('ammodalbuttonbar');
    var modalBody = document.getElementById('ammodalbody');
    if (bb != null || bb != undefined) {
        modalBody.removeChild(bb);
    };
    clearTimeout(timetoclose);
    asCurrentModal = null;
};
function amClose() {

    var mm = document.getElementById('ammodalmask');
    if (mm != null || mm != undefined) {
        var tmp = mm.parentNode
        tmp.removeChild(mm);
    };
    var mTable = document.getElementById('ammodalshell');
    mTable.style.display = 'none';
    var bb = document.getElementById('ammodalbuttonbar');
    var modalBody = document.getElementById('ammodalbody');
    if (bb != null || bb != undefined) {
        modalBody.removeChild(bb);
    };
    clearTimeout(timetoclose);
    asCurrentModal = null;
};
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 amLoadShell(title,img) {
    var mTable;
    mTable = document.getElementById('ammodalshell');
    if (mTable == undefined) {
        mTable = document.createElement('table');
        mTable.setAttribute('cellpadding', '0');
        mTable.setAttribute('cellspacing', '0');
        var mDiv = document.createElement('div');
        mDiv.setAttribute('id', 'ammodalbody');
        mDiv.setAttribute('class', 'ammodalbody');
        mTable.setAttribute('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', 'ammodaltitle');
        tDiv.innerHTML = title;
        tDiv.className = 'ammodaltitle';
        if (img != '') {
            var cImg = new Image();
            cImg.src = img;
            cImg.setAttribute('style', 'float:right;margin-top:3px;margin-right:3px;cursor:pointer;');
            cImg.style.cursor = 'pointer';
            cImg.onclick = function() { amClose(); };
            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('ammodaltitle');
        tDiv.innerHTML = title;
        var mDiv = document.getElementById('ammodalbody');
        mDiv.r
    };
    return mTable;

};
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 };
};