function prepareForms() {
  for (var i=0; i < document.forms.length; i++) {
    var thisform = document.forms[i];
    thisform.onsubmit = function() {
      return validateForm(this);
    }
  }
	if (document.getElementsByName("committee")[0].checked) {
		document.getElementById("add_committee_div").style.display = 'block';
	} else {
		document.getElementById("add_committee_div").style.display = 'none';
	}
	if (document.getElementsByName("ride_co_ordinator")[0].checked) {
		document.getElementById("add_ride_div").style.display = 'block';
	} else {
		document.getElementById("add_ride_div").style.display = 'none';
	}
	if (document.getElementsByName("admin")[0].checked) {
		document.getElementById("add_admin_div").style.display = 'block';
	} else {
		document.getElementById("add_admin_div").style.display = 'none';
	}
}

function validateForm(whichform) {
  for (var i=0; i < whichform.elements.length; i++) {
    var element = whichform.elements[i];
    if (element.className.indexOf("required") != -1) {
      if (!isFilled(element)) {
        alert("Please fill in the "+element.name+" field.");
        return false;
      }
    }
    if (element.className.indexOf("email") != -1) {
      if (!isEmail(element)) {
        alert("The " +element.name+ " field must be a valid email address.");
        return false;
      }
    }
    if (element.className.indexOf("dropDown") != -1) {
      if (!isChosen(element)) {
        alert("Please fill in the "+element.name+" field.");
        return false;
      }
    } 
  }  
	// extras for CMA
	if (document.getElementsByName("committee")[0].checked) {
		var element = document.getElementsByName("committee_title")[0];
		if (!isFilled(element)) {
			alert("Please fill in the "+element.name+" field.");
        return false;
		}
	}
	if (document.getElementsByName("ride_co_ordinator")[0].checked) {
		var element = document.getElementsByName("area")[0];
		if (!isFilled(element)) {
			alert("Please fill in the "+element.name+" field.");
        return false;
		}
	}
	if (document.getElementsByName("admin")[0].checked) {
		var element = document.getElementsByName("username")[0];
		if (!isFilled(element)) {
			alert("Please fill in the "+element.name+" field.");
        return false;
		}
		var element = document.getElementsByName("password")[0];
		if (!isFilled(element)) {
			alert("Please fill in the "+element.name+" field.");
        return false;
		}
	}
	
  return true;
}

function isFilled(field) {
  if (field.value.length < 1) {
    return false;
  } else {
    return true;
  }
}

function isChosen(field) {
  if (field.value == "choose" || field.value == "Choose") {
    return false;
  } else {
    return true;
  }
}

function isEmail(field) {
  if (field.value == "") {
    return true;
  } else {
    if (field.value.indexOf("@") == -1 || field.value.indexOf(".") == -1) {
      return false;
    } else {
      return true;
    }
  }
}

function toggle(id, button) {
	// id is the id, EG committee_title, of element we are going to hide and show
	// button is the name, EG committee, of the element that will hold the value 
	// checked = true or false.
	if (document.getElementsByName(button)[0].checked) {
		document.getElementById(id).style.display = 'block';
	} else if (!document.getElementsByName(button)[0].checked) {
		document.getElementById(id).style.display = 'none';
	}	
}


addLoadEvent(prepareForms);
    
