var System = {
	init: function(cfg) {
		if (cfg.container) this.ctn = Ext.get(cfg.container);
		if (cfg.message) this.msg = Ext.get(cfg.message);
		if (cfg.status) this.sts = Ext.get(cfg.status);
		if (typeof cfg.contextPath != 'undefined') {
			this.ctxPth = cfg.contextPath;
		}
		if (cfg.loadMsg) this.loadMsg = cfg.loadMsg;
	},
	
	done: function() {
		this.sts.update(this.DONE);
	},
	
	message: function(msg) {
		this.msg.update(msg, true);
	},
	
	failed: function() {
		this.sts.update(this.FAILED);
	},
	
	loading: function() {
		this.sts.update(this.LOADING);
	},
	
	redirect: function(url, msg) {
	    this.sts.update(this.REDIRECT);
	    var a = msg.match(/success">([0-9]+)</);
	    this.go(url, 'id='+a[1], msg);
	},
	
	go: function(url, params, msg) {
		System.loading();
        Ext.Ajax.request({
        	url: this.ctxPth+url,
        	method: 'GET',
        	params: params,
        	scope: this,
        	success: function(res, opt) {
        		this.ctn.update(res.responseText, true);
        		if (typeof msg != 'undefined' && msg) {
        			this.sts.update(msg);
        		} else {
            		System.done();
        		}
        	},
        	failure: function(res, opt) {
        		this.failed();
        	}
        });
	},
	
	request: function(info) {
	    info.url = this.ctxPth+info.url;
	    Ext.Ajax.request(info);
	},
	
	getContainer: function() {
		return this.ctn;
	},
	
	isSuccess: function(msg) {
	    return msg.search(/id="success"/) > 0;
	},
	
	topMsgCt: null,
    topMsg: function(t, m) {
        var arr = ['<div class="top-msg">'];
        if (t) arr.push('<div class="t">'+t+'</div>');
        if (m) arr.push('<div class="b">'+m+'</div>');
        arr.push('</div>');
        
        if (this.topMsgCt == null){
            this.topMsgCt = Ext.DomHelper.insertFirst('top_msg_ctn', {id:'msg-div'}, true);
        }
        this.topMsgCt.alignTo(document, 't-t');
        var m = Ext.DomHelper.append(this.topMsgCt, {html:arr.join('')}, true);
        m.slideIn('t').pause(15).ghost("t", {remove:true});
    },
    getFrm: function(cfg) {
        var msg = null;
        switch (typeof cfg.msg) {
            case 'string': msg = Ext.get(cfg.msg); break;
            case 'object': msg = cfg.msg; break;
        }
        
        var result = null;
        switch (typeof cfg.result) {
            case 'string': result = Ext.get(cfg.result); break;
            case 'object': result = cfg.result; break;
        }
        
        var sbmtBtn = null;
        switch (typeof cfg.sbmtBtn) {
            case 'string': sbmtBtn = Ext.get(cfg.sbmtBtn); break;
            case 'object': sbmtBtn = cfg.sbmtBtn; break;
        }
        
        return {
            msg: msg,
            result: result,
            sbmtBtn: sbmtBtn,
            loading: false,
            load: function(t) {
                this.loading = t;
                if (this.sbmtBtn) {
                    this.sbmtBtn.dom.disabled = t;
                }
                if (this.msg) {
                    this.msg.update(t?System.loadMsg:'', true);
                }
            }
        }
    }
};
System.DONE = "Done";
System.FAILED = "Failed";
System.LOADING = "Loading ...";
System.REDIRECT = "Redirecting ...";

Date.patterns = {
	ISO8601Short:'Y-m-d',
    ISO8601Long:'Y-m-d H:i:s',
    SimpleShort:'Y-n-j'
};
Date.tryParseDate = function(d) {
	for (var p in this.patterns) {
        var a = Date.parseDate(d, eval('Date.patterns.'+p));
        if (typeof a != 'undefined') return a;
	}
	return null;
};