function SelectCombo(combo) {
	for (i = 0; i < combo.options.length; i++)
		combo.options[i].selected=true;
}

function allCombo(cmbOrigen, cmbDestino) {
	for (i = 0; i < cmbOrigen.options.length; i++)
		cmbOrigen.options[i].selected=true;
	addSelection(cmbOrigen,cmbDestino);
}
function allComboOrder(cmbOrigen, cmbDestino) {
	for (i = 0; i < cmbOrigen.options.length; i++)
		cmbOrigen.options[i].selected=true;
	addSelectionOrder(cmbOrigen,cmbDestino);
}
function addSelection(cmbOrigen, cmbDestino){
	if (cmbOrigen.options.selectedIndex != -1){
		for (i = 0; i < cmbOrigen.options.length; i++)
			if (cmbOrigen.options[i].selected) {
				cmbDestino.options.length = cmbDestino.options.length + 1;
				cmbDestino.options[cmbDestino.options.length - 1].text = cmbOrigen.options[i].text;
				cmbDestino.options[cmbDestino.options.length - 1].value = cmbOrigen.options[i].value;
				for (j = i; j < cmbOrigen.options.length-1; j++) {
					cmbOrigen.options[j].text=cmbOrigen.options[j+1].text;
					cmbOrigen.options[j].value=cmbOrigen.options[j+1].value;
					cmbOrigen.options[j].selected=cmbOrigen.options[j+1].selected;
				}
				cmbOrigen.options.length = cmbOrigen.options.length - 1;
				i--;
			}
	}
}
function addSelectionOrder(cmbOrigen, cmbDestino){
	if (cmbOrigen.options.selectedIndex != -1){
		var carry = new Option;
		var aux = new Option;
		for (i = 0; i < cmbOrigen.options.length; i++) {
			if (cmbOrigen.options[i].selected) {
				cmbDestino.options.length = cmbDestino.options.length + 1;
				carry.text = cmbOrigen.options[i].text;
				carry.value = cmbOrigen.options[i].value;
				for (j = 0; j < cmbDestino.options.length-1; j++) {
					if (carry.text.toUpperCase() < cmbDestino.options[j].text.toUpperCase()) {
						aux.text = cmbDestino.options[j].text;
						aux.value = cmbDestino.options[j].value;
						cmbDestino.options[j].text = carry.text;
						cmbDestino.options[j].value = carry.value;
						carry.text = aux.text;
						carry.value = aux.value;
					}
				}
				cmbDestino.options[cmbDestino.options.length-1].text = carry.text;
				cmbDestino.options[cmbDestino.options.length-1].value = carry.value;

				for (j = i; j < cmbOrigen.options.length-1; j++) {
					cmbOrigen.options[j].text=cmbOrigen.options[j+1].text;
					cmbOrigen.options[j].value=cmbOrigen.options[j+1].value;
					cmbOrigen.options[j].selected=cmbOrigen.options[j+1].selected;
				}
				cmbOrigen.options.length = cmbOrigen.options.length - 1;
				i--;
			}
		}
	}
}


function selectCombo(cmbOrigen) {
	for (i = 0; i < cmbOrigen.options.length; i++) {
		cmbOrigen.options[i].selected = true;
	}
}

function FiltrarCombo(combo, valor) {
	var j;
	//combo.selectedIndex = -1;
	for (i = 0; i < combo.options.length; i++) {
		if (combo.options[i].text.substr(0, valor.length).toUpperCase() == valor.toUpperCase()) {
			j = i;
			i = combo.options.length;
		}
	}
	if (j>=0) {
		combo.selectedIndex = j;
	}
}

function resetFiltro(combo, textbox) {
	combo.selectedIndex = -1;
	textbox.value='';
}

//Para un combo simple NO PARSEADO define el selected
function setSelected(iform, fieldName, fieldValue){
   ifield = document.forms[iform].elements[fieldName];
   for(i=0; i<ifield.length; i++){
 	     if(ifield[i].value == fieldValue)
			    ifield[i].selected = true;
	 }
}

