var arrError=new Array(); 
var i_error=-1;
//主函数
function chkFrm(oForm){
var o_frm=eval("document.getElementById(\""+oForm+"\")");
var i_totalElements=o_frm.elements.length;
var o_el;
	for(var i_loop=0;i_loop<i_totalElements;i_loop++){
	o_el=o_frm.elements[i_loop];
		if(!isNotChk(o_el)) continue;
		getRegExp(o_el);
	}
if(i_error==-1) return true;
showError();
resetError();
return false;
}

function resetError(){
	for(var i_loop=0;i_loop<=i_error;i_loop++){
	arrError.remove(i_loop);
	}
	i_error=-1;
}
Array.prototype.remove=function(dx){
    if(isNaN(dx)||dx>this.length){return false;}
    for(var i=0,n=0;i<this.length;i++){
        if(this[i]!=this[dx]){
        this[n++]=this[i]
        }
    }
    this.length-=1
}
  
function showError(){
	if(i_error==-1) return true;
	for(var i_loop=0;i_loop<=i_error;i_loop++){
	arrError[i_loop]="【"+(i_loop+1)+"】"+arrError[i_loop];
	}
	var s_errMsg=arrError.join("\r\n");
	s_errMsg="共有"+(i_error+1)+"项原因导致提交失败：\r\n"+s_errMsg;
	alert(s_errMsg);
	return false;
}

//判断对象是否需要进行验证
function isNotChk(el){
var s_chktype=el.getAttribute("chktype");
	if(s_chktype!=null){
		return true;
	}else{
		return false;
	}
}

//获取对象值
function getValue(el){
    var sType = el.type;
    switch(sType){
        case "text":
        case "password":
        case "textarea": return el.value;
     }
}

//根据验证类型判断所使用的正则表达式
function getRegExp(el){
var s_elChkType=el.getAttribute("chktype").toLowerCase();
	switch(s_elChkType){
		case "notcanempty": chk_notCanEmpty(el);break;
		case "limitlen": chk_limitlen(el);break;
		case "limitint": chk_limitInt(el);break;
		case "exactemail":chk_exactEmail(el);break;
		case "exacturl":chk_exactUrl(el);break;
		case "samepswd":chk_samepswd(el);break;
		case "changepswd":chk_changepswd(el);break;
	}
}

//-----------------------------------------限制不能为空--------------------------------
function chk_notCanEmpty(el){
var str=getValue(el);
	if(str==null) l_result=false;
	if(str.trim()==""){
	i_error++;
	arrError[i_error]=el.getAttribute("errormsg")+"不能为空值";
	return false;
	}else{
	return true;
	}
}

//------------------------------------------字符串限制长度-----------------------------------
function chk_limitlen(el){
var str=getValue(el);
str=str.trim();
var i_strLen=str.len();
var s_range=el.getAttribute("range");
var arr_limitLen=s_range.split(",");
	if(parseInt(arr_limitLen[0])==-1&&parseInt(arr_limitLen[1])!=-1&&i_strLen>parseInt(arr_limitLen[1])){
	i_error++;
	arrError[i_error]=el.getAttribute("errormsg")+"长度(双字节字符长度计2)必须小于"+arr_limitLen[1];
	return false;
	}
	if(parseInt(arr_limitLen[0])!=-1&&parseInt(arr_limitLen[1])==-1&&i_strLen<parseInt(arr_limitLen[0])){
	i_error++;
	arrError[i_error]=el.getAttribute("errormsg")+"长度(双字节字符长度计2)必须大于"+arr_limitLen[0];
	return false;
	}
	if(parseInt(arr_limitLen[0])!=-1&&parseInt(arr_limitLen[1])!=-1&&(i_strLen<parseInt(arr_limitLen[0])||i_strLen>parseInt(arr_limitLen[1]))){
	i_error++;
	arrError[i_error]=el.getAttribute("errormsg")+"长度(双字节字符长度计2)范围限制在"+arr_limitLen[0]+"至"+arr_limitLen[1];
	return false;
	}
return true;
}

