﻿function Ajax()
{
    this.xmlHttp = null;
    this.Screen = null;
    this.Exception = null;
    this.Event = function (r) {}
}

Ajax.prototype =
{
    Create:
        function ()
        {         
            if (this.xmlHttp != null) return true;
           
            var clsids = ["Msxml2.XMLHTTP.6.0","Msxml2.XMLHTTP.5.0",
                        "Msxml2.XMLHTTP.4.0","Msxml2.XMLHTTP.3.0", 
                        "Msxml2.XMLHTTP.2.6","Microsoft.XMLHTTP.1.0", 
                        "Microsoft.XMLHTTP.1","Microsoft.XMLHTTP"];

            try
            {
                this.Exception = null;
                this.xmlHttp = null;
            
                if (navigator.appName != "Netscape")
                {
                    for(var i = 0; i < clsids.length && this.xmlHttp == null; i++)
                    {
                        try 
                        {
                            this.xmlHttp = new ActiveXObject(clsids[i]);
                        }
                        catch(e) { this.xmlHttp = null; }
                    }
                    if (this.xmlHttp != null)
                        this.xmlHttp.onreadystatechange = this.PostBack;
                }
                else
                {
                    this.xmlHttp = new XMLHttpRequest();
                    if (this.xmlHttp != null)
                    {
                        this.xmlHttp.onload = this.PostBack;
                        this.xmlHttp.onerror = this.PostBack;
                    }
                }         
                
                this.Screen = new ScreenBox();
                this.Screen.Create("AjaxSBLock1", 50, true, true);
            }
            catch(e) { this.Exception = e; }            
            return (this.xmlHttp == null) ? false : true;
        },            
        
    PostBack:
        function ()
        {
            try
            {   
                this.Exception = null;                
                if ($Ajax.xmlHttp.readyState == 4 || $Ajax.xmlHttp.readyState == 'complete')
                {
                    var response = $Ajax.xmlHttp.responseText;
                    $Ajax.Event(response);
                    $Ajax.Screen.Hide();
                }
            }
            catch(e) { this.Exception = e; }
        },
        
    Send:
        function (url, args, pbfn, lock)
        { 
            try
            {
                if (this.xmlHttp == null) this.Create();
                if ($Browser.MOZ == true)
                {
                    this.xmlHttp.onload = this.PostBack;
                    this.xmlHttp.onerror = this.PostBack;
                }
                else
                    this.xmlHttp.onreadystatechange = this.PostBack;
            
                this.Event = pbfn;
                if (lock == true) this.Screen.Show();
            
                this.xmlHttp.open('POST', url, true);                 
                this.xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8;");
                this.xmlHttp.setRequestHeader("Content-Length", args.length);
                this.xmlHttp.send(args);
            }
            catch(e) { this.Exception = e; }
        },
        
    SendXML:
        function (url, xml, pbfn, lock)
        { 
            try
            {
                var args = xml;
                if (this.xmlHttp == null) this.Create();
                
                if ($Browser.MOZ == true)
                {
                    this.xmlHttp.onload = this.PostBack;
                    this.xmlHttp.onerror = this.PostBack;
                }
                else
                    this.xmlHttp.onreadystatechange = this.PostBack;
                            
                this.Event = pbfn;
                if (lock == true) this.Screen.Show();

                this.xmlHttp.open('POST', url, true);
                this.xmlHttp.setRequestHeader("Content-Type", "text/xml");
                this.xmlHttp.setRequestHeader("Content-Length", args.length);
                this.xmlHttp.send(args);
            }
            catch(e) { this.Exception = e; }
        }        
}

$Ajax = new Ajax();