function Subir(cmbOrigen){
	if (cmbOrigen.options.selectedIndex != -1){
		if (cmbOrigen.options.selectedIndex != 0) {
			var aux = new Option;

			aux.text = cmbOrigen.options[cmbOrigen.options.selectedIndex].text;
			aux.value = cmbOrigen.options[cmbOrigen.options.selectedIndex].value;

			cmbOrigen.options[cmbOrigen.options.selectedIndex].text = cmbOrigen.options[cmbOrigen.options.selectedIndex-1].text;
			cmbOrigen.options[cmbOrigen.options.selectedIndex].value = cmbOrigen.options[cmbOrigen.options.selectedIndex-1].value;

			cmbOrigen.options[cmbOrigen.options.selectedIndex-1].text = aux.text;
			cmbOrigen.options[cmbOrigen.options.selectedIndex-1].value = aux.value;
		}
	}
}

function Bajar(cmbOrigen){
	if (cmbOrigen.options.selectedIndex != -1){
		if (cmbOrigen.options.selectedIndex != (cmbOrigen.options.length - 1) ) {
			var aux = new Option;

			aux.text = cmbOrigen.options[cmbOrigen.options.selectedIndex].text;
			aux.value = cmbOrigen.options[cmbOrigen.options.selectedIndex].value;

			cmbOrigen.options[cmbOrigen.options.selectedIndex].text = cmbOrigen.options[cmbOrigen.options.selectedIndex+1].text;
			cmbOrigen.options[cmbOrigen.options.selectedIndex].value = cmbOrigen.options[cmbOrigen.options.selectedIndex+1].value;

			cmbOrigen.options[cmbOrigen.options.selectedIndex+1].text = aux.text;
			cmbOrigen.options[cmbOrigen.options.selectedIndex+1].value = aux.value;
		}
	}
}

function addNivelIdioma(cmbOrigen1,cmbOrigen2, cmbDestino){
	if (cmbOrigen2.options.selectedIndex > 0){
		for (i = 0; i < cmbDestino.options.length; i++){
			valores=cmbDestino.options[i].value.split(',');
			if(valores[0]==cmbOrigen1.options[cmbOrigen1.options.selectedIndex].value)
			{

				return;
			}
		}

		//bla bla
		cmbDestino.options.length = cmbDestino.options.length + 1;
		cmbDestino.options[cmbDestino.options.length - 1].text = cmbOrigen1.options[cmbOrigen1.options.selectedIndex].text+'-'+cmbOrigen2.options[cmbOrigen2.options.selectedIndex].text;
		cmbDestino.options[cmbDestino.options.length - 1].value = cmbOrigen1.options[cmbOrigen1.options.selectedIndex].value+','+cmbOrigen2.options[cmbOrigen2.options.selectedIndex].value+ ',' + cmbDestino.options[cmbDestino.options.length - 1].text;

	}
}

function removeSelection(cmbOrigen)
{
		if (cmbOrigen.options.selectedIndex != -1)
		{
			//alert(cmbOrigen.options.selectedIndex]);
			cmbOrigen.removeChild(cmbOrigen.options[cmbOrigen.options.selectedIndex]);
			//cmbOrigen.options.removeChild(cmbOrigen.options[cmbOrigen.options.selectedIndex]);
		}
}

function addAreasInteres(){
	combo = new Object();
	combo = document.getElementById('areasInteresNoSel');
	if (combo.options.selectedIndex != -1){
		for (i = 0; i < combo.options.length; i++)
		{
			if (combo.options[i].selected) {
				for (j = 0; j < AreasInteres.length; j++)
				{
					if(AreasInteres[j].value == combo.options[i].value)
					{
						if(AreasInteres[j].id_padre == '')
						{
							// LLeva al padre e hijas
							for (k = 0; k < AreasInteres.length; k++)
							{
								if(AreasInteres[k].id_padre == AreasInteres[j].value)
								{
									AreasInteres[k].sel=true;
									AreasInteres[k].noSel=false;
								}
							}
							AreasInteres[j].sel=true;
							AreasInteres[j].noSel=false;
						}
						else
						{
							// Solo lleva la hija y su padre
							AreasInteres[j].sel=true;
							AreasInteres[j].noSel=false;
							for(k=0; k<AreasInteres.length; k++)
							{
								if(AreasInteres[k].value==AreasInteres[j].id_padre)
								{
									AreasInteres[k].sel=true;
									AreasInteres[k].noSel=true;
								}
							}
						}
					}
				}
			}
		}
		ordenarAreasInteres();
	}
}

