/* 	INFORMIT AJAXRESPONSE TOOLKIT  
	Downloaded from www.informit.com
*/

function HTTP()
{
	this.toString = function() { return "HTTP"; }
	
	this.status = function(_status)
	{
		var s = _status.toString().split("");
		switch(s[0])
		{
			case "1":
				return this.getInformationalStatus(_status);
				break;
			case "2":
				return this.getSuccessfulStatus(_status);
				break;
			case "3":
				return this.getRedirectionStatus(_status);
				break;
			case "4":
				return this.getClientErrorStatus(_status);
				break;
			case "5":
				return this.getServerErrorStatus(_status);
				break;
		}
	}
	
	this.getInformationalStatus = function(_status)
	{
		// Informational 1xx
		// http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.1
		switch(_status)
		{
			case 100:
				return "Continue";
				break;
			case 101:
				return "Switching Protocols";
				break;
		}
	}
	
	this.getSuccessfulStatus = function(_status)
	{
		// Successful 2xx
		// http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2
		switch(_status)
		{
			case 200:
				return "OK";
				break;
			case 201:
				return "Created";
				break;
			case 202:
				return "Accepted";
				break;
			case 203:
				return "Non-Authoritative Information";
				break;
			case 204:
				return "No Content";
				break;
			case 205:
				return "Reset Content";
				break;
			case 206:
				return "Partial Content";
				break;
		}
	}
	
	this.getRedirectionStatus = function(_status)
	{
		// Redirection 3xx
		// http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3
		switch(_status)
		{
			case 300:
				return "Multiple Choices";
				break;
			case 301:
				return "Moved Permanently";
				break;
			case 302:
				return "Found";
				break;
			case 303:
				return "See Other";
				break;
			case 304:
				return "Not Modified";
				break;
			case 305:
				return "Use Proxy";
				break;
			case 306:
				return "(Unused)";
				break;
			case 307:
				return "Temporary Redirect";
				break;
		}
	}
	
	this.getClientErrorStatus = function(_status)
	{
		// Client Error 4xx
		// http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4
		switch(_status)
		{
			case 400:
				return "Bad Request";
				break;
			case 401:
				return "Unauthorized";
				break;
			case 402:
				return "Payment Required";
				break;
			case 403:
				return "Forbidden";
				break;
			case 404:
				return "File not found.";
				break;
			case 405:
				return "Method Not Allowed";
				break;
			case 406:
				return "Not Acceptable";
				break;
			case 407:
				return "Proxy Authentication Required";
				break;
			case 408:
				return "Request Timeout";
				break;
			case 409:
				return "Conflict";
				break;
			case 410:
				return "Gone";
				break;
			case 411:
				return "Length Required";
				break;
			case 412:
				return "Precondition Failed";
				break;
			case 413:
				return "Request Entity Too Large";
				break;
			case 414:
				return "Request-URI Too Long";
				break;
			case 415:
				return "Unsupported Media Type";
				break;
			case 416:
				return "Requested Range Not Satisfiable";
				break;
			case 417:
				return "Expectation Failed";
				break;
		}
	}
	
	this.getServerErrorStatus = function(_status)
	{
		// Server Error 5xx
		// http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.5
		switch(_status)
		{
			case 500:
				return "Internal Server Error";
				break;
			case 501:
				return "Not Implemented";
				break;
			case 502:
				return "Bad Gateway";
				break;
			case 503:
				return "Service Unavailable";
				break;
			case 504:
				return "Gateway Timeout";
				break;
			case 505:
				return "HTTP Version Not Supported";
				break;
		}
	}
}

function Ajax()
{
	this.toString = function() { return "Ajax"; }
	this.http = new HTTP();
	
	this.makeRequest = function(_method, _url, _callbackMethod)
	{
		this.request = (window.XMLHttpRequest)? new XMLHttpRequest(): new ActiveXObject("MSXML2.XMLHTTP"); 
		this.request.onreadystatechange = _callbackMethod;
		this.request.open(_method, _url, true);
		this.request.send(_url);	
	}
	
	this.checkReadyState = function(_id, _1, _2, _3)
	{
		if (typeof(_id) == "object") {
			_id_obj = _id;
		} else {
			_id_obj = document.getElementById(_id);
		}
		if (typeof(this.request) != "undefined" && typeof(this.request.readyState) != "undefined") {
			switch(this.request.readyState)
			{
				case 1:
					_id_obj.innerHTML = _1;
					break;
				case 2:
					_id_obj.innerHTML = _2;
					break;
				case 3:
					_id_obj.innerHTML = _3;
					break;
				case 4:
					_id_obj.innerHTML = "";
					return this.http.status(this.request.status);
			}
		}
	}
}

var ajax = new Ajax();
var ajax_list = new Ajax();


