//执行操作
var ACTION_POSTFIX = "shtml";
var submitting = false;
function doAction(actionName, param) {
   if(!submitting) {
	   if(!param) param = "";
	   var form = document.forms[0];
	   if(form.onsubmit && !form.onsubmit())return;
	   var action = form.action;
	   var index = action.lastIndexOf("." + ACTION_POSTFIX);
	   if(index == - 1) {
	   		index = action.lastIndexOf(".do");
	   		if(index == - 1)return;
	   }
	   index = action.lastIndexOf("/", index);
	   if(index == - 1)return;
	   form.action = action.substring(0, index + 1) + actionName + "." + ACTION_POSTFIX +(param == "" ? "" : "?" + param);
	   submitting = true;
	   form.submit();
   }
}
function getCurrentAction() {
	var action = window.location.pathname;
	var endIndex = action.lastIndexOf("." + ACTION_POSTFIX);
	if(endIndex == - 1) {
		endIndex = action.lastIndexOf(".do");
	   	if(endIndex == - 1)return "";
	}
	var beginIndex = action.lastIndexOf("/", endIndex);
	return action.substring(beginIndex + 1, endIndex);
}
function formOnSubmit() {
	return true;
}
function escapeUTF(src) {
   var ret = "";
   for(i = 0; i < src.length; i++) {
      var ch = src.charCodeAt(i);
      if(ch <= 0x7F) {
         ret += escape(src.charAt(i));
      }
      else if(ch <= 0x07FF) {
         ret += '%' +((ch >> 6) | 0xC0).toString(16) + '%' +((ch & 0x3F) | 0x80).toString(16);
      }
      else if(ch >= 0x0800) {
         ret += '%' +((ch >> 12) | 0xE0).toString(16) + '%' +(((ch >> 6) & 0x3F) | 0x80).toString(16) + '%' +((ch & 0x3F) | 0x80).toString(16);
      }
   }
   return ret;
}


