//Split out for localization.
var L_GOBACK_TEXT = "Go back to the previous page.";
var L_REFRESH_TEXT = "Refresh the page.";
var L_MOREINFO_TEXT = "More information";
var L_OFFLINE_USERS_TEXT = "For offline users";
var L_RELOAD_TEXT = "Retype the address.";
var L_HIDE_HOTKEYS_TEXT = "Hide tab shortcuts";
var L_SHOW_HOTKEYS_TEXT = "Show more tab shortcuts";

//used by invalidcert.js
var L_CertUnknownCA_TEXT = "The security certificate presented by this website was not issued by a trusted certificate authority.";
var L_CertExpired_TEXT = "The security certificate presented by this website has expired or is not yet valid.";
var L_CertCNMismatch_TEXT = "The security certificate presented by this website was issued for a different website's address.";
var L_CertRevoked_TEXT = "This organization's certificate has been revoked.";
//Need to include errorPageStrings.js when you include this file

function isExternalUrlSafeForNavigation(urlStr)
{
    var regEx = new RegExp("^(http(s?)|ftp|file)://", "i");
    return regEx.exec(urlStr);
}

function clickRefresh()
{
    var location = window.location.href;
    var poundIndex = location.indexOf('#');
    
    if (poundIndex != -1 && poundIndex+1 < location.length && isExternalUrlSafeForNavigation(location.substring(poundIndex+1)))
    {
        window.location.replace(location.substring(poundIndex+1));
    }
}

function navCancelInit()
{
    var location = window.location.href;
    var poundIndex = location.indexOf('#');
    
    if (poundIndex != -1 && poundIndex+1 < location.length && isExternalUrlSafeForNavigation(location.substring(poundIndex+1)))
    {
        var bElement = document.createElement("A");
        bElement.innerText = L_REFRESH_TEXT;
        bElement.href = 'javascript:clickRefresh()';
        navCancelContainer.appendChild(bElement);
    }
    else
    {
        var textNode = document.createTextNode(L_RELOAD_TEXT);
        navCancelContainer.appendChild(textNode);
    }
}

function expandCollapse(elem, changeImage)
{
    if (document.getElementById)
    {
        ecBlock = document.getElementById(elem);

        if (ecBlock != undefined && ecBlock != null)
        {
            if (changeImage)
            {
                //gets the image associated
                elemImage = document.getElementById(elem + "Image");
            }

            //make sure elemImage is good
            if (!changeImage || (elemImage != undefined && elemImage != null))
            {
                if (ecBlock.currentStyle.display == "none" || ecBlock.currentStyle.display == null || ecBlock.currentStyle.display == "")
                {
                    //shows the info.
                    ecBlock.style.display = "block";
                    if (changeImage)
                    {
                        //Just got in expanded mode. Thus, change image to "collapse"
                        elemImage.src = "up.png";
                    }
                }
                else if (ecBlock.currentStyle.display == "block")
                {
                    //hide info
                    ecBlock.style.display = "none";
                    if (changeImage)
                    {
                        //Just got in collapsed mode. Thus, change image to "expand"
                        elemImage.src = "down.png";
                    }
                }
                else
                {
                    //catch any weird circumstances.
                    ecBlock.style.display = "block";
                    if (changeImage)
                    {
                        elemImage.src = "up.png";
                    }
                }
            }//end check elemImage
        }//end check ecBlock
    }//end getElemById
}//end expandCollapse


function initHomepage()
{
    // in real bits, urls get returned to our script like this:
    // res://shdocvw.dll/http_404.htm#http://www.DocURL.com/bar.htm

    //For testing use
    //DocURL = "res://shdocvw.dll/http_404.htm#http://www.microsoft.com/bar.htm"
    DocURL=document.location.href;

    var poundIndex = DocURL.indexOf('#');
    
    if (poundIndex != -1 && poundIndex+1 < location.length && isExternalUrlSafeForNavigation(location.substring(poundIndex+1)))
    {
   
       //this is where the http or https will be, as found by searching for :// but skipping the res://
       protocolIndex=DocURL.indexOf("://", 4);
   
       //this finds the ending slash for the domain server
       serverIndex=DocURL.indexOf("/", protocolIndex + 3);
   
       //for the href, we need a valid URL to the domain. We search for the # symbol to find the begining
       //of the true URL, and add 1 to skip it - this is the BeginURL value. We use serverIndex as the end marker.
       //urlresult=DocURL.substring(protocolIndex - 4,serverIndex);
       BeginURL=DocURL.indexOf("#",1) + 1;
       urlresult=DocURL.substring(BeginURL, serverIndex);
       if (protocolIndex - BeginURL > 7)
           urlresult="";

        //for display, we need to skip after http://, and go to the next slash
       displayresult=DocURL.substring(protocolIndex + 3, serverIndex);
    } 
    else
    {
       displayresult = "";
       urlresult = "";
    }

    var aElement = document.createElement("A");

    aElement.innerText = displayresult;
    aElement.href = urlresult;

    homepageContainer.appendChild(aElement);
}


