var old_bg = "";
function inStyle(obj)
{
    old_bg = obj.style.background;
    if(!old_bg)old_bg = obj.style.backgroundColor;
	if(!old_bg)old_bg = obj.getAttribute("bgcolor");
	obj.style.background = "#FFFF66";
}
function outStyle(obj)
{
    obj.style.background = old_bg;
}

function selectAll(form,obj,targetName){
	for(i=0;i<form.length;i++){
	   if(form[i].name.indexOf(targetName)!=-1){
		   if(obj.checked) form[i].checked=true;
		   else form[i].checked=false;
	   }
	}
}

function ClearOptions(SelectObjectId)
{
	var SelectObject = typeof(SelectObjectId) == 'object'? SelectObjectId : document.getElementById(SelectObjectId);
	while(SelectObject.options.length > 0) SelectObject.options[0] = null;
}

function isDomain(str){
  if(/^[0-9a-z\.]+[0-9a-z\-0-9a-z]+[\.]+[0-9a-z]+$/.test(str)==false) return false;
  else return true;
}

function isEmail(s){
	var regx = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
	return regx.test(s);
}

function isChinese(s){
	var rxp = /^[\u4e00-\u9fa5]+$/g;
	return rxp.test(s);
}

function $e() {
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string') element = document.getElementById(element);		
		if (arguments.length == 1) return element;
		elements.push(element);
	}
	
	return elements;
}

function ReplaceSpecialChars(input){
	var SpecialChars = "１２３４５６７８９０－ＱＷＥＲＴＹＵＩＯＰＡＳＤＦＧＨＪＫＬＬＺＸＣＶＢＮＭ";
	var replacement = "1234567890-qwertyuiopasdfghjklzxcvbnm";
	
	for(var i = 0;i < input.length; i++){
		var charTemp = input.charAt(i);
		for(var j=0;j<SpecialChars.length;j++){
			var charTempS = SpecialChars.charAt(j);
			if(charTemp == charTempS){
				var charTempR = replacement.charAt(j);
				input = input.replace(charTempS,charTempR);
			}
		}
	}
	return input;
}

function hideElement(eid){
	var e = $e(eid);
	if(e){
		e.style.display = 'none';
	}
}

function ComboBox(InputBox){
	var inputs = null;
	var comboboxs = new Array;
	if(InputBox){
		inputs = document.getElementById(InputBox);
		comboboxs.push(inputs);
	}else{
		inputs = document.getElementsByTagName('INPUT');
		for(var i=0;i<inputs.length;i++){
			var item = inputs[i];
			if(item.type == 'text' && item.getAttribute('typex') == 'combobox')
				comboboxs[comboboxs.length] = item;
		}
	}
	if(comboboxs.length>0){
		for(var i=0;i<comboboxs.length;i++){
			var combobox = comboboxs[i];
			var items = combobox.getAttribute('items');
			if (items != null) {
				items = items.replace(/(^s*)|(s*$)/g, "");
				if (items != '') {
					var arritem = items.split(',');
					var selcon = document.createElement('select');
					selcon.options.add(document.createElement("option"));
					for (var j = 0; j < arritem.length; j++) {
						var oOption = document.createElement("option");
						var text = arritem[j];
						if (text != '') 
							text = text.replace(/(^s*)|(s*$)/g, "");
						oOption.text = text;
						oOption.value = text;
						selcon.options.add(oOption);
					}
					selcon.style.position = 'absolute';
					selcon.style.width = combobox.clientWidth + 20 + 'px';
					selcon.style.clip = 'rect(auto auto auto ' + combobox.clientWidth + 'px)';
					if(document.all && window.external)selcon.style.marginTop = '1px';
					selcon.onchange = function(){
						var input = this.nextSibling;
						input.value = this.value;
						input.select();
						input.focus();
					}
					var parent = combobox.parentNode;
					parent.insertBefore(selcon, combobox);
				}
			}            
		}
	}
};

