// A utility function to check an email address for the 
// correct format
function isEmail(str) {
    if (window.RegExp)
    {
        var tempStr = "a";
        var tempReg = new RegExp(tempStr);
        if (tempReg.test(tempStr)) supported = 1;
    }
    if (!supported)
    {
       return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
    }
    var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
    var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
    return (!r1.test(str) && r2.test(str));
}

// A utility function that returs true if a string contains only 
// whitespace characters.
function isblank(s)
{
    for ( var i = 0; i < s.length; i++ )
    {
        var c = s.charAt(i);
        if (( c != ' ' ) && ( c != '\n' ) && ( c != '\t' )) return false;
    }
    return true;
}

// This is the function that performs form verification.  It will be invoked
// from the onSubmit() event handler.  The handler should return whatever value
// this function returns.
function verify(f)
{
    var msg;
    var empty_fields = "";
    var errors = "";
		var currValue = "";
    
    // Loop through the elements of the form, looking for all
    // text and textarea elements that don't have an "optional" property
    // defined.  Then, check for fields that are empty and make a list of them.
    // Also, if any of these elements have a "min" or a "max" property defined,
    // then verify that they are numbers and that they are in the right range.
    // Put together error messages for fields that are wrong.
    for ( var i = 0; i < f.length; i++ )
    {
        var e = f.elements[i];
        if ((( e.type == "text" )      || ( e.type == "password" ) || 
             ( e.type == "textarea" )) && !e.optional )
        {
            if (( e.value == null ) || ( e.value == "" ) || isblank(e.value))
            {
                empty_fields += "\n          " + e.name;
                continue;
            }
      
            if ( e.name.indexOf ( 'email' ) != -1 )
            {
               if ( !isEmail ( e.value ))
               {
                 errors += "- The field " + e.name + " must be a valid email address\n";
               }
               continue;
             }

            if ( e.name.indexOf ( 'zip' ) != -1 )
            {
               if ( e.value.length!=5)
               {
                 errors += "- Your " + e.name + " has to be at least 5 digits\n";
               }

               if ( e.value == "00000")
               {
                 errors += "- Your " + e.name + " can not be all zeros\n";
               }

               if (isNaN(e.value))
               {
                 errors += "- Your " + e.name + " can only contain Numbers\n";
               }


               continue;
             }

            if ( e.name.indexOf ( 'zip' ) != -1 )
            {
               if ( e.value == "00000")
               {
                 errors += "- Your " + e.name + " can not be all zeros\n";
               }
               continue;
             }

///////////
            if ( e.name.indexOf ( 'zip' ) != -1 )
            {

               input = parseInt(e.value, 10)
               if (isNaN(input))
               {
                 errors += "- Your " + e.name + " can only contain Numbers\n";
               }
               continue;
             }

            if ( e.name.indexOf ( 'zip' ) != -1 )
            {

               if (isNaN(e.value))
               {
                 errors += "- Your " + e.name + " can only contain Numbers\n";
               }
               continue;
             }

///////////


      
             if ( e.numeric )
             {
               if (( e.min != undefined ) || ( e.max != undefined ) || ( e.min != null) || ( e.max != null ))
               {
                 var v = parseFloat(e.value);
                 if ( isNaN(v) ||
                     (( e.min != null ) && ( v < e.min )) ||
                     (( e.max != null ) && ( v > e.max )))
                 {
                     errors += "- The field " + e.name + " must be a number";
                     if ( e.min != null )
                        errors += " that is greater than " + e.min;
                     if ( e.max != null && e.min != null )
                        errors += " and less than " + e.max;
                     else if ( e.max != null )
                        errors += " that is less than " + e.max;
                     errors += "\n";
                 }
              }
            }
						currValue = e.value;
          }
          else
          {
	        if ( e.type == "select-one" && !e.optional )
            {
						for ( i=0; i < e.options.length; i++ )
						{
							if ( e.options[i].selected )
							{
								currValue = e.options[i].value;
							}
						}
               if (( currValue == null ) || ( currValue == "" ) || isblank(currValue))
               {
                 empty_fields += "\n          " + e.name;
                 continue;
                }
            }
          }
    }
    
    if ( !empty_fields && !errors ) return true;
    
    msg  = "___________________________________________________\n\n";
    msg += "The form was not submitted because of the following error(s).\n";
    msg += "Please correct the error(s) and re-submit.\n";
    msg += "___________________________________________________\n\n";
    
    if ( empty_fields )
    {
        msg += "- The following required field(s) are empty: " + 
               empty_fields + "\n";
        if ( errors ) msg += "\n";
    }
    msg += errors;
    
    alert (msg);
    return false;
}

