/*
 * Ext JS Library 2.0 RC 1
 * Copyright(c) 2006-2007, Ext JS, LLC.
 * licensing@extjs.com
 * 
 * http://extjs.com/license 
 */



function changePassword(win)
{

    if (Ext.get('newpassword1').getValue() != Ext.get('newpassword2').getValue()  )
    {
        Ext.Msg.alert("新密码输入有误!") ;
    }else
    {    
                Ext.Ajax.request(
                {
                    url:"login.py/login",
                    params:{username:Ext.get('username').getValue(),password:Ext.get('password').getValue(),
                        company:Ext.get('company').getValue(),newpassword1:Ext.get('newpassword1').getValue(),
                        newpassword2:Ext.get('newpassword2').getValue()},
                    method:"post",
                    success:function(result,request)
                    {
                        ret = Ext.util.JSON.decode(result.responseText)
                        if (ret.flag == "OK") {
                            Ext.Msg.alert('提示','修改密码成功!');
                            window.location.href="index.html" ;                           
                        }
                        else{Ext.Msg.alert('提示','修改密码不成功,请重新修改.'); }
                    }
                    })   
     }                            
}    


function login() {
                Ext.Msg.wait("","正在登录...");
                count++ ;
                Ext.Ajax.request(
                {
                    url:"login.py/login",
                    params:{username:Ext.get('username').getValue(),password:Ext.get('password').getValue(),company:Ext.get('company').getValue()},
                    method:"post",
                    success:function(result,request)
                    {
                        ret = Ext.util.JSON.decode(result.responseText)
                        if (ret.flag == "OK") {
                            if (ret.days >= 0 & ret.days <= 7) {alert('用户即将超期,请联系系统管理员')}
                            if (ret.days < 0) {alert('用户超期,请联系系统管理员'); return ;}
                            if (Ext.get('username').getValue().toUpperCase() == 'SYSTEM'){window.location.href="op_define_object.html"}
                            else{window.location.href="op_business_query.html" ;}                            
                        }
                        else{Ext.Msg.alert('提示','用户名或密码错误!'); }
                    }
                    })                
 }  
 
function portal() {
    
    if (Ext.get('newpassword1').getValue().length <=0 && Ext.get('newpassword1').getValue().length <= 0 )
      {
        login() ;
      } else
      {
        changePassword()  
      } 
    
}

Ext.onReady(function() {

    Ext.BLANK_IMAGE_URL = "../ext20/s.gif" ;
    Ext.get('loading').fadeOut({remove: true});
    count = 0 ;
    var form = new Ext.form.FormPanel({
        id:'field',
        baseCls: 'x-plain',
        labelWidth: 75,
        defaultType: 'textfield',

        items: [{
            id:'username',
            fieldLabel: '用户名',
            name: 'username',
            anchor:'70%',  // anchor width by percentage
            selectOnFocus:true
        },{
            id:'password',
            fieldLabel: '密    码',
            inputType:'password',
            name: 'password',
            anchor: '70%',  // anchor width by percentage
            handler: {},
            selectOnFocus:true
        },{
            id:'company',
            fieldLabel: '公司代码',
            name: 'company',
            selectOnFocus:true,
            anchor:'55%',  // anchor width by percentage
            listeners: {
                'click':{fn:function(){alert('test');}, scope:this }
        }},{
            hidden:true,
            hideLabel:true,
            id:'passwordLabel',            
            //fieldLabel: '输入用户名和密码,并将新密码输入两次)',
            value:"输入用户名和密码,并将新密码输入两次)",
            grow:true,
            //anchor:'55%',
            disabled:true
        },
{            
            hidden:true,
            hideLabel:true,
            id:'newpassword1',
            inputType:'password',
            name: 'newpassword1',
            anchor: '50%',
            selectOnFocus :true
    
        },        
        {            
            hidden:true,
            hideLabel:true,
            id:'newpassword2',
            inputType:'password',
            name: 'newpassword2',
            anchor: '50%',
            selectOnFocus :true

        }        
        ]
    });

    var ext_win = new Ext.Window({
        id:'login',
        title: '请输入用户名与密码',
        width: 280,
        height:160,
        minWidth: 240,
        minHeight: 140,
        modal:true,
        layout: 'fit',
        plain:true,
        bodyStyle:'padding:5px;',
        buttonAlign:'center',
        items: form,
        resizable:false,
        buttons: [{
            text: '确定',
            handler:portal            
           }
        ,{
            text: '取消',
            handler: function() {ext_win.destroy();}
        },
        {
            text: '修改密码',
            handler: function() 
            {
                ext_win.setHeight(240); 
                Ext.getCmp('newpassword1').show(); 
                Ext.getCmp('newpassword2').show();
                Ext.get('passwordLabel').value = "aaaaaaaaaa";
                Ext.getCmp('passwordLabel').show();
            }  
        }
        ]
    });
    
    ext_win.show();
    
});

