function Trim(text)
{
	ndx = 0;
    lgth = text.length;

    while ((ndx < lgth) && (text.charAt(ndx) == ' ')) ++ndx;
    while ((lgth > ndx) && (text.charAt(lgth - 1) == ' ')) --lgth;
    return text.substring(ndx, lgth);
}

function IsNumeric(num)
{
	ndx = 0;
    lgth = num.length;

	while (ndx < lgth) {
        if ((num.charAt(ndx) < '0') || (num.charAt(ndx) > '9'))
			return false;
		++ndx;
	}

	return true;
}

function IsAmount(num)
{
	ndx = 0;
	dec = 0;
    lgth = num.length;

	while (ndx < lgth) {
        if ((num.charAt(ndx) < '0') || (num.charAt(ndx) > '9')) {
			if ((num.charAt(ndx) == '.') && (dec == 0)) {
				dec++;
				ndx++;
				continue;
			}

			return false;
		}
		++ndx;
	}

	return true;
}

function FormError(obj, message)
{
	alert(message);
	obj.focus();
	return false;
}

function EditRequired(obj, msg)
{
	obj.value = Trim(obj.value);
	if (obj.value.length == 0) {
		return FormError(obj, msg);
	}

	return true;
}

function EditSelected(obj, msg)
{
	if (obj.selectedIndex >= 0) {
		if (obj.options[selectedIndex].text.charAt(0) != '-')
			return true;
	}

	return FormError(obj, msg);
}

function EditRadio(obj, msg)
{
    for (ndx = 0; ndx < obj.length; ndx++) {
        if (obj[ndx].checked)
			return true;
	}

	return FormError(obj[0], msg);
}

function FormatText(obj)
{
	obj.value = Trim(obj.value);
}

function FormatUpper(obj)
{
	obj.value = Trim(obj.value.toUpperCase());
}

function FormatNum(obj)
{
	obj.value = Trim(obj.value);

	if (!IsNumeric(obj.value))
		return FormError(obj, "The value entered must be numeric");

	return true;
}

function FormatAmt(obj)
{
	obj.value = Trim(obj.value);

	if (!IsAmount(obj.value))
		return FormError(obj, "The value entered must be an amount");

	return true;
}

function FormatPostal(obj)
{
	FormatUpper(obj);
	if ((obj.value.length == 6) && (obj.value.charAt(0) >= 'A') && (obj.value.charAt(0) <="Z"))
		obj.value = obj.value.substring(0, 3) + ' ' + obj.value.substring(3, 6);
}

function PhoneError(obj)
{
	return FormError(obj, '"' + obj.value + '" is not a valid telephone number.\n' +
							'North American numbers must include the area code.\n' +
							'International numbers must begin with "+".');
}

function FormatPhone(obj)
{
	obj.value = Trim(obj.value);

	if (obj.value.length == 0)
		return true;

	ndx = 0;
	num = obj.value;

	if (num.charAt(0) == '+') {
		ndx = 1;
		while (ndx < obj.value.length) {
			if ( ((num.charAt(ndx) < '0') || (num.charAt(ndx) > '9')) &&
				(num.charAt(ndx) != ' ') &&
				(num.charAt(ndx) != '-') &&
				(num.charAt(ndx) != '.') )
				return PhoneError(obj);
			++ndx;
		}

		return true;
	}

	if ((num.charAt(0) == '(') && (num.charAt(4) == ')')) {
		area = num.substring(1, 4);
		num = Trim(num.substring(5));
	} else {
		area = num.substring(0, 3);
		if ((num.charAt(3) == '-') || (num.charAt(3) == '.'))
			num = Trim(num.substring(4));
		else
			num = Trim(num.substring(3));
	}

	xchg = num.substring(0,3);
	if ((num.charAt(3) == '-') || (num.charAt(3) == '.'))
		num = Trim(num.substring(4));
	else
		num = Trim(num.substring(3));

	if (num.length > 4) {
		if ((num.charAt(4) == '-') || (num.charAt(4) == '.'))
			ext = Trim(num.substring(5));
		else
			ext = Trim(num.substring(4));

		num = Trim(num.substring(0, 4));
	}
	else
		ext = '';

	for (ndx = 0; ndx < ext.length; ndx++) {
		if ((ext.charAt(ndx) >= '0') && (ext.charAt(ndx) <= '9'))
			break;
	}

	ext = Trim(ext.substring(ndx));

	if (ext.length > 0)
		ext = 'Ext. ' + ext;

	if ((area.length != 3) || !IsNumeric(area) ||
		(xchg.length != 3) || !IsNumeric(xchg) ||
		(num.length  != 4) || !IsNumeric(num) ||
		(area.charAt(0) == '0') || (area.charAt(0) == '1') ||
		(xchg.charAt(0) == '0') || (xchg.charAt(0) == '1')) {
		return PhoneError(obj);
	}

	obj.value = Trim(area + ' ' + xchg + '-' + num + ' ' + ext);
	return true;
}

function PhoneRequired(obj, msg)
{
	if (!EditRequired(obj, msg))
		return false;

	return FormatPhone(obj);
}

function EmailError(obj)
{
	return FormError(obj, '"' + obj.value + '" is not a valid email address.');
}

function FormatEmail(obj)
{
	obj.value = Trim(obj.value);

	if (obj.value.length == 0)
		return true;

	ndx = 0;
	atsign = false;
	dot = false;

	while (ndx < obj.value.length) {
		if (obj.value.charAt(ndx) == ' ')
			return EmailError(obj);

		if (obj.value.charAt(ndx) == '@') {
			atsign = true;
			++ndx;
			break;
		}

		++ndx;
	}

	if (!atsign) return EmailError(obj);

	while (ndx < obj.value.length) {
		if (obj.value.charAt(ndx) == ' ')
			return EmailError(obj);

		if (obj.value.charAt(ndx) == '.') {
			dot = true;
		}

		++ndx;
	}

	if (!dot) return EmailError(obj);

	return true;
}

function EmailRequired(obj, msg)
{
	if (!EditRequired(obj, msg))
		return false;

	return FormatEmail(obj);
}

function SelectBox(checkbox)
{
	box = eval(checkbox);
	box.checked = !box.checked;
}

function PopUp(page, height, width)
{
	popup = window.open(page, "popup",
		"height=" + height + ",width=" + width + ",scrollbars=yes,left=1,top=1");
	return false;
}

function doClear(theText)
{
    if (theText.value == theText.defaultValue)
        theText.value = ""
}
