//		project: ajax loginhandler
//		version: 1.0
//		notes: this is a test release, so if you encounter any stupidities or bugs, please let me know

// 		blog: All About The Web - http://mysticearth.wordpress.com
//		email: ewinkelman@hotmail.com

//		feel free to use this code in personal and commercial projects, only if you keep this comments in the source 
// 		(c)	copyright 2007 Ewout Winkelman

var xmlHttp;

// call this function by createXMLHttpRequest();
function createXMLHttpRequest() {
	try {
		// Firefox, Opera 8.0+, Safari  
		xmlHttp=new XMLHttpRequest();  
	}
	catch (e) {  
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
  		catch (e) {    
  			try {
  				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  			}
    		catch (e){
    			alert("Your browser does not support AJAX!");
    			return false;
    		}
  		}
	}
}

//
// CONSUMENTEN LOGIN
//
function sendConsumentRequest(url) {	
	// create the xmlhttprequest object
	createXMLHttpRequest();
	
	// url is required
	// inputdata will be send to this url
	if(url == "" || url == null) {
		alert("No URL given, please check your sendRequest() statement");
	}
	
	// block the submit button
	document.getElementById("loginbutton").value = 'Bezig...';
	document.getElementById("loginbutton").disabled = true;
	
	
	// get the data the user entered
	var pasnummer = document.getElementById("pasnummer").value;
	var postcode_cijfers = document.getElementById("postcode_cijfers").value;
	var postcode_letters = document.getElementById("postcode_letters").value;
	var huisnr = document.getElementById("huisnr").value;
	
	// create the variablechain for use in our php-script
	var vars = 'pasnummer=' + pasnummer + '&postcode_cijfers=' + postcode_cijfers + '&postcode_letters=' + postcode_letters + '&huisnr=' + huisnr;
	
	// open a connect, but nothing will be send at the moment!
	xmlHttp.open("POST", url, true);
	
	// declare the "onreadystatechange"; in other words: when xmlhttp is sending a request the function (consumentLoginCallBack) will be called
	// to handle the status
	xmlHttp.onreadystatechange = consumentLoginCallBack;
	
	// this content-type is only needed when sending through POST.
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	
	// now we're actually sending something to the url
	xmlHttp.send(vars);
}

//
// ONDERNEMERS LOGIN
//
function sendOndernemerRequest(url) {	
	// create the xmlhttprequest object
	createXMLHttpRequest();
	
	// url is required
	// inputdata will be send to this url
	if(url == "" || url == null) {
		alert("No URL given, please check your sendRequest() statement");
	}
	
	// block the submit button
	document.getElementById("loginbutton").value = 'Bezig...';
	document.getElementById("loginbutton").disabled = true;
	
	// get the data the user entered
	var username = document.getElementById("username").value;
	var password = document.getElementById("password").value;
	
	// create the variablechain for use in our php-script
	var vars = 'username=' + username + '&password=' + password;

	// open a connect, but nothing will be send at the moment!
	xmlHttp.open("POST", url, true);
	
	// declare the "onreadystatechange"; in other words: when xmlhttp is sending a request the function (ondernemerLoginCallBack) will be called
	// to handle the status
	xmlHttp.onreadystatechange = ondernemerLoginCallBack;
	
	// this content-type is only needed when sending through POST.
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	
	// now we're actually sending something to the url
	xmlHttp.send(vars);
}

//
// VERENIGING LOGIN
//
function sendVerenigingRequest(url) {	
	// create the xmlhttprequest object
	createXMLHttpRequest();
	
	// url is required
	// inputdata will be send to this url
	if(url == "" || url == null) {
		alert("No URL given, please check your sendRequest() statement");
	}
	
	// block the submit button
	document.getElementById("loginbutton").value = 'Bezig...';
	document.getElementById("loginbutton").disabled = true;
	
	// get the data the user entered
	var username = document.getElementById("username").value;
	var password = document.getElementById("password").value;
	
	// create the variablechain for use in our php-script
	var vars = 'username=' + username + '&password=' + password;

	// open a connect, but nothing will be send at the moment!
	xmlHttp.open("POST", url, true);
	
	// declare the "onreadystatechange"; in other words: when xmlhttp is sending a request the function (verenigingLoginCallBack) will be called
	// to handle the status
	xmlHttp.onreadystatechange = verenigingLoginCallBack;
	
	// this content-type is only needed when sending through POST.
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	
	// now we're actually sending something to the url
	xmlHttp.send(vars);
}

function consumentLoginCallBack() {
	// if xmlhttp returned that everything went ok
	if(xmlHttp.readyState == 4) {

		// status = 200 = file found and ok!
		// just a default http "errorcode"
		if(xmlHttp.status == 200) {
			/*
				Wanneer de pagina wordt gevonden en alles ok is, doe dan het volgende:
				Hier kun je dus je eigen functies toevoegen
			*/
			if(xmlHttp.responseText == 'Ja') {
				alert("U bent succesvol ingelogd.");
				location.href="http://oisterwijk.positoos.nl/consument/mijnpositoos";
			}
			else {
				document.getElementById("loginbutton").value = 'Inloggen';
				document.getElementById("loginbutton").disabled = false;
				alert(xmlHttp.responseText);
			}
		}
		else {
			alert("Er is iets misgegaan: "+xmlHttp.status);
		}
	}
}

