﻿/** must same with util.js.*/
// js utilities

//make select row css onclick
function doOnClickRow(rowElm) {
	doOnClickRow2(rowElm, '_id0:rowIndex','tRow', true);
}
//make select row css onclick
//para:: 
//* rowElm:row element;
//* indexElmId:JSF rowIndex InputHidden element'Id;
//* rowIdPrefix: Prefix of row id;
//* noEvenRowCSS: if no EvenRowCSS
function doOnClickRow2(rowElm, indexElmId, rowIdPrefix, noEvenRowCSS) {
  if(noEvenRowCSS || EF(indexElmId)%2==0)
		E(rowIdPrefix + EF(indexElmId)).className= 'oddRow';
	else
		E(rowIdPrefix + EF(indexElmId)).className= 'evenRow';
		
	E(indexElmId).value= (rowElm.id).substring(rowIdPrefix.length);
	rowElm.className='selectRow';
}

function initRowCSS() {
	initRowCSS2('_id0:rowIndex','tRow');
}
function initRowCSS2(indexElmId, rowIdPrefix) {
	for(var i=0; i<100; i++){
		if(typeof (E(rowIdPrefix + i)) == undefined )
			break;
		if(i == EF(indexElmId) )
			E(rowIdPrefix + i).className= 'selectRow';
		else
			E(rowIdPrefix + i).className= 'oddRow';
	}	
 	//E(rowIdPrefix + '0').className= 'oddRow';
	//E(rowIdPrefix + EF(indexElmId)).className= 'selectRow';
}
 
//getElementById. $
function E(elementId) {
	return document.getElementById(elementId);
}
//get Element value By Element Id. $f
function EF(elementId) {
	return document.getElementById(elementId).value;
}

// test if the point in the element's region
function inRegion(x, y, element) {
	if (element != undefined && element != null) {
		if (x >= getAbsLeft(element) && y >= getAbsTop(element)) {
			if (x <= getAbsLeft(element) + element.offsetWidth) {
				if (y <= getAbsTop(element) + element.offsetHeight) {
					return true;
				}
			}
		}
	}
	return false;
}
// test if the element cross with another element
/*function inRegion(element1,element2) {
	if(element1!= undefined && element1!= null && element2!= undefined && element2!= null)
	if( x>= getAbsLeft(element) && y>= getAbsTop(element) )
		if( x<= getAbsLeft(element)+ element.offsetWidth)
			if(y<= getAbsTop(element)+ element.offsetHeight)
				return true;
	return false
}*/
//rm the item from array.  return newArr
function rmArrayItemByV0(arr, v0) {
	newArr = new Array(arr.length - 1);
	if (!arr) {
		return arr;
	}
	t = 0;
	for (iii = 0; iii < arr.length; iii += 1) {
		if (v0 == arr[iii][0]) {
			t = 1;
		} else {
			newArr[iii - t] = arr[iii];
		}
	}
	arr = newArr;
	return newArr;
}

//rm the item from array.  return newArr
function rmArrayItemByObjId(arr, id) {
	if (!arr) {
		return arr;
	}
	t = 0;
	newArr = new Array(arr.length - 1);
	for (iii = 0; iii < arr.length; iii += 1) {
		if (id == arr[iii].id) {
			t = 1;
		} else {
			newArr[iii - t] = arr[iii];
		}
	}
	arr = newArr;
	return newArr;
}
//search index by ObjId, return the item's index; -1 if error.
function getArrayIdxByObjId(arr, id) {
	if (!arr) {
		return -1;
	}
	for (var i = 0; i < arr.length; i++) {
		if (arr[i].id == id) {
			return i;
		}
	}
	return -1;
}
//search item by ObjId, return the item obj
function getArrayItemByObjId(arr, id) {
	if (!arr) {
		return null;
	}
	for (i = 0; i < arr.length; i++) {
		if (arr[i].id == id) {
			return arr[i];
		}
	}
	return "";
}
//search item by v0, return v1
function getArrayItemV1(arr, v0) {
	if (!arr || v0 < 0) {
		return "";
	}
	for (i = 0; i < arr.length; i++) {
		if (arr[i][0] == v0) {
			return arr[i][1];
		}
	}
	return "";
}

