﻿/// AJAX CLASS ///
//© 2008 SeatQuest LLC.

//http://www.hunlock.com/blogs/Snippets:_Synchronous_AJAX
function AJAX(url,callback,post_params)
{
    post_params = post_params || null;

    //ELI PRIVATE DECLERATIONS
    //-------------------------
    this.type=this.get_type();
    var request=this.get_request();
    
    //ELI PUBLIC DECLERATIONS
    //-------------------------
    this.get = function(url,callback,post_params)
    {
         var async=false;
         if(callback){async=true;}
         if(post_params!=null)
         {
            request.open("POST", url, async);
            request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            request.setRequestHeader("Content-length", post_params.length);
            request.setRequestHeader("Connection", "close");
            request.setRequestHeader("User-agent", navigator.userAgent);
            
         }
         else
            request.open("GET", url, async);
         if (callback)
         {
            //ELI this is a good example of closure since we are passing the request objec into the anonymous function.
            request.onreadystatechange = function() 
            {
                if (request.readyState==4 || request.readyState=="complete") 
                {
                    if(callback){callback(request,url);}
                }
            }             
         }
         request.send(post_params);
         if(!async){this.response = request;return request;}else{this.response =null;return null;}
    }
    
    //ELI trigger a get if values are in the contructor.
    if(url){this.get(url,callback,post_params);}

};

//ELI get the browser type
AJAX.prototype.get_type=function()
{
    if (window.XMLHttpRequest){return "GECKO";} else {return "IE";}
};

//ELI AJAX Reuqest.
AJAX.prototype.get_request = function() 
{
    //if(type=="IE"){return new ActiveXObject("Microsoft.XMLHTTP");}
    if (this.type == "IE") { return new ActiveXObject('MSXML2.XMLHTTP.3.0'); }
    if (this.type == "GECKO") { return new XMLHttpRequest(); }
    return null;
};