var EVTWindowCnt = 0;

function IsBrowserIE()
{
    var vIE = navigator.appName.toLowerCase();
    return (vIE.indexOf("microsoft internet explorer") >= 0);
}

function IsBrowserIE55()
{
    var dblVersion = 0;
    var vIE = new String(navigator.appVersion.toLowerCase());
    var sIE = vIE.indexOf(" msie ");
    
    if (sIE >= 0)
    {
        var eIE = vIE.indexOf(";", sIE);
        
        sIE += 6;
        dblVersion = parseFloat(vIE.substr(sIE, eIE - sIE));
    }
    
    return dblVersion >= 5.5;
}

function IsBrowserNN()
{
    var vIE = navigator.appName.toLowerCase();
    return (vIE.indexOf("netscape") >= 0);
}

function AppendUsageID(sPage)
{
    if ((g_UsageID != "") && (sPage.indexOf("UsageID=") < 0))
    {
        var sAnchor = "";
        var iAnchor = sPage.indexOf('#');
        
        if (iAnchor >= 0)
        {
            sAnchor = sPage.substr(iAnchor);
            sPage   = sPage.substr(0, iAnchor - ((sPage.indexOf(iAnchor-1) == '?') ? 2 : 1));
        }
        
        sPage += ((sPage.indexOf('?') == -1) ? "?" : "&") + "UsageID=" + g_UsageID;
        if (sAnchor != "")
            sPage += sAnchor;
    }
    
    return sPage;
}

function Popup(mypage, fUsageID, myname, x, y, w, h, scroll) 
{
    mypage = String_Replace(mypage, "&amp;", "&");
    
    if (scroll == "")
        scroll = "no";
    
    winprops = 'height=' + h + ',width=' + w + ',top=' + y + ',left=' + x + ',resizable=yes,scrollbars=' + scroll;

    if (fUsageID)
        mypage += ((mypage.indexOf('?') == -1) ? "?" : "&") + "UsageID=" + g_UsageID;

    var win = window.open(mypage, "", winprops);

    if (win.opener == null)
        win.opener = self;
    
    if (parseInt(navigator.appVersion) >= 4) 
    {
        try
        {
            win.focus(); 
        }
        catch (e)
        {
        }            
    }
    
    return win;
}

function centeredPopEx(mypage, fUsageID, myname, w, h, scroll) 
{
    var x = (screen.availWidth   - w) / 2;
    var y = ((screen.availHeight - h) / 2) - ((screen.height - screen.availHeight) + 10);
    
    return Popup(mypage, fUsageID, myname, x, y, w, h, scroll)
}

function centeredPop(mypage, myname, w, h, scroll) 
{
    return centeredPopEx(mypage, false, myname, w, h, scroll);
}

function CenterPopupInfo(objDiv, iWidth, iHeight, fHelp)
{
    var objPopup = centeredPop("", "", iWidth, IsBrowserIE55() ? 200 : iHeight, (fHelp || !IsBrowserIE55()) ? "yes" : "no");
    
    objPopup.document.write(objDiv.innerHTML);
    FixDocumentWidthEx(objPopup, true);
    
    return false;
}

function RestoredWindowEx(strPage, Percent, winProps)
{
    var width  = screen.availWidth  * (Percent + .05);
    var height = screen.availHeight * ((winProps == "") ? .70 : Percent); 
    var left   = (screen.availWidth  - width)  / 2;
    var top    = (screen.availHeight - height) / 3;
    
    if (winProps == "")
        winProps = "fullscreen=no,channelmode=no,directories=no,location=yes,menubar=yes,resizable=yes,scrollbars=yes,status=yes,titlebar=yes,toolbar=yes";
        
    var win = window.open(strPage, "", winProps + ",left=" + left + ",top=" + top + ",width=" + width + ",height=" + height);

    //if (win.opener == null)
    //    win.opener = self;

    if (win != null)// && (parseInt(navigator.appVersion) >= 4))
        win.focus(); 
    
    return win;
}    

