/*-------------------------------------------------------------
* 验证客户端输入数据
*Array.max()
*String.trim()
*isAlphaNumeric( strValue )
*isDate(sCheckStr)
*isEmail( strValue )
*isNumeric( strValue )
*isEmpty( strValue )
*isMoney( strValue )
*isFloat( strValue )
*isPhone( strValue )
*isPostalCode( strValue )
*isURL( strValue )
*checkLength( strValue, strParam )
*validate( strName, strDescription, strType)
*validate2( oField, strDescription, strType)
*validateFocus( strName, strDescription, strType)
*validateFocus2( oField, strDescription, strType)
*ActionConfirm(form,msg,field)
*findkey( strValue, strkey)检查输入数据中是否含有特殊字符
*------------------------------------------------------------*/

/*******************************
** 表单验证
使用说明:
在使用表单的页面填入如下script
<script language="JavaScript" type="text/javascript" src="../css/form.js">
</script>
<script language="JavaScript" type="text/JavaScript">
function checkForm(theForm) { //v2.0
  if (!checkNotNull(theForm.name,"姓名")) return false;
  if (!checkNumber(theForm.age,"年龄")) return false;
  ...
  return true;
}
</script>

注意:checkNumber(),checkLength()等方法并不检查是否为空

<FORM name="form1" acton="..." onSubmit="return checkForm(document.form1)">
...
<input type="submit" name="Submit" value="Submit">
</FORM>
********************************/
//////////////////////////////////////////////////////////////////////////////
///////////////系统常用javascriptfunction/////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
///////////////////
function checkExp( re, s )
{
	return re.test( s );
}
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];}
}
////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////
//清空：页面中sVar开始的选择框checked
function checkEmpty(sVal){
	var nlen = sVal.length;
	var coll = document.forms["form1"].tags("input");
	for (i=0;i < coll.length;i++){
		if (coll.item(i).name.substr(0,nlen) == sVal) {
			coll.item(i).checked = false;
		}
	}
}
//全选：页面中sVar开始的选择框checked
function checkAll(sVal){ 
	var nlen = sVal.length;
	var coll = document.forms["form1"].tags("input");
	for (i=0;i < coll.length;i++){
		if (coll.item(i).name.substr(0,nlen) == sVal) {
			coll.item(i).checked = true;
		}
	}
}
//选择：页面中sVar开始的选择框(checkbox)为sCheck状态值
function checkSelect(sVal,sCheck){ 
	var nlen = sVal.length;
	var coll = document.forms["form1"].tags("input");
	for (i=0;i < coll.length;i++){
		if (coll.item(i).name.substr(0,nlen) == sVal) {
			coll.item(i).checked = sCheck;
		}
	}
}
//返回：页面中选择的sVar开始的选择框(checkbox)结果数据
function checkValue(sVar){
	var nlen = sVar.length;
	var coll = document.forms["form1"].tags("input");
	var allfids="";
	for (i = 0;i < coll.length;i++){
	    if (coll.item(i).name.substr(0, nlen) == sVar) {
			var names = coll.item(i).value;
			if  (coll.item(i).checked == true){
				allfids = allfids + "," + names;
			}
		}
	}
	return allfids;
}

//返回：页面中没有选择的sVar开始的选择框(checkbox)结果数据
function checkNothing(sVar){
	var nlen = sVar.length;
	var coll = document.forms["form1"].tags("input");
	var allfids="";
	for (i = 0;i < coll.length;i++){
	    if (coll.item(i).name.substr(0, nlen) == sVar) {
			var names = coll.item(i).value;
			if  (coll.item(i).checked == false){
				allfids = allfids + "," + names;
			}
		}
	}
	return allfids;
}
//返回：页面中选择的sVar开始的输入框(INPUT)结果数据
function getInValue(sVar){
	var nlen = sVar.length;
	var coll = document.forms["form1"].tags("input");
	var allfids="";
	for (i = 0;i < coll.length;i++){
	    if (coll.item(i).name.substr(0, nlen) == sVar) {
			var names = coll.item(i).value;
			allfids = allfids + "," + names;
		}
	}
	return allfids;
}

