/* The parameters passed to the function are as follows:
 * url -        the url of the script that we wanna request. You can use either
 *              full or relative to your server urls.
 * elmnts -     this is either the id of the element that will contain the
 *              response from our request, or can also be a whole array of
 *              elements
 * vars -       the variables you want to send to your ajax script
 * affect -     a choice of either src or innerHTML (default is innerHTML)
 * loadingMsg - if this parameter is set, after the request has been send, the
 *              content of the changed element(s) will be set to whatever this
 *              variable is set, i.e. "Loading! Please wait...".
 */
function ajaxObj(url, elmnts, vars, affect, loadingMsg)
{
    this.obj = new Object();
    this.url = "/inc/ajax/" + url + ".php";
    this.loadInto = elmnts;
    this.affect = affect;
    this.vars = "get=" + elmnts + "&data=" + vars;

    // If we have set a loading message here it will be put into the changed elemnt(s)
    if (loadingMsg) {
        // If we wanna change just one element, simply do it using it's id
        if (typeof(this.loadInto) != "object") {
            document.getElementById(this.loadInto).innerHTML = loadingMsg;
        } else {
            // Or if the 'elmnts' parameter is an array - change all the elements of the array
            for (i in this.loadInto) {
                document.getElementById(this.loadInto[i]).innerHTML = loadingMsg;
            }
        }
    }
    // This prototype is used to create our request, send it and handle it
    this.init();
}

/* This function tries if different objects are available, until it finds one
 * that works, cause all the major browsers use different techniques to send the
 * request
 */
ajaxObj.prototype.create = function()
{
    try {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch(e) {
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch(e) {
            try {
                xmlHttp = new XMLHttpRequest();
            } catch(e) {
                return false;
            }
        }
    }
    this.obj = xmlHttp;
};

ajaxObj.prototype.handle = function()
{
    var o = this.obj;
    var into = this.loadInto;
    var affect = this.affect;

    // Set an event handler that is triggered everytime the readystate of the
    // object has changed
    o.onreadystatechange = function()
    {
        // If the readyState is 4, the request has been completed - we can proceed with using the response
        if (o.readyState == 4) {
            // If we want to change just one element - set it's innerHTML equal to the response
            if (typeof(into) != "object") {
                if (affect == 'src') {
                    document.getElementById(into).src = o.responseText;
                } else {
                    document.getElementById(into).innerHTML = o.responseText;
                }
              // Otherwise - we have more than one elements to change. We must
              // first split the data that is returned into parts for each one
              // of the elements and then update them
            } else {
                temp = o.responseText.split("@@");
                if (affect == 'src') {
                    for (i in into) {
                        document.getElementById(into[i]).src = temp[i];
                    }
                } else {
                    for (i in into) {
                        document.getElementById(into[i]).innerHTML = temp[i];
                    }
                }
            }
        }
    }
};

// This prototype simply sends the request to the desired url
ajaxObj.prototype.send = function()
{
    this.obj.open("POST", this.url, true);
    this.obj.setRequestHeader("If-Modified-Since", "Thu, 1 Jan 1970 00:00:00 GMT");
    this.obj.setRequestHeader("X-Requested-With", "XMLHttpRequest");
    this.obj.setRequestHeader("Cache-Control", "no-cache");
    this.obj.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    this.obj.send(this.vars);
};

/* This prototype calls all the other ones in the proper order. It's not really
 * necessary, cause we can just call this prototypes from within the object
 * constructor, but I use it, cause I'm planning to extend this object in the
 * future.
 */
ajaxObj.prototype.init = function()
{
    this.obj = null;
    this.create();
    this.handle();
    this.send();
};