var ills=["你奶奶","法轮","法伦","邪教","反革命","反政府","反共","他妈的","操他妈","王八","日你妈","操你","大日本","法西斯","共匪","胡锦涛","江泽民","温家宝","色情","情色","性交","性爱","做爱","淫秽","黄色","成人","枪支","弹药","迷药","蒙汉药","毒药","纯情妹","富婆","六合彩","六合采","特码"];
var bads=["<",">",",","?","(",")","#","&",":","!",";","'\"","\/","\\","^","*","+","|","`","~","[","]","{","}","=","%"];

Object.Ext = function(destination, source) {//目标对象将拥有源对象的所有属性和方法
  var property;
  for (property in source) {
    destination[property] = source[property];
  }
  return(destination);
};
//数字扩展
Object.Ext(Number.prototype, {
	toInt:function(){//将数字四舍五入成最接近的整数
		return(Math.round(this));
	},
	toFloat:function(){
		return(this);
	},
	toTrue:function(){
		if(this>=1){
			return(true)
		}
		else{
			return(false);
		}
	},
	chr:function(){//返回指定数字对应的字符
		return(String.fromCharCode(this));
	},
	asc:function(){//返回指定数字串对应的ASC数字
		return(String(this).asc());
	},
	zero:function(n){//给不足位的数字补零,返回补0后的字符串,n:最少位数
		return(this.toString().zero(n));
	},
	rand:function(max){//产生随机数(max:上限;this:下限)
		return(parseInt(Math.floor(Math.random() * ((Number(max)-this) + 1)) + this));
	},
	abs:function(){//返回由指定数字的绝对值
		return(Math.abs(this));
	},
	getSize:function() {
		if (this >= 1048576) {
			return (Math.ceil(this/104857.6)/10 + " MB");
		} else if (this >= 0) {
			return (Math.ceil(this/1024) + " KB");
		} else {
			return (this);
		}
	}
});