//search item by v0, return the index except for -1
function getArrayItemIndex(arr, v0) {
	if (!arr) {
		return -1;
	}
	for (i = 0; i < arr.length; i++) {
		if (arr[i][0] == v0) {
			return i;
		}
	}
	return -1;
}
//confirm Delete operation for JSF
function confirmDel() {
	if (confirm("\u5220\u9664\u540e\u6570\u636e\u4e0d\u80fd\u6062\u590d,\u4f60\u786e\u8ba4\u8981\u5220\u9664\uff1f")) {
		return true;
	}
	return false;
}


//confirm Clear items operation for JSF
function confirmClear() {
	if (confirm("\u786e\u8ba4\u8981\u6e05\u7a7a\uff1f")) {
		return true;
	}
	return false;
}

function isSucNaming(str){
	//return false;
	//var regNaming=/^([\w\-]|[^x00-xff])$/;
	var regNaming=/^([\w\-]|[^x00-xff]){2,16}$/;
	//var regNaming=/^([a-zA-Z][a-zA-Z0-9_]|[^x00-xff]){2,16}$/;
	if(regNaming.test(str)){
		var strLength=str.replace(/[^\x00-\xff]/g,'**').length;
		//alert(strLength);
		if(strLength<4||strLength>16)
			return false;
		return true;       	
    }else 
    	return false;
}

/**已不用此方法*/
function checkPasswd(theForm, emptyOK) {
	var newPasswd=$("_id0:newPasswd").value;
	var nickName=$("_id0:nickname").value;
	var confirmPasswd=$("confirmPasswd").value;
	//for (i = 0; i < theForm.length; i++) {
		//en = theForm.elements[i].name;
		//if (en == "_id0:nickname") {
			//nickName = trim(theForm.elements[i].value);
		//}
		//if (en == "_id0:newPasswd") {
			//newPasswd = trim(theForm.elements[i].value);
		//}	
	//}
	//alert("newPasswd"+newPasswd);
	if(isEmpty(nickName)){
		alert("昵称不能为空！");
		return false;
	}
	if(isEmpty(newPasswd)){
		alert("密码不能为空！");
		return false;
	}
	if(isEmpty(confirmPasswd)){
		alert("确定密码不能为空！");
		return false;
	}
	//alert(nickName+isSucNaming(nickName));
	
	if(isSucNaming(nickName)==false){
		//if()
		alert("昵称只允许数字，字母，下划线或中文，长度为2-16个字符。");
		return false;
	}
	//return false;
	confirmPasswd = trim(theForm.confirmPasswd.value);
	if ((emptyOK == "true") && isEmpty(newPasswd)) {
		return true;
	} else {
		if (newPasswd.length < 4 || newPasswd.length > 16) {
			alert("\u5bc6\u7801\u9519\u8bef\uff01\u5bc6\u7801\u957f\u5ea6\u5fc5\u987b\u5927\u4e8e4\uff0c\u5c0f\u4e8e16\uff01");
			return false;
		}
	}
	if (newPasswd.indexOf(confirmPasswd) >= 0 && confirmPasswd.indexOf(newPasswd) >= 0) {
		return true;
	}
	alert("\u5bc6\u7801\u9519\u8bef\uff01\u8bf7\u6ce8\u610f\u5bc6\u7801\u4e0d\u80fd\u4e3a\u7a7a\uff0c\u800c\u4e14\u4e24\u6b21\u8f93\u5165\u7684\u5bc6\u7801\u5fc5\u987b\u76f8\u540c\uff01");
	return false;
}
function fullScreen(url) {
	fullScreen2(url, "");
}
function fullScreen(url, title) {
	var w = screen.availWidth;
	var h = screen.availHeight;
	var fsWindow = window.open(url, title, "left=0,top=0,width=" + w + ",height=" + h + ",scrollbars=1,fullscreen=yes");
}
function openMaxWindow(url, title) {
	var w = screen.availWidth;
	var h = screen.availHeight;
	var fsWindow = window.open(url, title, "left=0,top=0,width=" + w + ",height=" + h + ",scrollbars=1,menubar=false,status=false,toolbar=false");
}

