function validate_required(field,alerttxt)
{
	//alert(field.value);
	with (field)
	{
		if (value==null||value=="")
  		{
  			alert(alerttxt);
  			return false;
  		}
		else
		{
			return true;
		}
	}
}

function validate_US_Zip( strValue ) {
	var objRegExp  = /(^\d{5}$)/;
	return objRegExp.test(strValue);
}

function validate_phone( strValue ) {
	var objRegExp  = /(^\d{3}-\d{3}-\d{4}$)/;
	return objRegExp.test(strValue);
}

function is_homeowner() {
	//alert(document.forms[0].own_home[0].checked);
	if (document.forms[0].own_home[0].checked)
		return true;
	else
		return false;
}

function validate_brickface_zip( strValue ) {
	return (in_array(strValue, valid_zips));
}

function in_array( strValue, search_array)
{
	for (i=0; i<search_array.length; i++)
	{
		if (strValue == search_array[i])
			return true;
	}
	return false;
}
function validate_required_radio(field,alerttxt)
{
	var checked = false;
	with (field)
	{
		for (i=0;i<field.length;i++)
		{
			if (field[i].checked)
			{
				checked = true;
				return true;
			}
		}
	}
	if (!checked)
	{
		alert(alerttxt);
		return false;
	}
	return true;
}

function validate_required_cb(field,alerttxt,howmany)
{
	var check_count = 0;
	with (field)
	{
		for (i=0;i<field.length;i++)
		{
			if (field[i].checked)
			{
				//alert(field[i].value);
				check_count++;
			}
		}
	}
	//alert(check_count + '<' + howmany);
	if (check_count < howmany)
	{
		alert(alerttxt);
		return false;
	}
	return true;
}

function validate_step1(thisform)
{
	with (thisform)
	{

//alert(educationlevel.value);

		if (validate_required_cb(interests,"Sorry, please select at least 1 product of interest.",1)==false)
  		{
  			interests[0].focus();
  			return false;
  		}

		if (validate_required_radio(own_home,"Sorry, please select if you own a home.")==false)
  		{
  			own_home[0].focus();
  			return false;
  		}
  		if (!is_homeowner())
  		{
  			alert('We\'re Sorry. Currently, we cannot accommodate requests from non-homeowners.');
  			 own_home[0].focus();
  			return false;
  		}
		if (validate_required(zip,"Sorry, please enter a valid zip code.")==false)
  		{
  			zip.focus();
  			return false;
  		}
  		if (!validate_US_Zip(zip.value))
  		{
  			alert('Sorry, that does not appear to be a valid zip code');
  			 zip.focus();
  			return false;
  		}
  		if (!validate_brickface_zip(zip.value))
  		{
  			alert('We\'re Sorry. Currently, we cannot accommodate requests from homeowners who live outside our licensed geographical areas.');
  			 zip.focus();
  			return false;
  		}

	}
}

function validate_step2(thisform)
{
	with (thisform)
	{
		if (validate_required(firstname,"Sorry, please enter a valid first name.")==false)
  		{
  			firstname.focus();
  			return false;
  		}
		if (validate_required(lastname,"Sorry, please enter a valid last name.")==false)
  		{
  			lastname.focus();
  			return false;
  		}
		if (validate_required(address,"Sorry, please enter a valid address.")==false)
  		{
  			address.focus();
  			return false;
  		}
  		if (validate_required(city,"Sorry, please enter a valid city.")==false)
  		{
  			city.focus();
  			return false;
  		}
		if (validate_required(state,"Sorry, select a valid state.")==false)
		{
  			state.focus();
			return false;
  		}
		var phone = phone1.value + '-' + phone2.value + '-' + phone3.value;

  		if (!validate_phone(phone))
  		{
  			alert("Sorry, please enter a valid phone number.");
  			phone1.focus();
  			return false;
  		}
		if (validate_required(email,"Sorry, please enter a valid email.")==false)
  		{
  			email.focus();
  			return false;
  		}
  		var EMAIL = "^[a-zA-Z0-9_-]+(\.([a-zA-Z0-9_-])+)*@[a-zA-Z0-9_-]+[.][a-zA-Z0-9_-]+([.][a-zA-Z0-9_-]+)*$";
  		var re = new RegExp(EMAIL);

  		if (!email.value.match(re))
		{
			alert('Sorry, that does not appear to be a valid email.');
  			email.focus();
  			return false;

		}
		if (validate_required_radio(best_time,"Sorry, please let us know when is the best time to call you.")==false)
  		{
  			best_time[0].focus();
  			return false;
  		}

	}
}

var isNN = (navigator.appName.indexOf("Netscape")!=-1);
function autoTab(input,len, e) {
var keyCode = (isNN) ? e.which : e.keyCode;
var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
if(input.value.length >= len && !containsElement(filter,keyCode)) {
input.value = input.value.slice(0, len);
input.form[(getIndex(input)+1) % input.form.length].focus();
}
function containsElement(arr, ele) {
var found = false, index = 0;
while(!found && index < arr.length)
if(arr[index] == ele)
found = true;
else
index++;
return found;
}
function getIndex(input) {
var index = -1, i = 0, found = false;
while (i < input.form.length && index == -1)
if (input.form[i] == input)index = i;
else i++;
return index;
}
return true;
}


