/** 按钮配置对象 */
var BtnCfg = function(cfgProperties)
{
	if(cfgProperties != undefined)
	{
		/** jquery按钮对象 */
		this.$btnEl = cfgProperties.$btnEl;
		/** 按钮点击动作类型 */
		this.actionType = cfgProperties.actionType;
		/** 关联URL */
		this.url = cfgProperties.url;
		/** 按钮点击关联jquery对象 */
		this.$targetEl = cfgProperties.$targetEl;
		/** 自定义函数 */
		this.customFn = cfgProperties.customFn;
		/** 执行成功后的回调函数 */
		this.callbackFn = cfgProperties.callbackFn;
		/** 执行动作之前的预处理函数，返回false时中止动作。 */
		this.preProcessFn = cfgProperties.preProcessFn;
		/** confirmMsg */
		this.confirmMsg = cfgProperties.confirmMsg;
		/**额外参数对象*/
		this.extraParam = cfgProperties.extraParam;
		/** 目标datagrid Jquery对象（仅适用于【ACTION_TYPE = OPEN_WINDOW_OF_EDIT_DATAGRID_ROW】的情况） */
		this.$datagridEl = cfgProperties.$datagridEl;
		/** datagrid的id请求参数名（仅适用于【ACTION_TYPE = OPEN_WINDOW_OF_EDIT_DATAGRID_ROW】的情况） */
		this.gridIdParamName = cfgProperties.gridIdParamName;
	}
};

/** 动作类型 */
BtnCfg.ACTION_TYPE = 
{
	/** 打开窗口 */
	OPEN_WINDOW : 0,
	/** 关闭窗口 */
	CLOSE_WINDOW : 1,
	/** 提交表单 */
	SUBMIT_FORM : 2,
	/** 重置表单 */
	RESET_FORM : 3,
	/** 保存grid脏数据 */
	SAVE_GRID : 4,
	/** 取消保存grid脏数据 */
	CANCEL_SAVE_GRID : 5,
	/** 删除grid记录 */
	DELETE_GRID : 6,
	/** 自定义函数 */
	CUSTOM_FN : 7,
	/** 编辑datagrid行 */
	EDIT_DATAGRID_ROW : 8,
	/** 打开编辑datagrid行的窗口 */
	OPEN_WINDOW_OF_EDIT_DATAGRID_ROW : 9
};

/** 表单配置对象 */
var FormCfg = function(cfgProperties)
{
	if(cfgProperties != undefined)
	{
		/** jquery表单对象 */
		this.$formEl = cfgProperties.$formEl;
		/** 表单提交动作类型 */
		this.actionType = cfgProperties.actionType;
		/** 表单提交关联jqueryDom对象 */
		this.$targetEl = cfgProperties.$targetEl;
		/** 执行动作之后的回调函数 */
		this.callbackFn = cfgProperties.callbackFn;
		/** 自定义事件 */
		this.customFn = cfgProperties.customFn;
		/** 表单提交关联dataGrid对象（不适用于ACTION_TYPE == LOAD_GRID） */
		this.$targetGrid = cfgProperties.$targetGrid;
		/**额外参数对象*/
		this.extraParam = cfgProperties.extraParam;
	}
};

/** 动作类型 */
FormCfg.ACTION_TYPE =
{
	/** 读取datagrid */
	LOAD_GRID : 0,
	/** 刷新datagrid */
	RELOAD_GRID : 1,
	/** 刷新tree */
	REFRESH_PANEL : 2,
	/** 关闭窗口 */
	CLOSE_WINDOW : 3,
	/** 自定义动作 */
	CUSTOM_FN : 4
};

/** datagrid配置对象 */
var GridCfg = function(cfgProperties)
{
	if(cfgProperties != undefined)
	{
		/** jquery dataGrid对象 */
		this.$gridEl = cfgProperties.$gridEl;
		/** datagrid的属性配置对象 */
		this.propCfgObj = cfgProperties.propCfgObj;
		/** datagrid动作对象数组 */
		this.actionCfgArr = cfgProperties.actionCfgArr;
	}
};

/** datagrid动作配置对象 */
var GridActionCfg = function(cfgProperties)
{
	if(cfgProperties != undefined)
	{
		/**事件类型*/
		this.eventType = cfgProperties.eventType;
		/**动作类型*/
		this.actionType = cfgProperties.actionType;
		/**目标元素*/
		this.$targetEl = cfgProperties.$targetEl;
		/**id参数名*/
		this.idParamName = cfgProperties.idParamName;
		/**额外参数对象*/
		this.extraParam = cfgProperties.extraParam;
		/**url*/
		this.url = cfgProperties.url;
		/**自定义函数*/
		this.customFn = cfgProperties.customFn;
		/** 执行动作之后的回调函数 */
		this.callbackFn = cfgProperties.callbackFn;
		/** 执行动作之前的预处理函数，返回false时中止动作。 */
		this.preProcessFn = cfgProperties.preProcessFn;
	}
};

