var gTabsIdArray = new Array();
gTabsIdArray.push("tabHome");
gTabsIdArray.push("tabLogin");
gTabsIdArray.push("tabRegister");

function changeTabs(tabId)
{
    for (var i = 0; i < gTabsIdArray.length; i++) {
        $(gTabsIdArray[i]).className = "tabUnselected";
    }
    $(tabId).className = "tabSelected";
    if (tabId == "tabRegister") {
        window.location.href = "SignUp.do";
    }
    if (tabId == "tabHome") {
        window.location.href = "PublicHome.do";
    }
}

function isLegal(s)
{
    var patrn = /[\S]{3,50}/;
    if (!patrn.exec(s)) {
        return false;
    }
    else {
        return true;
    }
}

function checkAutoLogin()
{
    if ($("check_auto_login").checked) {
        $("auto_login").value = "true";
    }
    else {
        $("auto_login").value = "false";
    }
}

function loginVerify()
{
    var userName = $("userId").value;
    var passwordValue = $("password").value;

    if (userName == null || userName.length == 0) {
        $("msgNotify").innerHTML = "<br>please input user name";
        return false;
    }
    if (passwordValue == null || passwordValue.length == 0) {
        $("msgNotify").innerHTML = "<br>please input password";
        return false;
    }
    // for userName and userEmail both be used to login, disable this check
    /*var regEmailAdr = /^([^@\s])+@([^@\s])+$/;
    if (!regEmailAdr.exec(userName)) {
        $("msgNotify").innerHTML = "Illegal email address";
        return false;
    }*/

    $("userPass").value = hex_md5(passwordValue);
    var tmpPassword = "";
    var passwordLen = passwordValue.length;
    for (var i = 0; i < passwordLen; i++) {
        tmpPassword += "*";
    }
    $("userPassTemp").value = tmpPassword;
    $("loginBtn").disabled = true;
    Tool.Wait("Waiting","System is verifying your account....");
    $("loginForm").submit();
}

function submitLogin()
{
    if (loginVerify() == true) {
        document.loginForm.submit();
        return;
        var loginHandler = function (data)
        {
            //window.location.href="PrivateHome.do";
            if (data) {
            }
            else {
                $(msgNotify).innerHTML = "Server error";
            }
        };
        var url = "LoginHandler.do";
        //"?userId="
        //+ $("userId").value + "&userPass="
        //+ $("userPass").value;
        var param = new Object();
        param["userId"] = $("userId").value;
        param["userPass"] = $("userPass").value;
        readRemoteUrl(url, loginHandler, null, param, "POST");

    }
    else {
    }
}

function goForgotPasswordPage()
{
    $("home_content").innerHTML = "<font color='#000000'>Loading...</font>";
    $("home_content").style.height = "500px";
    readRemoteUrl("ForgotPassword.do", null, "home_content");
}


function SetCookie(cookieName, cookieValue, cookieTime)
{
    date = new Date();
    date = date + cookietime;
    document.cookie = cookieName + "=" + escape(cookieValue) + ";   expires=" + date.toGMTString();
}

function keyPressLogin(evt)
{
    var theEvent = window.event || arguments.callee.caller.arguments[0];
    if (theEvent) {
        if (theEvent.keyCode == 13) {
            loginVerify();
        }
    }
}
