var xmlHttp;

function createXMLHttpRequest()
{
    if (window.ActiveXObject)
	{
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }else if (window.XMLHttpRequest)
	{
        xmlHttp = new XMLHttpRequest();
    }
}
function getPortaletContent(url)
{
	var ContentHttp;
    if (window.ActiveXObject)
	{
        ContentHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }else if (window.XMLHttpRequest)
	{
        ContentHttp = new XMLHttpRequest();
    }
    ContentHttp.open("POST",'/'+ url + ".ypc?timeStamp=" + new Date().getTime(), true);
    //xmlHttp.onreadystatechange = handleStateChange;
	ContentHttp.onreadystatechange = function(){
		if(ContentHttp.readyState == 4)
		{
			if(ContentHttp.status == 200)
			{
				HandlerPortaletContent(url,ContentHttp);
			}
		}
	};
	ContentHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");   
    ContentHttp.send();
}

function HandlerPortaletContent(url,ContentHttp)
{
	//alert(url);
	var cntInnerHtml=document.getElementById("CNT_"+url);
	cntInnerHtml.innerHTML = '';
	var theCont = ContentHttp.responseText;
	 var sss = "abc<script defer>alert('hello world')</script>";
	 var saaass = "";
	//theCont.replace("<script","&nbsp;<\\script");
	//sss.replace("script>","scr");
	//alert(sss);
	var saaass = "ipt>";
	//theCont = "&nbsp;<\script language=javascript defer>function test(){alert('hello,world!');}<\/script>";
	cntInnerHtml.innerHTML =theCont;
	//_xml_htmlInsert(cntInnerHtml,sss);
}
function _xml_htmlInsert(partId, viewData) 
{ 

	var regexp1 = /<script(.|\n)*?>(.|\n|\r\n)*?<\/script>/ig; 
	var regexp2 = /<script(.|\n)*?>((.|\n|\r\n)*)?<\/script>/im; 

/* draw the html first */ 
	partId.innerHTML = viewData.replace(regexp1, ""); 

	var result = viewData.match(regexp1); 
	if (result) 
	{ 
		for (var i = 0; i < result.length; i++) 
		{ 
			var realScript = result[i].match(regexp2); 
			//_xml_executeScript(realScript[2], partId); 
		} 
	} 
} 


function doPost(url,cheJsonObj,theRetHandler)
{
    var url = url + "?timeStamp=" + new Date().getTime();
    createXMLHttpRequest();
    xmlHttp.open("POST", url, true);
    //xmlHttp.onreadystatechange = handleStateChange;
	xmlHttp.onreadystatechange = function(){
		if(xmlHttp.readyState == 4)
		{
			if(xmlHttp.status == 200)
			{
				theRetHandler();
			}
		}
	};
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");   
    xmlHttp.send(escape(cheJsonObj.toJSONString()));
}
//网页局部褪色技术
var speed = 15;
var timers;
function changeColor(updaterId,defaultBgColor)
{
	var theUpdateDiv = document.getElementById(updaterId);
	var color = theUpdateDiv.style.backgroundColor;
	 // 当前背景色(rgb)的rg部分:如#123456的1234
	var color_rg = color.slice(1, 5); 
	 // 当前背景色(rgb)的b部分:如#123456的56
	 // 并将b部分增加一个数值,转换为10进制整数(向白色靠近)
	var color_b = parseInt(color.slice(5), 16) + speed;
	 // 如果b部分不超过255
	if (color_b < 256)
	{
		   // 求b的16进制形式
		var b1 = Math.floor((color_b / 16)).toString(16);
		var b2 = (color_b % 16).toString(16);
		   // 设置新的背景色
		   theUpdateDiv.style.backgroundColor = "#" + color_rg + b1 + b2;
		   timers = setTimeout("changeColor('"+updaterId+"')", 150);
	}else //背景色已经是白色
	{
		   // 停止计时器
		clearTimeout(timers);
		theUpdateDiv.style.backgroundColor = "#C1E2A5";
	}
}
function fadeText(updaterId,noteText,defaultBgColor)
{
	var theUpdateDiv = document.getElementById(updaterId);
	if(theUpdateDiv.hasChildNodes())
	{
		theUpdateDiv.removeChild(theUpdateDiv.childNodes[0]);
	}
	theUpdateDiv.style.backgroundColor = defaultBgColor;

	var teaaxt = document.createTextNode(noteText);
	// 颜色渐变时需要使用的定时器对象数组
	theUpdateDiv.style.backgroundColor = "#ffff00";
	theUpdateDiv.style.cursor = "hand";
	theUpdateDiv.appendChild(teaaxt);
	timers = setTimeout("changeColor('"+updaterId+"')", 150);
}
function fadeValue(updaterId,noteText,defaultBgColor)
{
	var theUpdateDiv = document.getElementById(updaterId);
	theUpdateDiv.style.backgroundColor = defaultBgColor;
	theUpdateDiv.value = noteText;
	// 颜色渐变时需要使用的定时器对象数组
	theUpdateDiv.style.backgroundColor = "#ffff00";
	theUpdateDiv.style.cursor = "hand";
	timers = setTimeout("changeColor('"+updaterId+"','"+defaultBgColor+"')", 150);
}

