/**
 *Javascript validation for login form
 *rqNotEmpty function, rqUsernameCheck, rqPasswordCheck and checkAllFields for the 2 required fields
 *
  *Author - Alex Pape
 *
 *Last Modified 15/04/09
*/

//function to check if field has been completed
function rqNotEmpty(entered,fieldName)
{
  // if the length of the data entered == 0 then display error message, otherwise use success message
  if(entered.length==0)
  {
    document.getElementById(fieldName + '_msg').innerHTML='<span style="color:#000;">This field is required</span>';
    document.getElementById(fieldName + '_msg').className="f_msg";
  }
  
  else
  {
    document.getElementById(fieldName + '_msg').innerHTML='<span style="color:#FFF;"></span>';
    document.getElementById(fieldName + '_msg').className="s_msg";
  }
  return true
}//end rqNotEmpty function

//function to check if the username has been completed correctly
function rqUsernameCheck(entered,fieldName)
{
  // if the length of the data entered == 0 then run through rqNotEmpty
  if(entered.length==0)
  {
    rqNotEmpty(entered,fieldName);
  }
  //otherwise check entered value against reg exp
  else
  {
    //reg exp to test for letters or numbers between 6-15 characters
     var regex = /^[a-zA-Z0-9_]{6,15}$/;
      //if test fails display error message
      if (!regex.test(entered))
      {
        document.getElementById(fieldName + '_msg').innerHTML='<span style="color:#970b0b;">Username must be between 6-15 characters and contain no symbols</span>';
        document.getElementById(fieldName + '_msg').className="f_msg";
      }
      else
      {
      document.getElementById(fieldName + '_msg').innerHTML='<span style="color:#fff;"></span>';
      document.getElementById(fieldName + '_msg').className="s_msg";
      return true;
      }
  }
}

//function to check if the password has been completed correctly
function rqPasswordCheck(entered,fieldName)
{
  // if the length of the data entered == 0 then run through rqNotEmpty
  if(entered.length == 0)
  {
   rqNotEmpty(entered,fieldName);
  }
   //otherwise check entered value against reg exp
  else
  {
     //reg exp to test for at least 1 letter, 1 number and between 6-15 characters
     var regex = /(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{6,15})$/;
    //if test fails display error message
      if (!regex.test(entered))
      {
        document.getElementById(fieldName + '_msg').innerHTML='<span style="color:#970b0b;">Password must be between 6-15 characters and contain 1 letter and 1 number</span>';
        document.getElementById(fieldName + '_msg').className="f_msg";
      }
      else
      {
      document.getElementById(fieldName + '_msg').innerHTML='<span style="color:#fff;"></span>';
      document.getElementById(fieldName + '_msg').className="s_msg";
      return true;
      }
  }
}

//function to disable the submit button until all fields have been completed correctly
function checkAllFields(fieldName)
{
	// create a shortened variable fro "s_msg" (success message)
	var ag = "s_msg"; //look in the big IF statement, this just shortens the code
	
	// create a variable called "failed" that will store the class name of the element we are working on.
	var failed = document.getElementById(fieldName + '_msg').className;

	// create variables of all the class names that are in our form regarding error/success messages.
	var f1 = document.getElementById('uname_msg').className;
        //var f2 = document.getElementById('password_msg').className;

	// if our failed variable is set to "f_msg" then our validation must have failed somewhere so diable the submit button
	if (failed == "f_msg")
	{
		document.getElementById('submit').disabled="disabled";
	}
	else //  else run the following last check
	{
		// if all our variables (class names) are equal to our variable "ag" which is "s_msg" (success message) then enable the submit button
		if (f1 == ag)
		{
			document.getElementById('submit').disabled="";
		}
		else // else keep the button disabled.
		{
			document.getElementById('submit').disabled="disabled";
		}
	}
}

