/* AJAX Class */

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


// 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(return_func, targetObj) {
	// Init connection
	var x = init_object();
	x.open("POST", this.uri, true);
	x.onreadystatechange =
		function() {
			if (x.readyState == 4) {
				eval(return_func + '(x.responseText, targetObj)');
				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 an access IE ActiveX object...
	try {
		obj = new ActiveXObject("Msxml2.XMLHTTP");
	// ...not found, try another IE version...
	} catch (e) {
		// Here we go with another IE object test
		try {
			obj = new ActiveXObject("Microsoft.XMLHTTP");
		// ...nope, not found that one either
		} catch (oc) {
			obj = null;
		}
	}

	// If not IE objects found, use standard jobbie for Firefox et al
	if(!obj && typeof XMLHttpRequest != "undefined") obj = new XMLHttpRequest();

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



/* Generic AJAX functions */

// Generic AJAX application function
function ajaxActions(ajaxObj, func, 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++) {
				// See if the element has a checked property
				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 + '=' + escape(theForm.elements[i].value));
				}
			}
		}
		// Check for function to call
		if (func) {
			// Build option array
			ajaxObj.setOptions(optArray);
			ajaxObj.connect(func, targetObj);
		}
	}
}


// Display output in target object
function ajaxOutput(output, targetObj) {
	// 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!');
	}
}


// 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;
}