/*
javascript 基础库

Validator  输入校验库
Ajax       AJAX远程访问库
*/

/*
通用客户端输入校验库
作者：黄振华
时间：2007-8-10
*/
function BaseValidate(){
    /* 文本框为空 */
	this.valueEmpty=function(field,name){
		if(field.value=="" || typeof(field.value)=='undefined'){
			alert(name+"不能为空！");
			field.focus();
			field.select();
			return false;
			}
		return true;
	}
	this.valueEmpty1=function(field,name){
		if(field.value=="" || typeof(field.value)=='undefined'){
			alert(name+"不能为空！");
			return false;
			}
		return true;
	}
	/* 文本框为undefined（radio等） */
	this.valueEmpty4Radio=function(field,name){
		if(field.length==undefined){
			field.checked = true;
			return true;
		}
		for(var i=0;i<field.length;i++)   
		  {
			if(field[i].checked == true)
				return true;
		  } 
		alert(name+"不能为空！");
		return false;
		
	}

	/* 下拉框为空 */
	this.valueSelectEmpty=function(field,fieldval,name){
		if(field.value==fieldval){
			alert(name+"不能为空！");
			return false;
			}
		return true;
	}
	/* 值大小比较（大于等于） */
	this.valueMoreEqual=function(field1,field2,name1,name2){
		if(field1.value < field2.value){
			alert(name1+"必须大于等于"+name2+"！");
			field1.focus();
			field1.select();
			return false;
			}
		return true;
	}
	
	/* float数字校验 */
	this.isFloat=function(field,name){
		var patm=/^(-?\d+)(\.\d+)?$/;
		if(!patm.exec(field.value)){
			alert(name+"必须为小数！");
			field.focus();
			field.select();
			return false;
		}
		return true;
	}
	
	this.conditionSelectValidate=function(content,name){
		if(content.value=="all" || content.value==""){
			alert(name+"不能为空！");
			return false;
		}
		return true;
	}
	
	this.valueSelectValidate=function(content){
		if(content.value=="all" || content.value==""){
			alert("请选择查询值！");
			return false;
		}
		return true;
	}

     /* 输入必须为数字 */
	this.isNum=function(field,name){
		var patm=/^[0-9\.]{1,20}$/;
		if(!patm.exec(field.value)){
			alert(name+"必须为数字！");
			field.focus();
			field.select();
			return false;
		}
		return true;
	}

	 /* 输入必须为整数 */
	this.isIntNum=function(field,name){
		var patm=/^[0-9]{1,20}$/;
		if(!patm.exec(field.value)){
			alert(name+"必须为正整数！");
			field.focus();
			field.select();
			return false;
		}
		return true;
	}
	
     //查询功能模块:数字校验
	this.valueSearchInputNumValidate=function(content){
		var patm=/^[0-9\.]{1,20}$/;
		if(content.value!="" && !patm.exec(content.value) ){
			alert("输入的必须是数字！");
			content.focus();
			content.select();
			return false;
		}
		return true;
	}
	
	//日期校验(yyy-MM-DD)
	this.valueInputDateValidate=function(content){
		var patm=/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/;
		if(!patm.exec(content.value) || content.value==""){
			alert("输入的日期格式有误，请按照：2006-01-02 格式！");
			content.focus();
			content.select();
			return false;
		}
		return true;
	}
	//日期校验(yyy-MM-DD)
	this.valueSearchInputDateValidate=function(content){
		var patm=/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/;
		if(content.value!="" && !patm.exec(content.value)){
			alert("输入的日期格式有误，请按照：2006-01-02 格式！");
			content.focus();
			content.select();
			return false;
		}
		return true;
	}
	//查询功能模块:日期校验(yyyyMM)
	this.valueInputMonthValidate=function(content){
		var patm=/^[0-9]{4}[0-9]{2}$/;
		if(!patm.exec(content.value) || content.value==""){
			alert("输入的日期格式有误，请按照：200601 格式！");
			content.focus();
			content.select();
			return false;
		}
		return true;
	}
	//日期校验(hh:mm:ss)
	this.valueSearchInputTimeValidate=function(content){
		var patm=/^[0-9]{2}:[0-9]{2}:[0-9]{2}$/;
		if(content.value!="" && !patm.exec(content.value)){
			alert("输入的日期格式有误，请按照：07:54:43 格式！");
			content.focus();
			content.select();
			return false;
		}
		return true;
	}
	//电子邮箱校验
	this.email=function(content){
	if (content.value.charAt(0)=="." ||        
			content.value.charAt(0)=="@"||       
			content.value.indexOf('@', 0) == -1 || 
			content.value.indexOf('.', 0) == -1 || 
			content.value.lastIndexOf("@")==content.value.length-1 || 
			content.value.lastIndexOf(".")==content.value.length-1) {
			alert("提示：电子邮箱地址格式不正确，请重新输入！");
			content.focus();
			return false;	
	}
	else {
		alert("提示：电子邮箱地址不能为空，请输入！");
		content.focus();
		return false;
	}
	if (content.value.length <8 || content.value.length >50) {
		alert("提示：请输入有效的电子邮箱地址！");
		content.focus();
		return false;
	}
	if (content.value.length >0 && !content.value.match( /^.+@.+$/ ) ) {
	    alert("提示：请输入有效的电子邮箱地址！");
		content.focus();
		return false;
	}
       }
	   
	/* 校验是否是正确的IP地址格式 */
	this.isIpAddress=function(field,name){
		var patm=/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
		if(!patm.exec(field.value)){
			alert(name+"必须为正确的IP地址格式!");
			field.focus();
			field.select();
			return false;
		}
		return true;
	}
	
	//密码确认验证，密码content，确认密码
	this.passwordConfirm=function(content,reContent){
		if(content.value!=reContent.value){
			alert("密码输入不一致!");
			reContent.focus();
			reContent.select();
			return false;
		}
		return true;
	}
	
	/* 长度必须为固定位数 */
	this.mixlength = function(field,length,name){
		if(field.value.length != length){
			alert(name+"长度必须为"+length+"位!");
			field.focus();
			field.select();
			return false;
		}
		return true;
	}

	/* 长度不能大于固定位数 */
	this.maxlength = function(field,length,name){
		if(field.value.length > length){
			alert(name+"长度必须在"+length+"字符以内!");
			field.focus();
			field.select();
			return false;
		}
		return true;
	}

}

