﻿//*****************************************************************************
// JavaScript String Object Extended Methods.
//*****************************************************************************
if ( !(new String).trim ){
    String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g,''); };
}

if ( !(new String).normalize && (new String).trim ){
    String.prototype.normalize = String.prototype.normalise = function() { return this.trim().replace(/\s+/g,' '); };
}

if ( !(new String).startsWith ){
    String.prototype.startsWith = function(str,i){ i=(i)?'i':'';var re=new RegExp('^'+str,i);return (this.normalize().match(re)) ? true : false ; };
}

if ( !(new String).endsWith ){
    String.prototype.endsWith = function(str,i){ i=(i)?'gi':'g';var re=new RegExp(str+'$',i);return (this.normalize().match(re)) ? true : false ; };
}

if ( !(new String).contains ){
    String.prototype.contains = function(str,i){ return (this.indexOf(str) != -1); };
}

if ( !(new String).isEmpty && (new String).trim ){
    String.prototype.isEmpty = function() { return (this.trim().length == 0); };
}

if ( !(new String).isNotEmpty && (new String).isEmpty ){
    String.prototype.isNotEmpty = function() { return (!this.isEmpty()); };
}

if ( !(new String).toAlphanumeric && (new String).normalize ){
    String.prototype.toAlphanumeric = function() {
        var newstr = '';
        for (var j = 0; j < this.length; j++)
        {
            var alphaa = this.charAt(j);
            var hh = alphaa.charCodeAt(0);
            if ((hh > 47 && hh < 58) || (hh > 64 && hh < 91) || (hh > 96 && hh < 123))
            {
                // This is an alphanumeric character!
                newstr += alphaa;
            }
            else
            {
                newstr += ' ';
            }
        }
        return (newstr.normalize());
    };
}