//解析对象
function parseJSObject(text) {
	if(text.substring(0, "LISTBEGIN[".length)=="LISTBEGIN[") { //列表
		var list = new Array();
		parseJSObjectList(text, 0, list);
		return list;
	}
	else {
		var	object = new Object();
		parseSingleJSObject(text, 0, object);
		return object;
	}
}
//解析单个对象,返回对象的结束位置
function parseSingleJSObject(text, beginIndex, object) {
	var objectEnd = -1, listEnd = -1, propertyEnd = -1, objValueEnd = -1;
	while(objectEnd==-1 && (propertyEnd=text.indexOf("=", beginIndex))!=-1) {
		//获取属性名称
		var propertyName = text.substring(beginIndex, propertyEnd);
		object.attributes = object.attributes ? object.attributes + "," + propertyName : propertyName;
		beginIndex = propertyEnd + 1;
		var propertyValue = null;
		if(text.substr(beginIndex, "LISTBEGIN[".length)=="LISTBEGIN[") { //列表
			propertyValue = new Array();
			beginIndex = parseJSObjectList(text, beginIndex, propertyValue);
		}
		else if(text.substr(beginIndex, "OBJECTBEGIN[".length)=="OBJECTBEGIN[") { //对象
			propertyValue = new Object();
			beginIndex = parseSingleJSObject(text, beginIndex + "OBJECTBEGIN[".length, propertyValue);
		}
		objectEnd = text.indexOf("?", beginIndex);
		if(objectEnd==-1) {
			objectEnd = text.length;
		}
		listEnd = text.indexOf("]LISTEND", beginIndex);
		if(listEnd==-1) {
			listEnd = text.length;
		}
		objValueEnd = text.indexOf("]OBJECTEND", beginIndex);
		if(objValueEnd==-1) {
			objValueEnd = text.length;
		}
		objectEnd = Math.min(Math.min(listEnd, objectEnd), objValueEnd);
		propertyEnd = text.indexOf("&", beginIndex);
		if(propertyEnd==-1 || propertyEnd>objectEnd) {
			propertyEnd = objectEnd;
		}
		else {
			objectEnd = -1;
		}
		//获取属性值
		if(propertyValue==null) {
			propertyValue = text.substring(beginIndex, propertyEnd);
		}
		if(propertyValue!="") {
			try {
				eval("object." + propertyName.replace("function", "FUNCTION") + "=propertyValue");
			}
			catch(e) {
			}
		}
		beginIndex = propertyEnd + 1;
	}
	if(objectEnd==-1) {
	    return text.length();
	}
	else if(objectEnd==objValueEnd) {
	    return objectEnd + "]OBJECTEND".length;
	}
	else if(objectEnd==listEnd) {
	    return objectEnd + "]LISTEND".length;
	}
	else {
	    return objectEnd + 1;
	}
}
//解析列表,返回结束位置
function parseJSObjectList(text, beginIndex, list) {
	var index = 0;
	beginIndex += "LISTBEGIN[".length;
	while(text.substring(beginIndex - "]LISTEND".length, beginIndex)!="]LISTEND") {
		var object = new Object();
		beginIndex = parseSingleJSObject(text, beginIndex, object);
		list[index++] = object;
	}
	return beginIndex;
}
//转换对象为java对象
function toJavaObject(object) {
	if(object[0]) { //数组
		return toJavaObjectList(object);
	}
	var text = "";
	var attributes = object.attributes.split(",");
	for(var i = 0; i < attributes.length; i++) {
		var value = eval("object." + attributes[i].replace("function", "FUNCTION"));
		if(value) {
			if(value[0]) {
				text += (text == "" ? "" : "&") + attributes[i] + "=" + toJavaObjectList(value);
			}
			else if(value.className) {
				text += (text == "" ? "" : "&") + attributes[i] + "=" + "OBJECTBEGIN[" + toJavaObject(value) + "]OBJECTEND";
			}
			else {
				text += (text == "" ? "" : "&") + attributes[i] + "=" + value;
			}
		}
	}
	return text;
}
//转换对象为java对象
function toJavaObjectList(array) {
	var text = "";
	try {
		for(var i=0; i<array.length; i++) {
			text += (text == "" ? "" : "?") + toJavaObject(array[i]);
		}
	}
	catch(e) {

	}
	return "LISTBEGIN[" + text + "]LISTEND";
}
//校验是否必须输入
function validateFieldRequired(src, required, fieldName) {
   if(src.value == "" && required) {
      alert((fieldName ? fieldName : "内容") + "不能为空！");
      src.focus();
      return "NaN";
   }
   return src.value;
}
//校验字符串
function validateStringField(src, mask, required, fieldName) {
   var value = validateFieldRequired(src, required, fieldName);
   if(value == "" || value == "NaN") {
      return value;
   }
   if(mask && mask != "") {
      var newMask = mask.replace(new RegExp("\\x27", "g"), "\\x27").replace(new RegExp(",", "g"), "");
      if(value.search(new RegExp("[" + newMask + "]")) != - 1) {
         alert((fieldName ? fieldName : "输入内容") + "不能包含" + mask + "等字符！");
         src.focus();
         src.select();
         return "NaN";
      }
   }
   return value;
}
//校验数字
function validateNumberField(src, required, fieldName) {
   var value = validateFieldRequired(src, required, fieldName);
   if(value == "" || value == "NaN") {
      return value;
   }
   var value = new Number(value);
   if(isNaN(value)) {
      alert((fieldName ? fieldName : "您") + "输入的数字不正确！");
      src.focus();
      src.select();
      return "NaN";
   }
   return value;
}
//校验日期
function validateDateField(src, required, fieldName) {
   var value = validateFieldRequired(src, required, fieldName);
   if(value == "" || value == "NaN") {
      return value;
   }
   var dateValue = new Date(value.replace(new RegExp("-", "g"), "/"));
   if(isNaN(dateValue)) {
      alert((fieldName ? fieldName : "您") + "输入的日期格式不正确！");
      src.focus();
      src.select();
      return "NaN";
   }
   return value;
}
//注销
function logout(url) {
	if(!url) {
		url = location.href;
	}
   	window.top.location = getContextPath() + "/platform/logout.do?url=" + url;
}
//新建流程,选择流程
function createWorkflowInstnace(applicationName, formName, workflowEntriesAsText, openFeatues) {
   var workflowEntriyList = parseJSObject(workflowEntriesAsText);
   if(workflowEntriyList.length == 1 && workflowEntriyList[0].activityEntries.length==1) {
      newWorkflowInstnace(getContextPath(), applicationName, formName, workflowEntriyList[0].workflowId + "." + workflowEntriyList[0].activityEntries[0].id, openFeatues);
      return;
   }
   var menuDefinition = new Array();
   var j = 0;
   for(var i = 0; i < workflowEntriyList.length; i++) {
      var menuItem = new Object();
	  menuDefinition[j++] = menuItem;
	  menuItem.title = workflowEntriyList[i].workflowName;
      menuItem.id = workflowEntriyList[i].workflowId;
      if(workflowEntriyList[i].activityEntries.length==1) {
          menuItem.id += "." + workflowEntriyList[i].activityEntries[0].id;
	  }
	  else {
	      for(var k=0; k<workflowEntriyList[i].activityEntries.length; k++) {
	      	 var menuItem = new Object();
	      	 menuDefinition[j++] = menuItem;
    	     menuItem.subMenu = true;
        	 menuItem.id = workflowEntriyList[i].activityEntries[k].id;
	         menuItem.title = workflowEntriyList[i].activityEntries[k].name;
    	  }
      }
   }
   showMenu(menuDefinition, "newWorkflowInstnace(\"" + getContextPath() + "\", \"" + applicationName + "\", \"" + formName + "\", \"{selectedId}\", \"" + openFeatues + "\")", event.srcElement);
   return;
}
//创建流程实例
function newWorkflowInstnace(contextPath, applicationName, formName, workflowId, openFeatues) {
   openurl(contextPath + "/" + applicationName + "/" + formName + ".do?act=create&workflowId=" + workflowId.split(".")[0] + "&activityId=" + workflowId.split(".")[1], openFeatues, applicationName + formName);
}
//创建新记录
function newrecord(applicationName, formName, openFeatues, param) {
   openurl(getContextPath() + "/" + applicationName + "/" + formName + ".do?act=create" + (param ? "&" + param : ""), openFeatues, applicationName + formName);
}
//配置(单一记录)
function openconfig(applicationName, formName, configKey, openFeatues) {
   openurl(getContextPath() + "/" + applicationName + "/" + formName + ".do" +(configKey != "" ? "?configKey=" + configKey : ""), openFeatues, applicationName + formName + configKey);
}
//配置
function newconfig(applicationName, formName, configKey, openFeatues) {
   openurl(getContextPath() + "/" + applicationName + "/" + formName + ".do?act=create" +(configKey != "" ? "&configKey=" + configKey : ""), openFeatues, applicationName + formName + configKey);
}
//打开记录
function openrecord(applicationName, formName, id, openFeatues) {
   openurl(getContextPath() + "/" + applicationName + "/" + formName + ".do?act=open&id=" + id, openFeatues, id);
}
//打开记录
function editrecord(applicationName, formName, id, openFeatues, workItemId, param) {
   openurl(getContextPath() + "/" + applicationName + "/" + formName + "." + ACTION_POSTFIX + "?act=edit" + (param ? "&" + param : "") +  "&id=" + id + (workItemId ? "&workItemId=" + workItemId : ""), openFeatues, id);
}
//打开URL
function openurl(url, openFeatues, name) {
   var fullScreen = false;
   if(!openFeatues) {
      openFeatues = "";
   }
   else {
      var parameters = getParmemters(openFeatues);
      var mode = getParameter(parameters, "mode");
      var width = getParameter(parameters, "width");
      var height = getParameter(parameters, "height");
      var sWidth = screen.availWidth;
      var sHeight = screen.availHeight;
      if(mode == "fullscreen") {
         fullScreen = true;
         openFeatues = openFeatues.replace("mode=fullscreen", "left=0,top=0,width=" +(sWidth - 8) + ",height=" +(sHeight - 24));
      }
      else if(mode == "center") {
         if(width != null && height != null) {
            openFeatues = openFeatues.replace("mode=center", "left=" +((sWidth - width) /2)+",top="+((sHeight-height)/ 2) + ",width=" + width + ",height=" + height);
         }
      }
      else {
         openFeatues = width == null ? "" : "width=" + width;
         openFeatues += height == null ? "" :(openFeatues == "" ? "" : ",") + "height=" + height;
      }
      openFeatues = "," + openFeatues;
   }
   var win = window.open(url, (name ? ("" + name).replace(new RegExp("[-./]", "g"), "") : ""), "scrollbars=yes,status=no,resizable=yes,toolbar=no,menubar=no,location=no" + openFeatues, false);
   if(fullScreen) win.resizeTo(sWidth, sHeight);
   win.focus();
   //return win;
}

