var ACTION_POSTFIX = "shtml";
var DEFAULT_CONTENT_PATH = "";
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;
	   beginSubmit();
	   form.submit();
	}
}
function submitForm() {
   if(submitting)return false;
   var form = document.forms[0];
   if(form.onsubmit && !form.onsubmit()) {
   	  return;
   }
   submitting = true;
   beginSubmit();
   form.submit();
}
function beginSubmit() {
	document.onclick = disableEvent;
	document.onmousedown = disableEvent;
	document.onkeydown = disableEvent;
	document.onkeyup = disableEvent;
	document.onmouseup = disableEvent;
	document.onactivate = disableEvent; 
	document.onfocusin = disableEvent;
	document.onfocusout = disableEvent;
	var tables = document.getElementsByTagName("table");
	if(tables[0]) {
		for(var i=0; i<tables.length; i++) {
			tables[i].onclick = disableEvent;
		}
	}
	var spans = document.getElementsByTagName("span");
	if(spans[0]) {
		for(var i=0; i<spans.length; i++) {
			spans[i].onclick = disableEvent;
		}
	}
	var inputs = document.getElementsByTagName("input");
	if(inputs[0]) {
		for(var i=0; i<inputs.length; i++) {
			inputs[i].onclick = disableEvent;
		}
	}
	var buttons = document.getElementsByTagName("button");
	if(buttons[0]) {
		for(var i=0; i<buttons.length; i++) {
			buttons[i].onclick = disableEvent;
		}
	}
	window.setTimeout("showSubmitting()", 200);
}
function disableEvent() {
	return false;
}
function showSubmitting() {
	var div = document.createElement("div");
	div.innerHTML = '\u6B63\u5728\u5904\u7406\u4E2D\uFF0C\u8BF7\u7A0D\u5019\uFF0E\uFF0E\uFF0E'; //<img style="border:none" src="' + getContextPath() + '/jeaf/common/img/loading.gif">
	div.style.cssText = "border:1 solid #aaaaaa; background:#ffff66; position:absolute; padding:5px; font-family:\u5B8B\u4F53; font-size:12px";
	var windowWidth = document.body.clientWidth;
	var windowHeight = document.body.clientHeight;
	
	div.style.left = windowWidth - 181 + document.body.scrollLeft;
	div.style.top = 1 + document.body.scrollTop;
	div.style.width = 180;
	div.style.height = 25;
	document.body.appendChild(div);
}
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;
}
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;
}
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 : "\u5185\u5BB9") + "\u4E0D\u80FD\u4E3A\u7A7A\uFF01");
      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 : "\u8F93\u5165\u5185\u5BB9") + "\u4E0D\u80FD\u5305\u542B" + mask + "\u7B49\u5B57\u7B26\uFF01");
         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 : "\u60A8") + "\u8F93\u5165\u7684\u6570\u5B57\u4E0D\u6B63\u786E\uFF01");
      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 : "\u60A8") + "\u8F93\u5165\u7684\u65E5\u671F\u683C\u5F0F\u4E0D\u6B63\u786E\uFF01");
      src.focus();
      src.select();
      return "NaN";
   }
   return value;
}
function logout(url) {
	if(!url) {
		url = location.href;
	}
	else if(url.indexOf("http://")==-1 && url.indexOf("https://")==-1) {
		url = location.protocol + "//" + location.host + url;
	}
   	window.top.location = getContextPath() + "/jeaf/logout." + ACTION_POSTFIX + "?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, param) {
   openurl(contextPath + "/" + applicationName + "/" + formName + "." + ACTION_POSTFIX + "?act=create&workflowId=" + workflowId.split(".")[0] + "&activityId=" + workflowId.split(".")[1], openFeatues, applicationName + formName);
}
function newrecord(applicationName, formName, openFeatues, param) {
   openurl(getContextPath() + "/" + applicationName + "/" + formName + "." + ACTION_POSTFIX + "?act=create" + (param ? "&" + param : ""), openFeatues, applicationName + formName);
}
function openconfig(applicationName, formName, configKey, openFeatues, param) {
   openurl(getContextPath() + "/" + applicationName + "/" + formName + "." + ACTION_POSTFIX + (configKey != "" ? "?configKey=" + configKey : ""), openFeatues, applicationName + formName + configKey);
}
function newconfig(applicationName, formName, configKey, openFeatues, param) {
   openurl(getContextPath() + "/" + applicationName + "/" + formName + "." + ACTION_POSTFIX + "?act=create" +(configKey != "" ? "&configKey=" + configKey : ""), openFeatues, applicationName + formName + configKey);
}
function openrecord(applicationName, formName, id, openFeatues, param) {
   openurl(getContextPath() + "/" + applicationName + "/" + formName + "." + ACTION_POSTFIX + "?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>0 ? "&workItemId=" + workItemId : ""), openFeatues, id);
}
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 = "left=0,top=0,width=" +(sWidth - 8) + ",height=" +(sHeight - 24);
      }
      else if(mode == "center") {
         if(width != null && height != null) {
            openFeatues = "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 += ",";
   }
   var win = window.open(url, (name ? ("" + name).replace(new RegExp("[-./]", "g"), "") : ""), openFeatues + "scrollbars=yes,status=no,resizable=yes,toolbar=no,menubar=no,location=no", false);
   if(fullScreen) win.resizeTo(sWidth, sHeight);
   win.focus();
}
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;
}

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() + "/jeaf/dialog/select." + ACTION_POSTFIX + "?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();
}

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() + "/jeaf/dialog/select." + ACTION_POSTFIX;
   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 openSelectUserDialog(width, height, multiSelect, param, scriptEndSelect, types, key, separator, dialogType, assignOrgId, hideRoot) {
   var left =(screen.width - width)/2;
   var top =(screen.height - height - 16)/2;
   var url = getContextPath() + "/jeaf/usermanage/select." + ACTION_POSTFIX + "?dialogType=" + dialogType;
   url += "&multiSelect=" + multiSelect; 
   url += "&param=" + escape(param); 
   url += (scriptEndSelect && scriptEndSelect!="" ? "&script=" + escape(scriptEndSelect) : ""); 
   url += (types && types!="" ? "&types=" + types : ""); 
   url += (key && key!="" ? "&key=" + escape(key) : ""); 
   url += ("&separator=" + (separator && separator!="" ?  escape(separator) : ",")); 
   url += (assignOrgId && assignOrgId!="" ? "&assignOrgId=" + assignOrgId : ""); 
   url += (hideRoot ? "&hideRoot=true" : ""); 
   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, personTypes, key, separator, assignOrgId, hideRoot) {
   openSelectUserDialog(width, height, multiSelect, param, scriptEndSelect, personTypes, key, separator, "person", assignOrgId, hideRoot);
}
function selectOrg(width, height, multiSelect, param, scriptEndSelect, orgTypes, key, separator, assignOrgId, hideRoot) {
   openSelectUserDialog(width, height, multiSelect, param, scriptEndSelect, orgTypes, key, separator, "org", assignOrgId, hideRoot);
}
function selectRole(width, height, multiSelect, param, scriptEndSelect, key, separator, assignOrgId, hideRoot) {
   openSelectUserDialog(width, height, multiSelect, param, scriptEndSelect, "", key, separator, "role", assignOrgId, hideRoot);
}
function selectPersonByRole(width, height, multiSelect, param, scriptEndSelect, key, separator, assignOrgId, hideRoot) {
   openSelectUserDialog(width, height, multiSelect, param, scriptEndSelect, "", key, separator, "roleMember", assignOrgId, hideRoot);
}
function openApplicationConfig() {
   var url = "/jeai/eai/openConfigure." + ACTION_POSTFIX;
   url += "?set=" + getContextPath().substring(1);
   url += "&application=" + document.getElementsByName("prefix")[0].value;
   url += "&from=" + window.location.protocol + "//" + window.location.host + getContextPath() + "/jeaf/application/openapplication." + ACTION_POSTFIX + "?prefix=" + document.getElementsByName("prefix")[0].value + "&view=" + document.getElementsByName("view")[0].value;
   top.location = url;
}
function getContextPath() {
	if(event && event.srcElement) { 
		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") ? document.getElementById("contextPath").value : DEFAULT_CONTENT_PATH;
}
function getMoneyCapital(money) { 
	money = new Number(money);
	if(isNaN(money) || money==0 || money>999999999999.99) {
        return "";
    }
    var nums = "\u96F6,\u58F9,\u8D30,\u53C1,\u8086,\u4F0D,\u9646,\u67D2,\u634C,\u7396,\u62FE,\u4F70,\u4EDF,\u842C,\u4EBF".split(",");
    var capital = "\u5143";
    money = Math.round(money*100);
    if(money % 100 ==0) {
        capital += "\u6574";
    }
    else {
        capital += nums[Math.floor(money % 100 / 10)] + "\u89D2";
        capital += nums[money % 10] + "\u5206";
    }
    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." + ACTION_POSTFIX + "?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"), ""));
}
function adjustPriority(applicationName, viewName, title, width, height) { 
   var left =(screen.width - width)/2;
   var top =(screen.height - height - 16)/2;
   var url = getContextPath() + "/jeaf/dialog/adjustPriority." + ACTION_POSTFIX;
   url += "?prefix=" + applicationName; 
   url += "&viewName=" + viewName; 
   url += "&title=" + escape(title); 
   window.open(url, "adjustPriority", "left=" + left + "px,top=" + top + "px,width=" + width + "px,height=" + height + "px,status=0,help=0,scrollbars=1,resizable=1", true).focus();
}
function getPropertyValue(properties, propertyName, nextPropertyName) {
	var index = properties.indexOf(propertyName + "=");
	if(index==-1) {
		return "";
	}
	index += propertyName.length + 1;
	var indexNext = -1;
	if(nextPropertyName && nextPropertyName!="") {
		indexNext = properties.indexOf("&" + nextPropertyName + "=", index);
		if(indexNext==-1) {
			indexNext = properties.indexOf("&", index);
		}
	}
	return (indexNext==-1 ? properties.substring(index) : properties.substring(index, indexNext));
}
function reloadValidateCodeImage(validateCodeImageId) {
	if(!validateCodeImageId) {
		validateCodeImageId = "validateCodeImage";
	}
	var src = document.getElementById(validateCodeImageId).src;
	var index = src.lastIndexOf("?");
	if(index!=-1) {
		src = src.substring(0, index);
	}
	document.getElementById(validateCodeImageId).src = src + "?reload=true&" + generateSeq();
}