//Originally written by Fabian Portilla (rfportilla@yahoo.com)
//This script may be used as is or can be modified as long as
//you post this message on the top.  If you make changes, simply 
//add your signature and nothing else above mine.

function CheckForm (form) {
  if( _check_checked ("Registration Type", form.os0)
  &&  _check_length ("Last Name", form.LastName, 2)
  &&  _check_length ("First Name", form.FirstName, 2)
  &&  _check_min_value("Birth Month", form.BirthMon, 1)
  &&  _check_length ("Birth Year", form.BirthYear, 2)
  &&  _check_checked ("Participation", form.Participate)
  &&  _check_length ("Primary Phone Area Code", form.PhonePrArea, 3)
  &&  _check_length ("Primary Phone Exchange", form.PhonePrExc, 3)
  &&  _check_length ("Primary Phone Number", form.PhonePrSta, 4)
  &&  _check_length ("Address", form.Address1, 5)
  &&  _check_length ("City", form.City, 3)
  &&  _check_length ("State", form.State, 2)
  &&  _check_length ("Zip", form.Zip, 5)
  &&  _check_length ("Registration Type", form.hosted_button_id, 5)
  ) {
    if(form.School.value == "Other" 
	&& (! (_check_length("School's Name",form.OtherSchoolName,5)
       &&  _check_length("School's State",form.ScState,2)))) {
	      return false;
	}
    for (i=0, n=form.os0.length; i<n; i++) {
      if (form.os0[i].checked) {
        var checkvalue = form.os0[i].value;
        break;
      }
    }
	if (checkvalue == "Active" || checkvalue == "Active+Event" || checkvalue == "Student") {
      if (! (_check_length ("School Name",form.School,5)
        &&   _check_min_value ("Graduation Month", form.GradMon, 1)
        &&  _check_length ("Graduation Year", form.GradYear, 4)
        )
	  ) {
		return false;
      }
	  if (! (checkvalue == "Student" || _check_length ("NC Registration Number", form.RegistrationNumber, 3))) {
	    return false;
	  }
    }
//    alert("Got Here2");
    return true 
  }
  else {return false}
}

function _check_length ( fieldname, field, len ) {
  if (field.value.length >= len)
  return true;
  alert ("Field has not been properly filled.  Please fill in required information: " + fieldname);
  field.focus();
  return false;
}

function _check_min_value( fieldname, field, min_value ) {
  if ( field.value >= min_value )
  return true;
  alert ("Field has not been properly filled.  Please fill in required information: " + fieldname)
  field.focus();
  return false;
}

function _check_checked ( fieldname, field ) {
  for (i=0, n=field.length; i<n; i++) {
    if (field[i].checked) {
      var checkvalue = field[i].value;
      break;
    }
  }
  
  if ( checkvalue ) 
  return true;
  alert ("Field has not been properly filled.  Please fill in required information: " + fieldname)
  field[0].focus();
  return false;
}

function updateAction ( form, newaction ) {
	form.hosted_button_id.value = newaction;
}
