function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function prepareInputsForHints() {
	var inputs = document.getElementsByTagName("input");
	for (var i=0; i<inputs.length; i++){
		// test to see if the hint span exists first
		if (inputs[i].parentNode.getElementsByTagName("span")[0]) {
			// the span exists!  on focus, show the hint
			inputs[i].onfocus = function () {
				this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
			}
			// when the cursor moves away from the field, hide the hint
			inputs[i].onblur = function () {
				this.parentNode.getElementsByTagName("span")[0].style.display = "none";
			}
		}
	}
	// repeat the same tests as above for selects
	var selects = document.getElementsByTagName("select");
	for (var k=0; k<selects.length; k++){
		if (selects[k].parentNode.getElementsByTagName("span")[0]) {
			selects[k].onfocus = function () {
				this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
			}
			selects[k].onblur = function () {
				this.parentNode.getElementsByTagName("span")[0].style.display = "none";
			}
		}
	}
}
addLoadEvent(prepareInputsForHints);function validateFormOnSubmit(theForm) {
	var reason = "";

	reason += validateEmpty(theForm.code, "Code Word");
	reason += validateEmpty(theForm.firstname, "First Name");
	reason += validateEmpty(theForm.lastname, "Last Name");
	reason += validateEmpty(theForm.company, "Company Name");
	reason += emailval(theForm,theForm.email.value);
	
	if (reason != "") {
		alert("Some fields need correction:\n\n" + reason);
		return false;
	}
	
	return true;
}

function emailval(theForm,email) {
	var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
	var regex = new RegExp(emailReg);
	if (regex.test(email)) {
		theForm.email.style.background = 'White'; 
		return '';
	} else {
		theForm.email.style.background = '#FDFF00'; 
		return "The Email Address field is not valid.\n";
	}
}

function validateEmpty(fld, fldname) {
	var error = "";
  
	if (fld.value.length == 0) {
		fld.style.background = '#FDFF00'; 
		error = "The " + fldname + " field has not been filled in.\n";
	} else {
		fld.style.background = 'White';
	}
	return error;   
}