﻿// checkavailability.js
// A means of checking the availability of a username without needing to reload the page

var ajax;

function checkUserName() {
    // Attempt to retrieve an AJAX object. If the attempt fails then try an alternative.
    if(!getAJAX()) {
        alert("Sorry, your browser doesn't support AJAX. Your username will be checked when you submit the form.");
        return;
    }
    
    var username=getUsernameField().value;
    
    if(!username)
        return;
    
    ajax.onreadystatechange=usernameCheckResult;
    ajax.open("GET","checkavailability.aspx?username="+username,true);
    ajax.send(null);
}

// Retrieve the username field from the document
function getUsernameField() {
    var textfields=document.getElementsByTagName("input");
    var username;
    for(var i=0;i<textfields.length;i++) {
        if((textfields[i].name.indexOf("txtUsername")!=-1 || textfields[i].id.indexOf("txtUsername")!=-1) && textfields[i].type=="text") {
            return textfields[i];
        }
    }
}

// Function called when a result is available from the ajax object
function usernameCheckResult() {
    if(ajax.readyState!=4)
        return;
    
    if(req.status!=200) {
        alert("Error checking username! (non-200 status) Your username will be checked when you submit the form.");
        return;
    }
    
    var result=ajax.responseXML;
    if(!result) {
        alert("Error checking username! (malformed response) Your username will be checked when you submit the form.");
        return;
    }
    
    var response=result.getElementsByTagName("available");
    if(!response[0]) {
        alert("Error checking username! (malformed document at node 'available') Your username will be checked when you submit the form.");
        return;
    }
    
    switch(response[0].getAttribute('status')) {
        case "true":
            document.getElementById("usernameAvailable").style.display="inline";
            document.getElementById("usernameUnavailable").style.display="none";
            getUsernameField().className="";
            break;
        case "false":
            document.getElementById("usernameAvailable").style.display="none";
            document.getElementById("usernameUnavailable").style.display="inline";
            getUsernameField().className="form_error";
            break;
        default:
            alert("Error checking username! (malformed document) Your username will be checked when you submit the form.");
            return;
            break;
    }
}

// Get an AJAX request object
// Taken from http://developer.apple.com/internet/webcontent/xmlhttpreq.html
function getAJAX() {
    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest) {
    	try {
			req = new XMLHttpRequest();
        } catch(e) {
			req = false;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
        	req = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		req = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		req = false;
        	}
		}
    }
	ajax=req;
	
	// And the vertict is...
	if(!req)
	    return false;
	return true;
}