/****************************************************************
 *   数据校验 Regular Expression
 ****************************************************************/	
function Validate() {}
/****************************************************************
 *log 校验记录
 *logend 输出错误
 ****************************************************************/
Validate.prototype.errorString = "";
Validate.prototype.LM_NONE  =0;   //log mode = do nothing
Validate.prototype.LM_ALERT =1;   //log mode = alert
Validate.prototype.LM_ERRS  =2;   //log mode = appand after errorString
Validate.prototype.LM_CLEAR =99;  //clear old error message
Validate.prototype.LM_KEEP =100; //keep the error message
Validate.prototype.log = function()
{  var showMode=0;
   var msg="";
   if(arguments.length>0){
      msg=arguments[0];
      if(arguments.length>1)  showMode=arguments[1];      
      switch(showMode){
       case this.LM_NONE:       
            break;	
       case this.LM_ERRS:
            this.errorString =this.errorString+msg+"\n";     
            break;
       case this.LM_ALERT:                      
       default: 
           if(msg.length>0) alert(msg);        
     }   
  } 
}
Validate.prototype.logend = function()
{  
   var clearMode=this.LM_CLEAR;       
   if(arguments.length>0)      clearMode=arguments[0];
   if(this.errorString.length>0)    alert(this.errorString);   
   if(clearMode==this.LM_CLEAR)      this.errorString="";
   
}
/***************************************************************
 *空值校验
 *
 ***************************************************************/
Validate.prototype.isNullValue = function (obj,name,showAlert)
{ var ret=false; 
  if(obj==null) ret=true;
   if(typeof(obj)=="string") ret=(obj.length==0);
   if("undefine"==(""+obj))  ret=true;
   if(ret) {this.log(name+"不能为空！",showAlert);}
   return ret;   
}
/****************************************************************
 *旁空
 *
 ****************************************************************/
Validate.prototype.isNULL = function(v,name,showAlert){
  var ret=false;
  if(v.length==0) {this.log(name+"不能为空！",showAlert);ret=true;}
  return ret;
}
/****************************************************************
 * isValidUserName 
 * 合法用户名-以字母数字开始的32位以内的字串包含下划线。
 ****************************************************************/
Validate.prototype.isValidUserName = function(string)
{
	var validUserNameRegExp = /^([a-z0-9])[a-z_0-9]{0,31}$/i;
	var isValid = validUserNameRegExp.test(string);
	return isValid;
}
Validate.prototype.isValidNickName = function(string)
{  var isValid=false;
  if(string.length>0) isValid=true;
	for(var i=0;i<string.length;i++)
	{  var ch=string.charAt(i);
	  isValid=(this.isOnlyChineseCharSet(ch)||this.isOnlyAlphaNumericUL(ch))&&isValid;  	
	}	
	return isValid;
}
/***************************************************************
 *包含中文字符校验
 *
 ***************************************************************/
Validate.prototype.isIncludeChineseCharSet = function (string)
{   var validRegExp=/[^\w,<>\.\{\}\[\];:"'?\/|!@#\$%\^&\*()-=\+`~]/i
     var isValid=validRegExp.test(string);
     return isValid;
}
/***************************************************************
 *仅包含中文字符
 *
 ***************************************************************/
Validate.prototype.isOnlyChineseCharSet = function (string)
{   var validRegExp=/^[^\w,<>\.\{\}\[\];:"'?\/|!@#\$%\^&\*()-=\+`~]+$/i
     var isValid=validRegExp.test(string);
     return isValid;
}
/***************************************************************
 *仅包含字母数字下划线
 *
 ***************************************************************/
Validate.prototype.isOnlyAlphaNumericUL = function (string)
{   var validRegExp=/^\w$/i
     var isValid=validRegExp.test(string);
     return isValid;
}
/**************************************************************
 * Email校验
 ***************************************************************/
Validate.prototype.isValidEmail = function(email)
{
   var validFormatRegExp = /^\w(\.?\w)*@\w(\.?[-\w])*\.([a-z]{3}(\.[a-z]{2})?|[a-z]{2}(\.[a-z]{2})?)$/i
   var isValid = validFormatRegExp.test(email);
   return isValid;
}
/**************************************************************
 * 身份证校验
 ***************************************************************/
Validate.prototype.isValidIDCard = function(idcard)
{
   var validIDCardRegExp = /^(\d{15}|\d{17}[0-9a-z])$/i
   var isValid = validIDCardRegExp.test(idcard);
   return isValid;
}
/**************************************************************
 * 邮编校验
 ***************************************************************/
Validate.prototype.isValidPostalCode = function(postalCode)
{
   var validFormat = /^[0-9]{6}$/
   var isValid = validFormat.test(postalCode);
   return isValid;
}

/**************************************************************
 * 整数
 ***************************************************************/
Validate.prototype.isValidInteger = function(string)
{
	var validCharactersRegExp = /^(0|[1-9][0-9]*)$/;
	var isValid = validCharactersRegExp.test(string);	
	return isValid;
}
/**************************************************************
 * 大于零的整数
 ***************************************************************/
Validate.prototype.isValidIntegerGreatZero = function(string)
{
	var validCharactersRegExp = /^[1-9][0-9]*$/;
	var isValid = validCharactersRegExp.test(string);	
	return isValid;
}
/**************************************************************
 * 密码校验
 ***************************************************************/
Validate.prototype.isValidPassword = function(password)
{
	var invalidCharactersRegExp = /[^\w]/i;
	var isValid = !(invalidCharactersRegExp.test(password));
	if (isValid){isValid = (password.length >= 6 && password.length <= 16);}
	return isValid;
}
/***************************************************************
 *空格校验
 *
 ***************************************************************/
Validate.prototype.isAllSpace = function(string,name,showAlert)
{                var ret=false;
                  if(this.isNullValue(string,name,this.LM_NONE)){
                   ret=true;}else{
	var validCharactersRegExp = /^ +$/;
	ret = validCharactersRegExp.test(string);
	}
	return ret;
}
Validate.prototype.execute = function (string)
{
     return eval(string);
}	
/****************************************************************
 * isDicemalfraction
 * 小数数字
 ****************************************************************/	
Validate.prototype.isDicemalfraction = function(string)
{
	var invalidCharactersRegExp =/[^\d.]/;
	var isValid = !(invalidCharactersRegExp.test(string));
	
	return isValid;
}
Validate.prototype.isValidFloatingPoint = function(string)
{
	var invalidCharactersRegExp = /[^\d\.-]/;
	var isValid = !(invalidCharactersRegExp.test(string));
	
	return isValid;
}

Validate.prototype.toNumber = function(num,I,F){
var newnum=num;
for(var i=0;i<I;i++) newnum=newnum*10;
newnum=Math.round(newnum);
var newnum=""+newnum+"";
var x=newnum.length;y=x-I; 
 if(y<0) newnum="0.00";
 else   newnum=newnum.substr(0,y)+"."+newnum.substr(y,x);
 return newnum;
}
Validate.prototype.getStr = function (chr,num){
   var  newstr="";
    for(var i=0;i<num;i++)   newstr=newstr+chr;
   return  newstr;
}

