/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


String.prototype.urlEncode=function(str){
    var ret="";
    var strSpecial="!\"#$%&'()*+,/:;<=>?[]^`{|}~%";
    for(var i=0;i<str.length;i++){
        var chr = str.charAt(i);
        var c=str2asc(chr);
        tt += chr+":"+c+"n";
        if(parseInt("0x"+c) > 0x7f){
            ret+="%"+c.slice(0,2)+"%"+c.slice(-2);
        }else{
            if(chr==" ")
                ret+="+";
            else if(strSpecial.indexOf(chr)!=-1)
                ret+="%"+c.toString(16);
            else
                ret+=chr;
        }
    }
    return ret;
}

String.prototype.urlDecode=function(str){
    var ret="";
    for(var i=0;i<str.length;i++){
        var chr = str.charAt(i);
        if(chr == "+"){
            ret+=" ";
        }else if(chr=="%"){
            var asc = str.substring(i+1,i+3);
            if(parseInt("0x"+asc)>0x7f){
                ret+=asc2str(parseInt("0x"+asc+str.substring(i+4,i+6)));
                i+=5;
            }else{
                ret+=asc2str(parseInt("0x"+asc));
                i+=2;
            }
        }else{
            ret+= chr;
        }
    }
    return ret;
}

function JSCTool(){
    //防止this指针被重定向引用的方法

    var _this=this;



    //JavaScript 类定义

    //构造方法

    var init=function(){

    }

    //构造方法调用

    init();



    //将一个日期转换为时间戳
    _this.convertDate2Timestamp=function(date){
        
        var datum = _this.strToDateObject(date);
        return datum.getTime()/1000;
    }

    //将时间戳转化为日期
    _this.convertTimestamp2Date=function(timestamp){
        var datum = new Date(timestamp * 1000);
        return _this.fillupZero(datum.getFullYear())+'-'+_this.fillupZero((datum.getMonth()+1))+'-'+_this.fillupZero(datum.getDate())+' '+_this.fillupZero(datum.getHours()) + ':' + _this.fillupZero(datum.getMinutes()) + ':' +_this.fillupZero(datum.getSeconds());
    }

    //填充补零的方法，一般情况下提供内部使用
    _this.fillupZero=function(number){
        if(0 <= number && number < 10){
            return '0'+number;
        }
        return number;
    }

    //对一个标准 year-month-day hour:min:second 的时间格式进行天的添加
    _this.addDaysToDateStr=function( dateStr, days ){
        
        var tempDate = _this.strToDateObject(dateStr);
        tempDate=tempDate.valueOf();
        tempDate = tempDate + days * 24 * 60 * 60 * 1000
        tempDate = new Date(tempDate)
        
        return _this.fillupZero(tempDate.getFullYear())+'-'+_this.fillupZero((tempDate.getMonth()+1))+'-'+_this.fillupZero(tempDate.getDate())+' '+_this.fillupZero(tempDate.getHours()) + ':' + _this.fillupZero(tempDate.getMinutes()) + ':' +_this.fillupZero(tempDate.getSeconds());
    }

    //判断当前日期是否合法如果不合法返回false
    _this.isDate=function (datestr)   {
        var   reg   =   /^(\d{4})-(\d{2})-(\d{2})$/;
        var   str   =   datestr;
        var   arr   =   reg.exec(str);
        if   (str== " ")   return   true;
        if   (!reg.test(str)&&RegExp.$2 <=12&&RegExp.$3 <=31){
            return   false;
        }
        return   true;
    }

    _this.strToDateObject=function(dateStr){
        var checkDateArr=dateStr.split(' ');
        if(checkDateArr.length >=2 ){
            var checkTimeArr=checkDateArr[1].split(':');
            switch(checkTimeArr.length){
                case 1 :
                    dateStr+=' :00:00';
                    break;
                case 2 :
                    dateStr+=' :00';
                    break;
                default :
                    dateStr+=' 00:00:00';
                    break;
            }
        }else{
            dateStr+=' 00:00:00';
        }

        var str = dateStr;//PHP中对应的UNIX时间戳为1223559328
        var new_str = str.replace(/:/g,'-');
        new_str = new_str.replace(/ /g,'-');
        var arr = new_str.split("-");

        return new Date(Date.UTC(arr[0],arr[1]-1,arr[2],arr[3]-8,arr[4],arr[5]));
    }
}
