//
//Form表单数据验证
//
// 使用步骤：
// 1. 在form中添加onsubmit方法。<form action="go.jsp" onsubmit="return checkForm(this);">
// 2. 在需要检查的控件中设置title属性值，包含指定关键字 [ 必选、必填、邮件、数字、金额、日期、图片 ] 即可。
//

//form的验证方法
// 参数form为需要验证的form对象。
// 参数funHandle为自定义的验证方法，将在所有验证结束后调用，必须返回boolean值。
// 参数disBut为boolean值，用于定义是否disalble表单中的按钮，默认为true
function checkForm(form,funHandle,disBut){
	for(i = 0; i < form.elements.length;i++){
		obj = form.elements[i];
		hideErrorBox(obj);
		if( !requiredCheck(obj) || !numberCheck(obj) || !emailCheck(obj) || !moneyCheck(obj) || !dateCheck(obj) || !imageCheck(obj) ) 
			return false;
	}
	res=(funHandle!=null)?funHandle(form):true;
	disBut=disBut==null?true:disBut;
	if(res && disBut)disabledBut(form);
	return res;
}

//图片验证，主要验证扩展名
function imageCheck(obj){
	if( obj.title.indexOf("图片")==-1)
		return true;
	str=obj.value.substring(obj.value.length-3,obj.value.length).toLowerCase();
	return "gif,jpg,png,bmp".indexOf(str)!=-1;
}

//日期验证
function dateCheck(obj){
	if(isEmpty(obj.value) || obj.title.indexOf("日期")==-1)
		return true;
	rex=/^20\d{2}\-\d{2}\-\d{2}$/
	if(!(rex.test(obj.value))){
		message(obj);
		return false;
	}
	return true;
}

//金额验证
function moneyCheck(obj){
	if(isEmpty(obj.value) || obj.title.indexOf("金额")==-1)
		return true;
	rex1=/^\d+\.\d{1,2}$/
	rex2=/^\d+$/
	str=obj.value;
	if(!( rex1.test(str) || rex2.test(str) )){
		message(obj);
		return false;
	}
	return true;
}

//邮件验证
function emailCheck(obj){
	if(isEmpty(obj.value) || obj.title.indexOf("邮件")==-1)
		return true;
	rex=/^[^@]+@[^@]+\.[^@]+$/
	if(!(rex.test(obj.value))){
		message(obj);
		return false;
	}
	return true;
}

//数字验证
function numberCheck(obj){
	if(isEmpty(obj.value) || obj.title.indexOf("数字")==-1)
		return true;
	rex=/^\d*$/;
	if(!(rex.test(obj.value))){
		message(obj);
		return false;
	}
	return true;
}

//空值验证
function requiredCheck(obj){
	if(obj.title.indexOf("必填")==-1&&obj.title.indexOf("必选")==-1)
		return true;
	if(isEmpty(obj.value)){
		message(obj);
		return false;
	}
	return true;
}

//输出提示信息
function message(obj){
	showErrorBox(obj);
	str="验证失败！"+obj.title;
	alert("提示：\n\n　　"+str+"\n\n");
	//if(!(obj.readonly || obj.disabled))
	//	obj.focus();
}

function getAbsPoint(e){
  var x = e.offsetLeft;
  var y = e.offsetTop;
  while(e = e.offsetParent){
    x += e.offsetLeft;
    y += e.offsetTop;
  }
  return {"x":x, "y": y};
}

//空值判断
function isEmpty(str){
	return trim(str)=="";
}

//去除前后的空格
function trim(str){
	rex=/^ +/;
	rex2=/ +$/;
	return str.replace(rex,"").replace(rex2,"");
}

//提交之前，让按钮失效
function disabledBut(form){
	for(i = 0; i < form.elements.length;i++){
		obj = form.elements[i];
		if(obj.type=="submit"||obj.type=="button"||obj.type=="reset")
			obj.disabled=true;
	}
}


var error_border_space=2;
document.write('<div onmouseover="this.style.display=\'none\';" id="error_border_div" style="position: absolute; display:none; z-index:9999; border:'+error_border_space+'px solid #FF5B5B;"></div>');

var oldBorder=null;
var oldObj=null;


//隐藏对象上的错误红色边框
function hideErrorBox(obj){
	if(obj=oldObj)
		obj.border=oldBorder;
}


//显示对象的错误红色边框
function showErrorBox(obj){
	eb=document.getElementById("error_border_div");
	eb.style.display="none";
	eb.style.width=(parseInt(obj.offsetWidth,10)-error_border_space*2) +"px";
	eb.style.height = (parseInt(obj.offsetHeight,10)-error_border_space*2)+"px";
	xy=getAbsPoint(obj);
	eb.style.top =xy.y+"px";
	eb.style.left =xy.x+"px";
	eb.style.display ="";	
}