/** datagrid触发事件类型 */
GridActionCfg.EVENT_TYPE =
{
	/** 选择行事件 */
	ON_SELECT_ROW : 'onSelect',
	/** 单击行事件 */
	ON_CLICK_ROW : 'onClickRow',
	/** 双击行事件 */
	ON_DB_CLICK_ROW : 'onDblClickRow',
	/** 双击单元格事件 */
	ON_DB_CLICK_CELL : 'onDblClickCell'
};

/** datagrid动作类型 */
GridActionCfg.ACTION_TYPE =
{
	/** 读取另一个DATA GRID */
	LOAD_GRID : 0,
	/** 编辑行 */
	EDIT_ROW : 1,
	/** 解除编辑行 */
	END_EDIT_ROW : 2,
	/** 读取一个panel */
	LOAD_PANEL : 3,
	/** 打开一个window */
	OPEN_WINDOW : 4,
	/** 自定义函数 */
	CUSTOM_FN :5
};

var SelectCfg = function(cfgProperties)
{
	if(cfgProperties != null)
	{
		/** jquery下拉单对象 */
		this.$selectEl = cfgProperties.$selectEl;
		/** 选择后动作类型 */
		this.actionType = cfgProperties.actionType;
		/** 关联URL */
		this.url = cfgProperties.url;
		/** 选择后关联jqueryDom对象 */
		this.$targetEl = cfgProperties.$targetEl;
		/** 返回下拉单JSON数据的key */
		this.responseDataKey = cfgProperties.responseDataKey;
		/** 返回JSON的下拉单显示文本的KEY */
		this.responseDataTextKey = cfgProperties.responseDataTextKey;
		/** 返回JSON的下拉单值的KEY */
		this.responseDataValueKey = cfgProperties.responseDataValueKey;
		/** 请求参数名 */
		this.paramName = cfgProperties.paramName;
		/** 额外请求参数对象 */
		this.extraParam = cfgProperties.extraParam;
		/** 请求成功后的回调函数 */
		this.callbackFn = cfgProperties.callbackFn;
		/** 执行动作之前的预处理函数，返回false时中止动作。 */
		this.preProcessFn = cfgProperties.preProcessFn;
		/** 自定义处理函数 */
		this.customFn = cfgProperties.customFn;
	}
};
SelectCfg.ACTION_TYPE =
{
	/** 填充另一个下拉单 */
	FILL_SELECT : 0,
	/** 读取datagrid */
	LOAD_DATAGRID : 1,
	/** 自定义函数 */
	CUSTOM_FN : 2
};

/**
 * 注册可编辑combobox默认事件
 * @param tips 空值时的提示信息
 * @param $parentEl combobox的父JQUERY对象
 * @param $comboboxEl combobox的JQUERY对象
 */
function regEditComboboxEvent(tips, $parentEl, $comboboxEl)
{
	$parentEl.bind
	({
		focusin : function(e)
		{
			if($comboboxEl.combobox('getValue') == '')
				$comboboxEl.combobox('setText', '');
		},
		focusout : function(e)
		{
			var text = $comboboxEl.combobox('getText');
			if(text != '')
				$comboboxEl.combobox('setValue', text);
			else
				$comboboxEl.combobox('setText', tips);
		}
	});
}

function showCreateWindow(url, $windowEl)
{
	var $detailInfoWindow = $windowEl;
	if($windowEl == null || $windowEl.length == 0)
		$detailInfoWindow = $('#detailInfoWindow');
	
	$detailInfoWindow.window({href : url});
	$detailInfoWindow.window('open');
}

function showUpdateWindow(pkId, url)
{
	var $detailInfoWindow = $('#detailInfoWindow');
	
	$detailInfoWindow.window({href : encodeURI(url+"?pkId="+pkId)});
	$detailInfoWindow.window('open');
}

/**
 * 渲染Grid
 * @param $tableEl jquery对象：表格元素
 * @param config dataGrid配置对象
 */
function renderDataGrid($tableEl, gridCfg)
{
	if(gridCfg.checkOnSelect == null)					//选中时check
		gridCfg.checkOnSelect = true;
	if(gridCfg.selectOnCheck == null)					//chekck时选中
		gridCfg.selectOnCheck = true;
	if(gridCfg.singleSelect == null)
		gridCfg.singleSelect = true;
	if(gridCfg.pagination == null)
		gridCfg.pagination = true;
	if(gridCfg.rownumbers == null)
		gridCfg.rownumbers = false;						//默认不显示行号，显示行号对于大数据量存在性能问题。
	if(gridCfg.fitColumns == null)
		gridCfg.fitColumns = true;						//字段调整列宽
	if(gridCfg.striped == null)
		gridCfg.striped = true;							//显示条纹
	if(gridCfg.remoteSort == null)
		gridCfg.remoteSort = false;						//禁用服务器端排序
	if(gridCfg.autoRowHeight == null)
		gridCfg.autoRowHeight = false;
	
	var e = gridCfg.onBeforeLoad;
	
	gridCfg.onBeforeLoad = function(param)					//刷新事件
	{
		$(this).datagrid('unselectAll');					//取消选择状态
		$(this).datagrid('uncheckAll');						//全部取消勾选checkbox
		if(e)
			e(param);
	};
	
	var onSelect = gridCfg.onSelect;
	
	try
	{
		$tableEl.datagrid(gridCfg);
	}
	catch (e)
	{
		$.messager.alert(e.name,e.message,'error');
	}
}

