function busquedaListado() {
	document.getElementById("srch").value = document.getElementById("inputsrch").value;

	document.frmFiltros.action = document.frmFiltros.action + '?srch=' + document.getElementById("inputsrch").value;
	document.frmFiltros.action = document.frmFiltros.action + '&rslt=' + document.getElementById("rslt").value;
	document.frmFiltros.action = document.frmFiltros.action + '&cord=' + document.getElementById("cord").value;
	document.frmFiltros.action = document.frmFiltros.action + '&tord=' + document.getElementById("tord").value;
	document.frmFiltros.action = document.frmFiltros.action + '&idord=' + document.getElementById("idord").value;

	document.frmFiltros.submit();
}

function resultadosListado() {
	document.getElementById("rslt").value = document.getElementById("inputrslt").value;

	document.frmFiltros.action = document.frmFiltros.action + '?srch=' + document.getElementById("srch").value;
	document.frmFiltros.action = document.frmFiltros.action + '&rslt=' + document.getElementById("inputrslt").options[document.getElementById("inputrslt").selectedIndex].value;
	document.frmFiltros.action = document.frmFiltros.action + '&cord=' + document.getElementById("cord").value;
	document.frmFiltros.action = document.frmFiltros.action + '&tord=' + document.getElementById("tord").value;
	document.frmFiltros.action = document.frmFiltros.action + '&idord=' + document.getElementById("idord").value;

	document.frmFiltros.submit();
}

function ordenarListado(campo,tipo,flecha) {
	cord = document.getElementById("cord");
	document.getElementById("tord").value = tipo;

	strcord = cord.value;

	campo_coma = ', ' + campo + ' ASC';
	strcord = strcord.replace(campo_coma,"");
	campo_coma = ', ' + campo + ' DESC';
	strcord = strcord.replace(campo_coma,"");
	campo_coma = campo + ' ASC, ';
	strcord = strcord.replace(campo_coma,"");
	campo_coma = campo + ' DESC, ';
	strcord = strcord.replace(campo_coma,"");
	strcord = strcord.replace(campo+" ASC","");
	strcord = strcord.replace(campo+" DESC","");

	if (strcord == '') {
		cord.value = campo + ' ' + tipo;
	}
	else {
		cord.value = campo + ' ' + tipo + ', ' + strcord;
	}

	document.frmFiltros.action = document.frmFiltros.action + '?srch=' + document.getElementById("srch").value;
	document.frmFiltros.action = document.frmFiltros.action + '&rslt=' + document.getElementById("inputrslt").value;
	document.frmFiltros.action = document.frmFiltros.action + '&cord=' + document.getElementById("cord").value;
	document.frmFiltros.action = document.frmFiltros.action + '&tord=';// + document.getElementById("tord").value;
	document.frmFiltros.action = document.frmFiltros.action + '&idord=' + document.getElementById(flecha).id;

	document.getElementById("idord").value = document.getElementById(flecha).id;

	document.frmFiltros.submit();
}

function ordenarListadoSimple(campo,tipo,flecha) {
	cord = document.getElementById("cord");
	document.getElementById("tord").value = tipo;

	strcord = cord.value;

	campo_coma = ', ' + campo + ' ASC';
	strcord = strcord.replace(campo_coma,"");
	campo_coma = ', ' + campo + ' DESC';
	strcord = strcord.replace(campo_coma,"");
	campo_coma = campo + ' ASC, ';
	strcord = strcord.replace(campo_coma,"");
	campo_coma = campo + ' DESC, ';
	strcord = strcord.replace(campo_coma,"");
	strcord = strcord.replace(campo+" ASC","");
	strcord = strcord.replace(campo+" DESC","");

	if (strcord == '') {
		cord.value = campo + ' ' + tipo;
	}
	else {
		cord.value = campo + ' ' + tipo + ', ' + strcord;
	}

	document.frmFiltros.action = document.frmFiltros.action + '&cord=' + document.getElementById("cord").value;
	document.frmFiltros.action = document.frmFiltros.action + '&tord=';// + document.getElementById("tord").value;
	document.frmFiltros.action = document.frmFiltros.action + '&idord=' + document.getElementById(flecha).id;

	document.getElementById("idord").value = document.getElementById(flecha).id;

	document.frmFiltros.submit();
}