function DropDown(inputbox,rowCellLen){
	if(typeof(inputbox) == 'string') inputbox = $e(inputbox);
	var dropdownId = inputbox.id + '_dropdown';
	if($e(dropdownId)){
		$e(dropdownId).style.display = 'block';
		hideControl("SELECT,OBJECT",dropdownId);
		return;
	}
	var div = document.createElement('DIV');
	div.id = dropdownId;
	div.style.position = 'absolute';
	div.style.top = getOffsetTop(inputbox) + inputbox.clientHeight + 3;
	div.style.width = inputbox.clientWidth + 2 + 'px';
	div.style.background = '#E5F3FF';
	div.style.border = "1px solid #7F9DB9";
	//div.style.borderTop = "none";
	var items = inputbox.getAttribute('items');
	var arritem = items.split(',');
	var table = "<table width='100%' border='0' cellpadding='3' cellspacing='1'><tr>";
	var rowCharCount = 0;
	var rowCellCount = 0;
	for (var j = 0; j < arritem.length; j++){
		table += "<td nowrap><a href=\"javascript:void($e('"+ inputbox.id +"').value = '"+ arritem[j] +"')\"><span style='font-size:12px'>"+ arritem[j] +"</span></a></td>";
		rowCharCount += arritem[j].length + 1;
		rowCellCount ++;
		if(rowCellLen){
			if(rowCellCount == rowCellLen){
				table += "</tr><tr>";
				rowCellCount = 0;
			}
		}else{
			if(rowCharCount * 12 > inputbox.clientWidth - 10){
				table += "</tr><tr>";
				rowCharCount = 0;
			}
		}
	}
	table += "</tr><table>";
	div.innerHTML = table;
	hideControl("SELECT,OBJECT",div);
	var parent = inputbox.parentNode;
	parent.insertBefore(div, inputbox);
	AttachEventEx(document.body,"onclick",function(){ if(event.srcElement.id != inputbox.id){ $e(dropdownId).style.display = 'none'; showControl(dropdownId);} });
};

//trim()
String.prototype.trim = function() {
	return (this.replace(/^\s+|\s+$/g,""));
}
//ltrim()
String.prototype.ltrim = function() {
	return (this.replace(/^\s*/,""));
}
//rtrim()
String.prototype.rtrim = function() {
	return (this.replace(/\s*$/,""));
}
//去掉HTML标记
String.prototype.stripTags = function() {
	return this.replace(/<\/?[^>]+>/gi, '');
}

function trimString(str)
{
   str=str.replace(/^ /g,"");
   str=str.replace(/ $/g,"");
   return str;
}
function checkMail(email)
{
	var pattern = /\S+@\S+/;
	return pattern.test(email);
}
function checkzip(str)
{
    var reg = /^[0-9]{6}$/;
    return (reg.test(str));
}

function showTips(title,text,width,height,showNeverShow,cookieName){
	if(getCookie(cookieName) == '1') return;
	var pannel = document.createElement("DIV");
	width = width ? width : 300;
	height = height ? height : 200;
	var pageWidth = document.body.clientWidth;
	var pageHeight = document.body.clientHeight;
	var DivId = GetRandomLetters(3);
	pannel.id = DivId;
	pannel.style.width = width;
	//pannel.style.height = height;
	pannel.style.background = "#F7F7F7";
	pannel.style.position = "absolute";
	pannel.style.pixelLeft = pageWidth/2 - (width/2);
	pannel.style.pixelTop = pageHeight/2 + document.body.scrollTop - (height/2);
	pannel.style.border = "1px solid #1F3B63";
	var titlebg = "#1F3B63";
	var titlecolor = "#FFFFFF";
	var html = "<table width='100%' height='100%' cellpadding='5' cellspacing='0' border='0'>";
		html += "<tr height='20' bgcolor='"+ titlebg +"'><td style='color:"+ titlecolor +";font-weight:bold;' nowrap>" + title + "</td><tr>";
		html += "<tr><td align='center' colspan='2' height='"+ height +"'>" + text + "</td><tr>";
		html += "<tr height='20'><td colspan='2' align='center' style='font-size:14px;'>";
		html += "<hr size='1' width='95%' color='#CCCCCC'>";
		if(showNeverShow){
			html += "&nbsp;<input type='checkbox' id='TipsNeverShow' onclick=\"if(this.checked){SetCookie('"+ cookieName +"','1')}else{SetCookie('"+ cookieName +"','0')}\"><label for='TipsNeverShow'><span style='color:red;font-size:12px'><b>不再提示</b></span></label>&nbsp;";
		}
		html += " <input type='button' value='确 定' onclick=\"removeElement('" + DivId + "')\">";
		html += "</td>";
		html += "</tr></table>";
	pannel.innerHTML = html;
	//document.body.appendChild(pannel);
	document.write(pannel.outerHTML);
	return pannel;
}

