
var _sRoot = "/";		//设置根url
var _iLocked = 0;
var None = 0;
var sPsrviewVer = "1.0.0.7";								// psrview 最新的版本。必须与 psrview 版本同步。

function document.oncontextmenu()
{
	var s = event.srcElement.tagName;
	// For Selection Ranges, s will be UNDEFINED
	if (s && s != "INPUT" && s != "TEXTAREA" || event.srcElement.disabled || document.selection.createRange().text.length == 0)
	{
		event.returnValue = false;
	}
}

function document.onselectstart()
{
	var s = event.srcElement.tagName;
	if (s != "INPUT" && s != "TEXTAREA") event.returnValue = false;
}

function document.ondragstart()
{
	event.returnValue = false;
}

// 功能：以 open 方式打开全屏窗口
function openMyFullWin(sPath, sName)
{
	var iX = window.screen.width - 10;
	var iY = window.screen.height - 60;
	if (!sName) sName = "";

	try
	{
		window.open(sPath, sName, "left=0,top=0,width=" + iX + ",height=" + iY + ",status=1,resizable=1");
	}
	catch(e) {}
}

// 功能：以 open 方式打开窗口
function openMyWin(sPath, sName, iX, iY)
{
	openStdWin(sPath, sName, iX, iY);
}

function openStdWin(sPath, sName, iX, iY)
{
	if (!iX) iX = 800;
	if (!iY) iY = 552;
	if (!sName) sName = "";

	var iL, iT;
	iL = (window.screen.width - iX)/2;
	iT = (window.screen.height - iY - 80)/2;
	
	if (iL < 0 ) iL = 0 ;	
	if (iT < 0 ) iT = 0 ;
	
	try
	{
		window.open(sPath, sName, "left=" + iL + ",top=" + iT + ",width=" + iX + ",height=" + iY + ",status=1,resizable=1");
	}
	catch(e) 
	{
	}
}

// 功能：以 showModalDialog 方式打开窗口
// 参数：sPath		-- 文件路径，必须包含完整的文件路径
//		 sParams	-- 后缀参数，格式如：paramname1=param1&paramname2=param2&...
function openMyDlg(sTitle, sPath, sParams, oArgs, iX, iY)
{

	if (!iX) iX = 500;
	if (!iY) iY = 340;

	var sURL = "/FrameTemp0.aspx";
		sURL += "?title=" + escape(sTitle);
		sURL += "&height=";
		sURL += "&filename=" + escape(sPath);
		sURL += "&param=" + escape(sParams);

	return window.showModalDialog(sURL, oArgs, "dialogWidth:" + iX + "px;dialogHeight:" + iY + "px; status:0; help:0; resizable:0; center:1;");
}

function openStdDlg(sPath, oArgs, iX, iY)
{
	return window.showModalDialog(sPath, oArgs, "dialogWidth:" + iX + "px;dialogHeight:" + iY + "px;help:0;status:0;scroll:0;center:1");
}

function openFrmObj( sUrl, sName,  iType )
{
	var url = _sRoot + getObjUrl(iType) + sUrl;		//根据iType从注册信息中找出编辑页面的url
	switch ( Number(iType) )
	{
		case None:
			// don't open anything
			break;
		default:
			openStdWin( url, sName);
	}
}

function openObj(iType, sId)
{
	var sUrl = "";
	if (sId)
	{
		sUrl += "?id=" + sId;
	}
	openFrmObj( sUrl, buildWinName(sId),  iType );
}

function openObjEx( iType, iParentType, sParentId, sParams )
{
	var url = "";
	if (sParentId)
	{
		url += "?pId=" + sParentId + "&pType=" + iParentType;
	}
	if ( typeof(sParams) != "undefined" )
	{
		url += sParams;
	}
	openFrmObj( url, buildWinName(),  iType );
}

function getObjUrl(i)
{
	switch ( Number(i) )
	{
		case None:
		case AppLicense:					return "";  // does not open a form
	}
	alert(i + " - 没有注册该对象代码！");          //^^
	return "";
}

function openPopup()
{
	return window.createPopup();
}

function buildWinName(s)
{
	if (s) return s.replace(/[-\{\}]/g, "");
	var d = new Date();
	return d.getTime();
}

