function requestDataSet(dataSource) {
	this.engine;
	if (dataSource.responseXML) {
		this.engine = new xmlDataSet(dataSource.responseXML);
	} else {
		this.engine = new txtDataSet(dataSource.responseText);
	}
	this.rowSet = this.engine.rowSet;
	this.metaData = this.engine.metaData;
	this.metaLabel = this.engine.metaLabel;
	var recSet = [];
	var rowIdx = 0; // Set record index to before first record; first record index = 1;

	//==============================================================================
	this.recNo = function() {
		return rowIdx;
	}
	//==============================================================================
	this.rowCount = function () {
		return this.rowSet.length-1;
	}
	//==============================================================================
	this.goto = function(recno) {
		if ((recno > 0) && (recno < this.rowSet.length)) {
			rowIdx = recno;
			recSet = this.rowSet[rowIdx];
			return true;
		} else if (recno == 0) {
			recSet = [];
			return true;
		} else { return false;}
	}

	//==============================================================================
	this.first = function() {
		return this.goto(1);
	}
	//==============================================================================
	this.prior = function() {
		return this.goto(rowIdx-1);
	}

	//==============================================================================
	this.next = function() {
		return this.goto(rowIdx+1);
	}

	//==============================================================================
	this.last = function() {
		return this.goto(this.rowCount());
	}

	//==============================================================================
	this.beforeFirst = function() {
		return this.goto(0);
	}

	//==============================================================================
	this.valueByName = function (FieldName) {
		var retValue = new ReturnValue();

		retValue.hasError = true;
		retValue.Value =  "";

		if (this.metaData[0] == "") {
			retValue.hasError = true;
			retValue.Value =  "*** รูปแบบ dataSource ไม่ถูกต้อง ***";
		}

		if (recSet.length == 0) {
			retValue.hasError = true;
			retValue.Value = "";
		}

		var fieldIdx = -1;
		for (var i=0; i < this.metaData.length; i++) {
			if (this.metaData[i] == FieldName) {
				fieldIdx = i;
				break;
			}
		}

		if (fieldIdx == -1) {
			retValue.hasError = true;
			retValue.Value =  "*** ไม่ปรากฏ Field : "+FieldName+" ***";
		}

		if (this.rowSet.length > 1) {
			retValue.hasError = false;
			retValue.Value = recSet.fieldValues[fieldIdx];
		}

		return retValue;
	}
}
// =========================================================================================
function xmlDataSet(dataSource){
	var root = dataSource.documentElement;

	// Retrive Metadata from XML
	var metaField = root.selectSingleNode("/DATASET/METADATA");
	this.metaData = null;
	this.metaLabel = null;
	this.rowSet = null;
	if (metaField.hasChildNodes) {
		this.metaData = new Array();
		this.metaLabel = new Array();
		var fieldCount = metaField.childNodes.length;
		for (var idx=0;idx<fieldCount;idx++) {
			var tmpMeta =metaField.childNodes[idx];
			this.metaData[idx] = tmpMeta.getAttribute("NAME");
			this.metaLabel[idx] = tmpMeta.getAttribute("LABEL");
		}

		// Retrive Data from XML
		var rawData = root.selectNodes("/DATASET/RECORD");
		var recCount = rawData.length;
		this.rowSet = new Array();
		this.rowSet[0] = [];
		for (var idx=0; idx<recCount; idx++) {
			var tmpData =rawData[idx];
			var rowIdx = this.rowSet.length;
			this.rowSet[rowIdx] = new dataRecord();
			for (var id=0;id <this.metaData.length; id++ ) {
				var tmpValue = tmpData.getElementsByTagName(this.metaData[id]).item(0).text;
				this.rowSet[rowIdx].addField(id,tmpValue);
			}
		}
	}
}
// =========================================================================================
function txtDataSet(dataSource) {
	var rawData = dataSource.split("\r\n");

	for(var i=0; i < this.rowSet.length; i++) {
		var tmpData = rawData[i].replace(/@#/g,"\r\n");
		rawData[i] = tmpData;
	}

	this.metaData = rawData[0].split("!");
	this.metaLabel = this.metaData;
	this.rowSet = new Array();
	this.rowSet[0] = [];
	for (var idx=1; idx<rawData.length; idx++) {
		if (rawData[idx] != "") {
			var rowIdx = this.rowSet.length;
			this.rowSet[rowIdx] = new dataRecord();
			var recSet = rawData[idx].split("!");
			for (var id=0;id <recSet.length; id++ ) {
				this.rowSet[rowIdx].addField(id,recSet[id]);
			}
		}
	}
}
// =========================================================================================
function dataRecord(){
	this.fieldValues = new Array();
	//==============================================================
	this.addField = function (fieldIdx,fieldValue) {
		this.fieldValues[fieldIdx] = fieldValue;
		return this.fieldValues[fieldIdx];
	}
}

function ReturnValue() {
	this.hasError = false;
	this.Value = "";
}

