/**
*javascript公用函数文件。
*/

/**
*判断字符串str是否为空或全为空格。如果为空字符串或全由空格组成，返回true,否则返回false.
*/
function isBlank(str)
{
	for(var i=0;i<str.length; i++)
		if(str.charAt(i)!=" ")
		   return false;
	return true;
}

/**
*判断表单theForm中是否有复选框被选中。如果有，返回true,否则返回false.
*请慎用！建议使用isAnyNamedBoxSelected()方法
*/
function isAnyBoxSelected(theForm)
{
	for(var i=0;i<theForm.length; i++)
		if(theForm.elements[i].type=="checkbox" && theForm.elements[i].checked)
		   return true;
	return false;
}

/**
*判断表单theForm中是否有名为Name的复选框被选中。如果有，返回true,否则返回false.
*/

function isAnyNamedBoxSelected(theForm,Name)
{
	for(var i=0;i<theForm.length; i++)
		if(theForm.elements[i].type=="checkbox" && theForm.elements[i].name==Name && theForm.elements[i].checked)
		   return true;
	return false;
}

/**
*判断表单theForm中是否有名为Name的控件被选中。如果有，返回true,否则返回false.
*/

function isAnyNamedElementSelected(theForm,Name)
{
	for(var i=0;i<theForm.length; i++)
		if(theForm.elements[i].type=="checkbox" && theForm.elements[i].name==Name && theForm.elements[i].checked)
		   return true;
		else if( theForm.elements[i].type=="hidden" && theForm.elements[i].name==Name )
			return true;
		else if( theForm.elements[i].type.indexOf("select")>=0 && theForm.elements[i].name==Name && theForm.elements[i].value!="" )
		   return true;
	return false;
}

/**
*将theBox所在表单中所有的复选框的选中状态与theBox一致。
*/
function selAllBoxes(theBox)
{
	var theForm=theBox.form;
	for(var i=0; i<theForm.length; i++)
		if(theForm.elements[i].type=="checkbox")
		    theForm.elements[i].checked=theBox.checked;
}

/**
*将theBox所在表单中所有名为Name的复选框的选中状态与theBox一致。
*/
function selectAllBoxes(theBox,Name)
{
	var theForm=theBox.form;
	for(var i=0; i<theForm.length; i++)
		if(theForm.elements[i].type=="checkbox" && theForm.elements[i].name==Name)
		    theForm.elements[i].checked=theBox.checked;
}

/**
*删除下拉列表框theSel中选中的选项。
*/
function deleteItem(theSel)
{
 var num = theSel.length;
 var i=0,j=0;
 while(i < num )
 {
	 if( !theSel.options[i].selected)
	 {
		 theSel.options[j].text = theSel.options[i].text;
		 theSel.options[j].value = theSel.options[i].value;
		 theSel.options[j].selected=false;
		 j++;
	 }
	 i++;
 }
 theSel.length = j;
}

/**
*判断字符串ItemName是否在下拉列表框theSel的选项名字中，如果在，返回true,否则返回false。
*/
function isInSelect(ItemName, theSel)
{
 var num = theSel.length;
 for( var i=0; i < num; i++)
 {
	 if( theSel.options[i].text == ItemName)
	 {
		 return true;
	 }
 }
 return false;
}

/**
*判断字符串ItemValue是否在下拉列表框theSel的选项值中，如果在，返回true,否则返回false。
*/
function isValueInSelect(ItemValue, theSel)
{
 var num = theSel.length;
 for( var i=0; i < num; i++)
 {
	 if( theSel.options[i].value == ItemValue)
	 {
		 return true;
	 }
 }
 return false;
}

/**
*将一个新的选项（名字和值）加入下拉列表框中，如果该选项已在下拉列表框中，则弹出提示对话框。
*/
function addItemToSelect(ItemName, ItemValue, theSel)
{
 var num = theSel.length;
 if( !isValueInSelect(ItemValue, theSel))
 {
	 theSel.length = num +1;
	 theSel.options[num].text = ItemName;
	 theSel.options[num].value = ItemValue;
 }
 //else
	 //alert("该项已在选项中！");
}

