//******************************************************
// Released under the OSI MIT license http://www.opensource.org/licenses/mit-license.php

//Copyright (c) 2005 Mike Surel

//Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
//documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
//the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
//to permit persons to whom the Software is furnished to do so, subject to the following conditions:

//The above copyright notice and this permission notice shall be included in all copies or substantial portions of
//the Software.

//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO 
//THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
//CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.

// Sends the request to the page specified in the object.
// We use a closure for the event handler. If the variable
// isn't global it loses scope so the use of a closure seemed
// like a more encapsulated and efficient way to do this.
//******************************************************
function jpAjax_SendRequest() 
{
    // Class variables Page and Request need to be set in order to do the
    // request.
    this._httpRequestObject.open('get', this.Page + this.Request);
    
    // The following two variables are used to the closure
    // can see the objects
    var myHTTPObject = this._httpRequestObject;
    var myTarget = this.TargetElement;
    
    // Event handler for the page being loaded. Closure
    // keeps it local
    this._httpRequestObject.onreadystatechange = function()
        {
            if (myHTTPObject.readyState == 4)
            {
                document.getElementById(myTarget).innerHTML = myHTTPObject.responseText;
            }
        };
    
    // Send the request to the web server
    this._httpRequestObject.send(null);
}

function jpAjax_GetDynamicImage()
{
    // Class variables Page and Request need to be set in order to do the
    // request.
    this._httpRequestObject.open('get', this.Page + this.Request);
    
    // The following two variables are used to the closure
    // can see the objects
    var myHTTPObject = this._httpRequestObject;
    var myTarget = this.TargetElement;
    
    // Event handler for the page being loaded. Closure
    // keeps it local
    this._httpRequestObject.onreadystatechange = function()
        {
            if (myHTTPObject.readyState == 4)
            {
                document.getElementById(myTarget).src = myHTTPObject.responseText;
            }
        };
    
    // Send the request to the web server
    this._httpRequestObject.send(null);
}
//******************************************************
// Our class for doing AJAX type requests. The constructure
// requires that you specify an element and a page on the
// server to send the request to. The request itself
// is optional.

// Public properties that can be set are:

// TargetElement    -   The HTML element in the page to update
// Page             -   Web page to call for server side processing
// Request          -   Additional information to send to the Page
//                      Often URL parameters. Handling these are the
//                      responsibility of the server
//******************************************************
function JpAjax(targetElement, page, request)
{
    // Make this null if the required arguments are not
    // specified.
    if (arguments.length < 2)
    {
        alert('You need to specify an HTML Element and a page');
        
        return;
    }

    // Initialize our properties
    this.TargetElement = targetElement;
    this.Page = page;
    if (request == null)
    {
        this.Request = "";
    }
    else
    {
        this.Request = request;
    }
    

    // Handle IE and non-IE browsers
    if(navigator.appName == "Microsoft Internet Explorer")
    {
        this._httpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
        if (this._httpRequestObject == null)
        {
	        this._httpRequestObject = new ActiveXObject("Msxml2.XMLHTTP");
        }
    }
    else
    {
        this._httpRequestObject = new XMLHttpRequest();
    }
}

// Expose the SendRequest function for client code to call.
JpAjax.prototype.SendRequest = jpAjax_SendRequest;
JpAjax.prototype.GetDynamicImage = jpAjax_GetDynamicImage;