//删除所在选择项：页面中的checked为true,须在页面中加一个隐含的delid项
function delrecord(){
  var coll=document.forms["form1"].tags("input");
  var delids="";
    for (i=0;i<coll.length;i++){
	    if (coll.item(i).name.substr(0,1)=="0") {
		 	if  (coll.item(i).checked==true){
				delids = delids + "," + coll.item(i).value;
            }
		}
   }
  document.form1.delids.value=delids;
  if (delids==""){
     alert("请选择删除选项！");   
   }else {  
   document.form1.submit();//提交
}

}
//审核所在选择项：页面中的checked为true,须在页面中加一个隐含的delid项
function checkrecord(){
  var coll=document.forms["form1"].tags("input");
  var delids="";
    for (i=0;i<coll.length;i++){
	    if (coll.item(i).name.substr(0,1)=="0") {
		 if  (coll.item(i).checked==true){
				delids = delids + "," + coll.item(i).value;
             }
		}
   }
  document.form1.delids.value=delids;
  if (delids==""){
     alert("请选择审核选项！");   
   }else { 
   document.form1.checks.value=1;
   document.form1.submit();//提交
}
}
//删除所在选择项：页面中的checked为true,须在页面中加一个隐含的delid项
function Rdelrecord(){
  var coll=document.forms["form1"].tags("input");
  var delids="";
  var workids="";
  var str="";
    for (i=0;i<coll.length;i++){
	    if (coll.item(i).name.substr(0,1)=="0") {
		 if  (coll.item(i).checked==true){
		 		str = coll.item(i).value;
				var tmp=str.split(",");
				delids=delids + "," + tmp[0];
				workids=workids + "," + tmp[1];
             }
		 }
   }
  document.form1.delids.value=delids;
  document.form1.workids.value=workids;
  if (delids==""){
     alert("请选择删除选项！");   
   }else {  
   document.form1.submit();//提交
}

}
//审核所在选择项：页面中的checked为true,须在页面中加一个隐含的delid项
function Rcheckrecord(){
  var coll=document.forms["form1"].tags("input");
  var delids="";
  var workids="";
  var str="";
    for (i=0;i<coll.length;i++){
	    if (coll.item(i).name.substr(0,1)=="0") {
		 if  (coll.item(i).checked==true){
		 		str = coll.item(i).value;
				var tmp=str.split(",");
				delids=delids + "," + tmp[0];
				workids=workids + "," + tmp[1];
            }
		 }
   }
  document.form1.delids.value=delids;
  document.form1.workids.value=workids;
  if (delids==""){
     alert("请选择审核选项！");   
   }else { 
   document.form1.checks.value=1;
   document.form1.submit();//提交
}

}
//传附件调用文件 
//参数据：fname：显示名称的名称，fid：显示名称代码的名称
function fileselect(fname,fid) {
	var return_value=showModalDialog("../public/upload/file.jsp?names="+fname.value+"&ids="+fid.value+"","","dialogWidth:37;dialogHeight:12;dialogTop:250;dialogLeft:230;status:no;scrollbars:no;help:no");
	if((typeof(return_value)=="undefined"))
		return;
	else
		var str1="";
		var str2="";
		tmpstr=return_value.split("*");
		str1=tmpstr[0];
		str2=tmpstr[1];
		fname.value=str1;
		fid.value=str2;
}

function uploadfile(sVar,sCode,sPath){
	var str=showModalDialog("../public/file.jsp?UploadPath="+sPath,"","dialogWidth:25;dialogHeight:10;dialogTop:250;dialogLeft:230;status:no;scrollbars:no;help:no");
	var str1="";
	var str2="";
	if((typeof(str)!="undefined"))
	{
		var tmpstr=str.split("*");
		sVar.value=tmpstr[0];
		sCode.value=tmpstr[1];
	}  
}

/////////////////////////////
//////////显示和关闭查询
function showadv(schange){ 
	eval("var ch="+schange+".style.display;");
   if (ch == ""){
      eval(schange+".style.display = 'none';");
    } else {
      eval(schange+".style.display = '';");
    }
}


////////////////////////////////////////////////////////////////////////////
/////////让光标停在得到焦点框的最后
function  endfocus()      
{      
   var  e  =  event.srcElement;      
   var  r  =e.createTextRange();      
   r.moveStart('character',e.value.length);      
   r.collapse(true);      
   r.select();      
} 

