﻿var bDisplay = false;
var bLogin = false;
var tFailed;
var sMenuItem = "";
var dWait = 0;
var bChecking = false;
var sBanner = "";
var iBanner = 0;
var vPopupArea = "";
var iSpace = 220;
var bResizing = false;

function resizeFrame()
{
    if (!bResizing)
    {
        bResizing = true;

        if (sBanner == "" && document.getElementById("logo") && parseInt(document.getElementById("logo").height) > 0)
        {
            document.getElementById("contentcell").style.height = parseInt(parseInt(document.body.clientHeight) - 55 - parseInt(document.getElementById("logo").height)) + "px";
            document.getElementById("f-content").style.height = parseInt(parseInt(document.body.clientHeight) - 55 - parseInt(document.getElementById("logo").height)) + "px";
	        document.getElementById("waitarea").style.height = parseInt(parseInt(document.body.clientHeight) - 55 - parseInt(document.getElementById("logo").height)) + "px";
	    }
        else if (document.getElementById('menucell') && document.getElementById('menucell').style.display == "none")
        {
            document.getElementById("contentcell").style.height = parseInt(document.body.clientHeight) + "px";
            document.getElementById("f-content").style.height = parseInt(document.body.clientHeight) + "px";
            //if (frames['content'].document.getElementById('spacercell')) { document.getElementById("waitarea").style.height = parseInt(parseInt(document.body.clientHeight) - parseInt(frames['content'].document.getElementById('spacercell').style.height)) + "px"; }
            //else { document.getElementById("waitarea").style.height = parseInt(document.body.clientHeight) + "px"; }
            document.getElementById("waitarea").style.height = parseInt(document.body.clientHeight) + "px";
        }
	    else if (document.getElementById('i-heading') && document.getElementById('i-heading').style.display == "none")
        {
            document.getElementById("contentcell").style.height = parseInt(document.body.clientHeight) + "px";
            document.getElementById("f-content").style.height = parseInt(document.body.clientHeight) + "px";
            document.getElementById("waitarea").style.height = parseInt(document.body.clientHeight) + "px";
	    }
        else
	    {
            document.getElementById("contentcell").style.height = parseInt(parseInt(document.body.clientHeight) - iBanner) + "px"; // - 38
            document.getElementById("f-content").style.height = parseInt(parseInt(document.body.clientHeight) - iBanner) + "px"; // - 38
	        document.getElementById("waitarea").style.height = parseInt(parseInt(document.body.clientHeight) - iBanner) + "px"; // - 38
	    }

        if (document.getElementById('menucell') && document.getElementById('menucell').style.display == "none")
        {
            document.getElementById("menucell").style.width = '0px';
            document.getElementById("contentcell").style.width = '670px'; //684
            document.getElementById("f-content").style.width = '670px'; //684
            document.getElementById("waitarea").style.width = '670px'; //684
        }
        else if (document.getElementById("msEmbedded"))
        {
            document.getElementById("contentcell").style.height = parseInt(parseInt(document.body.clientHeight) - iSpace) + "px";
            document.getElementById("f-content").style.height = parseInt(parseInt(document.body.clientHeight) - iSpace) + "px";
            document.getElementById("waitarea").style.height = parseInt(document.body.clientHeight - iSpace) + "px";
            document.getElementById("menucell").style.width = '150px';
            document.getElementById("contentcell").style.width = '830px';
            document.getElementById("f-content").style.width = '830px';
            document.getElementById("waitarea").style.width = '830px';
        }
        else
        {
            document.getElementById("menucell").style.width = '150px';
            document.getElementById("contentcell").style.width = '858px';
            document.getElementById("f-content").style.width = '858px';
            document.getElementById("waitarea").style.width = '858px';
        }

        moveWait("move");
	    if (document.getElementById("i-advert") && document.getElementById("i-advert").style.display == "block" || bDisplay == false)
	    {
	        moveAdverts();
	    }

        window.setTimeout("bResizing = false;", 500);
    }
}