/**
*将下拉列表框sSel中选中的选项加到下拉列表框dSel中。
*/
function addSelectToSelect(sSel, dSel)
{
 var snum = sSel.length;
 var dnum;
 var isExists = false;
 for( var i=0; i < snum; i++)
 {
	 if( sSel.options[i].selected && !isInSelect(sSel.options[i].text, dSel))
	 {
		 dnum = dSel.length;
		 dSel.length = dnum +1;
		 dSel.options[dnum].text = sSel.options[i].text;
		 dSel.options[dnum].value = sSel.options[i].value;
	 }
 }
}

/**
*将下拉列表框sSel中选中的选项移动到下拉列表框dSel中。
*/
function moveSelectToSelect(sSel, dSel)
{
 var snum = sSel.length;
 var dnum;
 var isExists = false;
 var j=0;
 isExists=isAnyItemSelected( sSel );
 if(isExists)
{
	 for( var i=0; i < snum; i++)
	 {
		 if( sSel.options[i].selected && !isInSelect(sSel.options[i].text, dSel))
		 {
			 dnum = dSel.length;
			 dSel.length = dnum +1;
			 dSel.options[dnum].text = sSel.options[i].text;
			 dSel.options[dnum].value = sSel.options[i].value;
		 }
		 if(!sSel.options[i].selected)
		 {
			 sSel.options[j].text = sSel.options[i].text;
			 sSel.options[j].value = sSel.options[i].value;
			 sSel.options[j].selected=false;
			 j++;
		  }
	   }
	 sSel.length=j;
}
else
{
	alert("Please select the records you want to move");
}
}


/**
*下拉列表框拷贝（将下拉列表框sSel中的所有选项复制并覆盖到下拉列表框dSel中）。
*/
function copySelectToSelect(sSel, dSel)
{
 var snum = sSel.length;
 dSel.length = snum;
 for( var i=0; i < snum; i++)
 {
	 dSel.options[i].text = sSel.options[i].text;
	 dSel.options[i].value = sSel.options[i].value;
 }
}

/**
*选中下拉列表框theSel中所有的选项。
*/
function selectAllItems(theSel)
{
 var num=theSel.length;
 for(var i=0; i<num; i++)
 {
	 theSel.options[i].selected = true;
 }
}

/**
*判断下拉列表框theSel中是否有选项被选中，如果有返回true,否则返回false。
*/
function isAnyItemSelected( theSel )
{
	var num=theSel.length;
    for(var i=0; i<num; i++)
	   if( theSel.options[i].selected)
		   return true;
	return false;
}

/**
*找出表单theForm中第一个被选中的名为Name的复选框的位置。如果有，返回在表单中的位置,否则返回0.
*/
function findFirstSelectedNamedBox( theForm, Name )
{
	for(var i=0;i<theForm.length; i++)
		if(theForm.elements[i].type=="checkbox" && theForm.elements[i].name==Name && theForm.elements[i].checked)
		   return i;
	return 0;
}

/**
*判断表单theForm中是否有名为Name的单选框被选中。如果有，返回true,否则返回false.
*/
function isAnyNamedRadioSelected(theForm,Name)
{
	for(var i=0;i<theForm.length; i++)
		if(theForm.elements[i].type=="radio" && theForm.elements[i].name==Name && theForm.elements[i].checked)
		   return true;
	return false;
}

/**
*找出表单theForm中第一个被选中的名为Name的单选框的位置。如果有，返回在表单中的位置,否则返回0.
*/
function findFirstSelectedNamedRadio( theForm, Name )
{
	for(var i=0;i<theForm.length; i++)
		if(theForm.elements[i].type=="radio" && theForm.elements[i].name==Name && theForm.elements[i].checked)
		   return i;
	return 0;
}

/**
*判断表单theForm中是否有名子中包含Name的文本域有内容。如果有，返回true,否则返回false.
*/
function isAnyNamedTextFilled(theForm,Name)
{
	for(var i=0;i<theForm.length; i++)
		if( (theForm.elements[i].type=="text" || theForm.elements[i].type=="textarea") && theForm.elements[i].name.indexOf( Name )>=0 && !isBlank(theForm.elements[i].value) )
		   return true;
	return false;
}
/**
*得到文件类型
*/
function getFileType(FilePath)
{
	var i = FilePath.lastIndexOf(".");
	if( i< 0) return "";
	return FilePath.substring(i+1,FilePath.length);
}

