    //<![CDATA[
    
// (c) Mikael Högqvist, ZIB 2006-2007

// Query library for SPARQL

function Query(querystr, cb) {

    var queryservice = 'http://' +  window.location.hostname + ':' + window.location.port + '/query/query';
    var req = init();
    req.onreadystatechange = queryReqCb;    
    
    function init() {
        var req;
    // branch for native XMLHttpRequest object
        if(window.XMLHttpRequest && !(window.ActiveXObject)) {
            try {
                req = new XMLHttpRequest();
            } catch(e) {
                req = false;
            }
        // branch for IE/Windows ActiveX version
        } else if(window.ActiveXObject) {
            try {
                req = new ActiveXObject("Msxml2.XMLHTTP");
            } catch(e) {
                try {
                    req = new ActiveXObject("Microsoft.XMLHTTP");
                } catch(e) {
                	req = false;
                }
    	    }
        }    
        
        return req;
    }
    
    this.query = function() {
        if(req) {
            q = escape(querystr);
            req.open("GET", queryservice + '?query=' + q + '&format=json', true);
            req.setRequestHeader('ACCEPT', 'application/sparql-results+json');
            req.send("");
    	}
    }

    function queryReqCb() {
     // only if req shows "loaded"
        if (req.readyState == 4) {
        // only if "OK"
            if (req.status == 200) {
                if(cb) {
                    cb(req.responseText);
                }
            } else {
                alert("There was a problem retrieving the XML data:\n" + req.statusText);
            }
        }
    }
}
    //]]