function moveAdverts()
{
    /*
    document.getElementById("i-advert").style.top = parseInt(parseInt(document.body.clientHeight) - parseInt(document.getElementById("i-advert").style.height) - 22) + "px";
    if (!bDisplay)
    {
        document.getElementById("i-advert").style.display = "block";
        document.getElementById("i-advert").width = "1000px";
        bDisplay = true;
    }
    document.getElementById("i-advert").style.left = parseInt(escape(document.body.clientWidth - 1000)/2 + 1) + "px";
    */
    if (document.getElementById("i-advert")) { document.getElementById("i-advert").style.display = "none"; }
}

function moveWait(vaction, vtimeout)
{
    clearTimeout(dWait);
    dWait = 0;

    if (checkFileUpload(true))
    {
        switch(vaction)
        {
            case "show":
                document.getElementById("i-wait").style.display = "block";
                document.getElementById("i-waitsml").style.display = "block";
                if (parseInt(vtimeout) > 0) { dWait = window.setTimeout("moveWait('show2');", parseInt(vtimeout)); }
                break;

            case "show2":
                document.getElementById("i-waitsml").style.display = "none";
                document.getElementById("i-wait").style.display = "block";
                document.getElementById("i-unavail").style.display = "block";
                break;

            case "show3":
                document.getElementById("i-waitsml").style.display = "none";
                document.getElementById("i-wait").style.display = "block";
                break;

            case "hide":
                document.getElementById("i-unavail").style.display = "none";
                document.getElementById("i-waitsml").style.display = "none";
                document.getElementById("i-wait").style.display = "none";
                //document.getElementById("contentcelldefault").innerHTML = "";
                break;

            case "hideall":
                document.getElementById("i-unavail").style.display = "none";
                document.getElementById("i-waitsml").style.display = "none";
                document.getElementById("i-wait").style.display = "none";
                document.getElementById("waitarea").style.display = "none";
                document.getElementById("popuparea").style.display = "none";
                document.getElementById("controlarea").style.display = "none";
                document.getElementById("i-printmsg").style.display = "none";
                document.getElementById('popuparea').innerHTML = "";
                //document.getElementById("contentcelldefault").innerHTML = "";
                break;  

            case "showpop":
                moveWait("hide");
                document.getElementById("waitarea").style.display = "block";
                document.getElementById("popuparea").style.display = "block";
                break;

            case "hidepop":
                document.getElementById("popuparea").style.display = "none";
                document.getElementById("waitarea").style.display = "none";
                document.getElementById("i-wait").style.display = "none";
                document.getElementById("i-printmsg").style.display = "none";
                document.getElementById('popuparea').innerHTML = "";
                break;

            case "showpop2":
                moveWait("hide");
                document.getElementById("waitarea").style.display = "block";
                document.getElementById("i-waitsml").style.display = "block";
                break;

            case "hidepop2":
                document.getElementById("i-wait").style.display = "none";
                document.getElementById("i-waitsml").style.display = "none";
                document.getElementById("waitarea").style.display = "none";
                break;        

            case "showctls":
                moveWait("hide");
                //document.getElementById("controlarea").style.display = "block";
                break;

            case "hidectls":
                document.getElementById("controlarea").style.display = "none";
                break;

            case "move":
            default:
                if (sBanner != "")
                {
                    document.getElementById("i-wait").style.width = parseInt(document.body.clientWidth) + "px";
                    document.getElementById("i-wait").style.height = parseInt(document.body.clientHeight) + "px";
                    document.getElementById("i-waitsml").style.left = setLeft(parseInt((parseInt(document.body.clientWidth) - parseInt(document.getElementById("i-waitsml").style.width)) / 2)) + "px";
                    document.getElementById("i-waitsml").style.top = parseInt((parseInt(document.body.clientHeight) - parseInt(document.getElementById("i-waitsml").style.height)) / 2) + "px";
                    document.getElementById("i-message").style.left = setLeft(parseInt((parseInt(document.body.clientWidth) - parseInt(document.getElementById("i-message").style.width)) / 2)) + "px";
                    document.getElementById("i-message").style.top = parseInt((parseInt(document.body.clientHeight) - parseInt(document.getElementById("i-message").style.height)) / 2) + "px";
                    document.getElementById("i-unavail").style.left = setLeft(parseInt((parseInt(document.body.clientWidth) - parseInt(document.getElementById("i-unavail").style.width)) / 2)) + "px";
                    document.getElementById("i-unavail").style.top = parseInt((parseInt(document.body.clientHeight) - parseInt(document.getElementById("i-unavail").style.height)) / 2) + "px";
                    if (document.getElementById('menucell') && document.getElementById('menucell').style.display == "none") { document.getElementById("waitarea").style.left = "0px"; }
                    else { document.getElementById("waitarea").style.left = setLeft(parseInt(((parseInt(document.body.clientWidth) - 1008) / 2) + 150)) + "px"; }
                    if (sBanner == "0") { document.getElementById("waitarea").style.top = "0px"; }
                    else { document.getElementById("waitarea").style.top = "18px"; }
                    document.getElementById("popuparea").style.left = setLeft(parseInt((parseInt(document.body.clientWidth) - parseInt(document.getElementById("popuparea").style.width)) / 2)) + "px";
                    document.getElementById("popuparea").style.top = parseInt((parseInt(document.body.clientHeight) - parseInt(document.getElementById("popuparea").style.height)) / 2) + "px";
                }
                else
                {
                    document.getElementById("i-wait").style.width = parseInt(document.body.clientWidth) + "px";
                    document.getElementById("i-wait").style.height = parseInt(document.body.clientHeight) + "px";
                    document.getElementById("i-waitsml").style.left = setLeft(parseInt((parseInt(document.body.clientWidth) - parseInt(document.getElementById("i-waitsml").style.width)) / 2)) + "px";
                    document.getElementById("i-waitsml").style.top = parseInt((parseInt(document.body.clientHeight) - parseInt(document.getElementById("i-waitsml").style.height)) / 2) + "px";
                    document.getElementById("i-message").style.left = setLeft(parseInt((parseInt(document.body.clientWidth) - parseInt(document.getElementById("i-message").style.width)) / 2)) + "px";
                    document.getElementById("i-message").style.top = parseInt((parseInt(document.body.clientHeight) - parseInt(document.getElementById("i-message").style.height)) / 2) + "px";
                    document.getElementById("i-unavail").style.left = setLeft(parseInt((parseInt(document.body.clientWidth) - parseInt(document.getElementById("i-unavail").style.width)) / 2)) + "px";
                    document.getElementById("i-unavail").style.top = parseInt((parseInt(document.body.clientHeight) - parseInt(document.getElementById("i-unavail").style.height)) / 2) + "px";
                    if (document.getElementById("logo") && parseInt(document.getElementById("logo").height) > 0)
                    {
                        document.getElementById("waitarea").style.left = setLeft(parseInt(((parseInt(document.body.clientWidth) - 1008) / 2) + 150)) + "px";
                        document.getElementById("waitarea").style.top = parseInt(parseInt(document.getElementById("logo").height) + 34) + "px";
                    }
                    else if (document.getElementById('menucell') && document.getElementById('menucell').style.display == "none" && frames['content'].document.getElementById('spacercell'))
                    {
                        document.getElementById("waitarea").style.left = setLeft((parseInt(document.body.clientWidth) - 670) / 2) + "px";
                        document.getElementById("waitarea").style.top = "0px"; //parseInt(frames['content'].document.getElementById('spacercell').style.height) + "px";
                    }
                    else
                    {
                        document.getElementById("waitarea").style.left = setLeft(parseInt(((parseInt(document.body.clientWidth) - 962) / 2) + 150)) + "px";
                        document.getElementById("waitarea").style.top = "140px";
                    }
                    document.getElementById("popuparea").style.left = setLeft(parseInt((parseInt(document.body.clientWidth) - parseInt(document.getElementById("popuparea").style.width)) / 2)) + "px";
                    document.getElementById("popuparea").style.top = parseInt((parseInt(document.body.clientHeight) - parseInt(document.getElementById("popuparea").style.height)) / 2) + "px";
                }

                if (document.getElementById("logo") && parseInt(document.getElementById("popuparea").style.top) < parseInt(document.getElementById("logo").height) + 16)
                {
                    document.getElementById("popuparea").style.top = parseInt(parseInt(document.getElementById("logo").height) + 16) + "px";
                }
                document.getElementById("controlarea").style.left = setLeft(parseInt(((parseInt(document.body.clientWidth) - 1008) / 2) + 2)) + "px";
                document.getElementById("controlarea").style.top = "250px";
                break;
        }
    }
}