function RestoredWindow(strPage, winProps)
{
    return RestoredWindowEx(strPage, .8, winProps);
}

function RestoredStrippedWindowEx(sPage, Percent, fUsageID)
{
    if (fUsageID)
        sPage = AppendUsageID(sPage);

    return RestoredWindowEx(sPage, Percent, "fullscreen=no,channelmode=no,directories=no,location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,titlebar=yes,toolbar=no");
}

function RestoredStrippedWindow(sPage, fUsageID)
{
    return RestoredStrippedWindowEx(sPage, .8, fUsageID)
}

function GetBrowserVersion()
{
    var vIE = navigator.appVersion.toLowerCase();
    var s   = vIE.indexOf("msie");
    
    if (s >= 0)
    {
        s += 5;
        strVer = vIE.substr(s, vIE.indexOf(";", s) - s);
    }        
    else
    {
        strVer = "0";
    }
    
    return parseFloat(strVer);
}

function GetSystemVersion()
{
    if (navigator.userAgent.indexOf('IRIX') != -1)
    { 
        var OpSys = "Irix"; 
    }
    else if ((navigator.userAgent.indexOf('Win') != -1) && (navigator.userAgent.indexOf('NT 5') != -1))
    { 
        var OpSys = "Windows 2000"; 
    }
    else if((navigator.userAgent.indexOf('Win') != -1) && (navigator.userAgent.indexOf('NT') != -1))
    { 
        var OpSys = "Windows NT"; 
    }
    else if((navigator.userAgent.indexOf('Win') != -1) && (navigator.userAgent.indexOf('95') != -1))
    { 
        var OpSys = "Windows95"; 
    }
    else if(navigator.userAgent.indexOf('Win') != -1)
    { 
        var OpSys = "Windows 98"; 
    }
    else if(navigator.userAgent.indexOf('Mac') != -1)
    { 
        var OpSys = "Macintosh"; 
    }
    else{ 
        var OpSys = "other"; 
    }
}

function FixDocumentWidthEx(objWindow, fMove)
{
    if (objWindow == null)
        objWindow = window;

    if (IsBrowserNN())
    {
        objWindow.sizeToContent();
    }
    else
    {
        var dx = objWindow.document.body.scrollWidth  - objWindow.document.body.clientWidth;
        var dy = objWindow.document.body.scrollHeight - objWindow.document.body.clientHeight;

        if (dx > 0 || dy > 0)
        {
            if ((objWindow.screen.availWidth * .95) < (objWindow.document.body.clientWidth + dx))
                dx = (objWindow.screen.availWidth * .95)- objWindow.document.body.clientWidth;
                
            if ((objWindow.screen.availHeight * .90) < (objWindow.document.body.clientHeight + dy))
                dy = (objWindow.screen.availHeight * .90) - objWindow.document.body.clientHeight;
         
            if (objWindow.dialogHeight == null)
            {
                // Window
                var idyAdjust = dy ? 20 : 0;

                if (fMove)
                    objWindow.moveBy(-dx/2, (-dy/2) + idyAdjust);
                objWindow.resizeBy(dx, dy);
            }
            else
            {
                // dialog
                var idxAdjust = dx ? 0 : 0;
                
                var iWidth  = parseInt(objWindow.dialogWidth)  + dx + idxAdjust;
                var iHeight = parseInt(objWindow.dialogHeight) + dy;

                if (fMove)
                {
                    var iLeft = parseInt(objWindow.dialogLeft) - (dx / 2);
                    var iTop  = parseInt(objWindow.dialogTop) - (dy / 2);
                    
                    objWindow.dialogLeft = iLeft;
                    objWindow.dialogTop  = iTop;
                }
                
                objWindow.dialogWidth  = iWidth  + "px";
                objWindow.dialogHeight = iHeight + "px";
            }
        }
    }                
}

