//提示信息
var alertNotNull = "不能为空！";
var alertOnlyNumber = "只能为数字！";
var alertOverMaxLength = "您输入的信息过长,请进行删减!"; 
var alertPostCode = "邮政编码长度必须是6位！";
var alertEmail = "请输入合法的电子邮件地址！";
var alertTel ="请输入合法的电话/传真号码！";
var alertMobil ="请输入合法的时手机号码！";
var alertDouble ="您的输入有错误，请重新输入!";
var alertNoCancel = "没有可以取消的纪录!";
var alertNoSelect ="没有可以选择的纪录!"
var alertDoUpdateOnlyOne = "至少选中一条记录并只可以选中一条记录！";
var alertDoUpdateNoSel = "请选择需要修改的记录!";
var alertDoDelNoSel = "没有可以删除的记录！";
var alertDoDelConfirm = "您确定要删除所选的记录吗？";
var alertDoAddNoSel = "请选择需要添加的记录！";
var alertDoAddConfirm ="您确定要添加所选的记录吗？";
var alertFillTrueNum = "请输入有效数字！";

// 检查用户输入的内容是否为空并显示提示信息
function checkTestNullAlert(obj,errorMessage){
    if(!checkIsNull(obj)){
		if(errorMessage==null||errorMessage==""){
			alert(alertNotNull);
		}
		alert(errorMessage);
		return true;  		
    }
}

// 检查用户输入的是否为数字并显示提示信息
function checkTestNumAlert(obj){
	if(!checknum(obj)){
		alert(alertOnlyNumber);
		return false;
	}
}

// 检查用户输入的内容是否为空，true为非空；false为空
function checkIsNull(obj){
	if(obj.value==null)
		return false;
	if(obj.value.length==0)
		return false;
	return true;
}

// 检查用户输入的是否为数字，true为数字； false非数字
function checknum(obj){
    if( obj.value.length == 0 ) {
        return true;
    }
    var patten = /^\-{0,1}[0-9]{0,}\.{0,1}[0-9]{0,}$/;
    if( obj.value.match(patten) ) {
        return true;
    }
    return false;
}

// 限定输入长度textArea textCounter(文本内容对象，最大长度);
function textCounter(text,max) {
	var maxLength = max;
	if (strlen(text.value) > maxLength){
		alert(alertOverMaxLength);
		text.focus();
	}
}
// 取字符串长度,中文*2
function strlen(str){
	var i;
	var len = 0;
	for (i=0;i<str.length;i++){
		if (str.charCodeAt(i)>255) len+=2; else len++;
	}
	return len;
}

// 验证是否为邮政编码,true合法 false不合法;
function checkPostCode(obj){
 	checkTestNumAlert(obj);
	if(obj.value.length!=6){
		alert(alertPostCode);
		return false;
	}
	return true;
}

//匹配数字
function checkIsNum(obj){
	var pattern_num = new RegExp("^[0-9]");
	if((!pattern_num.test(obj.value))){
		alert(alertFillTrueNum);
		return false;
	}
}

// 检查输入对象的值是否符合E-Mail格式,true合法 false不合法;
function checkIsEmail(obj){  
	var isRight = true;
	if(obj.value=="")
		return true;
	var myReg = /^([-_A-Za-z0-9\.]+)@([_A-Za-z0-9]+\.)+[A-Za-z0-9]{2,3}$/; 
	if(!myReg.test( obj.value )){
		alert(alertEmail);
		isRight = false;
	}
	return isRight;
}

//校验普通电话、传真号码：可以“+”开头，除数字外，可含有“-” 
function checkIsTel(obj){ 
	if(obj.value=="")
		return true;
	var isRight = true;
	var regu =/(^([0][1-9]{2,3}[-])?\d{3,8}(-\d{1,6})?$)|(^\([0][1-9]{2,3}\)\d{3,8}(\(\d{1,6}\))?$)|(^\d{3,8}$)/; 
	var patrn = new RegExp(regu);
	if (!patrn.exec(obj.value)){
		alert(alertTel);
		isRight = false;
	}
	return isRight;
} 

