//  gjax.js - Greg's AJAX.
//  Provides simple access to background data transfer mechanisms.
//  REQUIRES: Nothing.
//  Written by Greg Bowler, December 2009. www.g105b.com/gtools

/*  Gjax can be used to quickly set up AJAX connections on websites.
 *  Use a new Gjax object for each simultaneous AJAX connection.
 *
 *  EXAMPLE INTERACTION WITH PHP:
 *  <?php
 *      echo 'Your name is: ' . $_POST["name"];
 *  ?>
 *  -----
 *  <div id="nameGoesHere"></div>
 *  <input id="nameValue" type="text" name="name" value="Put your name here">
 *  <input type="button" value="Click" onClick="SayName()">
 *  -----
 *  function SayName()
 *  {
 *      var gjaxObj = new Gjax();
 *      gjaxObj.output = document.getElementById("nameGoesHere");
 *      gjaxObj.MakePostRequest("name.php",
 *          document.getElementById("nameValue").value);
 *
 *      // DIV nameGoesHere will show the inputted name once Php script triggers.
 *  }
 */
 
function Gjax(output, trigger, verbose)
{
    var parent = this;
    
    this.output = (output == undefined)
        ? null : output;                            // Refers to any html object to output the response to.
    this.trigger = (trigger == undefined)
        ? null : output;                            // Optional: Function to trigger when data response is received.
    this.verbose = (verbose == undefined)
        ? false : verbose;                          // Optional: Setting verbose to true alerts errors.

    this.httpRequest = false;
    this.mimeType = "text/html";                    // Mime type must be set if using other than text/html.
    
    // Resets and creates new this.httpRequest
    this.CreateHttpRequest = function()
    {
        parent.httpRequest = false;                 // Reset the httpRequest.
        
        if(window.XMLHttpRequest)                   // Compliant browsers.
        {
            parent.httpRequest = new XMLHttpRequest();
            if(parent.httpRequest.overrideMimeType)
            {
                parent.httpRequest.overrideMimeType(parent.mimeType);
            }
        }
        else if(window.ActiveXObject)               // IE
        {
            try
            {
                parent.httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch(e)
            {
                try
                {
                    parent.httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch(e)
                {
                }
            }
        }
        else
        {
            // XMLHttpObject can not be created.
            if(parent.verbose) alert("Gjax error: \nBrowser compliancy unknown. Cannot proceed.");
        }
        
        if(!parent.httpRequest)
        {
            // httpRequest should be set to a new instance at this point.
            if(parent.verbose) alert("Gjax error: \nCannot create XmlHttp instance.");
            return false;
        }
    }
    
    // Retrieve a GET request to a given URL.
    this.MakeGetRequest = function(url)
    {
        parent.CreateHttpRequest();
        
        parent.httpRequest.onreadystatechange = parent.OutputResponse;
        parent.httpRequest.open('GET', url, true);
        parent.httpRequest.send(null);
    }
    
    // Create and retrieve a POST request to a given URL, with given parameters.
    this.MakePostRequest = function(url, parameters)
    {   
        //alert(url + ", " + parameters);
        parent.CreateHttpRequest();
        
        parent.httpRequest.onreadystatechange = parent.OutputResponse;
        parent.httpRequest.open('POST', url, true);
        parent.httpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        parent.httpRequest.setRequestHeader("Content-length", parameters.length);
        parent.httpRequest.setRequestHeader("Connection", "close");
        parent.httpRequest.send(parameters);
    }
    
    // Triggered from an HttpRequest onReadyStateChange.
    this.OutputResponse = function()
    {
        if(parent.httpRequest.readyState == 4)
        {
            // HTTP Status 200: OK
            if(parent.httpRequest.status == 200)
            {
                result = parent.httpRequest.responseText;
                if(parent.output == null)
                {
                    // Gjax object has no output object - alert the result (only in verbose mode).
                    if(parent.verbose)alert("Gjax result: \n" + result);
                }
                else
                {
                    // Place returned content into output object.
                    parent.output.innerHTML = result;

                    if(parent.trigger != null)
                    {
                        // Trigger the given function once response data has been received.
                        parent.trigger();
                    }
                }
            }
            else
            {
                // Throw error when HTTP status is anything other than 200 (OK)
                if(parent.verbose)alert("Gjax error: \nThere was a problem with the request.");
            }
        }
    }
}