/**
 * 将form的元素组织成{key : value, ...}形式
 * @param {} $form
 */
function buildPostParamsByFormItems($form)
{
	var paramArr = $form.serializeArray();
	var postData = {};
	$.each(paramArr, function(i, paramJson)					//查询条件参数
	{
		if(postData[paramJson.name] != null)
		{
			var tempObj = postData[paramJson.name];
			if(!(tempObj instanceof Array))
				postData[paramJson.name] = [tempObj];
			
			postData[paramJson.name].push(paramJson.value);
		}
		else
			postData[paramJson.name] = paramJson.value;
	});
	return postData;
}

/**
 * 读取dataGrid数据
 * @param $table jquery对象：表格元素
 * @param url grid数据请求url
 * @param paramData 请求参数对象：{paramName : paramValue, ...}形式
 * @param callbackFn 请求成功的回调函数，参数为当前grid对象
 */
function loadGridDataByParam($table, url, param, callbackFn)
{
	if($table != null)
	{
		var opt = $table.datagrid('options');
		if(opt.pagination)		//分页信息
		{
			param.page = 1;
			param.rows = $table.datagrid('getPager').pagination('options').pageSize;
		}
		opt = $.extend(opt, 
		{
			queryParams : param,
   			pageNumber : 1,
   			onLoadSuccess : function(data)
   			{
				if(callbackFn instanceof Function)
					callbackFn($(this));
   			}
		});
		if(opt.url == null)
			opt.url = url;		//给options加上url属性时会自动请求后台数据。
		else
			$table.datagrid('load');
	}
}

/**
 * 验证初始化查询页面的输入参数
 * @param $table
 * @param $form
 * @param gridConfig
 * @return true：通过；false：不通过。
 */
function validateQueryPageParams($table, $form, gridConfig)
{
	if($table.length == 0 || $form.length == 0)
	{
		$.messager.alert('系统错误', '初始化页面时发生错误，找不到查询表格或查询表单。', 'error');
		return false;
	}
	
	var action = $form.attr("action");
	if(action == null || action.length == 0 || action.indexOf('.') == -1)
	{
		$.messager.alert('系统错误', '初始化页面时发生错误，查询表单的请求路径未正确设置。', 'error');
		return false;
	}
	
	var idField = gridConfig.idField;
	if(idField == null || idField.length == 0)
	{
		$.messager.alert('系统错误', '初始化页面时发生错误，没有配置查询结果集的主键。', 'error');
		return false;
	}
	
	var columns = gridConfig.columns;
	if(columns == null || columns.length == 0)
	{
		$.messager.alert('系统错误', '初始化页面时发生错误，没有配置查询结果集的列信息。', 'error');
		return false;
	}
	return true;
}

/**
 * 保存grid脏数据
 * @param {} updateUrl
 * @param {} $grid
 * @return {Boolean}
 */
function saveGrid(updateUrl, $table, callbackFn)
{
	var lastRowIndex = $table.data('lastRowIndex');
	
	$table.datagrid('endEdit', lastRowIndex);
	var updRowArr = $table.datagrid('getChanges', 'updated');
	if(updRowArr.length == 0)
	{
		$.messager.alert('提示', '没有需要更新的数据', 'info');
		return false;
	}
	
	$.ajax
	({
		url : updateUrl,
		data : {objArrStr : JSON.stringify(updRowArr)},
		type : "POST",
		success : function(data, textStatus, jqXHR)
		{
			data = $.parseJSON(data);
			UIutil.alertRespJsonMsg(data);
			
			if(data.flag)
			{
				$table.datagrid('acceptChanges');
				if(callbackFn instanceof Function)
					callbackFn($table);
			}
		}
	});
}

/**
 * 初始化dataGrid
 * @param {} dataGridCfgArr
 */
