﻿// This is the string library which conatins string manipulation strings which could 
// be called directly or used as slave by formating functions.

/////////////////////////////////////////////////////////////////////////////////////////
/// Written By Jamil On 29/07/2002
/// This function will trim the passed string , that is it will eliminate all leading 
/// and trailing SPACES, HTABS, VTABS, LF, CR and PF 
/////////////////////////////////////////////////////////////////////////////////////////////
function TrimStr(Str)
{
	Str = Str.replace(/^(\s+)+|(\s+)$/g,"");
	return Str
}

/////////////////////////////////////////////////////////////////////////////////////////
/// Written By Jamil On 29/072002 
/// this function will eliminate any repeated white spaces and replace them with a single
/// space.
/// Leading and trailing white spaces will be eliminated.
/////////////////////////////////////////////////////////////////////////////////////////////
function SqueezeStr(Str)
{
	Str = Str.replace(/^(\s+)+|(\s+)$/g,"");
	Str = Str.replace(/\s/g," ");
	Str = Str.replace(/(  +)/g," ");
	return Str
}


/////////////////////////////////////////////////////////////////////
// Compare the strings and true if equal .....
/////////////////////////////////////////////////////////////////////
function EqualStr(str1, str2, trim)
{
	//Initizlize
	if(arguments.length <3)
	{
		trim = true;
	}

	if(CompareStr(str1, str2, trim) == "e")
	{
		return true;
	}
	else
	{
		return false;
	}
}


/////////////////////////////////////////////////////////////////////
// Compare the strings and return e:equal;s:smaller;b:bigger.
/////////////////////////////////////////////////////////////////////
function CompareStr(str1, str2, trim)
{
	var inStr1, inStr2;
	
	//Initizlize
	if(arguments.length <3)
	{
		trim = true;
	}
	
	// The second string ....
	if(!str2)
	{
		inStr2 = new String("");
	}
	else
	{
		if(trim)
		{
			inStr2 = new String(str2);
		}
		else
		{
			inStr2 = new String(trimStr(str2));
		}
	}
	
	// the first string 
	if(!str1)
	{
		inStr1 = new String("");
	}
	else
	{
		if(trim)
		{
			inStr1 = new String(str1);
		}
		else
		{
			inStr1 = new String(trimStr(str1));
		}
	}
	
	// return the value ...
	if(inStr1.toUpperCase() < inStr2.toUpperCase())
	{
		return "s";
	}
	else if(inStr1.toUpperCase() > inStr2.toUpperCase())
	{
		return "b";
	}
	else
	{
		return "e";
	}
}

//////////////////////////////////////////////////////////////
// Count the words in a string.
//////////////////////////////////////////////////////////////
function CountWords(inStr, toTrim)
{
	var re = /\s+/;
	var tokenStr;
	
	// Do we have something to work with?
	if(!inStr)
	{
		inStr = ""
		return 0
	}
		
	if(!toTrim)
	{
		toTrim = 0;
	}
	
	// Should we squeeze the string.
	if(toTrim == 1)
	{
		inStr = SqueezeStr(inStr)
	}
	
	if(inStr == "")
	{
		return 0;
	}
	else
	{
		if(inStr.indexOf(" ") == -1)
		{
			return 1;
		}
		else
		{
			tokenStr = inStr.split(re)	
			return tokenStr.length;
		}
	}
}

/////////////////////////////////////////////////////////////////////////////////////////
/// Written By Jamil On 12/072005
/// this function will eliminate any white spaces 
/////////////////////////////////////////////////////////////////////////////////////////////
function ClearWS(Str)
{
	Str = Str.replace(/^(\s+)+|(\s+)$/g,"");
	Str = Str.replace(/\s/g,"");
	Str = Str.replace(/(  +)/g," ");
	return Str
}

/////////////////////////////////////////////////////////////
// pick the specified token from a delimitd string string.
/////////////////////////////////////////////////////////////
function PickToken(pStr, pDel, pPlace)
{
	var tokens;
	var token;
	var idx;

	//Split the string ...
	tokens = pStr.split(pDel)
	
	if(pPlace > tokens.length)
	{
		token = ""
	}
	else
	{
		token = tokens[pPlace-1];
	}
	return token
}

/////////////////////////////////////////////////////////
// LeftFill and RightFill sits on top strFill
// and Offer a shortcut to filling from left or right.
/////////////////////////////////////////////////////////
function LeftFill(oStr, fChar , nLen)
{
	return strFill(oStr,nLen,"L",fChar);
}

function RightFill(oStr, fChar , nLen)
{
	return strFill(oStr,nLen,"R",fChar);
}

