﻿

this.ajax = null;
this.Ajax = function (parent, callback) {
    var self = this;
    self.parent = parent;
    self.callback = callback;
    self.xmlhttp = new XMLHttpRequest();
    self.urlRequest = "php/request.php"; 
    self.urlPost = ""; //TODO
    self.xmlDocument = null;

    self.request = function (sqlArray) {
        var sql="";
        for (var i = 0; i < sqlArray.length; i++) {
            sql += sqlArray[i] + "&";
        }
        self.xmlhttp.onreadystatechange = self.receive;
        var url = self.urlRequest + "?" + sql + "&ms=" + new Date().valueOf();
        self.xmlhttp.open("GET", url, true);
        self.xmlhttp.send(null);
    }

    self.receive = function () {
        if (self.xmlhttp && self.xmlhttp.readyState == 4) {
            if (self.xmlhttp.status == 200) {
                self.xmlhttp.onreadystatechange = null;
                var dataArray = new Array();
                var data;
                data = self.xmlhttp.responseXML;
                self.data = data;
                if (data) {
                    data.records = data.childNodes[0].childNodes;
                    /*data.fieldNames = new Array();
                    if (data.records.length > 0) {
                        for (var i = 0; i < data.records[0].childNodes.length; i++)
                            data.fieldNames.push(data.records[0].childNodes[i].tagName);
                    }*/
                    dataArray.push(data);
                } else dataArray.push(self.xmlhttp.responseText);
                try {
                    self.callback.apply(self, dataArray);
                } catch (e) {
                    alert("ajax.js exception: " + e.message);
                    return;
                } //alert("Page unloaded while fetching data."); return; }

            }
        }
        return null;
    }

    var newXml = function (rootTagName, namespaceURL) {
        if (!rootTagName) rootTagName = "";
        if (!namespaceURL) namespaceURL = "";
        self.xmlDocument = document.implementation.createDocument(namespaceURL, rootTagName, null); //W3C standard
        return self.xmlDocument;
    }
    var createNode = function (name, value) {
        var node;
        var nodeType = 1; // element
        if (value != null) nodeType = 3; //text
        if (nodeType == 1) node = self.xmlDocument.createElementNS("", name); else node = self.xmlDocument.createTextNode(value);
        return node;
    }
    this.createDataset = function () {
        var dummy = newXml();
        var dataset = createNode("dataset");
        dummy.appendChild(dataset);
        return dummy;
    }
    this.appendRecord = function (id, value) {
        var records = createNode("record");
        var field = createNode("id");
        field.appendChild(createNode("id", id));
        records.appendChild(field);
        var field = createNode("value");
        field.appendChild(createNode("value", value));
        records.appendChild(field);
        self.xmlDocument.lastChild.appendChild(records);
    }
    

}
try { init.addLibraries(); } catch (e) { alert('ajax.js calling init.js: function init.addLibraries exception: ' + e.message); }