//校验手机号码：必须以数字开头，除数字外，可含有“-” 
function checkIsMobil(obj) { 
	if(obj.value=="")
		return true;
	var isRight = true;
	var regu =/(^[1][0-9]{10}$)|(^0[1][0-9]{10}$)/;
	var patrn = new RegExp(regu);
	if (!patrn.exec(obj.value)){
		alert(alertMobil);
		isRight = false;
	}
	return isRight;
} 

/**
 * 验证是否为小数,如不是,则自动截取
 */
function checkIsDouble(obj){
  if(obj.value==""){
  	return true;
  }
  var  patrn=/^[0-9|.]{1,20}$/;
	patrn.exec(obj.value);
	if(!patrn.exec(obj.value)){
		//alert(alertDouble);
		obj.value = obj.value.replace(/[^0-9|.|-]/g,"");
		return false;
	}
	if(obj.value.split(".").length > 2){
		if(isNaN(parseFloat(obj.value))){
			 obj.value = obj.value.substring(0,obj.value.length-1);}
		else{
			 obj.value = parseFloat(obj.value);
		}
	}
	var tempString = obj.value.substring(1,obj.value.length);
	if(tempString.indexOf("-") != -1){	
		obj.value = obj.value.substring(0,tempString.indexOf("-")+1);
	}
}

/**
 * 验证是否为百分比,自动转化为百分比
 */
function checkIsPercent(obj){
	var percentIndex = obj.value.lastIndexOf("%");
	var permillageIndex = obj.value.lastIndexOf("‰");
	var printMark;
	if(obj.value=="")
		return;
	else if(percentIndex!=-1 && permillageIndex!=-1){
		printMark = percentIndex<permillageIndex?"%":"‰";
	}else{
		printMark = permillageIndex!=-1?"‰":"%";
	}
	checkIsDouble(obj);
	obj.value += printMark;
}

// 提交用户表单 
function goActionWithTarget(index,action,target){
  var myForm = document.forms[index];
  myForm.action=action;
  if(target.length != 0) {
    myForm.target = target;
  }
  myForm.submit();
}

//删除列表页多选框选中的项
function goDeleteActionWithTargetByIds(index,action,target,ids){
  var myForm = document.forms[index];
  if( checkboxvalidate(ids) ) {
      if( confirm("您确定要操作所选的项目吗？") ) {
          if(myForm.current!=null) {
              myForm.current.value = 1;
          }
          goActionWithTarget(index,action,target);
      }
  }else{
      alert('没有可以操作的项目！');
  }
}

//更新列表页某一项
function goActionWithTargetDoCheckBox(index,action,target,checkboxobj){
		var myForm = document.forms[index];
		var checkCount = 0;
		if(checkboxobj != null){
				if(checkboxobj.length==null && checkboxobj.checked) {
							goActionWithTarget(index,action,target);
				}else{
						for(i=0;i<checkboxobj.length;i++){
								if(checkboxobj[i].checked)
										checkCount++;
						}
						if(checkCount == 1){
								goActionWithTarget(index,action,target);
						}else{
								alert("修改操作时至少选中一条记录并只可以选中一条记录");
								deselect(checkboxobj);
						}
				}
		}else{
				 alert('请选择需要修改的记录');
		}
}

// 更新操作提交goActionWithTargetForUpdate(form下标，action,target,ID数组)
function goActionWithTargetForUpdate(index,action,target,checkboxobj){
	var myForm = document.forms[index];
	var checkCount = 0;
	if(checkboxobj != null){
		if(checkboxobj.length==null && checkboxobj.checked) {
			goActionWithTarget(index,action,target);
		}else{
			for(i=0;i<checkboxobj.length;i++){
				if(checkboxobj[i].checked)
					checkCount++;
				}
				if(checkCount == 1){
					goActionWithTarget(index,action,target);
				}else{
					alert(alertDoUpdateOnlyOne);
					deselect(checkboxobj);
					}
				}
		}else{
			alert(alertDoUpdateNoSel);
		}
}

// （框架页中）添加操作提交goActionWithTargetForAdd(form下标，action,target,ID数组)
function goActionWithTargetForAdd(index,action,target,ids){
  var myForm = document.forms[index];
  if( checkboxvalidate(ids) ) {
      if( confirm(alertDoAddConfirm) ) {
          goActionWithTarget(index,action,target);
      }
  }else{
      alert(alertDoAddNoSel);
  }
}

