﻿/* Ajax (c) Kagilo QQ432706 */
var Ajax = 
{
    UNINITIALIZED:  0,
    LOADING:        1,
    LOADED:         2,
    INTERACTIVE:    3,
    COMPLETED:      4,

    pool: [],

    createAjax: function()
    {
        try { return new XMLHttpRequest() } catch (e) { }
        try { return new ActiveXObject("Msxml2.XMLHTTP") } catch (e) { }
        try { return new ActiveXObject("Microsoft.XMLHTTP") } catch (e) { }

        return false;
    },
        
    getAjax: function()
    {
        var pool = Ajax.pool;

        for (var name in pool)
        {
            if (pool[name].readyState == Ajax.UNINITIALIZED || pool[name].readyState == Ajax.COMPLETED)
            {
                return pool[name];
            }
        }

		pool.push(Ajax.createAjax());
        return pool[pool.length-1];
    },

    request: function(url, options)
    {
        var ajax = Ajax.getAjax();

        var o = 
        {
            method:         "get",
            asynchronous:   true,
            encoding:       "UTF-8",
            params:         "",
            cache:          false,
            headers:        {}
        };
        for (var name in options)
        {
            o[name] = options[name];
        }
        o.method = o.method.toLowerCase();
        if (o.method != "get" && o.method != "post")
        {
            o.method = "get";
        }

        if (o.method == "get" && o.params != "")
        {
            url += (url.indexOf("?") >= 0 ? "&" : "?") + o.params;
        }
        if (o.method == "post")
        {
            o.headers["Content-type"] = "application/x-www-form-urlencoded" + (o.encoding ? "; charset=" + o.encoding : "");
        }
        if (!o.cache)
        {
            o.headers["If-Modified-Since"] = 0;
        }

        try
        {
            ajax.open(o.method, url, o.asynchronous, o.username, o.password);

            for (var name in o.headers)
            {
                ajax.setRequestHeader(name, o.headers[name]);
            }

            var body = o.method == "post" ? o.params : null;

            if (o.asynchronous)
            {
                ajax.onreadystatechange = function()
                {
                    if(ajax.readyState == Ajax.COMPLETED)
                    {
                        if(ajax.status == 200 || ajax.status == 304)
                        {
                            if (o.callback)
                            {
                                o.callback(ajax);
                            }
                            else
                            {
                                eval(ajax.responseText);
                            }
                            
                        }
                        else
                        {
                            if (o.error)
                            {
                                o.error(ajax);
                            }
                            else
                            {
                                alert("error loading page\n\n" + ajax.status + ":" + ajax.statusText);
                            }
                        }
                    }
                }
            }
                
            ajax.send(body);

            if (!o.asynchronous)
            {
                return ajax;
            }

            return true;

        }
        catch (e)
        {
            return false;
        }
    },

    update: function(eid, url, options)
    {
        options = options || {};

        options._callback = options.callback;

        options.callback = function(x)
        {
            if (typeof eid == "string")
            {
                document.getElementById(eid).innerHTML = x.responseText;
            }
            else
            {
                for (var name in eid)
                {
                    document.getElementById(eid[name]).innerHTML = x.responseText;
                }
            }
            
            if (options._callback)
            {
                options._callback(x);
            }
        }

        Ajax.request(url, options);
    }
};