﻿window.onload = function() {
    if ($('#username').val().length < 4 && $('#username').val().length > 1)
        $('#username').focus();
}
function showCheckCode() {
    var checkcode = document.getElementById('checkcode');
    if (checkcode) {
        if (checkcode.innerHTML == "" || document.getElementById('codeimg') == null)
            checkcode.innerHTML = '验证码<input type="text" maxlength="4" name="code" id="code" class="inp01" /> <img id="codeimg" src="CheckCode/Index.aspx" alt="看不清?换一张"/> <a href="javascript:void(0)" onclick="ChangeCode();">换一张</a>';
    }
}

function ChangeCode() {
    $('#codeimg')[0].src = 'CheckCode/Index.aspx?id=' + Math.random();
    return false;
}


function Trim(Str) {
    var ResultStr = "";
    Temp = Str.split(/\s/);
    for (i = 0; i < Temp.length; i++)
        ResultStr += Temp[i];
    return ResultStr;
}

function LoginSubmit() {
    var username = $('#username').val();
    username = Trim(username);
    var password = $('#password').val();
    var code = $('#code').val();
    var codeText = "";
    if (code) codeText = Trim(code.toLowerCase());
    if (username.length < 4) {
        alert('帐号错误!');
    } else if (password.length < 4) {
        alert('密码错误!');
    } else if (codeText.length < 4) {
        alert('验证码错误!');
    } else {
        document.getElementById('login').disabled = true;
        $.post('Home/Login', {
            username: username,
            password: password,
            code: codeText
        }, function(msg) {
            //0,登录成功.1.验证码错误,2用户名或密码错误,-1.未知错误.
            if (msg == 0) {
                alert('登录成功!');
                location.href = "/User/Index"
                return;
            }
            else if (msg == 1) alert('验证码错误!');
            else if (msg == 2) alert('用户名或密码错误!');
            else if (msg == -1) alert('登录失败,未知错误,请稍后再试!');
            document.getElementById('login').disabled = false;
            $('#code').val('');
            $('#codeimg')[0].src = 'CheckCode/Index.aspx?id=' + Math.random();
        });
        //AjaxSubmit('/Home/Login", "username=" + username + "&password=" + password + "&code=" + codeText, InitForAjax);
    }
}