function initGoBack()
{
    //fills in the span container for "back to previous page"
    //Basically, makes "back to previous page" a clickable item IF there's something in the navstack.

    if (history.length < 1)
    {
        //this page is the only thing. Nothing in history.
        var textNode = document.createTextNode(L_GOBACK_TEXT);
        goBackContainer.appendChild(textNode);
    }
    else
    {
        var bElement = document.createElement("A");
        bElement.innerText = L_GOBACK_TEXT ;
        bElement.href = "javascript:history.back();";
        goBackContainer.appendChild(bElement);
    }				
}

function initMoreInfo(infoBlockID)
{
    var bElement = document.createElement("A");
    bElement.innerText = L_MOREINFO_TEXT;
    bElement.href = "javascript:expandCollapse(\'infoBlockID\', true);";
    moreInfoContainer.appendChild(bElement);				
}

function initOfflineUser(offlineUserID)
{
    var bElement = document.createElement("A");
    bElement.innerText = L_OFFLINE_USERS_TEXT;
    bElement.href = "javascript:expandCollapse('offlineUserID', true);";
    offlineUserContainer.appendChild(bElement);
}

function setTabInfo(tabInfoBlockID)
{
    //removes the previous tabInfo text
    var bPrevElement = document.getElementById("tabInfoTextID");
    var bPrevImage   = document.getElementById("tabInfoBlockIDImage");

    if (bPrevElement != null)
    {
        tabInfoContainer.removeChild(bPrevElement);
    }

    if (bPrevImage != null)
    {
        tabImageContainer.removeChild(bPrevImage);
    }

    var bElement = document.createElement("A");
    var bImageElement = document.createElement("IMG");

    var ecBlock = document.getElementById(tabInfoBlockID);

    //determines if the block is closed
    if ((ecBlock != undefined && ecBlock != null) &&
        (ecBlock.currentStyle.display == "none" || ecBlock.currentStyle.display == null || ecBlock.currentStyle.display == ""))
    {
        bElement.innerText = L_SHOW_HOTKEYS_TEXT;
        bImageElement.alt = L_SHOW_HOTKEYS_TEXT;
        bImageElement.src="down.png";
    }
    else
    {
        bElement.innerText = L_HIDE_HOTKEYS_TEXT;
        bImageElement.alt = L_HIDE_HOTKEYS_TEXT;
        bImageElement.src="up.png";
    }

    bElement.id = "tabInfoTextID";
    bElement.href = "javascript:expandCollapse(\'tabInfoBlockID\', false); setTabInfo('tabInfoBlockID');";


    bImageElement.id="tabInfoBlockIDImage";
    bImageElement.border="0";
    bImageElement.className="actionIcon";

    tabInfoContainer.appendChild(bElement);
    tabImageContainer.appendChild(bImageElement);
}


function skipTabCheck(skipTabFrom)
{
    if (skipTabFrom.skipTabCheckbox.checked)
    {
        window.external.SkipTabsWelcome();
    }
}

