/* Add a class to a string */
String.prototype.addClass = function(theClass)
{
	if (this != "")
	{
		if (!this.classExists(theClass))
		{
			return this + " " + theClass;
		}
	}
	else
	{
		return theClass;
	}
	
	return this;
}

/* Check if a class exists in a string */
String.prototype.classExists = function(theClass)
{
	var regString = "(^| )" + theClass + "\W*";
	var regExpression = new RegExp(regString);
	
	if (regExpression.test(this))
	{
		return true;
	}
	
	return false;
}

/* Remove a class from a string */
String.prototype.removeClass = function(theClass)
{
	var regString = "(^| )" + theClass + "\W*";
	var regExpression = new RegExp(regString);
	
	return this.replace(regExpression, "");
}


String.prototype.validEmail = function()
{
	if (this.match(/^[\w\.\-]+@([\w\-]+\.)+[a-zA-Z]+$/))
	{
		return true;
	}
	
	return false;
}

String.prototype.validZipCode = function()
{
	if (this.match(/^\d{5}$/))
	{
		return true;
	}
	
	return false;
}

String.prototype.validPhone = function()
{
	if (this.match(/^((\([2-9]\d{2}\) ?)|([2-9]\d{2}-)|([2-9]\d{2}(\.| )?))\d{3}(-|\.| )?\d{4}$/))
	{
		return true;
	}
	
	return false;
}

String.prototype.validMatch = function(className, labelText)
{
		var pattern = /match-\w+(\s|$)/;
		var results, mId;
		
		if ((results = pattern.exec(className)) != null)
		{
			mId = results[0].substring(6).replace(/\s/,"");
		}
		
		if (this != document.getElementById(mId).value)
		{
			var spans2 = document.getElementById(mId).parentNode.getElementsByTagName("span");
			for (var j = 0; j < spans2.length; j++)
			{
				if (spans2[j].className.classExists("labelText"))
				{
					labelText2 = spans2[j];
				}
			}
			
			return "The fields "
				+ labelText2.childNodes[0].nodeValue.toLowerCase().replace(/:/, "")
				+ " and "
				+ labelText.childNodes[0].nodeValue.toLowerCase().replace(/:/, "") 
				+ " must match";
		}
		
		return "";
}