var achternaam=0,voornaam=0,email=0,username=0,ok=true,url=0,mode=0,ind=0,form=0;

function empty(input)
{
    return (input == '' || input == null) ? true : false;
}

function GetXmlHttpObject() { 
	var objXMLHttp=null
	if (window.XMLHttpRequest) {
		objXMLHttp=new XMLHttpRequest()
	} else if (window.ActiveXObject) {
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
	}
	return objXMLHttp
}

function indb() {
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null) {
		alert("Your browser doensn't support AJAX!");
	}
	switch (mode) {
		case 'reg':
			url = "checkdb.php?mode=name&voornaam="+voornaam+"&achternaam="+achternaam;
		break;
		case 'fpw':
			url = "checkdb.php?mode=forgetpw&username="+username+"&email="+email;
		break;
		case 'email':
			url = "checkdb.php?mode=email&email="+email;
		break;
		case 'usrnm':
			url = "checkdb.php?mode=usrnm&username="+username;
		break;
	}
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}

function stateChanged() {
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
		xmlDoc=xmlHttp.responseXML;
		ind = xmlDoc.getElementsByTagName("indb")[0].childNodes[0].nodeValue;
		switch (mode) {
			case 'reg':
				if(ind == '1') {
					ok = true; mode='email'; indb();
				} else if(ind == '2') {
					ok = false; alert("'"+voornaam+" "+achternaam+"' is al geregistreerd");
				} else {
					ok = false; alert("'"+voornaam+" "+achternaam+"' komt niet voor in de database");
				}
			break;
			case 'fpw':
				if(ind == '1') {
					ok = true; sendform("forgetpw");
				} else {
					ok = false; alert("Gebruikersnaam '"+username+"' met emailadres '"+email+"' komt niet voor in de database");
				}
			break;
			case 'email':
				if(ind == '0') {
					ok = true; mode='usrnm'; indb();
				} else {
					ok = false; alert("Emailadres '"+email+"' is al geregistreerd door een ander account");
				}
			break;
			case 'usrnm':
				if(ind == '0') {
					ok = true; sendform("register");
				} else {
					ok = false; alert("'"+username+"' is al in gebruik");
				}
			break;
		}
	}
}	

function checkRegForm() {
	email = document.forms["register"].elements["email"].value;
	voornaam = document.forms["register"].elements["voornaam"].value;
	achternaam = document.forms["register"].elements["achternaam"].value;
	username = document.forms["register"].elements["username"].value;
	ok = true;	mode='reg'; form='register';
	if(ok && empty(voornaam)) {
		alert("Vul aub je voornaam in");
		ok = false;
	}
	if(ok && empty(achternaam)) {
		alert("Vul aub je achternaam in");
		ok = false;
	}
	if(ok && empty(email)) {
		alert("Vul aub je emailadres in");
		ok = false;
	}
	if(ok && empty(username)) {
		alert("Vul aub je gewenste gebruikersnaam in");
		ok = false;
	}
	if(ok && !email.match(/([a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4})/i)) {
		alert("Vul een aub een correct emailadres in");
		ok = false;
	}
	if(ok) {
		indb();
	}
}

function checkFPWForm() {
	email = document.forms["forgetpw"].elements["email"].value;
	username = document.forms["forgetpw"].elements["username"].value;
	ok = true;	mode='fpw'; form='forgetpw'
	if(ok && empty(username)) {
		alert("Vul aub je gebruikersnaam in");
		ok = false;
	}
	if(ok && empty(email)) {
		alert("Vul aub je emailadres in");
		ok = false;
	}
	if(ok && !email.match(/([a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4})/i)) {
		alert("Vul een aub een correct emailadres in");
		ok = false;
	}
	if(ok) {	
		indb();
	}
}

function checkVoteForm() {
//	ok = true; form='vote'; dfe = document.forms["vote"].elements;
//	if(ok && (empty(dfe["cat1"].value) || parseInt(dfe["cat1"].value)<1 || parseInt(dfe["cat1"].value) > 6)) {
//		alert("De eerste categorie is niet (goed) ingevuld");
//		ok=false;
//	}
//	if(ok && (empty(dfe["cat2"].value) || parseInt(dfe["cat2"].value)<1 || parseInt(dfe["cat2"].value) > 6)) {
//		alert("De tweede categorie is niet (goed) ingevuld");
//		ok=false;
//	}
//	if(ok && (empty(dfe["cat3"].value) || parseInt(dfe["cat3"].value)<1 || parseInt(dfe["cat3"].value) > 4)) {
//		alert("De derde categorie is niet (goed) ingevuld");
//		ok=false;
//	}
//	if(ok && (empty(dfe["cat4"].value) || parseInt(dfe["cat4"].value)<1 || parseInt(dfe["cat4"].value) > 1)) {
//		alert("De vierde categorie is niet (goed) ingevuld");
//		ok=false;
//	}
//	if(ok && (empty(dfe["cat5"].value) || parseInt(dfe["cat5"].value)<1 || parseInt(dfe["cat5"].value) > 1)) {
//		alert("De vijde categorie is niet (goed) ingevuld");
//		ok=false;
//	}
//	if(ok && (empty(dfe["cat6"].value) || parseInt(dfe["cat6"].value)<1 || parseInt(dfe["cat6"].value) > 3)) {
//		alert("De zesde categorie is niet (goed) ingevuld");
//		ok=false;
//	}
//	if(ok) {
		sendform();
//	}
}	
function checkProfileForm() {
	email = document.forms["profile"].elements["email"].value;
	username = document.forms["profile"].elements["username"].value;
	password = document.forms["profile"].elements["password"].value;
	password2 = document.forms["profile"].elements["password2"].value;
	password3 = document.forms["profile"].elements["password3"].value;
	ok = true; mode='email'; form = 'profile'
	if(ok && empty(password)) {
		alert("Vul aub je huidige wachtwoord in");
		ok = false;
	}
	if(ok && empty(username)) {
		alert("Vul aub je (gewenste) gebruikersnaam in");
		ok = false;
	}
	if(ok && empty(email)) {
		alert("Vul aub je emailadres in");
		ok = false;
	}
	if(ok && !email.match(/([a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4})/i)) {
		alert("Vul een aub een correct emailadres in");
		ok = false;
	}
	if(ok && !empty(password2) && !empty(password3) && password2!=password3) {
		alert("Nieuw wachtwoord en bevestiging zijn niet gelijk");
		ok = false;
	}
	if(ok) {	
		indb();
	}
}

function sendform() {
	document.forms[form].submit();
}
