/**
 *[Util Js]
 *Javascript Framework Util Tools
 *@version Mizi ver1.1.20090512 | 20090326 
 ***/

/**
 * ==============================
 * Removes spaces on both sides of a string
 * ********/
 String.prototype.trim = function(){
	 return this.replace(/(^\s*)|(\s*$)/g, "");
 }
 
 Number.prototype.trim = function(){
	 return this;
 }
 
 Boolean.prototype.trim = function(){
	 return this;
 }
 
/**
 * ===============================
 * Removes spaces on the left side of a string
 * ******/ 
 String.prototype.lTrim = function(){
	 return this.replace(/(^\s*)/g, "");
 }
 
/**
 * ===============================
 * Removes spaces on the Right side of a string
 * ******/  
 String.prototype.rTrim = function(){
     return this.replace(/(\s*$)/g, "");
 }
 
/**
 * ==============================
 * Removes HTML code of a string
 * ******/ 
 String.prototype.clearHTML = function(){
     return this.replace(/<[^>].*?>/g,"");
 }
 
/**
 * ==============================
 * Whether or not two strings of the same string 
 * ***************/ 
 String.prototype.equals = function(s){
	 return (this == s );
 }
 
var StringUtil ={
		
/**
 * ================================
 * return boolean
 * *****/
	isString: function(/*any*/ anyVal ){
		return typeof( anyVal ) == "string";
	},
		
/**
 * ===============================
 * equals
 * @param str1,str2
 * @return boolean, true | false
 * *******/	
	equals: function(/*String*/ s1,/*String*/ s2){
		if(typeof(s1) != typeof(s2)) { return false; }
		return s1.equals(s2);
	},
	
/**
 * ===============================
 * max length
 * @param s, String
 * @param n, Integer
 * @return boolean, true | false
 * *******/	
	max: function(/*String*/ s,/*Integer*/ n){
		return (s.toString().trim().length> n);
	},
	
/**
 * ===============================
 * min length
 * @param s, String
 * @param n, Integer
 * @return boolean, true | false
 * *******/	
	min: function(/*String*/ s, /*Integer*/ n){
		return (s.toString().trim().length< n);
	},
	
/**
 * ===============================
 * notNull
 * @param s, String, null
 * @return String, s | ""
 * *******/	
	notNull: function(/*Anything*/ s){
		if(s ==null){ s =""; }
		return s.toString();
	},
	
/**
 * s2o
 * 字符串转对象
 * @param (String)s, 类对象字符串类型, 例: "{name:'a',type:'b'}" 
 * @return Object
 * ********/
	s2o: function(/*String*/ s){
		if( s==null || s =="" ){ return {}; };
		var opts;
		s = s.replace(/\s+/g,'').replace(/\r\n/g,'\\r\\n').replace(/\n/g,'\\r\\n').replace(/\,\\r\\n/g,'\,').replace(/\,\\n/g,'\,').replace(/\'\\r\\n/g,'\'').replace(/\'\\n/g,'\'');
//		console.debug( s );
		eval("opts=" + s);
		return opts;
	},

/**
 * ===============================
 * RegExp 常用正则判断
 * @param s, String,
 * @return boolean, true | false
 * -------------------------------
 * @非负整数，”^d+$” 
 * @正整数：”^[0-9]*[1-9][0-9]*$” 
 * @非正整数：”^((-d+)|(0+))$” 
 * @负整数：”^-[0-9]*[1-9][0-9]*$” 
 * @整数：”^-?d+$” 
 * @非负浮点数：”^d+(.d+)?$” 
 * @正浮点数：”^((0-9)+.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*.[0-9]+)|([0-9]*[1-9][0-9]*))$” 
 * @非正浮点数：”^((-d+.d+)?)|(0+(.0+)?))$” 
 * @负浮点数：”^(-((
 * @正浮点数正则式 )))$” 
 * @英文字符串：”^[A-Za-z]+$” 
 * @英文大写串：”^[A-Z]+$” 
 * @英文小写串：”^[a-z]+$” 
 * @英文字符数字串：”^[A-Za-z0-9]+$” 
 * @英数字加下划线串：”^w+$” 
 * @E-mail地址：”^[w-]+(.[w-]+)*@[w-]+(.[w-]+)+$” 
 * @URL：”^[a-zA-Z]+://(w+(-w+)*)(.(w+(-w+)*))*(?s*)?$”
 * @匹配中文字符的正则表达式： [\u4e00-\u9fa5]
 * @匹配双字节字符(包括汉字在内)：[^\x00-\xff]
 * @匹配HTML标记的正则表达式：/<(.*)>.*<\/\1>|<(.*) \/>/
 * @匹配国内电话号码：\d{3}-\d{8}|\d{4}-\d{7}
 * @匹配中国邮政编码：[1-9]\d{5}(?!\d)
 * @匹配身份证：\d{15}|\d{18}
 * *******/	
	regExp: function(/*String*/ s, /*RegExp*/ re){
		return re.test(s.toString().trim());
	},
	
/**
 * ===============================
 * validEmail
 * @param email
 * @return boolean, true | false
 * *******/		
	validEmail: function(/*String*/ email){
		if(existString(email)) { return false; }
		var re = /(\S)+[@]{1}(\S)+[.]{1}(\w)+/;
		return re.test(email.toString().trim());
	},
	
/**
 * 
 * 
 * ********/	
	isEmpty: function (/*String*/ s ){
		if( s == null ) { return true; }
		if( s.trim().length == 0 ) { return true; }
		return false;
	},

/**
 * 
 * 
 * ********/	
	cutStr: function (/*String*/ s,/*int*/ l ){
		if( s.length < 50 ) { return s; }
		return s.substr(0,l-1) + "...";
	},
	
	
/**
 * 返回一个子字符串（头尾）所在的位置
 */	
	position: function( str, subStr ){
		return { "start": str.indexOf(subStr), "end": this.start + subStr.length }
	},	
	
	
	
	
	
	
	
	version: "1.0"
}

