/**
 * @author mpoisson
 * @version 1.0
 * 
 * Class used to handle Ajax services responses
 */

function ServiceResult() {
	this.errorCode = 0;
	this.errorDescription = "";
	this.start = 0;
	this.rows = 0;
	this.total = 0;
	this.currentPosition = 0;
	this.dataArray = null;
}

ServiceResult.prototype.getErrorCode = function() {
	return this.errorCode;
};

ServiceResult.prototype.setErrorCode = function(number) {
	this.errorCode = parseInt('0' + number, 10);
};

ServiceResult.prototype.getDescription = function() {
	return this.errorDescription;
};

ServiceResult.prototype.setErrorDescription = function(text) {
	this.errorDescription = text;
};

ServiceResult.prototype.hasErrors = function() {
	return (this.errorCode != 0);
};

ServiceResult.prototype.loadXML = function (xmlDocument) {
	
	try {
		this.errorCode = xmlDocument.getElementsByTagName('error')[0].childNodes[0].nodeValue;
		this.errorDescription = xmlDocument.getElementsByTagName('desc')[0].childNodes[0].nodeValue;
	} catch (e) {
		// Error parsing header
	}
	
	if (this.errorCode == 0) {
		var dataNode = xmlDocument.getElementsByTagName('data')[0];
		try {
			
			this.start = dataNode.attributes["start"].value;
			this.rows = dataNode.attributes["number"].value;
			this.total = dataNode.attributes["total"].value;
		} catch (e) {
			this.start = 0;
			this.rows = 0;
			for (i=0;i < dataNode.attributes.length; i++) {
				switch (dataNode.attributes[i].name) {
					case 'start':
						this.start = dataNode.attributes[i].value;
						break;
					
					case 'number':
						this.rows = dataNode.attributes[i].value;
						break;
					
					case 'total':
						this.total = dataNode.attributes[i].value;
						break;
				}
			}
		}
		
		if (dataNode.childNodes.length) {
			var temp = dataNode.childNodes[0].nodeValue;
			this.dataArray = temp.substring(0, temp.lastIndexOf("|#|")).split("|#|");
			this.rows = this.dataArray.length;		
			if (this.total ==0) {
				this.total = this.rows;
			}
		}
	} 
};

ServiceResult.prototype.loadData = function (data) {
	if (data.length) {
		this.errorCode = 0;
		this.errorDescription = "";
	} else {
		this.errorCode = 1000;
		this.errorDescription = "No data received";
	}
	if (this.errorCode == 0) {
		this.dataArray = data.substring(0, data.lastIndexOf("|#|")).split("|#|");
		this.rows = this.dataArray.length;		
	}
};

ServiceResult.prototype.getStart = function() {
	return this.start;
};

ServiceResult.prototype.rowCount = function() {
	return this.rows;
};

ServiceResult.prototype.getTotal = function() {
	return this.total;
};

/*******************************************************
 * Resultset iteration methods
 *******************************************************/
ServiceResult.prototype.moveNext = function () {
	if (this.currentPosition < this.rowCount())	this.currentPosition++;
};

ServiceResult.prototype.movePrev = function () {
	if (this.currentPosition > 0) this.currentPosition--;
};

ServiceResult.prototype.moveFirst = function () {
	if (this.currentPosition > 0) this.currentPosition = 0;
};

ServiceResult.prototype.getCurrentRow = function() {
	return trim(this.dataArray[this.currentPosition]);
};

ServiceResult.prototype.getRow = function(index) {
	return trim(this.dataArray[index]);
};

ServiceResult.prototype.hasNext = function() {
	return (this.currentPosition < this.rowCount());
};



