/*******************************
 * True Asynchronous AJAX calls
 *
 *
 *******************************/
var net = new Object();
net.READY_STATE_UNINITIALIZED = 0;
net.READY_STATE_LOADING = 1;
net.READY_STATE_LOADED = 2;
net.READY_STATE_INTERACTIVE = 3;
net.READY_STATE_COMPLETE = 4;

net.ContentLoader = function(item, onload, onerror) {
	this.url = item.targetPage;
	this.params = item.parameters;
	this.targetElement = item.targetElement;
	this.uniqueId = item.queueIndex;
	this.req = null;
	this.responseText = null;
	this.onload = onload;
	this.onerror = (onerror)?onerror:this.defaultError;
	this.loadXMLDoc();
	this.formatJSON = item.formatJSON;
}

net.ContentLoader.prototype = {
	
	loadXMLDoc:function() {
		
		if(window.XMLHttpRequest) {
			this.req = new XMLHttpRequest();
		}//*** end if
		else if(window.ActiveXObject) {
			this.req = new ActiveXObject("Microsoft.XMLHTTP");
		}//*** end else if
		
		if(this.req) {
			
			try {
				var loader = this;
				loader.req.onreadystatechange = function() {
					loader.onReadyState.call(loader);
				}//*** end function
				
				loader.req.open("POST", this.url, true);
				loader.req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
				loader.req.send(this.params);

			}//*** end try
			catch(error) {
				this.onerror.call(this);
			}//*** end catch
		}//*** end if
	}, //*** end function
	
	onReadyState:function() {
		var ready = this.req.readyState;
		if(ready == net.READY_STATE_COMPLETE) {
			var httpStatus = this.req.status;
			if(httpStatus == 200 || httpStatus == 0) {
				this.onload.call(this);
			}//*** end if
			else {
				this.onerror.call(this);
			}//*** end else
		}//*** end if
	}, //*** end function
	
	defaultError:function() {
		alert("error fetching data!"
				+ "\n\nreadyState: " + this.req.readyState
				+ "\nstatus: " + this.req.status
				+ "\nheaders: " + this.req.getAllResponseHeaders());
	}//*** end function
}

/*******************************
 * Support functions
 *
 *
 ******************************/
var requestQueue = [];
var ITEM_STATUS_WAITING = 0;
var ITEM_STATUS_ERROR = 1;

function parseJSON() {
	var resultArray = eval('(' + this.req.responseText + ')');
	this.formatJSON(resultArray, this.targetElement);
}

function queueItem(newTarget, newPage, newParams, isJSON, formatJSON) {
	this.targetElement = newTarget;
	this.targetPage = newPage;
	this.parameters = newParams;
	this.queueIndex = requestQueue.length;
	this.status = ITEM_STATUS_WAITING;
	if(isJSON != null) {
		this.isJSON = isJSON;
	}//*** end if
	else {
		this.isJSON = false;
	}//*** end else
	
	if(formatJSON != null) {
		this.formatJSON = formatJSON;
	}//*** end if
	else {
		this.formatJSON = null;
	}//*** end else
}

function addToQueue(item) {
	if(document.getElementById(item.targetElement) != null) {
		document.getElementById(item.targetElement).innerHTML = "<img src=\"/ksc/images/loading.gif\">&nbsp;Loading...";
	}//*** end if
	
	//requestQueue.push(new net.ContentLoader(item, toConsole, showError));
	if(item.isJSON) {
		var currentNet = new net.ContentLoader(item, parseJSON, showError);
	}//*** end if
	else {
		var currentNet = new net.ContentLoader(item, toConsole, showError);
	}//*** end else
} 

function checkForForm(targetElement,targetPage,parameters, isJSON, formatJSON) {
	var testElement = document.getElementById(targetElement);

	if(testElement.innerHTML.replace(/\s/g, "").length == 0) {
		loadPage(targetElement, targetPage, parameters, isJSON, formatJSON);
	}//*** end if
}