function insertTr(theYc_id,theTd,theTableName)
{
	if (theTd.length > 0)
	{
		var chkbox=document.createElement("input");
		chkbox.className = "checkbox"
		chkbox.type="checkbox";
		chkbox.onclick=function(){seledtSingel();}
		chkbox.value=theYc_id;
		chkbox.name="SELECTBOX";
		chkbox.id="SELECTBOX";
		var tr = document.createElement("tr"); 
			tr.className="null";
			tr.onmouseover=function(){this.className = 'mouseon';}
			tr.onmouseout=function(){this.className = 'null';}
			tr.ondblclick=function(){YC_EDIT(theYc_id);}
		var tdChk = document.createElement("td");
			tdChk.appendChild(chkbox);
		tr.appendChild(tdChk);
		for (var theTdSqs = 0; theTdSqs < theTd.length ; theTdSqs ++ )
		{
			var text = document.createTextNode(theTd[theTdSqs]);
			var td = document.createElement("td");
			td.appendChild(text);
			tr.appendChild(td);
		}
		var tbody = document.createElement("tbody");
		tbody.appendChild(tr);
		var parNode = document.getElementById(theTableName);
		parNode.appendChild(tbody);
	}
}

function insertTable(card,theTdA,theTableId)
{
	//var ddds = card.bindings[0].value;
	//alert(card.RS_page_cnt);
	//alert(card.RS_page_num);
	//alert(card.RS_page_size);
	var responseDiv = document.getElementById(theTableId);
	var theLen = responseDiv.childNodes.length;
	for (var theDelRow = 1; theDelRow < theLen ; theDelRow ++ )
	{
		responseDiv = document.getElementById(theTableId);
		if(responseDiv.hasChildNodes())
		{
			responseDiv.removeChild(responseDiv.childNodes[1]);
		}
	}
	for (i = 0; i < card.RS.length ; ++i)
	{
		//responseDiv.value = responseDiv.value + card.RS[i].hu_name + card.RS[i].hu_email +"\n" ;
		var theTd = new Array();
		for (var theTdSqs = 0; theTdSqs < theTdA.length ; theTdSqs++ )
		{
			theTd[theTdSqs] = card.RS[i][theTdA[theTdSqs]]
		}
		insertTr(card.RS[i].yc_id,theTd,theTableId)
		//alert(card.bindaaings[i].regex);
	}
	//fadeValue('YC_ID',"DDDDDDDDDDDDDDDDDDD",'');
	//responseDiv.value = xmlHttp.responseText ;
}

//遮蔽层提示框
var bgObj;
var title;
var msgObj;
function sAlert(str)
{
		var msgw,msgh,bordercolor;
		msgw=400;//提示窗口的宽度
		msgh=100;//提示窗口的高度
		bordercolor="#336699";//提示窗口的边框颜色
		titlecolor="#99CCFF";//提示窗口的标题颜色

		var sWidth,sHeight;
		sWidth=document.body.offsetWidth;
		sHeight=document.body.offsetHeight;

		bgObj=document.createElement("div");
		bgObj.setAttribute('id','bgDiv');
		bgObj.style.position="absolute";
		bgObj.style.top="0";
		bgObj.style.background="#777";
		bgObj.style.filter="progid:DXImageTransform.Microsoft.Alpha(startX=20, startY=20, finishX=100, finishY=100,style=1,opacity=75,finishOpacity=100);";
		bgObj.style.opacity="0.6";
		bgObj.style.left="0";
		bgObj.style.width=sWidth + "px";
		bgObj.style.height=sHeight + "px";
		document.body.appendChild(bgObj);

		msgObj=document.createElement("div")
		msgObj.setAttribute("id","msgDiv");
		msgObj.setAttribute("align","center");
		msgObj.style.position="absolute";
		msgObj.style.background="white";
		msgObj.style.font="12px/1.6em Verdana, Geneva, Arial, Helvetica, sans-serif";
		msgObj.style.border="1px solid " + bordercolor;
		msgObj.style.width = msgw + "px";
		msgObj.style.height = msgh + "px";
		msgObj.style.top = (document.documentElement.scrollTop + (sHeight-msgh)/2) + "px";
		msgObj.style.left=(sWidth-msgw)/2 + "px";

		title=document.createElement("h4");
		title.setAttribute("id","msgTitle");
		title.setAttribute("align","right");
		title.style.margin="0";
		title.style.padding="3px";
		title.style.background=bordercolor;
		title.style.filter="progid:DXImageTransform.Microsoft.Alpha(startX=20, startY=20, finishX=100, finishY=100,style=1,opacity=75,finishOpacity=100);";
		title.style.opacity="0.75";
		title.style.border="1px solid " + bordercolor;
		title.style.height="18px";
		title.style.width="100%";
		title.style.font="12px Verdana, Geneva, Arial, Helvetica, sans-serif";
		title.style.color="#FF0000";
		title.style.cursor="pointer";
		title.innerHTML="关闭";
		title.onclick=function()
		{
			document.body.removeChild(bgObj);
			document.getElementById("msgDiv").removeChild(title);
			document.body.removeChild(msgObj);
		}
		document.body.appendChild(msgObj);
		document.getElementById("msgDiv").appendChild(title);
		var txt=document.createElement("p");
		txt.style.margin="1em 0"
		txt.setAttribute("id","msgTxt");
		txt.innerHTML=str;
		document.getElementById("msgDiv").appendChild(txt);
}
function sRemove()
{
		document.body.removeChild(bgObj);
		document.getElementById("msgDiv").removeChild(title);
		document.body.removeChild(msgObj);
}