//  getAbsLeft(document.all.divID);   //left
function getAbsLeft(e) {
	l = e.offsetLeft;
	while (e = e.offsetParent) {
		l += e.offsetLeft;
	}
	return l;
} 
//  getAbsTop(document.all.divID);     //top
function getAbsTop(e) {
	t = e.offsetTop;
	while (e = e.offsetParent) {
		t += e.offsetTop;
	}
	return t;
} 

//String.prototype.ReplaceAll = stringReplaceAll;
function  replaceAllStr(orgText,AFindText,ARepText){
  raRegExp = new RegExp(AFindText,"g");
  return orgText.replace(raRegExp,ARepText)
}

// 名称:全替换函数
// 功能:把指定的文本中的所有的指定的旧字符替换成指定的新字符
// 返回:已经替换的文本
// 参数:text 指定的文本
// replacement 指定的旧字符
// target 指定的新字符
function replaceAll(text, replacement, target) {
	if (text == null || text == "") {
		return text;
	}//如果text无内容，返回text
	if (replacement == null || replacement == "") {
		return text;
	}//如果replacement无内容，返回text
	if (target == null) {
		target = "";
	}//如果target无内容，设置为空串
	var returnString = "";//定义返回值变量，并初始化
	var index = text.indexOf(replacement);//定义查找replacement的索引下标，并进行第一次查找
	while (index != -1) {//直至未找到replacement，要么进行下面的处理
		if (index != 0) {
			returnString += text.substring(0, index) + target;
		}//如果找到的replacement前有字符,累加到返回值中,并加上target 
		text = text.substring(index + replacement.length);//取掉找到的replacement及前边的字符
		index = text.indexOf(replacement);//进行查询，准备下一次处理
	}
	if (text != "") {
		returnString += text;
	}//如果找到的最后一个replacement后有字符，累加到返回值中
	return returnString;//返回
}
function toHTML(sStr) {
	sStr = replaceAll(sStr, "<", "&lt;");
	sStr = replaceAll(sStr, ">", "&gt;");
	sStr = replaceAll(sStr, " ", "&nbsp;");
	sStr = replaceAll(sStr, "\"", "&quot;");
	sStr = replaceAll(sStr, "\n", "<br>");
	return sStr;
}

//自动消失的消息显示框
function MyMsgDiv() {
	this.msgDiv = null;
	var left= document.body.clientWidth- 400;
//	this.msgStyle = "FONT-SIZE:12px;LEFT:expression(this.offsetParent.scrollLeft+20);COLOR:#ffffff;POSITION:absolute;TOP:expression(this.offsetParent.scrollTop+10);BACKGROUND-COLOR:#7f1ef6;";
	this.ffMsgStyle = "FONT-SIZE:12px;left:"+left+"px;COLOR:#ffffff;POSITION:absolute;TOP:10px;BACKGROUND-COLOR:#FF9900;";
	this.msgStyle= "FONT-SIZE:12px;	COLOR:#ffffff;BACKGROUND-COLOR:#FF9900;top:5px;right:200px;position:fixed;";
		
		this.createDiv = function () {
		this.msgDiv = document.createElement("div");
		//window.XMLHttpRequest
		if (window.navigator.userAgent.indexOf("MSIE")>= 1) {
			this.msgDiv.style.cssText = this.ffMsgStyle;
		} else {
			this.msgDiv.style.cssText = this.msgStyle;
		}
		document.body.insertBefore(this.msgDiv, document.body.childNodes[0]);
	};
	this.Show = function (msgstr) {
		this.msgDiv.innerHTML += "<div>" + msg + "</div>";
	};
	this.createDiv();  //放置此处是为了防止以下的md值为null 
	var closeTimer;//,btmTimer
	var md = this.msgDiv;
	
	this.Show = function (msg, showtime,icon) {
		if(md.innerHTML.length >100)
			md.innerHTML = "";
		if(icon==null || icon.length==0)
			md.innerHTML = "<div>" + msg + "</div>"+ md.innerHTML;
		else
			md.innerHTML = "<div><img src='"+icon+"'></img>" + msg + "</div>"+ md.innerHTML;
		
		md.style.visibility = "visible";
		/*
        if(btmTimer)window.clearTimeout(btmTimer);
			btm=0;
			if(showtime>4)
btm= showtime-2;
else if(showtime>2)
btm= showtime-1;
if(btm>0)
        btmTimer=setTimeout(this.Translucence,(showtime-1)*1000);
*/
		if (closeTimer) {
			window.clearTimeout(closeTimer);
		}
		closeTimer = setTimeout(this.Hidden, showtime * 1000);
	};
	/*
    this.Translucence = function()
    {
        //TODO改颜色以实现半透明效果
			md.innerHTML += " 半透明";
		md.style.background="#333333";
		md.style.color="#ff0000";
	//	md.style.filter.alpha.opacity=50;
//md.className='halfAlpha';
    }
*/
	this.Hidden = function () {
		md.innerHTML = "";
		md.style.visibility = "hidden";
	};
}
//显示消息
//参数：  msg:消息；showSec:显示秒数
function showMsg(msg, showSec,icon) {
	if(typeof(MY_MSG_DIV)== 'undefined' || MY_MSG_DIV== null)
		MY_MSG_DIV = new MyMsgDiv();
	MY_MSG_DIV.Show(msg, showSec,icon);
}

