/*
String Function Library
*/
	
/* ======================================================
Function:	cropNumeric
Note:		Removes non numeric characters from a string.
=======================================================*/
String.prototype.cropNumeric = function () {
	return String(this.replace(/[^0-9.]/g, ''));
} 


/* ======================================================
Function:	isNumeric
Note:		Determines if a string is a valid numeric
			value.
=======================================================*/
String.prototype.isNumeric = function () {
	return !this.match(/\d+/) ? false : !isNaN(this);
}


/* ======================================================
Function:	lTrim
Note:		Removes whitespace chars from the left side
			of a string.
=======================================================*/
String.prototype.lTrim = function () {
	return this.replace(/^\s*/, "");
}


/* ======================================================
Function:	rTrim
Note:		Removes whitespace chars from the right side
			of a string.
=======================================================*/
String.prototype.rTrim = function () {
	return this.replace(/\s*$/, "");
}


/* ======================================================
Function:	trim
Note:		Removes whitespace chars from around a 
			string.
=======================================================*/
String.prototype.trim  = function () { 
	return this.rTrim().lTrim(); 
}


/* ======================================================
Function:	matchCount
Note:		Counts the instances of a regex that occur
			within a string.  For a regular text match
			use /YOUR_TEXT/g.  (Regexs don't use quotes)
			Good Regex Reference:
			http://www.evolt.org/article/Regular_Expressions_in_JavaScript/17/36435/
=======================================================*/
String.prototype.matchCount = function (SubString) {
	var matches = this.match(SubString)
	return matches ? matches.length : 0; 
}


/* ======================================================
Function:	wordCount
Note:		Counts the number of words in a string.
=======================================================*/
String.prototype.wordCount = function () {
	return this.matchCount(/\s*\S+\s*/g);
}


/* ======================================================
Function:	charCount
Note:		Counts the number of words in a string.
=======================================================*/
String.prototype.charCount = function () {
	// This is coded to match the cf charCount library function, MS Word, and work cross-platform - which is why it's not optimal.
	var linefeeds = 0;
	var lf_length = (this.match(/\x0D/) ? 1 : 0) + (this.match(/\x0A/) ? 1 : 0); // length of a CRLF

	for(c = 0; c < this.length; c++) {
		if(this.charCodeAt(c) == 13) {
			linefeeds += lf_length;
		}
	}
		
	return this.length - linefeeds;
}
