/*

*******************************************************************
File: string.js
Date: 21/02/2005
Purpose: Provides extra useful routines to the string object prototype, primarily used in form validation

HISTORY:
*******************************************************************

*/

String.prototype.properCase = function()
{
	var strReturn = "";
	var intLength = this.length;
	var boolUCaseNext = false;
	
	if (intLength == 0)
	{
		return "";
	}
	
	strReturn += this.charAt(0).toUpperCase();
	
	for (var iCounter = 1; iCounter < intLength; iCounter++)
	{
		if (boolUCaseNext == true)
		{
			strReturn += this.charAt(iCounter).toUpperCase();
		}
		else
		{
			strReturn += this.charAt(iCounter).toLowerCase();
		}
		
		var iChar = this.charCodeAt(iCounter);
		
		if (iChar == 32 || iChar == 45 || iChar == 46)
		{
			boolUCaseNext = true;
		}
		else
		{
			boolUCaseNext = false
		}
		
		if (iChar == 99 || iChar == 67)
		{
			if (this.charCodeAt(iCounter-1) == 77 || this.charCodeAt(iCounter-1)==109)
			{
				boolUCaseNext = true;
			}
		}
	} 

	return strReturn;
}

String.prototype.ltrim = function()
{
	// Match spaces at beginning of text and replace with an empty string
	// i.e. trims leading white space
	return this.replace(/^\s+/,'');
}

String.prototype.rtrim = function() 
{
	// Match spaces at end of text and replace with an empty string
	// i.e. trims trailing white space
	return this.replace(/\s+$/,'');
}

String.prototype.trim = function() 
{
	// Match spaces at beginning and end of text and replace with an empty string
	// i.e. trims trailing and leading white space
	
	//return this.replace(/^\s+/,'').replace(/\s+$/,'');
	return this.ltrim().rtrim();
}


String.prototype.isNumeric = function() 
{
	// returns true if the string is a number (can have optional decimal places, but no commas)
	return this.trim().match(/^[\d]*\.?[\d]*$/);
}

String.prototype.isInteger = function()
{
	// returns true if the string is a valid whole number (no decimal places, commas)
	return this.trim().match(/^\d*$/);
}

String.prototype.isEmailAddress = function()
{
	// returns true if the string is a valid email address
	return this.trim().match(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/);
}

String.prototype.asFloat = function()
{
	// returns the float-typed value if the string is a float
	// but safely returns a float zero if the value is not a float (useful in arithmetic expressions on form fields)
	
	if (isNaN(this))
		return 0.0;
	else
		if (this.trim() == '')
			return 0.0;
		else
			return parseFloat(this);
}