// 是否为空，非空返回真，不非为空返回假
function isBlank(str) {
	var blankFlag = true;
	if (str.length == 0) return true;
	for (var i = 0; i < str.length; i++) {
		if ((str.charAt(i) != "") && (str.charAt(i) != " ")) {
			blankFlag = false;
			break;
		}
	}
	return blankFlag;
}

function checkNotNull(theField, fieldName) {
	
	if(isBlank(theField.value)){
		alert(fieldName + "不可为空！");
		theField.focus();
		return false;
	}

	return true;
}

// 是否为数字
function checkNumber(theField, fieldName) {
 	var pattern = /^([0-9]|(-[0-9]))[0-9]*((\.[0-9]+)|([0-9]*))$/;

 	if(theField.value == "") return true;
 	if (!pattern.test(theField.value)) {
 		alert(fieldName + "必须为合法数字");
 		theField.focus();
 		theField.select();
 		return false;
 	}

	return true;
}

// 是否为指定范围数字
function checkNumberRange(theField, fieldName, min, max) {
	if(theField.value == "") return true;
	if (!checkNumber(theField, fieldName)) return false;

	if ((min != "") && (theField.value < min)) {
		alert(fieldName + "不可小于" + min + "！");
		theField.focus();
		theField.select();
		return false;
	}

	if ((max != "") && (theField.value > max)) {
		alert(fieldName + "不可超过" + max + "！");
		theField.focus();
		theField.select();
		return false;
	}

	return true;
}

// 是否为整数
function checkInteger(theField, fieldName) {
	var pattern = /^(\d|(-\d))\d*$/;

	if(theField.value == "") return true;
	if (!pattern.test(theField.value)) {
		alert(fieldName + "必须为整数！");
		theField.focus();
		theField.select();
		return false;
	}

	return true;
}

// 是否为指定范围内整数
function checkIntegerRange(theField, fieldName, min, max) {
	if(theField.value == "") return true;
	if (!checkInteger(theField, fieldName)) return false;

	if ((min != "") && (theField.value < min)) {
		alert(fieldName + "不可小于" + min + "！");
		theField.focus();
		theField.select();
		return false;
	}

	if ((max != "") && (theField.value > max)) {
		alert(fieldName + "不可超过" + max + "！");
		theField.focus();
		theField.select();
		return false;
	}

	return true;
}

// 是否为正数
function checkPositiveNumber(theField, fieldName) {
	if(theField.value == "") return true;
	if (theField.value.charAt(0) == '-') {
		alert(fieldName + "必须为正数！");
		theField.focus();
		return false;
	}

	return true;
}

// 限制字串最大长度
function checkLength(theField, fieldName, maxLength) {
	if(theField.value == "") return true;
	if (theField.value.length > maxLength) {
		alert(fieldName + "的字数最多为" + maxLength + "字！");
		theField.select();
		theField.focus();
		return false;
	}

	return true;
}

// 限制字串长度,注意参数顺序
function checkLength2(theField, fieldName, maxLength, minLength) {
	if(theField.value == "") return true;
	if (theField.value.length > maxLength) {
		alert(fieldName + "的字数最多为" + maxLength + "位！");
		theField.focus();
		return false;
	}

	if ((minLength != "") && (theField.value.length < minLength)) {
		alert(fieldName + "的字数最少为" + minLength + "位！");
		theField.focus();
		return false;
	}

	return true;
}

// 所输入字符串是否均为合法字符
// charBag中为包含所有合法字符的字符串
function checkStrLegal(theField, fieldName, charBag) {
	if(theField.value == "") return true;
   	for (var i = 0; i < theField.value.length; i++) {
        var c = theField.value.charAt(i);
        if (charBag.indexOf(c) == -1) {
     		alert(fieldName + "含有非法字符(" + c + ")！");
     		theField.focus();
     		return false;
        }
    }

    return true;
}

