//
// openPopUps v1.01 Copyright (c) 2006 openWebWare.com
// This copyright notice MUST stay intact for use.
//
// The perfect pop up window replacement for your advertisements and web
// applications. Pop up blockers will fail to prevent an openPopUp window
// from opening.
//
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
// License for more details.
//
// You should have received a copy of the GNU Lesser General Public License along
// with this library; if not, write to the Free Software Foundation, Inc., 59
// Temple Place, Suite 330, Boston, MA 02111-1307 USA

// CSS Diretory
cssDir = "/";

// Images Directory
imageDir = "sysimages/";

// Include Style Sheet
//document.write('<link rel="stylesheet" type="text/css" href="' +cssDir+ 'styles.css">\n');

/* ---------------------------------------------------------------------- *\
  Function    : JSWindow()
  Description : creates a new window
  Usage       : JSWindow("title", oContent, [x], [y], [id], [width])
  Arguments   : title    - The title of the window as displayed in the titlebar
                oContent - The content of the Div called using getElementbyID
                x        - The number of pixels from the left the window appears when first open
                y        - The number of pixels from the top the window appears when first open
                id       - The <div> this window will use for its content
                width    - The default width of the window
\* ---------------------------------------------------------------------- */

function JSWindow(title, oContent, x, y, id, width, height,popupColor, minimizeIcon,closehandler,src)
{

    y = (clientHeight() - height) / 2;
    x = (clientWidth() - width) / 2;

    // define variables
    this.title = title;
    this.oContent = oContent;
    this.width = width;
    this.height = height;
    this.x = x;
    this.y = parseInt(popupScrollTop()) + y;
    this.id = id;
    this.bgcolor = popupColor;
    this.zIndex = 1000;
    this.closefunction = closehandler
    this.iframeId = "WindowFrame "+this.id;


    // make select boxes invisible
    var selects = document.getElementsByTagName("select");
    for (i = 0; i != selects.length; i++) {
        selects[i].style.visibility = "hidden";
    }

    var objOverlay

    if (! document.getElementById("overlay")) {
        objOverlay = document.createElement("div");
        objOverlay.setAttribute('id','overlay');
        objOverlay.onclick = function () {if (closehandler) { closehandler(); } return false;}
        objOverlay.style.display = 'none';
        objOverlay.style.position = 'absolute';
        objOverlay.style.top = '0';
        objOverlay.style.left = '0';
        objOverlay.style.zIndex = '90';
        objOverlay.style.width = '100%';
        document.body.insertBefore(objOverlay, document.body.firstChild);
    }
    else {
        objOverlay = document.getElementById("overlay");
    }

    // set height of Overlay to take up whole page and show
    objOverlay.style.height = docHeight() + 'px';
    objOverlay.style.display = 'block';


    // create the table window and define CSS properties
    this.oTable = document.createElement("table");
    this.oTable.id = "Window " + this.id;
    this.oTable.style.width = this.width + "px";
    this.oTable.style.height = this.height + "px";
    this.oTable.style.backgroundColor = "#FFFFFF";
    this.oTable.style.zIndex = this.zIndex;
    this.oTable.style.position = "absolute";
    this.oTable.style.left = this.x + "px";
    this.oTable.style.top = this.y + "px";
    this.oTable.style.border = "3px ridge #888888";
    //this.oTable.style.overflow = "hidden";
    this.oTable.cellSpacing = 0;
    this.oTable.cellPadding = 0;
    this.oTable.border = 0;

    // link from the table to the JSWindow object
    this.oTable.jsWindow = this;

    // if the table is clicked anywhere, show the table in front of other open windows
    this.oTable.onmousedown = JSWindow.prototype.onBringToFront;

    // append to document body
    document.body.appendChild(this.oTable);

    // add a row for the titlebar
    var oTR = this.oTable.insertRow(0);
    oTR.className = "JSWindowTitleStyle";

    // add header
    var oTD = oTR.insertCell(0);
    oTD.style.borderBottom = "1px solid #000000";
    oTD.style.backgroundImage = "url(" + imageDir + "title_bg.jpg)";
    oTD.height = "20px";
    oTD.width = "100%";
    //oTD.innerHTML = title;
    oTD.jsWindow = this;
    oTD.onmousedown = JSWindow.prototype.tdOnMouseDown;

    // add close button
    var oImg = document.createElement("img");
    oImg.style.margin = "2px";
    oImg.src = imageDir + "close.jpg";
    oImg.align = "right";
    oImg.jsWindow = this;
    //oImg.onmousedown = JSWindow.prototype.onClose;
    oImg.onclick = function () {if (closehandler) { closehandler(); } return false;}
    oTD.appendChild(oImg);

    // add a row for the window's content
    oTR = this.oTable.insertRow(1);

    this.oContentTD = oTR.insertCell(0);
    this.oContentTD.style.backgroundColor = this.bgcolor;
    this.oContentTD.width = this.width + "px";
    this.oContentTD.colSpan = 2;
    this.oContentTD.className = "JSWindowContentStyle";
    this.oContentTD.id = "JSWindowContentId";
    this.oContentTD.vAlign = "Top";

    // add a row for the window resize
    //oTR = this.oTable.insertRow(2);
    //oTD = oTR.insertCell(0);
    //oTD.style.borderTop = "1px solid #000000";
    //oTD.style.backgroundImage = "url(" + imageDir + "title_bg.jpg)";
    //oTD.innerHTML = "<div id=\"resizer\"></div>"
    //oTD.height = "20px";
    //oTD.width = "100%";
    //oTD.jsWindow = this;
    //oTD.onmousedown = JSWindow.prototype.resizeMouseDown;

    // add close button
    //var oImg = document.createElement("img");
    //oImg.style.margin = "2px";
    //oImg.src = imageDir + "move1.gif";
    //oImg.align = "right";
    //oImg.jsWindow = this;
    //oImg.onmousedown = JSWindow.prototype.resizeMouseDown;
    //oTD.appendChild(oImg);

    // add an iframe for the content
    this.oContentTD.innerHTML = "<iframe id=\""+this.iframeId+ "\" src=\""+src+"\" frameborder=\"0\" width=\""+this.width+"\" style=\"z-index:10;width:"+this.width+"px;height:"+(this.height-20)+"px;\" ></iframe>"
}

