<!--

// Begin AJAX

function createRequestObject(){
	var request_o; //declare the variable to hold the object.
	var browser = navigator.appName; //find the browser name
	if (window.ActiveXObject) {
		/* Create the object using MSIE's method */
		request_o = new ActiveXObject("Microsoft.XMLHTTP");
		if (!request_o) {
			request_o = new ActiveXObject("Microsoft.XMLHTTP");
		}
	} else if (window.XMLHttpRequest) {
		/* Create the object using other browser's method */
		request_o = new XMLHttpRequest();
	}
	return request_o; //return the object
}

// Ajax class

function ajaxRequester(url, containerId) {
	this.onreturnCallbacks = new Array();
	this.xml_request_obj = createRequestObject();
	this.post_vars = new Array();
	this.onreturn_handlers = new Array();
	this.onfetch_handlers = new Array();
	this.url = url;
	this.formId = false;
	
	this.add_field = function (field_name, field_val) {
		this.post_vars.push(new Array(field_name, field_val));
	}
	this.add_vars_from_form = function(form_id){
		var vars_array = get_array_from_from(form_id);
		for(var i=0; i<vars_array.length; i++) {
			field_name = vars_array[i][0];
			field_val = vars_array[i][1];
			this.add_field(field_name, field_val);
		}
	}
	this.clear_post_vars = function() {
		this.post_vars = new Array();
	}
	this.onFetch = function(callback) {
		this.onfetch_handlers.push(callback);
	}
	this.fetch = function() {
		var form_id = 'search_criteria';
		
		if (this.xml_request_obj) {
			if(this.xml_request_obj.readyState != 4 && this.xml_request_obj.readyState != 0) {
				this.xml_request_obj.abort();
			}
			// handle submittion of sticky forms - forms that are submitted every time
			if (this.formId !== false) {
				this.add_vars_from_form(this.formId);
			}
			// call any onfetch callbacks registered
			this.execOnFetchCallbacks();
			
			this.xml_request_obj.open('POST', this.url, true);
			// allow object to reference itself after retriving data
			var obj = this;
			this.xml_request_obj.onreadystatechange = function() {
				if (obj.xml_request_obj.readyState == 4 && obj.xml_request_obj.status != undefined) { // if XMLHttpRequest is fully loaded
					if (obj.xml_request_obj.status == 200) {
						obj.execOnReturnCallbacks();
					}
				}
			};
			this.xml_request_obj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			var murl_encoder = new url_encoder();
			for(var i=0; i<this.post_vars.length; i++) {
				field_name = this.post_vars[i][0];
				field_val = this.post_vars[i][1];
				murl_encoder.add_field(field_name, field_val);
			}
			// pass vars as URL encoded string
			var formVars = murl_encoder.toString();
			
			this.xml_request_obj.send(formVars);
			this.clear_post_vars();
		} else {
			alert('An error occurred while trying to load AJAX data.');
		}
	}
	this.execOnReturnCallbacks = function() {
		for (i in this.onreturnCallbacks) {
			this.onreturnCallbacks[i].apply(this);
		}
	}
	this.execOnFetchCallbacks = function() {
		for(i in this.onfetch_handlers){
			this.onfetch_handlers[i].apply(this);
		}
	}
	this.onReturn = function(callback) {
		this.onreturnCallbacks.push(callback);
	}
	this.setContainerId = function(containerId) {
		this.containerId = containerId;
		this.onReturn(
			function() {
				response_to_container(this.xml_request_obj, this.containerId);
			}
		);
	}
	if (containerId) {
		this.setContainerId(containerId);
	}
}

// END Ajax class

// loop through element in a form and bulid an array of all elements and selected options
function get_array_from_from(form_id) {
	var return_arr = new Array();
	var form_element;
	var the_form = document.getElementById(form_id);
	var count = 0;
	for (var i=0; i<the_form.elements.length; i++) {
		form_element = the_form.elements[i];
		// if form_element is a select with multiple seelctions available
		if (form_element.options && form_element.options.length > 1) {
			// alert('found multiple select');
			// loop through selected options
			for (var n=0; n<form_element.options.length; n++) {
				if (form_element.options[n].selected || form_element.options[n].checked) {
					return_arr[count] = new Array(form_element.name, form_element.options[n].value);
					count++;
				}
			}
		} else if ((form_element.type != 'checkbox' && form_element.type != 'radio') || form_element.checked) {
			return_arr[count] = new Array(form_element.name, form_element.value);
			count++;	
		}
	}
	return return_arr;
}

/*
 * url_encoder() -- formats data to be sent as HTTP encoded varaibles
 */
function url_encoder() {
	this.field_arr = new Array();
	/*
	 * add_field() -- add field/values pairs to be eoncoded into HTTP variables
	 */
	this.add_field = function(field_name, field_val) {
		this.field_arr.push(new Array(field_name, field_val));
	}
	/*
	 * toString() -- output HTTP encoded vars string
	 */
	this.toString = function() {
		var encoded_str = new String();
		var field_name, field_val;
		for(i=0; i<this.field_arr.length; i++){
			if (i>0) {
				encoded_str += '&';
			}
			field_name = this.field_arr[i][0];
			field_val = this.field_arr[i][1];
			encoded_str += field_name + '=' + field_val;
		}
		if (i > 0) return encoded_str;
		else return false;
	}
}

// get response from xmlHttpRequest obj and set as content of container element
function response_to_container (xml_request_obj, container_id) {
	if (xml_request_obj.readyState == 4 && xml_request_obj.status != undefined) { // if XMLHttpRequest is fully loaded
		if (xml_request_obj.status == 200) {
			var html = xml_request_obj.responseText;
			// set HTML
			if (document.getElementById) {
				return document.getElementById(container_id).innerHTML = html;
			} else {
				return document[container_id].innerHTML = html;
			}
			// call any onreturn callbacks registered
		} else {
			alert('An error occurred while trying to display AJAX data.\nStatus: ' + xml_request_obj.status);
		}
	}
	
}
//-->