/*	SAMPLE LISTENER FUNCTIONS
	Example code for what to write when the ajax connection returns different data types.

*/
function onXMLResponse()
{
	if(ajax.checkReadyState('body', 'loading...', 'loading...', 'loading...') == "OK")
	{
		var response = ajax.request.responseXML.documentElement;
		var header = response.getElementsByTagName('header')[0].firstChild.data;
		var description = response.getElementsByTagName('description')[0].firstChild.data;
		var sourceUrl = response.getElementsByTagName('sourceUrl')[0].firstChild.data;
		document.getElementById('body').innerHTML = "<b>" + header + "</b><br/>"
													 + description + "<br/><br/>"
													 + "<a href='" + sourceUrl + "'>Download the source files</a>";
	}
}

function onXHTMLResponse()
{
	if(ajax.checkReadyState('body', 'loading...', 'loading...', 'loading...') == "OK")
	{
		document.getElementById('body').innerHTML = ajax.request.responseText;
	}
}

function onJSONResponse()
{
	if(ajax.checkReadyState('body', 'loading...', 'loading...', 'loading...') == "OK")
	{
		eval("var response = ("+ajax.request.responseText+")");
		document.getElementById('body').innerHTML = "<b>" + response.header + "</b><br/>"
													 + response.description + "<br/><br/>"
													 + "<a href='" + response.sourceUrl + "'>Download the source files</a>";
	}
}
/* END INFORMIT AJAXRESPONSE TOOLKIT */

/*	void addToSelectedList ( string sourceid, string destinationid )
	string sourceid = DOM id for the select element with selection to move to destination
	string destinationid = DOM id for the select element destination to move to
	
	addToSelectedList takes the current selection in source element [sourceid] and moves it
	to the bottom of the destination select element [destinationid] after verifying that the
	selected element does not already exist in destination.
*/
function addToSelectedList(sourceid,destinationid) {
	var source_obj = document.getElementById(sourceid);
	var destination_obj = document.getElementById(destinationid);
	var addThese = new Array();
	
	// if the fields aren't located return values
	if (typeof(source_obj) == 'undefined') return;
	if (typeof(destination_obj) == 'undefined') return;
	
	for (i=0;i<source_obj.options.length;i++) {
		if (source_obj.options[i].selected && source_obj.options[i].value.length > 0) {
			addThese[addThese.length] = i;
			source_obj.options[i].selected = false;
		}
	}
	
	if (destination_obj.options.length > 0) {
		if (destination_obj.options[0].value == "" && addThese.length > 0) {
			destination_obj.options.length = 0;
		}
	}
	for (i=0;i<addThese.length;i++) {
		id = source_obj.options[ addThese[i] ].value;
		label = source_obj.options[ addThese[i] ].text;
		for (j=0;j<destination_obj.options.length;j++) {
			if (destination_obj.options[j].value == id) {
				id = -1;
				break;
			}
		}
		
		selectedindex = destination_obj.options.length + 1
		if (id != -1) {
			destination_obj.options[destination_obj.options.length] = new Option(label,id);
		}
	}
}

/*	void removeFromSelectedList( string selectid, string emptystr )
	string selectid = DOM id for the select element to remove selected elements from
	string emptystr = string to populate the select element with if all elements are removed
	
	removeFromSelectedList removes selected elements from the target select element [selectid].
	If the target select element is empty after all selected elements are removed the emptystr
	is put into the first select element.
*/
function removeFromSelectedList(selectid,emptystr) {
	var select_obj = document.getElementById(selectid);
	
	for (i=select_obj.options.length-1;i>=0;i--) {
		if (select_obj.options[i].selected) select_obj.options[i]=null;
	}
	
	if (select_obj.options.length == 0) {
		select_obj.options[0] = new Option(emptystr);
	}
}

/*	void moveSelection( string dir , string selectid )
	string dir = [ up | down ] to indicate the direction to move the selected element
	string selectid = DOM id for the target select element
	
	moveSelection shifts the selected elements up or down [dir] with error checking to prevent
	elements from being moved higher than the top or lower than the bottom of the list.
	Supports multiple selections by moving each selected element until all selected elements
	cannot be moved without effected each other ( see the neighborlocked boolean ).
*/
function moveSelection(dir,selectid) {
	var select_obj = document.getElementById(selectid);
	var moveto_text,moveto_value,valid_target;
	var neighborlocked = false;
	
	for (i=0;i<select_obj.options.length;i++) {
		if (select_obj.options[i].selected) {
			valid_target = true;
			if (dir == 'up') {
				target=i-1;
				origin=i;
				if (target < 0) valid_target = false;
			} else {
				target=i+1;
				origin=i;
				i=target;
				if (target >= select_obj.options.length) valid_target = false;
			} 
			if (neighborlocked) valid_target = false;
			if (valid_target) {
				moveto_text = select_obj.options[target].text;
				moveto_value = select_obj.options[target].value;
				select_obj.options[target].text = select_obj.options[origin].text;
				select_obj.options[target].value = select_obj.options[origin].value;
				select_obj.options[origin].text = moveto_text;
				select_obj.options[origin].value = moveto_value;
				select_obj.options[origin].selected = false;
				select_obj.options[target].selected = true;
			} else {
				neighborlocked = true;
			}
		} else {
			neighborlocked = false;
		}
	}
}
