/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
var LoginAjax = {
    _ajax_url : "/Login.do?action=LOGIN",
    
    _form_id : "login_form",
    _loading_msg_id : "loading_msg",
    _error_msg_id : "message_div",
    _submit_btn_id : "login_btn",
    
    submit : function(){
        
        // VALIDATE FORM
        if(! this._validateForm()){
            return;
        }
        $(this._error_msg_id).hide();
        //var param = $(this._form_id).serialize();
        var param = new Hash();
        param.set('email', $F('email_id'));
        param.set('passwd', MD5($F('passwd')));
        param.set('remember', $F('remember'));
        
        new Ajax.Request(this._ajax_url, 
        { 	
            parameters: param.toQueryString(),
            requestHeaders: {
                Accept: 'application/json'
            },
            onCreate: function(){
                LoginAjax._onCreate();
            },
            onSuccess: function(transport){
                var json = transport.responseText.evalJSON(true);
                if(json.RETURN){
                    LoginAjax._onSuccess(json);
                }else{
                   
                    $('email_id').blur();
                    $('passwd').blur();
                    $("login_form").enable();
                    LoginAjax._onFailure(json.MESSAGE);
                    
                    if(json.MESSAGE =="密码错误" ){                         
                        $('passwd').setValue('');
                        $('passwd').focus();
                    }
                    if( json.MESSAGE=="用户不存在"){
                        //$('email').setValue('');
                        $('email_id').focus();
                    }
                   
                }
            },
            onComplete : function(){
                LoginAjax._onComplete();
            }
                    
        });
    },
    
    _validateForm : function(){
        if(!$('email_id').present()){
            show_error_msg(this._error_msg_id, '请输入邮箱地址');
            $('email_id').focus();
            return false;
        }
        if ( !checkEmail($F('email_id')) ) {
            show_error_msg(this._error_msg_id, '邮箱地址格式不正确');
            $('email_id').focus();
            return false;
        }
        if(!$('passwd').present()){
            show_error_msg(this._error_msg_id, '请输入密码');
            $('passwd').focus();
            return false;
        }
        return true;
    },
    _onCreate : function(){
        $(this._form_id).disable();
        $(this._loading_msg_id).show();
    },
    _onSuccess : function(json){
        var red = $("redirect_url").getValue();
        if(red.trim().length==0){
            document.location.href= 'home.jsp';
        }else{
            document.location.href= red;
        }
    },
    
    _onFailure : function(msg){
        show_error_msg(this._error_msg_id, msg);
    },
    _onComplete : function(){
        $(this._loading_msg_id).hide();
        $(this._form_id).enable();
    },

    submitForm : function(){
        
        if(!this._validateForm()){
            return false;
        }
        $('email_value').value = $F('email_id');
        $('passwd_value').value = MD5($F('passwd'));
        //this._onCreate();
        $(this._loading_msg_id).show();
        $(this._submit_btn_id).disable();
        //alert("xxxx");
        return true;
    }
    
}