JSWindow.prototype.onBringToFront = function()
{
        this.jsWindow.bringToFront();
}

JSWindow.prototype.bringToFront = function()
{
        // if not already the last child of the document.body, make it so
        if ( document.body.childNodes[document.body.childNodes.length-1] !== this.oTable )
        {
                // move to bottom of document
                document.body.appendChild(this.oTable);
        }
}

JSWindow.prototype.resizeMouseDown = function()
{
        this.jsWindow.resizeOnMouseDown();
}

JSWindow.prototype.resizeOnMouseDown = function()
{

        // record that an onmousedown has just occurred
        this.bDown = true;

        // link from body to this JSWindow object
        document.body.jsWindow = this;

        // save body mouse handlers
        this.saveMouseMove = document.body.onmousemove;
        this.saveMouseUp = document.body.onmouseup;

        // set new handlers.

        document.body.onmousemove = JSWindow.prototype.resizeOnMouseMove;
        document.body.onmouseup = JSWindow.prototype.resizeOnMouseUp;
}


JSWindow.prototype.resizeOnMouseMove = function(evt)
{
        var e = window.event ? window.event : evt;
        if (this.jsWindow) {
            this.jsWindow.resizeMouseMove(e);
        }
}

JSWindow.prototype.resizeMouseMove = function(evt)
{
        // hide the contents when the popup is being moved
        this.oContentTD.style.visibility='hidden';

        if ( this.bDown )
        {
                this.dx = parseInt(this.oTable.offsetWidth, 10) - evt.clientX;
                this.dy = parseInt(this.oTable.offsetHeight, 10) - evt.clientY;
                //this.dx = parseInt(this.oTable.style.left, 10) - evt.clientX;
                //this.dy = parseInt(this.oTable.style.top, 10) - evt.clientY;
                this.bDown = false;
                //document.getElementById("resizer").innerHTML = evt.clientX + "," + evt.clientY + "  |  " + this.dx + "," + this.dy + "  |  " + this.oTable.offsetWidth + "," + this.oTable.offsetHeight;
        }
        else
        {
                //this.oTable.style.left = Math.max((this.dx + evt.clientX),0) + "px";
                //this.oTable.style.top = Math.max((this.dy + evt.clientY),0) + "px";
                this.oTable.style.width = Math.max((this.dx + evt.clientX),0) + "px";
                this.oTable.style.height = Math.max((this.dy + evt.clientY),0) + "px";

                document.getElementById(this.iframeId).style.width = (parseInt(this.oTable.style.width) - 6) + "px";
                document.getElementById(this.iframeId).style.height = (parseInt(this.oTable.style.height) - 46) + "px";
                //document.getElementById("resizer").innerHTML = evt.clientX + "," + evt.clientY + "  |  " + this.dx + "," + this.dy + "  |  " + this.oTable.offsetWidth + "," + this.oTable.offsetHeight;
        }
}

