function emailcheck(str) {
	var at = "@";
	var dot = ".";
	var lat = str.indexOf(at);
	var lstr = str.length;
	var ldot = str.indexOf(dot);
	
	if (str.indexOf(at) == -1){
	   alert("You have entered an invalid E-mail address.");
	   return false;
	}

	if (str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr) {
	   alert("You have entered an invalid E-mail address.");
	   return false;
	}

	if (str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr) {
		alert("You have entered an invalid E-mail address.");
		return false;
	}

	 if (str.indexOf(at,(lat+1)) != -1) {
		alert("You have entered an invalid E-mail address.");
		return false;
	 }

	 if (str.substring(lat-1,lat) == dot || str.substring(lat+1,lat+2) == dot) {
		alert("You have entered an invalid E-mail address.");
		return false;
	 }

	 if (str.indexOf(dot,(lat+2)) == -1) {
		alert("You have entered an invalid E-mail address.");
		return false;
	 }
	
	 if (str.indexOf(" ") != -1) {
		alert("You have entered an invalid E-mail address.");
		return false;
	 }

	 return true;					
}

function NoneWithCheck(ss) {
for(var i = 0; i < ss.length; i++) {
	if(ss[i].checked) { return false; }
	}
return true;
}

function validateMailingListFormFields() {
	var FirstName = document.theForm.FirstName;
	var LastName = document.theForm.LastName;
	var EmailAddress = document.theForm.EmailAddress;
	var confirmEmailAddress = document.theForm.confirmemail;
	var MailingListLength = document.theForm.MailingList.length;
	var MailingList = "";
		
	var msg = "Please complete the following fields before submitting this form: \n";
	var error = 0;
	
	for (i=0; i < MailingListLength; i++)
	{
		if (document.theForm.MailingList[i].checked) {
			MailingList = document.theForm.MailingList[i].value;
		}
	}
	
	if (FirstName.value == "") {
		msg = msg + "First Name. \n";
		error = 1;
	}
	
	if (LastName.value == "") {
		msg = msg + "Last Name. \n";
		error = 1;
	}
	
	if (EmailAddress.value != "") {
		if (emailcheck(EmailAddress.value) == false) {
			EmailAddress.value = "";
			EmailAddress.focus();
			return false;
		}
		if (EmailAddress.value != confirmEmailAddress.value) {
			alert("Your E-mail address and confirm E-mail address does not match. Please doublecheck your entry.");
			confirmEmailAddress.value = "";
			confirmEmailAddress.focus();
			return false;
		}
	}
	else {
		msg = msg + "You must enter a valid email address. \n";
		error = 1;
	}
		
if(NoneWithCheck(document.theForm.MailingList))
	{ msg = msg + "Select at least 1 mailing list topic. \n";
		error = 1;}

	if (error)
	{
		alert(msg);
		return false;
	}
	
	// All tests passed and the information is submitted
	return true;
}

function validateSampleRequestFormFields() {
	var FirstName = document.theForm.FirstName;
	var LastName = document.theForm.LastName;
	var JobTitle = document.theForm.JobTitle;
	var Company = document.theForm.Company;
	var StreetAddress = document.theForm.StreetAddress;
	var City = document.theForm.City;
	var State = document.theForm.State;
	var ZipCode = document.theForm.ZipCode;
	var BusPhone = document.theForm.BusPhone;
	var EmailAddress = document.theForm.EmailAddress;
	var confirmEmailAddress = document.theForm.confirmemail;
	var sampleLength = document.theForm.sample.length;
	var sample = "";
		
	var msg = "Please complete the following fields before submitting this form: \n";
	var error = 0;
	
	for (i=0; i < sampleLength; i++)
	{
		if (document.theForm.sample[i].checked) {
			sample = document.theForm.sample[i].value;
		}
	}
	
	if (FirstName.value == "") {
		msg = msg + "First Name. \n";
		error = 1;
	}
	
	if (LastName.value == "") {
		msg = msg + "Last Name. \n";
		error = 1;
	}
	
	if (JobTitle.value == "") {
		msg = msg + "Position. \n";
		error = 1;
	}
	
	if (Company.value == "") {
		msg = msg + "Company. \n";
		error = 1;
	}
	
	if (StreetAddress.value == "") {
		msg = msg + "StreetAddress. \n";
		error = 1;
	}
	
	if (City.value == "") {
		msg = msg + "City. \n";
		error = 1;
	}
	
	if (State.value == "") {
		msg = msg + "State. \n";
		error = 1;
	}
	
	if (ZipCode.value == "") {
		msg = msg + "ZipCode. \n";
		error = 1;
	}
	
	if (BusPhone.value == "") {
		msg = msg + "Business Phone Number. \n";
		error = 1;
	}
	
	if (EmailAddress.value != "") {
		if (emailcheck(EmailAddress.value) == false) {
			EmailAddress.value = "";
			EmailAddress.focus();
			return false;
		}
		if (EmailAddress.value != confirmEmailAddress.value) {
			alert("Your E-mail address and confirm E-mail address does not match. Please doublecheck your entry.");
			confirmEmailAddress.value = "";
			confirmEmailAddress.focus();
			return false;
		}
	}
	else {
		msg = msg + "You must enter a valid email address. \n";
		error = 1;
	}
		
if(NoneWithCheck(document.theForm.sample))
	{ msg = msg + "Select at least 1 sample. \n";
		error = 1;}

	if (error)
	{
		alert(msg);
		return false;
	}
	
	// All tests passed and the information is submitted
	return true;
}

function validateContactFormFields() {
	var FirstName = document.theForm.Contact_FirstName;
	var LastName = document.theForm.Contact_LastName;
	var EmailAddress = document.theForm.Contact_Email;
	var message = document.theForm.message;

	var msg = "Please complete the following fields before submitting this form: \n";
	var error = 0;
	
	if (FirstName.value == "") {
		msg = msg + "First Name. \n";
		error = 1;
	}
	
	if (LastName.value == "") {
		msg = msg + "Last Name. \n";
		error = 1;
	}
	
	if (EmailAddress.value == "") {
		msg = msg + "Email Address. \n";
		error = 1;
	}
	
	if (message.value == "") {
		msg = msg + "Email Message. \n";
		error = 1;
	}
	
	if (error)
	{
		alert(msg);
		return false;
	}
	
	// All tests passed and the information is submitted
	return true;
}
