// JavaScript Document
function tablaDeDatos1bit(xmlDOM,xPath){
this.xpath=xPath;
this.xmldom=xmlDOM;
this.columnas= Array();
this.columnas_nombres= Array();
this.columnas_titulos= Array();
this.columnas_tipoDeDatos= Array();
this.columnas_ID= Array();
this.columnas_orden= Array();
this.columnas_ordenamiento= Array();
this.qfilas=0;
this.qcolumnas=0;
this.fila_corriente=0;
this.valores= Array();
this.presentacionSelectNombres= Array();
this.presentacionSelectObjetos= Array();
this.presentacionSelectValores= Array();
this.presentacionSelectTextos= Array();
this.presentacionDivNombres= Array();
this.presentacionDivObjetos= Array();
this.presentacionDivCampos= Array();

// poblar
var raiz=xmlDOM.documentElement;
// armar la tabla de Datos
var oret=buscarSubDOM(raiz, xPath, this);
if (oret == null)
	return null;
else
	return this;

}
tablaDeDatos1bit.prototype.refrescarTabla = function (){
	var raiz=this.xmldom.documentElement;
	var xPath=this.xpath;
	// resetar los datos
	this.columnas.length=0;
	this.columnas_titulos.length=0;
	this.columnas_tipoDeDatos.length=0;
	this.columnas_ID.length=0;
	this.columnas_orden.length=0;
	this.columnas_ordenamiento.length=0;
	this.qfilas=0;
	this.qcolumnas=0;
	this.fila_corriente=0;
	this.valores.length=0;
	var oret=buscarSubDOM(raiz, xPath, this);
	if (oret == null)
		return null;
	else
		return this;

}
tablaDeDatos1bit.prototype.decodificarColNombre = function(columna_nombre) {
	var valida= false;
	var columna= 0;
	var	ipos;
	var scambio;
	for (var i=0;i<this.qcolumnas;i++){
		if (this.columnas_nombres[i] == columna_nombre){
			columna = i;
			valida = true;
			}
		}
	if (valida) {
		for(var i=0;i<this.qfilas;i++) {
			swork= this.valores [i] [columna];
			ipos= swork.indexOf("~", 0);
			while (ipos != -1) {
				switch (swork.charAt(ipos +1)) {
					case 'n':
						scambio= String.fromCharCode(241);break;
					case 'N':
						scambio= String.fromCharCode(209); break;
					case 'a':
						scambio= String.fromCharCode(225); break;
					case 'A':
						scambio= String.fromCharCode(193); break;
					case 'e':
						scambio= String.fromCharCode(233); break;
					case 'E':
						scambio= String.fromCharCode(201); break;
					case 'i':
						scambio= String.fromCharCode(237); break;
					case 'I':
						scambio= String.fromCharCode(205); break;
					case 'o':
						scambio= String.fromCharCode(243); break;
					case 'O':
						scambio= String.fromCharCode(211); break;
					case 'u':
						scambio= String.fromCharCode(250); break;
					case 'U':
						scambio= String.fromCharCode(218); break;
					case 'v':
						scambio= String.fromCharCode(252); break;
					case 'V':
						scambio= String.fromCharCode(220); break;
					case 'c':
						scambio= String.fromCharCode(39); break;
					case 'd':
						scambio= String.fromCharCode(186); break;
					case 'y':
						scambio= "&"; break;
					}
				swork= swork.replace("~"+swork.charAt(ipos +1), scambio); 
				ipos= swork.indexOf("~", 0);
				}
			this.valores [i] [columna]= swork;
			}
		return 1;
		}
	else
		return null; 
}


tablaDeDatos1bit.prototype.setFilaCorriente = function (ifila){
	this.fila_corriente= ifila;
}

tablaDeDatos1bit.prototype.getFilaCorriente = function() {
	return this.fila_corriente;
}

