// 创那httpRequest对象
var xmlHttp;
function createXMLHttpRequest() {
    if (window.ActiveXObject) {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    } 
    else if (window.XMLHttpRequest) {
        xmlHttp = new XMLHttpRequest();
    }
		return xmlHttp;
}

// 通过httpRequest对象绑定数据
function bindValueByXmlHttp(strObject, strTagName, objXmlHttp)
{
	var strValue = "";
	try{
	strValue = objXmlHttp.getElementsByTagName(strTagName)[0].firstChild.nodeValue;
	}
	catch(err){
	strValue = "";
	}
	SetObjectInnerHtml(strObject, strValue);
}

// 读取httpRequest对象指定数据
function getValueByXmlHttp(strTagName, objXmlHttp)
{
	var strValue = "";
	try{
	strValue = objXmlHttp.getElementsByTagName(strTagName)[0].firstChild.nodeValue;
	}
	catch(err){
	strValue = "";
	}
	return strValue;
}

// 通过httpRequest对象绑定数据
function BindObjectInnerHtmlByXmlAttribute(strObject, objXmlDoc, strAttribute)
{
	var strValue = objXmlDoc.getAttribute(strAttribute);
	BindObjectInnerHtml(strObject, strValue);
}

// onchange时重新绑定第二级Select
function ReBindSecondLevelClass(strValue, strObjIDKey)
{
	if(strValue!="0")
	{
		var strSubClass = PUserServices.GetSubClassListString(strValue).value;
		var arrSubClass = strSubClass.split(',');
		var obj = document.getElementById(strObjIDKey)
		obj.length = 0;
		obj.length++;
		obj.options[0].selected=true;
		obj.options[0].value = strValue;
		obj.options[0].text = strValue;
		
		for(var i=1; i<=arrSubClass.length; i++)
		{
			obj.length++;
			obj.options[i].text = arrSubClass[i-1];
			var strValue = arrSubClass[i-1];
			strValue = strValue.substring(2, strValue.length)
			obj.options[i].value = strValue;
		}
	}
}
//绑定2级联动的Select
function Bind2LevelClass(strRootClassIDKey, str2SubClassIDKey, strValue)
{
	var arrValue = strValue.split("-")
	var objRootClass = document.getElementById(strRootClassIDKey);
	var objSubClass = document.getElementById(str2SubClassIDKey);
	objRootClass.value = arrValue[0];
	ReBindSecondLevelClass(arrValue[0], str2SubClassIDKey);
	objSubClass.value = arrValue[1];
}

// 重新设置图片大小
function ResizeImage(strObjIDKey, iWidth, iHeight)
{
	var obj = document.getElementById(strObjIDKey);

	if (obj.width>iWidth)
	{
		if (iWidth!=null && iWidth!=0)
		{
			obj.width = iWidth;
		}
	}

	if (obj.height>iHeight)
	{
		if (iHeight!=null && iHeight!=0)
		{
			obj.height = iHeight;
		}
	}
}

// 通过类别字符串绑定Select, 
// strObjIDKey 控件名称
// strItemTag 各Opation之间的分隔符
// strKeyValueTag 各Key, Value之间的分隔符
function BindSelectByListString(strListString, strObjIDKey, strItemTag, strKeyValueTag)
{
	var obj = document.getElementById(strObjIDKey)
	if (obj!=null)
	{
		var arrItem = strListString.split(strItemTag);
		obj.length = 0;
		for(var i=0; i<arrItem.length; i++)
		{
			obj.length++;
			if (strKeyValueTag!="" && strKeyValueTag!=null)
			{
					var arrKeyValue = arrItem[i].split(strKeyValueTag);
					obj.options[i].text = arrKeyValue[0];
					obj.options[i].value = arrKeyValue[1];
			}
			else
			{
				obj.options[i].text = arrItem[i];
				obj.options[i].value = arrItem[i];
			}
		}
	}
}

function GetObjectChecked(strObj)
{
	var obj = document.getElementById(strObj);
	if (obj!=null)
	{
		return obj.checked;
	}
	else
	{
		return false;
	}
}


