/**
 * String.prototype.trim
 *
 * Removes leading and trailing characters
 * @returns string The trimmed string
 */
String.prototype.trim = function() {
  /*
  a = this.replace(/^\s+/, '');
  return a.replace(/\s+$/, '');
  */
  return this.replace(/(^\s+|\s+$)/g, '');
};
/**
 * String.prototype.ucwords
 *
 * Uppercase words in this string.
 * @author http://kevin.vanzonneveld.net
 * @author original Jonas Raoni Soares Silva (http://www.jsfromhell.com)
 * @author improved Waldo Malqui Silva
 * @author bugfix Onno Marsman
 * @returns string The modified string
 */
String.prototype.ucwords = function() {
    return (this+'').replace(/^(.)|\s(.)/g, function ( $1 ) { return $1.toUpperCase ( ); } );
};
String.prototype.escapeRegEx = function() {
 return this.replace(/(\/|\.|\*|\+|\?|\||\(|\)|\[|\]|\{|\}|\\)/g, '\\$1');
}
/**
 * String.prototype.toNumber
 *
 * Removes non-numeric characters
 * @param string frac The symbol representing the fractional character for a valid number.
 * @returns number A number represented with the fractional character being replaced by a decimal
 */
String.prototype.toNumber = function() {
  var r = this, re = '(&#[a-z0-9]+;|[^0-9';
  if (arguments.length > 0) {
    re += arguments[0].escapeRegEx();
  }
  re = new RegExp(re+'])', 'ig');
  r = r.replace(re, '');
  if (arguments.length > 0) {
    r = r.replace(arguments[0], '.');
  }
  return Number(r);
};
/**
 * String.prototype.concat
 *
 * Concatenate many strings together more efficiently than through the + operator
 *
 * Usage: var concated = String.concat('abc','def',...,'zzz');
 *
 * @author Jeremy Miller
 * @returns string The array of strings concatenated together
 */
String.prototype.concat = function() {
  if (arguments.length > 0) {
    return arguments.join('');
  }
};
/**
 * String.prototype.findFunction
 *
 * Converts this string to a reference to an existing function
 * if the function exists; otherwise returns false.
 * Example: "MyObj.SubObj.Fn" would return a reference to the Fn function of the respective child objects.
 * @returns mixed A function reference or false
 */
String.prototype.findFunction = function() {
  var pcs = this.split('.'), r = window;
  for (var i=0; i<pcs.length; i++) {
    if (r[pcs[i]] ) {
      r = r[pcs[i]];
    }
  }
  if (r == window) {
    return false;
  } else {
    return r;
  }
};
String.prototype.toVarSafe = function() {
  return this.replace(/^(.)|(_.)/g, function ( $1 ) { return $1.toUpperCase ( ).replace('_',''); } );
};
String.prototype.toIdSafe = function () {
  var r = this.toLowerCase();
  r = r.trim().replace(/[^a-z0-9]+/g, '_');
  return r;
};
String.prototype.toUrlSafe = function () {
  var r = this.toLowerCase();
  r = r.trim().replace(/[^a-z0-9]+/gi, '-');
  return r;
};
String.prototype.toMaxLen = function(max_len) {
  if (typeof max_len == 'number') {
    var r = this;
    if (r.length > max_len) {
      r = r.substr(0, max_len-1) + '&hellip;';
    }
    return r;
  } else {
    return this;
  }
};