function getParmemters(src) {
   var temp = src.split(",");
   var len = temp.length;
   var parameters = new Array(len);
   for(var i = 0; i < len; i++) {
      parameters[i] = temp[i].split("=");
   }
   return parameters;
}

function getParameter(parameters, name) {
   var i, len = parameters.length;
   for(i = 0; i < len && parameters[i][0] != name; i++);
   if(i == len)
   return null;
   return parameters[i][1];
}
//获取对象的绝对位置
function getAbsolutePosition(obj) {
   var pos = new Object();
   pos.left = 0;
   pos.top = 0;
   while(obj.tagName != "BODY") {
      pos.left += obj.offsetLeft;
      pos.top += obj.offsetTop;
      obj = obj.offsetParent;
   }
   return pos;
}
function getElement(parentElement, tagName, id) { //获取对象
   var elements = parentElement.getElementsByTagName(tagName);
   for(var i = 0; elements && i < elements.length; i++) {
      if(elements[i].id == id || elements[i].name == id) {
         return elements[i];
      }
   }
   return null;
}
/**
 * 打开列表选择对话框
 * @param title:对话框标题
 * @param type:当type=list时,数据源为source域;当type=config时,数据源为source指定得配置表中得指定字段
 * @param source:当type=list时为数据来源字段,格式为:标题1|值1,标题n|值n
 *				 当type=config时,格式为:配置表单名,字段名
 * 注:其他参数同openSelectDialog
 */