function GetObjectInnerHtml(strObj)
{
	var obj = document.getElementById(strObj);
	if (obj!=null)
	{
		return obj.innerHTML;
	}
	else
	{
		return "";
	}
}

function GetObjectValue(strObj)
{
	var obj = document.getElementById(strObj);
	if (obj!=null)
	{
		return obj.value;
	}
	else
	{
		return "";
	}
}



function SetObjectValue(strObj, strValue)
{
	var obj = document.getElementById(strObj);
	if (obj!=null)
	{
		obj.value = strValue;
	}
}


function SetObjectInnerHtml(strObj, strValue)
{
	var obj = document.getElementById(strObj);
	if (obj!=null)
	{
		obj.innerHTML = strValue;
	}
}

function SetObjectHref(strObj, strValue)
{
	var obj = document.getElementById(strObj);
	if (obj!=null)
	{
		obj.href = strValue;
	}
}


function XmlString2HtmlString(strValue)
{
	strValue = strValue.replace(/&lt;br&gt;/g, "<BR>");
	return strValue;
}
															
// 绑定数据到对象.innerHtml
function BindObjectInnerHtml(strObj, strValue)
{
	var obj = document.getElementById(strObj);
	if (obj!=null)
	{
		obj.innerHTML = strValue;
	}
}

// 绑定数据到对象.value
function BindObjectChecked(strObj, bValue)
{
	var obj = document.getElementById(strObj);
	if (obj!=null)
	{
		if(bValue=="True")
		{
			obj.checked = true;
		}
		else
		{
			obj.checked = false;
		}

	}
}

// 绑定数据到对象.value
function BindObjectValue(strObj, strValue)
{
	var obj = document.getElementById(strObj);
	if (obj!=null)
	{
		obj.value = strValue;
	}
}

// 绑定数据到对象.value
function BindObjectValue4Xml(strObj, strValue)
{
	var obj = document.getElementById(strObj);
	if (obj!=null)
	{
		strValue = strValue.replace(/&lt;BR&gt;/gi, "<br>");
		strValue = strValue.replace(/\\r\\n/gi, "\r\n");
//		strValue = strValue.replace(/<BR>/gi, "\r\n");
		obj.value = strValue;
	}
}


// 绑定数据到对象.value
function BindObjectInnerHtml4Xml(strObj, strValue)
{
	var obj = document.getElementById(strObj);
	if (obj!=null)
	{
		strValue = strValue.replace(/&lt;BR&gt;/gi, "<br>");
		obj.innerHTML = FormatXml4Html(strValue);
	}
}

// 绑定数据到对象.value
function FormatXml4Html(strValue)
{
//	strValue = strValue.replace(/&lt;BR&gt;/gi, "<br>");
	strValue = strValue.replace(/\\\\r\\\\n/gi, "<br>");
	return strValue;
}


// 绑定数据到对象.value
function BindObjectSrc(strObj, strValue)
{
	var obj = document.getElementById(strObj);
	if (obj!=null && strValue!="")
	{
		obj.src = strValue;
	}
}


// 从Hmtl字符串中分离javascript代码,并运行
function EvalScript(strValue)
{
//	alert(GetRegexString(strValue, 2));
eval(GetRegexString(strValue, 2));
}

// 通过Regex分离数据
function GetRegexString(strValue, iRegexIndex)
{
	var strRegex = "<SCRIPT([^>]*)>([^<]*)</SCRIPT>";
	var re = new RegExp(strRegex, "gi");
	r = strValue.match(re);
	if (r!=null)
	{
		switch(iRegexIndex)
		{
			case 1 :
				return unescape(RegExp.$1);
			case 2 :
				return unescape(RegExp.$2);
			case 3 :
				return unescape(RegExp.$3);
		}
	}
	else
	{
		return "";
	}
}
	
// 标签页切换
//var iTabCount = 0;
function ShowTabPage(iTabIndex, iTabCount)
{
	var strTabStrip = "";
	var strTabPage = "";
	for(var i=1; i<=iTabCount; i++)
	{
		strTabStrip = "TabStrip" + i;
		strTabPage = "TabPage" + i;
		
		if (iTabIndex==i)
		{
			document.getElementById(strTabStrip).className = "TabStripStyle1";
			document.getElementById(strTabPage).style.display = "block";
		}
		else
		{
			document.getElementById(strTabStrip).className = "TabStripStyle2";
			document.getElementById(strTabPage).style.display = "none";
		}
	}
//	ResetIframeSize();
}


