Ext.onReady(function(){
	
	var loginPanel = Ext.get("login_panel");
	
	var loginBtn = Ext.get("submitBtn");
	loginBtn.on({
		'click': { fn: login }
	});

	function login(){		
		var userField = Ext.get("field1");
		var user = userField.dom.value;
		var pwdField = Ext.get("field2");
		var passwd = pwdField.dom.value;
		var toUrl =  Ext.get("field3").dom.value;
		if(validate(user) === false){
			alert("账号不能为空！");
			return false;
		}
		
		if(validate(passwd) === false){
			alert("密码不能为空!");
			return false;
		}
		
		//loginPanel.mask('Please wait...', 'x-mask-loading');
		
		Ext.Ajax.request({
			url: '../admin/action.php',
			//url: 'login',
			 method :'POST',
			 params: {
				action: 'login'
				, user: user
				, passwd: passwd
			 },
			 success: function(o){
				loginPanel.unmask();
				if(typeof o == 'object'){
					var d = Ext.decode(o.responseText);
					if(typeof d == 'object'){
						if(d.success == true){
							if(d.message=='ok'){
								//var path = window.location.pathname,
								//path = path.substring(0, path.lastIndexOf('/') + 1);
								window.location = toUrl;
							}else{
								alert(d.message);
							}
								
						}else{
							if(d.errors && d.errors[0].msg){
								alert(d.errors[0].msg);
							}else{
								alert('Errors encountered on the server.');
							}
						}
					}
				}
			}
			, failure: function(){
				loginPanel.unmask();
				alert('Lost connection to server.');
			}
		});
	}
	
	function positionPanel(el, x, y){
        if(x && typeof x[1] == 'number'){
            y = x[1];
            x = x[0];
        }
        el.pageX = x;
        el.pageY = y;
       	
        if(x === undefined || y === undefined){ // cannot translate undefined points
            return;
        }
        
        if(y < 0){ y = 10; }
        
        var p = el.translatePoints(x, y);
        el.setLocation(p.left, p.top);
        return el;
    }
    

    function validate(field){
		if(field === ""){
			//field.markInvalid();
			return false;
		}
		return true;
	}
});