function login() {
   var userName = document.getElementById("userName");
   if (userName.value == null) {
       alert("Please enter your email and password");
       return;
   }
  
   userName.value = userName.value.trim();
   if (userName.value == "" || document.getElementById("password").value == "" ) {
	   alert("Please enter your email and password");
	   return;
   }
   document.getElementById("loginBean").submit();
}

String.prototype.trim = function () {
    var str = this.replace(/^\s\s*/, ''),
        ws = /\s/,
        i = str.length;
    while (ws.test(str.charAt(--i)));
    return str.slice(0, i + 1);
};

function testNonEmpty(obj, checkId) {
    obj.value = obj.value.trim();
    if (obj.value == null || obj.value == "") {
        error(checkId);
    } else {
      ok(checkId);
    }
}

var errors = new Object;
function error(checkId) {
   errors[checkId] = true;
   document.getElementById(checkId + "Error").style.display = 'block';
   document.getElementById(checkId + "Checked").style.display = 'none';
}

function ok(checkId) {
   errors[checkId] = false;
   document.getElementById(checkId + "Error").style.display = 'none';
   document.getElementById(checkId + "Checked").style.display = 'block';
}

// see http://www.regular-expressions.info/email.html for details
var emailFilter = new RegExp("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$", "i");
var firstEmail = "";
function checkEmail(email) {
   return email == null ? false : emailFilter.test(email.trim());
}

function testEmail(email, checkId) {
  email.value = email.value.trim();
  if (checkEmail(email.value)) {
     ok(checkId);
     firstEmail = email.value.trim();
  } else {
     error(checkId);
  }
}

function testRemail(email, checkId) {
  email.value = email.value.trim();
  if (checkEmail(email.value)) {
     if (firstEmail == email.value.trim()) {
        ok(checkId);
        return;
     }
  }
  error(checkId);
}

function testAll() {
// validate may be called before blur event so make sure we are not catching bogus error
  testNonEmpty(document.getElementById("firstName"), 'firstName');
  testNonEmpty(document.getElementById("lastName"), 'lastName');
  testEmail(document.getElementById("email"), 'email');
  testRemail(document.getElementById("reEmail"), 'remail');
}

function validateAndSubmit(formObj) {
    var count = 0;
    testAll();
    for (var entry in errors) {
       ++count;
       if (errors[entry]) {
          alert("Please fix the errors before continuing!");
          return false;
       }
    }
    if (count != 4) {
       alert("Please provide data in all required fields before continuing!");
       return false;
    }
    formObj.submit();
    return true;
}