function BindListPager(iPageIndex, iPageSize, iRecordCount, strPagePerfix)
{
	document.write ("共"+iRecordCount+"条 每页"+iPageSize+"条 第 ");
	var iPageCount = (iRecordCount - iRecordCount % iPageSize) / iPageSize + 1;

	document.write (" <select id=\"ListPager\" name=\"ListPager\" class=\"ListPager\" onChange=\"parent.location=this.options[this.selectedIndex].value\">");
	for(var i=1; i<=iPageCount; i++)
	{
		if(iPageIndex==i)
		{
			document.write ("<option value=\""+strPagePerfix+"_"+i+ ".html\" selected=\"selected\">"+i+"/"+iPageCount+"</option>");
		}
		else
		{
			document.write ("<option value=\""+strPagePerfix+"_"+i+ ".html\">"+i+"/"+iPageCount+"</option>");
		}
	}
//	<select id="ListPager" name="ListPager" class="ListPager" onChange="parent.location=this.options[this.selectedIndex].value">
	document.write ("</select> 页 ");

	if (iPageIndex>1)
	{
		document.write ("<a href=\""+strPagePerfix+"_"+(iPageIndex-1)+".html\">上一页</a> ");
	}
	else
	{
		document.write ("<span class=\"NoAction\">上一页</span> ");
	}
	
	if (iPageIndex<iPageCount)
	{
		document.write ("<a href=\""+strPagePerfix+"_"+(iPageIndex+1)+".html\">下一页</a> ");
	}
	else
	{
		document.write ("<span class=\"NoAction\">下一页</span> ");
	}
}

function BindListPager2(iPageIndex, iPageCount, strPagePerfix)
{
	var objPageSelect = document.getElementById("ListPager")
	objPageSelect.length = 0;
	var i;

	for (i=1 ; i <= iPageCount ; i++)
	{
		objPageSelect.length += 1;
		objPageSelect.options[i-1].text = i+"/"+iPageCount;
		objPageSelect.options[i-1].value = strPagePerfix + "_" + i + ".html";
		if (iPageIndex == i)
		{
			objPageSelect[i-1].selected=true;
		}
	}
}

// 绑定分页代码中 Page 的下拉选框 用户于模版生成的列表页面
function BuildPageForTemplate(strPageCount, strPagePerfix)
{
	objPageSelect = document.getElementById("Page")
	objPageSelect.length=0;
	var i;

	for (i=0 ; i < strPageCount ; i++)
	{
		objPageSelect.length += 1;
		objPageSelect.options[i].text = i+1;
		objPageSelect.options[i].value = i+1;
		if (Page == (i+1))
		{
			objPageSelect[i].selected=true;
		}
	}
}

// 读取.Net(C#)中设置的Cookies
function GetCSharpCookie(strKeyName, strKeyGroup)
{
	var strRegex = ""
	if (strKeyGroup!=null)
	{
		strRegex = strKeyGroup + "=[^;]*[&]?" + strKeyName + "=([^&;]*)(&|;|\s)"
	}
	else
	{
		strRegex = strKeyName + "=([^&;]*)(&|;)"
	}
//	alert(strRegex)
	var strCookie = document.cookie;
	alert(strCookie)
	var re = new RegExp(strRegex, "g");
	r = strCookie.match(re);
	if (r!=null)
	{
		return unescape(RegExp.$1);
	}
	else
	{
		return "";
	}
}

function GetASPCookie2(name)
{
	var search;
	
	search = name + "="
	offset = document.cookie.indexOf(search) 
	if (offset != -1)
	{
		offset += search.length ;
		end = document.cookie.indexOf("&", offset) ;
		if (end == -1)
		{
			end = document.cookie.length;
		}
		return unescape(document.cookie.substring(offset, end));
	}
	else
	{
		return "";
	}
}

