/*Functions used to check whether a username is taken
 check this before the form is submitted...very cool */
function checkUsername() {
    var username=document.register.username.value;
    xmlHttp=ajaxFunction();
    if (xmlHttp==null) {
        return;
    }
    var url = "ajax.lookup.php?type=username&username="+username;
    xmlHttp.onreadystatechange=stateChanged;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);


}
function stateChanged() {

    if((xmlHttp.readyState==4)&&(xmlHttp.status == 200)) {
        var xmlDoc=xmlHttp.responseXML.documentElement; //set xmlDoc as the XML document that was returned
        var returnedUsername = xmlDoc.getElementsByTagName('username')[0].childNodes[0].nodeValue; //set this as the first username that was called
        var formusername = document.getElementById('formusername'); //set this as the node to put the text into
        var enteredUserName = document.register.username.value;
        if(returnedUsername == enteredUserName)
        { //check to see if the usernames are equal
            formusername.style.color = "#FF0000";
            formusername.firstChild.nodeValue = 'Username Taken';
            alert("The Username is already in use. Please enter a valid username before continuing.");
        }
        else
        {
            //run more error checking
            var check = true;
            var illegalChars = /\W/;
            if (illegalChars.test(enteredUserName)) {
                check=false;
            }
            if(check)
            {
                formusername.style.color = "#333333";
                formusername.firstChild.nodeValue = "Username Acceptable";
            }
            else
            {
                formusername.style.color = "#FF0000";
                formusername.firstChild.nodeValue = "Invalid Username";
                alert("The Username is invalid. The username cannot contain any white space characters such as a space or a tab.");
            }
        }
    }
}
