// Description: This file supports AXAJ routines used to interact fluidly with the server,
//	such as maintaining a session, loging in, saving form field contents to the db without
//	reloading the page, etc.
// Source: Many of the basic AJAX object routines were taken from various sources
//	but then heavily or lightly modified.  Code is original unless otherwise noted.
// Author: Nathanael Dewhurst
// Date: 03/02/2007

// Mostly taken from http://www.hunlock.com/blogs/Concurrent_Ajax
function ajaxObject(response_el, url) {                                    // This is the object constructor
   var that = this;                                                    // A workaround for some javascript idiosyncrocies
   var updating = false;                                             // Set to true if this object is already working on a request
   this.callback = function() {}                                     // A post-processing call -- a stub you overwrite.

   this.update = function(passData, httpMethod) {                                // Initiates the server call.
//   		alert("you've successfully called the update function");
      if (updating == true) { return false; }                          // Abort if we're already processing a call.
      updating = true;                                                 // Set the updating flag.
      // The following try-catch blocks were taken from http://www.w3schools.com/ajax/ajax_source.asp
      var xmlHttp=null;
		try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
		}
		catch (e) {
			// Internet Explorer
			try {
				xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch (e) {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
		}
	  if (xmlHttp == null) {                                              // If we couldn't initialize xmlHttp...
         alert("Your browser does not support AJAX.\n" +
		 	"Please update your browser in order to use this website.  For help, contact technical support.");                // Sorry msg.
         return false;                                               // Return false (WARNING - SAME AS ALREADY PROCESSING!)
      } else {
		xmlHttp.onreadystatechange = function() {                      // When the browser has the request info..
//			alert("readystate's a changin' to:\n" + xmlHttp.readyState);
            if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") { //   see if the complete flag is set.
            	if (xmlHttp.status == 200) {
					//            	alert("Response text: " + xmlHttp.responseText +
					//						"\nresponseElement: " + responseElement);
					if (responseElement) 
						responseElement.innerHTML = xmlHttp.responseText; //   It is, so put the new data in the object's response_el
					delete xmlHttp; //   delete the xmlHttp object since it's done.
					//Why can we delete this and then access its responseText attribute?? It seems to be working at least once, though. (ND - 2008-03-18)
					updating = false; //   Set the updating flag to false so we can do a new request
					that.callback(xmlHttp.responseText); //   Call the post-processing function.
				}
            }                                                        // End Ajax readystate check.
         }															// End create post-process fucntion block.
        // NRD: I'm not sure this timestamp addition is necessary, because we're modifying the http headers to prevent caching,
		//	but I'll use it for both POST and GET requests anyway just in case (03/02/2007)
		var timestamp = new Date();                                 // Get a new date (this will make the url unique)
		 if(httpMethod == "GET") {                                        // GET (we'll pass data via the URI)
//		 	alert("Gettin' There...");
	         var uri=urlCall+'?'+passData+'&timestamp='+(timestamp*1);   // Append date to url (so the browser doesn't cache the call)
//	         alert("URI: " + uri);
	         xmlHttp.open("GET", uri, true);                                // Open the url this object was set-up with.
	         xmlHttp.send(null);                                            // Send the request.
	    }
	    else {																// POST (more secure, and we can pass more data)
	    	var uri=urlCall+'?timestamp='+(timestamp*1);
			xmlHttp.open("POST", uri, true);
			xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xmlHttp.send(passData);
	    }
         return true;                                                // Everything went a-ok.
      }                                                              // End Ajax setup aok if/else block
   }

   // This area set up on constructor calls.
   var responseElement = document.getElementById(response_el);                     // Remember the response_el associated with this object.
   var urlCall = url;                                                // Remember the url associated with this object.
}                                                                    // End AjaxObject