// 模态对话框
function OpenModalDialog(strUrl)
{
	return window.showModalDialog(strUrl, "","edge:raised;status:no;scroll:no;help:no;resizable:no;unadorned:yes;dialogWidth:600px;dialogHeight:350px");
} 
// 模态对话框 By Param
function OpenModalDialogByParam(strUrl, strName, strParam)
{
	return window.showModalDialog(strUrl, strName, strParam);
} 

// 模态对话框 By Param
function OpenModalDialogByWH(strUrl, iWidth, iHeight)
{
	var strParam = "edge:raised;status:no;scroll:no;help:no;resizable:no;unadorned:yes;dialogWidth:"+iWidth+"px;dialogHeight:"+iHeight+"px";
	return window.showModalDialog(strUrl, "", strParam);
} 


// 非模态对话框
function OpenModelessDialog(strUrl)
{
	return window.showModelessDialog(strUrl, "","edge:raised;status:no;scroll:no;help:no;resizable:no;unadorned:yes;dialogWidth:600px;dialogHeight:350px");
} 
// 非模态对话框 By Param
function OpenModelessDialogByParam(strUrl, strName, strParam)
{
	return window.showModelessDialog(strUrl, strName, strParam);
} 

function GetLoadingHtmlCode()
{
	return "<div style=\"width:100%; height:26px;\"><strong>Loading...</strong><img src=\"/Images/Loading.gif\" alt=\"Loading\" width=\"16\" height=\"16\" /></div>"
	
}
// 弹出新窗口
function OpenNewWindnow(strUrl)
{
	return window.open(strUrl);
}


//弹出页面, 防止IE工具对window.open的拦截
window._open = window.open;
window.open = window_new_open;
function window_new_open( a,b,c )
{
	var win;
	if( c ) win=window._open( a,b,c );
	else if( b ) win=window._open( a,b );
	else win=window._open( a );
	if( win!=null&&!win.closed ) return win;
	var option='status:0;help:0;dialogleft:10000px;dialogtop:10000px;dialogheight:0px;dialogwidth:0px';
	win=showModalDialog( 'open.htm',[a,b,c],option );
	return win;
}

//弹出页面
function popup(weburl,w,h)
{
		window.open(weburl,w,"toolbar=no,location=no,directories=no,menubar=no,resizable=no,status=no,scrollbars=no,width="+w+",height="+h+",left=100,top=10");
}

//弹出页面can be resized
function popup1(weburl,w,h)
{
window.open(weburl,"mybox","toolbar=yes,location=yes,directories=yes,menubar=yes,resizable=yes,status=yes,scrollbars=no,width="+w+",height="+h+",left=100,top=10")
}

// 替换字符串中文字
function ReplaceString(inputString, fromString, toString)
{
	var temp = inputString;
	if (fromString == "")
	{
		return inputString;
	}
	if (toString.indexOf(fromString) == -1)
	{
		while (temp.indexOf(fromString) != -1)
		{
			var toTheLeft = temp.substring(0, temp.indexOf(fromString));
			var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
			temp = toTheLeft + toString + toTheRight;
		}
	}
	else
	{
		var midStrings = new Array("~", "`", "_", "^", "#");
		var midStringLen = 1;
		var midString = "";
		while (midString == "")
		{
			for (var i=0; i < midStrings.length; i++)
			{
				var tempMidString = "";
				for (var j=0; j < midStringLen; j++)
				{
					tempMidString += midStrings;
				}
				if (fromString.indexOf(tempMidString) == -1)
				{
					midString = tempMidString;
					i = midStrings.length + 1;
				}
			}
		}
		while (temp.indexOf(fromString) != -1)
		{
			var toTheLeft = temp.substring(0, temp.indexOf(fromString));
			var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
			temp = toTheLeft + midString + toTheRight;
		}
		while (temp.indexOf(midString) != -1)
		{
			var toTheLeft = temp.substring(0, temp.indexOf(midString));
			var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
			temp = toTheLeft + toString + toTheRight;
		}
	}
	return temp;
}
function XMLHttp(strUrl)
{
	var strText = "";
	eval ('try {xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {xmlhttp = null;}');
	if (xmlhttp != null)
	{
		xmlhttp.Open("GET", strUrl, false);
		xmlhttp.Send();
		if (xmlhttp.status==200)
		{
			strText = xmlhttp.responseText;
		}
		else
		{
			strText=-1;
		}
	}
	return strText;
}

