// Ajax Interface by Edward Kmett

// This is designed to retrieve executable javascript from the server 
// and run it in the current page context. This helps us bypass all of
// the XML parsing. 

// In order to use, call the following:
// ajax("http://www.example.com/my.cgi?field="+escape(field));

// which can return a snippet of executable javascript which is processed by the client
// If you would rather use a POST url:
// ajax("http://www.example.com/my.cgi","field="+escape(field));

// The mime-type returned by the server is ignored, but SHOULD be text/javascript.
// The exception is if you gzip the data before sending it to the browser, then you will
// need to set the mime-type to text/plain, due to the fact that Mozilla does not support
// gzipping javascript for some reason.

// In the event that the server returns an XML/HTML document rather than javascript
// it will be launched in a new window and shown to the end user.

var ajax_failures = 0;
var ajax_successes = 0;

function get_http_request_object() { 
	var A = null;
	try { 
		A = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) { 
		try { 
			A = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e2) { 
			A = null;
		}
	}
	if (!A && typeof XMLHttpRequest != "undefined") { 
		A = new XMLHttpRequest();
	}
	return A;
}

var last_xml_http = null;


// Call a url on the server. 
// The target web page should return executable javascript as its contents

// context may be used by the returned javascript

// this will run server side javascript inside of a scope in which
// they can refer to context.foo for properties of the context object
// and xml_http.foo to refer to properties of their own XMLHttpRequest

function ajax(url,post,context) {
	var xml_http = get_http_request_object();
	if (xml_http) { 
		xml_http.open(post ? "POST" : "GET", url, true);
		xml_http.onreadystatechange = function() { 			
			if (xml_http.readyState==4&& xml_http.responseText) {
				if (xml_http.responseText.charAt(0) != "<") { 
					try { 
						eval(xml_http.responseText);
						ajax_successes++;
					} catch(e) {
						ajax_failures++;
					}
				} else { 
					// may not work in IE
					var win = window.open("","ajax-failure","width=300,height=400");
					win.document.open();
					win.document.write(xml_http.responseText);
					win.document.close();
					win.focus();
					
					ajax_failures++;
				}
			}
		};
		xml_http.send(post);
		last_xml_http = xml_http;
		return true;
	} else { 
		last_xml_http = xml_http;
		return false;
	}
}

// Same as above, but tell the previous HTTP request to abort if it hasn't already
// completed. Use this if the previous request is made irrelevant by the current
// request for instance.
/*
function ajax_with_cancel_last(url,post,context,on_cancel) { 
	if (last_xml_http&&last_xml_http.readyState!=4&&last_xml_http.on_cancel) {
		last_xml_http.on_cancel();
		last_xml_http.abort();
	}
	ajax(url,post,context,on_cancel);
}
*/


function content_pane_asynchronous_javascript_and_extensible_hypertext_markup_language(url,content_pane,post){
	var str;
	var xml_http = get_http_request_object();
	if (xml_http) { 
		xml_http.open(post ? "POST" : "GET", url, true);
		xml_http.onreadystatechange = function() { 			
			if (xml_http.readyState==4&& xml_http.responseText) {
					var str = xml_http.responseText;
					content_pane.setContent(str, true);
			}
		};
		xml_http.send(post);
		return true;
	} else { 
		return false;
	}
}


//ajax function with callback
function ajaxcb(url,post,callBack) {
	var xml_http = get_http_request_object();
	if (xml_http) { 
		xml_http.open(post ? "POST" : "GET", url, true);
		xml_http.onreadystatechange = function() { 			
			if (xml_http.readyState==4&& xml_http.responseText) { 
				callBack(xml_http.responseText);
			}
		};
		xml_http.send(post);
		return true;
	} else { 
		return false;
	}
}
