var spinner = null;

function startup() {
  if($("vuForms")) {
    $("vuForms").reset();
	spinner = $("statusIndicator");
    spinner.style.visibility = "hidden";
	$("send").disabled = false;
  }
}

/*
 * Gets the element selected by the radio button group or
 * null is no button is selected.
 */
function getRadioGroupSelection(thisForm, radioGroup) {
  var checked = $(thisForm).getInputs('radio', radioGroup).find(
    function(input) {
    	return input.checked;
    }
  );
  return (checked) ? checked : null;
}

function validateForm() {
  var selected;
  var field;
  var pattern;

  // Title Validation
  field = getRadioGroupSelection("vuForms", "title");
  if(field == null) {
    alert('Please indicate the your title.');
  	$("title1").activate();
  	return false;
  }

  // First Name Validation
  field = $F("firstName").strip();
  if(field.length == 0) {
    alert("Please provide your first name.");
    $("firstName").activate();
    return false;
  }
  pattern = /^[a-zA-Z\-\.\s']*$/;
  if(!field.match(pattern)) {
    alert("Please use only spaces, letters, dashes, apostrophes, or periods in your first name.");
    $("firstName").activate();
    return false;
  }
  
  // Last Name Validation
  field = $F("lastName").strip();
  if(field.length == 0) {
    alert("Please provide your last name.");
    $("lastName").activate();
    return false;
  }
  pattern = /^[a-zA-Z\-\.\s']*$/;
  if(!field.match(pattern)) {
    alert("Please use only spaces, letters, dashes, apostrophes, or periods in your last name.");
    $("lastName").activate();
    return false;
  }
  
  // VU Affiliation Validation
  if($("affiliation").selectedIndex == 0) {
    alert("Please select one of the VU affiliation options.");
    $("affiliation").activate();
    return false;
  }
  
  // Email Address Validation
  field = $F("emailAddress").strip();
  if(field.length == 0) {
    alert("Please provide your email address.");
    $("emailAddress").activate();
    return false;
  }
  pattern = /^.*@[\w\-\.]+\.\w+$/;
  if(!field.match(pattern)) {
    alert("Please enter a valid email address.");
    $("emailAddress").activate();
    return false;
  }
  
  // Street Address 1 Validation
  field = $F("homeStreet1").strip();
  if(field.length == 0) {
    alert("Please provide your street address.");
    $("homeStreet1").activate();
    return false;
  }
  pattern = /^[\s\w\-\.#,]*$/;
  if(!field.match(pattern)) {
    alert("Please use only spaces, digits, letters, dashes, commas, periods, or hashes in your street address.");
    $("homeStreet1").activate();
    return false;
  }
  
  // City Validation
  field = $F("homeCity").strip();
  if(field.length == 0) {
    alert("Please provide your city.");
    $("homeCity").activate();
    return false;
  }
  pattern = /^[\s\w\-\.#]*$/;
  if(!field.match(pattern)) {
    alert("Please use only spaces, digits, letters, dashes, periods, or hashes in your city.");
    $("homeCity").activate();
    return false;
  }
  
  // State Validation
  if($("homeState").selectedIndex == 0) {
    alert("Please select one of the state options.");
	$("homeState").activate();
    return false;
  }
  
  // Zip Validation
  field = $F("homeZip").strip();
  if(field.length == 0) {
    alert("Please provide your zip.");
    $("homeZip").activate();
    return false;
  }
  pattern = /^\d{5}(-\d{4})?$/;
  if(!field.match(pattern)) {
    alert("Please indicate your zip in either the 5-digit (nnnnn) or the 9-digit (nnnnn-nnnn) format.");
    $("homeZip").activate();
    return false;
  }
  
  //alert("Would normally submit");
  //return false;
  $("send").disabled = true;
  spinner.style.visibility = "visible";
  return true;
}