/*	Copyright (c) 1996,2001 Picdar Technology Limited       All Rights Reserved
 *
 * 	This is UNPUBLISHED PROPRIETARY work of Picdar Technology Limited;
 * 	the contents of this file may not be disclosed to third parties, copied or
 * 	duplicated in any form, in whole or in part, without the prior written
 * 	permission of Picdar Technology Limited
 *
 *	Module   :	String001.js
 *
 *	Synopsis :	Various String Utils for use in javascript functions.
 *
 *	Author   :	Adam Creeger
 *
 */

/*	Does the string contain the value ?
 */
function ContainsValue(string,value)
	{
	var isIn = false;
	var tmpStr = "";
	
	for (j = 0; !isIn && j < string.length; ++j)
		{
		if (string.charAt(j) != "\r" && string.charAt(j) != "\n" && j != string.length - 1)
			tmpStr += string.charAt(j);
		else
			{
			if (j == string.length - 1)
				tmpStr += string.charAt(j);
			if (value == tmpStr)
				isIn = true;
			tmpStr = "";
			}
		}
	
	return isIn;
	}

/* This function examines "strSource" and retrieves the arguments of the function "strFunc".
 */
function GetFunctionArgs(strFunc,strSource)
	{
	var results = new Array();
	var result = "-1";
	var argString = "";
	
	/*	First, match the fucntion anme plus the opening (. We read into argString everything
		found after this string
	 */
	var myRegExp = eval("/" + strFunc + "\\((.*)/i");
	myRegExp.multiline = true;
	if (myRegExp.test(strSource))
		{
		results = strSource.match(myRegExp);
		argString = results[1];
		}
	
	/*	Now iterate through the string to read the arguemnts in. We read until we find the
		matching ) for the fucntions arguments. ) inside quoted strings are ignored. The 
		commas spearing the arguments are changed into | characters. Any | characters
		in the arguments are changed to ' ' (a single space). This is to allow the arguments
		to be quickly split up. Spliting on a comma splits string arguments with commas in it
		up also !
	 */
	var i;
	var strLen = argString.length;
	var inQuotes = false;
	var bracketCount = 1;
	var c;
	var resultStr = "";
	
	for (i = 0; i < strLen; ++i)
		{
		c = argString.charAt(i)
		if (c == "'")
			{
			if (inQuotes)
				inQuotes = false;
			else
				inQuotes = true;
			}
		
		if (c == "(" && !inQuotes)
			++bracketCount;
		
		if (c == ")" && !inQuotes)
			--bracketCount;
		
		if (bracketCount == 0)
			break;
		
		if (!inQuotes && c == ",")
			c = "|";
		if (inQuotes && c == "|")
			c = " ";
		
		resultStr += c;
		}
	
	if (resultStr != "")
		result = resultStr;
	
	return result
	}
	
/* This function examines "strSource" and retrieves the last argument of the function "strFunc".
*/

function GetLastArg(strFunc,strSource)
	{
	strArgs = GetFunctionArgs(strFunc,strSource);
	var results = strArgs.split('|');
	result = results[results.length - 1];
	
	result = StringTrim(result," ");
	if (result.charAt(0) == "'" && result.charAt(result.length - 1) == "'")
		result = result.substring(1,result.length - 1);
	else if (result.charAt(0) == '\"' && result.charAt(result.length - 1) == '\"')
		result = result.substring(1,result.length - 1);
	return result;
	}

/* This function returns true if "strValue" is just whitespace.
*/

function IsEmpty(strValue)
	{
	var MyRegExp = /^\s*$/; //Check for entirely white space.
	return MyRegExp.test(strValue);
	}

/*
This function is used to Trim "strChar" from the left of the string "strValue".
*/	

function StringTrimLeft(strValue,strChar)
	{
	var result = strValue;
	while (result.charAt(0) == strChar)
		{
		result = result.substring(1,result.length);
		}
	return result;
	}
	
/*
This function is used to Trim "strChar" from the right of the string "strValue".
*/	

function StringTrimRight(strValue,strChar)
	{
	var result = strValue;
	while (result.charAt(result.length - 1) == strChar)
		{
		result = result.substring(0,result.length - 1);
		}
	return result;
	}


/*
This function is used to Trim "strChar" from both sides of the string "strValue".
*/

function StringTrim(strValue,strChar)
	{
	strValue = StringTrimLeft(strValue,strChar);
	strValue = StringTrimRight(strValue,strChar);
	return strValue;
	}
	
/*
This function is used to insert "strChar" into the beginning of "strValue" until
the length of "strValue" is "intMaxLength".
*/

function StringPadLeft(strValue,strChar,intMaxLength)
	{
	if (strChar!="")
		{
		while (strValue.length < intMaxLength)
			strValue = strChar + strValue;
		}
	return strValue
	}

/*
This function is used to append "strChar" to "strValue" until
the length of "strValue" is "intMaxLength".
*/

function StringPadRight(strValue,strChar,intMaxLength)
	{
	if (strChar!="")
		{
		while (strValue.length < intMaxLength)
			strValue = strValue + strChar;
		}
	return strValue
	}