String.prototype.trim = trim;
String.prototype.startsWith = startsWith;
String.prototype.endsWith = endsWith;
String.prototype.equals = equals;
String.prototype.equalsIgnoreCase = equalsIgnoreCase;
String.prototype.toCharArray = toCharArray;
String.prototype.encodeHTML = encodeHTML;
String.prototype.encodeJS = encodeJS;
String.prototype.encodeXML = encodeXML;
String.prototype.isEmail = isEmail;

String.prototype.isEmpty = function() {
    return typeof(this) == "undefined" || this.trim().length == 0;
}

function isEmpty(str) {
    return str == null || str.isEmpty();
}

function trim() {
    return this.replace(/(^\s*)|(\s*$)/g, "");
}

function startsWith(str) {
    if (str.length > this.length) return false;
    return this.indexOf(str) == 0;
}

function endsWith(str) {
    if (str.length > this.length) return false;
    return this.lastIndexOf(str) == this.length - str.length;
}

function equals(object) {
    return this == object.toString(10);
}

function equalsIgnoreCase(object) {
    return this.toLowerCase() == object.toString(10).toLowerCase();
}

function toCharArray() {
    var len = this.length;
    var chars = new Array(len);
    for (var i = 0; i < len; i++) chars[i] = this.charAt(i);
    return chars;
}

function encodeHTML() {
    if (this.isEmpty()) return "";
    return this.replace(/\"/g, "&quot;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
}

function encodeJS() {
    if (this.isEmpty()) return "";
    return this.replace(/\'/g, "\'").replace(/\"/g, "\"").replace(/\n/g, "\n").replace(/\r/g, "");
}

function encodeXML() {
    if (this.isEmpty()) return "";
    return this.encodeHTML().replace(/&/g, "&amp;");
}

function isEmail() {
    var avail = /^[A-z]{1}[A-z0-9\x2d\.]*[A-z0-9]{1}@[A-z]{1}[A-z0-9\x2d\.]*[A-z0-9]{1}\.[A-z]{2,5}$/gi;
    var inval = /^.*((\x2d\.)|(\.\x2d)|(\.\.)|(\x2d\x2d)).*$/gi;
    return !this.isEmpty() && !inval.test(this) && avail.test(this)
}