// Checks 2 password fields (pword1 and pword2) to make sure they are
// not blank and are the same.
function checkPW (f)
{
    //f = self.document.forms[0];
    if ((( f.pword1.value == null ) || ( f.pword1.value == "" ) || isblank(f.pword1.value)) &&
        (( f.pword1.value == null ) || ( f.pword1.value == "" ) || isblank(f.pword1.value)) ||
       ( f.pword1.value != f.pword2.value ))
    {
        alert ( 'Passwords must match and may not be blank\n' +
                'please try again' );
    f.pword1.value = '';
    f.pword2.value = '';
    f.pword1.focus();
        return false;
    }
    return true;
}

// Given a form element on a page,
// determine which form it is on (most of the time this is 0).
function getFormId ( e )
{
  for ( var i=0; i < document.forms.length; i++ )
  {
    var f = document.forms[i];
  
    if ( navigator['appName'].indexOf ( 'Netscape' ) != -1 )
    {
      for ( j=0; j<f.elements.length; j++ )
      {
        var e1 = f.elements[j];
        if ( e1 == e )
          return i;
      }
    }
    else
    {
      if ( document.forms[i].contains ( e ))
      {
        return i;
      }
    }
  }
  return 0;
}

function getPrevElement ( f, e )
{
  retVal = 0;
  // Walk through the form and match the element
  // Find the value of the element on the form
  for ( i = 0; i < f.length; i ++ )
  {
    var el = f.elements[i];
    if ( el.name == e.name )
    {
      if ( i != 0 )
        retVal = i - 1;
      break;
    }
  }
  return f.elements[retVal];
}

function getNextElement ( f, e )
{
  retVal = 0;
  // Walk through the form and match the element
  // Find the value of the element on the form
  for ( i = 0; i < f.length; i ++ )
  {
    var el = f.elements[i];
    if ( el.name == e.name )
    {
      if ( i + 1 > f.length )
        retVal = i;
      else
      retVal = i + 1;
      break;
    }
  }
  return f.elements[retVal];
}

function checkNumeric ( e, radix )
{
  /*
   || isNaN only tests the "first element" of a string, SO...
   || Parse each element of the textbox
  */
  for ( var j = 0; j < e.value.length; j++ )
  {
    var test = e.value.substr ( j, 1 );
      var v = parseInt(test, radix);
    if ( isNaN ( v ))
    {
      alert ( "Field (" + e.name + ") must be numeric" );
      e.value = '';
      e.focus;
      return false;
    }
  }
  return true;
}

function checkEmpty ( e )
{
  // Find out which form we are using
  var formID = getFormId ( e );
  var f = document.forms[formID];
  
  if ( e.value.length == 0 )
  {
    alert ( 'Field [' + e.name + '] may not be empty' );
    // x = getPrevElement ( f, e );
    // x.focus();
    e.focus();
     return true;
  }
  return false;
}

function checkPrev ( e )
{
  // Find out which form we are using
  var formID = getFormId ( e );
  var f = document.forms[formID];
  
  x = getPrevElement ( f, e );
  if ( x.value.length == 0 )
  {
    alert ( 'Field [' + x.name + '] may not be empty length' );
    x.focus();
     return true;
  }
  return false;
}

