var usererror;

function validate_form(theForm) {
var reason = "";
  reason += validateUsername(theForm.user);
  reason += validatePassword(theForm.pass, theForm.ass);
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

return true;
}

function validateUsername(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
	var username= fld.value
	var xhReq=new XMLHttpRequest()
 	xhReq.open("GET", "signup-checks.php?t=u&s=" + username, false);
 	xhReq.send(null);	
	var serverResponse = xhReq.responseText;
 
    if (fld.value == "") {
        error = "<li>You didn't enter a username.</li>";
    } else if ((fld.value.length < 4) || (fld.value.length > 15)) {
        error = "<li>The username is the wrong length.</li>";
    } else if (illegalChars.test(fld.value)) {
        error = "<li>The username contains illegal characters.</li>";
	} else if (serverResponse == "True") {
		error = "<li>This username is already in use.</li>";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validatePassword(fld1,fld2) {
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers 
 
    if (fld1.value == "") {
        error = "<li>You didn't enter a password.</li>";
    } else if ((fld1.value.length < 7) || (fld1.value.length > 15)) {
        error = "<li>The password is the wrong length.</li>";
    } else if (illegalChars.test(fld1.value)) {
        error = "<li>The password contains illegal characters.</li>";
    } else if (fld1.value!=fld2.value) {
        error = "<li>The password fields do not match up.</li>";
    } else {
        fld1.style.background = 'White';
    }
   return error;
}   

function validateEmail(fld) {
    var error = "";
	var emailAdd= fld.value
	var xhReq=new XMLHttpRequest()
 	xhReq.open("GET", "signup-checks.php?t=e&s=" + emailAdd, false);
 	xhReq.send(null);	
	var serverResponse = xhReq.responseText;
 
    if (fld.value == "") {
        error = "<li>You didn't enter an e-mail address.</li>";
	} else if (serverResponse == "True") {
		error = "<li>This e-mail address already has an account.</li>";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateFirst(fld) {
    var error = "";
    if (fld.value == "") {
        error = "<li>You didn't enter a first name.</li>";
	    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateLast(fld) {
    var error = "";
    if (fld.value == "") {
        error = "<li>You didn't enter a last name.</li>";
	    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateStatus(theForm) {
	if(theForm.status.value==theForm.oldStatus.value){
		alert("This is already your current status.");
		return false;
	}
	return true;
}

function validateFrontpage(theForm){
	var reason = "";
	var errorBox = document.getElementById( "signuperrors" );
	reason += validateUsername(theForm.user);
	reason += validatePassword(theForm.pass, theForm.pass2);
	reason += validateEmail(theForm.email);
	reason += validateFirst(theForm.first);
	reason += validateLast(theForm.last);
	if (reason != "") {
		errorBox.innerHTML = "The following need correction:<ul>" + reason +"</ul>";
		return false;
	}
}


