﻿    var $ = jQuery.noConflict();
    $(document).ready(function(){
        $('#txtUserName').val('');
        $('#txtPwd').val('');
        $('#txtCheckCode').val('');
        setLoginStyle();
        isLogin();
    });
    
    function setLoginStyle(){
        $('#div_login').find('input[ltype]').each(function(){
            $(this).focus(function(){
                $(this).attr('style', 'background-image:url("/images/input_bg.gif")');
                $(this).next().hide();
            });
            
            $(this).blur(function(){
               $(this).attr('style', 'background-image:url("/images/input_bg.gif")');
                if($(this).val() == ''){
                    $(this).removeAttr('style');
                }
            });
        });
    }
    
    function changeImgCode(){
        document.getElementById('imgCode').src = '/Ajax.aspx?operate=GetCheckCode&rd=' + Math.random();
    }
    
    function checkLoginForm(){
        $('#sp_error').html('');
        var canSub = true;
        if($('#txtUserName').val() == ''){
            canSub = false;
            $('#txtUserName').next().show();
        }
        if($('#txtPwd').val() == ''){
            canSub = false;
            $('#txtPwd').next().show();
        }
        if($('#txtCheckCode').val() == ''){
            canSub = false;
            $('#txtCheckCode').next().show();
        }
        return canSub;
    }
    
    function login(){
        if(!checkLoginForm()){
            return false;
        }
        var userName = $('#txtUserName').val();
        var userPwd = $('#txtPwd').val();
        var code = $('#txtCheckCode').val();
        $('#img_loginWait').show();
        $.ajax({
            url : '/Ajax.aspx?rd=' + Math.random(),
            type : 'post',
            data : 'operate=Login&userName=' + userName + '&userPwd=' + userPwd + '&code=' + code,
            dataType : 'json',
            success : function(data){
                $('#img_loginWait').hide();
                initLoginSuccessDiv(data, true);
            }
        });
    }
    
    function isLogin(){
        $('#img_loginWait').show();
        $.ajax({
            url : '/Ajax.aspx?rd=' + Math.random(),
            type : 'get',
            data : 'operate=IsLogin',
            dataType : 'json',
            success : function(data){
                $('#img_loginWait').hide();
                initLoginSuccessDiv(data, false);
            }
        });
    }
    
    function initLoginSuccessDiv(data, showMsg){
        changeImgCode();
        if(data == null){
            if(showMsg){
                $('#sp_error').html('Login Failed!');
            }
        }else{
            $('#div_login').hide();
            $('#div_loginSuccess').show();
            $('#sp_loginUser').html(data.Fullname);
        }
    }
    
    function loginOut(){
        $('#img_loginWait').show();
        $.ajax({
            url : '/Ajax.aspx?rd=' + Math.random(),
            type : 'get',
            data : 'operate=LoginOut',
            success : function(){
                $('#img_loginWait').hide();
                $('#sp_loginUser').html('');
                $('#div_loginSuccess').hide();
                $('#div_login').show();
            }
        });
    }