function initDataGrid(dataGridCfgArr)
{
	$(dataGridCfgArr).each(function(i, dataGridCfg)
	{
		var $dataGrid = dataGridCfg.$gridEl;
		var dataGridPropCfg = dataGridCfg.propCfgObj instanceof Function ? dataGridCfg.propCfgObj() : dataGridCfg.propCfgObj;
		var actionCfgArr = dataGridCfg.actionCfgArr;
		$(actionCfgArr).each(function(j, actionCfg)
		{
			var eventFn = null;
			var actionType = actionCfg.actionType;
			if(actionType == GridActionCfg.ACTION_TYPE.LOAD_GRID)		//读取另一个GIRD
			{
				eventFn = function(rowIndex, rowData)
				{
					var $targetGrid = actionCfg.$targetEl;
					if($dataGrid.datagrid('getSelections').length != 1)
					{
						$targetGrid.datagrid('loadData', []);
						e.preventDefault();
					}
					else
					{
						var idParamName = actionCfg.idParamName;
						var url = actionCfg.url;
						var id = rowData[$dataGrid.datagrid('options').idField];
						if(id == null || id == '')
							return;
						
						var paramCfg = {};
						if(actionCfg.extraParam)
						{
							if(actionCfg.extraParam instanceof Function)
								paramCfg = actionCfg.extraParam(rowIndex, rowData);
							else
								paramCfg = actionCfg.extraParam;
						}
						paramCfg[idParamName] = id;
						
						loadGridDataByParam($targetGrid, url, paramCfg);
					}
				};
			}
			else if(actionType == GridActionCfg.ACTION_TYPE.EDIT_ROW)		//编辑行
			{
				eventFn = function(rowIndex, field, value)
				{
					var lastRowIndex = $dataGrid.data('lastRowIndex');
					
					$dataGrid.datagrid('endEdit', lastRowIndex);
					$dataGrid.datagrid('beginEdit', rowIndex);		//执行行编辑方法
					$dataGrid.data('lastRowIndex', rowIndex);
					
					var ed = $dataGrid.datagrid('getEditor', {index:rowIndex,field:field});
					if(ed != null)
					{
						$(ed.target).focus();
						$(ed.target).select();
					}
				};
			}
			else if(actionType == GridActionCfg.ACTION_TYPE.LOAD_PANEL)		//读取panel
			{
				eventFn = function(rowIndex, rowData)
				{
					var $targetPanel = actionCfg.$targetEl;
					if($dataGrid.datagrid('getSelections').length != 1)
					{
						$targetPanel.panel('body').html('请先选择一条记录。');
						e.preventDefault();
					}
					else
					{
						var idParamName = actionCfg.idParamName;
						var url = actionCfg.url;
						var id = rowData[$dataGrid.datagrid('options').idField];
						
						$targetPanel.panel('refresh', url + '?' + idParamName + '=' + id);
					}
				};
			}
			else if(actionType == GridActionCfg.ACTION_TYPE.END_EDIT_ROW)		//结束编辑
			{
				eventFn = function(rowIndex, rowData)
				{
					var selRows = $dataGrid.datagrid('getRows');
					for(var i in selRows)
					{
						idx = $dataGrid.datagrid('getRowIndex', selRows[i]);
						$dataGrid.datagrid('endEdit', idx);
					}
					
					$dataGrid.data('lastRowIndex', rowIndex);
				};
			}
			else if(actionType == GridActionCfg.ACTION_TYPE.OPEN_WINDOW)		//打开window
			{
				eventFn = function(rowIndex, rowData)
				{
					if(actionCfg.preProcessFn instanceof Function)
					{
						if(actionCfg.preProcessFn(rowIndex, rowData) == false)
							return false;
					}
					
					var $targetWindow = actionCfg.$targetEl;
					$dataGrid.datagrid('unselectAll');
					$dataGrid.datagrid('selectRow', rowIndex);
					
					var idParamName = actionCfg.idParamName || 'id';
					var id = rowData[$dataGrid.datagrid('options').idField];
					var url = actionCfg.url;
					var paramCfg = {};
					if(actionCfg.extraParam)
					{
						if(actionCfg.extraParam instanceof Function)
							paramCfg = actionCfg.extraParam(rowIndex, rowData);
						else
							paramCfg = actionCfg.extraParam;
					}
					paramCfg[idParamName] = id;
					
					url = SplicingUrlAndParams(url, paramCfg);
					$targetWindow.window({href : url});
					$targetWindow.window('open');
				};
			}
			else if(actionType == GridActionCfg.ACTION_TYPE.CUSTOM_FN)		//自定义函数
			{
				eventFn = actionCfg.customFn;
			}
			
			if(eventFn == null)
				return true;
			
			if(actionCfg.eventType == GridActionCfg.EVENT_TYPE.ON_SELECT_ROW)		//选择行事件
			{
				if(dataGridPropCfg[GridActionCfg.EVENT_TYPE.ON_SELECT_ROW] != null)
				{
					var existFn = dataGridPropCfg[GridActionCfg.EVENT_TYPE.ON_SELECT_ROW];
					dataGridPropCfg[GridActionCfg.EVENT_TYPE.ON_SELECT_ROW] = function(rowIndex, rowData)
					{
						existFn(rowIndex, rowData);
						eventFn(rowIndex, rowData);
					};
					
				}
				else
					dataGridPropCfg[GridActionCfg.EVENT_TYPE.ON_SELECT_ROW] = function(rowIndex, rowData){eventFn(rowIndex, rowData);};
			}
			else if(actionCfg.eventType == GridActionCfg.EVENT_TYPE.ON_CLICK_ROW)		//点击行事件
			{
				if(dataGridPropCfg[GridActionCfg.EVENT_TYPE.ON_CLICK_ROW] != null)
				{
					var existFn = dataGridPropCfg[GridActionCfg.EVENT_TYPE.ON_CLICK_ROW];
					dataGridPropCfg[GridActionCfg.EVENT_TYPE.ON_CLICK_ROW] = function(rowIndex, rowData)
					{
						existFn(rowIndex, rowData);
						eventFn(rowIndex, rowData);
					};
				}
				else
					dataGridPropCfg[GridActionCfg.EVENT_TYPE.ON_CLICK_ROW] = function(rowIndex, rowData){eventFn(rowIndex, rowData);};
			}
			else if(actionCfg.eventType == GridActionCfg.EVENT_TYPE.ON_DB_CLICK_ROW)	//双击行事件
			{
				var existFn = dataGridPropCfg[GridActionCfg.EVENT_TYPE.ON_DB_CLICK_ROW];
				dataGridPropCfg[GridActionCfg.EVENT_TYPE.ON_DB_CLICK_ROW] = function(rowIndex, rowData)
				{
					$dataGrid.datagrid('unselectAll');
					$dataGrid.datagrid('selectRow', rowIndex);
					
					if(existFn instanceof Function)
						existFn(rowIndex, rowData);
					
					eventFn(rowIndex, rowData);
				};
			}
			else if(actionCfg.eventType == GridActionCfg.EVENT_TYPE.ON_DB_CLICK_CELL)	//双击单元格事件
			{
				var existFn = dataGridPropCfg[GridActionCfg.EVENT_TYPE.ON_DB_CLICK_CELL];
				dataGridPropCfg[GridActionCfg.EVENT_TYPE.ON_DB_CLICK_CELL] = function(rowIndex, field, value)
				{
					$dataGrid.datagrid('unselectAll');
					$dataGrid.datagrid('selectRow', rowIndex);
					
					if(existFn instanceof Function)
						existFn(rowIndex, field, value);
					
					eventFn(rowIndex, field, value);
				};
			}
		});
		
		renderDataGrid($dataGrid, dataGridPropCfg);
	});
}

