// Global variables
var m_bFocus;
var m_cOverID = "";
var m_bOver;
var m_Name;
var m_oDiv = null;
var m_bMouseDown = false;
var m_bIsMozilla = (document.all) ? 0 : 1;
var m_aDivArray = new Array();
var X = 0;
var Y = 0;
xMousePos = 0; // Horizontal position of the mouse on the screen
yMousePos = 0; // Vertical position of the mouse on the screen
xMousePosMax = 0; // Width of the page
yMousePosMax = 0; // Height of the page


// Browser Detection Javascript
// copyright 1 February 2003, by Stephen Chapman, Felgall Pty Ltd

// You have permission to copy and use this javascript provided that
// the content of the script is not changed in any way.

function whichBrs()
{
    var agt=navigator.userAgent.toLowerCase();
    if (agt.indexOf("opera") != -1)
        return 'Opera';
    if (agt.indexOf("staroffice") != -1)
        return 'Star Office';
    if (agt.indexOf("webtv") != -1)
        return 'WebTV';
    if (agt.indexOf("beonex") != -1)
        return 'Beonex';
    if (agt.indexOf("chimera") != -1)
        return 'Chimera';
    if (agt.indexOf("netpositive") != -1)
        return 'NetPositive';
    if (agt.indexOf("phoenix") != -1)
        return 'Phoenix';
    if (agt.indexOf("firefox") != -1)
        return 'Firefox';
    if (agt.indexOf("safari") != -1)
        return 'Safari';
    if (agt.indexOf("skipstone") != -1)
        return 'SkipStone';
    if (agt.indexOf("msie") != -1)
        return 'Internet Explorer';
    if (agt.indexOf("netscape") != -1)
        return 'Netscape';
    if (agt.indexOf("mozilla/5.0") != -1)
        return 'Mozilla';
    if (agt.indexOf('\/') != -1)
    {
        if (agt.substr(0,agt.indexOf('\/')) != 'mozilla')
        {
            return navigator.userAgent.substr(0,agt.indexOf('\/'));
        }
        else
            return 'Netscape';
    }
    else if (agt.indexOf(' ') != -1)
        return navigator.userAgent.substr(0,agt.indexOf(' '));
    else
        return navigator.userAgent;
}


// Set Netscape up to run the "captureMousePosition" function whenever
// the mouse is moved. For Internet Explorer and Netscape 6, you can capture
// the movement a little easier.

//if (document.layers)  // Netscape
//{
//    document.captureEvents(Event.MOUSEMOVE);
//    document.onmousemove = captureMousePosition;
//}
//else if (document.all) // Internet Explorer
//{
//    document.attachEvent("onmousemove",captureMousePosition);
//}
//else if (document.getElementById) // Netcsape 6
//{
//    document.onmousemove = captureMousePosition;
//}


if(whichBrs() == "Internet Explorer")
    document.attachEvent("onmousemove",captureMousePosition);
else
    document.addEventListener("mousemove", captureMousePosition, false);

function captureMousePosition(e)
{
    if (document.layers)
    {
        // When the page scrolls in Netscape, the event's mouse position
        // reflects the absolute position on the screen. innerHight/Width
        // is the position from the top/left of the screen that the user is
        // looking at. pageX/YOffset is the amount that the user has
        // scrolled into the page. So the values will be in relation to
        // each other as the total offsets into the page, no matter if
        // the user has scrolled or not.
        xMousePos = e.pageX;
        yMousePos = e.pageY;
        xMousePosMax = window.innerWidth+window.pageXOffset;
        yMousePosMax = window.innerHeight+window.pageYOffset;
    }
    else if (document.all)
    {
        // When the page scrolls in IE, the event's mouse position
        // reflects the position from the top/left of the screen the
        // user is looking at. scrollLeft/Top is the amount the user
        // has scrolled into the page. clientWidth/Height is the height/
        // width of the current page the user is looking at. So, to be
        // consistent with Netscape (above), add the scroll offsets to
        // both so we end up with an absolute value on the page, no
        // matter if the user has scrolled or not.

        if(window.event)
        {
            xMousePos = window.event.x+document.body.scrollLeft;
            yMousePos = window.event.y+document.body.scrollTop;
            xMousePosMax = document.body.clientWidth+document.body.scrollLeft;
            yMousePosMax = document.body.clientHeight+document.body.scrollTop;
        }
        else
        {
            xMousePos = e.x+document.body.scrollLeft;
            yMousePos = e.y+document.body.scrollTop;
            xMousePosMax = document.body.clientWidth+document.body.scrollLeft;
            yMousePosMax = document.body.clientHeight+document.body.scrollTop;
        }

    }
    else if (document.getElementById)
    {
        // Netscape 6 behaves the same as Netscape 4 in this regard
        xMousePos = e.pageX;
        yMousePos = e.pageY;
        xMousePosMax = window.innerWidth+window.pageXOffset;
        yMousePosMax = window.innerHeight+window.pageYOffset;
    }

    //window.status = "xMousePos=" + xMousePos + ", yMousePos=" + yMousePos + ", xMousePosMax=" + xMousePosMax + ", yMousePosMax=" + yMousePosMax;

}


