
function Validate_contact(contact_form) 
{
    var err = "";
    err += check_fname(trim(contact_form.f1rst.value), true);
    err += check_lname(trim(contact_form.lastnam3.value), true);
    err += check_email(trim(contact_form._3mail.value), true);
    err += check_address(trim(contact_form.address.value), true);
    err += check_city(trim(contact_form.city.value), true);
    err += check_state(trim(contact_form.state.value), true);
    err += check_zip(trim(contact_form.zip.value), true);
    phone = contact_form.phone1.value + contact_form.phone2.value + contact_form.phone3.value + contact_form.phone4.value;
    err += check_phone(trim(phone), false);
    err += check_msg(trim(contact_form.c0mm3nts.value), false);

    if (err != "") {
    	var error_msg = "The following fields had errors, please change them and submit the form again.\n\n";
        alert(error_msg + err);
        return false;
    }
	return true;
}

function check_fname (str, bReq) {
	var error = "";
	if (str == "" && bReq) {
    	error = "First Name: can't be blank.\n";
    }
    else if (!isAlpha(str))
    {
    	error = "First Name: You can only enter letters for this field.\n";
    }
    return error;
}
function check_lname (str, bReq) {
	var error = "";
	if (str == "" && bReq) {
    	error = "Last Name: can't be blank.\n";
    }
    else if (!isAlpha(str))
    {
    	error = "Last Name: You can only enter letters for this field.\n";
    }
    return error;
}
function check_address (str, bReq) {
	var error = "";
	if (str == "" && bReq) {
    	error = "Address: can't be blank.\n";
    }
 
    return error;
}
function check_city (str, bReq) {
	var error = "";
	if (str == "" && bReq) {
    	error = "City: can't be blank.\n";
    }
   
    return error;
}
function check_state (str, bReq) {
	var error = "";
	if (str == "" && bReq) {
    	error = "State: can't be blank.\n";
    }
    else if (!isAlpha(str))
    {
    	error = "State: You can only enter letters for this field.\n";
    }
    return error;
}
function check_zip (str, bReq) {
	var error = "";
	if (str == "" && bReq) {
    	error = "Zip: can't be blank.\n";
    }
    
    return error;
}
function check_phone (str, bReq) {
	var error = "";
	if (str == "") {
    	//do nothing,phone not required
    }
    else if (!isNum(str))
    {
    	error = "Phone: You can only enter numbers for this field.\n";
    }
    return error;
}
function check_email (str, bReq) {

 	var error = "";
 	if (str == "" && bReq) {
    	error = "Email: can't be blank. \n";
    }
    else if (!isEmail(str) && str != "")
    {
    	error = "Email: You didn't enter a valid email address.\n";
    }
    return error;
}
function check_msg (str, bReq) {
 	var error = "";

    return error;
}


// check to see if input is alphanumeric
function isAlphaNumeric(str)
{
	if (str.match(/^[a-zA-Z0-9 \-.,]+$/))
	{
		return true;
	}
	else
	{
		return false;
	} 
}
// check to see if input is alpha
function isAlpha(str)
{
	if (str.match(/^[a-zA-Z \-.,]+$/))
	{
		return true;
	}
	else
	{
		return false;
	} 
}
// check to see if input is numeric
function isNum(str)
{
	if (str.match(/^[0-9]+$/))
	{
		return true;
	}
	else
	{
		return false;
	} 
}
// check to see if input is a zip code
function isZip(str)
{
	if (str.match(/^[0-9]+$/) && (str.length == 5 || str.length == 9))
	{
		return true;
	}
	else
	{
		return false;
	} 
}
function isEmail(str)
{
	validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
	var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/
	
   	// search email text for regular exp matches
   	if (str.search(validRegExp) == -1) 
   	{	 
   		return false;
    }
	if (str.match(illegalChars)) {
		return false;
	}
	
	return true;
}
function trim(s)
{
	return rtrim(ltrim(s));
}

function ltrim(s)
{
	var l=0;
	while(l < s.length && s[l] == ' ')
	{	l++; }
	return s.substring(l, s.length);
}

function rtrim(s)
{
	var r=s.length -1;
	while(r > 0 && s[r] == ' ')
	{	r-=1;	}
	return s.substring(0, r+1);
}
function autoTab(element, nextElement) 
{
    if (element.value.length == element.maxLength && nextElement != null) {
        element.form.elements[nextElement].focus();
    }
}
//-->

