function validateFormOnSubmit(theForm) {
var reason = "";

  reason += validateEmail(theForm.email);
  reason += validateEmpty(theForm.realname);

  if (reason != "") {
  	document.getElementById("errorText").innerHTML = reason;
    return false;
  }
  document.getElementById('errorText').innerHTML = '';

  return true;
}

function validateEmpty(fld) {
    var error = "";

    if (fld.value.length == 0) {
        fld.style.background = '#eee';
        error = "<p>A required field has not been filled in.</p>"
    } else {
        fld.style.background = '#fff';
    }
    return error;
}

function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}

function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;

    if (fld.value == "") {
        fld.style.background = '#eee';
        error = "<p>Email is a required field.</p>";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = '#eee';
        error = "<p>Please enter a valid email address.</p>";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = '#eee';
        error = "<p>The email address contains illegal characters.</p>";
    } else {
        fld.style.background = '#fff';
    }
    return error;
}

function hideElement(p_id) {
	if (document.getElementById(p_id).className != "hideElement") {
		document.getElementById(p_id).className = "hideElement";
	}
}

function showElement(p_id) {
	if (document.getElementById(p_id).className != "showElement") {
		document.getElementById(p_id).className = "showElement";
	}
}
