AjaxFunctions.prototype.xmlRequest = 0;
AjaxFunctions.prototype.rslElmId = "";
AjaxFunctions.prototype.rsFunctName = "";
AjaxFunctions.prototype.strLoading = "Loading";
AjaxFunctions.prototype.frmLoaded = false;
AjaxFunctions.prototype.blnPage = false;
AjaxFunctions.prototype.intPageId = 0;

function AjaxFunctions() {

}

AjaxFunctions.prototype.createXmlRequestObj = function() 
{
	if (window.ActiveXObject) 
	{
		AjaxFunctions.prototype.xmlRequest = new ActiveXObject("Microsoft.XMLHTTP");
	} 
	else if (window.XMLHttpRequest) 
	{
		AjaxFunctions.prototype.xmlRequest = new XMLHttpRequest();
	}
}

AjaxFunctions.prototype.handleStateChangeWithoutLoading = function() {
	if (AjaxFunctions.prototype.xmlRequest.readyState == 4 
	&& AjaxFunctions.prototype.xmlRequest.status == 200) 
	{
		AjaxFunctions.prototype.parseResults();	
	}
}
AjaxFunctions.prototype.handleStateChange = function() 
{
	if (AjaxFunctions.prototype.xmlRequest.readyState == 4 
	&& AjaxFunctions.prototype.xmlRequest.status == 200) 
	{
		AjaxFunctions.prototype.parseResults();	
	} else {
		if (AjaxFunctions.prototype.xmlRequest.readyState == 0) {
			document.getElementById(AjaxFunctions.prototype.rslElmId).innerHTML = 'Het object is aangemaakt, maar niet geinitaliseerd.';
		} else if (AjaxFunctions.prototype.xmlRequest.readyState == 1) {
			document.getElementById(AjaxFunctions.prototype.rslElmId).innerHTML = 'Het object is aangemaakt, maar de verstuur methoden is niet aangeroepen.';
		} else if (AjaxFunctions.prototype.xmlRequest.readyState == 2) {
			document.getElementById(AjaxFunctions.prototype.rslElmId).innerHTML = 'Het object is aangemaakt, en de verstuur methoden is aangeroepen, maar er is geen response.';
		} else if (AjaxFunctions.prototype.xmlRequest.readyState == 3) {
			document.getElementById(AjaxFunctions.prototype.rslElmId).innerHTML = 'Er is een response, maar in een ander formaat.';
		} else {
			if (AjaxFunctions.prototype.xmlRequest.status == 404) {
				document.getElementById(AjaxFunctions.prototype.rslElmId).innerHTML = 'Opgevraagde gegevens zijn niet gevonden, pagina bestaat niet.';
			} else if (AjaxFunctions.prototype.xmlRequest.status == 301) {
				document.getElementById(AjaxFunctions.prototype.rslElmId).innerHTML = 'De pagina is verhuisd';
			} else if (AjaxFunctions.prototype.xmlRequest.status == 500) {
				document.getElementById(AjaxFunctions.prototype.rslElmId).innerHTML = 'Interne server error';
			} else {
				AjaxFunctions.prototype.parseLoading();
			}
		}
	}
}

AjaxFunctions.prototype.handleStateChangeFunction = function() 
{
	if (AjaxFunctions.prototype.xmlRequest.readyState == 4 
	&& AjaxFunctions.prototype.xmlRequest.status == 200) 
	{
		eval(AjaxFunctions.prototype.rsFunctName);
	} else {
		AjaxFunctions.prototype.parseLoading();
	}	
}

AjaxFunctions.prototype.handleStateChangeUpdate = function() 
{ 
	if (AjaxFunctions.prototype.xmlRequest.readyState == 4 
	&& AjaxFunctions.prototype.xmlRequest.status == 200) 
	{
		AjaxFunctions.prototype.parseResultsInIFrame(); 
	} 
}

AjaxFunctions.prototype.parseResultsInIFrame = function() {
	EditorBox.prototype.setContent(AjaxFunctions.prototype.xmlRequest.responseText);
	//AjaxFunctions.prototype.xmlRequest = null;
}

AjaxFunctions.prototype.parseResults = function() {
	document.getElementById(AjaxFunctions.prototype.rslElmId).innerHTML = AjaxFunctions.prototype.xmlRequest.responseText;
	
	if (AjaxFunctions.prototype.intPageId > 0)
	{
		AjaxFunctions.prototype.sendRequestWithoutElementUpdate('controllers/page.controller.php', 'pageid=' + AjaxFunctions.prototype.intPageId);
	}
	
	if (EditorBox.prototype.range) {
		if (EditorBox.prototype.range) {
			EditorBox.prototype.range.body.focus();
			EditorBox.prototype.rangeNode = document.getElementById('IFRAMEHTMLCONTENT').contentWindow.document.selection.createRange();
		}
	}
	
	if (AjaxFunctions.prototype.frmLoaded) {
		EditorBox.prototype.iniatilizeEditor();
		AjaxFunctions.prototype.setFrm(false);
	}
	//AjaxFunctions.prototype.xmlRequest = null;
}

AjaxFunctions.prototype.parseLoading = function() {
	document.getElementById(AjaxFunctions.prototype.rslElmId).innerHTML = AjaxFunctions.prototype.strLoading + AjaxFunctions.prototype.xmlRequest.statusText;
}

AjaxFunctions.prototype.setResultElement = function(resElmId) {
	AjaxFunctions.prototype.rslElmId = resElmId;
}

AjaxFunctions.prototype.setResultFunction = function(resFunction) {
	AjaxFunctions.prototype.rsFunctName = resFunction;
}	

