// JavaScript Document
function EzRemoteScripter() {
    //private variables
    var _XmlHttpRequest = null;
    var _This = null;
   
    //public properties
    this.GetResponseXML = function() {
        return (_XmlHttpRequest) ? _XmlHttpRequest.responseXML : null;
    }
   
    this.GetResponseText = function() {
        return (_XmlHttpRequest) ? _XmlHttpRequest.responseText : null;
    }
   
    this.GetXMLHttpRequestObj = function() {
        return _XmlHttpRequest;
    }
   
    //public methods
    this.InitXmlHttpRequest = function(Method, CallUri) {
        _InitXmlHttpRequest();
        _This = this;
        switch( arguments.length ) {
            case 2:
                _XmlHttpRequest.open(Method, CallUri);   
                break;
            case 3:
                _XmlHttpRequest.open(Method, CallUri, arguments[2]);
                break;
        }
       
        if( arguments.length >= 4) {
            _XmlHttpRequest.open(Method, CallUri, arguments[2], arguments[3]);
        }
		
		if(Method == 'POST') {
			_XmlHttpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
			_XmlHttpRequest.setRequestHeader('Content-length', '0');
		}
    }
   
    this.Commit = function(Data) {
        if( _XmlHttpRequest ) {
            _XmlHttpRequest.send(Data);
        }
    }
   
    this.Close    = function() {
        if( _XmlHttpRequest ) {
            _XmlHttpRequest.abort();
        }
    }
   
    //public events
    this.OnUninit         = function() {};
    this.OnLoading         = function() {};
    this.OnLoaded         = function() {};
    this.OnInteractive     = function() {};
    this.OnSuccess        = function() {};
    this.OnFailure         = function() {};
   
    //private events
    function _OnUninit()         { _This.OnUninit(); };
    function _OnLoading()         { _This.OnLoading(); };
    function _OnLoaded()         { _This.OnLoaded(); };
    function _OnInteractive()     { _This.OnInteractive(); };
    function _OnSuccess()         { _This.OnSuccess(); };
    function _OnFailure()         { _This.OnFailure(); };
   
    //private methods
    function _InitXmlHttpRequest() {
        _XmlHttpRequest = _GetXmlHttpRequest();
        _XmlHttpRequest.onreadystatechange = _StateHandler;
    }
   
    function _StateHandler() {
        switch(_XmlHttpRequest.readyState) {
            case 0:
                window.setTimeout("void(0)", 100);
                _OnUninit();
                break;
            case 1:
                window.setTimeout("void(0)", 100);
                _OnLoading();
                break;
            case 2:
                window.setTimeout("void(0)", 100);
                _OnLoaded();
                break;
            case 3:
                window.setTimeout("void(0)", 100);
                _OnInteractive();
                break;
            case 4:
                if (_XmlHttpRequest.status == 200) {
                    _OnSuccess();
                } else {
                    _OnFailure();
                }
                return;
                break;
        }
    }
   
    function _GetXmlHttpRequest() {
        var requester
        try {
            requester = new XMLHttpRequest();
        } catch (error) {
            try {
                requester = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (error) {
                return null;
            }
        }
        return requester;
    }
}