//字符串扩展
Object.Ext(String.prototype, {
	trim: function(){//去掉字符串的前后空格
		if (isNull(this)) return "";
		var strs=this;
		if (strs=="") return "";
		while ((strs.charAt(0)=='\n') || (strs.charAt(0,1)=='\r')) strs=strs.substring(1,strs.length);
		while ((strs.charAt(strs.length-1)=='\n') || (strs.charAt(strs.length-1)=='\r')) strs=strs.substring(0,strs.length-1);
		strs=strs.replace(/(^\s*)|(\s*$)/g, "");
	return(strs);
	},
	reg:function(para){return(para?new RegExp(this,para):new RegExp(this));},
	tagindex:function(tags){return(this.Ucase().search(("<"+tags.Ucase()+".*?>").reg()));},
	//stripScripts: function() {return this.replace(new RegExp(para.ScriptFragment, 'img'), '');},
	//extractScripts: function() {return (this.match((para.ScriptFragment).reg("img")) || []).map(function(scriptTag) {return (scriptTag.match((para.ScriptFragment).reg("im")) || ['', ''])[1];});},
	//evalScripts: function() {return this.extractScripts().map(function(script) { return eval(script) });},
	Ltrim: function(){return(this.replace(/^\s+/,""));},
	Rtrim: function(){return(this.replace(/\s+$/,""));},
	value: function(name){//得到变量字符串中的值(var a='b="para" c="hong" d="jun"')
		var temp = "(^|\\s)"+name+"\\s*=\\s*(\"|\')([^\"|\']*)\\2(\\s|$)";
		temp = this.match(temp.reg("i"));
		if (temp!=null) return(temp[3]);
		temp = null;
		return("");
	},
	sql:function (){//将特殊字符替换掉//不计空格(32)
		if (!isArray(bads)) return(this);
		var sArr=bads,str=this.replace(/\[/gi,"").replace(/\]/gi,"");
		var sArr=bads,str=this.replace(/\·/gi,"-").replace(/\¤/gi,"-").replace(/\×/gi,"-").replace(/\С/gi,"-").replace(/\°/gi,"-");
		str=str.replace(/\\/gi,"").replace(/\+/gi,"").replace(/\*/gi,"").replace(/\?/gi,"？");
		str=str.replaceAll("'","").replaceAll((34).chr(),"").replaceAll((9).chr(),"").replaceAll((10).chr(),"").replaceAll((13).chr(),"").trim();
		for(var i=0; i<sArr.length; i++){if(str.inc(sArr[i],null)){str=str.replaceAll(sArr[i],sArr[i].toBig());}}
		str=str.trim();
		sArr=null;
		return(str);
	},
	sqls:function (){return(this.replace(/\"/gi,"").replace(/\'/gi,"").replace(/\;/gi,"").replace(/\n/gi,"").replace(/\r/gi,"").replace(/\\/gi,"").replace(/\#/gi,"").replace(/\+/gi,"").replace(/\,/gi,"，").replace(/\*/gi,"").replace(/\(/gi,"（").replace(/\)/gi,"）"));},
	replaceAll: function(s,ss){
		var str=this;
		while(str.inc(s,null)){str=str.replace(s,ss);}
		return(str);
	},
	replaceHtml: function(){
		var retvalue=this.replace(/\{(.*)+\}/gi,"").replace(/&nbsp;/gi,"").replace(/<\/?[^>]+>/gi,"");
		retvalue=retvalue.replaceAll((9).chr(),"").replaceAll((32).chr(),"");
		//retvalue=retvalue.replaceAll((10).chr(),"").replaceAll((13).chr(),"");
		return(retvalue);
	},
	tagToLcase:function(){//将字符串中所有的标签中转为小写
		var reg,Match,retValue;
		reg = /<\/?[^>]+>/gi;
		retValue = this;
		while(((Match=reg.exec(retValue))!=null)){retValue=retValue.replace(Match[0],Match[0].Lcase());}
		return(retValue);
	},
	noEvent:function(){//过滤字符串中标签的ON事件
		return(this.replace(/on[a-z]+\s*=\s*"[^"]*"\s*/gi,""));
	},
	/*toHTML:function (){
		var s=this;
		s = s.replaceAll("&","&amp;");
		s = s.replaceAll("<","&lt;");
		s = s.replaceAll(">","&gt;");
		s = s.replaceAll("\"","&quot;");
		s = s.replaceAll("'","&apos;");
		return(s);
	},
	unHTML:function (){
		var s=this;
		s = s.replaceAll("&amp;","&");
		s = s.replaceAll("&lt;","<");
		s = s.replaceAll("&gt;",">");
		s = s.replaceAll("&quot;","\"");
		s = s.replaceAll("&apos;","'");
		return(s);
	},*/
	toQueryParams: function() {
	//将查询字符串格式的字符串转换为键值对数组
	//例如var s="a=1&b=2&c=3"; var s2=s.toQueryParams(); s2为{a:1,b:2,c:3}
		var pairs = this.match(/^\??(.*)$/)[1].split('&');
		return pairs.inject({}, function(params, pairString) {
		  var pair = pairString.split('=');
		  params[pair[0]] = pair[1];
		  return params;
		});
	},
	count:function(s){//字符s在字符串中出现的次数
		var retValue=0,i;
		for (i=0; i<this.length; i++){
			if(s==this.charAt(i)){
				retValue++;
			}
		}
		return(retValue);
	},
	inc: function(k1,k2){//字符是否是在字符串中
		return k2==null?this.indexOf(k1)>-1?true:false:(k2+this+k2).indexOf(k2+k1+k2)>-1?true:false;
	},
	ind: function(str){//字符串中第一个出现指定字符串的位置
		return(this.indexOf(str));
	},
	Lind: function(str){//字符串中最后个出现指定字符串的位置
		return(this.lastIndexOf(str));
	},
	anc: function(anchor){//为字符串建立HTML锚记并插入到指定元素内
		return(this.anchor(anchor));
	},
	con: function(){//将两个或两个以上的字符串组合到一起
		var arg=arguments,temp=this;
		for (var i=0; i<arg.length; i++){
			temp=temp.concat(arg[i]);
			}
		return(temp);
	},
	toArray: function() {
		return this.split("");
	},
	Left: function(n){
	//从左边截取n个字符 ,如果包含汉字,则汉字按两个字符计算
	//alert("abcdefg".left(3)==="abc")
	//alert("中国abcdefg".left(5)==="中国a")
	if (this.getByte()>n){
		var temp="",len=0;
		for (var i=0; i<this.length; i++){
			len++;
			if((this.charAt(i)).isChinese()) len++;
			temp+=this.charAt(i);
			if (len>=n) break;
			}
			return(temp+"...");
		 }
		 else
		 return(this);
	},
	Right: function(n){
		//从右边截取n个字符 ,如果包含汉字,则汉字按两个字符计算
		//alert("abcdefg中国".right(5)==="g中国")
		//alert("abcdefg".right(3)==="efg")
		if (this.getByte()>n){
		var temp="",len=0;
		for (var i=(n-1); i>this.length; i++){
			len++;
			if((this.charAt(i)).isChinese()) len++;
			temp+=this.charAt(i);
			if (len>=n) break;
			}
			return(temp);
		 }
		 else
		 return(this);
		 //return this.slice((this.slice(-n).replace(/[\x00-\xff]/g,"").length-n));
	},
	toFixed: function(n){//格式化小数位数,n:格式化面几位小数,数字,为空时返回整数
		return(Number(this.trim()).toFixed(n));
	},
	getSize:function(){
		if (!(this.trim()).isInt()) return(this);
		return(Number(this.trim()).getSize());
	},
	toInt:function(){
		if (this.isInt()){return(Number(this).toInt());}
		return(0);
	},
	toFloat:function(){
		try{
			return(parseFloat(this));
		}
		catch(para){
			return(0);
		}
	},
	toTrue:function(){
		if(this.Lcase()=="true" || this=="1"){
			return(true)
		}
		else{
			return(false);
		}
	},
	Lcase:function(){//转换成小写字符串
		return(this.toLowerCase());
	},
	Ucase:function(){//转换成大写字符串
		return(this.toUpperCase());
	},
	chr:function(){//返回指定字符型ASC数字对应的字符
		if (this.isInt()) return(String.fromCharCode(Number(this)));
		 else return "";
	},
	asc:function(){//返回指定字符或字符串对应的ASC数字
		if (isNull(this)) return "";
		if (this.length==1) return(this.charCodeAt(0));
		else {
			var temp="";
			for (var i=0; i<this.length; i++){
				temp+=String(this.charCodeAt(i));
				}
				return(temp);
			}
	},
	todate:function(){//字符串(yyyy-mm-dd)转成日期型
		//if (!this.isDate()&& !this.isDateTime()) return "";
		return(new Date(this.replace(/-/g,'/')));
	},
	zero:function(n){//给不足位的字符串补零,n保证字符串的最少位数
		if (this.length>=n) return(this);
		var zero=this.toString();
		while(zero.length<n) zero="0"+zero.toString();
		return(zero);
	},
	getByte:function(){//得到字符串字节数(中文算两个)
		var blen = 0;
		for(i=0; i<this.length; i++) {
			if (this.charAt(i).isDoubleByte()) blen ++;
			blen ++;
		}
		return(blen);
	},
	reverse:function(){//字符串反转
		return(this.toArray().reverse().join(""));
	},
	abs:function(){//返回由指定数字符串的绝对值
		if (this.isNumber()) return(Math.abs(this));
		return("");
	},
	toBig:function (){//将指定(标点及特殊符号)字符串(除中文外)转换为大写
		if (isNull(this)) return("");
		var temp = "", tmp = "";
		for (var i=0; i<this.length; i++){
			tmp=this.charAt(i);
			if (tmp.isChinese() || tmp.asc()>255) temp+=this.charAt(i);
				else temp+=(tmp.asc()+65248).chr();
			}
		tmp=null;
		return(temp);
	},
	getChinese:function (){
		var temp="";
		for (var i=0; i<this.length; i++){
			if (this.charAt(i).isCN()) temp+=this.charAt(i);
		}
		return(temp);
	},
	
//判断函数开始
	isill:function (){//判断字符串是否有不良词汇
		if (!isArray(ills)) return("");
		var ill=ills,str=this.getChinese(),reValue="";
		for (var i=0; i<ill.length; i++) {
			if (str.inc(ill[i],null)) {
					reValue=ill[i];
					break;
				}
			}
			ill=str=null;
		return(reValue);
	},
	isDateTime: function(){
		//1.判断是否符合 yyyy-mm-dd hh:mm:ss的日期格式, 而且闰年闰月等也要正确
		//alert("2002-12-12 10:10:40".isDateTime())  //true
		 try{
			 var arr=(this.length==19)?this.split(/\D/):[];
			 --arr[1];
			 eval("var d=new Date("+arr.join(",")+")")    
			 return     Number(arr[0])==d.getFullYear() && Number(arr[1])==d.getMonth() && Number(arr[2])==d.getDate() && Number(arr[3])==d.getHours() && Number(arr[4])==d.getMinutes() && Number(arr[5])==d.getSeconds();
			 }
			 catch(x){
				 return false;
		}
	},
	isDate:function(){//日期正则表达式(yyyy-mm-dd),如闰年,2月等都不能错
		var reg = /^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$/;
		//var reg1=/^(\d{4})([-])(\d{2})([-])(\d{2})/;//yyyy-mm-dd
		//var reg2=/^(\d{4})([\/])(\d{2})([\/])(\d{2})/;//yyyy/mm/dd
		return(reg.test(this));
	},
	StartWith: function(){
		// 检查 是否以特定的字符串开始
		//alert("abc".StartWith("a"))  //true
		var _string=this
		return $A(arguments).any(function(value){return _string.slice(0,value.length)==value})
	},
	EndWith: function(){
		// 检查 是否以特定的字符串结束
		//alert("abc".EndWith("c"))  //true
		 var _string=this;
		 return $A(arguments).any(function(value){return _string.slice(value.length*(-1))==value});
	},
	isColor: function(){
		//判断是否是颜色值
		if (this.length!=7) return false;
		return (this.search(/\#[a-fA-F0-9]{6}/) != -1);
	},
	isInt:function(){//字符串是否由0-9的数字组成
		return(/^[0-9]+$/.test(this));	
	},
	isLetter:function(){//字符串是否由字母组成
		return(/^[a-zA-Z]+$/.test(this));	
	},
	isNumberLetter:function(){//字符串是否由数字和字母组成
		return(/^[0-9a-zA-Z]+$/.test(this));	
	},
	isNumberLowercase:function(){//字符串是否由数字和小写字母组成
		return(/^[0-9a-z]+$/.test(this));	
	},
	isNumber_Letter:function(){//字符串是否由数字和字母及_组成
		return(/^[0-9a-zA-Z\_]+$/.test(this));	
	},
	isNumber_Letter_Chinese:function(){//字符串是否由数字和字母及中文
		return(new RegExp("^[0-9a-zA-Z\u4e00-\u9fa5]+$").test(this));
	},
	isNumber:function(){//验证正负整数或正负小数
		return(/(^[-|+]?\d\d*\.\d*$)|(^[-|+]?\d\d*$)|(^[-|+]?\.\d\d*$)/.test(this));
	},
	isDecimal:function(){
		return(/(^[0-9]+$)|(^\d*\.\d*$)/.test(this));
	},
	isInteger:function(){//字符串是否正整数组成
	if (!this.isInt()) return false;
		return(!!Number(this.substr(0,1)));	
	},
	isTel:function(){//验证国内电话号码(含分机号(1-5位):0731-8929559-123,分机号可以有或没有)
		return(/^(\d{3}-\d{8}|\d{4}-\d{7,8})(\-\d{1,5})?$/.test(this));
	},
	isMobile:function(){//验证手机号
		return(/^1[3|5][0-9]\d{8}$/.test(this));
	},
	isEmail:function(){//验证Email
		return(/^([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5})$/.test(this));
	},
	isIp:function(){//验证IP地址/^((\d|[1-9]\d|2[0-4]\d|25[0-5]|1\d\d)(?:\.(\d|[1-9]\d|2[0-4]\d|25[0-5]|1\d\d)){3})$/
		return(/^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$/.test(this));
	},
	isZip:function(){//验证邮政编码
		return(/^\d{4,6}$/.test(this));
	},
	isQQ:function(){//验证QQ
		return(/^\d{5,12}$/.test(this));
	},
	isIdentity:function(){//验证身份证号
		return(/^\d{15}$|^\d{18}$|^\d{17}x$/.test(this));
	},
	isDomain:function(){//验证域名(不含http://)
		try{return(/^([\w-]+\.)+((com)|(net)|(org)|(gov\.cn)|(info)|(cc)|(com\.cn)|(net\.cn)|(org\.cn)|(name)|(biz)|(tv)|(cn)|(mobi)|(name)|(sh)|(ac)|(io)|(tw)|(com\.tw)|(hk)|(com\.hk)|(ws)|(travel)|(us)|(tm)|(la)|(me\.uk)|(org\.uk)|(ltd\.uk)|(plc\.uk)|(in)|(eu)|(it)|(jp))$/.test(this.Lcase()));}
		catch(para){return false;}
	},
	isURL:function(){//验证URL
		return(/^((ht|f)tp(s?))\:\/\/([0-9a-zA-Z\-]+\.)+[a-zA-Z]{2,6}(\:[0-9]+)?(\/\S*)?$/.test(this));
	},
	isFileURL:function(filetype){//验证带扩展名的文件地址
		if (isNull(this) || isNull(filetype)) return(false);
		filetype=filetype.Lcase();
		var Reg={};
		Reg["image"]=/^(.*?)\.(jpg|jpeg|png|gif|bmp|ico|tiff|wmf)+$/;
		Reg["media"]=/^(.*?)\.(wmv|asf|wma|mp3|mpeg|mpeg4|mp4|avi|vob|wav|midi|meg|ape|mpg)+$/;
		Reg["real"]=/^(.*?)\.(rm|ra|rmvb)+$/;
		Reg["flash"]=/^(.*?)\.(swf)+$/;
		Reg["flv"]=/^(.*?)\.(flv)+$/;
		Reg["file"]=/^(.*?)\.(rar|zip|doc|docx|wps|ppt|xls|exe)+$/;
		return(Reg[filetype].test(this.Lcase()));
	},
	isChinese:function(){//校验字符串是否为全部中文(含全角字符)
		return((/^([\u4E00-\u9FA5]|[\uFE30-\uFFA0])*$/gi).test(this));
	},
	isCN:function(){//校验字符串是否全部为纯中文(不含全角字符)
		var reValue=true;
		if (isNull(this)) return false;
		for (var i=0; i<this.length; i++) {//19968-40869
			if (this.charAt(i).asc()<13379 || this.charAt(i).asc()>40870) {
				reValue=false;
				break;
				}
			}
		return(reValue);
	},
	isDoubleByte:function(){//匹配双字节字符
		return((/^([^\x00-\xff])*$/gi).test(this));
	},
	isValidByte:function (min,max) {//检测字符串长度
		if (isNull(this)) return false;
		var blen = this.getByte();
		if (blen > max || blen < min) {
		return false;
		}
		return true;
	},
	isAccount:function(min,max){//匹配帐号是否合法,least:最大位数;max:最小位数
	if (!this.isNumber_Letter()) return false;
		return(this.isValidByte(min,max));
	}
});
//日期扩展
Object.Ext(Date.prototype, {
	format:function(type){
		var retValue="",returnType=type?type.toInt():0;
		switch(returnType){
			case 0://YYYY-MM-DD
				retValue=(this.getFullYear()+"-"+(this.getMonth()+1).zero(2)+"-"+this.getDate().zero(2)); break;
			case 1://(YYYY年MM月DD日)
				retValue=(this.getFullYear()+"年"+(this.getMonth()+1).zero(2)+"月"+this.getDate().zero(2)+"日"); break;
			case 2://YYYY-MM-DD HH:MM:SS
				retValue=(this.getFullYear()+"-"+(this.getMonth()+1).zero(2)+"-"+this.getDate().zero(2)+" "+this.getHours().zero(2)+":"+this.getMinutes().zero(2)+":"+this.getSeconds().zero(2)); break;
			case 3://YYYY年MM月DD日 HH时MM分SS秒
				retValue=(this.getFullYear()+"年"+(this.getMonth()+1).zero(2)+"月"+this.getDate().zero(2)+"日 "+this.getHours().zero(2)+"时"+this.getMinutes().zero(2)+"分"+this.getSeconds().zero(2)+"秒"); break;
			case 4://HH时MM分SS秒
				retValue=(this.getHours().zero(2)+"时"+this.getMinutes().zero(2)+"分"+this.getSeconds().zero(2)+"秒"); break;
			case 5://HH:MM:SS
				retValue=(this.getHours().zero(2)+":"+this.getMinutes().zero(2)+":"+this.getSeconds().zero(2)); break;
			case 6://格式化星期
				retValue=this.week(); break;
			case 7://YYYY年MM月DD日 星期 HH时MM分SS秒
				retValue=(this.getFullYear()+"年"+(this.getMonth()+1).zero(2)+"月"+this.getDate().zero(2)+"日 "+this.week()+" "+this.getHours().zero(2)+"时"+this.getMinutes().zero(2)+"分"+this.getSeconds().zero(2)+"秒"); break;
			default:
				break;
		}
		returnType=null
		return(retValue);
	},
	week:function(){//格式化星期
		var weeks=["星期日","星期一","星期二","星期三","星期四","星期五","星期六"];
		return(weeks[this.getDay()]);
	},
	comDate:function(Compare){//比较两个日期,如果Compare(日期2)为空或null，就与今天比较
		if (isNull(Compare)){Compare = new Date().date().todate();}
			else {
				if (!Compare.isDate()) return "";
					else Compare = Compare.todate();
				}
		var a = this.getTime(),b = Compare.getTime();
		if(a>b) return -1;//日期1在日期2之后
		if(a==b) return 0;//日期1与日期2是同一天
        if(a<b) return 1;//日期1在日期2之前
	}
});

