/*
* A wonderful AJAX Object
* http://www.hunlock.com/blogs/The_Ultimate_Ajax_Object
***************************************************************/
function ajaxObject(url, callbackFunction) {
  var that=this;      
  this.updating = false;
  this.abort = function() {
    if (that.updating) {
      that.updating=false;
      that.AJAX.abort();
      that.AJAX=null;
    }
  }
  this.update = function(passData,postMethod) { 
    if (that.updating) { return false; }
    that.AJAX = null;                          
    if (window.XMLHttpRequest) {              
      that.AJAX=new XMLHttpRequest();              
    } else {                                  
      that.AJAX=new ActiveXObject("Microsoft.XMLHTTP");
    }                                             
    if (that.AJAX==null) {                             
      return false;                               
    } else {
      that.AJAX.onreadystatechange = function() {  
        if (that.AJAX.readyState==4) {             
          that.updating=false;                
          that.callback(that.AJAX.responseText,that.AJAX.status,that.AJAX.responseXML);        
          that.AJAX=null;                                         
        }                                                      
      }                                                        
      that.updating = new Date();                              
      if (/post/i.test(postMethod)) {
        var uri=urlCall+'?'+that.updating.getTime();
        that.AJAX.open("POST", uri, true);
        that.AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        that.AJAX.setRequestHeader("Content-Length", passData.length);
        that.AJAX.send(passData);
      } else {
        var uri=urlCall+'?'+passData+'&timestamp='+(that.updating.getTime()); 
        that.AJAX.open("GET", uri, true);                             
        that.AJAX.send(null);                                         
      }              
      return true;                                             
    }                                                                           
  }
  var urlCall = url;        
  this.callback = callbackFunction || function () { };
}

/*
* This is how to create the ajax object.
* The first item in the array is the URL to call
* The second item is the function that is called when the response returns - Optional
**************************************************************************************
var myRequest = new ajaxObject('http://www.somedomain.com/process.php', processData);


* If you are only going to use the called function once, you can also do this
*****************************************************************************
var myRequest = new ajaxObject('http://www.somedomain.com/process.php');
		myRequest.callback = function(responseText, responseStatus, responseXML) {
			// do stuff here.
		}


* No request is actually made until an update is called, like this:
* The parameters are sent as a GET reqeust
* Corresponding PHP variables ($id and $color) are set.
*******************************************************************
		myRequest.update('id=1234&color=blue');  // Server is contacted here.


* To send a POST request
* Regular $_POST variables are set - 'POST' is not case sensitive
*************************************************************************
		myRequest.update('id=1234&color=blue','POST');
		

* Example of a good function used to catch the response
* responseText, responseStatus, and responseXML are the three possible reponses
************************************************************************************
function processData(responseText, responseStatus, responseXML) {
  if (responseStatus==200) {
    alert(responseText);
  } else {
    alert(responseStatus + ' -- Error Processing Request);
  }
}


* Check to see if a request is still processing
*************************************************************************
if (myRequest.updating) {
  alert('this request is being processed.');
} else {
  alert('this object is idle');
}


* Check the amount of time the request has spent in process
**************************************************************************
if (myRequest.updating) {
  now=new Date();
  alert('This request is '+now-myRequest.updating+' miliseconds old.');
}


* Abort the request
****************************************************************************
myRequest.abort();


*/


var getElementsByClassName = function (className, tag, elm){
    if (document.getElementsByClassName) {
        getElementsByClassName = function (className, tag, elm) {
            elm = elm || document;
            var elements = elm.getElementsByClassName(className),
            nodeName = (tag)? new RegExp("\\b" + tag + "\\b", "i") : null,
            returnElements = [],
            current;
            for(var i=0, il=elements.length; i<il; i+=1){
                current = elements[i];
                if(!nodeName || nodeName.test(current.nodeName)) {
                    returnElements.push(current);
                }
            }
            return returnElements;
        };
    }
    else if (document.evaluate) {
        getElementsByClassName = function (className, tag, elm) {
            tag = tag || "*";
            elm = elm || document;
            var classes = className.split(" "),
                classesToCheck = "",
                xhtmlNamespace = "http://www.w3.org/1999/xhtml",
                namespaceResolver = (document.documentElement.namespaceURI === xhtmlNamespace)? xhtmlNamespace : null,
                returnElements = [],
                elements,
                node;
            for(var j=0, jl=classes.length; j<jl; j+=1){
                classesToCheck += "[contains(concat(' ', @class, ' '), ' " + classes[j] + " ')]";
            }
            try {
                elements = document.evaluate(".//" + tag + classesToCheck, elm, namespaceResolver, 0, null);
            }
            catch (e) {
                elements = document.evaluate(".//" + tag + classesToCheck, elm, null, 0, null);
            }
            while ((node = elements.iterateNext())) {
                returnElements.push(node);
            }
            return returnElements;
        };
    }
    else {
        getElementsByClassName = function (className, tag, elm) {
            tag = tag || "*";
            elm = elm || document;
            var classes = className.split(" "),
                classesToCheck = [],
                elements = (tag === "*" && elm.all)? elm.all : elm.getElementsByTagName(tag),
                current,
                returnElements = [],
                match;
            for(var k=0, kl=classes.length; k<kl; k+=1){
                classesToCheck.push(new RegExp("(^|\\s)" + classes[k] + "(\\s|$)"));
            }
            for(var l=0, ll=elements.length; l<ll; l+=1){
                current = elements[l];
                match = false;
                for(var m=0, ml=classesToCheck.length; m<ml; m+=1){
                    match = classesToCheck[m].test(current.className);
                    if (!match) {
                        break;
                    }
                }
                if (match) {
                    returnElements.push(current);
                }
            }
            return returnElements;
        };
    }
    return getElementsByClassName(className, tag, elm);
};

function addEvent(obj, evType, fn) {
    if (obj.addEventListener){
        obj.addEventListener(evType, fn, false);
        return true;
    } else if (obj.attachEvent) {
        var r = obj.attachEvent("on"+evType, fn);
        return r;
    } else {
        return false;
    }
}

function getTarg(jsEvent) {
    var targ;
    if (jsEvent.target) targ = jsEvent.target;
    else if (jsEvent.srcElement) targ = jsEvent.srcElement;
    if (targ.nodeType == 3) // defeat Safari bug
    targ = targ.parentNode;
    return targ;
}

function stopDef(e) {
	e = e||event;
    if(e.preventDefault) e.preventDefault();
    else e.returnValue = false;
}