JSWindow.prototype.resizeOnMouseUp = function()
{
        if (this.jsWindow) {
            this.jsWindow.resizeMouseUp();
        }
}

JSWindow.prototype.resizeMouseUp = function()
{
        // un-hide the contents when the popup stops being moved
        this.oContentTD.style.visibility='visible';

        document.body.onmouseup = this.saveMouseUp;
        document.body.onmousemove = this.saveMouseMove;
        document.body.jsWindow = null;
}


JSWindow.prototype.tdOnMouseDown = function()
{
        if (this.jsWindow) {
            this.jsWindow.onMouseDown();
        }
}

JSWindow.prototype.onMouseDown = function()
{

        // record that an onmousedown has just occurred
        this.bDown = true;

        // link from body to this JSWindow object
        document.body.jsWindow = this;

        // save body mouse handlers
        this.saveMouseMove = document.body.onmousemove;
        this.saveMouseUp = document.body.onmouseup;

        // set new handlers.

        document.body.onmousemove = JSWindow.prototype.bodyOnMouseMove;
        document.body.onmouseup = JSWindow.prototype.bodyOnMouseUp;
}

JSWindow.prototype.bodyOnMouseMove = function(evt)
{
        var e = window.event ? window.event : evt;
        if (this.jsWindow) {
            this.jsWindow.onMouseMove(e);
        }
}

JSWindow.prototype.onMouseMove = function(evt)
{
        // hide the contents when the popup is being moved
        this.oContentTD.style.visibility='hidden';

        // if mouse not down, stop the move (for IE only)
        //if ( (document.all) && !(evt.button & 1) )
        //{
        //        if (bopera) opera.postError("here 4");
        //        this.onMouseUp();
        //        return;
        //}

        if ( this.bDown )
        {
                this.dx = parseInt(this.oTable.style.left, 10) - evt.clientX;
                this.dy = parseInt(this.oTable.style.top, 10) - evt.clientY;
                this.bDown = false;

        }
        else
        {

                this.oTable.style.left = Math.max((this.dx + evt.clientX),0) + "px";
                this.oTable.style.top = Math.max((this.dy + evt.clientY),0) + "px";
        }
}

JSWindow.prototype.bodyOnMouseUp = function()
{
        this.jsWindow.onMouseUp();
}

JSWindow.prototype.onMouseUp = function()
{
        // un-hide the contents when the popup stops being moved
        this.oContentTD.style.visibility='visible';

        document.body.onmouseup = this.saveMouseUp;
        document.body.onmousemove = this.saveMouseMove;
        document.body.jsWindow = null;
}


