/******************************************************************************
 * Functions for forms
 ******************************************************************************/

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}


function btnOn() {
	/* changes the color of a button's
	 * background-color when attached
	 * to a mouseover event */
	this.style.backgroundColor="#D89918";
} //end btnOn()

function btnOff() {
	/* returns the button's background-color
	 * back to white */
	this.style.backgroundColor="#ffffff";
} //end btnOff()

function initBtn() {
	/* onload event to attach btnOn()
	 * and btnOff() to butons */
	var aBtn = document.getElementsByTagName("button");
	for(var i=0;i<aBtn.length;i++){
		aBtn[i].onmouseover = btnOn;
		aBtn[i].onmouseout = btnOff;
	} // end for loop
}

addLoadEvent(initBtn);

function validate(formElm) {
	/* Checks the required fields for
	 * data. If a field is empty, an alert
	 * notifies the visitor, highlights the field,
	 * and gives the field focus */
	var name = document.getElementById("name");
	var eventname = document.getElementById("eventname");
	var telephone = document.getElementById("telephone");
	var emailaddress = document.getElementById("emailaddress");
	var msg = document.getElementById("msg");
	var aFields = [name, eventname, telephone, emailaddress, msg];
	
	// reset fields
	for(var j=0;j<aFields.length;j++){
		aFields[j].className = "";
	} // end for loop
	
	// loop through fields. First field with no data
	// give it the class of error, add focus,
	// and alert the visitor
	for ( var i = 0; i < aFields.length; i++) {
		var this_field = aFields[i];
		if (this_field.value == "" || this_field.value == null) { 
		
			this_field.className = "error";
			this_field.focus();
			//var label = this_field.parentNode.childNodes[1];
			//var label = this_field.parentNode.getElementsByTagName("LABEL")[0];
			var label = (this_field.getAttribute("id")=="emailaddress")?"email address":this_field.getAttribute("id");
			alert( "Please add your " + label + ", it's required.");
			return false;
		}
	} // end for loop
	
	// check email address
	var email = document.getElementById("emailaddress");
	if (email.value != "" || email.value != null) {
		if (email.value.indexOf("@") == -1 || email.value.indexOf(".") == -1) {
    		email.focus();
    		email.className = "error";
    		alert("Not a valid email address.");
    		return false;
  		}
  	}
	
	
	return true;
	
} // end validate()



function fix() {
	/* when the form is reset any fields with
	 * class error are removed */
	var n = document.getElementById("required");
	var a = n.getElementsByTagName("input");
	for(var j=0;j<a.length;j++){
		a[j].className = "";
	} // end for loop
	
} // end reset()