function writeCookie2(name, value, hours, domain)
{
  var expire = "";
  if(hours != null)
  {
    expire = new Date((new Date()).getTime() + hours * 3600000);
    expire = "; expires=" + expire.toGMTString();
  }
  document.cookie = name + "=" + escape(value) + expire+ "; domain="+domain;
} 

function writeCookie(name, value, hours)
{
  var expire = "";
  if(hours != null)
  {
    expire = new Date((new Date()).getTime() + hours * 3600000);
    expire = "; expires=" + expire.toGMTString();
  }
  document.cookie = name + "=" + escape(value) + expire;
} 
function readCookie(name)
{
  var cookieValue = "";
  var search = name + "=";
  if(document.cookie.length > 0)
  { 
    offset = document.cookie.indexOf(search);
    if (offset != -1)
    { 
      offset += search.length;
      end = document.cookie.indexOf(";", offset);
      if (end == -1) end = document.cookie.length;
      cookieValue = unescape(document.cookie.substring(offset, end))
    }
  }
  return cookieValue;
}

//firefoe 英文换行 intLen 多少字符换行 objId 换行的id 
function toBreakWord(intLen,objId){
	var oBrowser = new detectBrowser();
	if(oBrowser.isFF){
		var obj=document.getElementById(objId);
		var strContent=obj.innerHTML;  
		var strTemp="";
		while(strContent.length>intLen){
			strTemp+=strContent.substr(0,intLen)+"<br>";  
			strContent=strContent.substr(intLen,strContent.length);  
		}
		strTemp+="<br>"+strContent;
		obj.innerHTML=strTemp;
	}
	
}

function detectBrowser(){
		//用法: var oBrowser = new detectBrowser();oBrowser.isIE	
		var sAgent = navigator.userAgent.toLowerCase();
		this.isIE = (sAgent.indexOf("msie")!=-1); //IE6.0-7
		this.isFF = (sAgent.indexOf("firefox")!=-1);//firefox
		this.isSa = (sAgent.indexOf("safari")!=-1);//safari
		this.isOp = (sAgent.indexOf("opera")!=-1);//opera
		this.isNN = (sAgent.indexOf("netscape")!=-1);//netscape
		this.isMa = this.isIE;//marthon
		this.isOther = (!this.isIE && !this.isFF && !this.isSa && !this.isOp && !this.isNN && !this.isSa);//unknown Browser
	}
function doOnclickSubmit(){
	var submitDiv=document.getElementById("submitDiv");
	submitDiv.style.display="none";	
	var waitDiv=document.getElementById("waitDiv");
	waitDiv.innerHTML="请稍候，正在提交中...<img src='/common/img/loading12.gif'>";	
}	

