if (!hk) var hk = {};
if (!hk.isn) hk.isn = {};
if (!hk.isn.crm) hk.isn.crm = {};
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;
		}
		
		// For global tooltip instance
		Ext.QuickTips.init();
	},
	
	done: function() {
		this.sts.update(this.DONE);
	},
	
	message: function(msg) {
		this.msg.update(msg, true);
		this.sts.update(this.DONE);
	},
	
	failed: function() {
		this.sts.update(this.FAILED);
	},
	
	loading: function() {
		this.sts.update(this.LOADING);
		window.location = '#';
	},
	
	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();
        	}
        });
		this.msg.update("", true);
	},
	
	request: function(info) {
	    info.url = this.ctxPth+info.url;
	    Ext.Ajax.request(info);
	    window.location = '#';
	},
	
	getContainer: function() {
		return this.ctn;
	},
	
	isSuccess: function(msg) {
	    return msg.search(/id="success"/) > 0; 
	},
	
	setTitle: function(title) {
	   document.title = this.BASE_TITLE + title;
	}
};
System.DONE = '';
System.FAILED = 'Failed';
System.LOADING = 'Loading ...';
System.REDIRECT = 'Redirecting ...';
System.BASE_TITLE = 'Asia Pacific Academy of Ophthalmology (APAO) - ';

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;
};
