/**
* File Name: validate.js
* Author: ibm
* Date: 2000-8-21
* Reversion:
* Copyright (c) 2000 by AsiaEC.com, Inc. All Rights Reserved.
* 
*/

function Validate(frmName)
{
	 var frmName=document.forms[frmName];
	 var msg = "";
	 Validate.prototype.getMsg=function()
	 {
	 	return msg;
	 }
	 
	/**
	* 非空校验
	* frmName 表单名称
	* txtName 输入框名称
	* txtLab 输入框标签名
	*/
	Validate.prototype.isNull=function(txtName,txtLab)
	{
	
		var temp;
		temp=frmName.elements[txtName].value;
		if(temp.Trim()=="")
		{
			msg += "请输入" + txtLab + "！\r\n";
		}
		
	}
		/**
	* 数字校验
	* frmName 表单名称
	* txtName 输入框名称
	* txtLab 输入框标签名
	*/
	Validate.prototype.isNumber=function(txtName,txtLab)
	{
	
		var temp;
		temp=frmName.elements[txtName].value.Trim();
		if(temp.match(/\D/))
		{
			msg +=   txtLab + "只能是数字！\r\n";
		}
		
	}
		/**
	* 数字校验或者两位小数
	* frmName 表单名称
	* txtName 输入框名称
	* txtLab 输入框标签名
	*/
	Validate.prototype.isDNumber=function(txtName,txtLab)
	{
	
		var temp;
		temp=frmName.elements[txtName].value.Trim();
		if(temp.match(/^[0-9]*\.{0,1}[0-9]{1,2}$/)||temp.match(/^[0-9]+\.$/))
		{
			
		}
		else{
			msg +=   txtLab + "只能是数字或者两位小数！\r\n";
		}
		
	}
	
	Validate.prototype.inNumberRange=function(txtName,txtLab,minNumber,maxNumber)
	{
		var temp;
		temp=frmName.elements[txtName].value.Trim(); 
		len=temp.length; 
		dotNum=0; 
		if (len==0)
		{ 
			msg +=txtLab + "必须在"+minNumber+"和"+maxNumber+"之间！\r\n";
			return;
		}
		for(var i=0;i<len;i++)
		{ 
			oneNum=temp.substring(i,i+1); 
			if (oneNum==".") 
			dotNum++; 
			if ( ((oneNum<"0" || oneNum>"9") && oneNum!=".") || dotNum>1) 
			{
				msg +=txtLab + "必须在"+minNumber+"和"+maxNumber+"之间！\r\n";
				return;
			}
		} 
		if (len>1 && temp.substring(0,1)=="0")
		{ 
			if (temp.substring(1,2)!=".") 
			{
				msg +=txtLab + "必须在"+minNumber+"和"+maxNumber+"之间！\r\n";
				return;
			}
		} 
		if(temp<minNumber || temp>maxNumber)
		{
			msg +=txtLab + "必须在"+minNumber+"和"+maxNumber+"之间！\r\n";
			return;
		}
		
	}
	
	Validate.prototype.isEq=function(txtName,txtLab,txtName2,txtLab2)
	{
		var temp;
		temp=frmName.elements[txtName].value.Trim();
		var temp2;
		temp2=frmName.elements[txtName2].value.Trim();
		if(temp!=temp2)
		{
			msg+=txtLab+"和"+txtLab2+"不相同！\r\n";
		}
	}
	/***判断是否为邮箱***/
	Validate.prototype.isEmail=function(txtName,txtLab)
	{
			var temp;
			temp=frmName.elements[txtName].value.Trim();
	        if(temp.match(/[\w-.]+@{1}[\w-]+\.{1}\w{2,4}(\.{0,1}\w{2}){0,1}/ig)!=temp)
	               msg += "请正确输入" + txtLab + "！\r\n";
	    
	}
	
	Validate.prototype.isMobile=function(txtName,txtLab)
	{
		var temp;
		temp=frmName.elements[txtName].value.Trim();
		if(!temp.isMobile())
		{
			msg += "请输入正确的" + txtLab + "！\r\n";
		}
		
	}
	
	Validate.prototype.isTel=function(txtName,txtLab)
	{
		var temp;
		temp=frmName.elements[txtName].value.Trim();
		if(!temp.isTel())
		{
			msg += "请输入正确的" + txtLab + "！\r\n";
		}
		
	}
		//时间比较函数
	Validate.prototype.cmpdate=function(txtName1,txtName2,txtLab1,txtLab2){
		var a,ass,aD,aS;
		var b,bss,bD,bS;
		a=frmName.elements[txtName1].value.Trim();	//得到第一个时间；
		b=frmName.elements[txtName2].value.Trim();	//得到第二个时间；	
		ass=a.split("-");					  	//以"-"分割字符串，返回数组；
		aD=new Date(ass[0],ass[1],ass[2]);		//格式化为Date对像;
		aS=aD.getTime();	//得到从 1970 年 1 月 1 日开始计算到 Date 对象中的时间之间的毫秒数
		
		bss=b.split("-");
		bD=new Date(bss[0],bss[1],bss[2]);
		bS=bD.getTime();
		
		if(bS<aS){
			msg += txtLab1 + "不能超过"+txtLab2+"！\r\n";
		}
	}
	
	
	//校验网址URL
	Validate.prototype.isUrl=function(txtName,txtLab)
	{
		var temp;
		temp="http://"+frmName.elements[txtName].value.Trim();
	    var pattern = /^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/;
	    if(!pattern.exec(temp))
	        msg += "请输入正确的" + txtLab + "！\r\n";
	}
	String.prototype.isMobile = function() {  
  		return (/^(?:13\d|15[689])-?\d{5}(\d{3}|\*{3})$/.test(this.Trim()));  
	} 
	String.prototype.isTel = function()
	{
	    //"兼容格式: 国家代码(2到3位)-区号(2到3位)-电话号码(7到8位)-分机号(3位)"
	    return (/^(([0\+]\d{2,3}-)?(0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/.test(this.Trim()));
	}
	
	String.prototype.Trim = function()
	{
	     return this.replace(/(^\s*)|(\s*$)/g, "");
	}
	String.prototype.LTrim = function()
	{
	     return this.replace(/(^\s*)/g, "");
	}
	String.prototype.Rtrim = function()
	{
	     return this.replace(/(\s*$)/g, "");
	}
	
	String.prototype.replaceAll=function(s1,s2)
	{	var demo=this;
		 while(demo.indexOf(s1)!=-1)
		 demo=demo.replace(s1,s2);
		 return demo;
	}
	
	
	

	
}