﻿
    // Web Service
    // ***********
    function webService(url)
    {
        function webServiceCall(service, parameters, async, successFunction, errorFunction)
        {
            var _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);
                        }
                        delete _xmlHTTP;
                        break;
                }
            }

            _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,
                        _currentTableName = "",
                        _thisTableName = "";

                    if (_xmlDocument.firstChild.nodeType == 7)
                        _xmlDocument.removeChild(_xmlDocument.firstChild);

                    if (_xmlDocument.childNodes.length > 0 && _xmlDocument.childNodes[0].nodeType == 1)
                    {
                        _dataSet = new dataSet();
                        for (_iRow = 0; _iRow < _xmlDocument.childNodes[0].childNodes.length; _iRow++)
                        {
                            if (_xmlDocument.childNodes[0].childNodes[_iRow].nodeType == 1)
                            {
                                _thisTableName = (_parserType == 1 ? _xmlDocument.childNodes[0].childNodes[_iRow].baseName : _xmlDocument.childNodes[0].childNodes[_iRow].tagName);
                                if (_thisTableName != _currentTableName)
                                {
                                    _dataTable = new dataTable(_thisTableName);
                                    _dataSet.tables.push(_dataTable);
                                    _currentTableName = _thisTableName;
                                    _columnsPopulated = false;
                                }

                                _dataRow = new dataRow();
                                _dataTable.rows.push(_dataRow);
                                for (_iColumn = 0; _iColumn < _xmlDocument.childNodes[0].childNodes[_iRow].childNodes.length; _iColumn++)
                                {
                                    if (_xmlDocument.childNodes[0].childNodes[_iRow].childNodes[_iColumn].nodeType == 1)
                                    {
                                        if (_parserType == 1)
                                        {
                                            if (!_columnsPopulated)
                                                _dataTable.columns.push(new dataColumn(_xmlDocument.childNodes[0].childNodes[_iRow].childNodes[_iColumn].baseName));

                                            _dataRow.columns.push(new dataColumn(_xmlDocument.childNodes[0].childNodes[_iRow].childNodes[_iColumn].baseName, _xmlDocument.childNodes[0].childNodes[_iRow].childNodes[_iColumn].text));
                                        }
                                        else if (_parserType == 2)
                                        {
                                            if (!_columnsPopulated)
                                                _dataTable.columns.push(new dataColumn(_xmlDocument.childNodes[0].childNodes[_iRow].childNodes[_iColumn].tagName));

                                            _dataRow.columns.push(new dataColumn(_xmlDocument.childNodes[0].childNodes[_iRow].childNodes[_iColumn].tagName, _xmlDocument.childNodes[0].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
                    {
                        _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;
    }

    function dataColumn(name, value)
    {
        this.name = name;
        this.value = value;
    }

    function dataRow()
    {
        this.columns = [];
    }
    dataRow.prototype.getValue = function (columnName)
    {
        for (var i = 0; i < this.columns.length; i++)
        {
            if (this.columns[i].name.toLowerCase() == columnName.toLowerCase())
                return this.columns[i].value;
        }
        return "";
    };

    function dataTable(name)
    {
        this.name = (name ? name : "");
        this.columns = [];
        this.rows = [];
    }

    function dataSet()
    {
        this.tables = [];
    }
    dataSet.prototype.getTable = function (name)
    {
        for (var i = 0; i < this.tables.length; i++)
        {
            if (this.tables[i].name == name)
                return this.tables[i];
        }
        return null;
    };
