
var FormErrors = "";
var ErrorLocation = "";

function addError(Location,Error)
{
  if (FormErrors == "")
  {
    FormErrors = "\n" + Error + "\n";
    ErrorLocation=Location;
  }
  else
  {
    FormErrors += Error + "\n";
  }
  errfound=true;
  return true;
}


function ValidLength(item,len) {
   return (item.length >= len);
}

function ValidEmail(item) {
   if (!ValidLength(item, 5)) return false;
   if (item.indexOf ('@', 0) == -1) return false;
   if (item.indexOf ('.', 0) == -1) return false;
   return true;
}

function FormatPhone(item) {
   newstring="(";
   digitstr="0123456789"
   for (i=0; i < item.length;i++)
      {
      if (digitstr.indexOf(item.substring(i,i+1),0)!=-1){
         newstring=newstring+item.substring(i,i+1);
         if (newstring.length==4) newstring=newstring+") ";
         if (newstring.length==9) newstring=newstring+"-";
         }
      }
   if (newstring.length==1)
      {
      return "";
      }
   else
      {
      return newstring;
      }
}

function ValidPhone(item) {
   newstring=FormatPhone(item);
   if (newstring.length==14){
      item=newstring;
      return true;
      }
   else {
      if (newstring.length >= 10) return true;
      }
   return false;
}

/*
function ValidState(item) {
   item=item.toUpperCase();
   statestr="  |AL|AK|AZ|CA|CO|CT|DE|DC|FL|GA|HI|ID|IL|IN|IA|KS|KY|LA|ME|MD|";
   statestr=statestr+"MA|MI|MN|MS|MO|MT|NE|NV|NH|NJ|NM|NY|NC|ND|OH|OK|PA|PR|RI|";
   statestr=statestr+"SC|SD|TN|TX|UT|VT|VI|VA|WA|WV|WI|WY|AB|BC|MB|NB|NF|NS|ON|PI|PE|QB|SK|YK|NT|";
   return (statestr.indexOf(item,0)!=-1)
}
*/

function showerrors(text,elem) {
   window.alert(text);
   elem.select();
   elem.focus();
}

function toUpperLower(item) {
   newstring="";
   firstone=true;
   for (i=0; i < item.length;i++)
      {
      thisone=item.substring(i,i+1);
      if (thisone==" ") {
         newstring=newstring+thisone;
         firstone=true;
         continue;
         }
      if (firstone && thisone.toLowerCase()==thisone){
         thisone=thisone.toUpperCase()
         newstring=newstring+thisone;
         firstone=false;
         }
      else
         {
         firstone=false;
         newstring=newstring+thisone;
         }
      }
   return newstring;
}

function Validate() {
   errfound = false;
   if (!ValidLength(document.orderform.FirstName.value,1))
      addError(document.orderform.FirstName,"Your first name is missing.");
   if (!ValidLength(document.orderform.LastName.value,1))
      addError(document.orderform.LastName,"Your last name is missing.");
   if (!ValidPhone(document.orderform.Phone.value))
      addError(document.orderform.Phone,"The telephone number is missing or incorrect.");
   if (!ValidEmail(document.orderform.Email.value))
      addError(document.orderform.Email, "Your email address is missing or incorrect.");
   if (!ValidLength(document.orderform.Address.value,2))
      addError(document.orderform.Address,"The address is missing.");
   if (!ValidLength(document.orderform.City.value,2))
      addError(document.orderform.City,"The city is missing");
   if (!ValidLength(document.orderform.Zip.value,5))
      addError(document.orderform.Zip,"The ZIP/postal code is missing or incorrect.");
   if (!CheckQty())
      addError(document.orderform.elements[8],"No quantities have been chosen.");
   if (errfound==true) showerrors( "The following errors were encountered:\n" + FormErrors, ErrorLocation );
   FormErrors="";
   ErrorLocation="";
   return !errfound; /* true if there are no errors */
}


function clean(string) {
   outstring="";
   LenStr=string.length+1;
   n=0;
   NumStr="0123456789";
   for (n=0;n<LenStr;n++) {
      if (NumStr.indexOf(string.substring(n,n+1))>-1)
         {
         outstring=outstring+string.substring(n,n+1);
         }
   }
   if (outstring.length==0) {
      outstring=0;
   }
   return outstring;
}

function CheckQty() {
   formlen=document.orderform.length-3;
   i=8;
   while (i<formlen) {
      if (document.orderform.elements[i].value != "") {
         return true;
      }
      i=i+3;
   }
      return false;
}

var ie4 = document.all;
var ns4 = document.layers;

if(ns4) document.captureEvents(Event.MOUSEDOWN);

document.onmousedown = right;

function right(e){
   var msg = "Copyright © 2000\nBlue Ribbon Foods\nAll Rights Reserved.";
   if (ns4 && e.which == 3)
   {
      alert(msg);  // Delete this line to disable but not alert user
      return false;
   }
else
   if (ie4 && event.button==2)
   {
      alert(msg); // Delete this line to disable but not alert user
      return false;
   }
return true;
}

function trap() {
if(navigator.appName == 'Netscape' && document.images)
   {
   for(i=0;i<document.images.length;i++)
      document.images[i].onmousedown = right;
   }
}

var FormSent=false;
 
function SendForm() {
   if (FormSent==false) {
      if (Validate()) {
         document.orderform.submit();
         FormSent=true;
      }
   }
   else 
   {
      alert("The form has been submitted.  Just a moment please.");
   }
   return
}