
var sai = {
    _turl: "sai",
    _lurl: "sai",
    _gui: "box",
    _sessionid: new Date().getTime() + "" + Math.floor(9000 * Math.random() + 1000),
    _sessionOptions: {},

    translateText: function(text, languagePair) {
        return sai.sendRequest(text, languagePair, "translate");
	},
	
	lookup: function(text, languagePair) {
        var options = {};
        options["gui"] = sai._gui;
        options["lp"] = languagePair;
        options["format"] = "html";
        return sai.sendGenericRequest(sai._lurl, "lookup", options, text);
	},

    sendRequest: function(text, languagePair, service) {
        var options = {};
        options["gui"] = sai._gui;
        options["lp"] = languagePair;
        return sai.sendGenericRequest((service == "lookup" ? sai._lurl : sai._turl), service, options, text);
    },

    sendGenericRequest: function(url, service, options, data) {
        var xmlReq;
        if(window.XMLHttpRequest) {
            xmlReq = new XMLHttpRequest();
        }
        else if(window.ActiveXObject) {
            xmlReq = new ActiveXObject("Microsoft.XMLHTTP");
        }
        options["sessionid"] = sai._sessionid;
        var query = "?";
        if(sai._sessionOptions) {
            for(var name in sai._sessionOptions) {
                if(sai._sessionOptions.hasOwnProperty(name)) {
                    query += name + "=" + sai._sessionOptions[name] + "&";
                }
            }
        }
        for(var name in options) {
            if(options.hasOwnProperty(name)) {
                query += name + "=" + options[name] + "&";
            }
        }
        query += "service=" + service;
        xmlReq.open("POST", url + query, true);
        xmlReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
        xmlReq.send(data);
        return new WebServiceRequest(xmlReq, service);
    }
};

function WebServiceRequest(xmlReq, resultType) {
	this._xmlReq = xmlReq;
    this._parsedResponse = null;
    this._resultType = resultType;

    this.isComplete = function() {
        return this._xmlReq.readyState == 4;
	};
	
	this.isSuccess = function() {
        if(this.isComplete()) {
            if(this._xmlReq.status == 200) {
                return this.getParsedResponse().error == "";
            }
            else {
                return false;
            }
        }
        else {
            return false;
        }
	};

    this.getResultType = function() {
        return this._resultType;
    };

    this.getOutput = function () {
        return this.getParsedResponse().body;
	};
	
	this.getErrorMessage = function() {
        if(this._xmlReq.status == 200) {
            return this.getParsedResponse().error;
        }
        else {
            return "HTTP Error " + this._xmlReq.status + ": " + this._xmlReq.statusText;
        }
	};
	
	this.cancel = function() {
        if(!this.isComplete()) {
            this._xmlReq.abort();
        }
    };

    this.getParsedResponse = function() {
        if(this._parsedResponse == null) {
            this._parsedResponse = new SAIResponse(this._xmlReq.responseText);
        }
        return this._parsedResponse;
    };
}

function SAIResponse(txtResponse) {
    this.body = "";
    this.error = "";

    this._init = function(txtResponse) {
        var bodyIndex = txtResponse.indexOf("body=");
        if ( bodyIndex >= 0 ) {
            var endIndexThirdLine = txtResponse.indexOf("\n", bodyIndex);
            this.body = txtResponse.substring(endIndexThirdLine + 1);
        }
        else {
            var errorIndex = txtResponse.indexOf("error=");
            if (errorIndex >= 0) {
                var endIndexSecondLine = txtResponse.indexOf("\n", errorIndex);
                this.error = txtResponse.substring(errorIndex+"error=".length, endIndexSecondLine);
            }
        }
    };

    this._init(txtResponse);
}


