function validateLoginForm()
{
  var usr = checkText(document.forms[0].user,'userErr');
  var pswd = checkText(document.forms[0].passwd,'passwdErr');
  if (!usr || !pswd)
  {
    alert("Username/Password required.");
    return false;
  }
  return checkRadios()
}

function validateRegForm()
{
  var el = document.forms[0].elements;
  for (var i = 0; i < el.length; ++i)
  {
    if (el[i].type == "text" || el[i].type == "password")
    {
      var ret = checkText(el[i], el[i].name + "Err");
      if ( !ret )
      {
	alert("Required field left empty");
        if(el[i].focus)
          el[i].focus();
	return false;
      }
    }
  }

  var mail = document.forms[0].email;
  var emailformat = /^.+\@.+\..+$/;
  if ( !emailformat.test(mail.value) )
  {
    alert("Email address is not valid");
    var mailval = mail.value;
    mail.value = "";
    checkText(mail, mail.name + "Err");
    mail.value = mailval;
    if (mail.focus)
      mail.focus();

    return false;
  }
  
  var pass1 = document.forms[0].passwd1;
  var pass2 = document.forms[0].passwd2;
  if (pass1.value !== pass2.value)
  {
    alert("Passwords do not match");
    pass1.value = "";
    pass2.value = "";
    checkText(pass1, pass1.name + "Err"); 
    checkText(pass2, pass2.name + "Err");
    if (pass1.focus)
      pass1.focus();
    
    return false;
  }

  return true;
}

function checkRadios()
{
  var el = document.forms[0].elements;
  for(var i = 0 ; i < el.length ; ++i)
  {
    if(el[i].type == "radio")
    {
      var radiogroup = el[el[i].name]; // get the whole set of radio buttons.
      var itemchecked = false;
      for(var j = 0 ; j < radiogroup.length ; ++j)
      {
        if(radiogroup[j].checked)
        {
          itemchecked = true;
          break;
        }
      }
      if(!itemchecked)
      { 
        var vs = el[i].name + "vs";
        var elem = document.getElementById(vs);
        elem.className="vsmissing";
        alert("Please make a pick for the " + radiogroup[0].value.toUpperCase() + " vs. " + radiogroup[1].value.toUpperCase() + " game.");
        if(el[i].focus)
          el[i].focus();
        return false;
      }
    }
  }
  return true;
} 

function checkText(txt,msgid)
{
  var emptyString = /^\s*$/;
  var msgEl = document.getElementById(msgid);
  if (emptyString.test(txt.value))
  {
    msgEl.firstChild.nodeValue = "Required";
    return false;
  }
  else
  {
    msgEl.firstChild.nodeValue = "";
  }
  return true;
}

function clearErr(id)
{
  document.getElementById(id).className="vsnormal";
}

function loginTable(id)
{
  var content = document.getElementById('hiddenLogin').innerHTML;
  document.getElementById(id).innerHTML = content;
}
function regTable(id)
{
  var content = document.getElementById('hiddenReg').innerHTML;
  document.getElementById(id).innerHTML = content;
}
function picksTable(id)
{
  var content = document.getElementById('hiddenPicks').innerHTML;
  document.getElementById(id).innerHTML = content;
}