function openListDialog(title, type, source, width, height, multiSelect, param, scriptEndSelect, key) {
   var left =(screen.width - width)/2;
   var top =(screen.height - height - 16)/2;
   window.open(getContextPath() + "/platform/dialog/select.do?title=" + escape(title) + "&type=" + type + "&source=" + source + "&multiSelect=" + multiSelect + "&param=" + escape(param) +(scriptEndSelect ? "&script=" + escape(scriptEndSelect) : "") +(key ? "&key=" + escape(key) : ""), "selectDialog", "left=" + left + "px,top=" + top + "px,width=" + width + "px,height=" + height + "px,status=0,help=0,scrollbars=1,resizable=1", true).focus();
}
/**
 * 打开对话框
 * @param applicationName:系统名称
 * @param dialogName:对话框名称,在select-cfg.xml中定义
 * @param width:对话框宽度
 * @param height:对话框高度
 * @param multiSelect:是否多选
 * @param param:单选时格式为:表单字段名1{数据库字段1},...表单字段名n{数据库字段n}
 * 				多选时格式为:表单关键字字段名{数据库关键字字段},表单字段名1{数据库字段1|标题1|宽度1|默认值1},...表单字段名n{数据库字段n|标题n|宽度n|默认值n},默认值在数据库字段为空时有效
 * 						注:关键字作为判断是否重复的依据,多选数据间用逗号分隔,要求所有字段一一对应,数据库字段为CATEGORY表示当前分类
 * @param scriptEndSelect:完成选择后执行的脚本
 * @param defaultCategory:默认分类
 * @param key:快速过滤关键字
 * @param separator:分隔符
 */