JSWindow.prototype.onMinimize = function()
{
  this.jsWindow.minimize();
}



JSWindow.prototype.minimize = function()
{
        // hide the content
        this.oContent.style.visibility = "hidden";
        this.oContent.style.position = "absolute";
        document.body.appendChild(this.oContent);

        this.oTable.deleteRow(1);

        // save current position
        this.saveX = this.oTable.style.left;
        this.saveY = this.oTable.style.top;

  // get the "window bar"
        if ( !window.jsWindowBar )
        {
                window.jsWindowBar = document.createElement("span");
          document.body.appendChild(window.jsWindowBar);
        }

        window.jsWindowBar.appendChild(this.oTable);
        this.oTable.style.position = "static";
        this.oTable.style.left = "0px";
        this.oTable.style.top = "0px";

        this.oMinTD.innerHTML = "<img src='" + imageDir + "maximize.jpg'>";
        this.oMinTD.onmousedown = JSWindow.prototype.onMaximize;
        this.oTable.style.width = "220px";
  this.oTable.style.borderBottom = "0px";

}


JSWindow.prototype.onMaximize = function()
{
        this.jsWindow.maximize();
}

JSWindow.prototype.maximize = function()
{
        document.body.appendChild(this.oTable);
        this.oTable.style.position = "absolute";

        this.oTable.style.left = this.saveX;
        this.oTable.style.top = this.saveY;
        this.oTable.style.width = this.width + "px"
        this.oTable.style.borderBottom = "1px solid #000000";

        // add the content again.
        oTR = this.oTable.insertRow(1);

  this.oContentTD = oTR.insertCell(0);
        this.oContentTD.colSpan = 3;
        this.oContentTD.innerHTML = document.getElementById(this.id).innerHTML;

        //oTD.appendChild(this.oContent);
        this.oContent.style.position = "static";
        this.oContent.style.visibility = "visible";
        this.oContentTD.style.backgroundColor = this.bgcolor;
        this.oContentTD.className = "JSWindowContentStyle";

  this.oMinTD.style.paddingRight = "1px";
        this.oMinTD.innerHTML = "<img src='" + imageDir + "minimize.jpg'>";
        this.oMinTD.onmousedown = JSWindow.prototype.onMinimize;

}

JSWindow.prototype.onMaximize = function()
{
        this.jsWindow.maximize();
}

JSWindow.prototype.close = function()
{
    //if (this.closefunction) {
    //    this.closefunction();
    //}

    if (this.oTable) {
        this.oTable.style.display = "none";
    }

    // make select boxes visible
    var selects = document.getElementsByTagName("select");
    for (i = 0; i != selects.length; i++) {
        selects[i].style.visibility = "visible";
    }


    var objOverlay = document.getElementById('overlay');
    objOverlay = document.getElementById('overlay');
    objOverlay.style.display = 'none';

    // disable keypress listener
    document.onkeypress = '';

    var cObj = this.oTable;
    if (cObj) {
        cObj.parentNode.removeChild(cObj);
    }

}

JSWindow.prototype.onClose = function()
{
    this.jsWindow.close();
}

JSWindow.prototype.reload = function(c)
{
    this.oContentTD.innerHTML = c;
    // go thru the children and if any are an iframe then reload the frame
    var ifr = this.oContentTD.getElementsByTagName("IFRAME")
    for (var i=0;i<ifr.length;i++) {
        ifr[i].src = ifr[i].src;
    }
}

/* ---------------------------------------------------------------------- *\
  Function    : hideDiv()
  Description : Hides all Divs on the page.
  Usage       : hideDiv([divNumber])
  Arguments   : divNumber - The number of Divs in the page that must be hidden
\* ---------------------------------------------------------------------- */

function hideDiv(divNumber) {
    for (var count = 1; count <= divNumber;) {
        document.getElementById('Div' + count).style.display = 'none';
        count++;
    }
}

