var requestType = 'html';
var functions_allowed_to_call = new Array('registerCheckAnvrComplete','loginProgress','forgotpasswordProgress','resetpasswordProgress','logoutProcess','shadowBorder','checkBirthdaysPopup','sayingOfTheDayPopup');
var response = '';

if (!window.execScript) {
	window.execScript = function(script) {setTimeout(script, 0);};
}

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;

}

var ajaxRequests = new Array();
function ajaxRequest(page,request) {

	if (page.substr(0,1) == '/') {
		this.page = 'ajax'+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 = null;
        }

};

ajaxRequest.prototype.process = function() {

	this.setCached();
	this.setFormRequest();

	var layer = this.container;
	if (this.container === '') {
		this.output = 'xml';
	}

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

	var xmlHttp = getXmlHttp(this.output);

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

		var objLayer = document.getElementById(layer);

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

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

				if (document.getElementById(layer) === null) {
					alert('Requested container/layer ('+layer+') could not be found');
					document.getElementById(layer).removeChildren();
					document.getElementById(layer).setAttribute('class','');
					document.getElementById(layer).className = '';
				}
				else {
					document.getElementById(layer).innerHTML = xmlHttp.responseText;
					document.getElementById(layer).setAttribute('class','');
					document.getElementById(layer).className = '';
				}

			}

			var function_name = function_to_call.substr(0,function_to_call.indexOf('('));
			if (function_to_call !== '' && in_array(function_name,functions_allowed_to_call) === true) {

				response = xmlHttp.responseText;
				eval(function_to_call);

			}

			var re = /<script(\s[^>]*)?>([\s\S]*?)<\/script>/gi, match;
			while (match = re.exec(xmlHttp.responseText)) {window.execScript(match[2], 'javascript');}

			xmlHttp = null;

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

			if (layer !== '' && output !== '') {
				document.getElementById(layer).innerHTML = xmlHttp.statusText;
				document.getElementById(layer).setAttribute('class','');
				document.getElementById(layer).className = '';
			}
			xmlHttp = null;

		}
		else {
		
			if (layer !== '' && output !== '') {
			
				// Is er nog geen loading div? Dan aanmaken
				if (document.getElementById('loadingDiv'+layer) === null) {

					document.getElementById(layer).setAttribute('class','loadingDivPosition');

				}

			}
		
		}

	};

	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 && document.getElementById(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;
                                     
                                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 if (elements[i].type == 'radio') {
                                        
                                            if (elements[i].checked) {
                                                pairs.push(ename + "=" + encodeURIComponent(value));
                                            }
                                        
                                        }
                                        else {
                                        
                                                pairs.push(ename + "=" + encodeURIComponent(value));
                                        
                                        }
                                
                                }
        
                	}
        
                }
        
                return pairs.join("&");

        }

};