function setLeft(vL)
{
    if (vL < 0) { vL = 0; }
    return vL;
}

function runLogout()
{
    bLogin = false;
    window.onbeforeunload = null;
    moveWait("show", 20000);
    Services.runLogout( showMenuArea );
    Services.runRedirect( "login.aspx", changePage );
}

function showLoginArea(result)
{
    if (result)
    {
        Services.runRedirect( location.href, changePage );
    }
    else
    {
        if (frames["content"].location.href == "about:blank")
        {
            Services.runRedirect("login.aspx", changePage);
        }

        if (bLogin)
        {
            showMessage(1500, 100, "FALSE", "Authentication Failed", "Y");
            frames["content"].enableLogin(true);
        }
    }
    moveWait("hide");
    bLogin = false;
}

function showMessage(iTime, iOpacity, sSuccess, sMessage, sSetup)
{
    if (sSetup == "Y")
    {
        if (sMessage != "" && sSuccess != "")
        {
            if (sSuccess == "TRUE")
            {
                document.getElementById('i-msgnotify').src = 'include/img/notify-success.jpg';            
                document.getElementById('i-msgnotify').alt = "Successful";
            }
            else if (sSuccess == "FALSE")
            {
                document.getElementById('i-msgnotify').src = 'include/img/notify-failed.jpg';        
                document.getElementById('i-msgnotify').alt = "Failed";
            }
            else if (sSuccess == "ALERT")
            {
                document.getElementById('i-msgnotify').src = 'include/img/notify-alert.jpg';        
                document.getElementById('i-msgnotify').alt = "Alert";
            }
            document.getElementById('i-msgnote').innerHTML = sMessage;
        }
        else
        {
            sSetup = "";
        }
    }

    if (sSetup != "")
    {
        if (iOpacity > 0)
        {
            document.getElementById('i-message').style.opacity = parseFloat(iOpacity / 100);
            document.getElementById('i-message').style.filter = "alpha(opacity=" + parseInt(iOpacity) + ")";
            document.getElementById('i-message').style.display = "block";
            tFailed = setTimeout("showMessage(" + parseInt(50) + "," + parseInt(iOpacity - 10) + ",'','','N');", iTime);
        }
        else
        {
            tFailed = null;
            document.getElementById('i-message').style.display = "none";
            document.getElementById('i-message').style.opacity = "1.0";
            document.getElementById('i-message').style.filter = "alpha(opacity=100)";
        }
    }
}