function passivelyLoadPage(targetElement,targetPage,parameters, isJSON, formatJSON) {
	var testElement = document.getElementById(targetElement);

	if(testElement.innerHTML.replace(/\s/g, "").length == 0) {
		loadPage(targetElement, targetPage, parameters, isJSON, formatJSON);
	}//*** end if
}

function passivelyLoadForm(targetElement, formObject, isJSON, formatJSON) {
	var testElement = document.getElementById(targetElement);

	if(testElement.innerHTML.replace(/\s/g, "").length == 0) {
		submitForm(targetElement, formObject, isJSON, formatJSON);
	}//*** end if
}

function loadPage(te, tp, p, isJSON, formatJSON) {
	addToQueue(new queueItem(te, tp, p, isJSON, formatJSON));
}

function submitForm(targetEl, formObject, isJSON, formatJSON) {
	var currentForm = null;
	var formAction = null;
	
	if(formObject.action) {
		currentForm = formObject;
	}//*** end if
	else if(document.forms[formObject]) {
		currentForm = document.forms[formObject];
	}//*** end else if
	else {
		alert("The form could not be found.");
		return false;
	}//*** end else
	formAction = currentForm.action;
	
	//*** cycle through elements and build parameters
	var params = 'a=b';
	var name = '';
	var value = '';
	var currentType = null;
	
	for(x = 0; x < currentForm.elements.length; x++) {
		name = null;
		value = null;
		currentType = currentForm.elements[x].type;
		
		if(currentType == 'checkbox') {
			if(currentForm.elements[x].checked == true) {
				name = currentForm.elements[x].name;
				//value = escape(encodeURI(currentForm.elements[x].value));
				value = currentForm.elements[x].value;
			}//*** end if checked
		}//*** end if
		else if(currentType == 'radio') {
			if(currentForm.elements[x].checked == true) {
				name = currentForm.elements[x].name;
				//value = escape(encodeURI(currentForm.elements[x].value));
				value = currentForm.elements[x].value;
			}//*** end if checked
		}//*** end else if
		else if(currentType == 'select') {
			name = currentForm.elements[x].name;
			//value = escape(encodeURI(currentForm.elements[x].options[currentForm.elements[x].selectedIndex].value));
			value = currentForm.elements[x].options[currentForm.elements[x].selectedIndex].value;
		}//*** end else if
		else if(currentType == 'select-one') {
			name = currentForm.elements[x].name;
			//value = escape(encodeURI(currentForm.elements[x].options[currentForm.elements[x].selectedIndex].value));
			value = currentForm.elements[x].options[currentForm.elements[x].selectedIndex].value;
		}//*** end else if
		else if(currentType == 'text') {
			name = currentForm.elements[x].name;
			//value = escape(encodeURI(currentForm.elements[x].value));
			value = currentForm.elements[x].value;
		}//*** end else if
		else if(currentType == 'textarea') {
			name = currentForm.elements[x].name;
			//value = escape(encodeURI(currentForm.elements[x].text));
			value = currentForm.elements[x].text;
			if(value == null || value.length() == 0) {
				value = currentForm.elements[x].value;
			}//*** end if
		}//*** end else if
		else if(currentType == 'hidden') {
			name = currentForm.elements[x].name;
			//value = escape(encodeURI(currentForm.elements[x].value));
			value = currentForm.elements[x].value;
		}//*** end else if
		
		if(name != null && value != null) {
			params += '&';
			params += name;
			params += "=";
			params += value;
		}//*** end if
		
	}//*** end for

	loadPage(targetEl, formAction, params, isJSON, formatJSON);

}

function toConsole() {
	if(this.targetElement != null && document.getElementById(this.targetElement) != null) {
		//alert(displayText);
		document.getElementById(this.targetElement).innerHTML = this.req.responseText;
	}//*** end if
}

function showError() {
	var errMessage = "error fetching data!"
				+ "<p>readyState: " + this.req.readyState
				+ "</p><p>status: " + this.req.status
				+ "</p><p>headers: " + this.req.getAllResponseHeaders() + "</p>";
	if(document.getElementById(this.targetElement) != null) {
		document.getElementById(this.targetElement).innerHTML = errMessage;
	}//*** end if
}