// 批量操作提交表单goActionWithTargetForBatch(form下标，action,target,ID数组,操作名称[如:"删除，修改，提交，审核.."],数据信息名称)
function goActionWithTargetForBatch(index,action,target,ids,active,message){
  var myForm = document.forms[index];
  if( checkboxvalidate(ids) ) {
      if( confirm("您确定对这些"+message+"信息执行"+active+"操作吗？") ) {
          goActionWithTarget(index,action,target);
      }
  }else{
      alert("请您指定想要"+active+"的"+message+"记录！");
  }
}

// checkBox全部选择和全部取消
function checkAll(checkBoxObj,functionBox){
	  if(functionBox.checked){
	  	  choiceAll(checkBoxObj);
	  }else{
	  	  deselect(checkBoxObj);
	  }
}
// 	全部选择checkBox
function choiceAll(checkBoxObj){
    if(checkBoxObj != null){
    	 if(checkBoxObj.length == null){
    	  	 checkBoxObj.checked = true;
    	  }else{
    	  	  for(var i=0; i < checkBoxObj.length; i++){
		    	  checkBoxObj[i].checked = true;
		      }
    	  }
    }else{
    	alert(alertNoSelect);
  	    return;
    }
}

// 	全部取消checkBox
function deselect(checkBoxObj){
	  if(checkBoxObj !=null){
	  	  if(checkBoxObj.length == null){
	  	  	  checkBoxObj.checked = false;
	  	  }else{
	  	  	  for(var i = 0; i < checkBoxObj.length; i++){
			  	     checkBoxObj[i].checked = false;
			  }
	  	  }
	  }else{
	  	  alert(alertNoCancel);
	  }
}

// 判断多选择是否有选中 true 有选中，false没有选中
function checkboxvalidate(checkboxobj) {
    var isCheck=false;
	if(checkboxobj !=null ){
		if(checkboxobj.length==null && checkboxobj.checked) {
			isCheck = true;
		}else{
			for(i=0;i<checkboxobj.length;i++){
				if(checkboxobj[i].checked)
					isCheck = true;
			}
		}
	}
	return isCheck;
}

//只要有一个未选种全选状态取消
function changCheck(ids,allcheck){
	var sin = document.getElementsByName(ids);
	var all = document.getElementsByName(allcheck)[0];
	var flag = true;
	for(var i=0;i<sin.length;i++){
		if(sin[i].checked == false){
			flag = false;
			break;
		}
	}
	if(flag)
		all.checked = true;
	else
		all.checked = false;
}

//清除查询条件，参数为要清除查询条件的域name名称，参数形式为"A,B,C,D"，中间以","分隔
function clearSearchValue(clearValue){
	var clearTmp = clearValue.split(",");
	for (var i=0;i<clearTmp.length;i++){
		if (clearTmp[i]!=undefined&&""!=clearTmp[i]){
			document.all(clearTmp[i]).value = "";
		}
	}
}

//页面下拉框默认选中，参数obj为要默认选中的下拉框名称，形式为"document.all.A"，currentValue为从库中取出要默认选中的值
function initSelect(obj,currentValue){
	for(var i=0;i<obj.options.length;i++){
		if(obj.options[i].value == currentValue){
			obj.options[i].selected = true;
			break;
		}
	}
}
//单选按钮默认选中 obj单选按钮对象 value匹配字符串
function initRadio(obj,value){
	for(var i=0;i<obj.length;i++){
		if(obj[i].value==value){
			obj[i].checked = true;
		}
	}
}
//去掉’符号，防止SQL注入
function isChar(obj){
   	   var res = true;
   	   var regexp = "'";
   	  	for(var i=0;i<obj.value.length;i++){
   	  		if(obj.value.charAt(i)=="'"){
   	  			alert("非法字符[']，请重新输入！");
   	  			obj.value = obj.value.replace(/'/,"");
   	  			res = false;
   	  		}
   	  	}
   	  	return res;
   	  }

