//-------------------------------
//-------------------------------
//-- Javascript Form Validator
//-------------------------------
//-------------------------------
//
// Written by Art Kay
// Last update: 6/7/2008
// NO Reuse or Modification without expressed written consent.
// art@akawebdesign.com
//
//-------------------------------


/* --------------------------------------------------------
           FUNCTIONS

Individual Form Validators

  checkFirstname()
  checkLastname()
  checkPhone()
  checkCompany()
  checkTitle()
  checkEmail()
  checkWebsite()
  checkFind()

Page Validation Methods

  validateEmailList()
  validateContactForm()
  validateAdvertiseForm()
  validateFeedbackForm()

           DIRECTIONS

In order to use the Page Validation Methods in this file,
you need to:

(1) Include this JS file in your page <head>

(2) Use the following attribute in your <form> tag

      onsubmit="return validateContactForm(this);"

---------------------------------------------------------- */



//----------------------
//To prevent against
//SPAM from Viagra, tramadol
//Cialis, and Phentermine
//----------------------


function viagra(spam)
{
	var returnValue = true;
	

	//------------------------------
	//check for Viagra submissions
	//------------------------------
	for (var i=0; i < spam.length; i++)
	{
		var b = i;
		if (spam.charAt(b) == "v" || spam.charAt(b) == "V")
		{
			b++;
			if (spam.charAt(b) == "i" || spam.charAt(b) == "I")
			{
				b++;
				if (spam.charAt(b) == "a" || spam.charAt(b) == "A")
				{
					b++;
					if (spam.charAt(b) == "g" || spam.charAt(b) == "G")
					{
						b++;
						if (spam.charAt(b) == "r" || spam.charAt(b) == "R")
						{
							b++;
							if (spam.charAt(b) == "a" || spam.charAt(b) == "A")
							{
										returnValue = false;
										
										while (returnValue == false)
										{
											alert("Stop Spamming!!!");
										}
							}
						}
					}
				}
			}
		}
			
	
	}
	
	return returnValue;

}



function cialis(spam)
{

	var returnValue = true;

	//------------------------------
	//check for Cialis submissions
	//------------------------------
	for (var a=0; a < spam.length; a++)
	{
		var b = a;
		if (spam.charAt(b) == "c" || spam.charAt(b) == "C")
		{
			b++;
			if (spam.charAt(b) == "i" || spam.charAt(b) == "I")
			{
				b++;
				if (spam.charAt(b) == "a" || spam.charAt(b) == "A")
				{
					b++;
					if (spam.charAt(b) == "l" || spam.charAt(b) == "L")
					{
						b++;
						if (spam.charAt(b) == "i" || spam.charAt(b) == "I")
						{
							b++;
							if (spam.charAt(b) == "s" || spam.charAt(b) == "S")
							{
										returnValue = false;
										
										while (returnValue == false)
										{
											alert("Stop Spamming!!!");
										}
							}
						}
					}
				}
			}
		}
			
	
	}

	return returnValue;
}





function spamCheck(form)
{
	var returnValue = true;
	
	var spam = form.Firstname.value;
	var spamm = form.Lastname.value;
	
	if (!viagra(spam))
	{
		returnValue = false;
		return returnValue;
	}
	
	if (!cialis(spam))
	{
		returnValue = false;
		return returnValue;
	}	

	if (!viagra(spamm))
	{
		returnValue = false;
		return returnValue;
	}
	
	if (!cialis(spamm))
	{
		returnValue = false;
		return returnValue;
	}	



// Don't allow firstname to be the same as last name

	if (form.Firstname.value == form.Lastname.value)
	{
		returnValue = false;

		while (returnValue == false)
		{
			alert("Stop Spamming!");
		}
		form.Firstname.value = "";
		form.Firstname.focus();
		return returnValue;

	}

	if (form.Firstname.value == "phentermine")
	{
		returnValue = false;

		while (returnValue == false)
		{
			alert("Stop Spamming!");
		}
		form.Firstname.value = "";
		form.Firstname.focus();
		return returnValue;

	}

	if (form.Firstname.value == "Dawin")
	{
		returnValue = false;

		while (returnValue == false)
		{
			alert("Stop Spamming!");
		}
		form.Firstname.value = "";
		form.Firstname.focus();
		return returnValue;

	}

	if (form.Firstname.value == "tramadol")
	{
		returnValue = false;

		while (returnValue == false)
		{
			alert("Stop Spamming!");
		}
		form.Firstname.value = "";
		form.Firstname.focus();
		return returnValue;

	}

	if (form.Lastname.value == "Dawin")
	{
		returnValue = false;

		while (returnValue == false)
		{
			alert("Stop Spamming!");
		}
		form.Lastname.value = "";
		form.Lastname.focus();
		return returnValue;

	}

	if (form.Lastname.value == "phentermine")
	{
		returnValue = false;

		while (returnValue == false)
		{
			alert("Stop Spamming!");
		}
		form.Lastname.value = "";
		form.Lastname.focus();
		return returnValue;

	}

	if (form.Lastname.value == "tramadol")
	{
		returnValue = false;

		while (returnValue == false)
		{
			alert("Stop Spamming!");
		}
		form.Lastname.value = "";
		form.Lastname.focus();
		return returnValue;

	}
	
	
	return returnValue;

}
//------------------------
//End Spam Protection
//------------------------