function showLoginDetailsArea(result)
{
    if (document.getElementById("logindetailsarea"))
    {
        document.getElementById("logindetailsarea").style.color = c1;
        document.getElementById("logindetailsarea").innerHTML = result;
    }
}

function changePassword()
{
    parent.frames['content'].showPopup("mcChangePass", "", "", "", "", "", "", "", "", "", "", "");
}

function resetPassword()
{
    moveWait("show", 20000);
    Services.getPopup("mcLoginEmail", "", frames['content'].document.getElementById('loginuser').value, "", "", "", "", "", "", "", "", "", showPopupBack);
}

function showMenuArea(result)
{
    if (result.toString() == "")
    {
        moveWait("show2");
    }
    else if (result.toString().length >= 5 && result.toString().substr(0,5) == "FALSE")
    {
        if (location.href.match("/test/")) { alert(result.toString()); }
        moveWait("hide");
        frames["content"].enableLogin(true);
        showMessage(1500, 100, "FALSE", "Authentication Failed", "Y");
    }
    else if (result.toString() == "PASS2")
    {
        Services.getPopup("mcLogin", "", "", "", "", "", "", "", "", "", "", "", showPopupBack);
    }
    else
    {
        if (document.getElementById("menuarea")) { document.getElementById("menuarea").innerHTML = result; }
        Services.showLogin( showLoginArea );
        Services.showLoginDetails( showLoginDetailsArea );
    }
}

