/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * File:	string.js															 *
 * Author:  Keith Borgmann														 *
 * Date:	2008-11-18															 *
 * Version: 1.0.4																 *
 * Description:																	 *
 *	Extensions to the js String object											 *
 *																				 *
 * Extension List:																 *
 *	 .trim()	   // same as java's .trim()									 *
 *	 .toFilename(c)// make it filename 'friendly' (for Windows)					 *
 *	 .replaceAll(c,w) //replace c with w.  Will NOT work for all chars.			 *
 *	 .toNumeric()  // returns a numeric version of the string					 *
 *	 .isNumeric()  // true if there is a numeric version of the string.			 *
 *	 .left(n)	  // similar to .substring(n,m)  (negative n = 'all BUT n')		 *
 *	 .right(n)	 // similar to .substring(n,m)  (negative n = 'all BUT n')		 *
 *	 .repeat(n)	// repeats the string n times (n=0 returns "")					 *
 *	 .pad(n, c)	// negative n pads on the left side								 *
 *	 .round(dec)   // rounds to dec decimal places.								 *
 *	 .sameAs(str)  // counts the number of characters that str has the same		 *
 *	 .toProperCase(str)  // capitalize the first letter of each word.			 *
 *																				 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */


String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g, "");
};

String.prototype.toFilename = function(c) {  //replace: \/:*?"<>|
	if (!c) c="";
	return this.trim().replace(/\\|\/|\:|\*|\?|\"|\>|\<|\|/g, c);
	//return this.trim().replace(/\*|\:|\/|\\|\s/g, c);
};

String.prototype.replaceAll = function(c, w) {
	if ( typeof(c)=="string" && c.length>0 ) {
		eval("var replaced = this.trim().replace(/\\"+c.charAt(0)+"/g, w);");
		return replaced;
	} else
		return this;
};

String.prototype.escapeXML = function() {
	return this.replace(/\</g, "&lt;").replace(/\n/g, "<br>");
	//> &gt;, < &lt;, & &amp;, % &#37;
};

String.prototype.toNumeric = function() {
	v = parseFloat(this);
	return isNaN(v) ? 0 : v;
};

String.prototype.isNumeric = function() {
	v = parseFloat(this);
	return !isNaN(v);
};


String.prototype.left = function(n) {
	if(n>this.length) 
		n=this.length;
	else if (n<0) {
		n+=this.length;
		if (n<0) n=0;
	}
	return this.substring( 0, n );
};

String.prototype.right = function(n) {
	if(n>this.length) 
		n=this.length;
	else if (n<0) {
		n+=this.length;
		if (n<0) n=0;
	}
	return this.substring( this.length-n );
};



String.prototype.repeat = function(n) {
	if (n<1)
		return "";

	var a = new Array(n);
	for (var x=0; x<n; x++) 
		a[x]=this;

	return a.join("");
};

String.prototype.pad = function(n, c, trunc) {
	var text = "";
	if ( typeof(c)!="string" || c.length < 1 )
		c = " ";
	c = c.substring(0,1);

	if ( typeof(n)=="number" ) {
		var width = Math.abs(n);
		var onLeft = (n<0);
		var padding = width - this.length;

		if (padding >= 0) {

			if (onLeft)
				text = c.repeat(padding) + this;
			else
				text = this + c.repeat(padding);

		} else if (trunc==true)
			text = (onLeft) ? this.right(width) : this.left(width);
		else
			text = this;
	}

	return text;
};

String.prototype.round = function(nDec) {
	if (nDec==null || nDec<=0)
		return ""+ Math.round( this.toNumeric() );

	var mult =  Math.pow(10,nDec)
	
	var n = Math.round( this.toNumeric()*mult );	//correct number * 10^nDec
	var num = (""+n).pad(-(nDec+1),"0");			//make width at least nDec+1

	return num.left(-nDec)+"."+num.right(nDec);		//force exactly nDec decimals
}


String.prototype.sameAs = function(text) {
	if (text==this)
		return this.length;

	var size = Math.min(this.length, text.length);
	var match = true;
	var count=0;

	while(  count<size  &&  this.charAt(count)==text.charAt(count)  )
		count++;

	return count;
}

String.prototype.toProperCase = function() {
	var split = this.split(" ");
	var text = "";
	for (x=0; x<split.length; x++) {
		if (x!=0)
			text+=" ";
		text += split[x].substring(0,1).toUpperCase() + 
			split[x].substring(1).toLowerCase();
	}
	return text;
}
