/**
*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;	
   
}



