﻿var nox = new function() {
	
	this.requests = new Array();
	this.defaultSettings = new Object();
	this.customSettings = new Object();
	
	this.setDefaultSettings = function() {
		this.defaultSettings.asynchronous = true;
		this.defaultSettings.alertJavascript = false;
		this.defaultSettings.compressJavascript = false; // <-- not yet used, can be set in NOX.vb instead. Search for .WriteAttributeString("compress"
	}
	
	this.setDefaultSettings();
	
	this.state = new Object();
	this.state.timesToUseCustomSettings = 0;
	
	this.pendingCallbacks = [];
	
	this.information = new Array();
	this.information.version = 1.1;
	this.information.lastChanged = new Date();
	this.information.lastChanged.setDate(2);
	this.information.lastChanged.setMonth(3 - 1);
	this.information.lastChanged.setFullYear(2009);
	this.information.lastChanged.setHours(0, 0, 0, 0);
	
	this.call = function (vbFunction) {
		var thisRequest = this.requests.push(new noxRequest(this.requests.length)) - 1;
		paramArray = new Array();
		if (arguments.length > 1)
		{
			for (var i = 1; i < arguments.length; i++)
				paramArray.push('argument' + (i-1) + '=' + arguments[i]);
		}
		
		if (!this.defaultSettings.asynchronous)
		{
			var pars = new Array('rnd=' + Math.random(), 'vbFunction=' + vbFunction, paramArray.join('&'));
			//'settings=' + this.defaultSettings.toSource(),  < -- was removed from the line above because toSource() does not work in IE
			this.requests[thisRequest].httpRequest.open('GET', 'nox/code/nox.aspx?' + pars.join('&'), false);
			this.requests[thisRequest].httpRequest.send(null);
			var response = this.requests[thisRequest].httpRequest.responseText;
			
			if (this.defaultSettings.alertJavascript)
				alert(response);
			eval(response);
		}
		else
		{
			var pars = new Array('rnd=' + Math.random(), 'vbFunction=' + vbFunction, paramArray.join('&'));
			this.requests[thisRequest].httpRequest.open('GET', 'nox/code/nox.aspx?' + pars.join('&'), true);
			this.requests[thisRequest].httpRequest.send(null);
			var currentRequest = this.requests[thisRequest].httpRequest;
			currentRequest.onreadystatechange = function () {
				// Hier een if-statement maken die ervoor zorgt dat het resultaat niet wordt uitgevoerd als de request is geannuleerd. Annuleren ook nog mogelijk maken in de module
				if (currentRequest.readyState == 4)
				{
					if (currentRequest.status == 200)
					{
						var response = currentRequest.responseText;
						if (nox.defaultSettings.alertJavascript)
							alert(response);
						eval(response);
						
						// Eventuele callback uitvoeren (alle callbacks doorlopen om te controleren of er 1 is met corresponderende requestIndex. Zo ja, die functie uitvoeren)
						for (var i = 0; i < nox.pendingCallbacks.length; i++)
						{
							if (nox.pendingCallbacks[i].requestIndex == thisRequest)
							{
								var args = nox.requests[thisRequest].callbackValues;
								nox.pendingCallbacks[i].fn(args);
								nox.pendingCallbacks.splice(i, 1);
								break;
							}
						}
					}
				}
			}
		}
	}
	
	this.SetCallback = function (fn) {
		// A callback is an anonymous function that will be executed after the result of the next nox request is received and processed.
		this.pendingCallbacks.push({'requestIndex':this.requests.length,'fn':fn});
	}
	
}

function noxRequest(index) {
	
	this.index = index;
	this.httpRequest = httpRequest();
	this.callbackValues = [];

}

function httpRequest() {
	try {
		// Firefox, Opera 8.0+, Safari
		return new XMLHttpRequest();
	}
	catch (e) {
		// Internet Explorer
		try {
			return new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
				return new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
}