function removeAreasInteres(){
	combo = new Object();
	combo = document.getElementById('areasInteresSel');
	if (combo.options.selectedIndex != -1){
		for (i = 0; i < combo.options.length; i++)
			if (combo.options[i].selected) {
				for (j = 0; j < AreasInteres.length; j++)
				{
					if(AreasInteres[j].value == combo.options[i].value)
					{
						if(AreasInteres[j].id_padre == '')
						{
							// LLeva al padre e hijas
							for (k = 0; k < AreasInteres.length; k++)
							{
								if(AreasInteres[k].id_padre == AreasInteres[j].value)
								{
									AreasInteres[k].sel=false;
									AreasInteres[k].noSel=true;
								}
							}
							AreasInteres[j].sel=false;
							AreasInteres[j].noSel=true;
						}
						else
						{
							// Solo lleva la hija y su padre
							AreasInteres[j].sel=false;
							AreasInteres[j].noSel=true;
							for(k=0; k<AreasInteres.length; k++)
							{
								if(AreasInteres[k].value==AreasInteres[j].id_padre)
								{
									AreasInteres[k].sel=true;
									AreasInteres[k].noSel=true;
								}
							}
						}
					}
				}
			}
		ordenarAreasInteres();
	}
}

function ordenarAreasInteres()
{
	var textSel = '';
	var textNoSel = '';
	var padreActual = -1;
	var hay_hijas_sel = false;
	var hay_hijas_nosel = false;
	var opti_fondo = '';

	for(k=0; k<AreasInteres.length; k++)
	{
		hay_hijas_sel=false;
		hay_hijas_nosel=false;
		if(AreasInteres[k].id_padre == '' && AreasInteres[k].tiene_hijos)
		{
			for(l=0; l<AreasInteres.length; l++)
			{
				if(AreasInteres[l].sel && AreasInteres[l].id_padre==AreasInteres[k].value)
					hay_hijas_sel = true;
				if(AreasInteres[l].noSel && AreasInteres[l].id_padre==AreasInteres[k].value)
					hay_hijas_nosel = true;
			}
			AreasInteres[k].sel = hay_hijas_sel;
			AreasInteres[k].noSel = hay_hijas_nosel;
		}
	}
	comboNoSel = new Object();
	comboSel = new Object();
	comboNoSel = document.getElementById('areasInteresNoSel');
	comboSel = document.getElementById('areasInteresSel');
	comboSel.options.length=0;
	comboNoSel.options.length=0;
	for (j = 0; j < AreasInteres.length; j++)
	{
		if(AreasInteres[j].id_padre == '') {
			padreActual = j;
			opti_fondo = '#3333CC';
		}
		else opti_fondo = 'black';

		if(AreasInteres[j].sel && AreasInteres[j].id_padre == AreasInteres[padreActual].value && padreActual != -1 && AreasInteres[padreActual].noSel)
		//	textSel = AreasInteres[padreActual].text +' ' + AreasInteres[j].text;
			textSel = AreasInteres[j].text;
		else textSel = AreasInteres[j].text;
		if(AreasInteres[j].noSel && AreasInteres[j].id_padre == AreasInteres[padreActual].value && padreActual != -1 && AreasInteres[padreActual].sel)
		//	textNoSel = AreasInteres[padreActual].text +' ' + AreasInteres[j].text;
			textNoSel = AreasInteres[j].text;
		else textNoSel = AreasInteres[j].text;
		if(AreasInteres[j].sel)
		{
			comboSel.options.length++;
			comboSel.options[comboSel.options.length-1].text = textSel;
			comboSel.options[comboSel.options.length-1].value = AreasInteres[j].value;
			comboSel.options[comboSel.options.length-1].style.color = opti_fondo;
		}
 		if(AreasInteres[j].noSel)
		{
			comboNoSel.options.length++;
			comboNoSel.options[comboNoSel.options.length-1].text = textNoSel;
			comboNoSel.options[comboNoSel.options.length-1].value = AreasInteres[j].value;
			comboNoSel.options[comboNoSel.options.length-1].style.color = opti_fondo;
		}
	}
}

