/// *********** Extend String Object ***********

// whitespace 
String.prototype.WHITESPACE = " \t\n\r";

// trims space, tabs, new lines from end of string
String.prototype.trimTrailingWhitespace = function() {
	return this.trimTrailing(this.WHITESPACE);
}

// trims space, tabs, new lines from front of string
String.prototype.trimLeadingWhitespace = function() {
	return this.trimLeading(this.WHITESPACE);
}


// trims any trailing truncated words, numbers, or symbols
String.prototype.trimTrailingTruncatedWord = function() {
	var trim = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*,.?()-_=+[]{}";
	return this.trimTrailing(trim);
}
	
/**
 * @param whatToTrim - the string of what to be trimming from the right of the string
 * Example:  if  param whatToTrim =  ' \t\n\r' then 'test   '  => 'test'
 */
String.prototype.trimTrailing = function(whatToTrim) {
	// copy ref to str
	var str = this; 

	// we have a string with trailing whatToTrim
	if(whatToTrim.indexOf(str.charAt(str.length - 1)) != -1) 
	{
		// loop from right until we don't find any trailing whatToTrim
		for(var i = str.length - 1; i >= 0 && whatToTrim.indexOf(str.charAt(i)) != -1; i--);

		// get substring from chars 0 to last non-whatToTrim char
		str = str.substring(0, i + 1);
	}
	return str;
}

/**
 * @param whatToTrim - the string of what to be trimming from the left of the string
  * Example:  if  param whatToTrim =  ' \t\n\r' then '   test'  => 'test'
 */
String.prototype.trimLeading = function(whatToTrim) {
	// copy ref to str
	var str = this; 

	// we have a string with a leading 'whatToTrim'
	if(whatToTrim.indexOf(str.charAt(0)) != -1) 
	{
		var strLen = str.length;
		// loop from left until we don't find any leading 'whatToTrim'
		for(var i = 0; i < strLen && whatToTrim.indexOf(str.charAt(i)) != -1; i++);

		// get new substring with leading 'whatToTrim' removed
		str = str.substring(i, strLen);
	}
	return str;
}

/**
 * @param grabAfter		String of what to start grabbing after
 * @param numCharsToGrab	number of characters to grab
 */
String.prototype.getSubstrAfter = function(grabAfter, numCharsToGrab) {
	var section = this; 
	var isTruncated = false;
			
	// find match
	var grabAfterDex = section.indexOf(grabAfter);
	if(grabAfterDex != -1) {
		section = section.substr(grabAfterDex + grabAfter.length);
	}

	// replace p, br, and spaces with empty space
	section = section.replace(/<p>/gi, " ");
	section = section.replace(/<\/p>/gi, " ");
	section = section.replace(/<br>/gi, " ");		
	section = section.replace(/<br\/>/gi, " ");
	section = section.replace(/<br \/>/gi, " ");
	section = section.replace(/&nbsp;/gi, " ");
	section = section.replace(/<p class="MsoNormal">/gi, " ");
	section = section.replace(/<span>/gi, " ");
	section = section.replace(/<\/span>/gi, " ");
	section = section.replace(/size="(\d)"|face="(\d|\w|\s)*"/gi, " ");
	section = section.replace(/<font>/gi, " ");
	section = section.replace(/<\/font>/gi, " ");
	section = section.replace(/<div>/gi, " ");
	section = section.replace(/<\/div>/gi, " ");
	section = section.replace(/<a(.)*<\/a>/gi, " ");
	section = section.replace(/<[^>]+>/gi, " ");
	
		
	section = section.trimLeadingWhitespace();
		
	// if numCharsToGrab is not set, default 60
	numCharsToGrab = numCharsToGrab || 60;

	if(section.length > numCharsToGrab) {
		section = section.substr(0, numCharsToGrab);
		section = section.trimTrailingTruncatedWord();
		isTruncated = true;
	}

	section = section.trimTrailingWhitespace()
	
	if(isTruncated) {
		section += '...';
	}
	
	return section;
}	