//----------------------------
//----------------------------
// Individual Field Validators
//----------------------------
//----------------------------


function checkFirstname(form)
{

	var returnValue = true;

	if (form.Firstname.value == "")
	{
		returnValue = false;
		alert("First Name field is blank!");	
		form.Firstname.focus();
		return returnValue;

	}
	
	else
		return returnValue;

}

function checkLastname(form)
{

	var returnValue = true;

	if (form.Lastname.value == "")
	{
		returnValue = false;
		alert("Last Name field is blank!");	
		form.Lastname.focus();
		return returnValue;

	}
	
	else
		return returnValue;

}

function checkPhone(form)
{

	var returnValue = true;

	if (form.Phone.value == "")
	{
		returnValue = false;
		alert("Phone Number field is blank!");
		form.Phone.focus();
		return returnValue;
	}
	
	else
		return returnValue;
}

function checkCompany(form)
{

	var returnValue = true;

	if (form.Company.value == "")
	{
		returnValue = false;
		alert("Company field is blank!");	
		form.Company.focus();
		return returnValue;

	}
	
	else
		return returnValue;

}

function checkTitle(form)
{

	var returnValue = true;

	if (form.Title.value == "")
	{
		returnValue = false;
		alert("Title field is blank!");	
		form.Title.focus();
		return returnValue;

	}
	
	else
		return returnValue;

}

function checkEmail(form)
{

	var returnValue = true;

	// check if field is blank
	if (form.Email.value == "")
	{
		returnValue = false;
		alert("Email field is blank!");	
		form.Email.focus();
		return returnValue;

	}
	
	// there must be >= 1 character before @, so we
	// start looking at character position 1 
	// (i.e. second character)
	var i = 1;
	var email = form.Email.value;
	var sLength = email.length;

	// look for @
	while ((i < sLength) && (email.charAt(i) != "@"))
	{
	  i++;
	}

	if ((i >= sLength) || (email.charAt(i) != "@"))
	{
		returnValue = false;
		alert("Email field is invalid!");	
		form.Email.focus();
		return returnValue;
	}
	
	else
		i += 2;

	// look for .
	while ((i < sLength) && (email.charAt(i) != "."))
	{ 
	  i++;
	}

	// there must be at least one character after the .
	if ((i >= sLength - 1) || (email.charAt(i) != "."))
	{
		returnValue = false;
		alert("Email field is invalid!");	
		form.Email.focus();
		return returnValue;
	}


	else
		return returnValue;

}

function checkWebsite(form)
{

	var returnValue = true;

	if (form.Website.value == "")
	{
		returnValue = false;
		alert("Website field is blank!");	
		form.Website.focus();
		return returnValue;

	}
	
	else
		return returnValue;

}

function checkFind(form)
{

	var returnValue = true;

	if (form.Find.value == 0)
	{
		returnValue = false;
		alert("How did you find us?");
		form.Find.focus();
		return returnValue;
	}
	
	if (form.Find.value == "Other")
	{
		if (form.Other.value == "")
		{
			returnValue = false;
			alert("How did you find us?");
			form.Other.focus();
			return returnValue;
		}
		else
			return returnValue;
		
	}
	
	else
		return returnValue;

}




//----------------------------------
//----------------------------------
// Validation for Email List Form
//----------------------------------
//----------------------------------


function validateEmailList(form)
{	
	var returnValue = true;

	if (!checkEmail(form))
	{
		returnValue = false;
		return returnValue;
	}

	else
		return returnValue;
}

//----------------------------------
//----------------------------------
// Validation for Contact Us Form
//----------------------------------
//----------------------------------


function validateContactForm(form)
{	
	var returnValue = true;

	if (!checkFirstname(form))
	{
		returnValue = false;
		return returnValue;
	}

	if (!checkLastname(form))
	{
		returnValue = false;
		return returnValue;
	}

	if (!checkEmail(form))
	{
		returnValue = false;
		return returnValue;
	}

	if (!checkPhone(form))
	{
		returnValue = false;
		return returnValue;
	}

	else
		return returnValue;
}



//----------------------------------
//----------------------------------
// Validation Advertising Form
//----------------------------------
//----------------------------------


function validateAdvertiseForm(form)
{	
	var returnValue = true;

	if (!checkFirstname(form))
	{
		returnValue = false;
		return returnValue;
	}

	if (!checkLastname(form))
	{
		returnValue = false;
		return returnValue;
	}

	if (!checkCompany(form))
	{
		returnValue = false;
		return returnValue;
	}	

	if (!checkEmail(form))
	{
		returnValue = false;
		return returnValue;
	}

	if (!checkPhone(form))
	{
		returnValue = false;
		return returnValue;
	}

	else
		return returnValue;
}


//----------------------------------
//----------------------------------
// Validation for Feedback Form
//----------------------------------
//----------------------------------


function validateFeedbackForm(form)
{	
	var returnValue = true;

	if (!checkFirstname(form))
	{
		returnValue = false;
		return returnValue;
	}

	if (!checkLastname(form))
	{
		returnValue = false;
		return returnValue;
	}

	if (!checkEmail(form))
	{
		returnValue = false;
		return returnValue;
	}

	if (!checkPhone(form))
	{
		returnValue = false;
		return returnValue;
	}

	else
		return returnValue;
}
