//**********************************************************
//	文件：check.js
//  创建：宫能方[2011/08/18]
//  功能：1、字符串处理方法扩展 [String类] 
//        2、录入项有效性验证 [jquery插件]
//        3、客户端ID自动生成 [jquery插件]
//**********************************************************
// 1、字符串处理方法扩展 ---- begin
// 截取两端空格
String.prototype.trim = function(){ return this.replace(/^\s+|\s+$/g, ""); }  
String.prototype.trimRight = function(){ return this.replace(/\s+$/g, ""); }  
String.prototype.trimLeft = function(){ return this.replace(/^\s+/g, ""); }  
String.prototype.trimR = function(){ return this.trimRight(); }                     //别名-trimRight 
String.prototype.trimL = function(){ return this.trimLeft(); }                      //别名-trimLeft   
//
// 删除字符串中两端及中间全部空字符
String.prototype.trimAll = function () { return this.replace(/\s/g, ""); }
String.prototype.cutBlank = function () { return this.trimAll(); }                 //别名-trimAll
//
// 字符替换
String.prototype.replaceAll  = function(s1,s2){ return this.replace(new RegExp(s1,"gm"),s2);    }  
//
// 字节长度
String.prototype.lengthB = function () { var s = this.replace(/[^\x00-\xff]/g,"..");	return s.length; }
//
// 千分位
String.prototype.colonInt = function(){     //设置整型数千分位
    var s = this.trim();
    if(this.isInt()) return this.replace(/(?=(?!\b|-)(?:\d{3})+(?!\d))/g, ",");
    return this;
},
String.prototype.colon = function () {      //设置整型和浮点
    if( this.isFloat()){
        var s = this.uncolon();
        var a = s.split(".");
        return a.length==1 ? a[0].colonInt() : a[0].colonInt() + "." + a[1] ;
    }
    return this;
},
String.prototype.uncolon = function(){ return this.trim().replace(/,/g,""); },      // 取消千分位
// 数值格式化：n=小数位数、colon=true|false
String.prototype.numFormat = function(n, colon)
{
    var s = this.uncolon();
    if(s.isFloat()){
         return colon ? parseFloat(s, 10).toFixed(n).colon() : parseFloat(s, 10).toFixed(n) ;
    }
    return this;
},
//
// 格式验证
String.prototype.isEmpty = function () { return this.trim()==""; }                                              //两端截取后为空
String.prototype.isMailbox = function () { var re = /^\w+@(\w+\.)+\w{2,3}$/; return re.test( this.trim() ); }      //电子邮箱
String.prototype.isUrl = function () { var re = /^(\w+\.)+\w{2,3}(\/\w+)*(\/|\/\w+\.\w+)?$/; return re.test( this.trim() ); }                   //[不带http:// ]URL
String.prototype.isHttp = function () { var re = /^(http:\/\/)(\w+\.)+\w{2,3}(\/\w+)*(\/|\/\w+\.\w+)?$/; return re.test( this.trim() ); }       //[带http:// ]URL
String.prototype.isHttps = function () { var re = /^(https:\/\/)(\w+\.)+\w{2,3}(\/\w+)*(\/|\/\w+\.\w+)?$/; return re.test( this.trim() ); }     //[带https:// ]URL
//
String.prototype.isW = function () { var re = /^\w+$/; return ( re.test(this.trim()) ) }        //正则表达式中w表示[字母/数子/下划线]
String.prototype.isId = function () { return this.isW(); }                                      //约定：Id由[字母/数子/下划线]组成
String.prototype.isD = function (n) {        	                                                //[n(数值)位]数字字符串
    s = this.trim();
    var re = /^\d+$/;
    return n ? (s.length==n ? re.test(s) : false) : re.test(s);  
}
String.prototype.isMobile = function () { return this.isD(11); }	                            //11位手机号码
String.prototype.isPostcode = function () { return this.isD(6); }	                            //6位邮编
//
String.prototype.isInt = function () { var v=Number(this);  return isNaN(v) ? false : parseInt(this)==v ? true : false; }
String.prototype.isFloat = function () { return !isNaN( Number(this) ); }                       //浮点数
String.prototype.isReal = function () { return this.isFloat(); }	                            //浮点数-实数别名
String.prototype.isDouble = function () { return this.isFloat(); }	                            //浮点数-双精度别名
//
String.prototype.isDateString = function () {	                            //日期格式-仅支持“减号”分割[yyyy-mm-dd]
	var re = /^(\d{4})-(1[0-2]|0?[1-9])-(0?[1-9]|[12][0-9]|3[01])$/;
	return (re.test(this.trimAll()));
}
String.prototype.isTimeString = function () {	                            //日期格式-仅支持“分号”分割[hh:mm:ss]
	var re = /^([01]?[0-9]|[0-9]|2[0-3]):[0-5]?[0-9](:[0-5]?[0-9])?$/;
	return (re.test(this.trimAll()));
}
//
// escape编码改良(+置换成%2B)-->服务器端HttpUtility.UrlDecode解码
function encode(s){ return escape(s).replace(/\+/g, "%2B");    }  
function decode(s){ return unescape(s.replace(/%2B/g, "+"));    }  
//
// 1、字符串处理方法扩展 ---- end
//
//**********************************************************
// 2、录入项有效性验证 ---- begin
$.fn.extend({
    // 不许为空----必填项验证    
    noEmpty : function (prompt){
        if($(this).val().isEmpty()){
            alert(prompt);
            $(this).focus();
            return false;
        }
        return true;
    },
    // 非法字符----整型数值验证    
    beInt : function (prompt){
        if($(this).val().trim() == "") return true;
        if(! $(this).val().isInt()){
            alert(prompt);
            $(this).focus();
            return false;
        }
        return true;
    },
    // 非法字符----整型数值验证    
    beFloat : function (prompt){
        if($(this).val().trim() == "") return true;
        if(! $(this).val().isFloat()){
            alert(prompt);
            $(this).focus();
            return false;
        }
        return true;
    },
    // 格式错误----电子邮箱验证    
    beMailbox : function (prompt){
        if($(this).val().trim() == "") return true;
        if(! $(this).val().isMailbox()){
            alert(prompt);
            $(this).focus();
            return false;
        }
        return true;
    },
    // 格式错误----日期[yyyy-mm-dd]    
    beDate : function (prompt){
        if($(this).val().trim() == "") return true;
        if(! $(this).val().isDateString()){
            alert(prompt);
            $(this).focus();
            return false;
        }
        return true;
    },
    // 格式错误----时间[hh:mm | hh:mm:ss]    
    beTime : function (prompt){
        if($(this).val().trim() == "") return true;
        if(! $(this).val().isTimeString()){
            alert(prompt);
            $(this).focus();
            return false;
        }
        return true;
    },
    // 长度限制[Byte]    
    limitLength : function (len, prompt){
        if(! $(this).val().lengthB() <= len ){
            alert(prompt);
            $(this).focus();
            return false;
        }
        return true;
    },
       // 长度限制[Byte]    
       mustLength: function (len, prompt) {
       	if (!$(this).val().lengthB() == len) {
       		alert(prompt);
       		$(this).focus();
       		return false;
       	}
       	return true;
       },

	   
   
    end : {}    
});
// 3、录入项有效性验证 ---- end
//
//**********************************************************
// 3、客户端ID自动生成 ---- begin
// 自动编码：8-16位
// 调用方法：1、$.ID.id(n)， n(数值)=8-16[<8取8、大于16取16]
//           2、$.ID.idx()， x=8-16， 如：$.ID.id10 等价 $.ID.id(10)  
$.ID ={
    count   : 10000,
    key     : "ABCDEFGHIJKLMNoPQRSTUVWXYZ0123456789",
    k1      : "",
    k2      : "",
    k3      : "",
    //
    prepare : function(){
        var d = new Date;
        this.k1 = this.encoding(Date.parse(d) + d.getMilliseconds(), 8);
        this.k2 = this.encoding(this.count++, 2);
        this.k3 = this.encoding(parseInt( 100000000*Math.random(d.getMilliseconds())),6);
    },
    id     : function(bits){
        if(! bits || bits <= 8) return this.id8();
        if(bits == 9) return this.id9();
        if(bits == 10) return this.id10();
        if(bits == 11) return this.id11();
        if(bits == 12) return this.id12();
        if(bits == 13) return this.id13();
        if(bits == 14) return this.id14();
        if(bits == 15) return this.id15();
        if(bits == 16) return this.id16();
    },
    //////
    id8    : function(){ this.prepare(); return this.k1.substring(0,7) + this.k2.charAt(1);     },
    id9    : function(){ this.prepare(); return this.k1 + this.k2.charAt(1);    },
    id10   : function(){ this.prepare(); return this.k1.substring(0,6) + "_" + this.k1.substring(6)  + this.k2.charAt(1);     },
    id11   : function(){ this.prepare(); return this.k1.substring(0,6) + "_" + this.k1.substring(6)  + this.k2.charAt(1)  + this.k3.charAt(5);     },
    id12   : function(){ this.prepare(); return this.k1.substring(0,6) + "_" + this.k1.substring(6)  + this.k2.charAt(1)  + this.k3.substring(4);     },
    id13   : function(){ this.prepare(); return this.k1.substring(0,6) + "_" + this.k1.substring(6)  + this.k2.charAt(1)  + this.k3.substring(3);    },
    id14   : function(){ this.prepare(); return this.k1.substring(0,6) + "_" + this.k1.substring(6)  + this.k2.charAt(1)  + this.k3.substring(2);     },
    id15   : function(){ this.prepare(); return this.k1.substring(0,6) + "_" + this.k1.substring(6)  + this.k2.charAt(1)  + this.k3.substring(1);     },
    id16   : function(){ this.prepare(); return this.k1.substring(0,6) + "_" + this.k1.substring(6)  + this.k2.charAt(1)  + this.k3;    },
    //n=运算值 bits=位数(最大8位)
    encoding : function (n, bits){
        bits = bits < 1 ? 1 : bits % 9;  //最大8位、最少1位限制 
        n += 36^bits;
        var m = this.key.length;
        var s = "";
        for(var i=0; i<bits; i++ ){
            s = this.key.charAt(n % m) + s;
            n = parseInt(n / m)
        }
        return s;
    },

    end:{}
}
// 2、客户端ID自动生成 ---- end
//
// end
//**********************************************************