function cambiarSrcFlecha(flecha) {
	if (flecha.substring(0,3) == 'asc') document.getElementById(flecha).src = '../../img/ico_ord_asc_sel.gif';
	if (flecha.substring(0,3) == 'des') document.getElementById(flecha).src = '../../img/ico_ord_desc_sel.gif';
}

function cambiarSrcFlechaPopup(flecha) {
	if (flecha.substring(0,3) == 'asc') document.getElementById(flecha).src = '../../../img/ico_ord_asc_sel.gif';
	if (flecha.substring(0,3) == 'des') document.getElementById(flecha).src = '../../../img/ico_ord_desc_sel.gif';
}

// FUNCION PARA SELECCIONAR O DESSELECCIONAR TODOS LOS CHECKBOX A LA VEZ
function seleccionMultiple(chkbox) {
	for (var i=0;i<document.frmListado.elements.length;i++) {
		if (document.frmListado.elements[i].type == "checkbox") {
			document.frmListado.elements[i].checked = chkbox.checked;
		}
	}
}

// VALIDACION Y CONFIRMACION AL ELIMINAR REGISTROS
function validarEliminacionUno(id) {
	opcion = confirm("Se eliminara el registro seleccionado.\n¿Desea continuar?");
	if (opcion) {
		document.location.href = 'grupos.php?accion=del&id=' + id;
	}
}

function validarEliminacionVarios() {
	seleccionados = false;
	for (var i=0;i<document.frmListado.elements.length;i++) {
		if (document.frmListado.elements[i].type == "checkbox" && document.frmListado.elements[i].checked) {
			seleccionados = true;
			break;
		}
	}

	if (seleccionados) {
		action = document.frmListado.action;
		if (action.search(/\?/) > -1) document.frmListado.action = document.frmListado.action + '&accion=del';
		else document.frmListado.action = document.frmListado.action + '?accion=del';
		document.frmListado.submit();
	}
	else {
		alert("No ha seleccionado ningun registro para eliminar.");
	}
}

function validarEliminacionVariosDetalle(url) {
	seleccionados = false;
	for (var i=0;i<document.frmListado.elements.length;i++) {
		if (document.frmListado.elements[i].type == "checkbox" && document.frmListado.elements[i].checked) {
			seleccionados = true;
			break;
		}
	}

	if (seleccionados) {
		document.frmListado.action = url + '?accion=del';
		document.frmListado.submit();
	}
	else {
		alert("No ha seleccionado ningun registro para eliminar.");
	}
}

function validarEliminacionVariosConOrigen() {
	seleccionados = false;
	for (var i=0;i<document.frmListado.elements.length;i++) {
		if (document.frmListado.elements[i].type == "checkbox" && document.frmListado.elements[i].checked) {
			seleccionados = true;
			break;
		}
	}

	if (seleccionados) {
		document.frmListado.action = document.frmListado.action + '&accion=del';
		document.frmListado.submit();
	}
	else {
		alert("No ha seleccionado ningun registro para eliminar.");
	}
}

// ORDENACION REGISTROS
function subirElemento(inputSelect) {
	eleSel = document.getElementById(inputSelect);
	seleccionado = eleSel.selectedIndex;
	//alert(seleccionado);
	if ((seleccionado != -1) && (seleccionado > 0)) {
		value_ant = 0;
		value_temp = eleSel[seleccionado-1].value;
		text_temp = eleSel[seleccionado-1].text;
		eleSel[seleccionado-1].value = eleSel[seleccionado].value;
		eleSel[seleccionado-1].text = eleSel[seleccionado].text;
		eleSel[seleccionado].value = value_temp;
		eleSel[seleccionado].text = text_temp;
		eleSel.selectedIndex = seleccionado-1;
	}
}

function bajarElemento(inputSelect) {
	eleSel = document.getElementById(inputSelect);
	seleccionado = eleSel.selectedIndex;
	//alert(seleccionado);
	if ((seleccionado != -1) && (seleccionado < (eleSel.length-1))) {
		value_ant = 0;
		value_temp = eleSel[seleccionado+1].value;
		text_temp = eleSel[seleccionado+1].text;
		eleSel[seleccionado+1].value = eleSel[seleccionado].value;
		eleSel[seleccionado+1].text = eleSel[seleccionado].text;
		eleSel[seleccionado].value = value_temp;
		eleSel[seleccionado].text = text_temp;
		eleSel.selectedIndex = seleccionado+1;
	}
}

