var sslWorks = false;

function UseSSL() 
{
    sslWorks = true;
    var cbox = document.getElementById('cbUseSSL');
    if (!cbox)
        return;
    if (enforceSSL == "true") 
    {
        cbox.checked = true;
        cbox.disabled = true;
    }
    else 
    {
        if (getCookie("useSSL") == undefined)
            cbox.checked = true;
        else if (getCookie("useSSL") == "true")
            cbox.checked = true;
        else
            cbox.checked = false;
        cbox.disabled = false;
    }       
}

function ClientValidate(source, arguments)
{
    var tmp = document.getElementById("pnlLogonFailed");
    if (tmp)
        tmp.style.display="none";
    tmp = document.getElementById("login_notice");
    if (tmp)
        tmp.style.display="none";
       
    arguments.IsValid = true;
}

function getLogonUrl()
{
    var retString;
    var cbox = document.getElementById('cbUseSSL'); //.checked = true;
    
    if (!cbox)
        return "";
        
    if (location.protocol == "http:")
    {
        if (cbox.checked == true)   
            retString = "https://" + location.hostname + ":" + httpsport + location.pathname;
        else
            retString = location.pathname;
    }
    else
    {
       if (cbox.checked == true)   
            retString = location.pathname;
        else
            retString = "http://" + location.hostname + ":" + httpport + location.pathname;
    }
    return retString;
}
    
function performSSLCheck()
{
    if (document.getElementById('cbUseSSL'))
            document.getElementById('cbUseSSL').disabled = true;

    var loginBtn = document.getElementById('btnLogin');
    if (loginBtn)
    {
        loginBtn.onclick = function() 
        {
            WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("btnLogin", "", true, "", getLogonUrl(), false, false));
        };
    }

    var secScript = document.createElement("script");
    var path = location.pathname.substring(0, location.pathname.lastIndexOf('/'));
    path = path + "/scripts/CheckSSL.js";

    if (location.protocol == "http:") 
    {
        secScript.src = "https://" + location.hostname + ":" + httpsport + path;
    }
    else 
    {
        document.getElementById("pnlLogonSection").setAttribute("style", "display:block;");
        secScript.src = path;
    }

    secScript.setAttribute("onload", "EnforceSSLRedirection();");
    secScript.setAttribute("onerror", "EnforceSSLRedirection();");

    document.body.appendChild(secScript);
}

function EnforceSSLRedirection() 
{
    if (document.getElementById("cbUseSSL") != null) 
    {
        var sslChecked = document.getElementById("cbUseSSL").checked;

        if (location.protocol == "http:") {
            if (sslWorks) {
                if (sslChecked) {
                    var url = "https://" + location.hostname + ":" + httpsport + parent.location.pathname;
                    parent.window.top.location.href = url;
                    setCookie("useSSL", true, 365);
                }
                else {
                    //do nothing
                }
            }
            else {
                if (enforceSSL == "true") {
                    document.getElementById("pnlEnforceSSLError").setAttribute("style", "display:block;");
                    if (location.pathname == "/LoginDialog.aspx")
                        parent.setHeight();
                }
                else {
                    //do nothing            
                }
            }
        }
        else //https
        {

            if (sslChecked) {
                if (enforceSSL == "true") {
                    document.getElementById("cbUseSSL").disabled = true;
                    setCookie("useSSL", true, 365);
                }
                else {
                    //do nothing
                }
            }
            else {
                var url = "http://" + location.hostname + ":" + httpport + parent.location.pathname;
                parent.window.top.location.href = url
                setCookie("useSSL", false, 365);
            }
        }
    }    
}