/**
 * 注册页面按钮事件
 * @param btnJsonArr 格式为：<pre>
 *[
 *  {
 *    selectEl : $select1,
 *    actionType : BTN_ACTION_TYPE.FILL_SELECT,
 *    targetEl : $target1,
 *    url : "xxx"
 *  },
 *  {...},
 *  {...},
 *  ...
 *]</pre>
 */
function regPageSelectEvent(selectCfgArr)
{
	$(selectCfgArr).each(function(i, selectCfg)
	{
		var $select = selectCfg.$selectEl;
		var actionType = selectCfg.actionType;
		if(actionType == SelectCfg.ACTION_TYPE.FILL_SELECT)		//填充下拉单
		{
			var $targetEl = selectCfg.$targetEl;	//目标下拉单
			var url = selectCfg.url;
			var paramName = selectCfg.paramName;
			var extraParam = selectCfg.extraParam;
			var dataKey = selectCfg.responseDataKey;
			var textKey = selectCfg.responseDataTextKey;
			var valueKey = selectCfg.responseDataValueKey;
			var callbackFn = selectCfg.callbackFn;
			
			$select.change(function(e)
			{
				var targetSelect = $targetEl.get(0);
				if(! $select.val())
				{
					targetSelect.options.length = 1;
					$targetEl.triggerHandler('change');
					e.preventDefault();
				}
				else
				{
					var paramData = {};
					if(extraParam instanceof Function)
						paramData = extraParam();
					else if(extraParam instanceof Object)
						paramData = extraParam;
					
					paramData[paramName] = $(this).val();
					$.ajax
					({
						url : url,
						data : paramData,
						type : "POST",
						success : function(data, textStatus, jqXHR)
						{
							data = $.parseJSON(data);
							UIutil.alertRespJsonMsg(data);
							
							if(data.flag && data[dataKey] != null)
							{
								targetSelect.options.length = 0;
								$(data[dataKey]).each(function(i, item)
								{
									targetSelect.options.add(new Option(item[textKey],item[valueKey]));
								});
								if(callbackFn instanceof Function)
									callbackFn(data);
							}
						}
					});
				}
			});
		}
		else if(actionType == SelectCfg.ACTION_TYPE.LOAD_DATAGRID)		//读取datagird
		{
			var $targetEl = selectCfg.$targetEl;	//目标datagrid
			var url = selectCfg.url;
			var paramName = selectCfg.paramName;
			var extraParam = selectCfg.extraParam;
			$select.change(function(e)
			{
				if($(this).val() == '')
				{
					$targetEl.datagrid('loadData', []);
					e.preventDefault();
				}
				else
				{
					var paramData = {};
					if(extraParam != null)
						paramData = extraParam;
					paramData[paramName] = $(this).val();
					
					loadGridDataByParam($targetEl, url, paramData);
				}
			});
		}
		else if(actionType == SelectCfg.ACTION_TYPE.CUSTOM_FN)			//自定义函数
		{
			var customFn = selectCfg.customFn;
			$select.change(function(e)
			{
				customFn();
			});
		}
	});
}

