var requestType = 'html';
var functions_allowed_to_call = new Array();


function getXmlHttp(requestType) {

	var xmlHttp = null;

        try {
                xmlHttp = new XMLHttpRequest();
        }
        catch (e) {

                try {
                        xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
                }
                catch (e) {

                        try {
                                xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
                        }
                        catch (e) {
                                xmlHttp = false;
                        }

                }

        } 

        if (xmlHttp.overrideMimeType) {

		xmlHttp.overrideMimeType('text/'+requestType);

	}
        return xmlHttp;

}

/**
 * Settings instellen
 */
ajaxRequests = new Array();
function ajaxRequest(page,request) {

        if (page.substr(0,1) == '/') this.page = 'app'+page;
        else this.page = page;

        this.method             = 'GET';
        this.request            = request.replace("'","\'");
        this.form               = '';
        this.nocache            = true;
        this.function_to_call   = '';
        this.container          = '';
        this.output             = 'html'; // 'html', 'xml', '' (geen output return)

        ajaxRequests.push(this);

}

ajaxRequest.prototype.setCached = function() {

        if (this.nocache == undefined || this.nocache == true) this.nocache = '&t='+new Date().getTime();
        else this.nocache = '';

}

ajaxRequest.prototype.setFormRequest = function() {

        if (this.method == 'POST' && this.form != '') this.formRequest = this.buildFormRequest();
        else this.formRequest = 0; // null werkt niet in FF (length required error)

}

ajaxRequest.prototype.process = function() {

	this.setCached();
	this.setFormRequest();
	/**
	 * Geen container opgegeven? Dan XML teruggeven
	 */
	layer = this.container;
	if (this.container == '') this.output = 'xml';
	if (this.formRequest == 0) this.formRequest = null;

	function_to_call = this.function_to_call;
	output = this.output;

	var xmlHttp = getXmlHttp(this.output);

	xmlHttp.open(this.method, this.page+'?'+this.request+''+this.nocache, true);
	xmlHttp.onreadystatechange = function(e) {

		if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {

			if (layer != '' && output != '') {

				if (document.getElementById(layer) == null) alert('Requested container/layer could not be found');
				else document.getElementById(layer).innerHTML = xmlHttp.responseText;
				
				var ajaxloader = document.getElementById("ajaxloader");
				if(ajaxloader) {
					document.getElementById("ajaxloader").style.display='none';
				}
			}
			xmlHttp = null;

			//Controleer of er een functie uitgeroepen moet worden, zo ja dan controleren of die functie toegestaan is
			function_name = function_to_call.substr(0,function_to_call.indexOf('('));
			if (function_to_call != '' && in_array(function_name,functions_allowed_to_call) == true) {

				eval(function_to_call);

			}

		}
        else if (xmlHttp.readyState == 4 && xmlHttp.status == 404) {

			if (layer != '' && output != '') document.getElementById(layer).innerHTML = xmlHttp.statusText;
			xmlHttp = null;

		}
	}

	if (this.method == 'POST') {
	       xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
	}
	xmlHttp.send(this.formRequest);


}


ajaxRequest.prototype.buildFormRequest = function() {

        if (this.form != null) {

                var request = '?';
                var pairs = new Array();
        
                with (document.getElementById(this.form)) {
        
                	for (var i=0; i < elements.length; i++) {
        
                                ename = elements[i].id;
                                value = elements[i].value;
                                
                                /**
                                 * Controleer of de type een checkbox is.
                                 * Standaard geeft deze functie altijd de waarde mee waardoor het lijkt of deze aangevinkt is.
                                 * Dus contorleren of het een checkbox is, zo ja controleren of deze gechecked is, Dan meegeven                                                   
                                 */                                                 
                                if (elements[i].type != 'checkbox' || (elements[i].type == 'checkbox' && elements[i].checked)) {
        
                                        if (elements[i].type == 'select-multiple') {
        
                                                for (var n=0; n < elements[i].options.length; n++) {
                                                
                                                        if (elements[i].options[n].selected) {
        
                                                                pairs.push(ename + "=" + encodeURIComponent(elements[i].options[n].value));
        
                                                        }
                                                
                                                }
                                        
                                        }
                                        else {
                                        
                                                pairs.push(ename + "=" + encodeURIComponent(value));
                                        
                                        }
                                
                                }
        
                	}
        
                }
        
                return pairs.join("&");

        }

}