//
//  Written by:  Edward Smith, UT Gen Libs - EIPO <macvelli@cs.utexas.edu>
//
//  Function to trim whitespace from both ends of an input string
//    s	- The string to be trimmed
//
function trim(s) {
  var from;
  var to;

  from = s.indexOf(' ');
  if (from == 0) {
    from++;
    to = s.indexOf(' ', from);
    while ((to - from) == 0) {
      from++;
      to = s.indexOf(' ', from);
    }
    if (to == -1)
      to = s.length;
  } else if (from > 0) {
    var last = s.indexOf(' ', from+1);
    if ((last-from) == 1) {
      to = from;
      from = 0;
    } else
      return s;
  } else
    return s;

  return (s.substring(from, to));
}

//
//  Function to check input on large registration form
//
function okLargeInput(e) {
  var ok = false;

  e[0].value = trim(e[0].value);
  if (e[0].value.length == 0) {
    alert("Please enter your first name");
    return false;
  }

  e[1].value = trim(e[1].value);
  if (e[1].value.length == 0) {
    alert("Please enter your last name");
    return false;
  }

  e[2].value = trim(e[2].value);
  if (e[2].value.length == 0) {
    alert("Please enter your email address or \"None\"");
    return false;
  }

  if (e[3].selectedIndex == 0) {
    alert("Please select a university");
    return false;
  }

  for (i = 4; i < 10; i++)
    ok = ok || e[i].checked;

  if (!ok) {
    alert("Please select a value for Year in School");
    return false;
  }

  if (e[10].selectedIndex == 0) {
    alert("Please select a birth month");
    return false;
  }

  if (e[11].selectedIndex == 0) {
    alert("Please select a birth day");
    return false;
  }

  return true;
}

//
//  Function to check input on small registration form
//
function okSmallInput(e) {
  var ok = false;

  e[0].value = trim(e[0].value);
  if (e[0].value.length == 0) {
    alert("Please enter your first name");
    return false;
  }

  e[1].value = trim(e[1].value);
  if (e[1].value.length == 0) {
    alert("Please enter your last name");
    return false;
  }

  if (e[2].selectedIndex == 0) {
    alert("Please select a birth month");
    return false;
  }

  if (e[3].selectedIndex == 0) {
    alert("Please select a birth day");
    return false;
  }

  return true;
}

//
//  Function to register from long registration form
//    f         - Submitted form
//
function largeReg(f) {
  var month;
  var day;

  if (!okLargeInput(f.elements))
    return;

  month = f.month.options[f.month.selectedIndex].value;
  day = f.day.options[f.day.selectedIndex].value;

  f.submit();
}

//
//  Function to register from short registration form
//    f		- Submitted form
//
function shortReg(f) {
  var month;
  var day;
  var uid;

  if (!okSmallInput(f.elements))
    return;

  month = f.month.options[f.month.selectedIndex].value;
  day = f.day.options[f.day.selectedIndex].value;

  f.submit();
}