function webRequest(url,xmlstr,method,isAsync,callback,callbackargs)
{
    var xmlhttp = null;
	if (window.ActiveXObject) {
       xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }else if (window.XMLHttpRequest) {
       xmlhttp = new XMLHttpRequest();
    }
	if(!method) method = "GET";
	if(!isAsync) isAsync = false;
	if(isAsync && callback){
		var args = new Array();
		args.push(xmlhttp);
		if(typeof(callbackargs) == 'object'){
			args = args.concat(callbackargs);
		}
		xmlhttp.onreadystatechange = function(){callback.apply(null,args);}
	}
	xmlhttp.Open(method,url,isAsync);
    if(method.toLowerCase() == 'post'){
		xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	}
    xmlhttp.Send(xmlstr);
	if(!isAsync){
		return xmlhttp.responseText;
	}
}

function GetRandomLetters(length)
{
	var str="";
	for(i=0;i<length;i++){
		var rnd=parseInt((122-97+1)*Math.random()+97);
	    var s=String.fromCharCode(rnd);
		str+=s;
	}
	return str;
}
function removeElement(elementId){
    var element = document.getElementById(elementId);
	if(element) element.parentElement.removeChild(element);
}

function getOffsetLeft(src){
	var set=0;
	if(src)
	{
		if (src.offsetParent)
			set+=src.offsetLeft+getOffsetLeft(src.offsetParent);
		
		if(src.tagName!="BODY")
		{
			var x=parseInt(src.scrollLeft,10);
			if(!isNaN(x))
				set-=x;
		}
	}
	return set;
}
function getOffsetTop(src){
	var set=0;
	if(src)
	{
		if (src.offsetParent)
			set+=src.offsetTop+getOffsetTop(src.offsetParent);
		
		if(src.tagName!="BODY")
		{
			var y=parseInt(src.scrollTop,10);
			if(!isNaN(y))
				set-=y;
		}
	}
	return set;
}

function IfNumber(iV) //每次输入都要检查是否合法的数字
{
	var newPar=/^(-|\+)?\d+(\.\d+)?$/;
	aa=iV+String.fromCharCode(event.keyCode)+'0';
	event.returnValue =newPar.test(aa);
}

function IfNumber2(iV) //每次输入都要检查是否合法的数字
{
	var newPar=/^(-|\+)?\d+?$/;
	aa=iV+String.fromCharCode(event.keyCode)
	event.returnValue =newPar.test(aa);
}

//* 为对象绑定事件
function AttachEventEx(eid,env,action)
{
	var object = typeof(eid)=='object'?eid:document.getElementById(eid);
	if (object && object.addEventListener){
		object.addEventListener(env,action,true);
	}else if(object){
		object.attachEvent(env,action);
	}
}

//** Tips
function AddTips(s){
	//if( s == '') return;
	var args = arguments;
	var id,position,width,height;
	if(args[1]) id = args[1];
	if(args[2]) position = args[2]; //显示位置,有 top bottom left right mouse(鼠标跟随) 等多种属性
	if(args[3]) width = args[3];
	if(args[4]) height = args[4];
	if(!id) id = 'dvTips';
	var dvTips = document.getElementById(id);
	if(!dvTips){
		dvTips = document.createElement("DIV");
		dvTips.id = id;
        dvTips.style.border = '1px solid #F2BB6F';
		dvTips.style.background = '#FDF7E7';
		dvTips.style.position = 'absolute';
		dvTips.style.padding = '5px';
		if(width) dvTips.style.width = width;
		if(height) dvTips.style.height = height;
		document.body.appendChild(dvTips);
	}
    //dvTips.innerHTML = "<font color='red'>"+ s +"</red>";
	if(s != '') dvTips.innerHTML = s;
	if(position == 'top'){
		dvTips.style.top = getOffsetTop(event.srcElement) - ( dvTips.clientHeight + 5 );
		dvTips.style.left = getOffsetLeft(event.srcElement);
	}else if(position == 'bottom'){
		dvTips.style.top = getOffsetTop(event.srcElement) + ( event.srcElement.clientHeight + 5 );
		dvTips.style.left = getOffsetLeft(event.srcElement);
	}else if(position == 'left'){
		dvTips.style.top = getOffsetTop(event.srcElement);
		dvTips.style.left = getOffsetLeft(event.srcElement) - ( dvTips.clientWidth + 5 );
	}else if(position == 'right'){
		dvTips.style.top = getOffsetTop(event.srcElement);
		dvTips.style.left = getOffsetLeft(event.srcElement) + ( event.srcElement.clientWidth + 5 );
	}else{
		dvTips.style.left = event.clientX + 5 + document.body.scrollLeft;
		dvTips.style.top = event.clientY + 5 + document.body.scrollTop;
	}
	dvTips.style.display = '';
}