// 所输入字符串是否均为合法字符
// charBag中为包含非法字符的字符串
function checkStrLegal2(theField, fieldName, charBag) {
	if(theField.value == "") return true;
   	for (var i = 0; i < theField.value.length; i++) {
        var c = theField.value.charAt(i);
        if (charBag.indexOf(c) > -1) {
     		alert(fieldName + "含有非法字符(" + c +")！");
     		theField.focus();
     		return false;
        }
    }

    return true;
}

// 验证是否 为空
function isEmpty( strValue )
{
	if( strValue == "" )
		return true;
	else
		return false;
}
// 验证是否 电话
function isPhone( strValue )
{
	// 普通电话	(0755)4477377-3301/(86755)6645798-665
	// 不带区号电话 88989899
	// Call 机	95952-351
	// 手机		130/131/133/135/136/137/138/13912345678
	// 或者为空
	if( isEmpty( strValue ) ) return true;
	return checkExp( /(^\(\d{3,5}\)\d{6,8}(-\d{2,8})?$)|(^\d{7,8}$)|(^\d+-\d+$)|(^(130|131|133|135|136|137|138|139)\d{8}$)/g, strValue );
}
// 验证是否 货币
function isMoney( strValue )
{
	// 货币必须是 -12,345,678.9 等格式 或者为空
	if( isEmpty( strValue ) ) return true;
	
	return checkExp( /^[+-]?\d+(,\d{3})*(\.\d+)?$/g, strValue );
}
// 验证是否 邮政编码
function isPostalCode( strValue )
{
	// 邮政编码必须是6位数字
	return checkExp( /(^$)|(^\d{6}$)/gi, strValue )
}

// 验证是否 URL
function isURL( strValue )
{
	// http://www.yysoft.com/ssj/default.asp?Type=1&ArticleID=789
	if( isEmpty( strValue ) ) return true;
	
	var pattern = /^(http|https|ftp):\/\/(\w+\.)+[a-z]{2,3}(\/\w+)*(\/\w+\.\w+)*(\?\w+=\w*(&\w+=\w*)*)*/gi;
	// var pattern = /^(http|https|ftp):(\/\/|\\\\)(\w+\.)+(net|com|cn|org|cc|tv|[0-9]{1,3})((\/|\\)[~]?(\w+(\.|\,)?\w\/)*([?]\w+[=])*\w+(\&\w+[=]\w+)*)*$/gi;
	// var pattern = ((http|https|ftp):(\/\/|\\\\)((\w)+[.]){1,}(net|com|cn|org|cc|tv|[0-9]{1,3})(((\/[\~]*|\\[\~]*)(\w)+)|[.](\w)+)*(((([?](\w)+){1}[=]*))*((\w)+){1}([\&](\w)+[\=](\w)+)*)*)/gi;

	return checkExp( pattern, strValue );
	
}
// 电子邮件验证
function checkEmail(theField) {
	var pattern = /^.+@.+\..+$/;

	if(theField.value == "") return true;
	if (!pattern.test(theField.value)) {
		alert("请输入合法的电子邮件地址");
		theField.focus();
		theField.select();
		return false;
	}

	return true;
}
// 去掉空格
function trim(inputString) {

if (typeof inputString != "string") { return inputString; }
var retValue = inputString;
var ch = retValue.substring(0, 1);
while (ch == " ") {
//检查字符串开始部分的空格
retValue = retValue.substring(1, retValue.length);
ch = retValue.substring(0, 1);
}
ch = retValue.substring(retValue.length-1, retValue.length);
while (ch == " ") {
//检查字符串结束部分的空格
retValue = retValue.substring(0, retValue.length-1);
ch = retValue.substring(retValue.length-1, retValue.length);
}
while (retValue.indexOf(" ") != -1) {
//将文字中间多个相连的空格变为一个空格
retValue = retValue.substring(0, retValue.indexOf(" ")) + retValue.substring(retValue.indexOf(" ")+1, retValue.length);
}
return retValue;
}

// 是否为只读域（如file，text等域只接受右边按钮选择传回的结果）
function checkReadField() {
	alert("请点击后面的图标进行选择");
	// this.blur();
}

function UUserLogin(sUserID,surl) {
	if (sUserID == "") {
		alert("对不起,您已经超时，请重新登录！"); 
		to.window.close();
		window.location = surl;
	}
}