function ValidLength(str,number)
{
  if (str < number)
    return false;
  return true;
}

function ValidChars(CheckStr,chars)
{
  for (i = 0;  i < CheckStr.length;  i++)
  {
    ch = CheckStr.charAt(i);
    for (j = 0;  j < chars.length;  j++)
      if (ch == chars.charAt(j))
        break;
      if (j == chars.length)
        return false;
  }
  return true;
}  

function ValidString(CheckStr,RepeatNum)
{
  if (CheckStr.length >= RepeatNum)
  {
    for (k = 0; k < CheckStr.length - 2; k++)
    {
      l = k+1;
      if  (CheckStr.charAt(k) == CheckStr.charAt(l))
      {
        m = l+1;
        if (CheckStr.charAt(k) == CheckStr.charAt(m))
        {
          return false;
          break;
        }
        else
        {
          k++;
        };
      }  
    }
  }
  return true;
}

function InValidWords(CheckStr)
{
  CheckStr = CheckStr.toLowerCase();
  if (CheckStr.search(/fuck/i) != -1 || CheckStr.search(/shit/i)!= -1 || CheckStr.search(/bitch/i) != -1 || CheckStr.search(/asdf/i) != -1 || CheckStr.search(/asfd/i) != -1 || CheckStr.search(/noname/i) != -1 || CheckStr.search(/firstname/i) != -1 || CheckStr.search(/lastname/i) != -1 || CheckStr.search(/zip/i) != -1 || CheckStr.search(/zipcode/i) != -1)
    return true;
  return false;
}
  
function ValidVowel(checkStr)
{
  checkVowel = "AEIOUYaeiouy";
  for (z = 0; z < checkStr.length; z++)
  {
    for (a = 0; a < checkVowel.length; a++)
    {
      if (checkStr.charAt(z) == checkVowel.charAt(a))
      {  
        return true;
      } 
    }
  }
  return false;
}

function ValidNumber (checkStr)
{
  checkNumber = "0123456789";
  for (z = 0; z < checkStr.length; z++)
  {
    for (a = 0; a < checkNumber.length; a++)
    {
      if (checkStr.charAt(z) == checkNumber.charAt(a))
      {  
        return true;
      } 
    }
  }
  return false;
}  

function ValidNumberZip (checkStr)
{
  checkNumber = "0123456789-";
  for (z = 0; z < checkStr.length; z++)
  {
    for (a = 0; a < checkNumber.length; a++)
    {
      if (checkStr.charAt(z) == checkNumber.charAt(a))
      {  
        return true;
      } 
    }
  }
  return false;
}  

