// FormLib.js
// Common functions used with forms.

validate= new Object();

function isInteger( value)
{
   return (value==parseInt(value));
}

function inRange (value, low, high) {
   return ( ! (value < low) && value<=high);
}

function checkFormat (value, format) {
   var formatOkay=true;

   if (value.length != format.length) {
      return false;
   }

   for (var i=0; i< format.length; i++)
   {
      if (format.charAt(i) == '#' && ! isInteger(value.charAt(i) ) )
      {
         return false;
      }
      else if (format.charAt(i) != '#' &&
               format.charAt(i) != value.charAt(i) ) 
      {
         return false;
      }
   }
   return true;
}


// Takes a form and an array of element names, verifies that each has a value.

function requireValues (form, requiredValues)
{
   for (var i=0; i<requiredValues.length; i++) 
   {
      element=requiredText[i];
      if (form[element].value=="")
      {
         alert("Please enter a value for " + element + ".");
	 return false;
      }
   }
   return true;
}



// Takes a form and an array of element names, verifies that each has 
// a value checked.

function requireRadios (form, requiredRadio)
{
   for (var i=0; i<requiredRadio.length; i++) 
   {
      element=requiredRadio[i];
      isChecked=false;
      for (j=0; j<form[element].length;j++)
      {
         if (form[element][j].checked)
	 {
	   isChecked=true;
	 }
      }
      
      if (!isChecked)
      {
         alert("Please choose a " + form[element][0].name + ".");
	 return false;
      }
   }
   return true;
}


// Verify that there are no uncorrected formatting problems with elements
// validated on a per element basis.

function checkProblems ()
{
   for (element in validate)
   {
      if (! validate[element] )
      {
         alert("Please correct the format of " + element + ".");
	 return false;
      }
   }
   return true;

}


// Verifies that the value of the provided element has ##### format.

function checkZip ( element )
{

   if (! checkFormat (element.value, "#######" ) ) 
   {
      alert("Please enter a five digit zip code"); 
      element.focus();
      validate[element.name]=false;
   }
   else
   {
      validate[element.name]=true;
   }
   
   return validate[element.name];
}

// Verifies that the value of the provided element has ####-###-#### format.

function checkPhone ( element )
{
   if (! checkFormat (element.value, "###-###-####" ) ) 
   {
      alert("Please enter " + element.name + " in 800-555-1212 format");
      element.focus();
      validate[element.name]=false;
   }
   else
   {
      validate[element.name]=true;
   }
   
   return validate[element.name];
}


// Verifies that the value provided is an integer between 1 and 150.
function checkPhone ( element )
{
   if (! isInteger(element.value) ||
       ! inRange(element.value, 1 , 150 )) 
   {
      alert("Please enter a number between 1 and 150");
      element.focus();
      validate[element.name]=false;
   }
   else
   {
      validate[element.name]=true;
   }
   
   return validate[element.name];
}








