// JavaScript Document
function rellenaSelect1(idSelect) {
	var ddl = document.getElementById(idSelect);

	while (ddl.length> 0) {
		ddl.remove(0);
	}

	var opcionI=document.createElement('option');
	opcionI.text = "--- SIN CATEGORIA ---";
	opcionI.value = "0";
	opcionI.title = "";
	try {
		ddl.add(opcionI,null); // standards compliant
	} catch(ex) {
		ddl.add(opcionI); // IE only
	}

	for (var j in opciones1) {
		var opcion=document.createElement('option');
		opcion.text = opciones1[j].text;
		opcion.value = opciones1[j].value;
		opcion.title = opciones1[j].textNC;
		try {
			ddl.add(opcion,null); // standards compliant
		} catch(ex) {
			ddl.add(opcion); // IE only
		}
	}

}

function rellenaSelect1Mant(idSelect) {
	var ddl = document.getElementById(idSelect);

	while (ddl.length> 0) {
		ddl.remove(0);
	}
 	
	for (var j in opciones1) {
		var opcion=document.createElement('option');
		opcion.text = opciones1[j].text;
		opcion.value = opciones1[j].value;
		opcion.title = opciones1[j].textNC;
		try {
			ddl.add(opcion,null); // standards compliant
		} catch(ex) {
			ddl.add(opcion); // IE only
		}
	}

}

function rellenaSelect2(idSelect1, idSelect2) {

	var ddl = document.getElementById(idSelect2);
	var ddl1 = document.getElementById(idSelect1);

	//opcionActiva(idSelect1);
	var indiceActivo = ddl1.selectedIndex;

	while (ddl.length> 0) {
		ddl.remove(0);
	}
		
	if (indiceActivo == 0) {
		var opcionI=document.createElement('option');
		opcionI.text = "--- ELIJA CATEGORIA ---";
		opcionI.value = "0";
		opcionI.title = "N/A";
		try {
			ddl.add(opcionI,null); // standards compliant
		} catch(ex) {
			ddl.add(opcionI); // IE only
		}
	} else {
		var opcionI=document.createElement('option');
		opcionI.text = "--- SIN FAMILIA ---";
		opcionI.value = "0";
		opcionI.title = "N/A";
		try {
			ddl.add(opcionI,null); // standards compliant
		} catch(ex) {
			ddl.add(opcionI); // IE only
		}
		for (var j in opciones2[indiceActivo-1]) {
			var opcion=document.createElement('option');
			opcion.text = opciones2[indiceActivo-1][j].text;
			opcion.value = opciones2[indiceActivo-1][j].value;
			opcion.title = opciones2[indiceActivo-1][j].textNC;
			try {
				ddl.add(opcion,null); // standards compliant
			} catch(ex) {
				ddl.add(opcion); // IE only
			}
		}
	}
}

function rellenaSelect2Mant(idSelect1, idSelect2) {

	var ddl = document.getElementById(idSelect2);
	var ddl1 = document.getElementById(idSelect1);

	//opcionActiva(idSelect1);
	var indiceActivo = ddl1.selectedIndex;

	while (ddl.length> 0) {
		ddl.remove(0);
	}
				
	for (var j in opciones2[indiceActivo]) {
		var opcion=document.createElement('option');
		opcion.text = opciones2[indiceActivo][j].text;
		opcion.value = opciones2[indiceActivo][j].value;
		opcion.title = opciones2[indiceActivo][j].textNC;
		try {
			ddl.add(opcion,null); // standards compliant
		} catch(ex) {
			ddl.add(opcion); // IE only
		}
	}

	if (ddl.length > 0) {
		ddl.selectedIndex = 0;
	}
}