function vistaPrevia() {
	alert(document.getElementById("imagen").value);
	document.getElementById("img_previa").src = document.getElementById("imagen").value;
	document.getElementById("img_previa").src.replace(/\\/,"\\");
	document.getElementById("vista_previa").style.display = "block";
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////FUNCIONES PARA PASO DE OPTIONS ENTRE DOS SELECTS /////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////

// MIAS NUEVAS 16-04-2008 SERGIO

// ESPECIFICA PARA ASOCIACION DE ARCHIVOS CON SUBCARPETAS
function agregarOptionSubcarpeta(formulario,input,input_sel,quitar,subcarpeta) {
	// Variables
	var eleinput = formulario.getElementById(input);
	var eleinput_sel = formulario.getElementById(input_sel);

	// Si existe opcion, select disabled o opcion erronea no la pasamos
	for (i=0;i<=eleinput_sel.length-1;i++) {
		if (eleinput.value == eleinput_sel.options[i].value) return;
	}
	if (eleinput.disabled == true) return;
	if(eleinput.selectedIndex == -1) return;

	// Paso de valores
	valor_pasando = eleinput.options[eleinput.selectedIndex].value
	texto_pasando = subcarpeta+eleinput.options[eleinput.selectedIndex].text;
	posicion_pasando = eleinput.options[eleinput.selectedIndex].id;
	eleinput_sel.options[eleinput_sel.options.length] = new Option(texto_pasando,valor_pasando);
	eleinput_sel.options[eleinput_sel.options.length-1].id = posicion_pasando;
	//eleinput_sel.options[eleinput_sel.options.length-1].selected = true;

	// Quitamos o no la opcion del select origen
	if (quitar == 1) eleinput.options[eleinput.selectedIndex] = null;
}

function agregarOption(formulario,input,input_sel,quitar) {
	// Variables
	var eleinput = formulario.getElementById(input);
	var eleinput_sel = formulario.getElementById(input_sel);

	// Si existe opcion, select disabled o opcion erronea no la pasamos
	for (i=0;i<=eleinput_sel.length-1;i++) {
		if (eleinput.value == eleinput_sel.options[i].value) return;
	}
	if (eleinput.disabled == true) return;
	if(eleinput.selectedIndex == -1) return;

	// Paso de valores
	valor_pasando = eleinput.options[eleinput.selectedIndex].value
	texto_pasando = eleinput.options[eleinput.selectedIndex].text;
	posicion_pasando = eleinput.options[eleinput.selectedIndex].id;
	eleinput_sel.options[eleinput_sel.options.length] = new Option(texto_pasando,valor_pasando);
	eleinput_sel.options[eleinput_sel.options.length-1].id = posicion_pasando;
	//eleinput_sel.options[eleinput_sel.options.length-1].selected = true;

	// Quitamos o no la opcion del select origen
	if (quitar == 1) eleinput.options[eleinput.selectedIndex] = null;
}

function quitarOption(num_elem,formulario,input,input_sel,agregar) {
	// Variables
	var eleinput = formulario.getElementById(input);
	var eleinput_sel = formulario.getElementById(input_sel);

	// Validacion
	if(num_elem < 0) return;

	// Paso valores
	valor_pasando = eleinput_sel.options[num_elem].value;
	texto_pasando = eleinput_sel.options[num_elem].text;
	posicion_pasando = eleinput_sel.options[num_elem].id;
	eleinput_sel.options[num_elem] = null;

	if (agregar == 1) {
		// Retorno de opcion al select origen
		eleinput.options[eleinput.options.length] = new Option(texto_pasando,valor_pasando);
		eleinput.options[eleinput.options.length-1].id = posicion_pasando;

		act_posicion = eleinput.options.length-1;

		if (act_posicion > 0) cumple_condicion = parseInt(eleinput.options[act_posicion].id) < parseInt(eleinput.options[act_posicion-1].id);
		else cumple_condicion = 0;

		while(cumple_condicion) {
			tmp_valor = eleinput.options[act_posicion].value;
			tmp_texto = eleinput.options[act_posicion].text;
			tmp_posicion = eleinput.options[act_posicion].id;

			eleinput.options[act_posicion].value = eleinput.options[act_posicion-1].value;
			eleinput.options[act_posicion].text = eleinput.options[act_posicion-1].text;
			eleinput.options[act_posicion].id = eleinput.options[act_posicion-1].id;

			act_posicion--;

			eleinput.options[act_posicion].value = tmp_valor;
			eleinput.options[act_posicion].text = tmp_texto;
			eleinput.options[act_posicion].id = tmp_posicion;

			if(act_posicion == 0) cumple_condicion = false;
			else cumple_condicion = parseInt(eleinput.options[act_posicion].id) < parseInt(eleinput.options[act_posicion-1].id);
		}
	}
}

function vaciarSelect(formulario,input,input_sel,agregar) {
	var eleinput_sel = formulario.getElementById(input_sel);
	while(eleinput_sel.options.length > 0) {
		quitarOption(0,formulario,input,input_sel,agregar);
	}
}


///////////////



max_municipios = "3";
//Intenta enviar la aficion a la tabla de seleccionadas
function try_add(formulario,input,input_sel)
{
	var eleinput = formulario.getElementById(input);
	var eleinput_sel = formulario.getElementById(input_sel);

	for (i=0;i<=eleinput_sel.length-1;i++) {
		//eleSel.options[i].selected = true;
		if (eleinput.value == eleinput_sel.options[i].value) return;
		//alert("seleccionado"+i);
	}

	if (eleinput.disabled == true) {
		return;
	}
	if(eleinput.selectedIndex == -1)
	{
		return;
	}
	//hay elementos seleccionados en la lista
	/*if(demasiados_muni(FrmClientes.list_sel_muni))
	{
		alert("Puede seleccionar un máximo de 3 municipios");
		return;
	}*/
	//hay menos de 3 municipios seleccionados
	valor_pasando = eleinput.options[eleinput.selectedIndex].value

	texto_pasando = eleinput.options[eleinput.selectedIndex].text;

	posicion_pasando = eleinput.options[eleinput.selectedIndex].id;


	eleinput_sel.options[eleinput_sel.options.length] = new Option(texto_pasando,valor_pasando);
	eleinput_sel.options[eleinput_sel.options.length-1].id = posicion_pasando;
	eleinput_sel.options[eleinput_sel.options.length-1].selected = true;
	eleinput.options[eleinput.selectedIndex] = null;

}

//Intenta eliminar el municipio situado en num_elem de la lista de seleccionados
function try_quit(num_elem,formulario,input,input_sel)
{
	var eleinput = formulario.getElementById(input);
	var eleinput_sel = formulario.getElementById(input_sel);
	if(num_elem < 0)
	{
		return;
	}
	valor_pasando = eleinput_sel.options[num_elem].value;
	texto_pasando = eleinput_sel.options[num_elem].text;
	posicion_pasando = eleinput_sel.options[num_elem].id;
	eleinput_sel.options[num_elem] = null;

	eleinput.options[eleinput.options.length] = new Option(texto_pasando,valor_pasando);
	eleinput.options[eleinput.options.length-1].id = posicion_pasando;

	act_posicion = eleinput.options.length-1;

	if (act_posicion > 0) cumple_condicion = parseInt(eleinput.options[act_posicion].id) < parseInt(eleinput.options[act_posicion-1].id);
	else cumple_condicion = 0;

	while(cumple_condicion)
	{
		tmp_valor = eleinput.options[act_posicion].value;
		tmp_texto = eleinput.options[act_posicion].text;
		tmp_posicion = eleinput.options[act_posicion].id;

		eleinput.options[act_posicion].value = eleinput.options[act_posicion-1].value;
		eleinput.options[act_posicion].text = eleinput.options[act_posicion-1].text;
		eleinput.options[act_posicion].id = eleinput.options[act_posicion-1].id;

		act_posicion--;

		eleinput.options[act_posicion].value = tmp_valor;
		eleinput.options[act_posicion].text = tmp_texto;
		eleinput.options[act_posicion].id = tmp_posicion;

		if(act_posicion == 0)
		{
			cumple_condicion = false;
		}
		else
		{
			cumple_condicion = parseInt(eleinput.options[act_posicion].id) < parseInt(eleinput.options[act_posicion-1].id);
		}
	}
}

//Intenta vaciar la lista de seleccionados
function try_quit_all(formulario,input,input_sel)
{
	var eleinput_sel = formulario.getElementById(input_sel);
	while(eleinput_sel.options.length > 0)
	{
		try_quit(0,formulario,input,input_sel);
	}
}

//Pone a selected las opciones de un select multiple para guardarlas
function opcionesSelected(inputSelect,inputHidden) {
	document.getElementById(inputSelect).multiple=true;
	eleSel = document.getElementById(inputSelect);
	inpHid = document.getElementById(inputHidden);
	if (eleSel.options[0] != null) {
		arrayOptions = new Array();
		//alert('entro aqui');
		for (i=0;i<=eleSel.length-1;i++) {
			//eleSel.options[i].selected = true;
			arrayOptions[i] = eleSel.options[i].value;
			//alert("seleccionado"+i);
		}
		inpHid.value = arrayOptions.join(",");
	}
}


// Sirve para limitar el numero de opciones en intercambio entre selects
function maximoOpciones(inputSelect,inputSelectSel,numOpc) {
	selopc = document.getElementById(inputSelect);
	selopcsel = document.getElementById(inputSelectSel);
	if (selopcsel.length == numOpc) {
		selopc.disabled = true;
		selopc.blur();
	}
	else {
		selopc.disabled = false;
	}
}

function formato_cantidad(inp) {
	cantidad = document.getElementById(inp).value;
	pattern = /^\d+$/;
	if (!pattern.test(cantidad)) {
		document.getElementById(inp).value = 0;
	}
}

function formato_numerico(inp) {
	cantidad = document.getElementById(inp).value;
	pattern = /^\d+$/;
	if (!pattern.test(cantidad)) {
		document.getElementById(inp).value = '';
	}
}

function formato_dinero(inp) {
	valor = document.getElementById(inp).value;
	pattern = /^(?:\+|-)?\d+$/;
	pattern2 = /^(?:\+|-)?\d+,$/;
	if ((!pattern.test(valor)) && (!pattern2.test(valor))) {
		//çdocument.getElementById(inp).value = 0;
		pattern = /^(?:\+|-)?\d+,\d+$/;
		if (!pattern.test(valor)){
			if (document.getElementById(inp).value != '') {
				document.getElementById(inp).value = '';
			}
			//textoError = "\nPrecio Tarifa";
			//msg += textoError;
		}
		else {
			arrvalor = valor.split(",");
			document.getElementById(inp).value = arrvalor[0] + "," + arrvalor[1].substring(0,2);
		}
	}
}

function decimales(inp) {
	valor = document.getElementById(inp).value;
	arrvalor = valor.split(",");
	//alert(arrvalor.length);
	if (arrvalor != '') {
		if (arrvalor.length == 2) {
			if (arrvalor[1].length == 1) {
				document.getElementById(inp).value = arrvalor[0] + "," + arrvalor[1].substring(0,1) + 0;
			}
			if (arrvalor[1] == '') {
				document.getElementById(inp).value = arrvalor[0] + "," + '00';
			}
		}
		else {
			document.getElementById(inp).value = arrvalor[0] + "," + '00';
		}
	}
	else {
		document.getElementById(inp).value = '';
	}
}

function crearCuentaUsuarioSiNo() {
	if(document.getElementById('crear_cuenta').checked==true) {
		//document.getElementById('login').value = '';
		//document.getElementById('password').value = '';
		//document.getElementById('repite_password').value = '';
		//document.getElementById('email_user').value = '';
		document.getElementById('login').readOnly = false;
		document.getElementById('password').readOnly = false;
		document.getElementById('repite_password').readOnly = false;
		document.getElementById('email_user').readOnly = false;
		document.getElementById('login').className = '';
		document.getElementById('password').className = '';
		document.getElementById('repite_password').className = '';
		document.getElementById('email_user').className = '';
	}
	else {
		document.getElementById('login').value = '';
		document.getElementById('password').value = '';
		document.getElementById('repite_password').value = '';
		document.getElementById('email_user').value = '';
		document.getElementById('login').readOnly = true;
		document.getElementById('password').readOnly = true;
		document.getElementById('repite_password').readOnly = true;
		document.getElementById('email_user').readOnly = true;
		document.getElementById('login').className = 'lectura';
		document.getElementById('password').className = 'lectura';
		document.getElementById('repite_password').className = 'lectura';
		document.getElementById('email_user').className = 'lectura';
	}
}

function vista_previa(imagen,previa) {
	document.getElementById(previa).src = document.getElementById(imagen).value;
}

//Deshabilita el envio de formularios al pulsar intro
function stopRKey(evt) {
	var evt = (evt) ? evt : ((event) ? event : null);
	var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
	if ((evt.keyCode == 13) && (node.type=="text")) {return false;}
}
document.onkeypress = stopRKey;
