/** * Set up some IE-detection variables, to handle non-standard behavior. * * IS_IE: boolean that detects IE, from sarissa.sourceforge.net. * NAME_OF_CLASS: name of the 'class' attribute, used in set/get/removeAttribute() * **/ var IS_IE = document.all && window.ActiveXObject && navigator.userAgent.toLowerCase().indexOf("msie") > -1 && navigator.userAgent.toLowerCase().indexOf("opera") == -1; var NAME_OF_CLASS = IS_IE ? 'className' : 'class'; /** * Update an element by retrieving a url * * This function will create an XmlHttpRequest object, retrieve a url from it, and * call a callback function so that the element can be updated with the response. * * An exception will be thrown if XmlHttpRequest objects are not supported. * * The advantage of this function over using XmlHttpRequest directly is that this * function also takes care of feedback messages, if you pass in a status_element. * * The pre_send_callback function is optional. If given, it will be called just * before xmlhttp.send(). It is intended to be used to prepare the status_element, * by making it visible, changing its cursor, or something like that. * * The when_ready_callback function is required, and will be called when * readyState == 4. It will be passed one argument, the xmlhttp object. It is * assumed that the callback function is a closure, and any other necessary variables * (like the element to update) will be available within the function. * If the status_element was used and set up with pre_send_callback, then it should * be set back the way it was in when_ready_callback. * * The status_element argument is optional. If given, then for readyState values * 1, 2, and 3, status_element.innerHtml will be set to some feedback text. * * @argument url The url to GET * @argument pre_send_callback A callback function to call just before sending the request * @argument when_ready_callback A callback function to call when readyState == 4 * @argument status_element An element to contain status messages when readyState < 4 */ function update_element_from_url(url, pre_send_callback, when_ready_callback, status_element) { var xmlhttp = HTTP(); if ( !xmlhttp ) { throw new Error("XmlHttpRequest not available"); } xmlhttp.open("GET", url, true); xmlhttp.onreadystatechange = function() { if (status_element) { if (xmlhttp.readyState == 1) { status_element.innerHTML = "Loading.."; } else if (xmlhttp.readyState == 2) { status_element.innerHTML = "Loading...."; } else if (xmlhttp.readyState == 3) { status_element.innerHTML = "Loading......"; } } if (xmlhttp.readyState == 4) { when_ready_callback(xmlhttp); } }; if (pre_send_callback != null) { pre_send_callback(); } xmlhttp.send(null); } /** * Create an XMLHttpRequest object * * This will return an XmlHttpRequest object for various browsers. * If the object isn't supported, an alert will be displayed. * */ function HTTP() { if (window.XMLHttpRequest) { return new XMLHttpRequest(); } var xmlhttp = null; var objects_to_try = ["Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"]; for (var i=0; i < objects_to_try.length; i++) { try { xmlhttp = new ActiveXObject(objects_to_try[i]); return xmlhttp; } catch (e) { // Try the next one } } alert("Your browser does not support AJAX methods. Please use IE 6+ or Netscape 7+ (or compatible)."); return null; } /* -------------------------------------------------------------------- */ /* This is a utility method which finds the source node of an event under multiple browsers */ function getSrc(evt) { evt = (evt) ? evt : ((event) ? event : null); if (evt) { var elem = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null); return elem; } return; } /* -------------------------------------------------------------------- */ /* This utility method is used to attach event handlers to elements or the window object. */ function add_event_handler(object, event_name, handler_function) { if (object.addEventListener) { object.addEventListener(event_name, handler_function, false); } else if (object.attachEvent) { object.attachEvent('on' + event_name, handler_function); } }