﻿// Icons for the Popup Window
var MsgWindowIcons = {
    // build out the image Icons
    CloseWindow: 'WebResource.axd?d=hL0N21Ei2v-XYC_g-kXix1PpiV6xaBMISjU4SBlKIXm09HK2PaasnljLXhGsmLDnE8AedO91VsSNpzmNqg71Hgt69nnsy_0d0&t=634163671280000000',
    Default: 'WebResource.axd?d=hL0N21Ei2v-XYC_g-kXix1PpiV6xaBMISjU4SBlKIXm09HK2PaasnljLXhGsmLDn7PsVqJOVXNRz45LupzP5xA2&t=634163671280000000',
    Info: 'WebResource.axd?d=hL0N21Ei2v-XYC_g-kXix1PpiV6xaBMISjU4SBlKIXm09HK2PaasnljLXhGsmLDn7PsVqJOVXNRz45LupzP5xA2&t=634163671280000000',
    HourGlass: 'WebResource.axd?d=hL0N21Ei2v-XYC_g-kXix1PpiV6xaBMISjU4SBlKIXm09HK2PaasnljLXhGsmLDnoNho-L-wLUlXfMJ9_24DlC_bkZgLHbg70&t=634163671280000000',
    Loading: 'WebResource.axd?d=hL0N21Ei2v-XYC_g-kXix1PpiV6xaBMISjU4SBlKIXm09HK2PaasnljLXhGsmLDnGwvZ7T1jBrO-yPAwuh2cxw_WYEtwxWhg0&t=634163671280000000',
    Loading2: 'WebResource.axd?d=hL0N21Ei2v-XYC_g-kXix1PpiV6xaBMISjU4SBlKIXm09HK2PaasnljLXhGsmLDnGwvZ7T1jBrO3k7ycFM0hYanfNckVlJNb0&t=634163671280000000',
    Loader: 'WebResource.axd?d=hL0N21Ei2v-XYC_g-kXix1PpiV6xaBMISjU4SBlKIXm09HK2PaasnljLXhGsmLDnGwvZ7T1jBrPHsAm7T-jS-GoZRkcaZoYP0&t=634163671280000000',
    Critical: 'WebResource.axd?d=hL0N21Ei2v-XYC_g-kXix1PpiV6xaBMISjU4SBlKIXm09HK2PaasnljLXhGsmLDnE8AedO91VsT66-KcaXQgU33wgPNC28Uv0&t=634163671280000000',
    Error: 'WebResource.axd?d=hL0N21Ei2v-XYC_g-kXix1PpiV6xaBMISjU4SBlKIXm09HK2PaasnljLXhGsmLDnYwzo03YvsYaCn7OBlxqSzGbT_p5bFNva0&t=634163671280000000',
    Help: 'WebResource.axd?d=hL0N21Ei2v-XYC_g-kXix1PpiV6xaBMISjU4SBlKIXm09HK2PaasnljLXhGsmLDnoNho-L-wLUnqoRlwy04TNg2&t=634163671280000000',
    Print: 'WebResource.axd?d=hL0N21Ei2v-XYC_g-kXix1PpiV6xaBMISjU4SBlKIXm09HK2PaasnljLXhGsmLDnUBxSLITtNvJke12IDyI8DYNpdExrwu8Z0&t=634163671280000000',
    Success: 'WebResource.axd?d=hL0N21Ei2v-XYC_g-kXix1PpiV6xaBMISjU4SBlKIXm09HK2PaasnljLXhGsmLDnBfxyhQtZMk1SaNj9Ee7dtBOvMsJPXSj20&t=634163671280000000',
    Resize: 'WebResource.axd?d=hL0N21Ei2v-XYC_g-kXix1PpiV6xaBMISjU4SBlKIXm09HK2PaasnljLXhGsmLDnZGYLNKwkDiMHj8IVeQpNQdynNtt3SLdF0&t=634163671280000000',
    Maximize: 'WebResource.axd?d=hL0N21Ei2v-XYC_g-kXix1PpiV6xaBMISjU4SBlKIXm09HK2PaasnljLXhGsmLDnYwzo03YvsYb3P-TvmmlPJN8on1YVxTtq0&t=634163671280000000',
    Minimize: 'WebResource.axd?d=hL0N21Ei2v-XYC_g-kXix1PpiV6xaBMISjU4SBlKIXm09HK2PaasnljLXhGsmLDnVNrx6FMgqS38uR-Pm95qTemFaNtbIGuA0&t=634163671280000000',
    Dimmer: 'WebResource.axd?d=hL0N21Ei2v-XYC_g-kXix1PpiV6xaBMISjU4SBlKIXm09HK2PaasnljLXhGsmLDnPYJ81eikysOrrEdsC0gpAvZAhsNFTf160&t=634163671280000000'    
};
// preloader
for (prop in MsgWindowIcons) { newImage(MsgWindowIcons[prop]); }   