function rellenaSelect3(idSelect1, idSelect2, idSelect3) {

	var ddl1 = document.getElementById(idSelect1);
	var ddl2 = document.getElementById(idSelect2);
	var ddl3 = document.getElementById(idSelect3);

	//opcionActiva(idSelect1);
	var indiceActivo1 = ddl1.selectedIndex;
	var indiceActivo2 = ddl2.selectedIndex;

	while (ddl3.length> 0) {
		ddl3.remove(0);
	}
		
	if (indiceActivo2 == 0) {
		var opcionI=document.createElement('option');
		opcionI.text = "--- ELIJA FAMILIA ---";
		opcionI.value = "0";
		opcionI.title = "N/A";
		try {
			ddl3.add(opcionI,null); // standards compliant
		} catch(ex) {
			ddl3.add(opcionI); // IE only
		}
	} else {
		var opcionI=document.createElement('option');
		opcionI.text = "--- SIN SUBFAMILIA ---";
		opcionI.value = "0";
		opcionI.title = "N/A";
		try {
			ddl3.add(opcionI,null); // standards compliant
		} catch(ex) {
			ddl3.add(opcionI); // IE only
		}
		for (var j in opciones3[indiceActivo1-1][indiceActivo2-1]) {
			var opcion=document.createElement('option');
			opcion.text = opciones3[indiceActivo1-1][indiceActivo2-1][j].text;
			opcion.value = opciones3[indiceActivo1-1][indiceActivo2-1][j].value;
			opcion.title = opciones3[indiceActivo1-1][indiceActivo2-1][j].textNC;
			try {
				ddl3.add(opcion,null); // standards compliant
			} catch(ex) {
				ddl3.add(opcion); // IE only
			}
		}
	}
}

function rellenaSelect3Mant(idSelect1, idSelect2, idSelect3) {

	var ddl1 = document.getElementById(idSelect1);
	var ddl2 = document.getElementById(idSelect2);
	var ddl3 = document.getElementById(idSelect3);

	//opcionActiva(idSelect1);
	var indiceActivo1 = ddl1.selectedIndex;
	var indiceActivo2 = ddl2.selectedIndex;

	while (ddl3.length> 0) {
		ddl3.remove(0);
	}
	
	if (opciones3[indiceActivo1] != null) 
		for (var j in opciones3[indiceActivo1][indiceActivo2]) {
			var opcion=document.createElement('option');
			opcion.text = opciones3[indiceActivo1][indiceActivo2][j].text;
			opcion.value = opciones3[indiceActivo1][indiceActivo2][j].value;
			opcion.title = opciones3[indiceActivo1][indiceActivo2][j].textNC;
			try {
				ddl3.add(opcion,null); // standards compliant
			} catch(ex) {
				ddl3.add(opcion); // IE only
			}
		}

	if (ddl3.length > 0) {
		ddl3.selectedIndex = 0;
	}
}

function leeValores(txtFam, txtCat, txtSubCat) {
	var txtFamObj = document.getElementById(txtFam);
	var txtCatObj = document.getElementById(txtCat);
	var txtSubCatObj = document.getElementById(txtSubCat);

	alert("FAMILIA: " + txtFamObj.value);
	alert("CATEGORIA: " + txtCatObj.value);
	alert("SUBCATEGORIA: " + txtSubCatObj.value);
}

function buscaIndice(selObj, valor) {
	var i = 1;
	
	while (i < selObj.length) {
		if (selObj.options[i].value == valor)
			return i;
		else
			i++;
	}
	
	return 0;
}

function seleccionaInicial(lista, txtValorInicial) {
	var selObj = document.getElementById(lista);
	var valorInicial = document.getElementById(txtValorInicial).value;

	if (valorInicial != 0) {
		selObj.selectedIndex = buscaIndice(selObj, valorInicial);
	} else {
		selObj.selectedIndex = 0;
	}		
}

function seleccionaInicialV(lista, valorInicial) {
	var selObj = document.getElementById(lista);

	if (valorInicial != 0) {
		selObj.selectedIndex = buscaIndice(selObj, valorInicial);
	} else {
		selObj.selectedIndex = 0;
	}		
}