/**
 * 注册页面按钮事件
 * @param btnCfgArr 格式为：<pre>
 *[
 *  {
 *    btnEl : $btn1,
 *    actionType : BTN_ACTION_TYPE.OPEN_WINDOW,
 *    targetEl : $target1,
 *    url : "xxx"
 *  },
 *  {...},
 *  {...},
 *  ...
 *]</pre>
 */
function regPageBtnEvent(btnCfgArr)
{
	$(btnCfgArr).each(function(i, btnCfg)
	{
		var $btn = btnCfg.$btnEl;
		$btn.unbind('click');
		var actionType = btnCfg.actionType;
		var callbackFn = btnCfg.callbackFn;
		
		if(actionType == BtnCfg.ACTION_TYPE.OPEN_WINDOW)		//打开窗口
		{
			var $targetEl = btnCfg.$targetEl;
			$btn.data('url', btnCfg.url);
			$btn.linkbutton({'onClick': function(e, callbackFn)
			{
				if(btnCfg.preProcessFn instanceof Function)
				{
					if(btnCfg.preProcessFn() == false)
						return false;
				}
				
				if(callbackFn instanceof Function)
				{
					$targetEl.window
					({
						onLoad : function(){callbackFn($targetEl);}
					});
				}
				showCreateWindow($btn.data('url'), $targetEl);
			}});
			if($targetEl) 
			{
				$targetEl.window({
					onOpen : function(){
						$(".coveredEle").css("display" , "none");
					},
					onClose : function(){
						$(".coveredEle").css("display" , "inline");
					}
				});
			}
		}
		else if(actionType == BtnCfg.ACTION_TYPE.OPEN_WINDOW_OF_EDIT_DATAGRID_ROW)	//打开datagrid行的编辑窗口
		{
			$btn.data('url', btnCfg.url);
			var $targetEl = btnCfg.$targetEl;
			var $datagridEl =btnCfg.$datagridEl;
			var gridIdParamName =btnCfg.gridIdParamName || 'id';
			if($targetEl) 
			{
				$targetEl.window
				({
					onOpen : function()
					{
						$(".coveredEle").css("display" , "none");
					},
					onClose : function()
					{
						$(".coveredEle").css("display" , "inline");
					}
				});
			}
			$btn.linkbutton({'onClick': function()
			{
				var text = $btn.text();
				var selRowArr = $datagridEl.datagrid('getSelections');
				if(selRowArr.length == 0)
					$.messager.alert('提示', '请选择要' + text + '的行。', 'info');
				else if(selRowArr.length > 1)
					$.messager.alert('提示', '无法确定要' + text + '的行，只能选择一行' + text + '。', 'info');
				else
				{
					var rowData = selRowArr[0];
					var id = rowData[$datagridEl.datagrid('options').idField];
					var rowIndex = $datagridEl.datagrid('getRowIndex', id);
					if(btnCfg.preProcessFn instanceof Function)
					{
						if(btnCfg.preProcessFn(rowIndex, rowData) == false)
							return false;
					}
					
					var url = $btn.data('url');
					var paramCfg = {};
					if(btnCfg.extraParam)
					{
						if(btnCfg.extraParam instanceof Function)
							paramCfg = btnCfg.extraParam(rowIndex, rowData);
						else
							paramCfg = btnCfg.extraParam;
					}
					paramCfg[gridIdParamName] = id;
					
					url = SplicingUrlAndParams(url, paramCfg);
					
					$targetEl.window({href : url});
					if(callbackFn instanceof Function)
					{
						$targetEl.window
						({
							onLoad : function(){callbackFn($targetEl);}
						});
					}
					$targetEl.window('open');
				}
			}});
		}
		else if(actionType == BtnCfg.ACTION_TYPE.CLOSE_WINDOW)		//关闭窗口
		{
			var $targetEl = btnCfg.$targetEl;
			if(callbackFn instanceof Function)
			{
				$targetEl.window
				({
					onClose : function(){callbackFn($targetEl);}
				});
			}
			$btn.linkbutton({'onClick': function()
			{
				$targetEl.window('close');
			}});
		}
		else if(actionType == BtnCfg.ACTION_TYPE.SUBMIT_FORM)		//提交表单
		{
			var $targetEl = btnCfg.$targetEl;
			$btn.linkbutton({'onClick': function()
			{
				if($btn.attr('disabled') == 'disabled')
					return;
				if(btnCfg.preProcessFn instanceof Function)
				{
					if(btnCfg.preProcessFn() == false)
						return false;
				}
				$targetEl.form('submit');
				if(callbackFn instanceof Function)
					callbackFn();
			}});
		}
		else if(actionType == BtnCfg.ACTION_TYPE.RESET_FORM)		//重置表单
		{
			var $targetEl = btnCfg.$targetEl;
			$btn.linkbutton({'onClick': function()
			{
				$targetEl[0].reset();
				if(callbackFn instanceof Function)
					callbackFn($targetEl);
			}});
		}
		else if(actionType == BtnCfg.ACTION_TYPE.DELETE_GRID)		//删除grid记录
		{
			$btn.data('url', btnCfg.url);
			var $targetEl = btnCfg.$targetEl;
			var confirmMsg = btnCfg.confirmMsg;
			var msg = btnCfg.confirmMsg ? btnCfg.confirmMsg : '确定删除选择的记录吗？';
			$btn.linkbutton({'onClick': function()
			{
				if($targetEl == null || $targetEl.length ==0)
					return false;
				
				var selRowArr = $targetEl.datagrid('getSelections');
				if (selRowArr.length == 0)
				{
					$.messager.alert('提示', '请选择需要删除的行。', 'info');
					return false;
				}
				
				$.messager.confirm('提示', msg, function(r)
				{
					if(!r)
						return false;
						
					if(btnCfg.preProcessFn instanceof Function)
					{
						if(btnCfg.preProcessFn() == false)
							return false;
					}
					
					var paramObj = {};
					if(btnCfg.extraParam)
					{
						if(btnCfg.extraParam instanceof Function)
							paramObj = btnCfg.extraParam(selRowArr);
						else
							paramObj = btnCfg.extraParam;
					}
					
					var gridIdFieldName = $targetEl.datagrid('options').idField;
					var selIdArr = [];
					$.each(selRowArr, function(i, rowData)
					{
						selIdArr.push(rowData[gridIdFieldName]);
					});
					
					var gridIdParamName = btnCfg.gridIdParamName || 'ids';
					paramObj[gridIdParamName] = selIdArr.join(',');
					
					$.ajax
					({
						url : $btn.data('url'),
						data : paramObj,
						success : function(data, textStatus, jqXHR)
						{
							UIutil.alertRespJsonMsg(data);
							
							if(data.flag)
							{
								$.each(selIdArr, function(i, rowId)
								{
									var rowIndex = $targetEl.datagrid('getRowIndex', rowId);
									$targetEl.datagrid('deleteRow', rowIndex);				//删除勾选的行
								});
								
								if(callbackFn instanceof Function)
									callbackFn(data, selIdArr);
							}
						},
						error:function (XMLHttpRequest, textStatus, e)
						{
							$.messager.alert('错误','删除失败<br>' + e,'error');
						}
					});
				});
			}});
		}
		else if(actionType == BtnCfg.ACTION_TYPE.SAVE_GRID)  		//保存grid操作
		{
			$btn.data('url', btnCfg.url);
			var $targetEl = btnCfg.$targetEl;
			$btn.linkbutton({'onClick': function(e, args){saveGrid($btn.data('url'), $targetEl, callbackFn);}});
		}
		else if(actionType == BtnCfg.ACTION_TYPE.CANCEL_SAVE_GRID)  //取消保存grid操作
		{
			var $targetEl = btnCfg.$targetEl;
			$btn.linkbutton({'onClick': function(e, args)
			{
				$targetEl.datagrid('rejectChanges');
				$targetEl.datagrid('unselectAll');
			}});
		}
		else if(actionType == BtnCfg.ACTION_TYPE.CUSTOM_FN)		//自定义function
		{
			$btn.linkbutton({'onClick': function(e, args)
			{
				if($(this).attr('disabled') == 'disabled')
					return;
				btnCfg.customFn(e, args);
			}});
		}
		else if(actionType == BtnCfg.ACTION_TYPE.EDIT_DATAGRID_ROW)		//编辑datagrid行
		{
			var $targetEl = btnCfg.$targetEl;
			$btn.linkbutton({'onClick': function()
			{
				var selRows = $targetEl.datagrid('getSelections');
				if(selRows.length == 0)
				{
					$.messager.alert('提示','请选择一条要编辑的记录。', 'info');
					return;
				}
				$targetEl.datagrid('endEdit', $targetEl.data('lastRowIndex'));
				
				var rowIndex = $targetEl.datagrid('getRowIndex',selRows[0]);
				$targetEl.datagrid('beginEdit', rowIndex);
				$targetEl.data('lastRowIndex', rowIndex);
				if(callbackFn instanceof Function)
					callbackFn($targetEl);
			}});
		}
	});
}