function comprobarTematica()
{
	combo = new Object();
	combo = document.getElementById('tematica_xoad');

	for (i = 0; i < combo.options.length; i++)
	{
		for (j = 0; j < AreasInteres.length; j++)
		{
			if(AreasInteres[j].value == combo.options[i].value)
				combo.options[i].text=AreasInteres[j].text;
		}
	}

	if (combo.options.selectedIndex != -1)
	{
		for (i = 0; i < combo.options.length; i++)
			if (combo.options[i].selected)
			{
				for (j = 0; j < AreasInteres.length; j++)
				{
					if(AreasInteres[j].value == combo.options[i].value)
					{
						combo.options[i].text=AreasInteres[j].text;
						if(AreasInteres[j].id_padre=='' && AreasInteres[j].tiene_hijos)
						{
							alert(txt_combo1 + " " + AreasInteres[j].text + " " + txt_combo2);
							combo.options.selectedIndex = -1;
						}
						else
						{
							combo.options[i].text = AreasInteres[j].padre+AreasInteres[j].text;
						}
					}
				}
			}
	}

}

function selectComboCategorias(combo) {
	for (i = 0; i < combo.options.length; i++)
	{
		for (j = 0; j < AreasInteres.length; j++)
		{
			if(combo.options[i].value == AreasInteres[j].value && !AreasInteres[j].tiene_hijos)
				combo.options[i].selected=true;
		}
	}
}

function agregaCondicionAND()
{
	combo_noSel = new Object();
	combo_sel = new Object();
	combo_noSel = document.getElementById('CondicionesNoSel');
	combo_sel = document.getElementById('CondicionesSel');

	if (combo_noSel.options.selectedIndex != -1){
		for (i = 0; i < combo_noSel.options.length; i++)
		{
			if (combo_noSel.options[i].selected)
			{
/*
				combo_sel.options.length++;

				combo_sel.options[combo_sel.options.length-1].text = cond + combo_noSel.options[i].text;
				combo_sel.options[combo_sel.options.length-1].value = 'Y_' + combo_noSel.options[i].value;
*/
				var oOption = document.createElement("OPTION");
				combo_sel.options.add(oOption);

				if (combo_sel.options.length>1) cond=' Y ';
				else cond = '';

				oOption.text = cond + combo_noSel.options[i].text;
				oOption.value = 'Y_' + combo_noSel.options[i].value;
			}
		}

		//Inhabilito el otro botón
		btn = document.getElementById('btnOR');
		btn.disabled = true;
		document.getElementById('listado_actualizado').value=0;
	}


}


function agregaCondicionOR()
{
	combo_noSel = new Object();
	combo_sel = new Object();
	combo_noSel = document.getElementById('CondicionesNoSel');
	combo_sel = document.getElementById('CondicionesSel');

	if (combo_noSel.options.selectedIndex != -1)
	{
		for (i = 0; i < combo_noSel.options.length; i++)
		{
			if (combo_noSel.options[i].selected)
			{
/*
				combo_sel.options.length++;

				combo_sel.options[combo_sel.options.length-1].text = cond + combo_noSel.options[i].text;
				combo_sel.options[combo_sel.options.length-1].value = 'O_' + combo_noSel.options[i].value;
*/
				var oOption = document.createElement("OPTION");
				combo_sel.options.add(oOption);

				if (combo_sel.options.length>1) cond=' O ';
				else cond = '';

				oOption.text = cond + combo_noSel.options[i].text;
				oOption.value = 'O_' + combo_noSel.options[i].value;
			}
		}

		//Inhabilito el otro botón
		btn = document.getElementById('btnAND');
		btn.disabled = true;
		document.getElementById('listado_actualizado').value=0;
	}
}