// the commands for the buttons, and the return values
var MsgWindowButtons = {Ok:'ok', Cancel:'cancel', OkCancel:'okcancel', None:null};

function createButton(text) {
    var b = document.createElement('button');
    b.appendChild(document.createTextNode(text));
    return b;
}

function OkCancel(p) { p.window.close(); return false; }

function MsgWindowOptions(o) { 
    this.height = (o && o.height) ? o.height : 0;
    this.width = (o && o.width) ? o.width : 0;
    this.left = (o && o.left) ? o.left : null;
    this.top = (o && o.top) ? o.top : null;
    this.center = (o && o.center) ? o.center : "middle";
    this.resize = (o && o.resize) ? o.resize : 0;
    this.scrolling = (o && o.scrolling) ? o.scrolling : 1;

    this.modal = (o && typeof o.modal != "undefined") ? o.modal : false;
    this.dragenabled = (o && typeof o.dragenabled != "undefined") ? o.dragenabled : false;
    this.oncommand = (o && o.oncommand) ? o.oncommand : null;
    this.buttons = (o && o.buttons) ? o.buttons : null;
    this.icon = (o && o.icon) ? o.icon : null;
    this.showhandle = (o && typeof o.showhandle != "undefined") ? o.showhandle : true;
    this.showcontrols = (o && typeof o.showcontrols != "undefined") ? o.showcontrols : true;
}

