// file: HttpRpc.js

var ERROR_PREFIX = "ERROR:";

// class: HttpRpc

// constructor: HttpRpc
//
// Parameters:
//   url - url for request
//   errorFunc - function to handle error response
//   responseFunc - function to handle successful response
//   finalizerFunc - function to run after response regardless
//   method - request method (GET or POST)
//   bSync - if true, forces synchronous mode
//
function HttpRpc(url, errorFunc, responseFunc, finalizerFunc, method, bSync) {
    if(url.slice(0, 4) == 'http') {
    this.url = url;
    } else {
        if(url.charAt(0) != '/') {
            url = '/' + url;
        }
        this.url = 'http://' + window.location.host + url;
    }
    if(method) {
        this.method = method;
    } else {
        this.method = 'POST';
    }
    if(responseFunc) {
        this.responseFunc = responseFunc;
    } else {
        this.responseFunc = function(req) {};
    }
    if(errorFunc) {
        this.errorFunc = errorFunc;
    } else {
        this.errorFunc = function(req) {};
    }
    if(finalizerFunc) {
        this.finalizerFunc = finalizerFunc;
    } else {
        this.finalizerFunc = function(req) {};
    }
    this.bSync = bSync;
}

// func: getTransport
//   Gets the transport object for the request.
//
// Returns:
//   The transport object
//
HttpRpc.prototype.getTransport = function() {
    var xRequest = null;
    if(window.XMLHttpRequest) {
        xRequest = new XMLHttpRequest();
    } else if ( window.ActiveXObject ) {
        try {           
            xRequest = new ActiveXObject('Msxml2.XMLHTTP');
        } catch(err) {
            xRequest = new ActiveXObject('Microsoft.XMLHTTP');
        }
    }
    return xRequest;
}

// func: sendRequest
//   Sends the request.
// 
// Parameters:
//   paramStr - parameters for the request
//
HttpRpc.prototype.sendRequest = function(paramsStr) {
    if(!paramsStr) {
        paramsStr = '';
    }
    this.req = this.getTransport();
//    var req = this.getTransport();
    this.req.open(this.method, this.url, !this.bSync);
    this.req.setRequestHeader(
        "Content-Type","application/x-www-form-urlencoded; charset=utf-8");
    var oThis = this;
    this.req.onreadystatechange = function() { oThis.handleAjaxResponse() };
    this.req.send(paramsStr);
}

// func: handleAjaxResponse
//   Handles the response.
//
// Parameters:
//   req - the request
//
HttpRpc.prototype.handleAjaxResponse = function() {
    try {
        if(this.req.readyState == 4) {
            if(this.isSuccess(this.req)) {
                this.responseFunc(this.req);
            } else {
                this.errorFunc(this.req);
            }
        }
    }
    finally {
        if (this.req.readyState == 4) {
            this.finalizerFunc(this.req);
        }
    }
    if (this.req.readyState == 4) {
        // clears up memory leak in IE 6
        delete this.req;
        return;
    }
}

// func: isSuccess
//   Determines if the response was successful
//
// Parameters:
//   req - the request
//
// Returns:
//   true if the request was successful, false otherwise
//
HttpRpc.prototype.isSuccess = function(req) {
	try {
    	if (req == null || req.status == undefined) // safari bug, sometimes status is undefined
    	{
       		return true;
    	}
    	return ( (req.status >= 200 && req.status < 300)) && 
        	( (req.responseText == null) || req.responseText.slice(0, ERROR_PREFIX.length) != ERROR_PREFIX);
	} 
	catch (exception) {
		return false;
	}
}