function checkElement ( e, len, val, radix )
{
  // Find out which form we are using
  var formID = getFormId ( e );
  var f = document.forms[formID];
  
  // If we "tabbed out" without typing anything 
  // then the field length will be 0
  // So back up and type again.
  /*
  x = getPrevElement ( f, e );
  if ( x.value.length == 0 )
  {
    alert ( 'Field [' + x.name + '] may not be empty length' );
    x.focus();
     return;
  }
  */

  // Otherwise, extract it and move to the next field
  if ( e.value.substr ( e.value.length-1, 1 ) == val )
  {
    e.value = e.value.substr ( 0, e.value.length-1 );
    if ( e.value.length != 0 )
    {
      if ( checkNumeric ( e, radix ))
      {
        x = getNextElement ( f, e );
        x.focus();
         return;
      }
    }
  }

  // If the extracted length is 0 then the field is invalid.
  if ( e.value.length == 0 )
  {
    e.value = '';
    e.focus;
     return;
  }

  // Walk through the form and match the element
  // Find the value of the element on the form
  for ( i = 0; i < f.length; i ++ )
  {
    var el = f.elements[i];
    if ( el.name == e.name )
    {
      /*
       || isNaN only tests the "first element" of a string, SO...
       || Parse each element of the textbox
      */
      for ( var j = 0; j < e.value.length; j++ )
      {
        var test = e.value.substr ( j, 1 );
        var v = parseInt(test, radix);
        if ( isNaN ( v ))
        {
          alert ( "Field (" + e.name + ") must be numeric" );
          e.value = '';
          e.focus;
          return false;
        }
      }
      break;
    }
  }

  // Give the next element focus
  if ( e.value.length != len )
    return;
  else
    f.elements[i+1].focus();
  return;
}


function nextElement ( e, len, val, radix )
{
  alert ( 'nextElement' );
  // Find out which form we are using
  var formID = getFormId ( e );
  var f = document.forms[formID];
  
  // If we "tabbed out" without typing anything 
  // then the field length will be 0
  // So back up and type again.
  if ( e.value.length == 0 )
  {
    alert ( 'Field may not be empty' );
    x = getPrevElement ( f, e );
    x.focus();
     return;
  }

  // Otherwise do normal "key checks"
  if ( e.value.length == len || e.value.substr(e.value.length-1,1) == val )
  {
    // If the extracted length is 0 then the field is invalid.
    if ( e.value.length - 1 == 0 )
    {
      e.value = '';
      e.focus;
       return;
    }

    // Otherwise, extract it and move to the next field
    if ( e.value.substr ( e.value.length-1, 1 ) == val )
    {
      e.value = e.value.substr ( 0, e.value.length-1 );
      if ( e.value.length == 0 )
      {
        e.focus;
         return;
      }
    }

    // Walk through the form and match the element
    // Find the value of the element on the form
    for ( i = 0; i < f.length; i ++ )
    {
      var el = f.elements[i];
      if ( el.name == e.name )
      {
        /*
         || isNaN only tests the "first element" of a string, SO...
         || Parse each element of the textbox
        */
        for ( var j = 0; j < e.value.length; j++ )
        {
          var test = e.value.substr ( j, 1 );
          var v = parseInt(test, radix);
          if ( isNaN ( v ))
          {
            alert ( "Field (" + e.name + ") must be numeric" );
            e.value = '';
            e.focus;
            return false;
          }
        }
        break;
      }
    }
    // Give the next element focus
    f.elements[i+1].focus();
  }
}

// Checks an area code field. If the length is 3 and 
// it is numeric tabs to the next field.
function chkArea (e)
{
  if ( e.value.length == 3 )
  {
    var formID = getFormId ( e );
    var f = document.forms[formID];
  
  // Find the value of the element on the form
    for ( i = 0; i < f.length; i ++ )
  {
    var el = f.elements[i];
      if ( el.name == e.name )
    {
      // If the area code is not numeric
    // blank it out, give it focus and return.
        var v = parseFloat(e.value);
        if ( isNaN ( v ))
    {
      alert ( "Area Code must be numeric" );
      e.value = '';
      e.focus;
      return;
    }
    break;
    }
    }
  // Give the next element focus
  f.elements[i+1].focus();
  }
}

function showBrowserInfo ()
{
  var browser = "BROWSER INFORMATION:\n" ;
  for (var propname in navigator )
  {
    browser += propname + ": " + navigator[propname] + "\n";
  }
  alert ( browser );
}

function showTypes (f)
{
  retVal = true;
  for ( var i = 0; i < f.length; i++ )
  {
    var e = f.elements[i];
  alert ( "Type: [" + e.type + "] name [" + e.name + "] value [" + e.value + "]" );
  }
  return retVal;
}

function clearForm ( e )
{
  var formID = getFormId ( e );
  var f = document.forms[formID];
	
  for ( var i = 0; i < f.length; i++ )
  {
     var e = f.elements[i];
     if (( e.type == "text" )      || ( e.type == "password" ) || 
         ( e.type == "textarea" )  || ( e.type == 'select-one' ))
     {
				e.value = '';
		}

		if ( e.type == 'checkbox' )
		{
		  e.checked = false
		}
	}
}