/////////////////////////////////////////////////////////////////////////////////////////////////////
/// This function will fill the passed str pstr up to the length plength using the passed char pchar.
/// - pside = (L,R) decide if the filling will be added from the left or right.
/// - if pstr missing a blank str will be returned
/// - if pstr is already of length plength or greater than it will be returned as is.
/// - Only pstr and plength are required parameters
/// - When abscent pchar will default to space and pside will default to left.
/////////////////////////////////////////////////////////////////////////////////////////////////////
function strFill(pstr,plength,pside,pchar)
{
	var lengthGap;
	var usedChar, usedSide
	var newStr;
	var idx;
	
	if(!pstr)
	{
		pstr = "";
	}
	else
	{
		pstr = pstr + "";
	}

	if(arguments.length<2)
	{
		alert("The length you wish to extend to is missing.");
		return pstr;
	}

	// Default to a space if missing.
	usedChar = (pchar?pchar:" ");
	
	// Default to left side if missing.
	usedSide = (pside?pside.toLowerCase():"l");
	if((usedSide!="l") && (usedSide!="r"))
	{
		alert(pside + " is not a valid side, must be L(eft) OR R(ight)");
		return pstr;
	}

	// Fill the str as appropriate.
	lengthGap	= plength - pstr.length;
	newStr		= pstr + "";
	for(idx=0; idx < lengthGap; idx++)
	{
		if(usedSide=="l")
		{
			newStr	= usedChar + newStr;
		}
		else if(usedSide=="r")
		{
			newStr	= newStr + usedChar;
		}
	}
	return newStr;
}


///////////////////////////////////////////////////////////////////////////////
// Don Gledden
///////////////////////////////////////////////////////////////////////////////
function fixToProper(OrigValue)
{
	var temp = 0;
	var temp2 = 0;
	var stringValue  = OrigValue + " ";
	var lenString = OrigValue.length;
	OrigValue="";

	while (temp < lenString)
	{
		stringTemp = stringValue.substring(temp, temp+1);
		stringTemp2 = stringValue.substring(temp+1, temp+2);
		stringTemp3 = stringValue.substring(temp+2, temp+3);
		stringTemp4 = stringValue.substring(temp+3, temp+4);
		stringTemp5 = stringValue.substring(temp+4, temp+5);
		stringTemp6 = stringValue.substring(temp+5, temp+6);
			
		/*Explicity uppercase the first character of the string*/

		if (temp == 0)
		{
			OrigValue= OrigValue + stringTemp.toUpperCase();
			temp++;
		}
		else
		{
			/*Check to see if we've got a whitespace*/
			if (stringTemp == " ")
			{
					/*check for the word 'and', etc. */
					if ((stringTemp2 == "a") && (stringTemp3 == "n") && (stringTemp4 == "d") && (stringTemp5 == " "))
					{
						OrigValue= OrigValue + " ";
						temp++;
					}
					else if ((stringTemp2 == "t") && (stringTemp3 == "h") && (stringTemp4 == "e") && (stringTemp5 == " "))
					{
						OrigValue= OrigValue + " ";
						 temp++;
					}
					else if ((stringTemp2 == "t") && (stringTemp3 == "o") && (stringTemp4 == " "))
					{
						 OrigValue= OrigValue + " ";
						 temp++;
					}
					else if ((stringTemp2 == "f") && (stringTemp3 == "o") && (stringTemp4 == "r") && (stringTemp5 == " "))
					{
						 OrigValue= OrigValue + " ";
						 temp++;
					}
					else if ((stringTemp2 == "i") && (stringTemp3 == "n") && (stringTemp4 == " "))
					{
						OrigValue= OrigValue + " ";
						 temp++;
					}
					else if ((stringTemp2 == "a") && (stringTemp3 == " "))
					{
						 OrigValue= OrigValue + " ";
						 temp++;
					}
					else if ((stringTemp2 == "a") && (stringTemp3 == "t") && (stringTemp4 == " "))
					{
						 OrigValue= OrigValue + " ";
						 temp++;
					}
					else if ((stringTemp2 == "f") && (stringTemp3 == "r") && (stringTemp4 == "o") && (stringTemp5 == "m") && (stringTemp6 == " "))
					{
						 OrigValue= OrigValue + " ";
						 temp++;
					}
					else if ((stringTemp2 == "b") && (stringTemp3 == "y") && (stringTemp4 == " "))
					{
						 OrigValue= OrigValue + " ";
						 temp++;
					}
					else if ((stringTemp2 == "a") && (stringTemp3 == "n") && (stringTemp4 == " "))
					{
						 OrigValue= OrigValue + " ";
						 temp++;
					}
					else if ((stringTemp2 == "o") && (stringTemp3 == "r") && (stringTemp4 == " "))
					{
						 OrigValue= OrigValue + " ";
						 temp++;
					}
					else if ((stringTemp2 == "i") && (stringTemp3 == "f") && (stringTemp4 == " "))
					{
						 OrigValue= OrigValue + " ";
						 temp++;
					}
					else if ((stringTemp2 == "o") && (stringTemp3 == "f") && (stringTemp4 == " "))
					{
						 OrigValue= OrigValue + " ";
						 temp++;
					}
					else if ((stringTemp2 == "u") && (stringTemp3 == "p") && (stringTemp4 == " "))
					{
						 OrigValue= OrigValue + " ";
						 temp++;
					}
					else
					/*no special word, capitalize the first letter and skip it*/
					{
						OrigValue= OrigValue + " " + stringTemp2.toUpperCase();
						temp = temp + 2;
					}					
			}
			else
			/*no special characters or we're in the middle of the word, echo the character and step along*/
			{
				OrigValue= OrigValue + stringTemp;
				temp++;
			}
		}
	}
	var temp = 0;
	var temp2 = 0;
	return OrigValue;
}