tablaDeDatos1bit.prototype.getValorxFilaIDColID = function(fila,columna) {
	if (fila <= this.qfilas && columna <= this.qcolumnas) 
		return this.valores [fila] [columna];
	else
		return null;
}

tablaDeDatos1bit.prototype.getValorxFilaIDColNombre = function(fila,columna_nombre) {
	if ( fila > this.qfilas)
		return null;
	var valida= false;
	var columna= 0;
	for (var i=0;i<this.qcolumnas;i++){
		if (this.columnas_nombres[i] == columna_nombre){
			columna = i;
			valida = true;
			}
		}
	if (valida)
		return this.valores [fila] [columna];
	else
		return null; 
}

tablaDeDatos1bit.prototype.getFilaIDxValorColNombre = function(valor,columna_nombre) {
	if ( valor == null)
		return null;
	var valida= false;
	var columna= 0;
	var fila;
	for (var i=0;i<this.qcolumnas;i++){
		if (this.columnas_nombres[i] == columna_nombre){
			columna = i;
			valida = true;
			}
		}
	if (valida) {
		var cuenta= 0;
		for (var i=0;i<this.qfilas;i++){
			if (this.valores [i] [columna] == valor) {
				cuenta ++;
				fila= i;
				}
			}
		if (cuenta == 1)
			return fila;
		else
			return -1;
		}
	else
		return null; 
}

tablaDeDatos1bit.prototype.getValorxValorColNombre = function(columna_nombre_ret, valor_busqueda, columna_nombre_busqueda) {
	if ( valor_busqueda == null)
		return null;
	if ( columna_nombre_busqueda == null || columna_nombre_busqueda == "")
		return null;
	if ( columna_nombre_ret == null || columna_nombre_ret == "")
		return null;
	var	fila= this.getFilaIDxValorColNombre (valor_busqueda, columna_nombre_busqueda);
	return this.getValorxFilaIDColNombre (fila, columna_nombre_ret);
}

tablaDeDatos1bit.prototype.setValorxFilaIDColID = function(fila,columna, valor) {
	if (fila <= this.qfilas && columna <= this.qcolumnas) {
		this.valores [fila] [columna] = valor;
		return 1;
		}
	else
		return null;
}

tablaDeDatos1bit.prototype.setValorxFilaIDColNombre = function(fila, columna_nombre, valor) {
	if ( fila > this.qfilas)
		return null;
	var valida= false;
	var columna= 0;
	for (var i=0;i<this.qcolumnas;i++){
		if (this.columnas_nombres[i] == columna_nombre){
			columna = i;
			valida = true;
			}
		}
	if (valida) {
		this.valores [fila] [columna] = valor;
		return 1;
		}	
	else
		return null; 
}
tablaDeDatos1bit.prototype.presentarSelect = function(nombre, presentar, valor){
	// ver si ya esta registrado
	var indice=-1;
	for (var i=0;i<this.presentacionSelectNombres.length;i++){
		if (this.presentacionSelectNombres[i]==nombre) {indice=i};
		}
	if (indice == -1){
		// registrarlo
		var oselect= buscarElementoXTipoYNombre("select",nombre);
		if (oselect==null){return null};
		var texto= presentar;
		var valor= valor;
//		var texto=oselect.options[0].presentar.substring(1,oselect.options[0].presentar.length -1);
//		var valor=oselect.options[0].valor.substring(1,oselect.options[0].valor.length -1);
//		alert ("presentar: "+texto);
//		var valor=oselect.options[0].value.substring(1,oselect.options[0].value.length -1);
//		var texto=oselect.options[0].text.substring(1,oselect.options[0].text.length -1);
		// verificar las columnas
		var existevalor=false;
		var existetexto=false;
		for (var i=0;i<this.qcolumnas;i++){ 
			if (this.columnas_nombres[i] == valor) {existevalor=true}
			if (this.columnas_nombres[i] == texto) {existetexto=true}
			}
		if (!(existevalor && existetexto)) {return null};
		this.presentacionSelectNombres.push(nombre);
		this.presentacionSelectObjetos.push(oselect);
		this.presentacionSelectValores.push(valor);
		this.presentacionSelectTextos.push(texto);
		indice=this.presentacionSelectNombres.length - 1;
		}
	// presentar
//	alert ("items antes de borrar: " + this.presentacionSelectObjetos[indice].length +" - filas: " +this.qfilas);
	var nodoborrado;
	while (this.presentacionSelectObjetos[indice].length > 0)
		nodoborrado= this.presentacionSelectObjetos[indice].removeChild(this.presentacionSelectObjetos[indice].lastChild);
//	alert ("items despues de borrar: " + this.presentacionSelectObjetos[indice].length +" - filas: " +this.qfilas);
	for (var i=0, primero=true;i<this.qfilas;i++){
		var nuevoelemento=document.createElement("option");
		this.presentacionSelectObjetos[indice].appendChild(nuevoelemento);
		this.presentacionSelectObjetos[indice].options[i].index=i;
		this.presentacionSelectObjetos[indice].options[i].value=this.getValorxFilaIDColNombre(i,this.presentacionSelectValores[indice]);
		this.presentacionSelectObjetos[indice].options[i].text=this.getValorxFilaIDColNombre(i,this.presentacionSelectTextos[indice]);
		if (primero){
			this.presentacionSelectObjetos[indice].options[i].selected=true;
//			this.presentacionSelectObjetos[indice].options[i].valor="[" + this.presentacionSelectValores[indice] + "]";
//			this.presentacionSelectObjetos[indice].options[i].presentar="[" + this.presentacionSelectTextos[indice] + "]";
			primero=false;
			}
		else 
			this.presentacionSelectObjetos[indice].options[i].selected=false;
	}
}