var MsgWindow = {
    minimizeorder: 0,
    zIndexvalue: 20000,
    tobjects: [], //object to contain references to dhtml window divs, for destroy purposes
    lastactivet: {}, //reference to last active DHTML window

    // finds an already created window
    GetMsgWindowCore: function(t) {
        return { MsgWindow: MsgWindow, WebWindow: t, MsgWindowIcons: MsgWindowIcons, MsgWindowButtons: MsgWindowButtons, MsgWindowOptions: MsgWindowOptions, ControlWindow: new ControlWindows() };
    }
    , init: function(id) {
        var domwindow = document.createElement("div") //create dhtml window div
        domwindow.id = id;
        domwindow.className = "MsgWindow"
        var domwindowdata = ''
        domwindowdata = '<div class="drag-handle">'
        domwindowdata += '{Title} <div class="drag-controls"><img src="' + MsgWindowIcons.Minimize + '" title="Minimize" /><img src="' + MsgWindowIcons.CloseWindow + '" title="Close" /></div>'
        domwindowdata += '</div>'
        domwindowdata += '<div class="drag-icon"></div>'
        domwindowdata += '<div class="drag-contentarea"></div>'
        domwindowdata += '<div class="drag-buttons"></div>'
        domwindowdata += '<div class="drag-statusarea"><div class="drag-resizearea" style="background: transparent url(' + MsgWindowIcons.Resize + ') top right no-repeat;">&nbsp;</div></div>'
        domwindowdata += '</div>'
        domwindow.innerHTML = domwindowdata
        el("MsgWindowContainer").appendChild(domwindow)
        //this.zIndexvalue=(this.zIndexvalue)? this.zIndexvalue+1 : 100 //z-index value for DHTML window: starts at 0, increments whenever a window has focus
        var t = el(id);
        var divs = t.getElementsByTagName("div")
        for (var i = 0; i < divs.length; i++) { //go through divs inside dhtml window and extract all those with class="drag-" prefix
            if (/drag-/.test(divs[i].className))
                t[divs[i].className.replace(/drag-/, "")] = divs[i] //take out the "drag-" prefix for shorter access by name
        }
        //t.style.zIndex=this.zIndexvalue //set z-index of this dhtml window
        t.handle._parent = t //store back reference to dhtml window
        t.resizearea._parent = t //same
        t.controls._parent = t //same
        t.iframe = null
        t.onclose = function() { return true } //custom event handler "onclose"
        t.raiseoncommand = function(cmd, args) { if (t.oncommand) { return t.oncommand({ 'command': cmd, 'window': this, 'args': args }); } return false; }
        t.onmousedown = function() { MsgWindow.setfocus(this) } //Increase z-index of window when focus is on it
        t.handle.onmousedown = MsgWindow.setupdrag //set up drag behavior when mouse down on handle div
        t.resizearea.onmousedown = MsgWindow.setupdrag //set up drag behavior when mouse down on resize div
        t.controls.onclick = MsgWindow.enablecontrols
        t.show = function() { MsgWindow.show(this) } //public function for showing dhtml window
        t.hide = function() { MsgWindow.hide(this) } //public function for hiding dhtml window
        t.close = function() { MsgWindow.close(this) } //public function for closing dhtml window (also empties DHTML window content)
        t.setSize = function(w, h) { MsgWindow.setSize(this, w, h) } //public function for setting window dimensions
        t.moveTo = function(x, y) { MsgWindow.moveTo(this, x, y) } //public function for moving dhtml window (relative to viewpoint)
        t.isResize = function(bol) { MsgWindow.isResize(this, bol) } //public function for specifying if window is resizable
        t.isScrolling = function(bol) { MsgWindow.isScrolling(this, bol) } //public function for specifying if window content contains scrollbars
        t.addButton = function(text) { MsgWindow.addButton(this, text); }
        t.autoResize = function() { MsgWindow.autoResize(this); }
        t.load = function(contenttype, contentsource, title) { MsgWindow.load(this, contenttype, contentsource, title) } //public function for loading content into window
        this.tobjects[this.tobjects.length] = t
        return t //return reference to dhtml window div
    },

    open: function(t, contenttype, contentsource, title, msgwindowoptions, recalonload) {
        var d = MsgWindow //reference dhtml window object
        if (el(t) == null) //if window doesn't exist yet, create it
            t = this.init(t) //return reference to dhtml window div
        else
            t = el(t)
        this.setfocus(t);
        // load defaults into options for items not specified
        var options = new MsgWindowOptions(msgwindowoptions);
        // save the options for this window
        t.msgwindowoptions = options;
        t.setSize(options.width, options.height); //Set dimensions of window
        var xpos = options.left ? options.left : "middle"; //Get x coord of window
        var ypos = options.top ? options.top : "middle"; //Get y coord of window
        //t.moveTo(xpos, ypos) //Position window
        if (typeof recalonload != "undefined" && recalonload == "recal" && this.scroll_top == 0) { //reposition window when page fully loads with updated window viewpoints?
            if (window.attachEvent && !window.opera) //In IE, add another 400 milisecs on page load (viewpoint properties may return 0 b4 then)
                this.addEvent(window, function() { setTimeout(function() { t.moveTo(xpos, ypos) }, 400) }, "load")
            else
                this.addEvent(window, function() { t.moveTo(xpos, ypos) }, "load")
        }
        t.isResize(options.resize); //Set whether window is resizable
        t.isScrolling(options.scrolling); //Set whether window should contain scrollbars
        t.style.visibility = "visible"
        t.style.display = "block"

        t.contentarea.style.display = "block"
        t.buttons.style.display = (options.buttons) ? "block" : "none";
        t.icon.style.display = (options.icon) ? "block" : "none";

        t.moveTo(xpos, ypos) //Position window

        // create a modal dimmer
        if (options.modal) { ModalDimmer.Show(t); }
        // set the cursor style for the window
        t.handle.style.cursor = (options.dragenabled == false) ? 'default' : 'move';
        // set a command action
        t.oncommand = options.oncommand;

        t.load(contenttype, contentsource, title)
        if (t.state == "minimized" && t.controls.firstChild.title == "Restore") { //If window exists and is currently minimized?
            t.controls.firstChild.setAttribute("src", MsgWindowIcons.Minimize) //Change "restore" icon within window interface to "minimize" icon
            t.controls.firstChild.setAttribute("title", "Minimize")
            t.state = "fullview" //indicate the state of the window as being "fullview"
        }

        t.handle.style.display = (options.showhandle) ? "block" : "none";
        t.controls.style.display = (options.showhandle && options.showcontrols) ? "block" : "none";

        // process buttons
        if (options.buttons) { MsgWindow.addButtons(t); }
        if (options.icon) { MsgWindow.addIcon(t); }

        return t;
    },

    alert: function(msg, title, icon, cb) {
        title = (title) ? title : 'Application Message';
        icon = (icon) ? icon : MsgWindowIcons.Info;
        var callback = (cb) ? cb : OkCancel;
        var options = { 'Modal': true, 'Height': 50, 'Buttons': MsgWindowButtons.Ok, 'Icon': icon, 'Title': title, 'CallBackFunction': callback, 'ShowCloseWindow': false };
        return Dialogs.show(msg, options);
    },

    requestLoader: function(id, icon) {
        var div = el(id);
        icon = (icon) ? icon : MsgWindowIcons.Loader;
        if (div) {
            var html = '<div id="Loader" style="margin:0 auto;width:100%;padding-top:50px;text-align:center;"><img src="{0}" alt="Loading" border="0"/></div>';
            div.innerHTML = String.format(html, icon);
        }
    },

    requestStart: function(msg, icon) {
        icon = (icon) ? icon : MsgWindowIcons.Loading;
        msg = (msg) ? msg : 'Processing your request, please wait...';
        return Dialogs.show(msg, { Icon: icon, Modal: true, ShowTitlebar: false, Height: 75 });
    },

    requestEnd: function() {
        Dialogs.hide();
    },

    addIcon: function(t) {
        t.icon.innerHTML = '';
        var img = document.createElement('img');
        img.setAttribute('src', t.msgwindowoptions.icon);
        t.icon.appendChild(img);
    },

    addButtons: function(t) {
        t.buttons.innerHTML = ''; // clear out buttons
        switch (t.msgwindowoptions.buttons) {
            case MsgWindowButtons.Ok:
                t.addButton('Ok');
                break;
            case MsgWindowButtons.Cancel:
                t.addButton('Cancel');
                break;
            case MsgWindowButtons.OkCancel:
                t.addButton('Ok');
                t.addButton('Cancel');
                break;
        }
    },

    addButton: function(t, text) {
        var b = createButton(text);
        b.win = t;
        b.onclick = function() { return this.win.raiseoncommand(text); }
        t.buttons.appendChild(b);
        if (text == 'Ok' && b.focus) { b.focus(); }
    },

    setSize: function(t, w, h) { //set window size (min is 150px wide by 100px tall)
        t.style.width = Math.max(parseInt(w), 150) + "px"
        t.contentarea.style.height = Math.max(parseInt(h), 100) + "px"
    },

    moveTo: function(t, x, y) { //move window. Position includes current viewpoint of document
        this.getviewpoint() //Get current viewpoint numbers
        t.style.left = (x == "middle") ? this.scroll_left + (this.docwidth - t.offsetWidth) / 2 + "px" : this.scroll_left + parseInt(x) + "px"
        var ctop = (y == "middle") ? this.scroll_top + (this.docheight - t.offsetHeight) / 2 + "px" : this.scroll_top + parseInt(y) + "px"
        var calcTop = parseInt(ctop);
        t.style.top = (calcTop < 0) ? "50px" : ctop;
    },

    isResize: function(t, bol) { //show or hide resize inteface (part of the status bar)
        t.statusarea.style.display = (bol) ? "block" : "none"
        t.resizeBool = (bol) ? 1 : 0
    },

    isScrolling: function(t, bol) { //set whether loaded content contains scrollbars
        t.contentarea.style.overflow = (bol) ? "auto" : "hidden"
    },

    load: function(t, type, contentsource, title) { //loads content into window plus set its title (3 content types: "inline", "iframe", or "ajax")
        if (t.isClosed) {
            alert("This window has been closed and needs to be open or recreated.")
            return
        }
        var contenttype = type.toLowerCase() //convert string to lower case
        if (typeof title != "undefined")
            t.handle.firstChild.nodeValue = title
        if (contenttype == "inline")
            t.contentarea.innerHTML = contentsource
        else if (contenttype == "div") {
            var inlinedivref = el(contentsource)
            t.contentarea.innerHTML = (inlinedivref.defaultHTML || inlinedivref.innerHTML) //Populate window with contents of inline div on page
            if (!inlinedivref.defaultHTML)
                inlinedivref.defaultHTML = inlinedivref.innerHTML //save HTML within inline DIV
            inlinedivref.innerHTML = "" //then, remove HTML within inline DIV (to prevent duplicate IDs, NAME attributes etc in contents of DHTML window
            inlinedivref.style.display = "none" //hide that div
        }
        else if (contenttype == "iframe") {
            t.contentarea.style.overflow = "hidden" //disable window scrollbars, as iframe already contains scrollbars
            if (!t.contentarea.firstChild || t.contentarea.firstChild.tagName != "IFRAME")  //If iframe tag doesn't exist already, create it first
                t.contentarea.innerHTML = '<iframe src="" MsgWindowId="' + t.id + '" style="margin:0;padding:0;width:100%;height:100%" frameborder="0" name="_iframe-' + t.id + '"></iframe>'

            //set location of iframe window to specified URL
            window.frames["_iframe-" + t.id].location.replace(contentsource);
        }
        t.contentarea.datatype = contenttype //store contenttype of current window for future reference
    },

    setupdrag: function(e) {
        var d = MsgWindow //reference dhtml window object
        var t = this._parent //reference dhtml window div
        d.etarget = this //remember div mouse is currently held down on ("handle" or "resize" div)
        var e2 = window.event || e
        d.initmousex = e2.clientX //store x position of mouse onmousedown
        d.initmousey = e2.clientY
        d.initx = parseInt(t.offsetLeft) //store offset x of window div onmousedown
        d.inity = parseInt(t.offsetTop)
        d.width = parseInt(t.offsetWidth) //store width of window div
        d.contentheight = parseInt(t.contentarea.offsetHeight) //store height of window div's content div
        if (t.contentarea.datatype == "iframe") { //if content of this window div is "iframe"
            t.style.backgroundColor = "#F8F8F8" //colorize and hide content div (while window is being dragged)
            t.contentarea.style.visibility = "hidden"
        }
        document.onmousemove = d.getdistance //get distance travelled by mouse as it moves
        document.onmouseup = function() {
            if (t.contentarea.datatype == "iframe") { //restore color and visibility of content div onmouseup
                t.contentarea.style.backgroundColor = "white"
                t.contentarea.style.visibility = "visible"
            }
            d.stop()
        }
        return false
    },

    getdistance: function(e) {
        var d = MsgWindow
        var etarget = d.etarget
        var e2 = window.event || e
        d.distancex = e2.clientX - d.initmousex //horizontal distance travelled relative to starting point
        d.distancey = e2.clientY - d.initmousey
        if (etarget.className == "drag-handle") { //if target element is "handle" div
            if (etarget._parent.msgwindowoptions.dragenabled == false)
                return false;
            d.move(etarget._parent, e2);
        }
        else if (etarget.className == "drag-resizearea") //if target element is "resize" div
            d.resize(etarget._parent, e2)
        return false //cancel default dragging behavior
    },

    getviewpoint: function() { //get window viewpoint numbers
        var ie = document.all && !window.opera
        var domclientWidth = document.documentElement && parseInt(document.documentElement.clientWidth) || 100000 //Preliminary doc width in non IE browsers
        this.standardbody = (document.compatMode == "CSS1Compat") ? document.documentElement : document.body //create reference to common "body" across doctypes
        this.scroll_top = (ie) ? this.standardbody.scrollTop : window.pageYOffset
        this.scroll_left = (ie) ? this.standardbody.scrollLeft : window.pageXOffset
        this.docwidth = (ie) ? this.standardbody.clientWidth : (/Safari/i.test(navigator.userAgent)) ? window.innerWidth : Math.min(domclientWidth, window.innerWidth - 16)
        this.docheight = (ie) ? this.standardbody.clientHeight : window.innerHeight
    },

    rememberattrs: function(t) { //remember certain attributes of the window when it's minimized or closed, such as dimensions, position on page
        this.getviewpoint() //Get current window viewpoint numbers
        t.lastx = parseInt((t.style.left || t.offsetLeft)) - MsgWindow.scroll_left //store last known x coord of window just before minimizing
        t.lasty = parseInt((t.style.top || t.offsetTop)) - MsgWindow.scroll_top
        t.lastwidth = parseInt(t.style.width) //store last known width of window just before minimizing/ closing
    },

    move: function(t, e) {
        t.style.left = MsgWindow.distancex + MsgWindow.initx + "px"
        t.style.top = MsgWindow.distancey + MsgWindow.inity + "px"
    },

    autoResize: function(t) {
        function resizeIframe(iframeObj) {
            var getFFVersion = navigator.userAgent.substring(navigator.userAgent.indexOf("Firefox")).split("/")[1]
            var FFextraHeight = parseFloat(getFFVersion) >= 0.1 ? 50 : 0; // default 16
            //extra height in px to add to iframe in FireFox 1.0+ browsers
            if (iframeObj && !window.opera) {
                iframeObj.style.display = "block";
                if (iframeObj.contentDocument && iframeObj.contentDocument.body.offsetHeight) //ns6 syntax
                    iframeObj.height = iframeObj.contentDocument.body.offsetHeight + FFextraHeight
                else if (iframeObj.Document && iframeObj.Document.body.scrollHeight) //ie5+ syntax
                    iframeObj.height = parseInt(iframeObj.Document.body.scrollHeight);
            }
        }

        var iframe = window.frames["_iframe-" + t.id];
        if (iframe) {
            resizeIframe(iframe);
        }
    },

    resize: function(t, e) {
        t.style.width = Math.max(MsgWindow.width + MsgWindow.distancex, 150) + "px"
        t.contentarea.style.height = Math.max(MsgWindow.contentheight + MsgWindow.distancey, 100) + "px"
    },

    enablecontrols: function(e) {
        var d = MsgWindow
        var sourceobj = window.event ? window.event.srcElement : e.target //Get element within "handle" div mouse is currently on (the controls)
        if (/Minimize/i.test(sourceobj.getAttribute("title"))) //if this is the "minimize" control
            d.minimize(sourceobj, this._parent)
        else if (/Restore/i.test(sourceobj.getAttribute("title"))) //if this is the "restore" control
            d.restore(sourceobj, this._parent)
        else if (/Close/i.test(sourceobj.getAttribute("title"))) //if this is the "close" control
            d.close(this._parent)
        return false
    },

    minimize: function(button, t) {
        MsgWindow.rememberattrs(t)
        button.setAttribute("src", MsgWindowIcons.Maximize)
        button.setAttribute("title", "Restore")
        t.state = "minimized" //indicate the state of the window as being "minimized"
        t.icon.style.display = t.statusarea.style.display = t.buttons.style.display = t.contentarea.style.display = "none"
        if (typeof t.minimizeorder == "undefined") { //stack order of minmized window on screen relative to any other minimized windows
            MsgWindow.minimizeorder++ //increment order
            t.minimizeorder = MsgWindow.minimizeorder
        }
        t.style.left = "10px" //left coord of minmized window
        t.style.width = "200px"
        var windowspacing = t.minimizeorder * 10 //spacing (gap) between each minmized window(s)
        t.style.top = MsgWindow.scroll_top + MsgWindow.docheight - (t.handle.offsetHeight * t.minimizeorder) - windowspacing + "px"
        t.raiseoncommand('minimize');
    },

    restore: function(button, t) {
        MsgWindow.getviewpoint()
        button.setAttribute("src", MsgWindowIcons.Minimize)
        button.setAttribute("title", "Minimize")
        t.state = "fullview" //indicate the state of the window as being "fullview"
        t.contentarea.style.display = t.style.display = "block"
        t.buttons.style.display = (t.msgwindowoptions.buttons) ? "block" : "none"
        t.icon.style.display = (t.msgwindowoptions.icon) ? "block" : "none"
        if (t.resizeBool) //if this window is resizable, enable the resize icon
            t.statusarea.style.display = "block"
        t.style.left = parseInt(t.lastx) + MsgWindow.scroll_left + "px" //position window to last known x coord just before minimizing
        t.style.top = parseInt(t.lasty) + MsgWindow.scroll_top + "px"
        t.style.width = parseInt(t.lastwidth) + "px"
        t.raiseoncommand('restore');
    },


    close: function(t) {
        if (t.isClosed)
            return true;

        var closewinbol = false;
        try {
            closewinbol = t.onclose()
        }
        catch (err) { //In non IE browsers, all errors are caught, so just run the below
            closewinbol = true
        }
        finally { //In IE, not all errors are caught, so check if variable isn't defined in IE in those cases
            if (typeof closewinbol == "undefined") {
                alert("An error has occured somwhere inside your \"onclose\" event handler")
                closewinbol = true
            }
        }
        if (closewinbol) { //if custom event handler function returns true
            if (t.state != "minimized") //if this window isn't currently minimized
                MsgWindow.rememberattrs(t) //remember window's dimensions/position on the page before closing
            if (window.frames["_iframe-" + t.id]) //if this is an IFRAME DHTML window
                window.frames["_iframe-" + t.id].location.replace("about:blank")
            else
                t.contentarea.innerHTML = "";
            // clean up modaldimmer
            ModalDimmer.Hide(t);
            t.style.display = "none"
            t.isClosed = true //tell script this window is closed (for detection in t.show())
            // notify external script of what is going on
            t.raiseoncommand('close');
        }
        return closewinbol
    },


    setopacity: function(targetobject, value) { //Sets the opacity of targetobject based on the passed in value setting (0 to 1 and in between)
        if (!targetobject)
            return
        if (targetobject.filters && targetobject.filters[0]) { //IE syntax
            if (typeof targetobject.filters[0].opacity == "number") //IE6
                targetobject.filters[0].opacity = value * 100
            else //IE 5.5
                targetobject.style.filter = "alpha(opacity=" + value * 100 + ")"
        }
        else if (typeof targetobject.style.MozOpacity != "undefined") //Old Mozilla syntax
            targetobject.style.MozOpacity = value
        else if (typeof targetobject.style.opacity != "undefined") //Standard opacity syntax
            targetobject.style.opacity = value
    },

    setfocus: function(t) { //Sets focus to the currently active window
        this.zIndexvalue++
        t.style.zIndex = this.zIndexvalue
        t.isClosed = false //tell script this window isn't closed (for detection in t.show())
        this.setopacity(this.lastactivet.handle, 0.5) //unfocus last active window
        this.setopacity(t.handle, 1) //focus currently active window
        this.lastactivet = t //remember last active window
    },


    show: function(t) {
        if (t.isClosed) {
            alert("Window has been closed, so nothing to show. Open/Create the window again.")
            return
        }
        if (t.lastx) //If there exists previously stored information such as last x position on window attributes (meaning it's been minimized or closed)
            MsgWindow.restore(t.controls.firstChild, t) //restore the window using that info
        else
            t.style.display = "block"
        this.setfocus(t)
        t.state = "fullview" //indicate the state of the window as being "fullview"
    },

    hide: function(t) {
        if (t.msgwindowoptions.modal == true) {
            alert('You cannot hide a modal window.  Please use the close() method to remove modal windows.');
            return false;
        }
        t.style.display = "none"
    },

    stop: function() {
        MsgWindow.etarget = null //clean up
        document.onmousemove = null
        document.onmouseup = null
    },

    addEvent: function(target, functionref, tasktype) { //assign a function to execute to an event handler (ie: onunload)
        var eventMethod = (window.addEventListener) ? tasktype : "on" + tasktype
        if (target.addEventListener)
            target.addEventListener(eventMethod, functionref, false)
        else if (target.attachEvent)
            target.attachEvent(eventMethod, functionref)
    },

    destroy: function() {
        for (var i = 0; i < MsgWindow.tobjects.length; i++) {
            MsgWindow.tobjects[i].handle._parent = MsgWindow.tobjects[i].resizearea._parent = MsgWindow.tobjects[i].controls._parent = null
        }
        window.onunload = null;
    }

} //End MsgWindow object
// destroy all windows in memory
window.onunload = MsgWindow.destroy;

