/**
 * 网上公积金项目Ext初始化 By xuke
 */
Ext.BLANK_IMAGE_URL = '/hfnet/style/ext/resources/images/default/s.gif';

/**
 * 表单全键盘导航功能 xFocus:可选参数，用于设定页面加载完成后默认获取焦点的表单项，支持索引号和id/dom类型参数传入。
 */
var keyNav = function(xFocus) {
	var run = function() {
		var all = Ext.DomQuery.select('input[type!=hidden][class!="x-tbar-page-number"]'); // 查找所有非隐藏的录入项
		// var all1 = Ext.DomQuery.select('button'); //如果button的焦点顺序不对要加上这个
		// var all = all.concat(all1);
		Ext.each(all, function(o, i, all) { // 遍历并添加enter的监听
			    Ext.get(o).addKeyMap({
				        key : 13,
				        fn : function() {
					        try {
						        all[i + 1].focus();
					        } catch (e) {
						        event.keyCode = 9;
					        }
					        if (all[i + 1] && /button|reset|submit/.test(all[i + 1].type))
						        all[i + 1].click(); // 如果是按钮则触发click事件
					        return true;
				        }
			        })
		    });
		document.body.focus(); // 使页面获取焦点，否则下面设定默认焦点的功能有时不灵验
		try {
			var el;
			if (typeof eval(xFocus) == 'object') { // 如果传入的是id或dom节点
				el = Ext.getDom(xFocus).tagName == 'input' ? Ext.getDom(xFocus) : Ext.get(xFocus).first('input', true); // 找到input框
			} else {
				el = all[xFocus || 0]; // 通过索引号找
			}
			el.focus();
		} catch (e) {
			xFocus.focus();
		}
	}
	Ext.isReady ? run() : Ext.onReady(run); // 页面加载完成后添加表单导航
}

// 支持Grid中文排序
Ext.data.Store.prototype.applySort = function() {
	if (this.sortInfo && !this.remoteSort) {
		var s = this.sortInfo, f = s.field;
		var st = this.fields.get(f).sortType;
		var fn = function(r1, r2) {
			var v1 = st(r1.data[f]), v2 = st(r2.data[f]);
			if (typeof(v1) == "string") {
				return v1.localeCompare(v2);
			}
			return v1 > v2 ? 1 : (v1 < v2 ? -1 : 0);
		};
		this.data.sort(s.direction, fn);
		if (this.snapshot && this.snapshot != this.data) {
			this.snapshot.sort(s.direction, fn);
		}
	}
};
function unmaskDocAll() {
	var body = parent.Ext.get("tabs");
	body.unmask();
}
Ext.form.TextField.prototype.size = 20;
Ext.form.TextField.prototype.initValue = function() {
	if (this.value !== undefined) {
		this.setValue(this.value);
	} else if (this.el.dom.value.length > 0) {
		this.setValue(this.el.dom.value);
	}
	this.el.dom.size = this.size;
	if (!isNaN(this.maxLength) && (this.maxLength * 1) > 0 && (this.maxLength != Number.MAX_VALUE)) {
		this.el.dom.maxLength = this.maxLength * 1;
	}

};