// ************************************************************************<BR>
// DHTML functions for manipulating strings.							   <BR>
// ************************************************************************<BR>
// <SCRIPT>

var TRIM_LEADING	= 1;
var TRIM_TRAILING	= 2;
var TRIM_BOTH		= 3;

// Returns a copy of self's string with no leading and/or trailing spaces.
String.prototype.trim = function(flags)
{
	var result = this;
	
	// If flags wasn't passed trim both sides
	if (!flags)
		flags = TRIM_BOTH;
		
	var iPos = 0;
	if (flags & TRIM_LEADING)
	{
		while (iPos < result.length && result.charAt(iPos) == ' ')
			iPos++;
		result = result.substr(iPos, result.length - iPos);
	}
	
	if (flags & TRIM_TRAILING)
	{
		for (iPos = result.length - 1; iPos >= 0 && result.charAt(iPos) == ' '; )
			--iPos;
		result = result.substr(0, iPos + 1);
	}
	
	return result;
}

// Returns true if self's string is a valid date (in mm/dd/yyyy format).
String.prototype.isDate = function()
{
	return (this != "" && !isNaN(Date.parse(this)));
}

// Replaces all occurrences of from with to.
// Note: String.replace does NOT replace all occurrences (contrary to the docs).
String.prototype.replaceAll = function(from, to)
{
	var result = "";

	// Loop through and create a brand new string with the replaced text.
	for (var nFrom = 0, nTo = 0; (nTo = (this.substr(nFrom).indexOf(from) + nFrom)) >= nFrom; nFrom = nTo + from.length)
		result += (this.substr(nFrom, nTo - nFrom) + to);
	
	return (result += this.substr(nFrom));	
}

// Returns self converted to a number.  Any thousands separator is removed before making the conversion.
// If englishFormat is false, the commas and decimal points are swapped (Spanish format).
String.prototype.toNumber = function(englishFormat)
{
	var comma = (englishFormat == false ? "." : ",");

	var value = this.trim().replaceAll(comma, "");	

	// If we're not doing English, change the comma into a period for parseFloat to work (below)
	if (englishFormat == false)
		value = value.replaceAll(",", ".");
	
	return parseFloat(value);	
}

// Returns self's numeric string formatted using commas to separate the thousand's place.
// If englishFormat is false, the commas and decimal points are swapped (Spanish format).
String.prototype.toNumericFormat = function(englishFormat)
{
	var point = (englishFormat == false ? "," : ".");
	var comma = (englishFormat == false ? "." : ",");
	
	// Check that it's not empty
	if (this.trim() == "")
		return "";		
		
	// Verify it's a valid number
	var dValue = parseFloat(this.toNumber());	
	if (isNaN(dValue))
		return "";

	// Get the decimal points location
	var value = dValue.toString();
	
	// If we're not doing English, change the period back to a comma
	if (englishFormat == false)
		value = value.replaceAll(".", ",");
	
	var dot = value.lastIndexOf(point);
	if (dot < 0)
		dot = value.length;
		
	// Insert the comma starting 3 places from the decimal point
	while ((dot -= 3) > 0)
		value = value.substr(0, dot) + comma + value.substr(dot);
	
	return value;
}

// Formats the numeric value in the given element using commas to separate the thousand's place.
// If elementTitleForErrorMessage is not null, an alert box is shown if the value is not numeric.
// If englishFormat is false, the commas and decimal points are swapped (Spanish format).
String.prototype.formatNumericElement = function(element, elementTitleForErrorMessage, englishFormat)
{
	// Do the conversion
	var value = this.toNumericFormat(englishFormat);

	// Check for an error
	if (value == "" && this != "")
	{		
		if (elementTitleForErrorMessage != null)
		{
			alert("Please enter a valid numeric value" + (elementTitleForErrorMessage != "" ? (" for '" + elementTitleForErrorMessage + "'") : "") + ".");
			element.focus();
		}
		return false;
	}
		
	// Change the element's original value
	element.value = value;
	return true;
}

// Returns true if the given field's value is not empty; otherwise it shows an error message 
// using the given label and returns false.
function ValidateNumeric(field, label, englishFormat)
{
	var value = field.value.trim();
	var point = (englishFormat == false ? "," : ".");

	if (value == "" || ((value.substr(0, 1) < '0' || value.substr(0, 1) > '9') && value.substr(0, 1) != point))
	{
		alert("Please enter a valid numeric value for the " + label + ".");
		field.focus();
		return false;
	}
	return true;
}

