﻿var CRLF = String.fromCharCode(13) + String.fromCharCode(10);
var flagScroll = 0;

/******************* Alert Dialog script *******************/

function ShowDialogAlert(agrs) {
    //debugger;
    var dlg = document.getElementById("dlgDelete")
    dlg.value = agrs;
    ShowDialog(dlg);
}

function dialogOK(event, argument) {
    __doPostBack(event, argument);
}

function ShowDialog(dialog) {
    
    var splitValue = dialog.value.split("@%#*");
    var event = splitValue[0];
    var argument = splitValue[1];
    var type = splitValue[2];
    var title = splitValue[3];
    var message = splitValue[4];
    var cancelBtn = splitValue[6];
    var okBtn = splitValue[5];

    var html = ('<div unselectable=on id="alertDialog" class="btnDlgOuterContainer" style="overflow: visible; position: absolute; cursor:default; left: 520px; top: 250px; z-index: 500;">'
             + '<table cellspacing="0" cellpadding="0">'
             + '<tbody><tr><td id="dlgAlertTitleText" class="titleRw" style="font-size: 11px;">' + title + '</td>'
             + '<td class="titleRw"><img class="btn" src="../../Images/closedlg.gif" onclick="DlgCloseAlertDialog()" align="right"/></td></tr>'
             + '<tr><td colspan="2">'
             + '<table class="dlgBody" cellpadding="0" cellspacing="0"><tbody>'
             + '<tr><td class="dlgMsgBox"><table width="100%">'
             + ((type != "") ? '<tr><td><img id="dlgImageIcon" class="dlgimg" src="' + getDlgIcn(type) + '"/></td><td style="font-size: 11px;">' + message : ('<td>' + message))
             + '</td></tr></table>'
             + '<tr><td class="dlgBtnBox" style="text-align: right;" nowrap>'
             + ((type != "Question") ? '<button id="dlgButtonOK" style="width:70px;" onclick="DlgCloseAlertDialog()">'+ okBtn +'</button>'
                : ("<button id=\"dlgButtonOK\" style=\"width:70px;\" onclick=\"dialogOK('" + event + "','" + argument + "')\">"+ okBtn +"</button>&nbsp;"
             + '<button id="dlgButtonCancel" style="width:70px;" onclick="DlgCloseAlertDialog()">' + cancelBtn + '</button>'))
             + '</td></tr></tbody></table></td></tr></tbody></table></div>');

    DlgShowAlertDialog(html);
}

function getDlgIcn(i) {
    switch (i) {
        case "Info":
            return "../../Images/info.gif";
        case "Error":
            return "../../Images/error.gif";
        case "Warning":
            return "../../Images/warning.gif";
        case "Question":
            return "../../Images/quest.gif";
    }
}

function DlgShowAlertDialog(strinput) {
    var newdiv = document.createElement('div');
    var alertdlg = document.getElementById('dlgDelete');

    newdiv.className = 'alertoverlay';
    newdiv.id = 'alertoverlay';
    newdiv.style.zIndex = 500;

    var getWH = GetPageWithHeight();

    var h = getWH[1] + 'px';
    newdiv.style.height = h;

    var w = getWH[0] + 'px';
    newdiv.style.width = w;
    alertdlg.appendChild(newdiv);

    newdiv = document.createElement('div');
    newdiv.className = 'alertdiadiv';
    newdiv.id = 'alertdiadiv';
    newdiv.style.zIndex = 500;
    newdiv.style.height = h;
    newdiv.style.width = w;

    newdiv.innerHTML = BuildDivAlert(strinput);
    alertdlg.appendChild(newdiv);

    var dlgalert = document.getElementById('alertDialog');

    if (dlgalert != null && dlgalert != 'undefined') {
        // event handlers 
        if (!document.all) document.captureEvents(Event.MOUSEMOVE | Event.MOUSEDOWN | Event.MOUSEUP);
        document.getElementById('dlgAlertTitleText').onmouseover = function() { document.getElementById('dlgAlertTitleText').style.cursor = 'move'; }
        document.getElementById('dlgAlertTitleText').onmousedown = function mouseDown() { mouseDownDialog('alertDialog') };
        document.onmouseup = function() { document.onmousemove = null; };
    }

    window.onresize = ResizeNewDialog;
    window.onscroll = ScrollWindowDialog;
}

function ScrollWindowDialog() {
    //debugger;
    flagScroll = 1;
    ResizeNewDialog();
}

function BuildDivAlert(AContents) {
    var buf = new StringBuffer();
    buf.xAppend([CRLF, AContents, CRLF]);
    return buf.toString();
}

function DlgCloseAlertDialog() {
    var main = document.getElementById('dlgDelete');
    e = document.getElementById('alertoverlay');
    main.removeChild(e);
    var e = document.getElementById('alertdiadiv');
    main.removeChild(e);
}

/*********************** End Alert Dialog script ***********************/

/**************** Move dialog box *****************/

var moveElem;
var x, y;

function mouseDownDialog(id) {
    startMove(event, document.getElementById(id));
}

function startMove(e, elem) {
    if (!e) e = window.event;
    if (e.preventDefault) e.preventDefault();
    if (!setMouseCommon(e)) return true;
    moveElem = elem;
    document.onmousemove = mouseMove;
    return false;
}