/*
*判断某个控件的值是否为合法的数
*/
function isDigit(obj)
{
   if(obj==null)
   return false;

   var flag=true;
   var spot=0;
   var objValue=trim(obj.value);
   for(var i=0;i<objValue.length;i++)
   {
     var pstr=objValue.substring(i,i+1);
     if(pstr>=0&&pstr<=9 ||pstr==".")
	 {
       		if(pstr==".")
       		spot++;
       		continue;
        }
    else{
       		flag=false;
       		break;
       }
   } 

  if(!flag)
  {
   // alert(obj.parentNode.previousSibling.innerText+" 输入框输入了非法字符，请输入数字！")
    obj.focus();
    obj.select();
   }
  else if(spot>1)
  {
     //alert(obj.parentNode.previousSibling.innerText+" 输入框输多了小数点符号，请重输入数字！")
     flag=false;
     obj.focus();
     obj.select();
   }
   return flag;
} 

/*
*判断某个控件的值是否为合法的数,不要输出alert
*/
function isDigitNoAlert(obj)
{
   if(obj==null)
   return false;

   var flag=true;
   var spot=0;
   var objValue=trim(obj.value);
   for(var i=0;i<objValue.length;i++)
   {
     var pstr=objValue.substring(i,i+1);
     if(pstr>=0&&pstr<=9 ||pstr==".")
	 {
       		if(pstr==".")
       		spot++;
       		continue;
        }
    else{
       		flag=false;
       		break;
       }
   } 

  if(!flag)
  {
   // alert(obj.parentNode.previousSibling.innerText+" 输入框输入了非法字符，请输入数字！")
    obj.focus();
    obj.select();
   }
  else if(spot>1)
  {
     //alert(obj.parentNode.previousSibling.innerText+" 输入框输多了小数点符号，请重输入数字！")
     flag=false;
     obj.focus();
     obj.select();
   }
   return flag;
}


/*
*判断某个控件的值是否为整数
*/
function isNumber(obj)
{

   if(obj==null)
   return false;

   var flag=true;
   var objValue=trim(obj.value);
   //alert(objValue);
   
   for(var i=0;i<objValue.length;i++)
   {
     var pstr=objValue.substring(i,i+1);
     if(pstr>=0&&pstr<=9)
		 {
		   continue;
		 }
	 else
		 {
		   flag=false;
		   break;
		}
   }
   
   if(!flag)
   {
    alert(obj.parentNode.previousSibling.innerText+" 输入了非法字符，请输入数字！")
    obj.focus();
    obj.select();
   }

   return flag;	
   
}

/**
*去掉字串两边的空格 
*/
function trim(str) 
{ 
	return lTrim(rTrim(str)); 
} 

/**
*去掉字串左边的空格 
*/
function lTrim(str) 
{ 
	if (str.charAt(0) == " ") 
	{ 
		//如果字串左边第一个字符为空格 
		str = str.slice(1);//将空格从字串中去掉 
		//这一句也可改成 str = str.substring(1, str.length); 
		str = lTrim(str); //递归调用 
	} 
	return str; 
} 

/**
*去掉字串右边的空格 
*/
function rTrim(str) 
{ 
	var iLength; 
	iLength = str.length; 
	if (str.charAt(iLength - 1) == " ") 
	{ 
		//如果字串右边第一个字符为空格 
		str = str.slice(0, iLength - 1);//将空格从字串中去掉 
		//这一句也可改成 str = str.substring(0, iLength - 1); 
		str = rTrim(str); //递归调用 
	} 
	return str; 
} 
				


/**
*根据用户ID开启查看用户信息的窗口
*/
function openUserInfoWindow(UserID)
{
   window.open("../userman/user_info.jsp?UserID="+UserID,"查看用户资料","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,width=588,height=453,left=88,top=88");
}

/**
*根据EvalID和Type开启查看所有评论
*/
function openEvalWindow(WorkID,WorkType)
{
	var str="toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,width=588,height=453,left=88,top=88";
   window.open("../workeval/eval_list.jsp?WorkID="+WorkID+"&WorkType="+WorkType,"查看评论信息","");
}

/**
*根据用户名开启查看用户信息的窗口
*/
function openUserInfoWindowByUserName(UserName)
{
   window.open("../userman/user_info.jsp?UserName="+UserName,"查看用户资料","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,width=588,height=453,left=88,top=88");
}

/**
*开启群发邮件的窗口
*/
function openSendMailWindow(ToWho)
{
  window.open("../email/send_mail.jsp?openWin=1&ToWho="+ToWho,"发邮件","width=580 height=500 top=0 left=50 scrollbars=yes resizeable=yes");
}