function diagnoseConnection()
{
    window.external.DiagnoseConnection();
}
function BodyLoad()
{
    var iError = CertError();
    var iPolicy = PreventIgnoreCertErrors();
    var sRealPageUrl = RealPageURL();

    // list of certificate errors 

    // SecFlag: DLG_FLAGS_INVALID_CA   0x01000000
    var iCertUnknownCA = 16777216;

    // SecFlag: DLG_FLAGS_SEC_CERT_DATE_INVALID  0x04000000
    var iCertExpired = 67108864;

    // SecFlag: DLG_FLAGS_SEC_CERT_CN_INVALID   0x02000000
    var iCertCNMismatch = 33554432;

    // SecError: ERROR_INTERNET_SEC_CERT_REVOKED 12170
    var iCertRevoked = 12170;

    // show the cert errors
    if (iError == iCertRevoked)
    {
        document.all.CertRevoked.style.display="block";
        document.all.CertRevoked.innerText = L_CertRevoked_TEXT;

        document.all.overridelink.style.display="none";
        document.all.ImgOverride.style.display="none";
    }
    else
    {

        if (iError & iCertUnknownCA)
        {
            document.all.CertUnknownCA.style.display="block";
            document.all.CertUnknownCA.innerText = L_CertUnknownCA_TEXT;
        }

        if (iError & iCertExpired)
        {
            document.all.CertExpired.style.display="block";
            document.all.CertExpired.innerText = L_CertExpired_TEXT;
        }

        if (iError & iCertCNMismatch)
        {
            document.all.CertCNMismatch.style.display="block";
            document.all.CertCNMismatch.innerText = L_CertCNMismatch_TEXT;
        }

        if (iPolicy == 1)
        {
            document.all.overridelink.style.display="none";
            document.all.ImgOverride.style.display="none";
        }
        else
        {
            document.all.overridelink.href = sRealPageUrl;
        }
    }
}
function CertError()
{
    error = '0';

    DocQuery=document.location.search;
    BeginError = DocQuery.indexOf("SSLError=");
    
    if (BeginError > 0)
    {
        BeginError += 9;
        EndError = DocQuery.indexOf("&", BeginError);

        if (EndError > 0)
        {
            error = DocQuery.substring(BeginError,EndError);
        }
        else
        {
            error = DocQuery.substring(BeginError);
        }
     }

    return error;
}

function PreventIgnoreCertErrors()
{
    Policy = '0';

    DocQuery=document.location.search;
    BeginPolicy = DocQuery.indexOf("PreventIgnoreCertErrors=")+24;
    if (BeginPolicy > 0)
    {
        EndPolicy = DocQuery.indexOf("&", BeginPolicy);
        if (EndPolicy > 0)
        {
            Policy = DocQuery.substring(BeginPolicy,EndPolicy);
        }
        else
        {
            Policy = DocQuery.substring(BeginPolicy);
        }
    }

    return Policy;
}


function closePage() {
    window.close();
}

function BodyLoad()
{
    var iError = CertError();
    var iPolicy = PreventIgnoreCertErrors();
    var sRealPageUrl = RealPageURL();

    // list of certificate errors 

    // SecFlag: DLG_FLAGS_INVALID_CA   0x01000000
    var iCertUnknownCA = 16777216;

    // SecFlag: DLG_FLAGS_SEC_CERT_DATE_INVALID  0x04000000
    var iCertExpired = 67108864;

    // SecFlag: DLG_FLAGS_SEC_CERT_CN_INVALID   0x02000000
    var iCertCNMismatch = 33554432;

    // SecError: ERROR_INTERNET_SEC_CERT_REVOKED 12170
    var iCertRevoked = 12170;

    // show the cert errors
    if (iError == iCertRevoked)
    {
        document.all.CertRevoked.style.display="block";
        document.all.CertRevoked.innerText = L_CertRevoked_TEXT;

        document.all.overridelink.style.display="none";
        document.all.ImgOverride.style.display="none";
    }
    else
    {

        if (iError & iCertUnknownCA)
        {
            document.all.CertUnknownCA.style.display="block";
            document.all.CertUnknownCA.innerText = L_CertUnknownCA_TEXT;
        }

        if (iError & iCertExpired)
        {
            document.all.CertExpired.style.display="block";
            document.all.CertExpired.innerText = L_CertExpired_TEXT;
        }

        if (iError & iCertCNMismatch)
        {
            document.all.CertCNMismatch.style.display="block";
            document.all.CertCNMismatch.innerText = L_CertCNMismatch_TEXT;
        }

        if (iPolicy == 1)
        {
            document.all.overridelink.style.display="none";
            document.all.ImgOverride.style.display="none";
        }
        else
        {
            document.all.overridelink.href = sRealPageUrl;
        }
    }
}

function RealPageURL()
{
    urlresult = '';
    DocURL=document.location.href;

    BeginURL=DocURL.indexOf("#",1) + 1;
    urlresult=DocURL.substring(BeginURL);

    if (!isExternalUrlSafeForNavigation(urlresult))
    {
        urlresult = window.document.location.toString();
    }

    return urlresult;
}
