﻿
    // indigo.communication
    // ***********************************************************************************

    indigo.communication = {};
    indigo.communication._construct = function ()
    {

        // Include Required Name Spaces
        // ****************************
        //indigo.nameSpaceManager.include("indigo.data");

        function _webService(url)
        {
            _webService._constructBase(this);

            function _webServiceCall(service, parameters, async, successFunction, errorFunction)
            {
                _webServiceCall._constructBase(this);

                var _xmlHTTP = null;

                function _dispose()
                {
                    _xmlHTTP = null;
                }

                function _getXMLHTTP()
                {
                    try { _xmlHTTP = new ActiveXObject("Msxml2.XMLHTTP"); }
                    catch (e) { }

                    if (_xmlHTTP == null)
                    {
                        try { _xmlHTTP = new ActiveXObject("Microsoft.XMLHTTP"); }
                        catch (e) { }
                    }

                    if (_xmlHTTP == null && typeof XMLHttpRequest != "undefined")
                        _xmlHTTP = new XMLHttpRequest();
                }

                function _readyStateChanged()
                {
                    switch (_xmlHTTP.readyState)
                    {
                        case 0: // Not initialized
                            break;
                        case 1: // Has been set up
                            break;
                        case 2: // Has been sent
                            break;
                        case 3: // In process
                            break;
                        case 4: // Complete
                            if (_xmlHTTP.status == 200)
                            {
                                if (typeof successFunction != "undefined" && successFunction)
                                    successFunction(_xmlHTTP);
                            }
                            else
                            {
                                if (typeof errorFunction != "undefined" && errorFunction)
                                    errorFunction(_xmlHTTP);
                            }
                            _dispose();
                            break;
                    }
                }
                _webServiceCall._inherits(indigo.object);

                _getXMLHTTP();

                if (_xmlHTTP != null)
                {
                    if (async)
                        _xmlHTTP.onreadystatechange = _readyStateChanged;

                    _xmlHTTP.open("POST", url + "/" + service, async ? true : false);
                    _xmlHTTP.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                    _xmlHTTP.send((parameters ? parameters : ""));

                    if (!async)
                        _readyStateChanged();
                }
                else
                    throw "Browser not supported";
            }

            function _getDataSetFromXMLHTTP(_xmlHTTP)
            {
                var 
                    _xmlDocument = null,
                    _nodDataSet = null,
                    _records = null,
                    _iTable = 0,
                    _iRow = 0,
                    _iColumn = 0,
                    _parserType = 0,
                    _parser = null;

                try
                {
                    try
                    {
                        _xmlDocument = new ActiveXObject("Microsoft.XMLDOM");
                        _xmlDocument.async = false;
                        _xmlDocument.loadXML(_xmlHTTP.responseXML.text);
                        _parserType = 1;
                    }
                    catch (e)
                    {
                        if (typeof DOMParser != "undefined")
                        {
                            _parser = new DOMParser();
                            _xmlDocument = _parser.parseFromString(_xmlHTTP.responseXML.documentElement.textContent, "text/xml");
                            _parserType = 2;
                        }
                    }

                    if (_parserType > 0 && _xmlDocument != null)
                    {
                        var 
                            _columnsPopulated = false,
                            _dataSet = null,
                            _dataTable = null,
                            _dataRow = null,
                            _dataColumn = null;

                        if (_xmlDocument.firstChild.nodeType == 7)
                            _xmlDocument.removeChild(_xmlDocument.firstChild);

                        _dataSet = new indigo.data.dataSet();
                        for (_iTable = 0; _iTable < _xmlDocument.childNodes.length; _iTable++)
                        {
                            if (_xmlDocument.childNodes[_iTable].nodeType == 1)
                            {
                                _columnsPopulated = false;
                                _dataTable = new indigo.data.dataTable();
                                _dataSet.tables.add(_dataTable);
                                for (_iRow = 0; _iRow < _xmlDocument.childNodes[_iTable].childNodes.length; _iRow++)
                                {
                                    if (_xmlDocument.childNodes[_iTable].childNodes[_iRow].nodeType == 1)
                                    {
                                        _dataRow = new indigo.data.dataRow();
                                        _dataTable.rows.add(_dataRow);
                                        for (_iColumn = 0; _iColumn < _xmlDocument.childNodes[_iTable].childNodes[_iRow].childNodes.length; _iColumn++)
                                        {
                                            if (_xmlDocument.childNodes[_iTable].childNodes[_iRow].childNodes[_iColumn].nodeType == 1)
                                            {
                                                if (_parserType == 1)
                                                {
                                                    if (!_columnsPopulated)
                                                        _dataTable.columns.add(new indigo.data.dataColumn(_xmlDocument.childNodes[_iTable].childNodes[_iRow].childNodes[_iColumn].baseName));

                                                    _dataRow.columns.add(new indigo.data.dataColumn(_xmlDocument.childNodes[_iTable].childNodes[_iRow].childNodes[_iColumn].baseName, _xmlDocument.childNodes[_iTable].childNodes[_iRow].childNodes[_iColumn].text));
                                                }
                                                else if (_parserType == 2)
                                                {
                                                    if (!_columnsPopulated)
                                                        _dataTable.columns.add(new indigo.data.dataColumn(_xmlDocument.childNodes[_iTable].childNodes[_iRow].childNodes[_iColumn].tagName));

                                                    _dataRow.columns.add(new indigo.data.dataColumn(_xmlDocument.childNodes[_iTable].childNodes[_iRow].childNodes[_iColumn].tagName, _xmlDocument.childNodes[_iTable].childNodes[_iRow].childNodes[_iColumn].textContent));
                                                }
                                            }
                                        }
                                        _columnsPopulated = true;
                                    }
                                }
                            }
                        }
                        return _dataSet;
                    }
                    else
                    {
                        throw "Browser not supported";
                    }
                }
                catch (_e)
                {
                    throw _e;
                }
                finally
                {
                    _xmlDocument = null;
                    _nodDataSet = null;
                    _records = null;
                    _parser = null;
                    _dataSet = null;
                    _dataTable = null;
                    _dataRow = null;
                    _dataColumn = null;
                }
                return null;
            }

            function _getScalarFromXMLHTTP(_xmlHTTP)
            {
                var 
                    _xmlDocument = null,
                    _nodDataSet = null,
                    _parser = null,
                    _returnValue = null;

                try
                {
                    var _result = (typeof _xmlHTTP.responseXML.text != "undefined" ? _xmlHTTP.responseXML.text : _xmlHTTP.responseXML.documentElement.textContent);
                    switch (_result)
                    {
                        case "false":
                            _returnValue = false;
                            break;
                        case "true":
                            _returnValue = true;
                            break;
                        default:
                            _returnValue = _result;
                    }
                    return _returnValue;
                }
                catch (_e)
                {
                    throw _e;
                }
                finally
                {
                    _xmlDocument = null;
                    _nodDataSet = null;
                    _parser = null;
                    _returnValue = null;
                }
                return null;
            }

            function _getErrorMessage(_xmlHTTP)
            {
                var _errorMessage = (_xmlHTTP.responseText.indexOf(":") > -1 ? _xmlHTTP.responseText.substr(_xmlHTTP.responseText.indexOf(":") + 2) : _xmlHTTP.responseText);
                _errorMessage = (_errorMessage.indexOf("\r\n") > -1 ? _errorMessage.substr(0, _errorMessage.indexOf("\r\n")) : _errorMessage);
                return _errorMessage;
            }

            function _getScalar(service, parameters, async, successFunction, errorFunction, userState)
            {
                var _wsc = null;

                function success(_xmlHTTP)
                {
                    if (typeof successFunction != "undefined" && successFunction)
                    {
                        var _scalar = null;
                        try
                        {
                            _scalar = _getScalarFromXMLHTTP(_xmlHTTP);
                            successFunction(_scalar, userState);
                        }
                        catch (_e) { }
                        finally
                        {
                            _scalar = null;
                            _wsc = null;
                        }
                    }
                }

                function error(_xmlHTTP)
                {
                    if (typeof errorFunction != "undefined" && errorFunction)
                    {
                        var _errorMessage = null;
                        try
                        {
                            _errorMessage = _getErrorMessage(_xmlHTTP);
                            errorFunction(_errorMessage, userState);
                        }
                        catch (_e) { }
                        finally
                        {
                            _errorMessage = null;
                            _wsc = null;
                        }
                    }
                }

                _wsc = new _webServiceCall(service, parameters, async, success, error);
            }
            this.getScalar = _getScalar;

            function _getDataSet(service, parameters, async, successFunction, errorFunction, userState)
            {

                var _wsc = null;

                function success(_xmlHTTP)
                {
                    if (typeof successFunction != "undefined" && successFunction)
                    {
                        var _ds = null;
                        try
                        {
                            _ds = _getDataSetFromXMLHTTP(_xmlHTTP);
                            successFunction(_ds, userState);
                        }
                        catch (_e) { }
                        finally
                        {
                            if (_ds) _ds.dispose();
                            _ds = null;
                            _wsc = null;
                        }
                    }
                }

                function error(_xmlHTTP)
                {
                    if (typeof errorFunction != "undefined" && errorFunction)
                    {
                        var _errorMessage = null;
                        try
                        {
                            _errorMessage = _getErrorMessage(_xmlHTTP);
                            errorFunction(_errorMessage, userState);
                        }
                        catch (_e) { }
                        finally
                        {
                            _errorMessage = null;
                            _wsc = null;
                        }
                    }
                }

                _wsc = new _webServiceCall(service, parameters, async, success, error);

            }
            this.getDataSet = _getDataSet;
        }
        _webService._inherits(indigo.object);
        this.webService = _webService;
    };
    indigo.communication._construct();
