
//  A utility function that returns 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;
}


// check for null string and blank string 
// return true for a valid string.
function isValidString(s)
{
   if ( s == null || s == "" || isblank(s) )
      return false ;
   else
      return true;

}

function isValidEmail(str) {
   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0); 
}



// 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 verifyForm(f)
{
    var msg;
    var empty_fields = "";

    // Loop through the elements of the form, looking for all 
    // text and textarea elements that have an "required" 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.required) { 
            if ((e.type == "text") || (e.type == "textarea")) 
            {
                // first check if the field is empty
                if ( ! isValidString(e.value) ) 
                {
                    empty_fields += "\n          " + e.required;
                    continue;
                }
            } else if (e.type == "select-one")
            {
                for (j=0; j<e.options.length; j++)
                {
                  if (e.options[j].selected)
                  {
                    if ( ! isValidString(e.options[j].text) ) 
                    {
                      empty_fields += "\n          " + e.required;
                    }
                    break;        
                  }
                }
            }
         }  
            
     }
     if (!empty_fields) {
        if (isValidEmail(f.email.value)) return true;
        else {
           alert ("Ivalid e-mail address");
           return false; 
        }  
    }

    msg  = "______________________________________________________\n\n"
    msg += "                                                        \n";
    msg += "Please enter the following fields:     \n\n";
    msg += empty_fields ;
    msg += "\n______________________________________________________\n\n"

    
    alert(msg);
    return false;
}

// This funcion collects all values from a group of check boxes (f) and
// puts them in a single filed (of)
// It require to set a global variabl to hold the string
function CheckBoxes (s, f, of) {
if (f.checked)
      s = s + f.value + '  ||  ';
else
      s = s.replace(f.value + '  ||  ', '');      
of.value = s;
return s;
}

