function FormElement ()
{
}

FormElement.getLeft = function (obj)
{
	var left = 0;
	do {left += obj.offsetLeft;}
	while (obj = obj.offsetParent);
	return left;
}

FormElement.getTop = function (obj)
{
	var top = 0;
	top += obj.offsetHeight;
	do {top += obj.offsetTop;}
	while (obj = obj.offsetParent);
	return top;
}

FormElement.getElement = function (nameOrId)
{
	var obj = document.getElementById(nameOrId);
	// Some IE hacking:
	// In IE, getElementById() will find an object with the given name
	// rather than restricting it's search to id's only. Hence,
	// the obj.id != nameOrId condition.
	if (!obj || obj.id != nameOrId)
	{
		for (var i = 0; i < document.forms.length; i++)
		{
			obj = document.forms[i].elements[nameOrId];
			if (obj)
			{
				break;
			}
		}
	}
	if (!obj)
	{
		alert("Object corresponding to '" + nameOrId + "' not found!");
	}
	return obj;
}

// nameOrId: form element name or id
// display: any of the valid display styles ("block", "none", etc)
FormElement.setDisplay = function (nameOrId, display)
{
	var obj = FormElement.getElement(nameOrId);
	if (obj)
	{
		obj.style.display = display;
	}
}

// nameOrId: form element name or id
FormElement.disable = function (nameOrId)
{
	var obj = FormElement.getElement(nameOrId);
	if (obj)
	{
		obj.disabled = true;
	}
}

// nameOrId: form element name or id
FormElement.enable = function (nameOrId)
{
	var obj = FormElement.getElement(nameOrId);
	if (obj)
	{
		obj.disabled = false;
	}
}

// Returns true if the element is selected.
// In the case of a text box, this means that the text box
// has a non-space value.
FormElement.isSelected = function (nameOrId)
{
	var value = FormElement.getFirstSelectedValue(nameOrId);
	return (value != null && value != "");
}

// Returns the text of an input box, or the first selected
// value in a radio, select, or checkbox list.
// Optionally can return nulls as empty strings to avoid headache.
FormElement.getFirstSelectedValue = function (nameOrId, nullsAsEmptyString)
{
	var value = FormElement.getFirstSelectedValue(nameOrId);
	if (value == null && nullsAsEmptyString)
	{
		return "";
	}
	return value;
}

// Returns the text of an input box, or the first selected
// value in a radio, select, or checkbox list.
FormElement.getFirstSelectedValue = function (nameOrId)
{
	var obj = FormElement.getElement(nameOrId);
	if (!obj)
	{
		return null;
	}

	var firstObj = obj;
	if (typeof(obj.tagName) == "undefined"
			&& typeof(obj.length) != "undefined")
	{
		firstObj = obj[0];
	}

	var tag = firstObj.tagName.toLowerCase();
	if (tag == "select")
	{
		for (var i = 0; i < obj.length; i++)
		{
			if (obj.options[i].selected)
			{
				return obj.options[i].value;
			}
		}
	}
	else if (tag == "input")
	{
		switch (firstObj.type.toLowerCase())
		{
			case "checkbox":
			case "radio":
				// An array of elements (found by name)
				if (typeof(obj.length) != "undefined")
				{
					for (var i = 0; i < obj.length; i++)
					{
						if (obj[i].checked)
						{
							return obj[i].value;
						}
					}
				}
				else
				// One element (found by id)
				{
					if (obj.checked)
					{
						return obj.value;
					}
					return null;
				}
				break;
			case "text":
				return TextUtility.trim(obj.value)
		}
	}
	else if (tag == "textarea")
	{
		return TextUtility.trim(obj.value)
	}
	return null;
}

function TextUtility ()
{
}

TextUtility.isDate = function (date)
{
	var seconds = Date.parse(date + "");
	return !isNaN(seconds);
}

TextUtility.isEmail = function (inVal)
{
	var email = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
	return email.test(inVal);
}

// check if a string follows European phone number format 
// xx-xx-xxx-xxxx
TextUtility.isEuroPhone = function (inVal)
{
	inVal = Trim(inVal);
	if (inVal.length == 0)
	{
		return true;
	}
	if (inVal.length != 14)
	{
		return false;
	}

	// pattern matching using RE
	var euroPhone = /\d\d-\d\d-\d\d\d-\d\d\d\d/;
	return euroPhone.test(inVal);
}

// check if a string follows US phone number format 
// xxx-xxx-xxxx or xxxxxxxxxx
TextUtility.isUSPhone = function (inVal)
{
	inVal = Trim(inVal);

	if (inVal.length == 0)
	{
		return true;
	}

	var USPhone = /\d\d\d-\d\d\d-\d\d\d\d/;
	var USPhone2 = /\d\d\d\d\d\d\d\d\d\d/;
	
	if ((inVal.length == 12 && USPhone.test(inVal)) || 
		(inVal.length == 10 && USPhone2.test(inVal)))
	{
		return true;
	}
	else
	{
		return false;
	}
}

// check if a string follows US zip format 
// xxxxx or xxxxx-xxxx
TextUtility.isUSZip = function (inVal)
{
	inVal = Trim(inVal);
	if (inVal.length == 0)
	{
		return true;
	}
	if (inVal.length != 5 && inVal.length != 10)
	{
		return false;
	}

	var USZip = /\d\d\d\d\d/;
	var USZip2 = /\d\d\d\d\d-\d\d\d\d/;

	if ((USZip.test(inVal) && inVal.length == 5) || USZip2.test(inVal))
	{
		return true;
	}
	return false;
}

TextUtility.trim = function (input)
{
	return (input + "").replace(/^\s+|\s+$/gm, "");
}

function ClientSideAlert ()
{
	this.header = "";
	this.messages = new Array();
	
	if (arguments.length == 1)
	{
		this.header = arguments[0];
	}
}

ClientSideAlert.prototype.addMessageLine = function (message)
{
	this.messages.push(message + "\n");
}

ClientSideAlert.prototype.addMessage = function (message)
{
	this.messages.push(message);
}

ClientSideAlert.prototype.hasMessages = function ()
{
	return (this.messages.length > 0);
}

ClientSideAlert.prototype.display = function ()
{
	var message = this.header;
	for (var i = 0; i < this.messages.length; i++)
	{
		message += this.messages[i];
	}
	alert(message);
}

/*
FormElement.getFirstValue = function (nameOrId)
{
	var obj = FormElement.getElement(nameOrId);
	var tag = obj.tagName.toLowerCase();
	if (tag == "select")
	{
		return FormElement.getSelectListFirstValue(obj);
	}
	else if (obj.tagName == "input")
	{
		switch (obj.type.toLowerCase())
		{
			case "checkbox";
		if (obj.type.toLowerCase() == "checkbox")
		{
		}
	}
}
*/

/*
function SelectListItems (list, item)
{
	if (list == null)
	{
		return;
	}
	for (var i = 0; i < list.length; i++)
	{
		list.options[i].selected = (list.options[i].value == item.toString());
	}
}
*/

