//*******************************************************************
//
// String.Net.js
//   version       : 1.0
//   last revision : 14/07/2004 (dd/mm/yyyy)
//   by Aurelien
//
// Description :
//   This file enhance Javascript String Object Model by adding or replacing methods.
//   Based on Microsoft .Net Framework System.String Class Reference :
//   http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemStringClassTopic.asp
//
// Old Javascript Methods replaced :
//   _indexOf(String) - _indexOf(String, Integer)
//   _lastIndexOf(String) - _lastIndexOf(String, Integer)
//   _replace(String, String)
//
// New Methods added :
//   endsWith(String)
//   equals(String)
//   format(String0, String1, ..., StringN)
//   insert(Integer, String)
//   indexOf(String) - indexOf(String, Integer) - indexOf(String, Integer, Integer)
//   lastIndexOf(String) - lastIndexOf(String, Integer) - lastIndexOf(String, Integer, Integer)
//   padLeft(Integer) - padLeft(Integer, String)
//   padRight(Integer) - padRight(Integer, String)
//   remove(Integer, Integer)
//   replace(String, String)
//   startsWith(String)
//   trim()
//   trimEnd()
//   trimStart()
//
// History (dd/mm/yyyy) :
//   v1.0 - 14/07/2004 : First Release
//
// Please report any bug to :
//   http://blogs.developpeur.org/aurelien/archive/2004/07/14/2113.aspx
//   or
//   http://www.asp-php.net/scripts/scripting/librairienetjs.php
//
//*******************************************************************

String.prototype._indexOf = String.prototype.indexOf;
String.prototype._lastIndexOf = String.prototype.lastIndexOf;
String.prototype._replace = String.prototype.replace;

// endsWith(String)
String.prototype.endsWith = function(strChar)
{
 if (!strChar) {return false;}
 strChar += '';
 var intLength = strChar.length;
 return (this.substr(this.length - intLength, intLength) == strChar);
}

// equals(String)
String.prototype.equals = function(strChar)
{
 if (!strChar) {return false;}
 strChar += '';
 return (this == strChar);
}

// format(String0, String1, ..., StringN)
String.prototype.format = function()
{
 if (arguments.length == 0) {return this;}
 var strThis = this, i = 0;
 for (i = 0; i < arguments.length; i++) {strThis = strThis._replace(new RegExp('\\{' + i + '\\}', 'gi'), arguments[i]);}
 return strThis;
}

// insert(Integer, String)
String.prototype.insert = function(intIndex, strChar)
{
 if (isNaN(intIndex)) {return this;}
 if (intIndex < 0) {return this;}
 if (!strChar) {return this;}
 strChar += '';
 intIndex = parseInt(intIndex, 10);
 return (this.substr(0, intIndex) + strChar + this.substr(intIndex, this.length));
}

// indexOf(String)
// indexOf(String, Integer)
// indexOf(String, Integer, Integer)
String.prototype.indexOf = function()
{
 if (arguments.length == 0) {return -1;}
 var strChar = '' + arguments[0], intStart = -1, intCount = -1, intOut = -1;
 if (arguments.length == 1) {
  intOut = this._indexOf(strChar);
 }
 if (arguments.length == 2) {
  if (isNaN(arguments[1])) {return -1;}
  if (arguments[1] < 0) {return -1;}
  intOut = this._indexOf(strChar, arguments[1]);
 }
 if (arguments.length == 3) {
  if (isNaN(arguments[1])) {return -1;}
  if (isNaN(arguments[2])) {return -1;}
  if (arguments[1] < 0) {return -1;}
  if (arguments[2] < 0) {return -1;}
  intOut = this._indexOf(strChar, arguments[1]);
  if (intOut > (arguments[1] + arguments[2])) {intOut = -1;}
 }
 return intOut;
}

// lastIndexOf(String)
// lastIndexOf(String, Integer)
// lastIndexOf(String, Integer, Integer)
String.prototype.lastIndexOf = function()
{
 if (arguments.length == 0) {return -1;}
 var strChar = '' + arguments[0], intStart = -1, intCount = -1, intOut = -1;
 if (arguments.length == 1) {
  intOut = this._lastIndexOf(strChar);
 }
 if (arguments.length == 2) {
  if (isNaN(arguments[1])) {return -1;}
  if (arguments[1] < 0) {return -1;}
  intOut = this._lastIndexOf(strChar, arguments[1]);
 }
 if (arguments.length == 3) {
  if (isNaN(arguments[1])) {return -1;}
  if (isNaN(arguments[2])) {return -1;}
  if (arguments[1] < 0) {return -1;}
  if (arguments[2] < 0) {return -1;}
  intOut = this._lastIndexOf(strChar, arguments[1]);
  if (intOut < (arguments[1] - arguments[2])) {intOut = -1;}
 }
 return intOut;
}

// padLeft(Integer)
// padLeft(Integer, String)
String.prototype.padLeft = function()
{
 if (arguments.length == 0) {return this;}
 if (arguments.length >= 1 && isNaN(arguments[0])) {return this;}
 if (arguments[0] < 0) {return this;}
 var strThis = this, intLength = parseInt(arguments[0], 10), strChar = String.fromCharCode(32);
 if (arguments.length == 2) {strChar = '' + arguments[1];}
 while (strThis.length < intLength) {strThis = strChar + strThis;}
 return strThis;
}

// padRight(Integer)
// padRight(Integer, String)
String.prototype.padRight = function()
{
 if (arguments.length == 0) {return this;}
 if (arguments.length >= 1 && isNaN(arguments[0])) {return this;}
 if (arguments[0] < 0) {return this;}
 var strThis = this, intLength = parseInt(arguments[0], 10), strChar = String.fromCharCode(32);
 if (arguments.length == 2) {strChar = '' + arguments[1];}
 while (strThis.length < intLength) {strThis = strThis + strChar;}
 return strThis;
}

// remove(Integer, Integer)
String.prototype.remove = function(intIndex, intLength)
{
 if (isNaN(intIndex) || isNaN(intLength)) {return this;}
 if (intIndex < 0 || intLength < 0) {return this;}
 intIndex = parseInt(intIndex, 10); intLength = parseInt(intLength, 10);
 return (this.substr(0, intIndex) + this.substr(intIndex + intLength, this.length));
}

/*// replace(String, String)
String.prototype.replace = function(strFind, strReplace)
{
 if (!strFind) {return this;}
 if (!strReplace) {strReplace = '';}
 strFind += '';
 strFind = strFind._replace(/(\^|\$|\[|\]|\(|\)|\||\*|\+|\.|\?|\{|\}|\\)/gi, '\\$1');
 return (this._replace(new RegExp(strFind, 'g'), strReplace));
}
*/
// startsWith(String)
String.prototype.startsWith = function(strChar)
{
 if (!strChar) {return false;}
 strChar += '';
 var intLength = strChar.length;
 return (this.substr(0, intLength) == strChar);
}

// trim()
String.prototype.trim = function()
{
 return this._replace(/(^\s*)|(\s*$)/g, '');
}

// trimEnd()
String.prototype.trimEnd = function()
{
 return this._replace(/\s*$/g, '');
}

// trimStart()
String.prototype.trimStart = function()
{
 return this._replace(/^\s*/g, '');
}
