//Updated 03 Dec 2008

//in ajax get scripts, add a random number to the request to prevent caching

function send(url) {
	//see if onChange is defined, if it isn't assign it the default handler
	
	http.open("GET",url + "&sid="+Math.random(),true);
	http.onreadystatechange=http_state_changed;
	http.send(null);
}

function send_custom(url, callback) {
	//see if onChange is defined, if it isn't assign it the default handler
	http.open("GET",url + "&sid="+Math.random(),true);
	http.onreadystatechange=callback;

	http.send(null);
}


function get_xml_http_object(handler) { 
	var h=null;
	if (window.XMLHttpRequest)
	{
		h=new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		h=new ActiveXObject("Microsoft.XMLHTTP");
	}
	return h;
}

var http = get_xml_http_object(http_state_changed);

//Returns: an array of all the id's modified by the ajax code
function http_state_changed() {
	
	if (http.readyState==4 || http.readyState=="complete")
	{ 

		var modified = new Array();

		var xmldoc = http.responseXML;

		var data=http.responseXML.getElementsByTagName('field');

		var id, field_data, action;

		for (var i = 0; i < data.length; i++) { 
			
			id = data[i].getElementsByTagName('id')[0].firstChild.nodeValue; 

			if(data[i].getElementsByTagName('data')[0].firstChild) {

				field_data = 
					data[i].getElementsByTagName('data')[0].firstChild.nodeValue;
			} else {
				field_data = '';
			}				
				action = 
					data[i].getElementsByTagName('action')[0].firstChild.nodeValue;
			

			var elem = document.getElementById(id);

			modified[i] = id;
			
			if(elem == null && id != 'errors') {
				
				var id_2 = 
						data[i].getElementsByTagName('id_2')[0].firstChild.nodeValue; 

				if(data[i].getElementsByTagName('action_2')[0].firstChild != null) {
					var action = 
					data[i].getElementsByTagName('action_2')[0].firstChild.nodeValue; 
				} else {
					action='append';
				}
				elem = document.getElementById(id_2);
//				action = document.getElementById(action_2);
//				modified[i] = id_2;
//				action = 'append';

			} else if (id == 'errors') {

				if(error_function) {
					error_function(field_data);
				}
			}


			if(elem != null) {

				if(action=="replace") {
					elem.innerHTML = field_data;
				} else {
					elem.innerHTML = elem.innerHTML + field_data;
				}
			} else {

			}
		}

	}
	return modified; 
} 