function buscaIndiceT(selObj, valor) {
	var i = 1;
	
	while (i < selObj.length) {
		if (selObj.options[i].text == valor)
			return i;
		else
			i++;
	}
	
	return 0;
}

function seleccionaInicialT(lista, txtValorInicial) {
	var selObj = document.getElementById(lista);

	if (txtValorInicial != '') {
		selObj.selectedIndex = buscaIndiceT(selObj, txtValorInicial);
	} else {
		selObj.selectedIndex = 0;
	}		
}

function asignaInicial(lista, txtValor) {
	var selObj = document.getElementById(lista);
	var txtObj = document.getElementById(txtValor);

	txtObj.value = selObj.options[selObj.selectedIndex].value;
}

function seleccionaInicialN(lista, txtValorInicial, txtNombre) {
	var selObj = document.getElementById(lista);
	var txtObj2 = document.getElementById(txtNombre);
	var valorInicial = document.getElementById(txtValorInicial).value;

	if (valorInicial != 0) {
		selObj.selectedIndex = buscaIndice(selObj, valorInicial);
	} else {
		selObj.selectedIndex = 0;
	}
	try {
		txtObj2.value = selObj.options[selObj.selectedIndex].text;
	} catch (ex) {
		txtObj2.value = "";
	}
}

function asignaInicialN(lista, txtValor, txtNombre) {
	var selObj = document.getElementById(lista);
	var txtObj = document.getElementById(txtValor);
	var txtObj2 = document.getElementById(txtNombre);
	if (selObj.selectedIndex != -1) {
		txtObj.value = selObj.options[selObj.selectedIndex].value;
		txtObj2.value = selObj.options[selObj.selectedIndex].text;
	} else {
		txtObj.value = 0;
		txtObj2.value = "";
	}
}

function seleccionaInicialNC(lista, txtValorInicial, txtNombre, txtNombreNC) {
	var selObj = document.getElementById(lista);
	var txtObj2 = document.getElementById(txtNombre);
	var txtObj2NC = document.getElementById(txtNombreNC);
	var valorInicial = document.getElementById(txtValorInicial).value;

	if (valorInicial != 0) {
		selObj.selectedIndex = buscaIndice(selObj, valorInicial);
	} else {
		selObj.selectedIndex = 0;
	}
	try {
		txtObj2.value = selObj.options[selObj.selectedIndex].text;
		txtObj2NC.value = selObj.options[selObj.selectedIndex].title;
	} catch (ex) {
		txtObj2.value = "";
		txtObj2NC.value = "";
	}
}

function asignaInicialNC(lista, txtValor, txtNombre, txtNombreNC) {
	var selObj = document.getElementById(lista);
	var txtObj = document.getElementById(txtValor);
	var txtObj2 = document.getElementById(txtNombre);
	var txtObj2NC = document.getElementById(txtNombreNC);
	if (selObj.selectedIndex != -1) {
		txtObj.value = selObj.options[selObj.selectedIndex].value;
		txtObj2.value = selObj.options[selObj.selectedIndex].text;
		txtObj2NC.value = selObj.options[selObj.selectedIndex].title;
	} else {
		txtObj.value = 0;
		txtObj2.value = "";
		txtObj2NC.value = "";
	}
}

function comprobarDatos() {
	leeValores('txtFamilia', 'txtCategoria', 'txtSubCat');
	return false;
}

function seleccionaPorIndice(lista, txtValorInicial) {
	var selObj = document.getElementById(lista);
	var valorInicial = document.getElementById(txtValorInicial).value;

	if (valorInicial != 0) {
		selObj.selectedIndex = valorInicial;
	} else {
		selObj.selectedIndex = 0;
	}		
}

// ESPECIFICO PROVEEDORES