function popupScrollTop()
{
    var st = 0;
    if( typeof( window.pageYOffset ) == 'number' ) {
        //Netscape compliant
        st = window.pageYOffset;
    }
    else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
        //DOM compliant
        st = document.body.scrollTop;
    }
    else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
        //IE6 standards compliant mode
        st = document.documentElement.scrollTop;
    }

    return st;
}

function popupScrollLeft()
{
    var st = 0;
    if( typeof( window.pageYOffset ) == 'number' ) {
        //Netscape compliant
        st = window.pageXOffset;
    }
    else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
        //DOM compliant
        st = document.body.scrollLeft;
    }
    else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
        //IE6 standards compliant mode
        st = document.documentElement.scrollLeft;
    }

    return st;
}


function docHeight()
{
    var h;
    if (typeof document.height != 'undefined') {
        h = document.height;
    }
    else if (document.compatMode && document.compatMode != 'BackCompat') {
        h = document.documentElement.scrollHeight;
    }
    else if (document.body && typeof document.body.scrollHeight != 'undefined') {
        h = document.body.scrollHeight;
    }

    return h;
}

function docWidth()
{
    var w;
    if (typeof document.width != 'undefined') {
        w = document.width;
    }
    else if (document.compatMode && document.compatMode != 'BackCompat') {
        w = document.documentElement.scrollWidth;
    }
    else if (document.body && typeof document.body.scrollHeight != 'undefined') {
        w = document.body.scrollWidth;
    }

    return w;
}

function clientWidth()
{
    var w = 0;

    //IE
    if(!window.innerWidth)
    {
        //strict mode
        if(!(document.documentElement.clientWidth == 0))
        {
            w = document.documentElement.clientWidth;
        }
        //quirks mode
        else
        {
            w = document.body.clientWidth;
        }
    }
    //w3c
    else
    {
        w = window.innerWidth;
    }
    return w;
}

function clientHeight()
{
    var h = 0;

    //IE
    if(!window.innerWidth)
    {
        //strict mode
        if(!(document.documentElement.clientWidth == 0))
        {
            h = document.documentElement.clientHeight;
        }
        //quirks mode
        else
        {
            h = document.body.clientHeight;
        }
    }
    //w3c
    else
    {
        h = window.innerHeight;
    }
    return h;
}


/* ---------------------------------------------------------------------- *\
  Function    : createWindows()
  Description : determines if Window already exists, else calls JSWindow
  Usage       : createWindows([windowID], [width], "title")
  Arguments   : windowID - The <div> this window will use for its content
                width    - The default width of the window
                title    - The title of the window as displayed in the titlebar
\* ---------------------------------------------------------------------- */


function createWindow(title, width, height, popupColor, windowID, minimizeIcon, x, y, closehandler, src)
{
    var cObj = document.getElementById('Window ' + windowID);
    if (cObj) {

        // set height of Overlay to take up whole page and show
        var objOverlay = document.getElementById('overlay');
        objOverlay.style.height = docHeight() + 'px';
        objOverlay.style.display = 'block';

        cObj.style.display = 'block';
        cObj.style.top = parseInt(popupScrollTop()) + 100 + "px";
        var c = document.getElementById('JSWindowContentId');
    }
    else {
        var w = new JSWindow("&nbsp;" + title, document.getElementById(windowID), x, y, windowID, width, height, popupColor, minimizeIcon, closehandler,src);
        return w;

    }
}

function modalpopup(name)
{
    this.name = name
    this.callback = "";
    this.src = "";
    this.width = 640;
    this.height = 510;
    this.top = 220;
    this.left = 140;
    this.result = "";

    this.open = function(callback)
    {
        createWindow('', this.width,this.height, '#FFFFFF', this.name, 0, this.top, this.left,
            function ()
                {
                    if (typeof callback != "undefined" && callback && callback != "") {
                        setTimeout (callback,500);
                    }

                    $("Window " + name).jsWindow.close();

                } , this.src);

        return true;
    }
}

