//Código JS para la e-guía profesional

function init() {
	document.getElementById("first_1").selected = "true";
	//document.getElementById("first_2").selected = "true";
	document.getElementById("first_3").selected = "true";
	
	document.getElementById("producto").style.display = "none";
	document.getElementById("botoncillos").style.display = "none";
	document.getElementById("items").style.display = "none";
	
	exp = /msj/
	document.getElementById("formacceso").style.display = "none";
}

function cambiar_formulario(tipo) {
	if (tipo.value != "0") {
		document.getElementById("formulario").style.display = "block";
		if (tipo.value == "p") {
			document.getElementById("agente").style.display = "none";
			document.getElementById("zonas").style.display = "none";
			document.getElementById("selecciones").style.display = "block";
			document.getElementById("empresa").style.display = "block";
			document.getElementById("categoria").style.display = "block";
			if (document.getElementById("first_3").selected == false) {
				document.getElementById("producto").style.display = "block";
				document.getElementById("botoncillos").style.display = "block";
				document.getElementById("items").style.display = "block";
			}
		} else if (tipo.value == "r") {
			document.getElementById("agente").style.display = "block";
			document.getElementById("selecciones").style.display = "block";
			document.getElementById("zonas").style.display = "block";
			document.getElementById("empresa").style.display = "none";
			document.getElementById("categoria").style.display = "none";
			document.getElementById("producto").style.display = "none";
			document.getElementById("botoncillos").style.display = "none";
			document.getElementById("items").style.display = "none";
		} else {
			document.getElementById("formulario").style.display = "none";
			document.getElementById("form_busqueda").submit();
			//parent.location = "buscar.asp?act="+tipo.value;
		}
	} else {
		document.getElementById("formulario").style.display = "none";
	}
}

function cambiar_productos(tipo) {
	valor = tipo.value;
	valor = valor.substring(0,valor.indexOf("_"));
	categ = document.getElementById("b_categoria").value;
	categ = categ.substring(categ.indexOf("_")+1);
	if (tipo.value != "0_todas") {
		document.getElementById("producto").style.display = "block";
		document.getElementById("botoncillos").style.display = "block";
		document.getElementById("items").style.display = "block";
		
		var select = categ+'<br /><select id="b_producto" size="5" class="seleccion" style="width:180px !important; width:200px;">';
		var info = array_pro[valor-1];
		for (i=0; i<info.length; i++) {
			value = info[i];
			text = info[i].substring(info[i].lastIndexOf("_")+1);
			if (text != "Otros") {
				select = select + '<option value="'+value+'">'+text+'</option>';
			}
		}
		select = select + '</select>'
		
		document.getElementById("producto").innerHTML = select;

	} else {
		document.getElementById("producto").style.display = "none";
		document.getElementById("botoncillos").style.display = "none";
		document.getElementById("items").style.display = "none";
	}
}

function elementosabuscar(que) {
	elvalor = document.getElementById("b_producto").value;
	elvalor = elvalor.substring(0,elvalor.lastIndexOf("_"));
	if (que == "anadir" && elvalor != "") {
		distinto = true;
		objeto = document.getElementById("b_item");
		v_categ = elvalor.substring(0,elvalor.indexOf("_"));
		v_prod = elvalor.substring(elvalor.indexOf("_")+1);
		for (i=0; opt=objeto.options[i]; i++) {
			opcion = opt.value;
			if (opcion == elvalor || (opcion.substring(opcion.indexOf("_")+1) == "0" && opcion.substring(0,opcion.indexOf("_")) == v_categ)) {
				distinto = false;
				break;
			} else if (v_prod == "0" && opcion.substring(0,opcion.indexOf("_")) == v_categ) {
				objeto.options[i] = null;
				i = -1;
			}
		}
		if (distinto) {
			prod = document.getElementById("b_producto").value;
			p = prod.substring(0,prod.lastIndexOf("_"));
			categ = document.getElementById("b_categoria").value;
			categ = categ.substring(categ.indexOf("_")+1);
			prod = prod.substring(prod.lastIndexOf("_")+1);
			datos = categ + " - " + prod;
			
			eval("var etiqueta_"+p+" = document.createElement('option')");
			eval("etiqueta_"+p+".id = 'buscar_"+p+"'");
			eval("etiqueta_"+p+".value = '"+p+"'");
			eval("etiqueta_"+p+".appendChild(document.createTextNode('"+datos+"'))");
			document.getElementById("b_item").appendChild(eval("etiqueta_"+p));
		}
	} else {
		objeto = document.getElementById("b_item");
		for (i=0; opt=objeto.options[i]; i++) {
			if (opt.selected) {
				objeto.options[i] = null;
			}
		}
	}
}

