/* ajax.js - An Ajax library i use
 * $Id: ajax.js,v 1.2 2007/01/28 21:43:09 dick Exp $
 *
 * $Log: ajax.js,v $
 * Revision 1.2  2007/01/28 21:43:09  dick
 * whitespace cleanup.
 *
 * Revision 1.1  2007/01/25 06:03:37  dick
 * More XPWebScore skeleton checkin.
 */

////////////////////////////////////////////////////////////////////////////////
function xmlRequest () {
  var request = false;
  if (window.XMLHttpRequest) {
    try {
      request = new XMLHttpRequest();
    } catch(e) {
      request = false;
    }
  } else if (window.ActiveXObject) {
    try {
      request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
      try {
        request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch(e) {
        request = false;
      }
    }
  }
	return request;
}

/************************/
/*  Data Service Class  */
/************************/
function cDataService(caller) {
	this.caller  = caller;
	this.request = null;
	this.working = false;
	this.stopID  = null;
}

////////////////////////////////////////////////////////////////////////////////
cDataService.prototype.submitRequest = function() {
	this.stopService();
	eval(this.caller.showWait);
	this.request = xmlRequest();
	if (this.request) {
		this.working=true;
		this.stopID = setTimeout(this.caller.name+".service.timeout()", this.caller.maxWait);
		try {
			var responseFunc = this.caller.name+".service.handleResponse();";
			this.request.onreadystatechange = function() { eval(responseFunc) };
			this.request.open("POST", this.caller.url, true);
			this.request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
			var x=this.caller.getParams();
			this.request.send(x);
		} catch (e) {}
	}
}

////////////////////////////////////////////////////////////////////////////////
cDataService.prototype.handleResponse = function() {
  if (this.request.readyState == 4) {
		this.endService();
		eval(this.caller.hideWait);
		try {
	    if (this.request.status == 200) {
				try {
					this.caller.refresh(this.evalResponse(this.request.responseText));
				} catch (e) {
					this.caller.handleFailure(g.errorBadData, e);
				}
	    } else if (this.request.status != 0) {
				this.caller.handleFailure(g.errorBadStatus);
	    }
	  } catch (e) {}
	}
}

////////////////////////////////////////////////////////////////////////////////
cDataService.prototype.timeout = function() {
	this.stopService();
	this.caller.handleFailure(g.errorTimeout);
}

////////////////////////////////////////////////////////////////////////////////
cDataService.prototype.stopService = function () {
	if (this.working) this.request.abort();
	this.endService();
}

////////////////////////////////////////////////////////////////////////////////
cDataService.prototype.endService = function () {
	if (this.stopID) clearTimeout(this.stopID);
	this.stopID = null;
	this.working = false;
}

////////////////////////////////////////////////////////////////////////////////
cDataService.prototype.evalResponse = function(x) {
	if (x==null || x=="" || x.substring(0,1) != "{") return null;
	return eval('('+x+')');
}

////////////////////////////////////////////////////////////////////////////////
function StringBuffer() {
    this.buffer = [];
}

StringBuffer.prototype.append = function(string) {
  this.buffer.push(string);
  return this;
};

StringBuffer.prototype.prepend = function(string) {
  this.buffer.unshift(string);
  return this;
};

StringBuffer.prototype.toString = function() {
  return this.buffer.join("");
};

StringBuffer.prototype.isEmpty = function() {
    return (this.buffer.length == 0);
}

StringBuffer.prototype.dispose = function() {
    this.buffer = [];
}