//-------------------------  MAIN PROCEDURE -----------------------------------
function Valid(theForm)
{

  var StrChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-. ";
  var ValNums = "0123456789";
  var ValNumZip = "0123456789-";

  
  
/*---------------------- CHECK FIRST NAME -----------------------------*/
  if (!ValidLength(theForm.fname.value.length, 1))
  {
    alert ("Please enter your first name!");
    theForm.fname.focus();
    return false;
  }  
  
  if (!ValidChars(theForm.fname.value, StrChars))
  {
    alert("Your first name is not valid!");
    theForm.fname.focus();
    return false;
  }
  
  if (InValidWords(theForm.fname.value))
  {
    alert("Your first name is not valid!");
    theForm.fname.focus();
    return false;
  }  
/*------------------- CHECK LAST NAME --------------------------------*/  
  if (!ValidLength(theForm.lname.value.length, 2))
  {
    alert ("Please enter your last name!");
    theForm.lname.focus();
    return false;
  }  
  
  if (!ValidChars(theForm.lname.value, StrChars))
  {
    alert("Your last name is not valid!");
    theForm.lname.focus();
    return false;
  }
  
  if (InValidWords(theForm.lname.value))
  {
    alert("Your last name is not valid!");
    theForm.lname.focus();
    return false;
  } 

/* --- check address --*/ 
 
  var ValidAddyChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 #.-/,";
  if (!ValidLength(theForm.address.value.length, 1))
  {
    alert ("Please enter your address!");
    theForm.address.focus();
    return false;
  }
  
  if (!ValidChars(theForm.address.value, ValidAddyChars))
  {
    alert("Your address is not valid!");
    theForm.address.focus();
    return false;
  }
  
  if (InValidWords(theForm.address.value))
  {
    alert("Your address is not valid!");
    theForm.address.focus();
    return false;
  } 
/*----------------- CHECK City -------------------------- */
  if (!ValidLength(theForm.city.value.length, 2))
  {
    alert ("Please enter your city!");
    theForm.city.focus();
    return false;
  }
/*--------------- CHECK State -------------------------- */
  if (theForm.state.value == '')
  {
    alert ("Please choose your state!");
    theForm.state.focus();
    return false;
  }
/*-------------- CHECK zip -------------------------- */
  if (!ValidLength(theForm.zip.value.length, 5))
  {
    alert ("Please enter your zip code!");
    theForm.zip.focus();
    return false;
  }
  if (!ValidChars(theForm.zip.value, ValNumZip))
  {
    alert("Your zipcode is not valid!");
    theForm.zip.focus();
    return false;
  }

 /*----------------- CHECK EMAIL ------------------------------------*/
  var ValidEmailChars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_@.";
  if (!ValidLength(theForm.email.value.length, 3))
  {
    alert ("Please enter your email address!");
    theForm.email.focus();
    return false;
  }

  if (!ValidChars(theForm.email.value, ValidEmailChars))
  {
    alert("Your email address is not valid!");
    theForm.email.focus();
    return false;
  }

  dot = theForm.email.value.indexOf(".");
  afterdot = theForm.email.value.charAt(dot +1);
  if (dot == -1 || afterdot == "")
  {
    alert("Please enter your valid email");
    theForm.email.focus();
    return false;
  }

  at = theForm.email.value.indexOf("@");
  afterat = theForm.email.value.charAt(at +1);
  if (at == -1 || afterat == "")
  {
    alert("Please enter your valid email");
    theForm.email.focus();
    return false;
  }

  
/*------------- CHECK phone -------------------------- */
  if (!ValidLength(theForm.phone_1.value.length, 3))
  {
    alert ("Please enter your area code!");
    theForm.phone_1.focus();
    return false;
  }
  if (!ValidChars(theForm.phone_1.value, ValNums))
  {
    alert("Your area code is not valid!");
    theForm.phone_1.focus();
    return false;
  }
   if (!ValidLength(theForm.phone_2.value.length, 3))
  {
    alert ("Please enter your phone number!");
    theForm.phone_2.focus();
    return false;
  }
  if (!ValidChars(theForm.phone_2.value, ValNums))
  {
    alert("Your phone number is not valid!");
    theForm.phone_2.focus();
    return false;
  }
   if (!ValidLength(theForm.phone_3.value.length, 3))
  {
    alert ("Please enter your phone number!");
    theForm.phone_3.focus();
    return false;
  }
  if (!ValidChars(theForm.phone_3.value, ValNums))
  {
    alert("Your phone number is not valid!");
    theForm.phone_3.focus();
    return false;
  }
/*-------------- CHECK PCT Purchased -------------------------- 
  if (!ValidLength(theForm.pct_order.value.length, 1))
  {
    alert ("Please enter the percentage of subjects that purchased photographs code!");
    theForm.pct_order.focus();
    return false;
  }
*/
  if (!ValidChars(theForm.pct_order.value, ValNums))
  {
    alert("Your entry of percentage of subjects that purchased photographs is not valid!");
    theForm.pct_order.focus();
    return false;
  }
  /*-------------- CHECK AVG Order -------------------------- 
  if (!ValidLength(theForm.avg_order.value.length, 2))
  {
    alert ("Please enter the average order amount!");
    theForm.avg_order.focus();
    return false;
  }
*/
  if (!ValidChars(theForm.avg_order.value, ValNums))
  {
    alert("Your entry of average order amount is not valid!");
    theForm.avg_order.focus();
    return false;
  }
  /*-------------- CHECK PCT Yearbooks -------------------------- 
  if (!ValidLength(theForm.pct_yearbk.value.length, 1))
  {
    alert ("Please enter the percentage of schools that you provide yearbooks for!");
    theForm.pct_yearbk.focus();
    return false;
  }
*/
  if (!ValidChars(theForm.pct_yearbk.value, ValNums))
  {
    alert("Your entry of percentage of schools that you provide yearbooks for is not valid!");
    theForm.pct_yearbk.focus();
    return false;
  }
  /*-------------- CHECK PCT Buy Yearbooks -------------------------- 
  if (!ValidLength(theForm.pct_yearsl.value.length, 1))
  {
    alert ("Please enter the percentage of subjects purchase yearbooks!");
    theForm.pct_yearsl.focus();
    return false;
  }
*/
  if (!ValidChars(theForm.pct_yearsl.value, ValNums))
  {
    alert("Your entry of percentage of subjects that purchase yearbooks for is not valid!");
    theForm.pct_yearsl.focus();
    return false;
  }
  /*-------------- CHECK AVG Order --------------------------
  if (!ValidLength(theForm.avg_yearbk.value.length, 2))
  {
    alert ("Please enter the average yearbook order amount!");
    theForm.avg_yearbk.focus();
    return false;
  }
*/
  if (!ValidChars(theForm.avg_yearbk.value, ValNums))
  {
    alert("Your entry of average yearbook order amount is not valid!");
    theForm.avg_yearbk.focus();
    return false;
  }
//-----------------------------------------------------------------------
NewWin=null;
theForm.submit();
}