function RemoveTips(){
	var args = arguments;
	var id,delay;
	if(args[0]) id = args[0];
	if(typeof(args[1]) != 'undefined') delay = args[1];
	if(!id) id = 'dvTips';
	var dvTips = document.getElementById(id);
	if(dvTips){
		if(typeof(delay) == 'undefined' || delay == true) setTimeoutEx(function(){dvTips.style.display = 'none'},200);
		else dvTips.style.display = 'none';
	}
}

//重载 setTimeout
var _st = window.setTimeout;
window.setTimeoutEx = function(fRef, mDelay) 
{ 
	if(typeof(fRef) == 'function')
	{ 
		var argu = Array.prototype.slice.call(arguments,2);
		var f = (function(){ fRef.apply(null, argu);});
		return _st(f, mDelay);
	}
	return _st(fRef,mDelay);
}
// end of 重载 setTimeout

//重载 setInterval
var _setInterval = window.setInterval;
window.setIntervalEx = function(fRef, mDelay) 
{ 
	if(typeof(fRef) == 'function')
	{ 
		var argu = Array.prototype.slice.call(arguments,2);
		var f = (function(){ fRef.apply(null, argu);});
		return _setInterval(f, mDelay);
	}
	return _setInterval(fRef,mDelay);
}
// end of 重载 setInterval

//* 拖动层所须的代码
var moveobj = null;
var px = 0;
var py = 0;
var screenTop = 0;
var screenLeft = 0;
var moveable = false;
document.onmouseup = EndMove;
document.onmousemove = MouseMove;
function InitMove(ObjId){
	moveable = true;
	moveobj = document.getElementById(ObjId);
	px = event.x - moveobj.style.pixelLeft;
	py = event.y - moveobj.style.pixelTop;
} 

function MouseMove(){
	if(moveobj != null){		
		moveobj.style.left = event.x - px;
		moveobj.style.top = event.y - py;
	}
}

function EndMove(){
	moveable = false;
	moveobj = null;
}
// end of 拖动层所须的代码

/*
  Cookie 相关函数
*/
function getCookieVal (offset) 
{
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1) endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}


function getCookie (name) 
{
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) 
	{
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg) return getCookieVal (j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break; 
	}
	return null;
}

function SetCookie (name, value) 
{
	var argv = SetCookie.arguments;
	var argc = SetCookie.arguments.length;
	var expires = (2 < argc) ? argv[2] : null;
	var path = (3 < argc) ? argv[3] : null;
	var domain = (4 < argc) ? argv[4] : null;
	var secure = (5 < argc) ? argv[5] : false;
	document.cookie = name + "=" + escape (value) +
	                 ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
	                 ((path == null) ? "" : ("; path=" + path)) +
	                 ((domain == null) ? "" : ("; domain=" + domain)) +
	                 ((secure == true) ? "; secure" : "");
}

/*
var expdate = new Date();
expdate.setTime(expdate.getTime() +  (24 * 60 * 60 * 1000 * 365)); //一年后过期
SetCookie("WebTongCid", WebTongCid, expdate ,"/");
*/

//** end of Cookie 相关函数

//随机生成指定长度的小写英文字母字符串
function getRndString(len)
{
    var ret = "";
    while(ret.length < len){
        var i = Math.random();
        var ii = (122 - 97 + 1) * i + 97;
        ii = parseInt(ii);
        ret += String.fromCharCode(ii);
    }
	return ret;
}

//** 提示框
var oPopup;
var PopupLen;
var PopupWidth = 200;
var PopupHeight = 100;
var PopupTimer;
var PopupPosLeft = screen.width;
var PopupPosTop = screen.height;
function InitMsgBox(Msg)
{ 
	PopupLen = 0;
	oPopup   = window.createPopup();
	var oPopupBody = oPopup.document.body;
	oPopupBody.style.border ="solid black 1px";
	var titleContent = "";
	titleContent = titleContent + "<table cellPadding='5' bgcolor='#eeeeee' width='100%' height='100%' border=0 cellspacing=0 cellpadding=0>";
	titleContent = titleContent + "<tr><td align=center><font color='red' style='font-size:12px'><b>客户通 消息提示</b></font></td></tr>";
	titleContent = titleContent + "<tr><td style='font-size:12px'>"+ Msg +"</tr>";
	titleContent = titleContent + "</table>";    
	oPopupBody.innerHTML = titleContent;
	ShowMsgBox();
}
      
