/**
 * AJAX Class v0.8.5 (09/10/2008)
 * A class that sends and receives AJAX calls using the
 * XMLHttpRequest object. If output is delimited with js:
 * and :js it will be eval'd as Javascript code. If the
 * target object is not present the user is notified.
 */


// Constructor for AJAX class
function AjaxConnection(service_file) {
	this.uri = service_file;
	this.setOptions = setOptions;
	this.getOptions = getOptions;
	this.connect = connect;
	this.actions = doActions; 
}


// Method to process options
function setOptions(opts) {
	// Loop through the opts array
	for(i = 0, this.options = '', len = opts.length; i < len; i++) {
		this.options += "&" + opts[i];
	}
}


// Method to get options
function getOptions() {
	return this.options;
}


// Method to initialise a connection
function connect(targetObj) {
	// Init connection
	var x = init_object();
	x.open("POST", this.uri, true);
	x.onreadystatechange =
		function() {
			if (x.readyState == 4) {
				// Transer response into var
				var output = x.responseText;
				// Test for JS code
				var js = output.match(/js\:.*\:js/g);
				// Check to see if we eval contents...
				if (js != null && js.length > 0) {
					output = output.replace(/js\:.*\:js/g, "");
					for (i = 0; i < js.length; i++) {
						js[i] = js[i].replace(/(js\:|\:js)/g, "");
						eval(js[i]);
					}
				}

				// Check the target exists...
				if (checkObj(targetObj)) {
					// Target object
					var target = document.getElementById(targetObj);
					target.innerHTML = output;

				// ...else target not false throw error.
				} else if (targetObj !== false) {
					alert('Target object is not valid!');
				}
				delete x;

			} else {
				return;
			}
		}
	x.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	x.send(this.options);
}


// Method to determine the correct objec to use
function init_object() {
	// Init vars
	var obj;

	// Try to access XMLHttpRequest object (FF, Safari, Opera et al)
	try {
		obj = new XMLHttpRequest();
	} catch (e1) {
		// Try an access IE ActiveX object...
		try {
			obj = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e2) {
			// Here we go with another IE object test
			try {
				obj = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e3) {
				obj = null;
			}
		}
	}

	// If object is set return it to calling function
	if (obj) return obj;
}


// Generic AJAX application function to process data
function doActions(targetObj, vars, formId) {
	// Make sure we are connected
	if (connected == true) {
		// Build option array
		var optArray = new Array(0);
		// Check for vars to parse
		if (vars) {
			var qStr = vars.split(':');
			var qLen = qStr.length;
			for (var i = 0; i < qLen; i++) {
				optArray.push(qStr[i]);
			}
		}
		// Check for form vars to parse
		if (formId) {
			// Loop through form elements
			var theForm = document.getElementById(formId);
			// Loop through form elements
			for (i = 0, len = theForm.elements.length; i < len; i++) {
				// Check for multi select element and process...
				if (theForm.elements[i].type == 'select-multiple') {
					var ms = theForm.elements[i];
					// Loop through multi-select
					for (i2 = 0, len2 = ms.length; i2 < len2; i2++) {
						if (ms[i2].selected) optArray.push(theForm.elements[i].name + '=' + encodeURIComponent(ms[i2].value));
					}

				// ...else, see if the element has a checked property
				} else if (typeof(theForm.elements[i].name) != 'undefined' && (((theForm.elements[i].type == 'radio' || theForm.elements[i].type == 'checkbox') && theForm.elements[i].checked == true) || (theForm.elements[i].type != 'radio' && theForm.elements[i].type != 'checkbox'))) {
					optArray.push(theForm.elements[i].name + '=' + encodeURIComponent(theForm.elements[i].value));
				}
			}
		}
		// Build option array
		this.setOptions(optArray);
		this.connect(targetObj);
	}
}



/* Generic functions for use with AJAX class */

// Check for existence of an object
function checkObj(obj) {
	if (document.getElementById(obj)) {
		return true;
	}
	return false;
}


// Confirmation dialog
function confirmAction(txt) {
	var test = confirm(txt);
	if (test === true) return true;
	return false;
}


// Confirmation dialog
function confirmPrompt(txt, msg) {
	var test = prompt(txt, msg);
	if (test !== false) return test;
	return false;
}
