// Strings extended methods
// Author: Radu Buzea [www.hypnosis.ro]
// Version 1.02 [20 Feb. 2007]
// ================================================================================================

if (!String.prototype.ltrim) {
  String.prototype.ltrim = function () {
    return this.replace(/^\s+/g, "");
  }
}

if (!String.prototype.rtrim) {
  String.prototype.rtrim = function () {
    return this.replace(/\s+$/g, "");
  }
}

if (!String.prototype.trim) {
  String.prototype.trim = function () {
    var s = this.ltrim();
    return s.rtrim();
  }
}

if (!String.prototype.sameAs) {
  String.prototype.sameAs = function (str) {
    if (str == this) return 0;
    else if (str > this) return 1;
    else return -1;
  }
}

if (!String.prototype.sameAsIns) {
  String.prototype.sameAsIns = function (str) {
    str = str.toLowerCase();
    var sThis = this.toLowerCase();
    if (str == sThis) return 0;
    else if (str > sThis) return 1;
    else return -1;
  }
}

if (!String.prototype.strReplace) {
  String.prototype.strReplace = function (oldStr, newStr) {
    if (oldStr == "") return this;
    var result = this;
    var pStr = result.indexOf(oldStr);
    var oldLen = oldStr.length;
    var startS = "", endS = "";
    while (pStr > -1) {
      if (pStr > 0) startS = result.substr(0, pStr);
      else startS = "";
      if (pStr + oldLen < result.length - 1) endS = result.substr(pStr + oldLen);
      else endS = "";
      result = startS + newStr + endS;
      pStr = result.indexOf(oldStr);
    }
    return result;
  }
}

if (!String.prototype.strReplaceIns) {
  String.prototype.strReplace = function (oldStr, newStr) {
    if (oldStr == "") return this;
    var result = this;
    var oldStrIns = oldStr.toLowerCase();
    var resultIns = result.toLowerCase();
    var pStr = resultIns.indexOf(oldStrIns);
    var oldLen = oldStr.length;
    var startS = "", endS = "";
    while (pStr > -1) {
      if (pStr > 0) startS = result.substr(0, pStr);
      else startS = "";
      if (pStr + oldLen < result.length - 1) endS = result.substr(pStr + oldLen);
      else endS = "";
      result = startS + newStr + endS;
      resultIns = result.toLowerCase();
      pStr = resultIns.indexOf(oldStrIns);
    }
    return result;
  }
}


if (!String.prototype.isEmail) {
  String.prototype.isEmail = function () {
    var valid_chars = "1234567890qwertyuiopasdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";
    var regexp1 = /(@.*@)|(\.\.)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
    var regexp2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
    for (var i = 0; i < this.length; i++) 
      if (valid_chars.indexOf(this.charAt(i)) < 0) return false;
    if (!this.match(regexp1) && this.match(regexp2)) return true; 
    else return false;
  }
}

if (!String.prototype.isURL) {
  String.prototype.isURL = function () {
    var regexp = /http:\/\/[A-Za-z0-9\.-]{3,}\.[A-Za-z]{3}/;
    if (regexp.test(this)) return true; 
    else return false;
  }
}

if (!String.prototype.isInteger) {
  String.prototype.isInteger = function () {
    var ok = "1234567890";
    for (var i = 0; i < this.length; i++)
      if (ok.indexOf(this.charAt(i)) < 0) return false;
    return true;
  }
}

if (!String.prototype.asInteger) {
  String.prototype.asInteger = function () {
    if (!this.isInteger()) return 0;
    var sInt = "";
    for (var i = 0; i < this.length; i++) 
      if (this.charAt(i) == "0" && sInt != "") sInt += this.charAt(i);
      else if (this.charAt(i) != "0") sInt += this.charAt(i);
    if (!sInt.isInteger()) return 0;
    else return parseInt(sInt);
  }
}

if (!String.prototype.isNumeric) {
  String.prototype.isNumeric = function () {
    var ok = "1234567890";
    var dotFound = false;
    for (var i = 0; i < this.length; i++)
      if (ok.indexOf(this.charAt(i)) < 0) {
        if (this.charAt(i) == ".") {
          if (i == 0 || dotFound) return false;
          dotFound = true;
        } else return false;
      }
    return true;
  }
}

if (!String.prototype.isBool) {
  String.prototype.isBool = function () {
    if (this.sameAsIns("true") == 0 || this.sameAsIns("false") == 0) return true;
    else if (this == "0" || this == "1") return true;
    else return false;
  }
}

if (!String.prototype.asBool) {
  String.prototype.asBool = function () {
    if (this.sameAsIns("true") == 0 || this == "1") return true;
    else return false;
  }
}

if (!String.prototype.isMySQLDate) {
  String.prototype.isMySQLDate = function () {
    if (this == "0000-00-00") return true;
    var regexp = /[1-2]{1}[0-9]{3}-[0-9]{2}-[0-9]{2}/;
    if (regexp.test(this)) {      
      var mon = this.substring(5, 7);
      var day = this.substring(8, 10);
      if (mon.asInteger() < 1 || mon.asInteger() > 12) return false;
      if (day.asInteger() < 1) return false;
      if (mon.asInteger() == 2) {
        var maxday = 28;
        var year = this.substring(0, 4);
        if (year.asInteger() % 4 == 0) {
          if(year.asInteger() % 100 != 0) maxday++;
          else if (year.asInteger() % 400 == 0) maxday++;
        }
        if (day > maxday) return false;
      } else {
        var days = new Array(0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
        if (day > days[mon.asInteger()]) return false;
      }
    }
    return true;
  }
}

if (!String.prototype.asCaption) {
  String.prototype.asCaption = function () {
    var str = this.toLowerCase();
    var cr = "", lastcr = "", result = "";
    var alpha_chars = "qwertyuiopasdfghjklzxcvbnm";
    for (var i = 0; i < str.length; i++) {
      cr = str.charAt(i);
      if (result.trim() == "") result += cr.toUpperCase();
      else if (lastcr == " " && cr == " ") { } 
      else if (lastcr != "" && alpha_chars.indexOf(lastcr) < 0) result += cr.toUpperCase();
      else result += cr;
      lastcr = cr;
    }
    return result.trim();
  }
}

if (!String.prototype.htmlEntities) {
  String.prototype.htmlEntities = function () {
    var ci = 0, cr = "", result = "";
    for (var i = 0; i < this.length; i++) {
      cr = this.charAt(i);
      ci = this.charCodeAt(i);
      switch (cr) {
        case '"': result += "&quot;"; break;
        case '&': result += "&amp;"; break;
        case '<': result += "&lt;"; break;
        case '>': result += "&gt;"; break;
        case '©': result += "&copy;"; break;
        case '«': result += "&laquo;"; break;
        case '®': result += "&reg;"; break;
        case '»': result += "&raquo;"; break;
        case 'µ': result += "&micro;"; break;
        case '¶': result += "&para;"; break;
        case '•': result += "&middot;"; break;
        case '€': result += "&euro;"; break;
        default: 
          if ((ci >= 32 && ci <= 39) || (ci >= 40 && ci <= 127)) result += cr;
          else result += "&#" + ci + ";";
      }
    }
    return result;
  }
}
