


function cargaMaestro(columna) {
var sep = "", aux="-1#--#"
var combo

	for (var i = 1 ; i < tam ; i++) {

		if (!estaEn(a[i][columna], aux)) {

			aux = aux + sep + a[i][columna] + '#' + a[i][columna+1]
			sep = "#"
		}

	} 

    combo = document.getElementById(a[0][columna])

	cargacombo(combo, aux)
	
	sortSelect(combo)

	
}

function hayCoincidencias(fila, columna, cadena) {
var flag=true
var frm 
	for (var j = 0 ; j < cols ; j = j + 2) { //recorremos todos los combos menos el que estamos analizando
		if (columna != j) {
		    frm = document.getElementById(a[0][j])
			if (frm.value != a[fila][j] && frm.value!='-1') 
				{flag = false}
		}
	}

	if (flag) {
		flag = !estaEn (a[fila][columna], cadena)
	}

	return flag
}

function sortSelectDesc(obj) {
	var o = new Array();
	
	obj.options[0].innerHTML = 'z'+obj.options[0].innerHTML
	
	if (!hasOptions(obj)) { return; }

	for (var i = 0; i < obj.options.length ; i++) {
		o[o.length] = new Option( obj.options[i].text, obj.options[i].value, obj.options[i].defaultSelected, obj.options[i].selected) ;
		}
	if (o.length == 0) { return; }

	o = o.sort( 
		function(a,b) { 
			if ((a.text+"") > (b.text+"")) { return -1; }
			if ((a.text+"") < (b.text+"")) { return 1; }
			return 0;
			} 
		);

    var opt

	for (var i=0 ; i < o.length ; i++) {
    	opt = document.createElement("OPTION");
        opt.text = o[i].text
        opt.value = o[i].value;
		if (o[i].selected) {opt.selected = true}
		obj.options[i] = opt
		}

    //alert(obj[0].name)
    //alert(document.getElementById(obj[0]).innerText)	

	obj.options[0].innerHTML = obj.options[0].innerHTML.substr(1)
	//obj[0].innerText = obj[0].innerText.substring(1,3)

    //obj[0].innerText = '--'


	}

function cargaLista(col) {

var aux="", sep="", i


var valor = document.getElementById(a[0][col]).value


var total=0
//LLENA TODOS LOS COMBOS SEGUN LA OPCIÓN INDICADA
	
	for (var j = 0 ; j < cols ; j = j + 2) { //recorremos todos los combos y saltamos el que mandamos por parametro (col)

		if (col!=j) {	
		
			aux="-1#--#"
			sep=''
			for ( i = 1 ; i < tam ; i++) {
					
				if (hayCoincidencias(i, j, aux, col)) { //por ejemplo si (idMarca='VW')
					total = total + 1
					if (document.getElementById("idMarca").value == '-1' && j == 2)
						aux = aux + sep + a[i][j] + '#' + a[i][1] + ' ' + a[i][j+1]
					else
						aux = aux + sep + a[i][j] + '#' + a[i][j+1] //introducimos el elemento del array en curso, luego borraremos duplicados
					sep= "#"
				}//if


			} //for



			//ya tenemos el temita, ahora borramos llenamos el combo y borramos duplicados
			
			cargacombo(document.getElementById(a[0][j]), aux, document.getElementById(a[0][j]).value)

			if (j==10) 
				sortSelectDesc(document.getElementById(a[0][j]))
			else 
				sortSelect(document.getElementById(a[0][j]))

		}//if
					
	}//for
	
				    

}//function

function estaEn (valor, cadena) {
var flag = false
	var aa = cadena.split('#')
	
	for (var i = 0 ; i < aa.length ; i++) {
	
		if (aa[i]==valor) flag = true
	
	}
	
	return flag

}


function llenaCombo(origen, destino) {

var i = 0; cad = '', sep = ''

    borracombo (destino)
    for (i=0;i<origen.length;i++){
        cad = cad + sep + origen[i].value + '#' + origen.options[i].innerText
        sep = '#'
    }
    cargacombo(destino, cad, origen.value)

}

function etiqueta(texto) {
	tm_flag = !tm_flag
	if (tm_flag) buscando.innerText = texto
	else buscando.innerText = ''
}

function buscar() {
	
	
	document.getElementById("patron").value = ''
	document.getElementById("bLibre").value = ''
	
	
	document.forms[0].submit();
	
}

function buscar_patron() {
	
	if (document.getElementById("patronStr").value=='') {
		alert('escribe al menos una palabra para realizar la búsqueda')
		document.getElementById("patronStr").focus()
		return false
	}
	
	//tm = window.setInterval("etiqueta('realizando búsqueda...')",400);
	document.getElementById("bLibre").value = 'si'
	document.getElementById("patron").value = document.getElementById("patronStr").value
    document.forms[0].submit();
}

function focusPatron() {
var efrmBus = document.getElementById("frmBus")
	
	efrmBus.patron.style.color='black'; 
	if(efrmBus.patron.value=='<%=txt_escribe_aqui%>')
		efrmBus.patron.value=''
	else
		efrmBus.patron.select()
}

function quitaFoco(obj, num) {

var i


	document.getElementById("div_procesando").style.visibility = 'visible'
	
	document.getElementById("patronStr").focus()

	for ( i = 0 ; i < longitudForm ; i++ ) 
	{
		if (document.getElementById("frm")[i].type=='select-one') document.getElementById("frm")[i].disabled = true
	}

    cargaLista (num)

    for ( var i = 0 ; i < longitudForm ; i++ ) 
    {
	    if (document.getElementById("frm")[i].type=='select-one') document.getElementById("frm")[i].disabled = false
    }

    obj.focus()	
    document.getElementById("div_procesando").style.visibility = 'hidden'

}