function FixDocumentWidth()
{
    FixDocumentWidthEx(null, true);
}

function FixParentDocumentWidth()
{
    var dx  = document.body.scrollWidth  - document.body.clientWidth;
    var dy  = document.body.scrollHeight - document.body.clientHeight;
    var icxParent = document.parentWindow.parent.document.body.clientWidth;
    var icyParent = document.parentWindow.parent.document.body.clientHeight;

    if (dx > 0 || dy > 0)
    {
        if ((screen.availWidth * .95) < (icxParent + dx))
            dx = (screen.availWidth * .95) - icxParent;
            
        if ((screen.availHeight * .90) < (icyParent + dy))
            dy = (screen.availHeight * .90) - icyParent;
     
        var idyAdjust = dy ? 20 : 0;
        
        document.parentWindow.parent.window.moveBy(-dx/2, (-dy/2) + idyAdjust);
        document.parentWindow.parent.window.resizeBy(dx, dy);
    }
}

function SetSizeWidth(objWindow, Percent)
{
    if (Percent == 0)
        Percent = .95;
        
    var width  = screen.availWidth  * Percent;
    var height = screen.availHeight * Percent; 
    var left   = (screen.availWidth  - width)  / 2;
    var top    = (screen.availHeight - height) / 3;
    
    window.moveTo(left, top);
    window.resizeTo(width, height);
}

function RedirectEx(obj, sPage, fDoc)
{
    sPage = String_Replace(sPage, "&amp;", "&");
    
    if (obj == null)
        return sPage;
        
    if (fDoc)
        obj.location = sPage;
    else 
        obj.src = sPage;
}

function Redirect(sPage, fUsageID)
{
    if (fUsageID)
        sPage = AppendUsageID(sPage);

    var fFailed = false;
        
    try
    {
        RedirectEx(document, sPage, true);
    }
    catch(e)
    {
        fFailed = true;
    }        
    
    if (fFailed)
    {
        try
        {
            // If a page needs to restore params if user clicked "Cancel", it can impliment
            // the following function
            RedirectCancelRestore();
        }
        catch(e)
        {
            // Do nothing
        }                        
    }    
    return false;
}

function GetHelp(sPage)
{
    RestoredStrippedWindowEx(sPage, .7, false);
}

function DumpCookies()
{
    var win = centeredPop("", "", 800, 500, "yes");

    sCookies = new String(document.cookie);
    sCookies = String_Replace(sCookies, ";", "<br/>");
    sCookies = unescape(sCookies);
    sCookies = String_Replace(sCookies, "\t", "<br/>");

    win.document.write("Length: " + sCookies.length + "<br/><br/>" + sCookies);
}

function ExecutePage(sPage)
{
    sPage = RedirectEx(null, sPage, false);
    
    RedirectEx(document.getElementById("ExecutePage"), sPage, false);
}

function LogError(sMessage, e)
{
    var sHosted  = "0";
    var sCurPage = "";
    var iTask    = 0;
    var iStep    = 0;
    
    try
    {
        iTask = document.all.c_task.value;
        iStep = document.all.c_step.value;
    }
    catch (x)
    {
        iTask = 0;
        iStep = 0;
    }

    try
    {
        sCurPage = g_sCurPage;
    }
    catch (x)
    {
        sCurPage = "";
    }

    if (g_fHosted)
    {
        sHosted  = "1";
        sCurPage = "default.aspx";
    }
        
    ExecutePage((g_fHosted ? "" : "../") + "a_common/logerror.aspx" +
                "?UsageID=" + g_UsageID  +
                "&Hosted="  + sHosted +
                "&url="     + escape(document.location) +
                "&Page="    + sCurPage + 
                "&Task="    + iTask +
                "&Step="    + iStep +
                "&FormMessage="      + escape(sMessage) +
                "&ErrorType="        + escape(e.name) +
                "&ErrorMessage="     + escape(e.message) +
                "&ErrorDescription=" + escape(e.description));
}

