// 1. Check e-mail

function ValidEmail (addr, chirp)
  {
  fail = false;
  if (addr.value.length == 0) return false;
  var atpos = addr.value.indexOf('@',0);
  if( (addr.value.length < 7) ||
      (atpos == -1)  )
      fail = true;              // no '@' in email address!
 if (! fail)
    {
    if (addr.value.indexOf('.',atpos) == -1)
       fail = true;             // no '.' in address after the @.
    };
 if( (fail) && (chirp) )
     {
     alert ("Warning!\n The email address "+ addr.value +" is invalid!");
     addr.select();
     addr.focus();
     };
  return (! fail);
  }


//2. Check your_name 

function ValidSurname (name, chirp)
  {
  var okay = true;
if (name.value.length < 3)  
        okay = false;
if ( (! okay) && chirp )
     {
     alert ("Is '"+ name.value + "' really your name? Please re-enter!");
     name.select();
     name.focus();
     };
 return okay;
  }


//3. Confirm clear form!

function ConfirmClear ()
{
if (confirm ("Are you sure you want to clear the formulaire?") )
   return true;
return false;
}

// 4. Check power

function ValidPower (power, chirp)
  {
  var okay = true;
  if (power.value.length == 0) return true; 
  pattern = /^[0-9]*\.?[0-9]*$/;
  if(pattern.test(power.value)==false)
        okay = false;
  if (power.value < -0.000001 ) 
        okay = false;
  if (power.value > 2000 ) 
        okay = false;
if ( (! okay) && chirp )
     {
     alert ("Warning!\n The power must be >= 0 and <= 2000 or left blank!");
     power.select();
     power.focus();
     };
 return okay;
  }

// 5. Check speed

function ValidSpeed (speed, chirp)
  {
  var okay = true;
  if (speed.value.length == 0) return true; 
  pattern = /^[0-9]*\.?[0-9]*$/;
  if(pattern.test(speed.value)==false)
        okay = false;
  if (speed.value < -0.000001 ) 
        okay = false;
  if (speed.value > 6500 ) 
        okay = false;
if ( (! okay) && chirp )
     {
     alert ("Warning!\n The speed must be >= 0 and <= 6500 or left blank!");
     speed.select();
     speed.focus();
     };
 return okay;
  }

// 6. Check Excitation_Voltage

function ValidExcitation_Voltage (Excitation_Voltage, chirp)
  {
  var okay = true;
  if (Excitation_Voltage.value.length == 0) return true; 
  pattern = /^[0-9]*\.?[0-9]*$/;
  if(pattern.test(Excitation_Voltage.value)==false)
        okay = false;
  if (Excitation_Voltage.value < 9.99999999) 
        okay = false;
  if (Excitation_Voltage.value > 440 ) 
        okay = false;
if ( (! okay) && chirp )
     {
     alert ("Warning!\n The speed must be >= 10 and <= 440 or left blank!");
     Excitation_Voltage.select();
     Excitation_Voltage.focus();
     };
 return okay;
  }

// 6. Check Armature_Current

function ValidArmature_Current (Armature_Current, chirp)
  {
  var okay = true;
  if (Armature_Current.value.length == 0) return true; 
  pattern = /^[0-9]*\.?[0-9]*$/;
  if(pattern.test(Armature_Current.value)==false)
        okay = false;
  if (Armature_Current.value < -0.000001) 
        okay = false;
  if (Armature_Current.value > 2500 ) 
        okay = false;
if ( (! okay) && chirp )
     {
     alert ("Warning!\n The armature current must be >= 0 and <= 2500 or left blank!");
     Armature_Current.select();
     Armature_Current.focus();
     };
 return okay;
  }

// 7. Check Excitation_Current

function ValidExcitation_Current (Excitation_Current, chirp)
  {
  var okay = true;
  if (Excitation_Current.value.length == 0) return true; 
  pattern = /^[0-9]*\.?[0-9]*$/;
  if(pattern.test(Excitation_Current.value)==false)
        okay = false;
  if (Excitation_Current.value < -0.000001) 
        okay = false;
  if (Excitation_Current.value > 2500 ) 
        okay = false;
if ( (! okay) && chirp )
     {
     alert ("Warning!\n The excitation current must be >= 0 and <= 2500 or left blank!");
     Excitation_Current.select();
     Excitation_Current.focus();
     };
 return okay;
  }

// On Submit : validate data //

function CheckInput (thisform)
  { var errors = 0;                             //error count
    var duditem = "";                           //remember where it failed!
    var message = "\n";                         //error message

  if (! ValidPower (thisform.power, false) )
        { errors ++;
          duditem = thisform.power;
          message += "The power must be >= 0 and <= 2000 or left blank!\n"; 
        };

  if (! ValidSpeed (thisform.speed, false) )
        { errors ++;
          duditem = thisform.speed;
          message += "The speed must be >= 0 and <= 6500 or left blank!\n"; 
        };

  if (! ValidArmature_Current (thisform.Armature_Current, false) )
        { errors ++;
          duditem = thisform.Armature_Current;
          message += "The armature current must be >= 0 and <= 2500 or left blank!\n"; 
        };

  if (! ValidExcitation_Voltage (thisform.Excitation_Voltage, false) )
        { errors ++;
          duditem = thisform.Excitation_Voltage;
          message += "The excitation voltage must be >= 10 and <= 440 or left blank!\n"; 
        };

  if (! ValidExcitation_Current (thisform.Excitation_Current, false) )
        { errors ++;
          duditem = thisform.Excitation_Current;
          message += "The excitation current must be >= 0 and <= 2500 or left blank!\n"; 
        };

  if (! ValidEmail (thisform.your_email, false) )
        { errors ++;
          duditem = thisform.your_email;
          message += "Your e-mail address appears incorrect!\n"; 
        };

  if (! ValidSurname (thisform.your_name, false) )
        { errors ++;
          duditem = thisform.your_name;
          message += "Your name appears inaccurate!\n"; 
        };

  if (errors == 0)
     { if ( ! confirm ("Dear "
                   + thisform.your_name.value
                   + "\n Your form is about to submitted."
                   + "\n Are you sure you want to do this?" ) )
           { errors ++; };
     } else
     { message += "\nPlease re-enter this information and re-send!\n\nIf there are values that are not accepted in the formulaire\nbut you wish to communicate them to us,\nfeel free to use the comments section.";
       if (errors == 1)
           { alert  ("There was an error in your formuliare.\n"
                     + message);
           } else
           { alert ("There were errors in your formulaire.\n" + message);
           };
       duditem.select();
       duditem.focus();
     };

  if (errors != 0) return (false);
  return (true);      
  }

  //////////////////////// end of the main function /////////////////////