/**
 * ===============================================================
 * 关于数字的简便工具
 * **************/
var NumUtil ={
		
		
		
		
	version: "1.0"	
}

/**
 * ===============================================================
 * 关于时间的简便工具
 * **************/
var DateUtil ={
	YEAR: 0
	,MONTH: 1
	,DAY: 2
	,HOUR: 3
	,MINUTE: 4
	,SECOND: 5
	
	/** 时间按照一定格式格式化 */
	,format: function( date, type ){
		
		var _date = this.date( date );
		
		if( type==null ){
			type="yyyy-MM-dd";
		}
		
		return type.replace( /yyyy/g, _date.yyyy )
				   .replace( /MM/g, _date.MM )
				   .replace( /dd/g, _date.dd )
				   .replace( /hh/g, _date.hh )
				   .replace( /mm/g, _date.mm )
				   .replace( /ss/g, _date.ss )
				   .replace( /SS/g, _date.SS )
				   .replace( /m/g, _date.m )
	}	
	
	/** 获取时间各单位数值 */
	,date: function( date ){
		return {
			'yyyy': date.getFullYear()
			,'MM': this.serial( date.getMonth() + 1 ,2 )
			,'dd': this.serial( date.getDate() ,2 )
			,'hh': this.serial( date.getHours() ,2 )
			,'mm': this.serial( date.getMinutes() ,2 )
			,'ss': this.serial( date.getSeconds() ,2 )
			,'SS': date.getMilliseconds()
			,'M': date.getMonth() + 1
			,'d': date.getDate()
			,'h': date.getHours()
			,'m': date.getMinutes()
			,'s': date.getSeconds()
			,'S': date.getMilliseconds()
		}
	}
	
	/** 修改时间, type 修改时间的类型, n 修改时间参数 */
	,modify: function( date, type, n ){
		var y=0, M=0, d=0, h=0, m=0, s=0;
		
		switch( type ){
			case 0: y = n; break;
			case 1: M = n; break;
			case 2: d = n; break;
			case 3: h = n; break;
			case 4: m = n; break;
			case 5: s = n; break;
		}
		
		var _date = this.date( date );
		
		return new Date( _date.yyyy + y ,_date.M - 1 + M , _date.d + d , _date.h + h ,_date.m + m ,_date.s + s );
		
	}
     
    /** 给数字前面按照一定位数加0 */         	
	,serial: function( num, digit ){
        if( num.length >= digit ){
            return num;
        }
        var _format = "0000000000000000000000000000000000000000000000" + num;
        return _format.substring( _format.length-digit,_format.length);
    }
    
    /** 按已格式化的时间串还原为 Date (具有一个格式) */
    ,parse: function( str, type ){
    	
    	function getDate( subStr, i ){
    		var indexof = type.indexOf( subStr );
    		if( indexof !=-1 ){
    			return parseInt( str.substr( indexof, i ) ,10 );
    		}else{
    			return 0;
    		}
    	}
    	
    	return {
    		"yyyy" : getDate("yyyy",4)
			,'MM': getDate("MM",2)
			,'dd': getDate("dd",2)
			,'hh': getDate("hh",2)
			,'mm': getDate("mm",2)
			,'ss': getDate("ss",2)
			,'SS': getDate("SS",2)
			,'date': new Date( getDate("yyyy",4), getDate("MM",2) -1, getDate("dd",2), getDate("hh",2), getDate("mm",2), getDate("ss",2), getDate("SS",2) )
    	}
    }
		
	,version: "1.0"	
}