/***********  WebWindow proxy class for backward compatibilty  ***************************/

// finds an already created window
function getMsgWindow(iframe) {
    var id  = iframe.getAttribute('MsgWindowId');
    var obj = MsgWindow.GetMsgWindowCore(null);
    for (var i = 0; i < MsgWindow.tobjects.length; i++) {
        if (MsgWindow.tobjects[i].id == id) {
            obj.WebWindow = MsgWindow.tobjects[i];
            return obj;
        }
    }
    return obj;
}

function WebWindow( id ) {
    this.DragEnabled = false;
    this.CallBackFunction = null;
    this.Buttons = null;
    this.Icon = (id == 500) ? MsgWindowIcons.Error : MsgWindowIcons.Default;
    this.Modal = false;
    // title of the window
    this.Title = '';
        // style properties
    this.Width    = 350;
    this.Height   = 150;

    this.Left = null;
    this.Top = null;
    this.ShowTitlebar = true;
    this.ShowCloseWindow = true; 

    this.MsgWindow = null;

    this.Show = function(content, title) {
        var options = new MsgWindowOptions();
        options.height = this.Height;
        options.width = this.Width;
        options.modal = this.Modal;
        options.left = this.Left;
        options.top = this.Top;
        options.buttons = this.Buttons;
        options.oncommand = this.CallBackFunction;
        options.dragenabled = this.DragEnabled;
        options.icon = this.Icon;
        options.showhandle = this.ShowTitlebar;
        options.showcontrols = this.ShowCloseWindow;
        this.Title = (title) ? title : this.Title;
        this.MsgWindow = MsgWindow.open(this.id, 'inline', content, this.Title, options, 'recal');
    }

    this.Hide = function() {
        this.MsgWindow.close();
    }
}