function rellenaSelectP(idSelect) {
	var ddl = document.getElementById(idSelect);

	while (ddl.length> 0) {
		ddl.remove(0);
	}

	var opcionI=document.createElement('option');
	opcionI.text = "DESCONOCIDO!";
	opcionI.value = "0";
	try {
		ddl.add(opcionI,null); // standards compliant
	} catch(ex) {
		ddl.add(opcionI); // IE only
	}

	for (var j in proveedores) {
		var opcion=document.createElement('option');
		opcion.text = proveedores[j].text;
		opcion.value = proveedores[j].value;
		opcion.title = proveedores[j].textNC;
		try {
			ddl.add(opcion,null); // standards compliant
		} catch(ex) {
			ddl.add(opcion); // IE only
		}
	}

}

function rellenaSelectPMant(idSelect) {
	var ddl = document.getElementById(idSelect);

	while (ddl.length> 0) {
		ddl.remove(0);
	}
 	
	for (var j in proveedores) {
		var opcion=document.createElement('option');
		opcion.text = proveedores[j].text;
		opcion.value = proveedores[j].value;
		opcion.title = proveedores[j].textNC;
		try {
			ddl.add(opcion,null); // standards compliant
		} catch(ex) {
			ddl.add(opcion); // IE only
		}
	}

}


// ESPECIFICO Folletos

function rellenaSelectF(idSelect) {
	var ddl = document.getElementById(idSelect);

	while (ddl.length> 0) {
		ddl.remove(0);
	}

	var opcionI=document.createElement('option');
	opcionI.text = "DESCONOCIDO!";
	opcionI.value = "0";
	try {
		ddl.add(opcionI,null); // standards compliant
	} catch(ex) {
		ddl.add(opcionI); // IE only
	}

	for (var j in folletos) {
		var opcion=document.createElement('option');
		opcion.text = folletos[j].text;
		opcion.value = folletos[j].value;
		try {
			ddl.add(opcion,null); // standards compliant
		} catch(ex) {
			ddl.add(opcion); // IE only
		}
	}

}


function rellenaSelectFMant(idSelect) {
	var ddl = document.getElementById(idSelect);

	while (ddl.length> 0) {
		ddl.remove(0);
	}

	for (var j in folletos) {
		var opcion=document.createElement('option');
		opcion.text = folletos[j].text;
		opcion.value = folletos[j].value;
		try {
			ddl.add(opcion,null); // standards compliant
		} catch(ex) {
			ddl.add(opcion); // IE only
		}
	}

}

// ESPECIFICO Tiendas

function rellenaSelectT(idSelect) {
	var ddl = document.getElementById(idSelect);

	while (ddl.length> 0) {
		ddl.remove(0);
	}

	var opcionI=document.createElement('option');
	opcionI.text = "SIN TIENDA";
	opcionI.value = "0";
	try {
		ddl.add(opcionI,null); // standards compliant
	} catch(ex) {
		ddl.add(opcionI); // IE only
	}

	for (var j in tiendas) {
		var opcion=document.createElement('option');
		opcion.text = tiendas[j].text;
		opcion.value = tiendas[j].value;
		try {
			ddl.add(opcion,null); // standards compliant
		} catch(ex) {
			ddl.add(opcion); // IE only
		}
	}

}


function rellenaSelectTMant(idSelect) {
	var ddl = document.getElementById(idSelect);

	while (ddl.length> 0) {
		ddl.remove(0);
	}

	for (var j in tiendas) {
		var opcion=document.createElement('option');
		opcion.text = tiendas[j].text;
		opcion.value = tiendas[j].value;
		try {
			ddl.add(opcion,null); // standards compliant
		} catch(ex) {
			ddl.add(opcion); // IE only
		}
	}

}

function cambiarTienda() {
	var selObj = document.getElementById("txtTienda");
	var i = selObj.selectedIndex;
	document.getElementById("txtTiendaNombre").value = selObj.options[i].text;
}