//vbscript那样的trim函数
String.prototype.trim=function(){
return this.replace(/(^\s*)|(\s*$)/g, "");
}

//计算字符串的长度（一个双字节字符长度计2，ASCII字符计1）
String.prototype.len=function(){
return this.replace(/[^\x00-\xff]/g,"aa").length;
}

//--------------------------------------------限制为整数可控制范围---------------------------------------------
function chk_limitInt(el){
var str=getValue(el);
str=str.trim();
var number=parseInt(str);
var s_range=el.getAttribute("range");
var arr_limitLen=s_range.split(",");
	if(isNaN(number)){
	i_error++;
	arrError[i_error]=el.getAttribute("errormsg")+"只能输入数字";
	return false;
	}
	if(parseInt(arr_limitLen[0])==-1&&parseInt(arr_limitLen[1])!=-1&&number>parseInt(arr_limitLen[1])){
	i_error++;
	arrError[i_error]=el.getAttribute("errormsg")+"必须小于"+arr_limitLen[1];
	return false;
	}
	if(parseInt(arr_limitLen[0])!=-1&&parseInt(arr_limitLen[1])==-1&&number<parseInt(arr_limitLen[0])){
	i_error++;
	arrError[i_error]=el.getAttribute("errormsg")+"必须大于"+arr_limitLen[0];
	return false;
	}
	if(parseInt(arr_limitLen[0])!=-1&&parseInt(arr_limitLen[1])!=-1&&(number<parseInt(arr_limitLen[0])||number>parseInt(arr_limitLen[1]))){
	i_error++;
	arrError[i_error]=el.getAttribute("errormsg")+"范围限制在"+arr_limitLen[0]+"至"+arr_limitLen[1];
	return false;
	}
	return true;
}

//--------------------------------------------电子邮件是否正确---------------------------------------------
function chk_exactEmail(el){
var str=getValue(el);
str=str.trim();
var	i_email=/^[A-Za-z0-9]+([_\.-][A-Za-z0-9]+)*@[A-Za-z0-9]+([_\.-][A-Za-z0-9]+)*\.([A-Za-z]){2,4}$/i;
	if(i_email.test(str)){
	return true;
	}else{
	i_error++;
	arrError[i_error]=el.getAttribute("errormsg")+"不符合基本电子邮件地址规则";
	return false;
	}
}

//--------------------------------------------url是否正确---------------------------------------------
function chk_exactUrl(el){
var str=getValue(el);
str=str.trim();
var	i_url=/^http:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?$/ig;
	if(i_url.test(str)){
	return true;
	}else{
	i_error++;
	arrError[i_error]=el.getAttribute("errormsg")+"不符合url地址规则(http://www.howb2c.com)";
	return false;
	}
}

//-------------------------------------------- ퟀ     һ ---------------------------------------------
function chk_samepswd(el){
var pswd=document.getElementById("frmpswd").value;
var repswd=document.getElementById("frmrepswd").value;
pswd=pswd.trim();
repswd=repswd.trim();
	if(pswd!=repswd){
	i_error++;
	arrError[i_error]=el.getAttribute("errormsg");
	return false;
	}else{
	return true;
	}
}

//--------------------------------------------两次口令输入必须一致---------------------------------------------
function chk_changepswd(el){
var oldpswd=document.getElementById("frmoldpswd").value;
var newpswd=document.getElementById("frmnewpswd").value;
var repswd=document.getElementById("frmrepswd").value;
oldpswd=oldpswd.trim();
newpswd=newpswd.trim();
repswd=repswd.trim();
	if(oldpswd!=""){
		var s_range=el.getAttribute("range");
		var arr_limitLen=s_range.split(",");
		if(newpswd!=repswd||(newpswd.len()<parseInt(arr_limitLen[0])||newpswd.len()>parseInt(arr_limitLen[1]))){
		i_error++;
		arrError[i_error]=el.getAttribute("errormsg");
		return false;
		}
	}
		return true;
}