
function escortFramework() {

	/*
	 * This is the constructor for the framework class.
	 */
	this.settings = new Object();
	window.framework = this;
	window.onload = function() {
		window.framework.initFramework(true);
	}
	
}

escortFramework.prototype.initFramework = function(initial_) {
}

escortFramework.prototype.__XMLHttpRequest = function() {
	/*
	 * This function returns a proper XMLHttpRequest object regardles which browser or operating system the user is
	 * currently using.
	 */
	var request;
	try {
		request = new XMLHttpRequest();
	} catch (trymicrosoft) {
		try {
			request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (othermicrosoft) {
			try {
				request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (failed) {
				request = false;
			}
		}
	}
	return request;
}

escortFramework.prototype.requestGetUrl = function(url_, callback_) {
	/*
	 * This function will request the provided url_ using get and optionally calls a callback function if the request
	 * was successfully finished.
	 */
	var request = this.__XMLHttpRequest();
	if (request) {
		url_ += this.getUrlSuffix(url_);
		request.open('GET', url_, true);
		request.onreadystatechange = function() {
			// Only call the target callback function when the request was successfull AND finished. Otherwise we
			// can't be sure that the data has been processed.
			if (request.readyState == 4) {
				if (request.status != 200) {
					switch(request.status) {
						case 401: alert('Unauthorized request attempt [' + url_ + ']'); break;
						case 403: alert('Forbidden request attempt [' + url_ + ']'); break;
						case 404: alert('File does not exist [' + url_ + ']'); break;
					}
				} else {
					if (callback_) callback_(request.responseText);
				}
			}
		}
		request.send(null);
	} else alert('Error initializing XMLHttpRequest object [' + url_ + ']');
}


escortFramework.prototype.__buildForm = function(node_) {
	

	
	/*
	 * This function recursively iterates through the requested node and builds a parameter list which can be used to
	 * post using an XMLHttpRequest.
	 */
	var parameters = '';
	var elements = node_.getElementsByTagName('SELECT');
	for (n = 0; n < elements.length; n++) {
			
			if (elements[n].multiple) {
				for (x = 0; x < elements[n].length; x++) {
					if (elements[n][x].selected){
						parameters += elements[n].getAttribute('name') + '=';
						parameters +=  + elements[n][x].value;
						parameters += '&';
					}
					
				}			
			} else {
				parameters += elements[n].getAttribute('name') + '=';
				parameters += encodeURIComponent(elements[n].value);
				parameters += '&';
			}
		}
	var elements = node_.getElementsByTagName('INPUT');
	for (n = 0; n < elements.length; n++) {
		if (/(text|hidden)/i.test(elements[n].getAttribute('type'))) {
			parameters += elements[n].getAttribute('name') + '=';
			parameters += encodeURIComponent(elements[n].value);
		}
		if (
			/(checkbox|radio)/i.test(elements[n].getAttribute('type')) &&
			elements[n].checked
		) {
			parameters += elements[n].getAttribute('name') + '=';
			parameters += encodeURIComponent(elements[n].value);
		}
		parameters += '&';
	}
	var elements = node_.getElementsByTagName('TEXTAREA');
	for (n = 0; n < elements.length; n++) {
		parameters += elements[n].getAttribute('name') + '=' + encodeURIComponent(elements[n].value) + '&';
	}
	return parameters;
}

escortFramework.prototype.requestPostUrl = function(url_, form_, callback_) {
	/*
	 * This function tries to request the supplied url_ using post and therefore builds the parameters for the post
	 * prior to requesting the url. Optionally a callback_ function is called.
	 */
	
	var parameters = this.__buildForm(form_);
	

	// Then we're going to send the parameters we've created above to a freshly created XMLHttpRequest object.
	var request = this.__XMLHttpRequest();
	if (request) {
		url_ += this.getUrlSuffix(url_);
		request.open('POST', url_, true);
		request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		request.onreadystatechange = function() {
			// Only call the target callback function when the request was successfull AND finished. Otherwise we
			// can't be sure that the data has been processed.
			if (request.readyState == 4) {
				if (request.status != 200) {
					switch(request.status) {
						case 401: alert('Unauthorized request attempt [' + url_ + ']'); break;
						case 403: alert('Forbidden request attempt [' + url_ + ']'); break;
						case 404: alert('File does not exist [' + url_ + ']'); break;
					}
				} else {
					if (callback_) callback_(request.responseText);
				}
			}
		}
		request.send(parameters);
	} else alert('Error initializing XMLHttpRequest object [' + url_ + ']');
}

escortFramework.prototype.getUrlSuffix = function(url_) {
	/*
	 * This function prepares the supplied url_ so that it passes all the necessary information for the server side
	 * scripts to return the correct data.
	 */
	if (url_ == undefined) url_ = '';
	var suffix = (url_.indexOf('?') > -1 ? '&' : '?') + 'rn=' + Math.round(100000 * Math.random());
	for (var i in this.settings) {
		suffix += '&' + i + '=' + this.settings[i];
	}
	return suffix;
}

escortFramework.prototype.reloadSection = function(target_, callback_) {
	/*
	 * This function reloads the supplied section_ with the content returned by calling the supplied url_. If the
	 * section was not found, or the url is unavailable, nothing is done.
	 */
	var target = document.getElementById(target_);
	var last_id = Math.round(100000 * Math.random());
	target.setAttribute('last_id', last_id);
	if (
		target &&
		target.getAttribute('source')
	) {
		this.requestGetUrl(target.getAttribute('source'), function(result_) {
			// Only update the target and call the callback function if this is the last active reload request.
			if (last_id == target.getAttribute('last_id')) {
				target.innerHTML = result_;				
				if (callback_ != undefined) callback_();
			}
		} );
	}
}

escortFramework.prototype.batchReloadSections = function(targets_, callback_) {
	/*
	 * This function can be used to perform reloads on several targets at once. Afther all the reloads are done the
	 * provided callback_ function is called.
	 */
	var busy = 0;
	if (targets_.length && targets_.length > 0) for (var n = 0; n < targets_.length; n++) {
		busy++;
		this.reloadSection(targets_[n], function() {
			busy--;
			if (busy == 0 && callback_ != undefined) callback_();
		} );
	}
}