//Author : kingthy
//Date	 : 2008/05/30
//*************************************************************
String.prototype.repeated = function(count) {
    var v = '';
    for (var i = 0; i < count; i++) {
        v += this;
    }
    return v;
};
String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, '');
};
String.prototype.replaceAll = function(reallyDo, replaceWith, ignoreCase) {
    if (!RegExp.prototype.isPrototypeOf(reallyDo)) {
        return this.replace(new RegExp(reallyDo, (ignoreCase ? "gi" : "g")), replaceWith);
    } else {
        return this.replace(reallyDo, replaceWith);
    }
}
String.prototype.single = function(str, count) {
    var re = new RegExp(str + "{" + count + ",}", "gi");
    return this.replace(re, str);
};
String.prototype.padleft = function() {
    var l, c;
    if (arguments.length == 1) {
        l = arguments[0];
        c = '0';
    } else if (arguments.length == 2) {
        c = arguments[0];
        l = arguments[1];
    } else {
        return this;
    }
    var v = this;
    for (var i = this.length; i < l; i++) {
        v = c + v;
    }
    return v;
}
String.prototype.format = function() {
    var p1 = 0, p2 = 0;
    var s = '';
    var args = arguments;
    while (p1 < this.length) {
        p2 = this.indexOf('{', p1);
        if (p2 != -1) {
            s += this.substring(p1, p2).replace(/}}/g, '}');
            p1 = p2 + 1;
            if (p2 < (this.length - 1)) {
                if (this.charAt(p1) == '{') {
                    //escape {{
                    s += '{';
                    p1++;
                } else {
                    p2 = this.indexOf('}', p1);
                    if (p2 == -1 || p2 == p1) {
                        throw 'String.format error in position ' + p1;
                    } else {
                        var n = this.substring(p1, p2);
                        if (isNaN(n)) throw 'String.format error in position ' + p1;
                        n = parseInt(n);
                        if (n < args.length) {
                            s += args[n] + '';
                        }
                        p1 = p2 + 1;
                    }
                }
            } else {
                throw 'String.format error in position ' + p2;
            }
        } else {
            break;
        }
    }
    if (p1 < this.length) s += this.substring(p1).replace(/}}/g, '}');
    return s;
}
Array.prototype.foreach = function(f) {
    if (typeof (f) != 'function') return;
    for (var index = 0; index < this.length; index++) {
        f(this[index], index);
    }
};
Array.prototype.exist = function(f) {
    for (var index = 0; index < this.length; index++) {
        if (f(this[index], index)) return true;
    }
    return false;
}
Array.prototype.clone = function() {
    var o = [];
    this.foreach(function(item) { o.push(item); });
    return o;
};
