﻿/* 去掉文本内容左右空格 */
function trimVal(item) {
    if (item.attr("type") == "password")
        return item.val();
    else
        return $.trim(item.val());
}
/* 在文本框后提示用户输入 */
function prompt(item, message) {
    item.focus();
    prompts(item, message);
}
/* 在文本框后提示用户输入 */
function prompts(item, message) {
    clearAll(item);
    item.parent().append('<font style="color:red;">' + message + "</font>");
    item.val(trimVal(item));
}
/* 弹出客户端提示框 */
function clientWindow(item, message) {
    item.focus();
    if (message != null && message != "") {
        alert(message);
    }
    item.val(trimVal(item));
}
/* 清空文本框后的提示 */
function clearPrompt(item) {
    if (trimVal(item) != "") {
        clearAll(item);
    }
}
/*  直接清空文本框后所有提示  */
function clearAll(item) {
    item.parent().children('font').remove();
}
/***************************************
登陆页面JS

*****/
$(function () {
    
    setloginpanelposition();
    
    function setloginpanelposition() {
    
        /* 登陆面板位置调整 */
        var bodyheight = document.body.clientHeight;
        
        $('.login_body').css('top', (bodyheight - 290) / 2 + 'px');
    
        pageloadfocus();
    
    
    }
    
    function pageloadfocus() {
    
        /* 打开页面判断光标焦点设置 */
        if ($('#tbUsername').val() == null || $('#tbUsername').val() == "") {
            $('#tbUsername').focus();
        } else if ($('#tbPassword').val() == null || $('#tbPassword').val() == "") {
            $('#tbPassword').focus();
        } else if ($('#tbValidate').val() == null || $('#tbValidate').val() == "") {
            $('#tbValidate').focus();
        }  else {
            $('#tbValidate').val('').focus();
        }
    }
    
    $(window).resize(function() { setloginpanelposition();});
    
    $('#btnLogin').click(function() {//表单提交
        var username = $('#tbUsername');
        var password = $('#tbPassword');
        var valicode = $('#tbValidate');
        if (trimVal(username) == '') {
            prompt(username, '用户名不能为空');
            return false;
        } else {
            clearPrompt(username);
        }
        if (trimVal(password) == '') {
            prompt(password, '密码不能为空');
            return false;
        } else {
            clearPrompt(password);
        }
        if (trimVal(valicode) == '') {
            prompt(valicode, '验证码不能为空')
            return false;
        } else {
            clearPrompt(valicode);
        }
        
        return true;
    });
    $('img[alt="看不清，点击更换!"]').bind('click', function() {
        $('#tbValidate').val('');
        pageloadfocus();
    }).css('cursor', 'pointer').attr('src', 'valicode.ashx?' + new Date().getMilliseconds());
    $('#valicode').click(function() {
        var src = $('img[alt="看不清，点击更换!"]').attr('src');
        src = src.substring(0, 13);
        
        $('img[alt="看不清，点击更换!"]').attr('src', src + "?" + new Date().getMilliseconds())
        pageloadfocus();
        return false;
    }).css('color', '#FFFFFF');
});