function quitaCondicion()
{
	combo_sel = new Object();
	combo_sel = document.getElementById('CondicionesSel');

	if (combo_sel.options.selectedIndex != -1){
		for (i = combo_sel.options.length-1; i>=0;  i--)
		{
			if (combo_sel.options[i].selected)
			{
				combo_sel.removeChild(combo_sel.options[i]);

				//Si borré el primero le tengo que quitar el 'Y' u 'O' al 2do
				if (i==0 && combo_sel.options.length>0)
					combo_sel.options[0].text = combo_sel.options[0].text.substr(2);
			}
		}
	}

	//Si no queda ninguna seleccionada habilito los dos botones de nuevo
	if (combo_sel.options.length==0)
	{
		btn = document.getElementById('btnAND');
		btn.disabled = false;

		btn = document.getElementById('btnOR');
		btn.disabled = false;
	}

	document.getElementByID('listado_actualizado').value=0;

}

function selectAllListadoEmpresas()
{
	chk = document.getElementById('sel_all_listado_empresas').checked;
	cant = document.getElementById('cant_empresas').value;

	for (i=1;i<=cant;i++)
	{
		obj = document.getElementById('empresa_'+i);
		obj.checked = chk;
	}
}

function bloqueaBTNCondicion ()
{
	//Me fijo que tipo de condición tiene el primer elemento
	combo = new Object();
	btn = new Object();
	combo = document.getElementById('CondicionesSel');

	if (combo.options.length>0)
	{
		if (combo.options[0].text.substr(0,1)=='O')
			btn = document.getElementById('btnAND');
		else
			btn = document.getElementById('btnOR');

		btn.disabled = true;
	}

}

function addProvinciasBuscAvanzado(){
	var num = 0;
	comboSeleccionadas = document.getElementById('metodoLugarA');
	combo = new Object();
	combo = document.getElementById('provinciasNoSel');
	for (i = 0; i< combo.options.length; i++)
	{
		if (combo.options[i].selected) num = num + 1;
	}
	if ((comboSeleccionadas.options.length + num) >5){
		alert(msg_error6);
		return;
	}
	addSelection(document.getElementById('provinciasNoSel'), document.getElementById('metodoLugarA'));
}

function addAreasInteresBuscAvanzado(){
	var num = 0;
	comboSeleccionadas = document.getElementById('areasInteresSel');
	combo = new Object();
	combo = document.getElementById('areasInteresNoSel');
	for (i = 0; i< combo.options.length; i++)
	{
		if (combo.options[i].selected) num = num + 1;
	}
	if ((comboSeleccionadas.options.length + num) >5){
		alert(msg_error7);
		return;
	}

	if (combo.options.selectedIndex != -1){
		for (i = 0; i < combo.options.length; i++)
		{
			if (combo.options[i].selected) {
				for (j = 0; j < AreasInteres.length; j++)
				{
					if(AreasInteres[j].value == combo.options[i].value)
					{
						if(AreasInteres[j].id_padre == '')
						{
							// LLeva al padre e hijas
							for (k = 0; k < AreasInteres.length; k++)
							{
								if(AreasInteres[k].id_padre == AreasInteres[j].value)
								{
									AreasInteres[k].sel=true;
									AreasInteres[k].noSel=false;
								}
							}
							AreasInteres[j].sel=true;
							AreasInteres[j].noSel=false;
						}
						else
						{
							// Solo lleva la hija y su padre
							AreasInteres[j].sel=true;
							AreasInteres[j].noSel=false;
							for(k=0; k<AreasInteres.length; k++)
							{
								if(AreasInteres[k].value==AreasInteres[j].id_padre)
								{
									AreasInteres[k].sel=true;
									AreasInteres[k].noSel=true;
								}
							}
						}
					}
				}
			}
		}
		ordenarAreasInteresBuscadorAvanzado();
	}
}