tablaDeDatos1bit.prototype.presentarDetalleDiv = function(nombreDiv, nombreColumna, valorColumna){
	var indice=-1, columnanombre, columna, fila, existe;
	// ver que exista el nombre de la columna y una fila con el valor
	existe=false;
	for(var i=0;i<this.qcolumnas;i++){
		if (this.columnas_nombres[i]==nombreColumna){
			columna=i;
			existe=true;
			break;
			}
		}
	if (!existe) return null;
	existe=false;
	for(var i=0;i<this.qfilas;i++){
		if (this.valores[i][columna]==valorColumna){
			fila=i;
			existe=true;
			break;
			}
		}
	if (!existe) return null;
	// ver si ya esta registrado
	for (var i=0;i<this.presentacionDivNombres.length;i++){
		if (this.presentacionDivNombres[i]==nombreDiv) {indice=i};
		}
	if (indice == -1){
		// registrarlo
		var odiv= buscarElementosXTipoYNombre("div",nombreDiv);
		if (odiv==null){return null};
		// ver que las columnas existan
		existe=false;
		for (var i=0;i<odiv.length;i++){
			columnanombre=odiv[i].innerHTML;
			columnanombre=columnanombre.substring(1,columnanombre.length -1)
			for (var j=0, existe=false;j<this.qcolumnas;j++){
				if (this.columnas_nombres[j]==columnanombre){
					existe=true;
					break;
					}
				}
			if (!existe){return null};
			}
		// las columnas existen
		this.presentacionDivNombres.push(nombreDiv);
		this.presentacionDivObjetos.push(odiv);
		var campos= Array();
		for (var i=0;i<odiv.length;i++){
			columnanombre=odiv[i].innerHTML;
			campos.push(columnanombre.substring(1,columnanombre.length -1));
			}
		this.presentacionDivCampos.push(campos);
		indice=this.presentacionDivNombres.length - 1;
		}
	for (var i=0; i<this.presentacionDivCampos[indice].length;i++)
		this.presentacionDivObjetos[indice][i].innerHTML=this.getValorxFilaIDColNombre(fila,this.presentacionDivCampos[indice][i]);
}

		
		


