function Validate_contact(contact_form) 
{
    var err = "";
    err += check_name(trim(contact_form.j_name.value), true);
    err += check_email(trim(contact_form.j_email.value), true);
    err += check_company(trim(contact_form.j_company.value), true);
    err += check_addy(trim(contact_form.j_address.value), false);
    err += check_city(trim(contact_form.j_city.value), false);
    err += check_state(contact_form.j_state.selectedIndex, false);
    err += check_zip(trim(contact_form.j_zip.value), false);
    err += check_country(trim(contact_form.j_country.value), false);
    phone = contact_form.j_phone1.value + contact_form.j_phone2.value + contact_form.j_phone3.value;
    err += check_phone(trim(phone), true);
    err += check_msg(trim(contact_form.j_msg.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 Validate_fn(food_nutrition) 
{
    var err = "";
    err += check_name(trim(food_nutrition.Name.value), true);
    err += check_email(trim(food_nutrition.Email.value), true);
    err += check_title(trim(food_nutrition.Title1.value), false);
    err += check_company(trim(food_nutrition.Company.value), true);
    err += check_addy(trim(food_nutrition.Address.value), true);
    err += check_city(food_nutrition.City.value, true);
    err += check_state_txt(trim(food_nutrition.State.value), true);
    err += check_zip(trim(food_nutrition.Zip.value), true);
    err += check_country(trim(food_nutrition.Country.value), true);
    err += check_jar_phone(trim(food_nutrition.Phone.value), true);
    err += check_jar_fax(trim(food_nutrition.Fax.value), false);
	err += check_msg(trim(food_nutrition.Literature.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 Validate_cosmetic(cosmetic) 
{
    var err = "";
    err += check_name(trim(cosmetic.Name.value), true);
    err += check_email(trim(cosmetic.Email.value), true);
    err += check_title(trim(cosmetic.Title1.value), false);
    err += check_company(trim(cosmetic.Company.value), true);
    err += check_addy(trim(cosmetic.Address.value), true);
    err += check_city(cosmetic.City.value, true);
    err += check_state_txt(trim(cosmetic.State.value), true);
    err += check_zip(trim(cosmetic.Zip.value), true);
    err += check_country(trim(cosmetic.Country.value), true);
    err += check_jar_phone(trim(cosmetic.Phone.value), true);
    err += check_jar_fax(trim(cosmetic.Fax.value), false);
	err += check_msg(trim(cosmetic.Literature.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_name (str, bReq) {
	var error = "";
	if (str == "" && bReq) {
    	error = "Your Name: can't be blank.\n";
    }
    else if (!isAlpha(str))
    {
    	error = "Your Name: You can only enter letters for this field.\n";
    }
    return error;
}
function check_lastname (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_title (str, bReq) {
 	var error = "";

    if (str != "" && !isAlphaNumeric(str))
    {
    	error = "Title: You can only enter letters or numbers for this field.\n";
    }
    return error;
}
function check_company (str, bReq) {
 	var error = "";

	if (str == "" && bReq) {
    	error = "Company: can't be blank. \n";
    }
    if (str != "" && !isAlphaNumeric(str))
    {
    	error = "Company: You can only enter letters or numbers for this field.\n";
    }
    return error;
}
function check_addy (str, bReq) {
 	var error = "";

 	if (str == "" && bReq) {
    	error = "Address: can't be blank. \n";
    }
    else if (str != "" && !isAlphaNumeric(str))
    {
    	error = "Address: You can only enter letters and numbers for this field.\n";
    }
    return error;
}
function check_city (str, bReq) {
 	var error = "";

    if (str == "" && bReq) {
    	error = "City: can't be blank. \n";
    }
    else if (str != "" && !isAlpha(str))
    {
    	error = "City: You can only enter letters for this field.\n";
    }
    return error;
}
function check_country (str, bReq) {
 	var error = "";

    if (str == "" && bReq) {
    	error = "Country: can't be blank. \n";
    }
    else if (str != "" && !isAlpha(str))
    {
    	error = "Country: You can only enter letters for this field.\n";
    }
    return error;
}
function check_state(choice, bReq) {
    var error = "";
    if (choice == 0 && bReq) {
       error = "State: None selected.\n";
    }    
	return error;
}
function check_state_txt(str, bReq) {
    var error = "";

    if (str == "" && bReq) {
    	error = "State: can't be blank. \n";
    }
    else if (str != "" && !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";
    }
    else if (str != "" && !isZip(str))
    {
    	error = "Zip: You did not enter a valid zip code.\n";
    }
    return error;
}
function check_phone (str, bReq) {
 	var error = "";

    if (str == "" && bReq) {
    	error = "Phone: can't be blank. \n";
    }
    else if (str != "" && !isNum(str) || str.length < 10)
    {
    	error = "Phone: You did not enter a valid Phone number.\n";
    }
    return error;
}
function check_jar_phone (str, bReq) {
	var validPhoneChars = "0123456789()- ";
	var error = "";
	
	if (str == "" && bReq) {
    	error = "Phone: can't be blank. \n";
    }
    else 
	{
		var stripped = str.replace(/[\(\)\.\-\ ]/g, '');
		
		//strip out acceptable non-numeric characters
		if (isNaN(parseInt(stripped))) {
		   error = "Phone: Invalid phone number. Use only numbers, parentheses, dashes, and spaces.\n";
		}
		if (!(stripped.length == 10)) {
			error += "Phone: is the wrong length. Make sure you included an area code.\n";
		}
	}
    return error;
}
function check_jar_fax (str, bReq) {
	var validPhoneChars = "0123456789()- ";
	var error = "";
	
	if (str == "" && bReq) {
    	error = "Fax: can't be blank. \n";
    }
	else if (str != "")
	{
		var stripped = str.replace(/[\(\)\.\-\ ]/g, '');
		
		//strip out acceptable non-numeric characters
		if (isNaN(parseInt(stripped))) {
		   error = "Fax: Invalid fax number. Use only numbers, parentheses, dashes, and spaces.\n";
		}
		if (!(stripped.length == 10)) {
			error += "Fax: is the wrong length. Make sure you included an area code.\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;
}

function autoTab(element, nextElement) 
{
    if (element.value.length == element.maxLength && nextElement != null) {
        element.form.elements[nextElement].focus();
    }
}

// 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();
    }
}
//-->