function showPopupBack(result)
{
    if (result == null || result == "FALSE" || result == "")
    {
        checkLogin();
    }
    else
    {
        if (document.getElementById('popuparea'))
        {
            document.getElementById('popuparea').innerHTML = result;
        }

        if (document.getElementById('loginpass1'))
        {
            document.getElementById('popuparea').style.width = "300px";
            if (document.getElementById('i-passtext')) { document.getElementById('popuparea').style.height = "260px"; }
            else { document.getElementById('popuparea').style.height = "150px"; }
        }
        else if (document.getElementById('loginpass2'))
        {
            document.getElementById('popuparea').style.width = "250px";
            document.getElementById('popuparea').style.height = "80px";
        }
        else if (document.getElementById('loginuser2'))
        {
            document.getElementById('popuparea').style.width = "250px";
            document.getElementById('popuparea').style.height = "150px";
        }
        else if (vPopupArea.substr(0, 2) == "ms")
        {
            document.getElementById('popuparea').style.width = "615px";
            document.getElementById('popuparea').style.height = "500px";
        }
        else if (vPopupArea.substr(0, 7) == "mcAlert")
        {
            document.getElementById('popuparea').style.width = "350px";
            document.getElementById('popuparea').style.height = parseInt(parseInt(document.getElementById("tabAlert").style.height) + 20) + "px";
        }
        else if (vPopupArea.substr(0, 8) == "mcUpload")
        {
            document.getElementById('popuparea').style.width = "430px";
            document.getElementById('popuparea').style.height = "225px";
        }
        else if (vPopupArea.substr(0, 14) == "mcQuoteMessage")
        {
            document.getElementById('popuparea').style.width = "425px";
            document.getElementById('popuparea').style.height = "505px";
        }
        else
        {
            document.getElementById('popuparea').style.width = "425px";
            document.getElementById('popuparea').style.height = "425px";
        }

        vPopupArea = "";
        moveWait("move");
        moveWait("showpop");
        if (document.getElementById('loginpass1')) { document.getElementById("loginpass1").focus(); }
        else if (document.getElementById('loginpass2')) { document.getElementById("loginpass2").focus(); }
        else if (document.getElementById('loginuser2')) { document.getElementById("loginuser2").focus(); }
        else if (document.getElementById('msgSubject') && document.getElementById('msgDetails'))
        {
            if (document.getElementById('msgSubject').value != "")
            {
                document.getElementById('msgDetails').focus();
                document.getElementById('msgDetails').selectionStart = 0;
                document.getElementById('msgDetails').selectionEnd = 0; 
            }
            else
            {
                document.getElementById('msgSubject').focus();
            }
        }
    }
}

function openMenu(sHeading)
{
    if (document.getElementById("i-menubody" + sHeading + "div").style.display == "block")
    {
        if (document.getElementById("i-bracket" + sHeading))
        {
            document.getElementById("i-bracket" + sHeading).innerHTML = ">";
            document.getElementById("i-menuhead" + sHeading + "div").className = "menuheadalt";
            document.getElementById("i-menuhead" + sHeading + "tab").className = "menuheadalt";
        }
        document.getElementById("i-menubody" + sHeading + "div").style.display = "none";
    }
    else
    {
        if (document.getElementById("i-bracket" + sHeading))
        {
            document.getElementById("i-bracket" + sHeading).innerHTML = "^";
            document.getElementById("i-menuhead" + sHeading + "div").className = "menuhead";
            document.getElementById("i-menuhead" + sHeading + "tab").className = "menuhead";
        }
        document.getElementById("i-menubody" + sHeading + "div").style.display = "block";
    }
}

