// Setup the Array to hold any errors that will be alerted back to the user
var errors = new Array();

function display_errors(errors){
	//loop through all the errors in the errors array
	var err_msg = '';
	for(i=0;i<errors.length;i++){
		err_msg += errors[i] + "\n";
	}

	alert("There were "+errors.length+" errors:\n\n" + err_msg);
	return false;
}

// A utility function that returns true if a string contains only
// whitespace characters.
function isblank(e)
{
  if (e.value == null || e.value == "")
    return true;

  for(var i = 0; i < e.value.length; i++)
  {
     var c = e.value.charAt(i);
     if ((c != ' ') &&
         (c != '\n') &&
         (c != '\t'))
        return false;
  }
  return true;
}

// Checks if an optional field is blank
function checkblank(e)
{
  if (isblank(e))
  {
    errors.push("The Field \"" + e.description + "\" must be filled in.");
  }
  return true;
}


// Mike's check radio button function
function checkradio(e){
	var ischecked = '';
	for(i=0;i<e.length;i++){
	    (e[i].checked==false) ? ischecked += 'f' : ischecked += '';
	}
	if(ischecked=='ff'){
		errors.push("The Field \"" + e.description + "\" must be checked.");
	}
	return true;
}

// Checks if a field is numeric.
// If the optional min property is set, it checks it is greater than
// its value
// If the optional max property is set, it checks it is less than
// its value
function checknumber(e)
{
  var v = parseFloat(e.value);

  if (isNaN(v))
  {
    errors.push("The Field \"" + e.description + "\" must be a number");
  }

  if ((e.minNumber != null) && (v < e.minNumber))
  {
	errors.push("The Field \"" + e.description + "\" must be greater than or equal to "+e.minNumber);
  }

  if (e.maxNumber != null && v > e.maxNumber)
  {
	errors.push("The Field \"" + e.description + "\" must be less than or equal to "+e.maxNumber);
  }

  return true;
}

// Checks if a field looks like a date in the yyyy-mm-dd format
function checkdate(e)
{
  var slashCount = 0;
  if (e.value.length != 10)
  {
	errors.push("The Field \"" + e.description + "\" must have the format yyyy-mm-dd and be 10 characters in length");
  }

  for(var j = 0; j < e.value.length; j++)
  {
    var c = e.value.charAt(j);

    if ((c == '-'))
       slashCount++;

    if (c != '-' && (c < '0' || c > '9'))
    {
	errors.push("The Field \"" + e.description + "\" can contain only numbers and dashes");
    }
  }

  if (slashCount != 2)
  {
	errors.push("The Field \"" + e.description + "\" must have the format yyyy-mm-dd");
  }

  return true;
}

// Checks if a field contains any whitespace
function checkwhitespace(e)
{
  var seenAt = false;

  for(var j = 0; j < e.value.length; j++)
  {
     var c = e.value.charAt(j);

     if ((c == ' ') || (c == '\n') || (c == '\t'))
     {
		errors.push("The Field \"" + e.description + "\" must not contain whitespace.");
     }
  }
  return true;
}

// Now check for fields that are supposed to be emails.
// Only checks that there's one @ symbol and no whitespace
function checkemail(e)
{
  var seenAt = false;

  for(var j = 0; j < e.value.length; j++)
  {
    var c = e.value.charAt(j);

    if ((c == ' ') || (c == '\n') || (c == '\t'))
    {
		errors.push("The Field \"" + e.description + "\" must not contain whitespace");
    }

    if ((c == '@') && (seenAt == true))
    {
		errors.push("The Field \"" + e.description + "\" must contain only one @");
    }

    if ((c == '@'))
      seenAt = true;
  }

  if (seenAt == false)
  {
	errors.push("The Field \"" + e.description + "\" must contain one @");
  }
  return true;
}


// This is the function that performs <form> validation.
// It is invoked from the onSubmit( ) event handler.
// The handler should return whatever value this function
// returns.
function verify(f)
{
  
    // Loop through the elements of the form, looking for all
  // text and textarea elements. Report errors using a post validation,
  // field-by-field approach
  for(var i = 0; i < f.length; i++)
  {
     var e = f.elements[i];

     if (((e.type == "text") || (e.type == "textarea") || (e.type == "menu")))
     {
        // first check if the field is empty and shouldn't be
        if (!e.isOptional && !checkblank(e))
          return false;

        // Now check for fields that are supposed to be numeric.
        if (!isblank(e) && e.isNumeric && !checknumber(e))
          return false;

        // Now check for fields that are supposed to be dates
        if (!isblank(e) && e.isDate && !checkdate(e))
          return false;

        // Now check for fields that are supposed to be emails
        if (!isblank(e) && e.isEmail && !checkemail(e))
          return false;

        // Now check for fields that are supposed
        // not to have whitespace
        if (!isblank(e) && e.hasNospaces && !checkwhitespace(e))
          return false;

     } // if (type is text or textarea)
  } // for each character in field


//check radio buttons



/* If there are errors inside the error array, then run the function to pop up an alert with the errors listed.
   errors errors errors.... (felt like this comment needed the word errors some more) */
if(errors.length>0){
	display_errors(errors);
	return false;
}


  // There were no errors if we got this far
  return true;
}



function check_form(){
    //Reset the errors Array
    errors.length = 0;

	var form = document.regnews;
	form.email.isEmail = true;
	form.email.description = 'Email Address';
	

	return verify(form);
}