// String prototype functions
// trim functions
function strprotrtrim() { return this.replace(/^\s+/,''); }
function strprotltrim() { return this.replace(/\s+$/,''); }
function strprottrim() { return this.replace(/^\s+/,'').replace(/\s+$/,''); }
String.prototype.ltrim = strprotltrim;
String.prototype.rtrim = strprotrtrim;
String.prototype.trim = strprottrim;
// @name stringUtils
// @desc an object that contains javascript-based string utility functions
// @returns void
function stringUtils()
{
	this.DIGITS = '0123456789';
	this.SQUOTE = '\'';
	this.QUOTE = '\"';
	this.DELIMITER = ',';
	// @name substituteStr
	// @desc Substitutes arguments array into a string.  Syntax of replace values is modeled after java.text.MessageFormat class; i.e., {0} is replaced by args[0], {1} is replaced by args[1], etc.
	// @param str String that arguments will be substituted into
	// @param args Array of substitution values
	// @returns str with substitutions
	this.substituteStr = function(str,args)
	{
		if(str == null || typeof(str) == 'undefined' || typeof(args) == 'undefined')
			return str;
		returnstr = str;

		re = new RegExp('\\{0\\}');
		
		for(i = 0; i < args.length; i++)
		{
			re.compile('\\{' + i + '\\}','gi');
			returnstr = returnstr.replace(re,args[i]);
		}
		return returnstr;
	}
	// USE THIS WHEN SAFARI CAUSES PROBLEMS
	this.substituteStrReplace = function(str, args)
	{
		if(str == null || typeof(str) == 'undefined' || typeof(args) == 'undefined')
			return str;

		for(i = 0; i < args.length; i++)
		{
			var substr = "{"+i+"}";
			if (str.indexOf(substr) > -1)
			{
				var replacestr = args[i];
				var result = str.substring(0,str.indexOf(substr)) + replacestr + str.substring(str.indexOf(substr)+substr.length,str.length);
				str = result;
				//alert(result);
			}
		}
		return str;
	}
	// @name cleanUp
	// @desc cleans up bad msword characters that are converted to questions marks
	// @param currvalue String to clean up (usually from a text field)
	// @returns currvalue with substitutions
	this.cleanUp = function(text)
	{
		return this.encodeHighAscii(text);
	}
	this.fixUrls = function(src, usehandler)
	{
		// diable for now
		usehandler = false;
		var loc = window.location;
		var searchstr = 'href="' +loc.protocol + '//' + loc.hostname + loc.port;
		// src = src.replace(/\?exclusive=systemmgr\.linkhandler\&amp;/g,'?exclusive=systemmgr.linkhandler&');
		while (src.indexOf(searchstr) > -1)
		{
			if (usehandler)
				src = src.replace(searchstr, 'href="?exclusive=systemmgr.linkhandler&href=');
			else
				src = src.replace(searchstr, 'href="');
		}
		// add target attributes
		src = src.replace(/target=([A-z_]+)/g, 'target="$1"');
		return src;
	}
	this.cleanUpField = function(src)
	{
		if (!src.value) 
			return 'Object does not have a value!';
		src.value = this.cleanUp(src.value);
		return src.value;
	}
	this.makeTextId = function(obj1, obj2, maxlen)
	{
		if(obj2.value.length > 0) return;
		if (maxlen == null) maxlen = 10
		var source = obj1.value.substring(0,maxlen).toLowerCase();
		source = this.replaceChar(source,' ','_');
		obj2.value = source;
	}
	this.replaceChar = function(src,replacecharc,replacewithchar) 
	{
		var buffer = "";
		for(i=0; i<src.length; i++)
		{
			currchar = src.charAt(i);
			if (currchar != replacecharc) buffer += currchar;
			else buffer += replacewithchar;
		}
		return buffer;
	}
	this.stripNonNumeric = function(value)
	{
		var result = '';
		for (var i = 0; i < value.length; i++)
		{
			var current = value.charAt(i);
			if (this.DIGITS.indexOf(current) > -1) result += current;
		}
		return result;
	}
	this.trim = function(object)
	{
		if (!object || !object.value) return '';
		var trimmed = object.value.trim();
		object.value = trimmed;
	}
	this.encodeHighAscii = function(text)
	{
		var result = '';
		for (var i = 0; i < text.length; i++)
		{
			if (text.charCodeAt(i) > 127) 
				result += '&#'+ text.charCodeAt(i)+';';
			else 
				result += text.charAt(i);
		}
		return result;
	}
	this.listToString = function(list, delimiter)
	{
		if (!delimiter)
			delimiter = this.DELIMITER;
		var result = '';
		for (var i = 0; i < list.length; i++)
		{
			if (result.length > 0)
				result += delimiter;
			result += list[i];			
		}
		return result;
	}
	this.stripQuotes = function(text)
	{
		var result = text.replace('"','');
		result = result.replace("'","");
		return result;
	}
	this.getQueryString = function(except)
	{
		var thishref = window.location.href;
		if (thishref.indexOf('?') == -1) return '';
		else qs = thishref.substring(thishref.indexOf('?')+1,thishref.length);
		return this.exceptQS(qs, except);
	}
	this.putQS = function(qs, name, value)
	{
		var parameterArray = qs.split('&');
		var rebuiltqs = '';
		var modqs = false;
		for (i = 0; i < parameterArray.length; i++)
		{
			if (parameterArray[i].indexOf('=') > -1)
			{
				nameValueArray = parameterArray[i].split('=');
				if (nameValueArray.length == 2)
				{
					if (nameValueArray[0] == name)
					{
						nameValueArray[1] = value;
						modqs = true;
					}
				}
				if (rebuiltqs.length > 0) rebuiltqs += '&';
					
				rebuiltqs += nameValueArray[0]+'='+nameValueArray[1];
			}
		}
		// didn't match name, append
		if (!modqs)
		{
			if (rebuiltqs.length > 0) rebuiltqs += '&';
			rebuiltqs += name+'='+value;
		}
		return rebuiltqs;
	}
	this.getQS = function(qs, name)
	{
		var parameterArray = qs.split('&');
		var result = '';
		for (i = 0; i < parameterArray.length; i++)
		{
			nameValueArray = parameterArray[i].split('=');
			if (nameValueArray.length == 2) if (nameValueArray[0] == name) result == nameValueArray[1];
		}
		return result;
	}
	this.exceptQS = function(qs, except)
	{
		var parameterArray = qs.split('&');
		var rebuiltqs = '';
		for (i = 0; i < parameterArray.length; i++)
		{
			passparam = true;
			if (except != null && except.length > 0)
				if (parameterArray[i].indexOf(except+'=') > -1)
					passparam = false;
			if (passparam)
			{			
				if (i > 0)
					rebuiltqs += '&';
				rebuiltqs += parameterArray[i];
			}
		}
		return rebuiltqs;
	}
	this.wrapDelim = function(text, delim)
	{
		if (delim == null)
			delim = ',';
		if (text == null)
			text = '';
		if (text.charAt(0) != delim)
			text = delim + text;
		if (text.charAt(text.length-1) != delim)
			text = text + delim;
		return text;
	}
	this.escapeHtmlQuotes = function(text)
	{
	    return this.replaceChar(text,'\"','&quot;');
	}
}
stringutils = new stringUtils();

