/* *******************************************************************
incStringsAPI.js
String Parsing Functions
-scott 6/1/06
******************************************************************** */

// -------------------------------------------------------------------
// gfunTrimString(sText)
// Trims both sides of a string
// -------------------------------------------------------------------
function gfunTrimString(sText) {
	var sTemp = sText;
	if (sTemp.length > 0) {
		// Remove Starting Spaces
		while (sTemp.substring(0,1).match(/\s/)) sTemp = sTemp.substring(1,sTemp.length);
		// Remove Trailing Spaces
		while (sTemp.charAt(sTemp.length-1).match(/\s/)) sTemp = sTemp.substr(0,sTemp.length-1);
	}
	return sTemp;
}


// -------------------------------------------------------------------
// gfunStripTags(sText)
// Strip HTML Tags from a String (Used for Text Only Title)
// -------------------------------------------------------------------
function gfunStripTags(sText) {
	var i, j;
	var sHTML = sText;
	
	var arySplit = new Array();
	arySplit = sHTML.split("<");

	var j = (arySplit[0].length > 0) ? 1 : 0;

	for (i=j;i<arySplit.length;i++){
		if (arySplit[i].indexOf(">") != -1) arySplit[i] = arySplit[i].substring(arySplit[i].indexOf(">")+1);
		else arySplit[i] = "<" + arySplit[i];
	}

	sHTML = arySplit.join("");
	sHTML = sHTML.substring(1-j);

	sHTML = sHTML.replace(/&amp;/gi,"&");
	sHTML = sHTML.replace(/&nbsp;/gi," ");

	return sHTML;
}


// -------------------------------------------------------------------
// gfunURLEncode(sText)
// URL Encodes a string
// -------------------------------------------------------------------
function gfunURLEncode(sText) {
	var sTemp = sText;
	if (sTemp.length > 0) {
		sTemp = sTemp.replace(/%/g,"%25");
		sTemp = sTemp.replace(/\+/g,"%2B");
		sTemp = sTemp.replace(/\s/g,"+");
		sTemp = sTemp.replace(/&/g,"%26");
		sTemp = sTemp.replace(/'/g,"%27");
		sTemp = sTemp.replace(/"/g,"%22");
		sTemp = sTemp.replace(/\./g,"%2E");
		sTemp = sTemp.replace(/\?/g,"%3F");
		sTemp = sTemp.replace(/\!/g,"%21");
		sTemp = sTemp.replace(/-/g,"%2D");
	}
	return sTemp;
}


// -------------------------------------------------------------------
// gfunURLDecode(sText)
// Un-Econdes a String
// -------------------------------------------------------------------
function gfunURLDecode(sText) {
	var sTemp = sText;
	if (sTemp.length > 0) {
		sTemp = sTemp.replace(/\%26/g,"&");
		sTemp = sTemp.replace(/\%27/g,"'");
		sTemp = sTemp.replace(/\%22/g,"\"");
		sTemp = sTemp.replace(/\%2E/g,".");
		sTemp = sTemp.replace(/\%3F/g,"?");
		sTemp = sTemp.replace(/\%21/g,"!");
		sTemp = sTemp.replace(/\%2D/,"-");
		sTemp = sTemp.replace(/\+/g," ");
		sTemp = sTemp.replace(/\%2B/g,"+");
		sTemp = sTemp.replace(/\%25/g,"%");
	}
	return sTemp;
}


// -------------------------------------------------------------------
// gfunMakeLength(sText, nSize, bEnd)
// Make a string a specific length
// Parameters:
//		sText: The String to format to a specific length
//		nSize: The length in characters to make the string
//		bPlaceAtEnd: True - Will place the spaces to increase the size at the end of the string.
//					 False - Will place the spaces at the beginning of the string.
// -------------------------------------------------------------------
function gfunMakeLength(sText, nSize, bPlaceAtEnd) {
	if (bPlaceAtEnd == null) bPlaceAtEnd = true;
	var sTemp = sText;
	if (sTemp.length > nSize) sTemp = sTemp.substr(0, nSize);
	if (sTemp.length < nSize) {
		if (bPlaceAtEnd) sTemp = sTemp + gfunCreateString( (nSize - sTemp.length), "^");
		else sTemp = gfunCreateString( (nSize - sTemp.length), "^") + sTemp;
	}

	sTemp = sTemp.replace(/\^/g," ");
	
	return sTemp;
}


// -------------------------------------------------------------------
// gfunCreateString(nLength,sChar)
// Returns a String of the same number of characters at a specified length
// -------------------------------------------------------------------
function gfunCreateString(nLength, sChar) {
	var sTemp = "";
	for (var i = 0; i < nLength; i++) sTemp += sChar;
	return sTemp;
}


// -------------------------------------------------------------------
// gfunReplaceBetween(sText, sStart, sEnd, sNewMiddle, bGlobal, nStartPos)
// Replaces all the text between a start and end values with new text
// Parameters:
//		sText: The String to format to a specific length
//		sStart: The Start text to find
//		sEnd: The End text to find
//		sNewMiddle: The End text to find
//		bGlobal: If true, replace all instances, else replace just the first.
//		bIgnoreCase: Use true for case-insensitive search of Start and End keys
//		nStartPos: The position to begin searching
// -------------------------------------------------------------------
function gfunReplaceBetween(sText, sStart, sEnd, sNewMiddle, bGlobal, bIgnoreCase, nStartPos) {
	if (sNewMiddle == null) sNewMiddle = "";
	if (bGlobal == null) bGlobal = true;
	if (bIgnoreCase == null) bIgnoreCase = false;
	if (nStartPos == null) nStartPos = 0;
	
	var sTemp = sText;
	var nEndPos;
	
	if (bIgnoreCase) {
		sStart = sStart.toLowerCase();
		sEnd = sEnd.toLowerCase();
	}

	// Set the Search value based on case setting variable
	var sSearchValue = (bIgnoreCase) ? sTemp.toLowerCase() : sTemp;
	while ( sSearchValue.indexOf(sStart,nStartPos) > -1 && sSearchValue.indexOf(sEnd,nStartPos) > (sSearchValue.indexOf(sStart,nStartPos) + sStart.length - 1) ) {
		// Get the Start of the text to Replace
		nStartPos = sSearchValue.indexOf(sStart, nStartPos) + sStart.length;
		nEndPos = sSearchValue.indexOf(sEnd, nStartPos);
		
		// Now Replace in between the start and End
		sTemp = sTemp.substring(0, nStartPos) + sNewMiddle + sTemp.substring(nEndPos, sTemp.length);
		
		// Move the Start Position
		nStartPos += sNewMiddle.length + sEnd.length;
			
		if (!bGlobal) {
			// Only go through this loop once!
			nStartPos = sTemp.length;
		}
		else {
			// Update the search value
			sSearchValue = (bIgnoreCase) ? sTemp.toLowerCase() : sTemp;
		}
	}
	
	return sTemp;
}


// -------------------------------------------------------------------
// gfunToTitleCase(sText)
// Returns a version of the String in Title Case
// Example: 
//      Input: "this is a test"
//      Output: "This Is A Test"
// Parameters:
//		sText: The String to get the Title Case version for
// -------------------------------------------------------------------
function gfunToTitleCase(sText)
{
    return str.replace(/\w+/g, function(sText){return sText.charAt(0).toUpperCase() + sText.substr(1).toLowerCase();});
}