var ERROR_STOP = 0;
var ERROR_NONE = 1;
var ERROR_CONTINUE = 2;
function handleXMLErr(xml, bContinue)
{
	if (bContinue == null) bContinue = false;
	if (xml.parseError.errorCode != 0)
	{
		//alert("XML Parser Error: " + xml.parseError.reason);
		alert("XML解析错误: " + xml.parseError.reason);
		if (!bContinue)
		{
			return ERROR_STOP;
		}
		else
		{
			return ERROR_CONTINUE;
		}
	}
	var node = xml.selectSingleNode("/error");
	if (node)
	{
		if (!bContinue)
		{
			//openStdDlg("/_common/error/dlg_error.aspx?hresult=" + node.selectSingleNode("number").text, null, 400, 200);
			alert("发生运行时错误！");
			return ERROR_STOP;
		}
		else
		{
			return ERROR_CONTINUE;
		}
	}
	return ERROR_NONE;
}

//自动刷新页面
//周彤	2004-12-07
function autoRefresh(iObjectTypeCode)
{
	for (var i = 0; i < document.forms.length; i++)
	{ 
		var o = document.forms[i];
		// Find all "Grids" with a matching Object Type Code
		//防止误刷新被更改后父页面
		if (iObjectTypeCode == o.ObjectTypeCode)
		{
			//暂时用__doPostBack()，以后调用主页面的刷新函数
			__doPostBack("REFRESH","");
			
			
		}
	}
}

