
var formel;
var cnumel;
var zipel;
var bFinal = false;
var infoel;

// Getting elements.
formel = document.forms [0];
cnumel = formel.Cell_Number;
zipel  = formel.zipcode;

// Setting handlers.
cnumel.onkeydown     = OnKeyDown_CellNumber;
zipel.onkeydown      = OnKeyDown_ZIP;
formel.__oldonsubmit = formel.onsubmit;
formel.onsubmit      = OnFormSubmit;
infoel               = document.getElementById ( "infoelid" );

// Setting the zip field maximum length.
zipel.maxLength = 5;

//----------------------------------------
function OnKeyDown_CellNumber ( evt )
{
  evt = evt ? evt : window.event;  
  
  if ( !IsKeyNumeric ( evt.keyCode ) )
    {
      alert ( "Please, enter only digits.\nKey code: " + evt.keyCode );
      evt.returnValue = false;
//      return false;
    }
}
//----------------------------------------
function OnKeyDown_ZIP ( evt )
{
  evt = evt ? evt : window.event;  
  
  if ( !IsKeyNumeric ( evt.keyCode ) )
    {
      alert ( "Please, enter only digits." );
      evt.returnValue = false;
    }
}
//----------------------------------------
function IsKeyNumeric ( keycode )
// Check if the key is a numeric one.
{
  return keycode >= 48 && keycode <= 57 ||
         keycode >= 96 && keycode <= 105 ||
         keycode == 8 ||
         keycode == 9 ||
         keycode == 13 ||
         keycode == 45 ||
         keycode == 46 ||
         keycode == 144 ||
         keycode >= 33 && keycode <= 40;
}
//----------------------------------------
function OnFormSubmit ( )
{
  if ( this.__oldonsubmit )
    if ( !this.__oldonsubmit ( ) )
      return false;
      
  if ( cnumel.value.length != 10 )
    {
      alert ( "Your Cell Phone Number is invalid!\nIt should contain 10 numbers." );
      return false;
    }

  if ( zipel.value.length != 5 )
    {
      alert ( "Your ZIP code is invalid!\nIt should contain 5 numbers." );
      return false;
    }

  if ( bFinal )
    {
      return true;  // Allow submit.
    }
  else
    {
                      // This is not the final filling of this form.
      bFinal = true;  // Let the user check data before final submitting.
      Inform ( );     // Inform user to check again.
      return false;
    }
}
//----------------------------------------
function Inform ( )
{
  
  infoel.style.backgroundColor = "#A07000";  // Set background color of the warning message.
  infoel.style.color = "#FFFF00";            // Set color of the warning message.
  alert ( "You are almost done. Please verify all the information you have entered.\nIf there are any errors you can correct them on this page and then hit Send.\nMost importantly check that your cell phone number, email address and zip \ncode are accurate. It is necessary that these three items are 100% accurate. \nCheck your information again, make any necessary changes, then hit Send." );
}
//----------------------------------------

