﻿function Validator(theForm)
{
 
  var radioSelected = false;
  for (i = 0;  i < theForm.book_qty.length;  i++)
  {
    if (theForm.book_qty[i].checked)
        radioSelected = true;
  }
  if (!radioSelected)
  {
    alert("Please select one of the \"Item\" options.");
    return (false);
  }
 
  if (theForm.first.value == "")
  {
    alert("Please enter a value for the \"First\" field.");
    theForm.first.focus();
    return (false);
  }

  if (theForm.last.value == "")
  {
    alert("Please enter a value for the \"Last\" field.");
    theForm.last.focus();
    return (false);
  }

  if (theForm.street1.value == "")
  {
    alert("Please enter a value for the \"Address 1\" field.");
    theForm.street1.focus();
    return (false);
  }
 
  if (theForm.city.value == "")
  {
    alert("Please enter a value for the \"City\" field.");
    theForm.city.focus();
    return (false);
  }
 
  if (theForm.state.value == "")
  {
    alert("Please enter a value for the \"State/Province\" field.");
    theForm.state.focus();
    return (false);
  }
 
  if (theForm.zip.value == "")
  {
    alert("Please enter a value for the \"Zip/Postal Code\" field.");
    theForm.zip.focus();
    return (false);
  }
 
  if (theForm.zip.value.length < 5)
  {
    alert("Please enter at least 5 characters in the \"Zip/Postal Code\" field.");
    theForm.zip.focus();
    return (false);
  }
 
  if (theForm.country.value == "")
  {
    alert("Please enter a value for the \"Country\" field.");
    theForm.country.focus();
    return (false);
  }
 
  if (theForm.phone.value == "")
  {
    alert("Please enter a value for the \"Phone Number\" field.");
    theForm.phone.focus();
    return (false);
  }
 
  if (theForm.phone.value.length < 10)
  {
    alert("Please enter at least 10 characters in the \"Phone Number\" field.");
    theForm.phone.focus();
    return (false);
  }
 
  var checkOK = "0123456789--()";
  var checkStr = theForm.phone.value;
  var allValid = true;
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
  }
  if (!allValid)
  {
    alert("Please enter only digit and \"-()\" characters in the \"Phone Number\" field.");
    theForm.phone.focus();
    return (false);
  }
 
  if (theForm.email.value == "")
  {
    alert("Please enter a value for the \"E-Mail Address\" field.");
    theForm.email.focus();
    return (false);
  }
 
  if (theForm.email.value.length < 6)
  {
    alert("Please enter at least 6 characters in the \"E-Mail Address\" field.");
    theForm.email.focus();
    return (false);
  }
  
	if (theForm.email.value.length >0)
	{
		i=document.forms[0].email.value.indexOf("@");
	 	j=document.forms[0].email.value.indexOf(".", i);
	 	k=document.forms[0].email.value.indexOf(",");
	 	kk=document.forms[0].email.value.indexOf(" ");
	 	jj=document.forms[0].email.value.lastIndexOf(".") + 1;
	 	len=document.forms[0].email.value.length;
		if ((i>0) && (j>(1+1)) && (k==-1) && (kk==-1) && (len-jj >=2) && (len-jj<=3)) 
		{
	  	}else 
	  	{
	  		alert("Please enter an exact email address.\n'" + document.forms[0].email.value + "' is invalid.");
	   	return false;
	  }
	}
 
  if (theForm.source.selectedIndex == 0)
  {
    alert("The first \"Source\" option is not a valid selection.  Please choose one of the other options.");
    theForm.source.focus();
    return (false);
  }
 
  if (theForm.cctype.selectedIndex < 0)
  {
    alert("Please select one of the \"cctype\" options.");
    theForm.cctype.focus();
    return (false);
  }
 
  if (theForm.cctype.selectedIndex == 0)
  {
    alert("The first \"cctype\" option is not a valid selection.  Please choose one of the other options.");
    theForm.cctype.focus();
    return (false);
  }
 
  if (theForm.ccnumber.value == "")
  {
    alert("Please enter a value for the \"Credit Card Number\" field.");
    theForm.ccnumber.focus();
    return (false);
  }
 
  if (theForm.ccnumber.value.length < 4)
  {
    alert("Please enter at least 4 characters in the \"Credit Card Number\" field.");
    theForm.ccnumber.focus();
    return (false);
  }
 
  var checkOK = "0123456789--";
  var checkStr = theForm.ccnumber.value;
  var allValid = true;
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
  }
  if (!allValid)
  {
    alert("Please enter only digit and \"-\" characters in the \"Credit Card Number\" field.");
    theForm.ccnumber.focus();
    return (false);
  }
 
  if (theForm.ccmonth.selectedIndex < 0)
  {
    alert("Please select one of the \"Expiration Month\" options.");
    theForm.ccmonth.focus();
    return (false);
  }
 
  if (theForm.ccmonth.selectedIndex == 0)
  {
    alert("The first \"Expiration Month\" option is not a valid selection.  Please choose one of the other options.");
    theForm.ccmonth.focus();
    return (false);
  }
 
  if (theForm.ccyear.selectedIndex < 0)
  {
    alert("Please select one of the \"Expiration Year\" options.");
    theForm.ccyear.focus();
    return (false);
  }
 
  if (theForm.ccyear.selectedIndex == 0)
  {
    alert("The first \"Expiration Year\" option is not a valid selection.  Please choose one of the other options.");
    theForm.ccyear.focus();
    return (false);
  }
  return (true);
}
