﻿system.string = 
{
    strByteLen: function(value)
    {
        var i, byteLen = 0, len = value.length;
        for(i = 0; i< len; i++) 
        {
            if(value.charCodeAt(i) > 127 || value.charCodeAt(i) == 94) byteLen += 2;   
            else byteLen++; 
        }
        return byteLen;
    },
    
    substring: function(value, length)
    {
        if(this.strByteLen(value) < length) return value;
        
        var i, bytes = 0, count = value.length;
        
        length -= 3;
    
        for(i = 0; i < count; i++)
        {
            if(bytes >= length) break;
            
            if(value.charCodeAt(i) > 127 || value.charCodeAt(i) == 94) bytes += 2;
            else bytes++;
        }

        return value.substring(0, i) + '...';
    },
    
    trim: function(str)
    {
        return str.replace(/(^\s*)|(\s*$)/g,"");
    },
    
    isNumeric: function(value)
    {
        if(/^\d+$/.test(value)){ return true;}
        return false;
    },
    
    isEmail: function(value)
    {
        return /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(value); 
    },
    
    isBirthday: function(value)
    {
        return /^(([1-9]\d{3})(\-)((0[1-9])|(1[0-2]))(\-)((0[1-9])|([1-2][0-9])|(3[0-1])))?$/.test(value);
    },
    
    isPhone: function(value)
    {
        return /^((0\d{3,4})(\-)(\d{7,8}))?$/.test(value);
    },
    
    isMobile: function(value)
    {
        return /^(13\d{9})?$/.test(value);
    },
    
    urlEncode: function(str)
    {
        var histogram = {}, histogram_r = {}, code = 0, tmp_arr = [];
        var ret = str.toString();
        var replacer = function(search, replace, str) 
        {
            var tmp_arr = [];
            tmp_arr = str.split(search);
            return tmp_arr.join(replace);
        };
        
        histogram['!']   = '%21';
        histogram['%20'] = '+';
        
        ret = encodeURIComponent(ret);
        
        for (search in histogram) 
        {
            replace = histogram[search];
            ret = replacer(search, replace, ret) // Custom replace. No regexing
        }
        
        return ret.replace(/(\%([a-z0-9]{2}))/g, function(full, m1, m2) { return "%"+m2.toUpperCase();});
        
        return ret;
    },
    
    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;
    },
    
    formatIpAddress: function(ip)
    {
        if(ip == null || ip.length == 0) return "****";
        
        var index = ip.lastIndexOf(".");
        
        if(index == -1) return "****";
        
        return ip.substring(0, index) + ".***";
    },
    
    formatTime: function(time)
    {
        return time.substring(0, 10) + " " + time.substring(11, 19);
    }
};