function CookieSetEx(sName, sValue, fOwnCookie, fNoTask)
{
    var sPage = fNoTask ? sSetCookiePageNoTask : sSetCookiePage;
    ExecutePage(sPage + sName + "&value=" + sValue + "&Own=" + (fOwnCookie ? sName : ""));
}

function CookieSet(sName, sValue, fOwnCookie)
{
    CookieSetEx(sName, sValue, fOwnCookie, false);
}

function Cookies_ClearCookies()
{
    if (confirm("This will remove all EVT cookies from your machine.\r\EVT pages that use cookies will return to there default setting.\r\rIs this what you want to do?"))
        ExecutePage(sClearUserCookiesPage);
    return false;
}    

function GetMainConsoleContentDiv()
{
    return document.getElementById("MainConsoleContentDiv");
}

function WindowOnSize()
{
    var objDiv = document.getElementById("MainConsoleContentDiv");
    
    if (objDiv != null)
    {
        var fWizard    = parseInt(GetAttribute(objDiv, "Wizard")) == "1";
        var objTaskbar = document.getElementById("Taskbar");
        var iWidth     = document.body.clientWidth - 5;
        
        if (objTaskbar)
            iWidth -= objTaskbar.offsetWidth;

        if (fWizard)
            iWidth -= 18;

        if (iWidth >= 0)            
            objDiv.style.width = iWidth;
    }
}

function Skin_PreviewSkinEx(obj, iPreviewSkin)
{
    if (obj != null)
        iPreviewSkin = obj.value;
        
    for ( i = 0; i < document.styleSheets.length; i++ )
    {
        if ( document.styleSheets(i).owningElement.tagName == "STYLE" )
        {
            for ( j = 0; j < document.styleSheets(i).imports.length; j++ )
            {
                if (document.styleSheets(i).imports(j).href.indexOf("/Skins/Skin_") >= 0)
                {
                    var rsSkin = document.styleSheets(i).imports(j).href.split("/");
                        rsSkin = rsSkin[3].split("_");
                    var iSkin  = rsSkin[1];
                    
                    if (iSkin != iPreviewSkin)
                    {
                        var sURL   = String_Replace(document.styleSheets(i).imports(j).href, rsSkin[1], iPreviewSkin);

                        document.styleSheets(i).imports(j).href = sURL;
                    }                        
                }                       
            }                       
        }
    }
}

function Skin_PreviewSkin(obj)
{
    Skin_PreviewSkinEx(obj, 0);
}

var rfncWindowOnLoad   = null;

function WindowOnload(fncWindowOnload)
{
    if (rfncWindowOnLoad == null)
        rfncWindowOnLoad = new Array();
        
    rfncWindowOnLoad[rfncWindowOnLoad.length] = fncWindowOnload;
}

window.onload = Main_WindowOnLoad;
function Main_WindowOnLoad()
{
    try
    {
        WindowOnSize();
        
        if (rfncWindowOnLoad != null)
        {
            var ifnc;
            
            for (ifnc = 0; ifnc < rfncWindowOnLoad.length; ifnc++)
            {
                //alert(rfncWindowOnLoad[ifnc]);
                rfncWindowOnLoad[ifnc]();
            }            
        }
        //alert(foo.bar);
    }
    catch(e)
    {
        var sMessage = "Unexpected error loading page";
        
        LogError(sMessage, e);
        Form_SetProcessing(true);
        
        var sMessage = sMessage + "\r\n\r\n" +
                       "RECOMMEND REFRESHING PAGE" +
                       "\r\n\r\nError Info:\r\n\r\nName:\r\n\t" + e.name + "\r\n\r\nMessage:\r\n\t" + e.message;
                       
        if (e.message != e.description)
            sMessage += "\r\n\r\nDescription:\r\n\t" + e.description;
        
        alert(sMessage);
    }        
}

