//Form validation starts here

    // 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;
    }

    //this function validates the date field
    function validDate (month,day,year) {
	month=month+"";
	day=day+"";
	year=year+"";
	var theDate;

	theDate = month;
	theDate += "/";
	theDate += day;
	theDate += "/";
	theDate += year;

	return isDate (theDate, "MM/dd/yyyy");
    }

    //this function checks for a future date
    function futureDate (month,day,year) {
	month=month+"";
	day=day+"";
	year=year+"";
	var theDate;
	var todaysDate;

	theDate = month;
	theDate += "/";
	theDate += day;
	theDate += "/";
	theDate += year;

	todaysDate = formatDate(new Date(), "MM/dd/yyyy");
	return compareDates (theDate, "MM/dd/yyyy",
	       	             todaysDate, "MM/dd/yyyy");
    }

    //this function checks for a date greater than 3 months ago
    function pastDate (month,day,year) {
	var rawdate;
	var y1;
	var y2;
	var diff;

	y2  = parseFloat(month);
	y2 += parseInt(year+"") * 100;

	rawdate = new Date();

	// IE and Netscape return a different getYear value
	// For IE we don't add the 1900 while on Netscape we do
	y1  = parseInt(rawdate.getYear());
	if (y1 < 2002) {
	   y1 = (1900 + y1) * 100;
        }
	else {
	   y1 *= 100;
	}
	y1 += parseInt(rawdate.getMonth())+1;

	diff = y1 - y2;
	//alert ("y1="+y1+" y2="+y2+" diff="+diff);
	return ((diff > 3 && diff < 89) || diff > 91);
    }

    //this function returns false if the password is 'golf'
    function validPassword (password) {
	var rv = true;
	if (password == "golf") {
	   rv = false;
	}
	return rv;
    }

    //this function returns true if a string is a valid e-mail address

    function validEmail (email) {
	//N
	//first check for presence of invalid characters
	//alert ("We're in the validation function")
	  var invalidChars = " /:,;"
	  for (i=0; i<5; i++) { 
	     //G
	     badChar = invalidChars.charAt(i)
	     if (email.indexOf(badChar,0) > -1) {
	        //H
		return (false);
	     }//-H
					
	  }//-G    
	  //next check for presence of one @ character
	var atPos = email.indexOf("@",1);
	if (atPos == -1) {
	  //I
	  return (false);
	}//-I
	if (email.indexOf("@",atPos+1) > -1) {
	   //J
	   return (false);
	} //-J
	//next check for period
	var periodPos = email.indexOf(".",atPos);
	if (periodPos == -1) {
	  //K
	  return (false);
	} //-K
	if (periodPos+3 > email.length) {
	   //L
	   return (false);
	}//-L
	return (true);
      }//-N

      // 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 = "";

	  // 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++) {
	    //A
	    var e = f.elements[i];
	    if (((e.type == "text") || (e.type == "textarea") || (e.type == "password")) && !e.optional) {
	       //B
	       // first check if the field is empty
	       if ((e.value == null) || (e.value == "") || isblank(e.value)) {
		  //C
		  empty_fields += "\n          " + e.title;
		  continue;
	       } //-C
	    } //-B

	    // Now check for fields that are supposed to be numeric.
	    if (e.numeric || (e.min != null) || (e.max != null)) {
	       //D
	       var v = parseFloat(e.value);
	       if (isNaN(v) || 
		   ((e.min != null) && (v < e.min)) || 
		   ((e.max != null) && (v > e.max))) {
		  //E
		  errors += "- The field " + e.title + " 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";
	       }//-E
	    }//-D

	    // Now check for valid password
	    if (e.title == "Password") {
	       if (e.value == "") {
	          errors += "- The password fields can not be empty!\n";
	       }
	       else {
	          if (!validPassword(e.value)) {
	             errors += "- The password 'golf' is a reserved password. Try another!\n";
	          }
	          else {
		     if (document.registerform.New_Password.value == "") {
	                if (document.registerform.Password.value != document.registerform.Verify_Password.value) {
	                   errors += "- The Password and Verify Password fields don't match!\n";
		        }
		     }
	             else {
	                if (document.registerform.New_Password.value != document.registerform.Verify_Password.value) {
	                   errors += "- The New Password and Verify Password fields don't match!\n";
	                }
	             }
		  }
	       }
	    }

	    // Now check for fields that are supposed to be e-mail addresses
	    if (e.email) {
	       //F
	       if (!validEmail(e.value)){
		  //O
		  //alert ("We're in the If statement");
		  errors += "- The field " + e.name + " must be a valid e-mail address.\n";
	       } //-O	
	       //alert ("We're outside the If statement");
	    }//-F

	  }//-A

	  var hasMonth, myMonth, myDay, myYear;
	  for(var i = 0; i < f.length; i++) {
	     //A
	     var e = f.elements[i];

	     if (e.title == "FavoriteCourse") {
	        //if there is no course or slope rating entered
	        //make sure a course is selected, otherwise generate
	        //an error
	        var objScore = document.scoreform.FavoriteCourse;
	        if (objScore.options[objScore.selectedIndex].length == 0) {
	           errors += "You must choose a course from your list courses.\n";
	        }
	     }
             if (e.title == "Month") {
	        hasMonth = true;
	        for (var jj=0; jj < e.options.length; jj++) {
		    if (e.options[jj].selected) {
	               myMonth = e.options[jj].value;
		    }
	        }
	        //alert ("myMonth:"+myMonth);
	     }
	     if (e.title == "Day") {
	        for (var jj=0; jj < e.options.length; jj++) {
		    if (e.options[jj].selected) {
	               myDay = e.options[jj].value;
		    }
		}
	        //alert ("myDay:"+myDay);
	     }
	     if (e.title == "Year") {
	        for (var jj=0; jj < e.options.length; jj++) {
		    if (e.options[jj].selected) {
	               myYear = e.options[jj].value;
		    }
		}
	        //alert ("myYear:"+myYear);
	     }
	  }
	  //alert ("myMonth:"+myMonth);
	  //alert ("myDay:"+myDay);
	  //alert ("myYear:"+myYear);
	  if (hasMonth) {
	     if (!validDate(myMonth, myDay, myYear)) {
	        errors += "You must enter a valid date.\n";
             }
	     else {
 	        if (futureDate(myMonth, myDay, myYear)) {
	           errors += "You may not post scores for a future date.\n";
	        }
		else {
		  if (pastDate(myMonth, myDay, myYear)) {
		     errors += "You may not post a score older than 3 months.\n";
		  }
	        }
	     }
	  }
	  // Now, if there were any errors, display the messages, and
	  // return false to prevent the form from being submitted. 
	  // Otherwise return true.
	  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 these 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;
      }