AjaxFunctions.prototype.postRequestWithMime = function(url, mime, params, elementId) {
	AjaxFunctions.prototype.createXmlRequestObj();

	AjaxFunctions.prototype.setResultElement(elementId);
	AjaxFunctions.prototype.xmlRequest.open("POST", url, true);

	AjaxFunctions.prototype.xmlRequest.setRequestHeader("Content-type", mime);
	AjaxFunctions.prototype.xmlRequest.setRequestHeader("Content-length", params.length);
	AjaxFunctions.prototype.xmlRequest.setRequestHeader("Connection", "close");

	AjaxFunctions.prototype.xmlRequest.onreadystatechange = AjaxFunctions.prototype.handleStateChange;
	AjaxFunctions.prototype.xmlRequest.send(params);
}

AjaxFunctions.prototype.postRequest = function(url, params, elementId) {
	AjaxFunctions.prototype.createXmlRequestObj();
	
	AjaxFunctions.prototype.xmlRequest.open("POST", url, true);

	AjaxFunctions.prototype.xmlRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	AjaxFunctions.prototype.xmlRequest.setRequestHeader("Content-length", params.length);
	AjaxFunctions.prototype.xmlRequest.setRequestHeader("Connection", "close");

	if (document.getElementById(elementId)) {
		AjaxFunctions.prototype.setResultElement(elementId);
		AjaxFunctions.prototype.xmlRequest.onreadystatechange = AjaxFunctions.prototype.handleStateChange;
	} else {
		AjaxFunctions.prototype.setResultFunction(elementId);
		AjaxFunctions.prototype.xmlRequest.onreadystatechange = AjaxFunctions.prototype.handleStateChangeFunction;
	}
	
	AjaxFunctions.prototype.xmlRequest.send(params);
}

AjaxFunctions.prototype.sendRequestWithoutElementUpdate = function(url, query) {
	AjaxFunctions.prototype.createXmlRequestObj();
	if (query.length > 0) {
		var queryString = url + "?" + query;
	} else {
		var queryString = url;
	}
	AjaxFunctions.prototype.xmlRequest.onreadystatechange = AjaxFunctions.prototype.handleStateChangeUpdate;
	AjaxFunctions.prototype.xmlRequest.open("GET", queryString, true);
	AjaxFunctions.prototype.xmlRequest.send(null);
}

AjaxFunctions.prototype.sendRequestWithoutLoading = function(url, query, elementId) {
	AjaxFunctions.prototype.createXmlRequestObj();
	if (query.length > 0) {
		var queryString = url + "?" + query;
	} else {
		var queryString = url;
	}
	
	if (document.getElementById(elementId)) {
		AjaxFunctions.prototype.setResultElement(elementId);
		AjaxFunctions.prototype.xmlRequest.onreadystatechange = AjaxFunctions.prototype.handleStateChangeWithoutLoading;
	} else {
		AjaxFunctions.prototype.setResultFunction(elementId);
		AjaxFunctions.prototype.xmlRequest.onreadystatechange = AjaxFunctions.prototype.handleStateChangeFunction;
	}
	AjaxFunctions.prototype.xmlRequest.open("GET", queryString, true);
	AjaxFunctions.prototype.xmlRequest.send(null);
}

AjaxFunctions.prototype.sendRequest = function(url, query, elementId) {
	AjaxFunctions.prototype.createXmlRequestObj();
	if (query.length > 0) {
		var queryString = url + "?" + query;
	} else {
		var queryString = url;
	}
	
	if (document.getElementById(elementId)) {
		AjaxFunctions.prototype.setResultElement(elementId);
		AjaxFunctions.prototype.xmlRequest.onreadystatechange = AjaxFunctions.prototype.handleStateChange;
	} else {
		AjaxFunctions.prototype.setResultFunction(elementId);
		AjaxFunctions.prototype.xmlRequest.onreadystatechange = AjaxFunctions.prototype.handleStateChangeFunction;
	}
	AjaxFunctions.prototype.xmlRequest.open("GET", queryString, true);
	AjaxFunctions.prototype.xmlRequest.send(null);
}

AjaxFunctions.prototype.sendRequestAndReturn = function(url, query) {
	AjaxFunctions.prototype.createXmlRequestObj();
	if (query.length > 0) {
		var queryString = url + "?" + query;
	} else {
		var queryString = url;
	}
	
	AjaxFunctions.prototype.xmlRequest.open("GET", queryString, false);
	AjaxFunctions.prototype.xmlRequest.send(null);
	var strResult = AjaxFunctions.prototype.xmlRequest.responseText;
	AjaxFunctions.prototype.xmlRequest = null;
	return strResult;
}

// -- Custom functions -- //
AjaxFunctions.prototype.setPageId = function(pageid) {
	AjaxFunctions.prototype.intPageId = pageid;
	EditorBox.prototype.setPageId(pageid);
}

AjaxFunctions.prototype.setFrm = function(Frame) {
	AjaxFunctions.prototype.frmLoaded = Frame;
}

AjaxFunctions.prototype.setPage = function(Page) {
	AjaxFunctions.prototype.blnPage = Page;
}

AjaxFunctions.prototype.showImage = function(ImgSrc) {
	AjaxFunctions.prototype.sendRequest('PHP/Controllers/image.controller.php', 'action=singleImage&src=' + ImgSrc, 'configDiv');
}

AjaxFunctions.prototype.hideImage = function() {
	if (document.getElementById('configDiv')) {
		document.getElementById('configDiv').innerHTML = '';
	}
}