function selectAll() 
{
    var csh = window['SELECTBOX'].length;
	if (window['SELECTALL'].checked)
	{
		for (var i = 0; i < csh ; i++)
		{
			 window['SELECTBOX'][i].checked = true;
		}
	}else
	{
		for (var i = 0; i < csh ; i++)
		{
			 window['SELECTBOX'][i].checked = false;
		}
	}
}
function seledtSingel() 
{
	var theSelAllMark = "Y";
    var csh = window['SELECTBOX'].length;
	for (var i = 0; i < csh ; i++)
	{
		if (!window['SELECTBOX'][i].checked)
		{
			theSelAllMark = "N";
			i = csh;
		}
	}
	if (theSelAllMark == 'Y')
	{
		window['SELECTALL'].checked = true;
	}else
	{
		window['SELECTALL'].checked = false;
	}
}
function delSel() 
{
	var csh = window['SELECTBOX'].length;
	var theUpNe = "";
	for (var i = 0; i < csh - 1 ; i++)
	{
		if (window['SELECTBOX'][i].checked)
		{
			theUpNe = theUpNe + window['SELECTBOX'][i].value + ";";
		}
	}
	if (theUpNe == "")
	{
		sAlert('您尚未选择要删除的项目');
		return;
	}
	var cheJsonObj = 
	{
		"FUN":"DELMORE",
		"PAGE_SIZE": PAGE_SIZE,
		"PAGE_NUM": mySlider.getValue(),
		"DEL_DATASET": theUpNe
	};
    doPost("JSONExample.jsp",cheJsonObj,parseDataList);
}

function hiddenDiv(theDivId) 
{
	var thehiddenDiv = document.getElementById(theDivId);
	thehiddenDiv.style.display = "none";
}
function showDiv(theDivId) 
{
	var thehiddenDiv = document.getElementById(theDivId);
	thehiddenDiv.style.display = "";
}

function setCok(name,value)//两个参数，一个是cookie的名子，一个是值 
{ 
	var Days = 365; //此 cookie 将被保存 365 天 
	var exp = new Date(); //new Date("December 31, 9998"); 
	exp.setTime(exp.getTime() + Days*24*60*60*1000); 
	document.cookie = name + "="+ value + ";expires=" + exp.toGMTString(); 
}
function delCok(name)
{
	var exp = new Date();
	exp.setTime(exp.getTime() - 1);
	document.cookie= name + "=ERR;expires="+exp.toGMTString();
}

function getCok(name)
{ 
	var cName = name + "="; 
	var dc = document.cookie; 
	dc = unescape(dc);
	if (dc.length>0)
	{ 
		begin = dc.indexOf(cName); 
		if (begin != -1)
		{ 
			begin += cName.length; 
			end = dc.indexOf(";", begin); 
			if (end == -1) end = dc.length; 
			return (dc.substring(begin,end)); 
		} 
	} 
	return null; 
} 
function getPortalet(colpathinfo,i)
{
	var cntInnerHtml=document.getElementById("CNT_"+colpathinfo);
	cntInnerHtml.innerHTML = "内容获取中，请稍候...";
	//var time;
	tims = window.setTimeout("getPortaletContent('"+colpathinfo+"')",i*1000);
	//getPortaletContent(colpathinfo);
}
