function onLoginClick()
{
    actionAWSLogin();
}

function onLogoutClick()
{
    actionAWSLogout();
}

function saveAWSSession(sessionId, userName, userKind)
{
    var today = new Date();
    today.setMonth(12);
    setCookie("awsSessionId", sessionId, today);
    setCookie("awsUserName", userName);
    setCookie("awsUserKind", userKind);
}

function clearAWSSession()
{
    setCookie("awsSessionId", "", "");
    setCookie("awsUserName", "");
    setCookie("awsUserKind", "");
}

function updateLoginArea()
{
    var userKind = parseInt(getCookie("awsUserKind"));
    var isPrivat = (userKind == 2 || userKind == 10);

    var loginArea = document.getElementById("loginArea");
    loginArea.style.display = isPrivat ? 'none' : 'block';
    var infoArea = document.getElementById("userArea");
    infoArea.style.display = isPrivat ? 'block' : 'none';
}

function actionAWSLogin()
{
    var handleSuccess = function(o)
    {
        var data = YAHOO.lang.JSON.parse(o.responseText);

        if (parseInt(data.error_code) != 0) { // failed
            alert(data.error_message);
        } else { // success
            var userKind = parseInt(data.user_kind);
            if (userKind == 2 || userKind == 10) {
                document.getElementById("userFullName").value = data.user_full_name;
            } else {
                alert("Cannot login");
            }
        }
        saveAWSSession(data.sid, data.user_full_name, data.user_kind);
        updateLoginArea();
        initMenuArea();
        
        doAWSLogin();
    };

    var handleFailure = function(o)
    {
        alert('Login failed');
        clearAWSSession();
        updateLoginArea();
        initMenuArea();
    };

    var callback = {
        success: handleSuccess,
        failure: handleFailure
    };

    var login = document.getElementById("userName").value;
    var password = document.getElementById("userPassword").value;
    var url = "/aws/login.jsp?restype=json&user_name=" + login + "&password=" + password;
    YAHOO.util.Connect.asyncRequest('POST', url, callback);
}

function actionAWSCheckSession()
{
    var handleSuccess = function(o)
    {
        var data = YAHOO.lang.JSON.parse(o.responseText);
        if (parseInt(data.error_code) != 0) { // failed
            alert(data.error_message);
        } else { // success
            var userKind = parseInt(data.user_kind);
            if (userKind == 2 || userKind == 10) {
                document.getElementById("userFullName").value = data.user_full_name;
            }
        }
        saveAWSSession(data.sid, data.user_full_name, data.user_kind);
        updateLoginArea();
        initMenuArea();
        
        doAWSLogin();
    };

    var handleFailure = function(o)
    {
        alert('Check session failed');
        clearAWSSession();
        updateLoginArea();
        initMenuArea();
    };

    var callback = {
        success: handleSuccess,
        failure: handleFailure
    };

    var sid = getCookie("awsSessionId");
    var url = "/aws/login.jsp?restype=json&sid=" + sid;
    YAHOO.util.Connect.asyncRequest('POST', url, callback);
}

function actionAWSLogout()
{
    var handleSuccess = function(o)
    {
        var data = YAHOO.lang.JSON.parse(o.responseText);
        if (parseInt(data.error_code) != 0) { // failed
            //alert(data.error_message); //do nothing now
        } else { // success
            //still do nothing
        }

        document.getElementById("userName").value = "";
        document.getElementById("userPassword").value = "";
        document.getElementById("userFullName").value = "";

        clearAWSSession();
        initMenuArea();
        updateLoginArea();

        doAWSLogout();
    };

    var handleFailure = function(o)
    {
        alert('Logout failed');
        clearAWSSession();
        initMenuArea();
        updateLoginArea();
    };

    var callback = {
        success: handleSuccess,
        failure: handleFailure
    };

    var sid = getCookie("awsSessionId");
    var url = "/aws/logout.jsp?restype=json&sid=" + sid;
    YAHOO.util.Connect.asyncRequest('POST', url, callback);
}

function actionAWSInit()
{
    initLoginArea();
    initUserArea();
    initMenuArea();
    actionAWSCheckSession();
}

function initLoginArea()
{
    var html = "";
    html += "<table><tr><td>";
    html += "<form action=\"this\" id=\"loginForm\">";
    html += "<table><tr><td>";
    html += "User:";
    html += "</td><td>";
    html += "<input type=\"text\" id=\"userName\" value=\"\"/>";
    html += "</td></tr>";
    html += "<tr><td style='padding-top:3px'>";
    html += "Password:";
    html += "</td><td style='padding-top:3px'>";
    html += "<input type=\"password\" id=\"userPassword\" value=\"\"/>";
    html += "</td></tr>";                                          
    html += "<tr><td style='padding-top:7px'>";
    html += "<input type=\"button\" id=\"loginButton\" onclick=\"onLoginClick();\" value=\"Login\"/>";
    html += "</td></tr>";
    html += "</table></form>";
    html += "</td></tr></table>";
    document.getElementById("loginArea").innerHTML = html;
}

function initUserArea()
{
    var html = "";
    html += "<table><tr><td>";
    html += "<input type=\"text\" id=\"userFullName\" value=\"\" readonly class=\"readonlyInput\"/>";
    html += "</td></tr>";
    html += "</td><td style='padding-top:3px'>";
    html += "<input type=\"button\" id=\"logoutButton\" onclick=\"onLogoutClick();\" value=\"Logout\"/>";
    html += "</td></tr></table>";
    document.getElementById("userArea").innerHTML = html;
}

function initMenuArea()
{
    var html = "";
    html += "<table><tr><td><ul>";
    html += "<li><a id=\"pg_main\" href=\"index.html\">Main page</a></li>";
    html += "<li><a id=\"pg_tourn\" href=\"tournaments.html\">Tournament List</a></li>";
    html += "<li><a id=\"pg_sc\" href=\"scorecard.html\">Scorecard</a></li>";
    var userKind = parseInt(getCookie("awsUserKind"));
    if (userKind == 2 || userKind == 10) {
        html += "<li><a id=\"pg_res\" href=\"reservation.html\">Reservation</a></li>";
        html += "<li><a id=\"pg_ev\" href=\"events.html\">Events</a></li>";
        //html += "<li><a class=\"moduleDisabled\" id=\"pg_ev\" href=\"#\">Events</a></li>";
        if (userKind == 10) {
            html += "<li><a id=\"pg_hcp\" href=\"handicaplist.html\">Handicap List</a></li>";
            html += "<li><a id=\"pg_comm\" href=\"commoninfo.html\">Common Info</a></li>";
        }
    } else {
        html += "<li><a id=\"pg_preport\" href=\"problemreport.html\">Report problem</a></li>";
    }
    html += "";
    html += "</ul></td></tr></table>";
    document.getElementById("menuArea").innerHTML = html;
    var module = document.getElementById(aws_active_page);
    if (module == null || typeof module == undefined) {
        window.location.href = "index.html";
    } else {
        module.className = "moduleActive";
    }
}