/*********** End -- WebWindow proxy class for backward compatibilty  ***************************/

var Dialogs = {
    Count: 0,
    dwindows: [],

    show: function(msg, msgwindowoptions) {
        // increment the counter
        var num = ++this.Count;
        var win = new WebWindow('Dialog_' + num);
        // copy properties to WebWindow Object
        for (var option in msgwindowoptions)
            eval('win["' + option + '"] = msgwindowoptions.' + option + ';');
        // slide item down 10px
        win.Show(msg);
        this.dwindows.push(win);
        return win.MsgWindow;
    },
    
    hide: function() {
        var dialogWin = this.dwindows.pop();
        if (dialogWin == null)
            return;
        // move it away from everything
        dialogWin.Hide();
    },

    destroy: function() {
        MsgWindow.destroy();
    }
};

/*********************** Utility Functions for Control Windows ***************************/
function ControlWindows() {
    this.Url = '';
    this.Open = function(id, p, t, o) {
        if (this.Url == null) {
            alert('ControlWindow.Url is Null.  Please define Control window.');
            return false;
        }
        var url = this.Url + '?c=' + p;
        return MsgWindow.open(id, 'iframe', url, t, o);
    }
}
var ControlWindow = new ControlWindows();
/*********************** End of Utility Functions ****************************************/