function trim_str(str)
{
	return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}
function trim_field(item)
{
	item.value = trim_str(item.value);
}
function trim_all_input_fields(form)
{
	var boxes = form.getElementsByTagName('input');
	for (var i = 0; i < boxes.length; ++i) {
		trim_field(boxes[i]);
	}
}
function set_focus(item)
{
	if (typeof item != 'undefined' && item != null) {
		item.focus();
		if (item.tagName == 'INPUT')
			item.select();
	}
}
var lastBadField = null;
function set_bad_field(item, errMsg)
{
	//report_error(errMsg);
	item.style.color = '#ffffff';
	item.style.backgroundColor = '#770000';
	item.style.borderColor = '#770000';
	set_focus(item);
	lastBadField = item;
}
function clear_bad_field()
{
	if (lastBadField != null) {
		lastBadField.style.color = '#000000';
		lastBadField.style.backgroundColor = '#ffffff';
		lastBadField.style.borderColor = '#000000';
		lastBadField = null;
	}
}
function check_non_null(item)
{
	// if this form doesn't have this field, we skip it
	if (typeof item == 'undefined' || item == null) return true;
	var isOK = item.value != '';
	if (!isOK) {
		set_bad_field(item, 'Field cannot be empty');
	}
	return isOK;
}
function check_length(item, reqLength, emptyIsOK)
{
	// if this form doesn't have this field, we skip it
	if (typeof item == 'undefined' || item == null) return true;
	var length = item.value.length;
	var isOK = (emptyIsOK && length == 0) || (length >= reqLength);
	if (!isOK) {
		set_bad_field(item, 'Field value is too short');
	}
	return isOK;
}
function check_email_format(item, emptyIsOK)
{
	// if this form doesn't have this field, we skip it
	if (typeof item == 'undefined' || item == null) return true;
	var value = item.value;
	var pos = value.indexOf('@');
	var isOK = (emptyIsOK && value.length == 0) || (pos >= 1 && pos < value.length - 1);
	if (!isOK) {
		set_bad_field(item, 'Field is not a valid email address');
	}
	return isOK;
}
function check_forbidden_char(item, ch)
{
	// if this form doesn't have this field, we skip it
	if (typeof item == 'undefined' || item == null) return true;
	var isOK = (item.value.indexOf(ch) == -1);
	if (!isOK) {
		set_bad_field(item, 'Field has invalid character(s)');
	}
	return isOK;
}
function check_radio(list, label)
{
	for (idx = 0; idx < list.length; ++idx) {
		if (list[idx].checked)
			return true;
	}
	set_bad_field(label, "A selection is required");
	return false;
}
function validate_rq(form)
{
	clear_bad_field();
	trim_all_input_fields(form);
	if (!check_non_null(form.txt_fn)) return false;
	if (!check_non_null(form.txt_ln)) return false;
	if (!check_non_null(form.txt_org)) return false;
	if (!check_non_null(form.txt_city)) return false;
	if (!check_length(form.txt_state, 2, false)) return false;
	if (!check_length(form.txt_zip, 5, false)) return false;
	if (!check_length(form.txt_email, 3, false)) return false;
	if (!check_email_format(form.txt_email, false)) return false;
	if (!check_forbidden_char(form.txt_email, ' ')) return false;
	if (!check_length(form.txt_phone, 10, false)) return false;
	//if (!check_radio(form.rb_pcs, document.getElementById('lbl_pcs'))) return false;
	//if (!check_radio(form.rb_purchase, document.getElementById('lbl_purchase'))) return false;
	return true;
}