function CreateHttp()
{
    if(typeof XMLHttpRequest != "undefined")
    {
        return new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {

        var aVersions = new Array();
        aVersions[0] = "MSXML2.XMLHttp.5.0";
        aVersions[1] = "MSXML2.XMLHttp.4.0";
        aVersions[2] = "MSXML2.XMLHttp.3.0";
        aVersions[3] = "MSXML2.XMLHttp";
        aVersions[4] = "Microsoft.XMLHttp";

        for(i = 0;i < aVersions.length;i++)
        {
            try
            {
                var oXMLHttp = new ActiveXObject(aVersions[i]);
                return oXMLHttp;
            }
            catch(oError)
            {
            }
        }
    }
}

//if (m_bIsMozilla)
//{
//    document.captureEvents(Event.MOUSEDOWN | Event.MOUSEMOVE | Event.MOUSEUP);
//}

//document.onmousedown = MouseDown;
//document.onmousemove = MouseMove;
//document.onmouseup = MouseUp;

function ImagePreload(img)
{
    var a=new Image(); a.src=img; return a;
}

CloseOff = ImagePreload('xOff.gif');
CloseOn = ImagePreload('xOn.gif');

function ReplaceAll(cText,cReplace, cWith)
{
    for(i = 0; i < 1000;i++)
    {
        if(cText.search(cReplace) == -1)
            break;

        cText = cText.replace(cReplace,cWith);
    }

    return cText
}

function HighestZ()
{
    var documentDivs = new Array();

    documentDivs = document.getElementsByTagName("DIV");
    //documentDivs = document.all.tags("DIV");

    var highestZ = 0;
    for (var i = 0; i < documentDivs.length; i++)
    {
        var Zindex = documentDivs[i].style.zIndex;
        if (Zindex > highestZ)
        {
            highestZ = Zindex;
        }
    }

    return highestZ;
}

function GetOpacity(element)
{
    var opacity = null;

    //Get the opacity based on the current browser used
    if(! m_bIsMozilla)
    {
        filter = element.style.filter;
        if(filter)
        {
            alpha = filter.split("alpha(opacity=");
            opacity = alpha[1].substr(0,(alpha[1].length-1))/100;
        }
    }
    else
    {
        opacity = element.style.opacity;
    }

    return opacity;
}

function MouseDown(e)
{
    m_bMouseDown = true;

    if(m_cOverID == null)
        return;

    if(m_cOverID != "" && CanMove(m_cOverID))
    {
        if(m_oDiv == null)
        {
            m_oDiv = document.getElementById(m_cOverID);
            m_oDiv.style.zIndex=HighestZ()+1;
            m_oDiv.style.filter = 'alpha(opacity=70)';
        }

        if (m_bIsMozilla)
        {
            X = e.layerX;
            Y = e.layerY;
            return false;
        }
        else
        {
            X = event.offsetX;
            Y = event.offsetY;
        }
    }
}

function MouseMove(e)
{

    if (m_oDiv)
    {
        if (m_bIsMozilla)
        {
            m_oDiv.style.top = (e.pageY-Y) + 'px';
            m_oDiv.style.left = (e.pageX-X) + 'px';
            return false;
        }
        else
        {
            m_oDiv.style.left = event.clientX-X + document.body.scrollLeft;
            m_oDiv.style.top = event.clientY-Y + document.body.scrollTop;
            n = parseInt(m_oDiv.style.top);
            if(n < 0)
                MouseUp();

            return false;
        }
    }
}

function MouseUp()
{
    m_bMouseDown = false;

    if(m_oDiv)
    {
        m_oDiv.style.filter = 'progid:DXImageTransform.Microsoft.dropShadow(color=#616161, offX=3, offY=3, positive=true);alpha(opacity=100)';
    }

    m_oDiv = null;
}

function CanMove(cVar)
{
    var x = 0;
    for(var i = 0; i < m_aDivArray.length;i++)
    {
        if(m_aDivArray[i] == cVar)
            return false;

    }


    return true;
}

function CloseDiv(cTitle)
{
    oVar = document.getElementById(cTitle);
    document.body.removeChild(oVar);
    oVar = null;
    bFlag = 0;
}

function GetHeader(cTitle)
{
    cHeader =  '<table border="0" cellpadding="0" cellspacing="0" style="width:100%">\r\n';
    cHeader += '<tr>\r\n';
    cHeader += '<td width="100%" background="Header.gif" class="Title" style="padding:5px" ondblclick="void(0);" onmouseover="m_bOver=true;m_cOverID=\'' + cTitle + '\'" onmouseout="m_bOver=false;m_cOverID=\'\'" style="cursor:move">\r\n';
    cHeader += '<div id="Title'+cTitle+'">'+cTitle+'</div>\r\n';
    cHeader += '</td>\r\n';
    cHeader += '<td width="0" background="Header.gif" class="Title" align="right">\r\n';
    cHeader += '<img onclick="javascript:CloseDiv(\'' + cTitle + '\');void(0);" border=\"0\" style=\"display:inline-block\" id="Close" name="Close" alt="Close..." title="Close..." src="xOff.gif" border="0" onmouseover="this.style.cursor=\'hand\';window.status=\'Close Window\';this.src=CloseOn.src;return true" onmouseout="window.status=\'\';this.src=CloseOff.src;return true">&nbsp;\r\n';
    cHeader += '</td></tr>\r\n';
    cHeader += '</table>\r\n';
    return cHeader;
}

function Win(cName,nTop,nLeft,nHeight,nWidth,bHeader)
{
    m_Name = cName;
    var oWin = document.createElement("div");
    this.Identity = cName;
    this.Title = "Title"+cName;
    this.Opacity = "100";
    oWin.style.align = 'top';
    oWin.style.width = nWidth+'px';
    oWin.style.border = 'solid 1 #97AECA';
    oWin.className = 'Win';

    oWin.style.position = 'absolute';
    oWin.style.height = nHeight+'px';
    oWin.style.left = nLeft+'px';
    oWin.style.top = nTop+'px';
    oWin.style.zIndex = HighestZ()+1;
    oWin.style.filter = 'progid:DXImageTransform.Microsoft.dropShadow(color=#616161, offX=3, offY=3, positive=true);alpha(opacity=100)';
    oWin.style.visibility = "visible";

    oWin.id=cName;
    oWin.name=cName;

    oWin.onmousedown = function() {oWin.style.zIndex = HighestZ()+1};

    if(bHeader)
        oWin.innerHTML = GetHeader(cName)

    oWin.innerHTML += '<div id="'+cName+'Content" style="width:100%">\r\n';
    oWin.innerHTML += '</div>';


    document.body.appendChild(oWin);
}

Win.prototype.SetTitle = function(str)
{
    oVar = document.getElementById('Title'+this.Identity);
    oVar.innerHTML = str;
}

Win.prototype.SetTop = function(nTop)
{
    oVar = document.getElementById(this.Identity);
    oVar.style.top = nTop+'px';
}

Win.prototype.SetLeft = function(nLeft)
{
    oVar = document.getElementById(this.Identity);
    oVar.style.left = nLeft+'px';
}

Win.prototype.ShowWindow = function(bShow)
{
    oVar = document.getElementById(this.Identity);
    if(bShow)
        oVar.style.visibility = "visible";
    else
        oVar.style.visibility = "hidden";

}

Win.prototype.SetHeight = function(nHeight)
{
    oVar = document.getElementById(this.Identity);
    oVar.style.height = nHeight+'px';
}

Win.prototype.SetWidth = function(nWidth)
{
    oVar = document.getElementById(this.Identity);
    oVar.style.width = nWidth+'px';
}


Win.prototype.SetOpacity = function(nValue)
{
    oVar = document.getElementById(this.Identity);
    oVar.style.filter = 'alpha(opacity='+nValue+')';
}

Win.prototype.SetMoveable = function(bValue)
{
    if(bValue)
    {
        for(var i = 0; i < m_aDivArray.length;i++)
        {
            if(m_aDivArray[i] == this.Identity)
            {
                m_aDivArray.splice(i,1);
                break;
            }
        }

    }
    else
        m_aDivArray.push(this.Identity);
}

Win.prototype.SetBGColor = function(cValue)
{
    oVar = document.getElementById(this.Identity);
    oVar.style.background = cValue;
}

Win.prototype.SetBorder = function(cValue)
{
    oVar = document.getElementById(this.Identity);
    oVar.style.border = cValue;
}

Win.prototype.Load = function(cValue)
{
    oVar = document.getElementById(this.Identity+'Content');
    var oXMLHttp = CreateHttp();
    oXMLHttp.open("get",cValue ,false);
    oXMLHttp.send(null);
    var cResponse = oXMLHttp.responseText;
    cResponse = ReplaceAll(cResponse,"WindowID",this.Identity);
    oVar.innerHTML = cResponse;
}

Win.prototype.Close = function()
{
    oVar = document.getElementById(this.Identity);
    document.body.removeChild(oVar);
}

