/* Skeleton AJAX library
 */

var no_support = "Your browser does not support XMLHTTP.";

function ajax() {
	var xmlhttp = null;
	var result_handler = null;
	var obj = this;

	this.get = get;
	this.post = post;
	this.getValue = getValue;
	this.getRecords = getRecords;

	function state_change() {
		if (xmlhttp.readyState == 4) {
			if (xmlhttp.status == 200) {
				result_handler(obj, xmlhttp.responseXML.documentElement);
			} else {
				alert("Problem retrieving XML data:" + xmlhttp.statusText)
			}
		}
	}

	//function get(page, data, handler) {
	function get(page, data, handler, sgo, npagenr, sfilter, ssort) {
		if (xmlhttp != null) {
			if (data == null) {
				data = "";
			} else if (data != "") {
				data = "&" + data;
			}

	
			if (typeof sgo != "undefined" && sgo != '') {
				if (data != "") {
					data += "&";
				}
				data += "go=" + sgo;
			}

			if (typeof npagenr != "undefined" && npagenr != '') {
				if (data != "") {
					data += "&";
				}
				data += "pagenr=" + npagenr;
			}

			if (typeof sfilter != "undefined" && sfilter != '') {
				if (data != "") {
					data += "&";
				}
				data += "filter=" + sfilter;
			}
			
			if (typeof ssort != "undefined" && ssort != '') {
				if (data != "") {
					data += "&";
				}
				data += "sort=" + ssort;
			}
			
			if (handler != null) {
				result_handler = handler;
				xmlhttp.onreadystatechange = state_change;
			}
			
			xmlhttp.open("GET", "ajax.php?page=" + page + data, true);
	//		xmlhttp.open("GET", file, true);
			xmlhttp.send(null);
		} else {
			alert(no_support);
		}
	}

	function post(page, form_id, handler, sgo, npagenr, sfilter, ssearch, ssort) {
		if (xmlhttp != null) {
			post_data = "";

			form_obj = document.getElementById(form_id);
			for (i = 0; i < form_obj.elements.length; i++) {
				if (form_obj.elements[i].name != "") {
					if (post_data != "") {
						post_data += "&";
					}
					post_data += form_obj.elements[i].name + "=";
					switch (form_obj.elements[i].type) {
					   case "checkbox":
					       post_data += (form_obj.elements[i].checked ? "on" : "");
					       break;
					   default:
					       post_data += form_obj.elements[i].value;
					} 
				}
			}

			if (typeof sgo != "undefined" && sgo != '') {
				if (post_data != "") {
					post_data += "&";
				}
				post_data += "go=" + sgo;
			}

			if (typeof npagenr != "undefined" && npagenr != '') {
				if (post_data != "") {
					post_data += "&";
				}
				post_data += "pagenr=" + npagenr;
			}

			if (typeof sfilter != "undefined" && sfilter != '') {
				if (post_data != "") {
					post_data += "&";
				}
				post_data += "filter=" + sfilter;
			}

			if (typeof ssearch != "undefined" && ssearch != '') {
				if (post_data != "") {
					post_data += "&";
				}
				post_data += "search=" + ssearch;
			}
			
			if (typeof ssort != "undefined" && ssort != '') {
				if (post_data != "") {
					post_data += "&";
				}
				post_data += "sort=" + ssort;
			}

			if (handler != null) {
				result_handler = handler;
				xmlhttp.onreadystatechange = state_change;
			}

			xmlhttp.open("POST", "ajax.php?page=" + page, true);
			xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			xmlhttp.send(post_data);
		} else {
			alert(no_support);
		}
	}

	function getValue(key) {
		if (xmlhttp != null) {
			return xmlhttp.responseXML.getElementsByTagName(key)[0].firstChild.nodeValue;
		} else {
			return null;
		}
	}

	function getRecords(tag) {
		if (xmlhttp != null) {
			var obj = xmlhttp.responseXML.getElementsByTagName(tag);
			var result = new Array();

			for (record = 0; record < obj.length; record++) {
				result[record] = new Array();
				for (entry = 0; entry < obj[record].childNodes.length; entry++) {
					key = obj[record].childNodes[entry].nodeName;
					if (key[0] != "#") {
						//alert(key + ' ' + obj[record].childNodes[entry].nodeType);
						if (obj[record].childNodes[entry].hasChildNodes()==true) {
							result[record][key] = obj[record].childNodes[entry].firstChild.nodeValue;
						} else {
							result[record][key] = '';
						}
					}
				}
			}

			return result;
		} else {
			return null;
		}
	}

	if (window.XMLHttpRequest) {
		xmlhttp = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		xmlhttp = null;
	}

	if (xmlhttp == null) {
		alert(no_support);
	}
}

function ajax_clear(name) {
	document.getElementById(name).innerHTML = "";
}

function ajax_print(name, data) {
	document.getElementById(name).innerHTML += data;
}

function ajax_value(name, data) {
	document.getElementById(name).value = data;
}

function ajax_formvalue(formname, name, data) {
	var tmp = document.getElementById(formname);
	for(var j=0;j<tmp.elements.length;j++) {
		if (tmp.elements[j].name == name) {
			tmp.elements[j].value = data;
		}
	}
}

function ajax_formget(formname, name) {
	var tmp = document.getElementById(formname);
	for(var j=0;j<tmp.elements.length;j++) {
		if (tmp.elements[j].name == name) {
			return tmp.elements[j].value;
		}
	}
}

function ajax_focus(name) {
	document.getElementById(name).focus();
}

function ajax_hide(name) {
	document.getElementById(name).style.visibility = 'hidden';
	document.getElementById(name).style.display = 'none';
}

function ajax_show(name) {
	document.getElementById(name).style.visibility = 'visible';
	document.getElementById(name).style.display = 'block';
}

function ajax_get(name) {
	return document.getElementById(name).value;
}
