
function validateUsername( fld ) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
	 
    if (fld.value == "") {
     	fld.style.background = "yellow";
        error = "Username required.";
    } else if ((fld.value.length < 5) || (fld.value.length > 12)) {
        fld.style.background = 'yellow';
        error = "The username is the wrong length (5 to 12)).";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'yellow';
        error = "The username contains illegal characters.";
    } else {
        fld.style.background = 'White';
    } 

    return error;
}

function validatePassword(fld) {
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers 
 
    if (fld.value == "") {
        fld.style.background = 'yellow';
        error = "Password required.";
    } else if ((fld.value.length < 7) || (fld.value.length > 12)) {
        error = "Password length 7-12 characters.";
        fld.style.background = 'yellow';
    } else if (illegalChars.test(fld.value)) {
        error = "Password contains illegal characters.";
        fld.style.background = 'yellow';
    } else if (!((fld.value.search(/(a-z)+/)) && (fld.value.search(/(0-9)+/)))) {
        error = "Password needs at least one numeral.";
        fld.style.background = 'yellow';
    } else {
        fld.style.background = 'white';
    }
   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 = 'yellow';
        error = "Email address required.";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'yellow';
        error = "Please enter a valid email address.";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'yellow';
        error = "The email address contains illegal characters.";
    } else {
        fld.style.background = 'white';
    }
    return error;
}

function validate_form() {
  // init error message
  var errors = "";
  var holder = document.getElementById('errormessages');
  
  // check username
  var vun = validateUsername( document.getElementById('usr') );
  if ( vun.length > 0 ) errors = errors + "<li>" + vun;

  // check password
  var vpw = validatePassword( document.getElementById('pwd') );
  if ( vpw.length > 0 ) errors = errors + "<li>" + vpw;

  // check email
  var vel = validateEmail( document.getElementById('eml') );
  if ( vel.length > 0 ) errors = errors + "<li>" + vel;

  var joinparams = "?usr=" + document.getElementById('usr').value +
                   "&pwd=" + document.getElementById('pwd').value +
				   "&eml=" + document.getElementById('eml').value;
				   

  // no errors, we're done
  holder.style.background = "";
  if ( errors.length == 0 )
  {
	// finalize jopin process
	ahah( 'join.php'+joinparams, 'ajaxresponses' );
  	setTimeout( 'joinCompleted()', 1000 );
  	return true;
  } 

  // if we have errors, display
  holder.innerHTML = "<div style='text-align:left; margin: 5px; color:#FFF;'>Please Check:<ul>" + errors + "</ul></div>";
  holder.style.background = "red";
  
  // return error status
  return false;
}

function joinCompleted()
{
  var validate = clearAjax();
  
  if ( validate == 'OK' )
  {
	alert("Your account was created");
	location.href = "?challenge="+randomString( 25 );
  }
  else if ( validate != '' )
  {
	var holder = document.getElementById('errormessages');
    holder.innerHTML = "<div style='text-align:left; margin: 5px; color:#FFF;'>" + validate +"</div>";
    holder.style.background = "red";
  }
  else
  {
	setTimeout( 'joinCompleted()', 250 );
  }
}

function randomString( string_length ) {
	var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
	var randomstring = '';
	for (var i=0; i<string_length; i++) {
		var rnum = Math.floor(Math.random() * chars.length);
		randomstring += chars.substring(rnum,rnum+1);
	}
	return randomstring;
}

