﻿function AJAX(args) 
{
    args = args || {};
    this.method = args.method == "get" ? "get" : "post";
    this.requestTimeout = args.timeout || 30;
    this.xmlResponse = args.xml == true;
    this.callBackFunction = null;
    this.httpRequest = this.createHttpRequest();
    if (!this.httpRequest) 
    {
        alert("cannot create httpReauest object...");
    }
}

AJAX.prototype.createHttpRequest = function () 
{
    if (window.XMLHttpRequest) 
    {
        return new XMLHttpRequest;
    }
    try 
    {
        return new ActiveXObject("Msxml2.XMLHTTP");
    } 
    catch (e) 
    {
        try 
        {
            return new ActiveXObject("Microsoft.XMLHTTP");
        } 
        catch (e) 
        {
        }
    }
    return null;
};

AJAX.prototype.handler = function () 
{
    var response = null;
    if (this.httpRequest.readyState == 4) 
    {
        if (this.httpRequest.status == 200) 
        {
            if (this.xmlResponse) 
            {
                response = this.httpRequest.responseXML;
            } 
            else 
            {
                response = this.httpRequest.responseText;
            }
        }
        if (this.callBackFunction != null) 
        {
            this.callBackFunction.call(this, response);
        }
    }
};

AJAX.prototype.request = function (destURL, params, procFunc)
{
    if (!this.httpRequest) 
    {
        return;
    }
    var args = this.hashToStr(params);
    if (this.method == "get") 
    {
        destURL = destURL + "?" + args;
    }
    this.callBackFunction = procFunc;
    this.httpRequest.onreadystatechange = this.handler.bind(this);
    this.httpRequest.open(this.method, destURL, true);
    this.httpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    this.httpRequest.setRequestHeader("Content-length", args.length);
    this.httpRequest.setRequestHeader("Connection", "close");
    if (this.httpRequest.timeout) 
    {
        this.httpRequest.timeout = this.requestTimeout;
    }
    if (this.httpRequest.overrideMimeType) 
    {
        this.httpRequest.overrideMimeType(this.xmlResponse ? "text/xml" : "text/html");
    }
    this.httpRequest.send(args);
};

AJAX.prototype.formRequest = function (destURL, formID, procFunc) 
{
    var args = this.getFormData(formID);
    this.request(destURL, args, procFunc);
};

AJAX.prototype.getFormData = function (formID) 
{
    var theForm = document.getElementById(formID);
    var str = "";
    for (var e = 0; e < theForm.elements.length; e++) 
    {
        var name = theForm.elements[e].name;
        if (!name) 
        {
            continue;
        }
        args[name] = encodeURIComponent(theForm.elements[e].value);
    }
    return args;
};

AJAX.prototype.hashToStr = function (table) 
{
    var args = [];
    for (var name in table) 
    {
        args.push(name + "=" + table[name]);
    }
    return args.join("&");
};

Function.prototype.bind = function (obj) 
{
    var method = this, temp = function () 
    {
        return method.apply(obj, arguments);
    };
    return temp;
};