function removeAreasInteresBuscAvanzado(){
	combo = new Object();
	combo = document.getElementById('areasInteresSel');
	if (combo.options.selectedIndex != -1){
		for (i = 0; i < combo.options.length; i++)
			if (combo.options[i].selected) {
				for (j = 0; j < AreasInteres.length; j++)
				{
					if(AreasInteres[j].value == combo.options[i].value)
					{
						if(AreasInteres[j].id_padre == '')
						{
							// LLeva al padre e hijas
							for (k = 0; k < AreasInteres.length; k++)
							{
								if(AreasInteres[k].id_padre == AreasInteres[j].value)
								{
									AreasInteres[k].sel=false;
									AreasInteres[k].noSel=true;
								}
							}
							AreasInteres[j].sel=false;
							AreasInteres[j].noSel=true;
						}
						else
						{
							// Solo lleva la hija y su padre
							AreasInteres[j].sel=false;
							AreasInteres[j].noSel=true;
							for(k=0; k<AreasInteres.length; k++)
							{
								if(AreasInteres[k].value==AreasInteres[j].id_padre)
								{
									AreasInteres[k].sel=true;
									AreasInteres[k].noSel=true;
								}
							}
						}
					}
				}
			}
		ordenarAreasInteresBuscadorAvanzado();
	}
}

function ordenarAreasInteresBuscadorAvanzado()
{
	var textSel = '';
	var textNoSel = '';
	var padreActual = -1;
	var hay_hijas_sel = false;
	var hay_hijas_nosel = false;
	var opti_fondo = '';

	for(k=0; k<AreasInteres.length; k++)
	{
		hay_hijas_sel=false;
		hay_hijas_nosel=false;
		if(AreasInteres[k].id_padre == '' && AreasInteres[k].tiene_hijos)
		{
			for(l=0; l<AreasInteres.length; l++)
			{
				if(AreasInteres[l].sel && AreasInteres[l].id_padre==AreasInteres[k].value)
					hay_hijas_sel = true;
				if(AreasInteres[l].noSel && AreasInteres[l].id_padre==AreasInteres[k].value)
					hay_hijas_nosel = true;
			}
			AreasInteres[k].sel = hay_hijas_sel;
			AreasInteres[k].noSel = hay_hijas_nosel;
		}
	}
	comboNoSel = new Object();
	comboSel = new Object();
	comboNoSel = document.getElementById('areasInteresNoSel');
	comboSel = document.getElementById('areasInteresSel');
	comboSel.options.length=0;
	comboNoSel.options.length=0;
	for (j = 0; j < AreasInteres.length; j++)
	{

		if(AreasInteres[j].sel)
		//	textSel = AreasInteres[padreActual].text +' ' + AreasInteres[j].text;
			textSel = AreasInteres[j].text;
		else textSel = AreasInteres[j].text;
		if(AreasInteres[j].noSel)
		//	textNoSel = AreasInteres[padreActual].text +' ' + AreasInteres[j].text;
			textNoSel = AreasInteres[j].text;
		else textNoSel = AreasInteres[j].text;
		if(AreasInteres[j].sel)
		{
			comboSel.options.length++;
			comboSel.options[comboSel.options.length-1].text = textSel;
			comboSel.options[comboSel.options.length-1].value = AreasInteres[j].value;
			comboSel.options[comboSel.options.length-1].style.color = opti_fondo;
		}
 		if(AreasInteres[j].noSel)
		{
			comboNoSel.options.length++;
			comboNoSel.options[comboNoSel.options.length-1].text = textNoSel;
			comboNoSel.options[comboNoSel.options.length-1].value = AreasInteres[j].value;
			comboNoSel.options[comboNoSel.options.length-1].style.color = opti_fondo;
		}
	}
}