/**
*开启在线消息窗口
*/
function openOnlineWindow()
{
   window.open("../online/list_online_user.jsp","","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,width=350,height=388,left=188,top=88");
}

/**
*开启发在线消息的窗口
*/
function openSendMessageWindow(ReceiverID, ReceiverName)
{
  window.open("../online/send_message.jsp?ReceiverID="+ReceiverID+"&ReceiverName="+ReceiverName,"发送消息给","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no,width=410,height=215,left=188,top=188");
}

/**
*开启在线消息历史记录窗口
*/
function openHistoryWindow(ToUserID,ToUserName)
{
   window.open("../online/history.jsp?ToUserID="+ToUserID+"&ToUserName="+ToUserName,"历史记录","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,width=666,height=388,left=88,top=88");
}

/**
*开启聊天室窗口
*/
function openChatWindow(SpaceCode)
{
   window.open("../chat/index.jsp?SpaceCode="+SpaceCode);
}

/*
*更换图象
*/
function changeMenu(leftURL,URLMain)
{
   window.open(URLMain,"mainFrame","");  
   window.location.replace(leftURL);
}

function changeMenuTo(aURL,mainURL,address)
{
   window.open(mainURL,address,"");
   window.location.replace(aURL); 
}
function changeMenuToTo(aURL,leftURL,leftAddress,topURL,topAddress)
{
   window.open(leftURL,leftAddress,"");  
   window.open(topURL,topAddress,""); 
   window.location.replace(aURL);
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

/**
* 判断str1中包含多少个str2
*/
function countStr( str1, str2 )
{
	var len2 = str2.length;
	var pos = 0-len2;
	var num = 0;
	while( (pos=str1.indexOf(str2, pos+len2))>=0 )
		num ++;
	return num;
}

/**
* 判断str是否是标准的mathml代码
*/
function isValidMathml(str)
{
	 	if( str.indexOf("<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")>=0 )
	    {
			//alert(countStr(str, "<")+":"+countStr(str, "/>")+":"+countStr(str, "</") );
			if( countStr(str, "<")-countStr(str, "</") == countStr(str, "/>") + countStr(str, "</") )
				return true;
			else
				return false;
		}	 		
		else
	 		return true;
}

/**
 * 打开mathml编辑窗口
 */
function openMathmlEditor( ReturnValue )
{
	window.open("../addons/mathml/index.jsp?ReturnValue="+ReturnValue,"公式编辑器","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=560,height=350,left=150,top=100");
}


function alertErrors(Message)
{
	if(Message!="")
       alert(Message);
}

/**
 * itemArr是一个二位数组，每行表示一个图片信息，图片的名字为"Image*"，每行第0个元素为图片默认时的url，第1个元素是图片点击后的url，第2个元素是图片导航的链接地址，链接默认打开的框架名称为"document.parent.mainFrame"，id为要选中的图片的编号。
 */
function changeImageMenu( itemArr, id )
{
	var imageObject;
	for( var i=0; i<itemArr.length; i++ )
	{
		imageObject = eval( "document.Image"+i );
		if( imageObject != null )
		{
			if( i == id )
			{
				imageObject.src = itemArr[i][1];
				//imageSrcArr[i][3] = true;
				//alert( imageObject.src );
			}
			else 
			{
				//imageSrcArr[i][3] = false;
				if( imageObject.src.indexOf( itemArr[i][1])>=0 )
					imageObject.src = itemArr[i][0];
			}
		}
	}
	
   if( itemArr[id].length>=2 )
	{
	window.parent.mainFrame.location = itemArr[id][2];
	}
}

/**
 * 和changeImageMenu( itemArr, id )功能一样，区别在于能指定打开网页所在的框架对象。注意：frameObject不是框架名称，必须是框架对象
 */
function changeImageMenuFrame( itemArr, id, frameObject )
{
	var imageObject;
	for( var i=0; i<itemArr.length; i++ )
	{
		imageObject = eval( "document.Image"+i );
		if( imageObject != null )
		{
			if( i == id )
			{
				imageObject.src = itemArr[i][1];
			}
			else 
			{
				if( imageObject.src.indexOf( itemArr[i][1])>=0 )
					imageObject.src = itemArr[i][0];
			}
		}
	}
	if(itemArr[id].length>=2)
	{
	frameObject.location = itemArr[id][2];
	}
}