/**
 * 注册页面表单事件（包括验证、提交事件）
 * @param {} formCfgArr
 */
function regPageFormEvent(formCfgArr)
{
	$(formCfgArr).each(function(i, formCfg)
	{
		var $form = formCfg.$formEl;
		initFormItemValidateEvent($form);		//注册表单元素的验证事件
		
		var actionType = formCfg.actionType;
		if(actionType == FormCfg.ACTION_TYPE.LOAD_GRID)		//读取GRID
		{
			var $targetEl = formCfg.$targetEl;
			var preFn = formCfg.preProcessFn;
			$form.form					//注册表单的验证事件及dataGrid读取方法
			({
				onSubmit : function()
				{
					if(preFn instanceof Function)
					{
						if(preFn() == false)
							return false;
					}
					if(validateForm($form))
					{
						var action = $(this).attr("action");
						var postData = buildPostParamsByFormItems($form);
						
						var extraParam = formCfg.extraParam;
						if(extraParam)
						{
							if(extraParam instanceof Function)
								extraParam = extraParam();
							
							for(var key in extraParam)
							{
								if(! postData[key])
									postData[key] = extraParam[key];
							}
						}
						
	   					loadGridDataByParam($targetEl, action, postData);
					}
					return false;
				}
			});
		}
		else if(actionType == FormCfg.ACTION_TYPE.RELOAD_GRID)		//reload datagrid
		{
			var $targetEl = formCfg.$targetEl;
			var callbackFn = formCfg.callbackFn;
				
			var fn = function(respJson)
			{
				if($targetEl != null && $targetEl.length > 0)
				{
					if(callbackFn instanceof Function)
					{
						$targetEl.datagrid				//调用初始化方法会重新请求一遍数据，不需要再显式调用reload。
						({
							onLoadSuccess : function(data){callbackFn(respJson);}
						});
					}
					else
						$targetEl.datagrid('reload');
				}
			};
			regFormSubmitEvent($form, fn);
		}
		else if(actionType == FormCfg.ACTION_TYPE.CLOSE_WINDOW)
		{
			var $targetEl = formCfg.$targetEl;
			var callbackFn = formCfg.callbackFn;
			
			var fn = function(respJson)
			{
				if(callbackFn instanceof Function)
				{
					$targetEl.window
					({
						onClose : function(){callbackFn(respJson);}
					});
				}
				if($targetEl != null && $targetEl.length > 0)
					$targetEl.window('close');
			};
			regFormSubmitEvent($form, fn);
		}
		else if(actionType == FormCfg.ACTION_TYPE.REFRESH_PANEL)		//刷新panel
		{
			var $targetEl = formCfg.$targetEl;
				
			var fn = function($form)
			{
				if($targetEl != null && $targetEl.length > 0)
					$targetEl.panel('refresh');
			};
			regFormSubmitEvent($form, fn);
		}
		else if(actionType == FormCfg.ACTION_TYPE.CUSTOM_FN)		//自定义事件
		{
			$form.submit(function(e)
			{
				formCfg.customFn(e, $(this));
				e.preventDefault();
			});
		}
		else
		{
			var callbackFn = formCfg.callbackFn;
			regFormSubmitEvent($form, callbackFn);
		}
	});
}

