/**
 * Title: String
 * Date Created: 
 * Description: String extension
 * Dependencies: 
 * Amendment History:
 * Copyright (c) 2004
 * Company: Azeus Systems Philippines Inc.
 */


// Checks if the string object starts with the given string.
String.prototype.startsWith = function(aString) {
  if (aString == null || aString == '')
    return true;
  var sTmp = this.substr(0, aString.length);
  var bRes = (sTmp == aString);
  return bRes;  
}

// Replaces all instances of a string with another string.
String.prototype.strReplace = function(aOld, aNew) {
  var aStr = this;
	var iStart = aStr.indexOf(aOld);
	if (iStart==-1) 
	  return aStr;
  return aStr.substring(0,iStart)+aNew + aStr.substring(iStart+aOld.length).strReplace(aOld,aNew);
}

/**
 * Trims all characters on the left side of this string matching the given character.
 * This string is not modified. Instead, a new string is returned.
 * @param aTrimChar - the character to trim
 */
String.prototype.trimLeft = function(aTrimChar) {
  var result=this;
  while (result.length>0 && result.charAt(0)==aTrimChar ){
    result=result.substring(1);
  }
  return result;
}

String.prototype.trimRight = function(aTrimChar) {
  var result=this;
  while (result.length>0 && result.charAt(result.length-1)==aTrimChar ){
    result=result.substring(0,result.length-1);
  }
  return result;
}

String.prototype.trim = function(aTrimChar) {
  if (aTrimChar == null) aTrimChar = ' ';
  var result=this.trimLeft(aTrimChar);
  result=result.trimRight(aTrimChar);
  return result;
}
String.prototype.lengthb = function() {
  var r = new RegExp("\%[A-Z0-9]{2}","g");
  return encodeURI(this).replace(r,"X").length;
}

String.prototype.trimWSLeft = trimWSLeft;

String.prototype.trimWSRight = trimWSRight;

String.prototype.trimWS = trimWS;

function isWhiteSpace(aChar) {
  if ((aChar == ' ') || (aChar == '\n') || (aChar == '\r') || (aChar == '\t')) return true;
  return false;
}

function trimWSRight() {
  var result = this;
  while ((result.length > 0) && isWhiteSpace(result.charAt(result.length - 1)))
    result = result.substring(0, result.length - 1);
  return result;
}

function trimWSLeft() {
  var result = this;
  while ((result.length > 0) && isWhiteSpace(result.charAt(0)))
    result = result.substring(1);
  return result;
}

function trimWS() {
  var result = this.trimWSLeft();
  result = result.trimWSRight();
  return result;
}

function trimAllControls(aForm) {
  var i;
  for (i=0; i<aForm.elements.length; i++) {
    if (aForm.elements[i].type == 'text' || aForm.elements[i].type == 'textarea'
      || aForm.elements[i].type == 'password') {
        aForm.elements[i].onblur = _trimValue;
    }
  }
}

function _trimValue() {
  this.value = String(this.value).trimWS();
}

String.prototype.isChinese = String_isChinese;


function String_isChinese() {
  for (var i=0; i<this.length; i++)
    if (this.charCodeAt(i) > 128)
      return true;
  return false;
}
