// JavaScript Document
var SERVER = {
	loaded : false,
	requestobj : false,
	load : function() {
		if( SERVER.loaded == false ) {
			this.loaded = SERVER.loadrequestobj();
		}
		return this.loaded;
	},
	loadrequestobj : function () {
		if (window.XMLHttpRequest){ // if Mozilla, Safari etc or IE7
			this.requestobj = new XMLHttpRequest();
			if (this.requestobj.overrideMimeType) this.requestobj.overrideMimeType('text/xml');
			return true;
		}
		else if (window.ActiveXObject){ // if IE, not IE7
			try {
				this.requestobj = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch (e) {
				try {
					this.requestobj = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch (e) {
					return false;
				}
			}
			return true;
		}
		return false;
	},
	GETrequest : function(url,data,func) {
		if( !this.load() ) return false;
		this.requestobj.open('GET', url+'?'+data, true);
		if( func  ) this.requestobj.onreadystatechange = func;
		this.requestobj.send(null);
		return true;
	},
	POSTrequest : function(url,data,func) {
		if( !this.load() ) return false;
		this.requestobj.open('POST', url, true);
		if( func ) this.requestobj.onreadystatechange = func;
		this.requestobj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		this.requestobj.setRequestHeader("Content-length", data.length);
		this.requestobj.setRequestHeader("Connection", "close");
		this.requestobj.send(data);
		return true;
	},
	Ontvangst : function() {
		if( this.requestobj.readyState != 4 ) return false;
		else if( this.requestobj.status != 200 ) return 'Fout: '+this.requestobj.statusText;
		else return this.requestobj.responseText;
	}

}
function usernamecheck(username) {
	SERVER.POSTrequest('handlers/nameavailable.php','nicknaam=' + username, usernamecheckhandle);
}
function usernamecheckhandle() {
	var data = SERVER.Ontvangst();
	if( data ) { 
		if(data == 0) {
			alert("Gebruikersnaam niet beschikbaar!");
		} else {
			alert("Gebruikersnaam is beschikbaar");
		}
	}
}
