﻿// JScript File

window.onload = function() {

    var LoginForm = document.getElementById("LoginForm");

    if(LoginForm)
    {
        LoginForm.onsubmit = function(e)
        {
            var LoginComplete = function(IsAuthenticated)
            {
                if(IsAuthenticated)
                {
                    location = "http://" + location.hostname + "/ProxEASE/Members";
                }
                else
                {
                    document.getElementById("LoginFeedback").innerHTML = 
                        "Username or password is incorrect.";
                }
            }
            
            var LoginFailed = function()
            {
                AttemptLogin();
            }
            
            var AttemptCounter = 0;
            var AttemptLogin = function()
            {
                ++AttemptCounter;
                if (AttemptCounter > 3)
                {
                    document.getElementById("LoginFeedback").innerHTML = 
                        "Could not communicate with exqty.com. Please check your internet connection.";
                }
                else
                {
                    Authentication.Login(document.getElementById("Email").value,
                        document.getElementById("Password").value, LoginComplete, LoginFailed);
                }
            }
            
            AttemptLogin();
                
            if (e && e.preventDefault)
                e.preventDefault();
            return false;
        }
    };

    var LogoutButton = document.getElementById("Logout");
    if(LogoutButton)
    {
        LogoutButton.onclick = function()
        {
            var LogoutComplete = function() 
            {
                if (location.pathname.length > 17 &&
                    location.pathname.substr(0, 17) === "/ProxEASE/Members")
                {
                    location = "http://" + location.host + "/ProxEASE/Default.aspx";
                }
                else
                {
                    location = "http://" + location.host + location.pathname;
                }
            };
            
            var LogoutFailure = function()
            {
                AttemptLogout();
            };
            
            var AttemptCounter = 0;
            var AttemptLogout = function()
            {
                ++AttemptCounter;
                if (AttemptCounter > 3)
                {
                    alert("Could not communicate with exqty.com.  Please check your internet connection.");
                }
                else
                {                
                    Authentication.Logout(LogoutComplete, LogoutFailure);
                }
            };
            
            AttemptLogout();
        }
    };
    
};