function HtmlEncode(s)
{
	s = s.replace(/&/g, "&amp;");
	s = s.replace(/</g, "&lt;");
	s = s.replace(/>/g, "&gt;");
	return s.replace(/\"/g, "&quot;");
}
function decodeXml(s)
{
	// remove all unicode encoded symbols
	try
	{
		s = eval('"' + s.replace(/&#x(\w\w\w\w);/g, "\\u$1").replace(/"/g, '\\"') + '"');
	}
	catch (e) {;}
	// remove special encoding sequences
	s = s.replace(/&lt;/g, "<");
	s = s.replace(/&gt;/g, ">");
	s = s.replace(/&apos;/g, "'");
	s = s.replace(/&quot;/g, "\"");
	s = s.replace(/&amp;/g, "&");
	return s;
}
function URLEncode(s)
{
	//解决上面代码只替换第一个特殊字符的问题
	s = s.replace(/ /g, "%20");
	s = s.replace(/"/g, "%22");
	s = s.replace(/#/g, "%23");
	s = s.replace(/&/g, "%26");
	return s.replace(/\+/g, "%2B");
}


// 功能：公共删除 js 脚本
// 参数：functionid			-- 功能代码
//		 actionid			-- 动作代码
//		 dataxml			-- 要处理的数据，建议使用 oid 字符串，之间用分号(;)分隔。也可以用其他格式。
//		 title				-- 删除前的提示信息，如果没有传递该参数，默认为"确实要删除选中的记录吗？"
//		 isVerify			-- 是否只校验能否删除。如果为“true”，则只校验能否删除，而不删除数据。
// 返回："success"			-- 成功
//		 "cancel"			-- 取消删除
//		 "失败原因字符串"	-- 失败
// 注意：调用该函数的页面必须继承 AppPage
// zt	2005.09.06
function publicDelete(functionid,actionid,dataxml,title,isVerify)
{
	if (!isVerify)
	{
		if( window.confirm( (!title)?"确实要删除选择的记录吗？":title ) == false) 
			return "cancel";
	}

	return invokeByXMLHTTP(functionid,actionid,dataxml,isVerify);
}


// 功能：通过 XMLHTTP 方式调用后端函数
// 参数：functionid			-- 功能代码
//		 actionid			-- 动作代码
//		 dataxml			-- 要处理的数据，建议用 xml 格式
//		 isVerify			-- 是否只校验能否删除。如果为“true”，则只校验能否删除，而不删除数据。
// 返回："success"			-- 成功
//		 "失败原因字符串"	-- 失败
// 注意：调用该函数的页面必须继承 AppPage
// zt	2005.09.16
function invokeByXMLHTTP(functionid,actionid,dataxml,isVerify)
{
	if ( functionid == null )
		return "请设置功能代码！";
	
	if ( actionid == null )
		return "请设置动作代码！";

	if (!isVerify)
		isVerify = false;

	// 打开运行提示
	openWaiting();
	
	var oHTTP = new ActiveXObject("Msxml2.XMLHTTP");
	var sUrl = "/ApplicationMap.aspx";
	sUrl += "?functionid=" + escape(functionid);
	sUrl += "&actionid=" + escape(actionid);
	sUrl += "&isverify=" + isVerify;
	
	oHTTP.open( "POST", sUrl, false); 
	oHTTP.send("<dataxml mysessionstate=\"" + document.all["___MYSESSIONSTATE"].value + "\">" + dataxml + "<userdataxml></userdataxml></dataxml>");
	
	// 要求返回xml格式<xml result="true/flase" errormessage="错误信息！">任意正确格式的xml</xml>。
	var returnXML = oHTTP.responseText;
	
	delete(oHTTP);
	
	// 关闭运行提示
	closeWaiting();
	
	try
	{
		var xmlDom = new ActiveXObject("Microsoft.XMLDOM");
		xmlDom.loadXML(returnXML);
		
		if ( xmlDom.documentElement.attributes.getNamedItem("result").text == "true" )		// 如果删除成功
			return "success";
		else		// 如果删除失败
		{
			if ( xmlDom.documentElement.attributes.getNamedItem("errid") )
			{
				if ( xmlDom.documentElement.attributes.getNamedItem("errid").text == "001" )
					window.navigate("/ErrPage.aspx?errid=001");
			}
			else
				return xmlDom.documentElement.attributes.getNamedItem("errormessage").text ;
		}
	}
	catch (e)
	{
		return "返回了错误格式的 xml 信息！";
	}	
}

// 功能：打开提示
function openWaiting()
{
	if ( document.all["__divRuning"] )
		document.all["__divRuning"].style.display = "";
}

// 功能：关闭提示
function closeWaiting()
{
	if ( document.all["__divRuning"] )
		document.all["__divRuning"].style.display = "none";
}

function document.onkeydown()
{
	// 屏蔽 F5
	if (event.keyCode == 116)
	{
		event.keyCode = 0;
		event.returnValue = false;
	}
}

function document.onhelp()
{
 
	if (window.event == null || window.event == undefined) return false;

	window.event.returnValue =false;
	

	var myFileName = "";
	
	if (typeof(_MyHelpfile) != "undefined" && _MyHelpfile != undefined && _MyHelpfile !="")
	{
		 myFileName = _MyHelpfile;
	}
	else
	{
		//处理appGrid
		//1）Grid中没有数据
		if (document.location.pathname == "/_grid/gridcontainer.aspx")
		{
			if (typeof(parent._MyHelpfile) != "undefined" && parent._MyHelpfile != undefined && parent._MyHelpfile !="")
			{
				myFileName = parent._MyHelpfile;
			}
			else
			{
				//处理标签页中的appGrid
				if (typeof(parent.parent._MyHelpfile) != "undefined" && parent.parent._MyHelpfile != undefined && parent.parent._MyHelpfile !="")
				{
					myFileName = parent.parent._MyHelpfile;
				}
			}
		}

		//2）Grid中有数据		
		if (document.location.pathname == "/_grid/griddata.aspx")
		{
			if (typeof(parent.parent._MyHelpfile) != "undefined" && parent.parent._MyHelpfile != undefined && parent.parent._MyHelpfile !="")
			{
				myFileName = parent.parent._MyHelpfile;
			}
			else
			{
				//处理标签页中的appGrid
				if (typeof(parent.parent.parent._MyHelpfile) != "undefined" && parent.parent.parent._MyHelpfile != undefined && parent.parent.parent._MyHelpfile !="")
				{
					myFileName = parent.parent.parent._MyHelpfile;
				}
			}
		}
	}
	
	if (myFileName !="")
	{
		var sWidth = 750;
		var sHeight	= 510;
		var iX = screen.width-10;
		var iY = screen.height-55;
		var iTop,iLeft;
		iTop = (iY - sHeight)/2;
		iLeft = (iX - sWidth)/2;
		
		try {	
				var objWin = window.open(myFileName, "_MysoftHelpWin", "top=" + iTop + ",left=" + iLeft + ",width=" + sWidth + ",height=" + sHeight + ",status=1,resizable=1,location=0,menubar=1,toolbar=1,scrollbars=1");
				objWin.document.onhelp = MyHelpWinOnhelp;
				objWin.focus();
		}
		catch (e)
		{
			try
			{
				objWin = window.open(myFileName, "_MysoftHelpWin", "top=" + iTop + ",left=" + iLeft + ",width=" + sWidth + ",height=" + sHeight + ",status=1,resizable=1,location=0,menubar=1,toolbar=1,scrollbars=1");
				objWin.document.onhelp = MyHelpWinOnhelp;
				objWin.focus();
			}
			catch (e)
			{
				return false;
			}
		}
		  
	}
	
}

//功能：重写打开的帮助窗口的onhelp事件以屏蔽F1
function MyHelpWinOnhelp()
{

	if (window.event != null && window.event != undefined)
	{
		window.event.returnValue =false;
	}
	else
	{
		return false;
	}
}

// 打开关于明源CRM窗口			
function openAbout() 
{
	window.showModalDialog( "/about.aspx", "", "dialogHeight:" + 447 + "px; dialogWidth:" + 594 + "px; help:no; status:no;scroll:no;") ; 
}
			
// 功能：加锁
function _lock()
{
	if ( _iLocked <= 0 )
		_iLocked = 1;
	else
		_iLocked += 1;
}

// 功能：解锁
function _unlock()
{
	if ( _iLocked <= 0 )
		_iLocked = 0;
	else
		_iLocked -= 1;
}


//打印屏幕报表
//传递参数：
//	a_strRptid			---		报表ID
//	a_strReportName		---		报表名称
//	a_strFilter			---		报表过滤条件
//	a_strPramValue		---		报表参数
function doPrintPMReport(a_strRptid,a_strReportName,a_strFilter,a_strPramValue)
{
	//报表ID、报表名称、报表过滤条件、报表变量值
	if(a_strRptid==undefined||a_strReportName==undefined||a_strFilter==undefined||a_strFilter=="")return;
	
	//报表参数处理
	if(a_strPramValue==undefined)a_strPramValue = "";
	
	//如果报表名称、报表ID 都没有指定，则不进行任何处理
	if(a_strReportName=="" && a_strRptid=="")return;
	
	//“+”号从前端传递到后端时，会当成特殊字符，到后端后会不识别，先用“*plus*”替换，然后在后端再替换成“+”号
	a_strFilter = a_strFilter.replace(/\+/g,"*plus*")
	
	//将屏幕报表名称传递给隐藏页面，打开屏幕屏幕报表parmwhere
	refreshIframe.location="/Report/ScreenRpt_Refresh.aspx?rptname=" + escape(a_strReportName) + "&rptid=" + escape(a_strRptid) + "&parmwhere=" + escape(a_strFilter) + "&parmrptval=" + escape(a_strPramValue);		
}

//通过XMLHTTP通道不刷新页面从后台取数
//参数：sFile					-	打开文件
//		strBusinessType			-	业务对象
//		strBusinessInfo			-	业务信息
//		strPostStream			-	大容量业务参数
function GetDataByXMLHTTP(strFile,strBusinessType,strBusinessInfo,strPostStream)
{
	if (strFile == undefined || strFile == "") strFile = "/Pub_XmlHttp.aspx"
	var rdNum = Math.random();
	var oHTTP = new ActiveXObject("Msxml2.XMLHTTP");
	var sUrl = strFile + "?p_businessType=" + escape(strBusinessType) + "&p_businessInfo=" + escape(strBusinessInfo)  + "&rdnum=" + rdNum; 
		
	if (strPostStream==undefined)
	{
		oHTTP.open( "GET", sUrl, false); 
		oHTTP.send(); 
	}
	else
	{
		oHTTP.open( "POST", sUrl, false); 
		oHTTP.send(strPostStream); 
	}
	
	var bSuccess = handleXMLErr(oHTTP.responseXML); 
	if (bSuccess) 
	{
		if(oHTTP.responseText=="服务器超时") window.navigate("/ErrPage.aspx?errid=001");
		return oHTTP.responseText;	
	}
	else
	{
		alert("操作失败，请关闭重试！");
		return "-1";
	}
}


//通过XMLHTTP通道后台取数(不刷新页面)
//参数：strFile					-	打开文件(默认为/Pub_XmlHttp.aspx)
//		strBusinessType			-	业务对象
//		strParamStr				-	参数字符串
//		strPostStream			-	大容量业务参数
//
//************************************************************
//1、 AJAX 是一个技术名词，不应该作为函数名，请重新命名此函数！
//2、 本脚本块内已经存在一个相同功能的函数，为：GetDataByXMLHTTP(...)，建议直接使用 GetDataByXMLHTTP
//************************************************************
function XmlHttpRequest(strFile,strBusinessType,strParamStr,strPostStream)
{
	if (strFile == undefined || strFile == "") strFile = "/Pub_XmlHttp.aspx"
	
	var strTmp = "&";
	if (strFile.indexOf("?") == -1) strTmp ="?";
	
	var rdNum = Math.random();
	var sUrl = strFile + strTmp + "p_businessType=" + escape(strBusinessType) + "&rdnum=" + rdNum; 
	if (strParamStr !="") sUrl += "&" + strParamStr;
	
	var oHTTP = new ActiveXObject("Msxml2.XMLHTTP");	
	if (strPostStream==undefined)
	{
		oHTTP.open( "GET", sUrl, false); 
		oHTTP.send(); 
	}
	else
	{
		oHTTP.open( "POST", sUrl, false); 
		oHTTP.send(strPostStream); 
	}
	
	var bSuccess = handleXMLErr(oHTTP.responseXML); 
	if (bSuccess) 
	{
		if(oHTTP.responseText=="服务器超时") window.navigate("/ErrPage.aspx?errid=001");
		return oHTTP.responseText;	
	}
	else
	{
		alert("操作失败，请关闭重试！");
		return "-1";
	}
}