// 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 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 last_element_name = "";

  // 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];

    // Si dos elementos del formulario tienen el mismo nombre se
    // mira el primero y el resto se saltan
    if (e.name != last_element_name)
      last_element_name = e.name;
    else
      continue;

    // cogemos el nombre quitandole el _value
    if (e.desc != null) nombre = e.desc;
    else nombre = e.name;

    if (((e.type == "text") || (e.type == "textarea") || (e.type == "password") || (e.type == "file")) && !e.optional) {
      // first check if the field is empty
      if ((e.value == null) || (e.value == "") || isblank (e.value)) {
        empty_fields += "\n          " + nombre;
        continue;
      }
      // Now check for fields that are supposed to be numeric.
      if (e.numeric || (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 += "- " + nombre + " eremua zenbakia izan behar da, ";
          if (e.min != null)
            errors += e.min + " baino handiagoa eta ";
          if (e.max != null && e.min != null)
            errors += e.max + " baino txikiagoa ";
          else if (e.max != null)
            errors += e.max + " baino txikiagoa ";
          errors += ".\n";
        }
      }
    }

    if ((e.type == "radio") && !e.optional) {
      if (!radioChecker (e.name) && empty_fields.indexOf (e.name) == -1) empty_fields += "\n          " + nombre;
    }
    else if (typeof e.multiple != 'undefined' && ( e.multiple || !e.multiple ) && !e.optional) {
      if (e.selectedIndex < 1 && empty_fields.indexOf (e.name) == -1) empty_fields += "\n          " + nombre;
  }

  }


  // Now, if there were any errors, then 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 += "Ez da eskabidea bidali akats hauengatik:\n";
  msg += "Mesedez, zuzendu eta berriro bidali\n";
  msg += "______________________________________________________\n\n"
  if (empty_fields) {
    msg += "- Eskatzen diren eremu hauek hutsik daude:"
        + empty_fields + "\n";
    if (errors) msg += "\n";
  }
  msg += errors;
  alert (msg);
  return false;
}


function radioChecker (nombre) {
  valores = eval ('document.forms[0].' + nombre);
  for (i = 0; i < valores.length; i++) {
    if (valores[i].checked) return true;
  }
  return false;
}

