﻿

// This is a self executing method. It extends the JS String type
// to allow string formatting.
// e.g. alert( "Hello {0}!".format('world')  );
(function() {


    function _StringFormatInline() {
	    var txt = this;
	    for(var i=0;i<arguments.length;i++) {
		    var exp = new RegExp('\\{' + (i) + '\\}','gm');
		    txt = txt.replace(exp,arguments[i]);
	    }
	    return txt;
    }

    function _StringFormatStatic() {
	    for(var i=1;i<arguments.length;i++) {
		    var exp = new RegExp('\\{' + (i-1) + '\\}','gm');
		    arguments[0] = arguments[0].replace(exp,arguments[i]);
	    }
	    return arguments[0];
    }
    
    
    if(!String.prototype.format) {
        String.prototype.format = _StringFormatInline;
    }
    
    if(!String.format) {
        String.format = _StringFormatStatic;
    }

})();

// Extension method to allow for proper case formatting of a string. Essentially this method
// will take a string, lower case it and the set the first character of each word to upper case.
String.prototype.toProperCase = function()
{
  return this.toLowerCase().replace(/^(.)|\s(.)/g, 
      function($1) { return $1.toUpperCase(); });
}
