<!--
	function getParametros(formu) {
		var parametros = "";
		for(var i=0;i<formu.length;i++) {
			if(formu.elements[i].type == "button" || formu.elements[i].type == "reset" || formu.elements[i].type == "submit") continue;
			
			if(parametros != "") {
				if (formu.elements[i].type == "radio" || formu.elements[i].type == "checkbox") {
					if (formu.elements[i].checked) {
						parametros += "&" + formu.elements[i].name + "=" + formu.elements[i].value;	
					}
				}
				else {
					parametros += "&" + formu.elements[i].name + "=" + encodeURIComponent(formu.elements[i].value);
				}
			}
			else {
				if (formu.elements[i].type == "radio" || formu.elements[i].type == "checkbox") {
					if (formu.elements[i].checked) {
						parametros += formu.elements[i].name + "=" + formu.elements[i].value;	
					}
				}
				else {
					parametros += formu.elements[i].name + "=" + encodeURIComponent(formu.elements[i].value);
				}
			}
		}	
		return parametros;
	}

	function mostrarHorario(frmControl, horarioControl, id_alerta) {
		if(typeof(id_alerta) != 'undefined') {
			var ifrm = 'ifr_alerta_cotizacion_' + id_alerta;
			var frm = parent.frames[ifrm].document.forms[frmControl];
		} else {
			var frm = document.getElementById(frmControl);
		}
		window.document.getElementById(horarioControl).style.display = frm.sel_frecuencia.value == 2 ? 'block' : 'none';
		if(frm.sel_frecuencia.value != 1) {
			frm.hh.focus();
		}
	}	

	function crearAlerta() {
		var frm = document.frmAltaAlerta;
		if(document.getElementById('div_horario').style.display == 'block') {
			var hora = frm.hh.value;
			if(hora == '' || isNaN(hora) || hora > 24 || hora < 0) {
				alert("El formato de la hora no es correcto");
				frm.hh.focus();
				return;
			}
			var min = frm.mm.value;
			if(min == '' || isNaN(min) || min > 59 || min < 0) {
				alert("El formato de los minutos no es correcto");
				frm.mm.focus();
				return;
			}
		}

		if(checkForm(frm)) {
			frm.accion.value = 'crear-alerta';
			var httpPost = createRequestObject();
			httpPost.open("post", "/backend/cotizaciones/alertas-bbdd.php", true);
			httpPost.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			httpPost.onreadystatechange = function () {
				if(httpPost.readyState == 4){
					if (httpPost.responseText) {
							var resp = httpPost.responseText;
							if(resp.indexOf('OK#') >= 0) {
								var arr = resp.split("#");
								if(arr[1] != '') {
									window.document.getElementById('div_aux').innerHTML = '<strong>'+arr[1]+'</strong>';
								} else {
									window.document.getElementById('div_aux').innerHTML = '<strong>Se ha creado correctamente la alerta</strong>';
								}
								window.document.getElementById('div_aux').style.display = 'block';
								getAlertas();
							} else {
								var arr = resp.split("#");
								if(arr[1] != '') {
									window.document.getElementById('div_aux').innerHTML = '<strong>'+arr[1]+'</strong>';
								} else {
									window.document.getElementById('div_aux').innerHTML = '<strong>Ha ocurrido un error al intentar crear la alerta. Por favor, inténtalo más tarde</strong>';
								}
								window.document.getElementById('div_aux').style.display = 'block';
							}
					}
				}
			};
			httpPost.send(getParametros(frm));
		} 
	}

	function getAlertas() {
		var http = createRequestObject();
		http.open("get", "/backend/cotizaciones/alertas-bbdd.php?accion=obtener-alertas&r=" + Math.random());
		http.onreadystatechange = function () {
			if(http.readyState == 4){
				if (http.responseText) {
						var resp = http.responseText;
						//alert(resp);
						window.document.getElementById('div_alertas').innerHTML = resp;
				}
			}
		};
		http.send(null);

	}

	function modificarAlerta(id_alerta) {
		var capa = document.getElementById('div_alerta_' + id_alerta);
		capa.style.display = 'block';
	}

	function eliminarAlerta(id_alerta) {
		var capa = document.getElementById('div_alerta_' + id_alerta);
		if(confirm("¿Seguro que quieres eliminar la alerta?")) {
			var http = createRequestObject();
			http.open("get", "/backend/cotizaciones/alertas-bbdd.php?accion=eliminar-alerta&id_alerta=" + id_alerta + "&r=" + Math.random());
			http.onreadystatechange = function () {
				if(http.readyState == 4){
					if (http.responseText) {
							var resp = http.responseText;
							if(resp == 'OK') {
								alert("La alerta se ha eliminado correctamente");
								getAlertas();
							} else {
								capa.innerHTML = '<strong>' + resp + '</strong>' + capa.innerHTML;
							}
					}
				}
			};
			http.send(null);
		}
	}

	function guardarAlerta(id_alerta) {
		if(typeof(id_alerta) != 'undefined') {
			var ifrm = 'ifr_alerta_' + id_alerta;
			var frm = parent.frames[ifrm].document.forms['frmAlerta'];
		} else {
			var frm = document.getElementById('frmAlerta');
		}


		if(window.document.getElementById('div_horario').style.display == 'block') {
			var hora = frm.hh.value;
			if(hora == '' || isNaN(hora) || hora > 24 || hora < 0) {
				alert("El formato de la hora no es correcto");
				frm.hh.focus();
				return;
			}
			var min = frm.mm.value;
			if(min == '' || isNaN(min) || min > 59 || min < 0) {
				alert("El formato de los minutos no es correcto");
				frm.mm.focus();
				return;
			}
		}


		if(checkForm(frm)) {
			frm.accion.value = 'modificar-alerta';
			var httpPost = createRequestObject();
			httpPost.open("post", "/backend/cotizaciones/alertas-bbdd.php", true);
			httpPost.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			httpPost.onreadystatechange = function () {
				if(httpPost.readyState == 4){
					if (httpPost.responseText) {
							var resp = httpPost.responseText;
							var capa = window.document.getElementById('div_msg');
							if(resp.indexOf('OK#') >= 0) {
								var arr = resp.split("#");
								if(arr[1] != '') {
									capa.innerHTML = '<strong>'+arr[1]+'</strong>';
								} else {
									capa.innerHTML = '<strong>Se ha modificado correctamente la alerta</strong>';
								}
								//getAlertas();
							} else {
								var arr = resp.split("#");
								if(arr[1] != '') {
									capa.innerHTML = '<strong>'+arr[1]+'</strong>';
								} else {
									capa.innerHTML = '<strong>Ha ocurrido un error al intentar modificar la alerta. Por favor, inténtalo más tarde</strong>';
								}
							}
					}
				}
			};
			httpPost.send(getParametros(frm));
		} 
	}

	function getAlertasCotizaciones() {
		var http = createRequestObject();
		http.open("get", "/backend/cotizaciones/alertas-bbdd.php?accion=obtener-alertas-cotizaciones&r=" + Math.random());
		http.onreadystatechange = function () {
			if(http.readyState == 4){
				if (http.responseText) {
						var resp = http.responseText;
						//alert(resp);
						parent.document.getElementById('div_alertas_cotizaciones').innerHTML = resp;
				}
			}
		};
		http.send(null);

	}

	function mostrarDatosAlerta(id_alerta, orden_alerta) {
		var ifr = document.getElementById('ifr_alerta_' + id_alerta);
		if(ifr.src == 'about:blank') {
			ifr.src = "/backend/cotizaciones/alertas-bbdd.php?accion=mostrar-datos-alerta&id_alerta=" + id_alerta + "&orden_alerta=" + orden_alerta;
		}
		ifr.style.display = 'block';
		var bot = document.getElementById('ifr_boton_ocultar_alerta_' + id_alerta);
		bot.style.display = 'block';
	}

	function ocultarDatosAlerta(id_alerta) {
		var ifr = document.getElementById('ifr_alerta_' + id_alerta);
		ifr.style.display = 'none';
		var bot = document.getElementById('ifr_boton_ocultar_alerta_' + id_alerta);
		bot.style.display = 'none';
	}

	function buscarCotizacion() {
		var frm = window.document.frmAltaAlertaCotizacion;
		if(checkForm(frm)) {
			var http = createRequestObject();
			http.open("get", "/backend/cotizaciones/alertas-bbdd.php?accion=buscar-cotizacion&q=" + frm.q.value + "&r=" + Math.random());
			http.onreadystatechange = function () {
				if(http.readyState == 4){
					if (http.responseText) {
							var resp = http.responseText;
							//alert(resp);
							var arr = resp.split("#");
							if(arr[0] != '') {
								document.getElementById('div_cotizaciones').innerHTML = arr[0];
							}
							if(frm.id_alerta.value > 0) { // arr[1] Indica cuanto tengo q aumentar la altura del iframe
								var ifrm = 'ifr_alerta_cotizacion_' + frm.id_alerta.value;

								if(frm.id_alerta.value > 0) { // Aumento la altura del iframe
									var ifrm = 'ifr_alerta_cotizacion_' + frm.id_alerta.value;
									if(navigator.userAgent.indexOf("MSIE")!=-1) {
										parent.document.getElementById(ifrm).height = parent.document.getElementById(ifrm).contentWindow.document.body.scrollHeight;
									} else if(navigator.userAgent.indexOf("Firefox")!=-1){
										parent.document.getElementById(ifrm).height = parent.document.getElementById(ifrm).contentDocument.body.scrollHeight + 10 + 'px';
									}								
								}
								
							}

					}
				}
			};
			http.send(null);
		}
	}

	function seleccionarValor(codigo_mercado, codigo_valor, nombre_valor) {
		var frm = document.frmAltaAlertaCotizacion;
		document.getElementById('div_valor').innerHTML = 'Valor <strong>ya seleccionado</strong>';
		document.getElementById('div_valor_txt').innerHTML = '<strong>' + nombre_valor + '</strong>';
		document.getElementById('div_cotizaciones').innerHTML = '';
		frm.codigo_mercado.value = codigo_mercado;
		frm.codigo_valor.value = codigo_valor;
		frm.nombre_valor.value = nombre_valor;
		if(frm.id_alerta.value > 0) {
			var ifrm = 'ifr_alerta_cotizacion_' + frm.id_alerta.value;
			parent.document.getElementById(ifrm).height = 115;
		}
	}

	function mostrarCampos(id_alerta) {
		
		if(typeof(id_alerta) != 'undefined') {
			var ifrm = 'ifr_alerta_cotizacion_' + id_alerta;
			var frm = parent.frames[ifrm].document.forms['frmAltaAlertaCotizacion'];
		} else {
			var frm = document.getElementById('frmAltaAlertaCotizacion');
		}

		if(frm.sel_tipo.value == 1) {
			if(typeof(v) != 'undefined') {
				window.document.getElementById('div_campos_maxmin').style.display = 'none';
				window.document.getElementById('div_campos_porcentual').style.display = 'none';
				window.document.getElementById('div_campos_precio').style.display = 'none';
				window.document.getElementById('div_campos_cierre').style.display = 'block';
			} else {
				document.getElementById('div_campos_maxmin').style.display = 'none';
				document.getElementById('div_campos_porcentual').style.display = 'none';
				document.getElementById('div_campos_precio').style.display = 'none';
				document.getElementById('div_campos_cierre').style.display = 'block';
			}
		} else if(frm.sel_tipo.value == 4) {
			if(typeof(v) != 'undefined') {
				window.document.getElementById('div_campos_cierre').style.display = 'none';
				window.document.getElementById('div_campos_porcentual').style.display = 'none';
				window.document.getElementById('div_campos_precio').style.display = 'none';
				window.document.getElementById('div_campos_maxmin').style.display = 'block';
			} else {
				document.getElementById('div_campos_cierre').style.display = 'none';
				document.getElementById('div_campos_porcentual').style.display = 'none';
				document.getElementById('div_campos_precio').style.display = 'none';
				document.getElementById('div_campos_maxmin').style.display = 'block';
			}
		} else if(frm.sel_tipo.value == 2) {
			if(typeof(v) != 'undefined') {
				window.document.getElementById('div_campos_cierre').style.display = 'none';
				window.document.getElementById('div_campos_maxmin').style.display = 'none';
				window.document.getElementById('div_campos_porcentual').style.display = 'none';
				window.document.getElementById('div_campos_precio').style.display = 'block';
			} else {
				document.getElementById('div_campos_cierre').style.display = 'none';
				document.getElementById('div_campos_maxmin').style.display = 'none';
				document.getElementById('div_campos_porcentual').style.display = 'none';
				document.getElementById('div_campos_precio').style.display = 'block';
			}
		} else if(frm.sel_tipo.value == 3) {
			if(typeof(v) != 'undefined') {
				window.document.getElementById('div_campos_cierre').style.display = 'none';
				window.document.getElementById('div_campos_maxmin').style.display = 'none';
				window.document.getElementById('div_campos_precio').style.display = 'none';
				window.document.getElementById('div_campos_porcentual').style.display = 'block';
			} else {
				document.getElementById('div_campos_cierre').style.display = 'none';
				document.getElementById('div_campos_maxmin').style.display = 'none';
				document.getElementById('div_campos_precio').style.display = 'none';
				document.getElementById('div_campos_porcentual').style.display = 'block';

			}
		} else {
			if(typeof(v) != 'undefined') {
				window.document.getElementById('div_campos_cierre').style.display = 'none';
				window.document.getElementById('div_campos_maxmin').style.display = 'none';
				window.document.getElementById('div_campos_precio').style.display = 'none';
				window.document.getElementById('div_campos_porcentual').style.display = 'none';
			} else {
				document.getElementById('div_campos_cierre').style.display = 'none';
				document.getElementById('div_campos_maxmin').style.display = 'none';
				document.getElementById('div_campos_precio').style.display = 'none';
				document.getElementById('div_campos_porcentual').style.display = 'none';
			}
		}
	}

	function crearAlertaCotizacion() {
		var frm = document.frmAltaAlertaCotizacion;
		
		if(frm.codigo_valor.value == "") {
			alert("No has seleccionado ningún valor del buscador");
			return;
		}
		if(frm.sel_tipo.value == 2) {
			if(frm.precio_max.value == "" && frm.precio_min.value == "") {
				alert("No has rellenado ninguna de las condiciones para configurar la alerta");
				return;
			}
		}
		if(frm.sel_tipo.value == 3) {
			if(frm.dif_max.value == "" && frm.dif_min.value == "") {
				alert("No has rellenado ninguna de las condiciones para configurar la alerta");
				return;
			}
		}
		
		if(checkForm(frm)) {
			frm.accion.value = 'crear-alerta-cotizacion';
			var httpPost = createRequestObject();
	
			httpPost.open("post", "/backend/cotizaciones/alertas-bbdd.php", true);
			httpPost.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			
			httpPost.onreadystatechange = function () {
				if(httpPost.readyState == 4){
					if (httpPost.responseText) {
							var resp = httpPost.responseText;
							if(resp.indexOf('OK') >= 0) {
								alert("La alerta se ha creado correctamente " + resp);
								getAlertasCotizaciones();
							} else {
								var arr = resp.split("#");
								if(arr[1] != '') {
									window.document.getElementById('div_aux_cotizacion').innerHTML = '<strong>'+arr[1]+'</strong>';
								} else {
									window.document.getElementById('div_aux_cotizacion').innerHTML = '<strong>Ha ocurrido un error al intentar crear la alerta. Por favor, inténtalo más tarde</strong>';
								}
								window.document.getElementById('div_aux_cotizacion').style.display = 'block';
							}
							
					}
				}
			};
			httpPost.send(getParametros(frm));
		}
	}

	function mostrarDatosAlertaCot(id_alerta, orden_alerta) {
		var ifr = document.getElementById('ifr_alerta_cotizacion_' + id_alerta);
		if(ifr.src == 'about:blank') {
			ifr.src = "/backend/cotizaciones/alertas-bbdd.php?accion=mostrar-datos-alerta-cotizacion&id_alerta=" + id_alerta + "&orden_alerta=" + orden_alerta;
		}
		ifr.style.display = 'block';
		var bot = document.getElementById('ifr_boton_ocultar_alerta_cotizacion_' + id_alerta);
		bot.style.display = 'block';
	}

	function ocultarDatosAlertaCot(id_alerta) {
		var ifr = document.getElementById('ifr_alerta_cotizacion_' + id_alerta);
		ifr.style.display = 'none';
		var bot = document.getElementById('ifr_boton_ocultar_alerta_cotizacion_' + id_alerta);
		bot.style.display = 'none';
	}

	function guardarAlertaCotizacion(id_alerta) {
		if(typeof(id_alerta) != 'undefined') {
			var ifrm = 'ifr_alerta_cotizacion_' + id_alerta;
			var frm = parent.frames[ifrm].document.forms['frmAltaAlertaCotizacion'];
		} else {
			var frm = document.getElementById('frmAltaAlertaCotizacion');
		}

		if(frm.codigo_valor.value == "") {
			alert("No has seleccionado ningún valor del buscador");
			return;
		}
		if(frm.sel_tipo.value == 2) {
			if(frm.precio_max.value == "" && frm.precio_min.value == "") {
				alert("No has rellenado ninguna de las condiciones para configurar la alerta");
				return;
			}
		}
		if(frm.sel_tipo.value == 3) {
			if(frm.dif_max.value == "" && frm.dif_min.value == "") {
				alert("No has rellenado ninguna de las condiciones para configurar la alerta");
				return;
			}
		}


		if(checkForm(frm)) {
			frm.accion.value = 'modificar-alerta-cotizacion';
			var httpPost = createRequestObject();
			httpPost.open("post", "/backend/cotizaciones/alertas-bbdd.php", true);
			httpPost.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			httpPost.onreadystatechange = function () {
				if(httpPost.readyState == 4){
					if (httpPost.responseText) {
							var resp = httpPost.responseText;
							var capa = parent.frames[ifrm].document.getElementById('div_msg');
							if(resp.indexOf('OK#') >= 0) {
								alert("Se ha modificado correctamente la alerta");
								getAlertasCotizaciones();
							} else {
								alert("Ha ocurrido un error al intentar modificar la alerta. Por favor, inténtalo más tarde");
							}
					}
				}
			};
			httpPost.send(getParametros(frm));
		} 
	}

	function eliminarAlertaCotizacion(id_alerta) {

		var capa = document.getElementById('div_alerta_cotizacion_' + id_alerta);
		if(confirm("¿Seguro que quieres eliminar la alerta?")) {
			var http = createRequestObject();
			http.open("get", "/backend/cotizaciones/alertas-bbdd.php?accion=eliminar-alerta-cotizacion&id_alerta=" + id_alerta + "&r=" + Math.random());
			http.onreadystatechange = function () {
				if(http.readyState == 4){
					if (http.responseText) {
							var resp = http.responseText;
							if(resp == 'OK') {
								alert("La alerta se ha eliminado correctamente");
								getAlertasCotizaciones();
							} else {
								capa.innerHTML = '<strong>Ha ocurrido un error al intentar modificar la alerta. Por favor, inténtalo más tarde</strong>' + capa.innerHTML;
							}
					}
				}
			};
			http.send(null);
		}
	}

//-->
