//============
//  TDataSet
//
//	Version 1.0 (19/01/2008)
//


TDataSet.prototype.Depurar   = false;

TDataSet.prototype._aCampos  = null;
TDataSet.prototype._aDataSet = null;
TDataSet.prototype._iRecNo   = -1;


TDataSet.prototype.Append = function ()
{
	this._iRecNo = this._aDataSet.length;
	this._aDataSet [this._iRecNo] = new Object ();
	return (this._aDataSet [this._iRecNo]);
}


TDataSet.prototype.asBoolean = function (sCampo)
{
	var Field  = this.ByName (sCampo);
	var Tipo   = typeof Field;
	
	if (Tipo == "string")
	{	Field = Field.toUpperCase ();
		return Field == 'S' || Field == 'Y' || Field == '1' || Field == 'TRUE';
	}
	if (Tipo == 'number') return Field != 0;
	if (Tipo == 'boolean') return Field;
	return false;
}


TDataSet.prototype.asFloat = function (sCampo)
{
	return (parseFloat (this.ByName (sCampo)));
}


TDataSet.prototype.asInteger = function (sCampo)
{
	return (parseInt (this.ByName (sCampo)));
}


TDataSet.prototype.ByName = function (sCampo, vValor)
{
	try
	{	if (typeof (vValor) != 'undefined') this._aDataSet [this._iRecNo][this._aCampos [sCampo].Pos] = vValor;
		return (this._aDataSet [this._iRecNo][this._aCampos [sCampo].Pos]);
	} catch (exception)
	{	if (this.Depurar) alert ('ERROR al leer el Campo ' + sCampo);
		return ('');
	}
}


TDataSet.prototype.ByPos = function (iPos, vValor)
{
	try
	{	if (this.Eof ()) return ('&nbsp;');
		if (typeof (vValor) != 'undefined') this._aDataSet [this._iRecNo][iPos] = vValor;
		return (this._aDataSet [this._iRecNo][iPos]);
	} catch (exception)
	{	if (this.Depurar) alert ('ERROR al leer el Campo ' + iPos);
		return ('');
	}
}


TDataSet.prototype._Campos = function (aCampos)
{
	var oAux = null;
	var i    = 0; 
	
	if (aCampos)
	{	this._aCampos = new Array ();
		for (i = aCampos.length - 1; i >= 0; i--)
		{	oAux      = new Object ();
			oAux.Pos  = i;
			oAux.Tipo = aCampos [i].Tipo;
			this._aCampos [aCampos [i].Nombre] = oAux;
		}
	}
}


TDataSet.prototype.Delete = function ()
{
	var i = this._iRecNo;
	var l = this.RecordCount () - 1;
	
	if (! this.Eof ())
	{	while (i < l)	this._aDataSet [i++] = this._aDataSet [i]
		this._aDataSet.pop ();
	}
}


TDataSet.prototype.Eof = function ()
{
	return (this._iRecNo < 0 || this._iRecNo >= this._aDataSet.length);
}


TDataSet.prototype.Field = function ()
{
	try
	{	return (this._aDataSet [this._iRecNo]);
	} catch (exception)
	{	if (this.Depurar) alert ('ERROR al leer el registro.');
		return ('');
	}
}


TDataSet.prototype.Fields = function ()
{
	var Fields = new Array ();
	
	for (Field in this._aCampos) Fields [Field] = 0;
	return Fields;
}


TDataSet.prototype.First = function ()
{
	if (this._aDataSet.length)	this._iRecNo = 0;
	else this._iRecNo = -1;
}


TDataSet.prototype.Locate = function (sCampo, vValor)
{
	if (! this.Eof () && this.ByName (sCampo) == vValor) return (true);
	this.First ();
	while (! this.Eof () && this.ByName (sCampo) != vValor) this.Next ();
	return (! this.Eof ())
}


TDataSet.prototype.Next = function ()
{
	if (! this.Eof ()) this._iRecNo++;
	return (this.Eof ());
}


TDataSet.prototype.RecNo = function (iValor)
{
	if (typeof (iValor) != 'undefined' && iValor > 0 && iValor <= this.RecordCount ()) this._iRecNo = iValor - 1;
	return (this._iRecNo + 1);
}


TDataSet.prototype.RecordCount = function ()
{
	try
	{	return (this._aDataSet.length);
	} catch (exception)
	{	if (this.Depurar) alert ('El DataSet no se ha inicializado.');
		return (0);
	}
}


TDataSet.prototype.Vaciar = function ()
{
	this._aDataSet = null;
	this._iRecNo   = -1;
}


function TDataSet (aCampos, aDatos)
{
	if (typeof (aCampos) == 'undefined') aCampos = null;
	if (typeof (aDatos) == 'undefined') aDatos = null;
	
	this._Campos (aCampos);
	this._aDataSet = aDatos;
	if (aDatos && this._aDataSet.length) this._iRecNo = 0;
}


function DataSet ()
{
	var Aux     = null;
	var aResult = new Array ();
	var i       = arguments.length - 1;
	var j       = 0;
	
	if (i > 0)
	{ Aux = eval ('(' + arguments [0] + ')');
		for (; i > 0; i--)
		{	for (j = Aux.length - 1; j >= 0 && Aux [j].DataSet != arguments [i]; j--);
			if (j >= 0) aResult.unshift (new TDataSet (Aux [j].Campos, Aux [j].Datos));
			else aResult.unshift (new TDataSet ());
		}
	}
	switch (aResult.length)
	{	case 0: return (new TDataSet ());
		case 1: return (aResult [0]);
	}
	return (aResult);
}


//=================================================================================
//=================================================================================
//=================================================================================
//=================================================================================

// Hay que mirarla mejor
TDataSet.prototype.Copiar = function (oDataSet, iInicio, iRegistros)
{
	if (iInicio > oDataSet.RecordCount ()) iInicio = oDataSet.RecordCount () - iRegistros;
	if (iInicio < 1) iInicio = 1;
	iInicio--;
	this._aDataSet = oDataSet._aDataSet.slice (iInicio, iInicio + iRegistros);
}