/**
 * 注册表单的提交事件
 * @param $form
 * @param fn 提交成功后的回调函数
 */
function regFormSubmitEvent($form, callbackFn)
{
	$form.form					//注册表单的验证事件及提交方法
	({
		url : $form.attr('action'),
		onSubmit : function()
		{
			if(!validateForm($form))
				return false;
		},
		success : function(data)
		{
			var respJson = $.parseJSON(data);
			UIutil.alertRespJsonMsg(respJson, callbackFn);
		}
	});
}


//对Date的扩展，将 Date 转化为指定格式的String   
//月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符，   
//年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字)   
//例子：   
//(new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423   
//(new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18   
Date.prototype.format = function(fmt) {   
var o = {   
 "M+" : this.getMonth()+1,                 //月份   
 "d+" : this.getDate(),                    //日   
 "h+" : this.getHours(),                   //小时   
 "m+" : this.getMinutes(),                 //分   
 "s+" : this.getSeconds(),                 //秒   
 "q+" : Math.floor((this.getMonth()+3)/3), //季度   
 "S"  : this.getMilliseconds()             //毫秒   
};   
if(/(y+)/.test(fmt))   
 fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));   
for(var k in o)   
 if(new RegExp("("+ k +")").test(fmt))   
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));   
return fmt;   
} 