/*
*	XMLhttp请求发送和处理
*   作者：丁卫明
*   时间：2007-9-12
*/
function Ajax(){
	this.varAsync = null;
	this.xmlHttp = null;
	this.createHttpRequest=function(){
		var temp;
		if(window.XMLHttpRequest){
			temp=new XMLHttpRequest();
		} 
		else if (window.ActiveXObject){
			temp=new ActiveXObject("Msxml2.xmlHttp");
			if (!temp){
				temp=new ActiveXObject("Microsoft.xmlHttp");
			}
		}
		return temp;
	}

	this.sendData=function(url){
			this.xmlHttp=this.createHttpRequest();
			var loader=this;
			this.xmlHttp.onreadystatechange=function(){
				loader.aftersearch.call(loader);
			}
			if(this.varAsync==null){
				this.varAsync = true;
			}
			this.xmlHttp.open("POST",url,this.varAsync);
			this.xmlHttp.send("");
	}

	this.aftersearch=function(){
		if(this.xmlHttp.readyState == 4){
			if(this.xmlHttp.status == 200){
				try{
					var data=this.xmlHttp.responseText;
					dealBack(data);
				}
				catch(e) {
					alert(e);
				}
			}
		}
	}
	
}


/*
*	输入提示
*   作者：丁卫明
*   时间：2008-1-15
*/
function Tips(){
	this.getY=function(el){
		var y=0;
		for(var e=el;e;e=e.offsetParent)
			y+=e.offsetTop;
		for(e=el.parentNode;e&&e!=document.body;e=e.parentNode)
			if(e.scrollTop)y-=e.scrollTop;
		return y;
	}
	
	this.getX=function(el){
		var x=0;
		while(el){
			x+=el.offsetLeft;
			el=el.offsetParent;
		}
		return x;
	}

	this.tips=function(id,str){
		var obj=document.getElementById(id);
		var x=this.getX(obj);
		var y=this.getY(obj);
		var l=x;
		var t=y;
		var popObj=document.getElementById("tips");
		if(popObj!=null){
			popObj.innerHTML="<div class=\"tipcon\">"+str+"</div>";
			popObj.style.display="inline";
			var menu_h=popObj.offsetHeight;
			t-=menu_h;popObj.style.left=l+"px";
			popObj.style.top=t+"px";
		}
	}

	this.outtips=function(){
		document.getElementById("tips").style.display='none';
	}

}