function setMouseCommon(e) {
    if (e.offsetX) {
        x = e.offsetX;
        y = e.offsetY;
        var o = e.srcElement;
        x += o.offsetLeft + 2;
        y += o.offsetTop;
        o = o.offsetParent;
    }
    return true;
}

function mouseMove(e) {
    if (!e) e = window.event;
    moveElem.style.position = 'absolute';
    moveElem.style.left = (e.clientX - x) + 'px';
    moveElem.style.top = (e.clientY - y) + 'px';
    return false;
}
/******************** End Move dialog box *********************/

/****** Positioning DialogBox to the Center of Browser *******/

function ShowDialogInCenter(id) {
    var dialog = document.getElementById(id);
    var getWH = GetPageWithHeight();

    var bWidth = getWH[0];
    var bHeight = getWH[1];

    dialogWidth = dialog.offsetWidth;
    dialogHeight = dialog.offsetHeight;

    dialogTop = (bHeight / 2) - (dialogHeight / 2);
    dialogLeft = (bWidth / 2) - (dialogWidth / 2);

    dialog.style.top = dialogTop + "px";
    dialog.style.left = dialogLeft + "px";
}

/****** End positioning DialogBox to the Center of Browser *******/

/******* Resize window script *********
*** when resizing browser window, the dialog is reposition to the center of browser ***/
function ResizeNewDialog() {
    var w = 0, h = 0, scrollx = 0, scrolly = 0;

    var getWH = GetPageWithHeight();
    var getScrollWH = getScrollXY();

    var dlgalert = document.getElementById('alertDialog');

    w = getWH[0];
    h = getWH[1];
    scrollx = getScrollWH[0];
    scrolly = getScrollWH[1];

    if (flagScroll == 1) {
        flagScroll = 0;
        w += scrollx;
        h += scrolly;
    }
    else
    {
        if (scrollx > w) {
        w = w + (scrollx - w);
        }
        if (scrolly > h) {
            h = h + (scrolly - h);
        }
    }

    var overlayDivalert = document.getElementById('alertoverlay');
    if (overlayDivalert != null && overlayDivalert != 'undefined') {
        overlayDivalert.style.height = h + 'px';
        overlayDivalert.style.width = w + 'px';
    }

    var resizeDivalert = document.getElementById('alertdiadiv');
    if (resizeDivalert != null && resizeDivalert != 'undefined') {
        resizeDivalert.style.height = h + 'px';
        resizeDivalert.style.width = w + 'px';
        if (dlgalert != null && dlgalert != 'undefined') {
            ShowDialogInCenter('alertDialog');
        }
    }
}

function GetPageWithHeight() {
    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 != 0) {
//        //IE 6+ in 'standards compliant mode'
//        w = document.documentElement.clientWidth;
//        h = document.documentElement.clientHeight;
//    }
//    else if (document.body) {
//        w = document.body.clientWidth;
//        h = document.body.clientHeight;
//    }
    //    return [w, h];

    if (typeof (window.innerWidth) == 'number') {
        //Non-IE
        w = window.innerWidth;
        h = window.innerHeight;
    }
    //    else if (document.documentElement && document.documentElement.clientWidth != 0) {
    //        //IE 6+ in 'standards compliant mode'
    //        w = document.documentElement.clientWidth;
    //        h = document.documentElement.clientHeight;
    //    }
    //    else if (document.body) {
    //        w = document.body.clientWidth;
    //        h = document.body.clientHeight;
    //    }
    if (navigator.appName.indexOf("Microsoft") != -1) {
        w = document.body.offsetWidth;
        h = document.body.offsetHeight;
    }
    return [w, h];

}

function getScrollXY() {
//    var scrOfX = 0, scrOfY = 0;
//    if (typeof (window.pageYOffset) == 'number') {
//        //Netscape compliant
//        scrOfY = window.pageYOffset;
//        scrOfX = window.pageXOffset;
//    }
//    else if (document.body) {
//        //DOM compliant

//        if (navigator.appName.indexOf("Microsoft") != -1 && navigator.appVersion.indexOf("MSIE 7") != -1) {
//            scrOfY = document.body.scrollHeight;
//            scrOfX = document.body.scrollWidth;
//        }
//        else {
//            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];

    var scrOfX = 0, scrOfY = 0;
    if (typeof (window.pageYOffset) == 'number') {
        //Netscape compliant
        scrOfY = window.pageYOffset;
        scrOfX = window.pageXOffset;
    }
    else if (document.body) {
        //DOM compliant

        if (navigator.appName.indexOf("Microsoft") != -1) {
            scrOfY = document.documentElement.scrollTop;
            scrOfX = document.documentElement.scrollLeft;
        }
        else {
            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];
    
}

/****** End Resize window script ***********/

/**********************************************************************
/ StringBuffer is a simple utility object which delivers
/ faster string concatenation than the Javascript + operator
***********************************************************************/
function StringBuffer() { this.buffer = [] }
StringBuffer.prototype.append = function append(string) { this.buffer.push(string) }
StringBuffer.prototype.xAppend =
function xAppend(strings) {
    var i, ALen;
    ALen = strings.length;
    for (i = 0; i < ALen; i++) this.buffer.push(strings[i]);
}
StringBuffer.prototype.cleanUp = function cleanUp() { this.buffer = [] }
StringBuffer.prototype.toString = function toString() { return this.buffer.join("") }