function checkRegExp(src, regE)
{
	return regE.test(src.trim());
}

//删除空格
String.prototype.lTrim = function () {
return this.replace(/^\s*/, "");
}
// remove trailing whitespace
String.prototype.rTrim = function () {
return this.replace(/\s*$/, "");
}
// remove leading and trailing whitespace
String.prototype.trim = function () {
return this.rTrim().lTrim();
}


function AlertFocus(strObj, msg)
{
	alert(msg);
	try
	{
		
		var obj = document.getElementById(strObj);
		if (obj!=null)
		{
			obj.focus()
		}
	}
	catch(e)
	{
	}
}

/*function AlertFocus(obj, msg)
{
	alert(msg);
	try
	{
		obj.focus()
	}
	catch(e)
	{
	}
}*/

// 读取用户提交的地址栏中的指定参数
function GetQueryString(priStrQueryName)
{
	priStrValue = "";
	priIsFound = false;
	if (this.location.search.indexOf("?")==0&&this.location.search.indexOf("=")>1)
	{
		priArraySource = unescape(this.location.search).substring(1,this.location.search.length).split("&");
		priGetQSi = 0;
		while (priGetQSi<priArraySource.length&&!priIsFound)
		{
			if (priArraySource[priGetQSi].indexOf("=")>0)
			{
				if (priArraySource[priGetQSi].split("=")[0].toLowerCase()==priStrQueryName.toLowerCase())
				{
					priStrValue = priArraySource[priGetQSi].split("=")[1];
					priIsFound = true;
				}
			} 
			priGetQSi ++ ;
		}	
	}
	return priStrValue;
} 

// 跳转选择框
function MM_jumpMenu(targ,selObj,restore)
{
	var strReturn = "";
	priStrValue = "";
	if (this.location.search.indexOf("?")==0&&this.location.search.indexOf("=")>1)
	{
		priArraySource = unescape(this.location.search).substring(1,this.location.search.length).split("&");
		priGetQSi = 0;
		while (priGetQSi<priArraySource.length)
		{
			if (priArraySource[priGetQSi].indexOf("=")>0)
			{
				if (priArraySource[priGetQSi].split("=")[0].toLowerCase()!="page")
				{
					// priStrValue = priArraySource[priGetQSi].split("=")[1];
					strReturn += "&" + priArraySource[priGetQSi].split("=")[0] + "=" + priArraySource[priGetQSi].split("=")[1];
				}
			}
			priGetQSi ++ ;
		}	
	}

	eval(targ+".location='?page="+selObj.options[selObj.selectedIndex].value+ strReturn + "'");
	if (restore)
		selObj.selectedIndex=0;
}


// 绑定分页代码中 Page 的下拉选框
function BuildPage(Page,PageCount)
{
	objPageSelect = document.getElementById("Page")
	objPageSelect.length=0;
	var i;

	for (i=0 ; i < PageCount ; i++)
	{
		objPageSelect.length += 1;
		objPageSelect.options[i].text = i+1;
		objPageSelect.options[i].value = i+1;
		if (Page == (i+1))
		{
			objPageSelect[i].selected=true;
		}
	}
}