function MsgBox()
{ 
	PopupLen += 4;      
	if (PopupLen > PopupHeight)
	{   
		window.clearInterval(PopupTimer);     
	}        
	else
	{
		//oPopup.show(document.body.clientWidth - PopupWidth, document.body.clientHeight - PopupLen, PopupWidth, PopupLen, document.body); 
	    //oPopup.show(screen.width - PopupWidth, screen.height - PopupLen, PopupWidth, PopupLen, document.body); 
		oPopup.show(PopupPosLeft - PopupWidth, PopupPosTop - PopupLen, PopupWidth, PopupLen, document.body);
	}    
}
   
function ShowMsgBox()
{
	PopupTimer = window.setInterval("MsgBox()",15);    
}
//** end of 提示框


//** 隐藏指定控件
function hideControl(tagName, overCtrl)
{
	if(typeof(overCtrl) == 'string') overCtrl = $e(overCtrl);
	var x = getOffsetLeft(overCtrl);
	var y = getOffsetTop(overCtrl);
	var w = overCtrl.offsetWidth;
	var h = overCtrl.offsetHeight;
	if (!overCtrl.overlap) overCtrl.overlap = new Array ();
	var arTags = tagName.split(",");
	for(var j = 0;j < arTags.length;j++){
		var hideTags = document.getElementsByTagName(arTags[j]);
		for (var i = 0; i < hideTags.length; ++i)
		{
			var obj = hideTags[i];
			if (!obj || !obj.offsetParent)
				continue;

			var ox = getOffsetLeft(obj);
			var oy = getOffsetTop(obj);
			var ow = obj.offsetWidth;
			var oh = obj.offsetHeight;

			if (ox > (x + w) || (ox + ow) < x)
				continue;
			if (oy > (y + h) || (oy + oh) < y)
				continue;

			if(obj.style.visibility == "hidden")
				continue;

			overCtrl.overlap[overCtrl.overlap.length] = obj;
			obj.style.visibility = "hidden";
		}
	}
}

function showControl(overCtrl)
{
	if(typeof(overCtrl) == 'string') overCtrl = $e(overCtrl);
	if (overCtrl.overlap)
	{
		var i;
		for (i = 0; i < overCtrl.overlap.length; ++i)
			overCtrl.overlap[i].style.visibility = "";
	}
	overCtrl.overlap = null;
}

//** end of 隐藏指定控件

//---------------------------------------------------
//	功能：
//		对齐两个表的各列，主要用于实现分开表头和表体的对齐
//	参数：
//		oHeader		用作表头的表对象
//		oBody		用作表体的表对象
//		iNums		如果一次不能对齐，则循环对齐循环次数
//
//	返回：
//		无
//---------------------------------------------------
function AlignTHeaderTBody(oHeader,oBody,iNums)
{	
	if(iNums<1 || iNums>5) iNums = 5;
	if(oHeader == null || oBody == null)return;
	
	if(oHeader.rows.length<1 || oBody.rows.length<1)return;
	if(oHeader.rows(0).cells.length<1 || oBody.rows(0).cells.length<1)return;
	
	var iCount = 0;
	var pRowHeader = oHeader.rows(0);
	var pRowBody = oBody.rows(0);	
	var iLen = pRowHeader.cells.length;
	if(iLen > pRowBody.cells.length)iLen = pRowBody.cells.length;
	
	var bExit = false;
	while(iCount++ < iNums && !bExit)
	{
		bExit = true
		for(var i=iLen-1;i>-1;i--)
			pRowHeader.cells(i).style.width = pRowBody.cells(i).offsetWidth;
		for(var i=0;i<iLen;i++)
			pRowBody.cells(i).style.width = pRowHeader.cells(i).offsetWidth;
		for(var i=iLen-1;i>-1;i--)
			if(pRowHeader.cells(i).offsetWidth != pRowBody.cells(i).offsetWidth)
				bExit = false;									
	}	
	oHeader.style.left = oBody.offsetLeft;
	oBody.style.left = oHeader.offsetLeft;
	if(oHeader.offsetWidth > oBody.offsetWidth)
		oBody.style.width = oHeader.offsetLeft;
	else
		oHeader.style.width = oBody.offsetLeft;
}
//-----------------------------End-----------------------------