// AJAX framework. (c) 2007 Vaidas Balsys

// Function to create AJAX object
function ajaxObject() {
	var myObject;
	try {
		// Firefox, Opera 8.0+, Safari
		myObject = new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer
		try {
			myObject = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				myObject = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	return myObject;

}

// Function for retrieving data from remote AJAX responder. Multiple text lines
// (each line ends with \n) are placed to select box - one line per option
// Required parameters:
// 		target - select box object, e.g. document.getElementById('mySelect')
// 		source - url string to AJAX responder, e.g. 'getdata.php?min=1&max=7'
//		prompt - first zero value option in select box, e.g. 'please select'
//				 leave it empty if you do not want this option
function getAjaxSelectData(target, source, prompt) {
	xmlHttp = new ajaxObject();
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4) {
			sourceData = xmlHttp.responseText;
			sourceData = sourceData.split("\n");
			while (target.length > 0) target.remove(0);
			if (prompt != '') {
				option = document.createElement('option');
				option.text = prompt;
				option.value = 0;
				try {
					target.add(option, null);
				} catch(ex) {
					target.add(option);
				}
			}
			for (i = 0; i < sourceData.length - 1; i++) {
				option = document.createElement('option');
				option.text = sourceData[i];
				option.value = sourceData[i];
				try {
					target.add(option, null);
				} catch(ex) {
					target.add(option);
				}
			}
		}
	}
	xmlHttp.open("GET", source,true);
	xmlHttp.send(null);
}

function getAjaxHtmlData(target, source) {
	xmlHttp = new ajaxObject();
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4) {
			sourceData = xmlHttp.responseText;
			target.innerHTML = sourceData;
		}
	}
	xmlHttp.open("GET", source,true);
	xmlHttp.send(null);
}
