function empty_field(field, msg)
{
	var not_empty_rx = /\S/;
	var field_str = field.value.toString();

	if (!field_str.match(not_empty_rx))
	{
		fix_element(field, msg);
		return true;
	}

	if (field.value == "")
	{
		fix_element(field, msg);
		return true;
	}
	else
	{
		return false;
	}
}

function fix_element(element, msg)
{
	alert(msg);
	element.focus();
}

function trim_string(str)
{
	var s_input = new String(str);
	var i_start, i_end;
	var s_trimmed;
	var c_char;

	i_end = s_input.length - 1;
	i_start = 0;
	
	c_char = s_input.charAt(i_start);
	while ((i_start < i_end) && ((c_char == "\n") || (c_char == "\r") || (c_char == "\t") || (c_char == " ")))
	{
		i_start++;
		c_char = s_input.charAt(i_start);
	}

	c_char = s_input.charAt(i_end);
	while ((i_end >= 0) && ((c_char == "\n") || (c_char == "\r") || (c_char == "\t") || (c_char == " ")))
	{
		i_end--;
		c_char = s_input.charAt(i_end);
	}

	s_trimmed = (i_start <= i_end ? s_input.substring(i_start,i_end+1) : '');
	return s_trimmed;
}

function check_email(o_email)
{
	var s_trimmed_email = new String(trim_string(o_email.value));
	
	var rx_email = /^(.+)@(.+)$/;
	var rx_special_chars = "\\(\\)<>@,$/;:\\\\\\\"\\.\\[\\]";
	var rx_valid_chars = "\[^\\s" + rx_special_chars + "\]";
	var rx_quoted_user = "(\"[^\"]*\")";
	var rx_ip_domain = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var rx_atom = rx_valid_chars + '+';
	var rx_word = "(" + rx_atom + "|" + rx_quoted_user + ")";
	var rx_user = new RegExp("^" + rx_word + "(\\." + rx_word + ")*$");
	var rx_domain = new RegExp("^" + rx_atom + "(\\." + rx_atom +")*$");

	var a_match = s_trimmed_email.match(rx_email);

	if (a_match == null)
	{
		fix_element(o_email, "E-mail address seems to be incorrect (check @ and .'s)");
		return false;
	}

	var s_user = a_match[1];
	var s_domain = a_match[2];

	if (s_user.match(rx_user) == null)
	{
		fix_element(o_email, "Your e-mail username doesn't seem to be valid.");
		return false;
	}

	var a_IP = s_domain.match(rx_ip_domain);
	if (a_IP != null)
	{
		for (var i =1; i <= 4; i++)
		{
			if (a_IP[i] > 255)
			{
				fix_element(o_email, "Your e-mail Destination IP address is invalid!");
				return false;
			}
		}
		return true;
	}

	var a_domain = s_domain.match(rx_domain)
	if (a_domain == null)
	{
		fix_element(o_email, "The domain name in your e-mail address doesn't seem to be valid.");
		return false;
	}

	var rx_atoms = new RegExp(rx_atom,"g");
	var a_dom = s_domain.match(rx_atoms);
	var i_len = a_dom.length;
	if (a_dom[a_dom.length - 1].length < 2 || a_dom[a_dom.length - 1].length > 3)
	{
		fix_element(o_email, "The e-mail address must end in a three-letter domain, or two letter country.");
		return false;
	}

	if (i_len < 2)
	{
		fix_element(o_email, "This e-mail address is missing a hostname!");
		return false;
	}

	o_email.value = s_trimmed_email;
	return true;
}

function default_selected(selbox)
{
	return !selbox.selectedIndex;
}

function validate(form)
{
	with (form)
	{
		name.value	= trim_string(name.value);
		email.value	= trim_string(email.value);
		address.value	= trim_string(address.value);
		city.value	= trim_string(city.value);
		areacode.value	= trim_string(areacode.value);
		phone.value	= trim_string(phone.value);
		work_areacode.value	= trim_string(work_areacode.value);
		work_phone.value	= trim_string(work_phone.value);
		zipcode.value	= trim_string(zipcode.value);
		property_location.value = trim_string(property_location.value);
		property_descr.value = trim_string(property_descr.value);
		
		if (empty_field(name, "Please enter your Full Name.")) { return false; }
		if (empty_field(email, "Please enter your E-mail Address.")) { return false; }
		if (empty_field(address, "Please enter your Address.")) { return false; }
		if (empty_field(city, "Please enter your City of residence.")) { return false; }
		if (default_selected(state)) { fix_element(state, "Please select your state of residence."); return false; }
		if (empty_field(zipcode, "Please enter your Zip Code.")) { return false; }
		if (empty_field(areacode, "Please enter a value for the Area code field.")) { return false; }
		if (empty_field(phone, "Please enter a value for the Phone field.")) { return false; }
		if (default_selected(howdidyouhearaboutus)) { fix_element(howdidyouhearaboutus, "Please choose an option for:\nHow did you hear about us?"); return false; }
		if (empty_field(property_location)) { fix_element(property_location, "Please enter the address of the property."); return false; }
		if (empty_field(property_descr)) { fix_element(property_descr, "Please enter a description of the property."); return false; }

		return check_email(email);
	}
}