function elementosaguardar(que) {
	lamarca = document.getElementById("b_marca").value;
	lamarca_num = lamarca.substring(0,lamarca.lastIndexOf("_"));
	lamarca_nombre = lamarca.substring(lamarca.indexOf("_")+1);
	elvalor = document.getElementById("b_producto").value;
	elvalor = elvalor.substring(0,elvalor.lastIndexOf("_"));
	if (que == "anadir" && elvalor != "") {
		distinto = true;
		objeto = document.getElementById("b_item");
		v_categ = elvalor.substring(0,elvalor.indexOf("_"));
		v_prod = elvalor.substring(elvalor.indexOf("_")+1);
		lanumeracion = lamarca_num+"_"+elvalor;
		for (i=0; opt=objeto.options[i]; i++) {
			opcion = opt.value;
			opcion2 = opcion.substring(opcion.indexOf("_")+1);
			op_m = opcion.substring(0,opcion.indexOf("_"));
			op_c = opcion2.substring(0,opcion2.indexOf("_"));
			op_p = opcion2.substring(opcion2.indexOf("_")+1);
			if (opcion == lanumeracion || (op_m == lamarca_num && op_c == v_categ && op_p == "0") || (op_m == "0" && op_c == v_categ && op_p == "0") || (op_m == "0" && op_c == v_categ && op_p == v_prod)) {
				distinto = false;
				break;
			} else if ((v_prod == "0" && op_c == v_categ && op_m == lamarca_num) || (op_c == v_categ && op_p == v_prod && lamarca_num == "0") || (lamarca_num == "0" && op_c == v_categ && v_prod == "0")) {
				objeto.options[i] = null;
				i = -1;
			}
		}
		if (distinto) {
			prod = document.getElementById("b_producto").value;
			p = prod.substring(0,prod.lastIndexOf("_"));
			categ = document.getElementById("b_categoria").value;
			categ = categ.substring(categ.indexOf("_")+1);
			prod = prod.substring(prod.lastIndexOf("_")+1);
			datos = lamarca_nombre + " - " + categ + " - " + prod;
			p = lamarca_num+"_"+p;
			eval("var etiqueta_"+p+" = document.createElement('option')");
			eval("etiqueta_"+p+".id = 'buscar_"+p+"'");
			eval("etiqueta_"+p+".value = '"+p+"'");
			eval("etiqueta_"+p+".appendChild(document.createTextNode('"+datos+"'))");
			document.getElementById("b_item").appendChild(eval("etiqueta_"+p));
		}
	} else {
		objeto = document.getElementById("b_item");
		for (i=0; opt=objeto.options[i]; i++) {
			if (opt.selected) {
				objeto.options[i] = null;
			}
		}
	}
}

function abuscar(sin) {
	objeto = document.getElementById("b_item");
	for (i=0; opt=objeto.options[i]; i++) {
		objeto.options[i].selected = true;
	}
	if (sin != "sinreturn") {
		return true;
	}
}

function checkenvio(formulario, campos_revisar, id) {
   campos = campos_revisar.split(",")
   for (i=0; i<campos.length; i++) {
      campo = eval("formulario." + campos[i])
      rExp = / /gi  //para quitar espacios en blanco
      if (campo.value.replace(rExp,"")=="") {	
		if (campo.name=="imagen" && campo.value == "") {
			if (confirm('¿Desea continuar sin adjuntar su imagen o logotipo?')) {
				location.href="index.asp?msj=enviado";
				return false;
			} else {
				return false;
			}
		} else {
			rExp = /_/gi
			nombre = campo.name.replace(rExp," ")
			alert("Hay campos obligatorios sin rellenar.\nPor favor, compruébelo.")
			return false;
		}
      } else if (campo.name=="fundacion" || campo.name=="cpostal" || campo.name=="telefono1") {
			if (isNaN(campo.value)) {
				alert("Hay campos erróneos.\nPor favor, asegúrese de escribir sólo números donde corresponda.")
				return false;
			}
	  }
   }
   return true;
}

function confirmarEliminarImagen(id){
	if (confirm('¿Seguro que desea borrar la imagen?')) {
		location.href="edicion.asp?delete=1&id="+id; 
		return false;
	} else {
		return false;
	}	
}

function mostrarformacceso() {
	if (document.getElementById("formacceso").style.display == "none") {
		document.getElementById("formacceso").style.display = "block";
	} else {
		document.getElementById("formacceso").style.display = "none";
	}
}

function confirmEliminar() {
	if (confirm("Se va a eliminar un registro permanentemente.\n¿Desea continuar?")) {
		return true;
	}
	return false;
}

function cambiarTipo(tipo) {
	var elagente = document.getElementById("elagente");
	var elnif = document.getElementById("elnif");
	var lazona = document.getElementById("lazona");

	if (tipo.value == "1" || tipo.value != "2") {
		elagente.style.display = "none";
		elnif.style.display = "none";
		lazona.style.display = "none";
	} else {
		elagente.style.display = "block";
		elnif.style.display = "block";
		lazona.style.display = "block";
	}
}

function saltoMenu(ventana,objeto) {
  eval(ventana+".location='"+objeto.options[objeto.selectedIndex].value+"'");
}

function objetoAjax(){
	var xmlhttp=false;
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
		   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp = false;
  		}
	}

	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}

function hacerConsulta(datos){
	divResultado = document.getElementById('listamarcas');
	document.getElementById('marcajax').value = "";
	if (datos != 'consultajax.asp?p=' && datos != 'consultajax.asp?p= ') {
		divResultado.innerHTML = '<img class="img" src="imagen/logos/cargando.gif" width="140" height="12" align="left" />';
		ajax=objetoAjax();
		ajax.open("GET", datos);
		ajax.onreadystatechange=function() {
			if (ajax.readyState==4) {
				divResultado.innerHTML = ajax.responseText
			}
		}
		ajax.send(null)
	} else {
		alert('Error en el dato introducido.\nPor favor, vuelva a intentarlo.');
	}
}