function GetShowPagesQueryString()
{
	var strReturn = "";
	priStrValue = "";
	if (this.location.search.indexOf("?")==0&&this.location.search.indexOf("=")>1)
	{
		priArraySource = unescape(this.location.search).substring(1,this.location.search.length).split("&");
		priGetQSi = 0;
		while (priGetQSi<priArraySource.length)
		{
			if (priArraySource[priGetQSi].indexOf("=")>0)
			{
				if (priArraySource[priGetQSi].split("=")[0].toLowerCase()!="page")
				{
					// priStrValue = priArraySource[priGetQSi].split("=")[1];
					strReturn += "&" + priArraySource[priGetQSi].split("=")[0] + "=" + priArraySource[priGetQSi].split("=")[1];
				}
			}
			priGetQSi ++ ;
		}	
	}
	return strReturn;
}
// 生成分页代码
function ShowPages(iPageSize, iPageIndex, iRecordCount)
{
	iPageSize = parseInt(iPageSize)
	iPageIndex = parseInt(iPageIndex)
	iRecordCount = parseInt(iRecordCount)
	
	if (iRecordCount> iPageSize)
	{
		iPageCount = parseInt((iRecordCount+iPageSize-1) / iPageSize);
	}
	else if (iRecordCount>0)
	{
		iPageCount = 1;
	}
	else
	{
		iPageCount = 0;
	}
	var reString = "" ;
	
	
	reString += "<table width='100%' cellspacing='0' cellpadding='0' border='0'>";
	reString += "<tr><td align='center' class='ShowPage'>共 " + iRecordCount + " 条 每页 " + iPageSize + " 条 第 <select name=Page class='PageShowSelect' id='Page' onChange=\"MM_jumpMenu('self',this,0)\" ></select> 页";
	var iPerPage = iPageIndex - 1;
	if (iPerPage>0)
	{
		reString += "&nbsp;<a href=\"?page=" + iPerPage + GetShowPagesQueryString() + "\">上一页</a>";
	}
	else
	{
		reString += "&nbsp;<font color=\"#CCCCCC\">上一页</font>";
	//	reString += "&nbsp;上一页";
	}
	
	var iNextPage = iPageIndex + 1;
	if (iNextPage<=iPageCount)
	{
		reString += "&nbsp;<a href=\"?page=" + iNextPage + GetShowPagesQueryString() + "\">下一页</a>";
	}
	else
	{
		reString += "&nbsp;<font color=\"#CCCCCC\">下一页</font>";
		//reString += "&nbsp;";
	}
	reString += "</td></tr></table>";

	document.write(reString);
	BuildPage(iPageIndex , iPageCount);
}


// 绑定控件
function BindObject(strObjID, strValue, iMode)
{
	var obj = document.getElementById(strObjID);

	if (obj!=null)
	{
		if (iMode==null)
		{
			switch (obj.type)
			{
				case "text" : case "select-one" :
				{
					iMode = 3;
					break;
				}
				default :
				{
					try
					{
						obj.innerHTML;
						iMode = 1;
					}
					catch(e)
					{
						try
						{
							obj.text;
							iMode = 2;
						}
						catch(e)
						{
							try
							{
								obj.value;
								iMode = 3;
							}
							catch(e)
							{
								try
								{
									obj.src;
									iMode = 4;
								}
								catch(e)
								{
									
								}
							}
						}
					}
				}
			}
		}

		switch (iMode)
		{
			case 1 :
			{
				obj.innerHTML = strValue;
				break;
			}
			case 2 :
			{
				obj.text= strValue;
				break;
			}
			case 3 :
			{
				obj.value= strValue;
				break;
			}
			case 4 :
			{
				if (strValue!=null && strValue!="")
				{
					obj.src= strValue;
				}
				break;
			}
			default :
				break;
		}
	}
}



// 读取ASP中设置的Cookies
function GetASPCookie(name)
{
	var search;
	
	search = name + "="
	offset = document.cookie.indexOf(search) 
	if (offset != -1)
	{
		offset += search.length ;
		end = document.cookie.indexOf("&", offset) ;
		if (end == -1)
		{
			end = document.cookie.length;
		}
		return unescape(document.cookie.substring(offset, end));
	}
	else
	{
		GoBackToLogin();
		return "";
	}
}

// 通过cookie提取Url参数
function GetUrlByCookie()
{
//	var strUrl = "200602,25,ReallyNet";
	var strReturn = "", strPerfix = "";
	strReturn = GetCSharpCookie("CookiePUserXmlPath")
	arrStr = strReturn.split("/")
	for (var i=1; i<arrStr.length-1; i++)
	{
		strReturn += strPerfix + arrStr[i];
		strPerfix = ",";
	}
	
//	return strReturn;
//	strReturn = strUrl;
/*	strReturn = strUrl.replace(/,/g, "/");
	strReturn = "/PUserXml/" + strReturn + "/Music.xml";*/
	return strReturn;
}