function setMenuItem(sNewMenu)
{
    if (sNewMenu != "" && document.getElementById("i-menubodytitle" + sNewMenu + "div"))
    {
        document.getElementById("i-menubodytitle" + sNewMenu + "div").className = "menubodytitlealt";
        if (sMenuItem != "" && document.getElementById("i-menubodytitle" + sMenuItem + "div"))
        {
            document.getElementById("i-menubodytitle" + sMenuItem + "div").className = "menubodytitle";
        }
        sMenuItem = sNewMenu;
    }
    moveWait("hidepop");
}

function getMenuItem()
{
    return sMenuItem;
}

function changePage(result)
{
    if (result != "")
    {
        frames["content"].location.href = result;
    }
    resizeFrame();
    moveWait("hide");
}

function init()
{
    window.onbeforeunload = null;
    resizeFrame();
    moveWait("hide");
    if (frames["content"].location.href == "about:blank")
    {
        moveWait("show", 20000);
    }
}

function pausecomp(millis) 
{
    var date = new Date();
    var curDate = null;
    do { curDate = new Date(); } 
    while(curDate-date < millis);
}

function keyPressHandler(e)
{
    if (e && e.keyCode == e.DOM_VK_ESCAPE)
    {
        if (document.getElementById('loginform2')) { frames['content'].enableLogin(true); }
        moveWait('hidepop');
        resizeFrame();
    }
}

function checkFileUpload(bAllow)
{
    var result = true;
    
    if (!bChecking)
    {
        bChecking = true;
        var frameid = getAttFrame();
        if (frameid >= 0 && frames[frameid] && frames[frameid].document && frames[frameid].document.getElementById("myFile") && (frames[frameid].document.getElementById("myFile").value != "" || frames[frameid].document.getElementById("myFile").text != ""))
        {
            result = false;
            if (bAllow) { result = confirm("There is a file waiting to be attached.\nYou may not have pressed the \"Attach\" button to attach your file to the message.\nAre you sure you would like to continue without attaching this file?"); }
            else { alert("There is a file waiting to be attached.\nPlease press the \"Attach\" button to attach your file before continuing."); }
            if (result) { frames[frameid].document.getElementById("myFile").value = ""; frames[frameid].document.getElementById("myFile").text = ""; }
        }
        bChecking = false;
    }
    else
    {
        result = false;
    }
    return result;
}

function getAttFrame()
{
    var frameid = -1;
    for (var i = 0; i < frames.length; i++)
    {
        if (frames[i].name == "attachment") { frameid = i; }
    }
    return frameid;
}

function getVar(sName)
{
    if (sName = (new RegExp('[?&]'+encodeURIComponent(sName)+'=([^&]*)')).exec(location.search))
    return decodeURIComponent(sName[1]);
}

function checkLogin()
{
    frames['content'].onbeforeunload = null;
    moveWait("hideall");
    location.href = location.href;
    //frames['content'].location.href = "login.aspx?m=1";
}

function showMsgBox(vMsg, vTitle)
{
    vPopupArea = "mcAlert";
    //Services.getPopup("mcAlert", "", "Thank You", "Your quote will be processed shortly, and someone will contact you in the next 24 hours with further details concerning your quote.", "", "", "", "", "", "", "", "", showPopupBack);
    Services.getPopup("mcAlert", "", vTitle, vMsg, "", "", "", "", "", "", "", "", showPopupBack);
}

sBanner = getVar("b");
if (isNaN(sBanner)) { sBanner = ""; }
if (sBanner == "0") { iBanner = 1; }
else if (sBanner == "1") { iBanner = 38; }

