String.prototype.toUpperCaseFirst = function() {
        return this.substr(0,1).toUpperCase()+this.substr(1,this.length);
}

String.prototype.toUpperCaseWords = function() {
        var t=this.split(' ');
        var ret='';
        for(var i in t) {
                ret=ret+t[i].substr(0,1).toUpperCase()+t[i].substr(1,t[i].length);
                if(i<t.length-1) {
                        ret=ret+' '
                }
        }
        return ret;
}

String.prototype.Capitalize = function() {
        return this.toLowerCase().toUpperCaseWords();
}

String.prototype.Replace = function(pattern, replacement) {
  return this.split(pattern).join(replacement);
}

String.prototype.lTrim = function () {
        var chars;
        if (arguments.length == 0) {
                chars = "\\s";
        } else {
                chars = arguments[0];
        }
        var re = new RegExp('^'+chars+'*(.*)');
        return this.replace(re, '$1');
        
}

String.prototype.rTrim = function () {
        var chars;
        if (arguments.length == 0) {
                chars = "\\s";
        } else {
                chars = arguments[0];
        }
        var re = new RegExp('(.*?)'+chars+'*$');
        return this.replace(re, '$1');        

}

String.prototype.Trim = function() {
        if (arguments.length == 0) {
        	return this.rTrim().lTrim();
        }
	return this.rTrim(arguments[0]).lTrim(arguments[0]);
}

String.prototype.Compact = function() {
        var blankExists = false
        var newValue = new String()
        var ch

        for (var i=0; i < this.length; i++) {
                ch = this.charAt(i)
                if ( ch == " " ) {
                        if ( blankExists == false ) {
                                blankExists = true
                                newValue = newValue + ch
                        }
                } else {
                        newValue = newValue + ch
                        blankExists = false
                }
        }

        if ( newValue == null ) return this
        return newValue
}

String.prototype.toInt = function() {

        var v = parseInt(this);
        if (isNaN(v)) {
                return null;
        }
        if (arguments[0] != null) {
                if (v<parseInt(arguments[0])) v = parseInt(arguments[0]);
        }
        if (arguments[1] != null) {
                if (v>parseInt(arguments[1])) v = parseInt(arguments[1]);
        }

        return v;
}


String.prototype.parseQuery = function() {
        var query = this.toString();
        var qpos = query.search(/\?/);
        if (qpos > -1) {
                query = query.substring(qpos+1);
        }
        if (query.length == 0) return new Array();
        var args = new Object();
        var fields, field, f;
        if (query) {
                fields = query.split('&');
                for (f = 0; f < fields.length; f++) {
                        field = fields[f].split('=');
                        if (field.length >= 2) {
                                args[unescape(field[0].replace(/\+/g, ' '))] = unescape(field[1].replace(/\+/g, ' '));
                        }
                }
        }
        return args;
}

String.prototype.copyToClipboard = function() {

        if (window.clipboardData) { 
             // IE send-to-clipboard method.
             window.clipboardData.setData('Text', this);
     
        } else if (window.netscape) {
             // You have to sign the code to enable this or allow the action 
             // in about:config by changing user_pref("signed.applets.codebase_principal_support", true);
             netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
          
             // Store support string in an object.
             var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
             if (!str) return false;
             str.data=this;
          
             // Make transferable.
             var trans = Components.classes["@mozilla.org/widget/transferable;1"].createInstance(Components.interfaces.nsITransferable);
             if (!trans) return false;
          
             // Specify what datatypes we want to obtain, which is text in this case.
             trans.addDataFlavor("text/unicode");
             trans.setTransferData("text/unicode",str,this.length*2);
          
             var clipid=Components.interfaces.nsIClipboard;
             var clip = Components.classes["@mozilla.org/widget/clipboard;1"].getService(clipid);
             if (!clip) return false;
          
            clip.setData(trans,null,clipid.kGlobalClipboard);
       }
}
