/* 
 * Use this function to send an AJAX request -- this function
 * can be called with multiple different requests at the same time
 * and they won't collide.
 * 
 * Parameters:
 *    URL - A string URL that the AJAX submits to
 *    method - A string (either 'GET' or 'POST') that describes how
 *      this request should be processed.  Note that it must be given
 *      in all caps to work
 *    responseFunction - The name of the response function WITHOUT
 *      any parathesis.  The sendAJAX function will send a single
 *      parameter to this function which will be the XMLhttp object
 *      it used to make the request (so that the function can ask
 *      onReadyState etc).  Make sure that the function given accepts
 *      a single parameter.
 *      For example: 'wooba' should correspond to a function declared
 *      like the following: function wooba(http) { ... }
 *    variables - The variables to send along with this request in
 *      standard URL format: 'arg1=val1&arg2=val2&arg3=val3'
 *    asyncronous - True if this function should submit the request
 *      and continue executing (without being sure if it's been sent
 *      yet) or False if it should at least wait until the request is
 *      processed.  Most of the time you can get away with asyncronous
 *      unless this page is somehow going to go away before the request
 *      can be processed (like if you're doing AJAX on a window close
 *      event)
 *
 * Returns:
 *    boolean - True if the operation appears to have succeeded false if
 *      it failed
 */
function sendAJAX(URL, method, responseFunction, variables, asyncronous) {
	// Get our http object that we'll use for our requests
	var http = createRequestObject();
	// Make sure that our method is upper case (if not it will
	// break in FireFox)
	method = method.toUpperCase();
	// Whether or not AJAX appears to have succeeded
	var succesful = false;
	
	if(http != null) {
		//alert('Despite your best efforts, your file -- ' + window.checkinPath + ' -- is still going  to be checked in.  Nice try sucka!');
		// Use AJAX to ask the server to checkin the file
		http.abort();
		// Open a location (to reset the Session timer)
		http.open(method, URL, asyncronous);
		//alert('I\'ve submitted your checkin request to ' + window.sitePath + '/CMCMS/checkinFile.php');
		// We don't need to specify a return function since we won't still be here
		// when he gets back
		eval('http.onreadystatechange = function() { ' + responseFunction + '(http); };');
		// This line must be included for POST to work, otherwise the Server will
		// drop the variables
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		// Make sure the browser does not cache our request and stop sending
		// AJAX every time we request it
		http.setRequestHeader('Cache-Control', 'no-cache');
		// Variables are in standard form: 'arg1=val1&arg2=val2&arg3=val3'
		http.send(variables);
		// Note the apparent success
		succesful = true;
	}
	
	// Return the success or failure of this AJAX operation
	return succesful;
}


/* 
 * Creates and returns an XMLhttp object that can be used for AJAX control
 * (if it can't, it'll return null)
 *
 * Returns:
 *    XMLhttp - An XMLhttp request object that can be used to make AJAX
 *      calls.  If the object could not be instantiated this function will
 *      return null
 */
function createRequestObject() {
	// Default our return value to null in case the HTTP object can't be
	// initialized
	var toReturn = null
	
	// Get the XML http object which will be used to make requests to the server
	// If in any browser but IE, just use the XMLhttp object
	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		toReturn = new XMLHttpRequest();
		if (toReturn.overrideMimeType) {
			// Reset the mime type, in case Firefox breaks because the content
			// returned is not valid XML
			toReturn.overrideMimeType('text/xml');
		}
	} 
	// Otherwise, use the Microsoft ActiveX control
	else if (window.ActiveXObject) { // IE
		try {
			toReturn = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) {
			try {
				toReturn = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (e) {}
		}
	}
	
	// Return the created XMLHTTP object
	return toReturn;
}