function openSelectDialog(applicationName, dialogName, width, height, multiSelect, param, scriptEndSelect, defaultCategory, key, separator) {
   var left =(screen.width - width)/2;
   var top =(screen.height - height - 16)/2;
   var url = getContextPath() + "/platform/dialog/select.do";
   url += "?prefix=" + applicationName; //系统名称
   url += "&dialog=" + dialogName; //对话框名称
   url += "&multiSelect=" + multiSelect; //是否多选
   url += "&param=" + escape(param); //参数
   url += (scriptEndSelect && scriptEndSelect!="" ? "&script=" + escape(scriptEndSelect) : ""); //执行的脚本
   url += (defaultCategory && defaultCategory!="" ? "&viewPackage.categories=" + escape(defaultCategory) : ""); //默认分类
   url += (key && key!="" ? "&key=" + escape(key) : ""); //过虑关键字
   url += (separator && separator!="" ? "&separator=" + escape(separator) : ""); //分隔符
   window.open(url, "selectDialog", "left=" + left + "px,top=" + top + "px,width=" + width + "px,height=" + height + "px,status=0,help=0,scrollbars=1,resizable=1", true).focus();
}
//选择用户
function selectPerson(width, height, multiSelect, param, scriptEndSelect, defaultCategory, key, separator) {
   openSelectDialog('platform/usermanage', 'person', width, height, multiSelect, param, scriptEndSelect, defaultCategory, key, separator);
}
//选择用户,按角色分类
function selectPersonByRole(width, height, multiSelect, param, scriptEndSelect, defaultCategory, key, separator) {
   openSelectDialog('platform/usermanage', 'personByRole', width, height, multiSelect, param, scriptEndSelect, defaultCategory, key, separator);
}
//选择部门
function selectDepartment(width, height, multiSelect, param, scriptEndSelect, defaultCategory, key, separator) {
   openSelectDialog('platform/usermanage', 'selectDepartment', width, height, multiSelect, param, scriptEndSelect, defaultCategory, key, separator);
}
//选择角色
function selectRole(width, height, multiSelect, param, scriptEndSelect, key, separator) {
   openSelectDialog('platform/usermanage', 'role', width, height, multiSelect, param, scriptEndSelect, key, separator);
}
//系统配置,TODO,cas改为从配置中获取
function openApplicationConfig() {
   var url = "/cas/cas/eai/openConfigure.do";
   url += "?set=" + getContextPath().substring(1);
   url += "&application=" + document.getElementsByName("prefix")[0].value;
   url += "&from=" + window.location.protocol + "//" + window.location.host + getContextPath() + "/platform/application/openapplication.do?prefix=" + document.getElementsByName("prefix")[0].value + "&view=" + document.getElementsByName("view")[0].value;
   top.location = url;
}
//获取应用路径
function getContextPath() {
		if(event && event.srcElement) { //查找最近的contextPath域
		var obj = event.srcElement;
		while(obj.tagName!="BODY") {
			var input = getElement(obj, "input", "contextPath");
			if(input!=null) {
				return input.value;
			}
			obj = obj.offsetParent;
		}
	}
	return document.getElementById("contextPath").value;
}
//提交
function submit() {
   if(submitting)return false;
   var form = document.forms[0];
   if(form.onsubmit) {
   	  if(form.onsubmit()) {
   	     submitting = true;
   	     form.submit();
   	  }
   }
   else {
      submitting = true;
      form.submit();
   }
}
function getMoneyCapital(money) { //获取大写的金额
	money = new Number(money);
	if(isNaN(money) || money==0 || money>999999999999.99) {
        return "";
    }
    var nums = "零,壹,贰,叁,肆,伍,陆,柒,捌,玖,拾,佰,仟,萬,亿".split(",");
    var capital = "元";
    money = Math.round(money*100);
    if(money % 100 ==0) {
        capital += "整";
    }
    else {
        capital += nums[Math.floor(money % 100 / 10)] + "角";
        capital += nums[money % 10] + "分";
    }
    money = Math.floor(money/100);
    var i = 0;
    do {
        if(i%4==0) {
        	capital = (i==0 ? "" : nums[12 + i/4]) + capital;
        }
        else {
        	capital = nums[9 + i%4] + capital;
        }
        i++;
        capital = '<font style="text-decoration:underline">&nbsp;' + nums[money%10] + '&nbsp;</font>' + capital;
        money = Math.floor(money/10);
    }while(money>0);
    return capital;
}
function trim(str) {
	return str.replace(/(^\s*)|(\s*$)/g,'');
}
function ltrim(str) {
	return str.replace(/^\s*/g,'');
}
function rtrim(str) {
	return str.replace(/\s*$/g,'');
}
function generateSeq() {
	var now = new Date();
	return "seq=" + (now.getSeconds()*1000 + now.getMilliseconds());
}
function sendMail(mailAddress, name) {
	openurl(getContextPath() + "/webmail/mail.do?to=" + escape(name ? "\"" + name + "\" <" + mailAddress + ">" : mailAddress), "width=760,height=520", "mail");
}
function getTimeValue(fieldName) {
	var time = document.getElementsByName(fieldName)[0].value;
	if(time=="") {
		return "";
	}
	return new Date(time.replace(new RegExp("-", "g"), "/").replace(new RegExp("\\x2E0", "g"), ""));
}