function ondernemerLoginCallBack() {
	// if xmlhttp returned that everything went ok
	if(xmlHttp.readyState == 4) {

		// status = 200 = file found and ok!
		// just a default http "errorcode"
		if(xmlHttp.status == 200) {
			/*
				Wanneer de pagina wordt gevonden en alles ok is, doe dan het volgende:
				Hier kun je dus je eigen functies toevoegen
			*/
			if(xmlHttp.responseText == 'Ja') {
				alert("U bent succesvol ingelogd.");
				location.href="http://oisterwijk.positoos.nl/ondernemer/mijnpositoos";
			}
			else {
				document.getElementById("loginbutton").value = 'Inloggen';
				document.getElementById("loginbutton").disabled = false;
				alert(xmlHttp.responseText);
			}
		}
		else {
			alert("Er is iets misgegaan: "+xmlHttp.status);
		}
	}
}

function verenigingLoginCallBack() {
	// if xmlhttp returned that everything went ok
	if(xmlHttp.readyState == 4) {

		// status = 200 = file found and ok!
		// just a default http "errorcode"
		if(xmlHttp.status == 200) {
			/*
				Wanneer de pagina wordt gevonden en alles ok is, doe dan het volgende:
				Hier kun je dus je eigen functies toevoegen
			*/
			if(xmlHttp.responseText == 'Ja') {
				alert("U bent succesvol ingelogd.");
				location.href="http://oisterwijk.positoos.nl/vereniging/mijnpositoos";
			}
			else {
				document.getElementById("loginbutton").value = 'Inloggen';
				document.getElementById("loginbutton").disabled = false;
				alert(xmlHttp.responseText);
			}
		}
		else {
			alert("Er is iets misgegaan: "+xmlHttp.status);
		}
	}
}
var GOOGLEMAPSURL = '';
function sendOndernemersListRequest(url, projectcode, mapsurl) {	
	GOOGLEMAPSURL = mapsurl;
	
	// if projectcode is chosen
	if(projectcode != 0) {
		// create the xmlhttprequest object
		createXMLHttpRequest();
		
		// url is required
		// inputdata will be send to this url
		if(url == "" || url == null) {
			alert("No URL given, please check your sendOndernemersListRequest() statement");
		}
		// open a connect, but nothing will be send at the moment!
		xmlHttp.open("GET", url+'?getOndernemersLijstBijProjectcode='+projectcode, true);
		
		// declare the "onreadystatechange"; in other words: when xmlhttp is sending a request the function (loginCallBack) will be called
		// to handle the status
		xmlHttp.onreadystatechange = callbackOndernemersListRequest;
		
		xmlHttp.send(null);
	}
	else {
		// empty list and google maps
		document.getElementById('ondernemersListDiv').innerHTML="";
		document.getElementById('googlemapsDiv').innerHTML="";
	}
}
function callbackOndernemersListRequest() {
	// if xmlhttp returned that everything went ok
	if(xmlHttp.readyState == 4) {	
		// status = 200 = file found and ok!
		// just a default http "errorcode"
		if(xmlHttp.status == 200) {
			//document.getElementById('googlemapsDiv').innerHTML="";
			/*var gmapsDiv = document.getElementById('googlemapsDiv');
			var newIframe = document.createElement('iframe');
			newIframe.style.display="block";
			newIframe.style.width="801px";
			newIframe.style.height="450px";
			newIframe.style.border="0";
			newIframe.frameBorder="0";
			newIframe.style.margin="0";
			newIframe.style.padding="0";
			newIframe.src=GOOGLEMAPSURL;
			newIframe.id="gmaps";
			newIframe.name="gmaps";
			gmapsDiv.appendChild(newIframe);*/
			document.getElementById('ondernemersListDiv').innerHTML=xmlHttp.responseText;
		}
		else {
			alert("Er is iets misgegaan: "+xmlHttp.status);
		}
	}
}

/* vereniging list inschrijving*/
function sendVerenigingOptionsRequest(url, verenigingofgoededoel, gemeente) {	
	// create the xmlhttprequest object
	createXMLHttpRequest();
	
	// url is required
	// inputdata will be send to this url
	if(url == "" || url == null) {
		alert("No URL given, please check your sendVerenigingOptionsRequest() statement");
	}
	// open a connect, but nothing will be send at the moment!

	if(verenigingofgoededoel == "vereniging") {
		xmlHttp.open("GET", url+'?getVerenigingOptionsRequest='+verenigingofgoededoel+'&gemeente='+gemeente, true);
	}
	else if(verenigingofgoededoel == "goededoel") {
		xmlHttp.open("GET", url+'?getVerenigingOptionsRequest='+verenigingofgoededoel+'&gemeente='+gemeente+'&goededoel=true', true);
	}
	else {
		xmlHttp.open("GET", url+'?getVerenigingOptionsRequest='+verenigingofgoededoel+'&gemeente='+gemeente+'&empty=true', true);	
	}
	
	// declare the "onreadystatechange"; in other words: when xmlhttp is sending a request the function (loginCallBack) will be called
	// to handle the status
	xmlHttp.onreadystatechange = callbackVerenigingOptionsRequest;
	
	xmlHttp.send(null);
}
function callbackVerenigingOptionsRequest() {
	// if xmlhttp returned that everything went ok
	if(xmlHttp.readyState == 4) {	
		// status = 200 = file found and ok!
		// just a default http "errorcode"
		if(xmlHttp.status == 200) {
			document.getElementById('verenigingDiv').innerHTML=xmlHttp.responseText;
		}
		else {
			alert("Er is iets misgegaan: "+xmlHttp.status);
		}
	}
}
