﻿var nox = new function() {
	
	this.requests = new Array();
	this.defaultSettings = new Object();
	this.customSettings = new Object();
	
	this.setDefaultSettings = function() {
		this.defaultSettings.asynchronous = false; // <-- not yet used, calls are always synchronous
		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.information = new Array();
	this.information.version = 1.0;
	this.information.lastChanged = new Date();
	this.information.lastChanged.setDate(28);
	this.information.lastChanged.setMonth(12 - 1);
	this.information.lastChanged.setFullYear(2008);
	this.information.lastChanged.setHours(0, 0, 0, 0);
	
	this.call = function(vbFunction) {
		var thisRequest = this.requests.push(new request(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 toString() 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
		{
			alert('Asynchrone requests werken nog niet.');
			/*var pars = new Array('rnd=' + Math.random(), 'vbFunction=' + vbFunction, 'settings=' + this.defaultSettings.toSource(), paramArray.join('&'));
			this.requests[thisRequest].httpRequest.open('GET', 'nox/code/nox.aspx?' + pars.join('&'), true);
			this.requests[thisRequest].httpRequest.send(null);
			this.requests[thisRequest].httpRequest.onreadystatechange = function() {
				//alert(this.requests[thisRequest].httpRequest.readyState);
				alert('x'); // <!-- alert x werkt wel. De alert hierboven niet ...
			}*/
		}
	}
	
}

function request(index) {
	
	this.index = index;
	this.httpRequest = httpRequest();

}

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;
			}
		}
	}
}
