var x;
x = $(document);
x.ready(inicializarEventos);
function inicializarEventos() {
	var x;
	x = $(".PuntosMapa");
	x.hover(function() {
		x = $(this);
		nombreToltip = "Toltip" + x.attr("id");
		toltip = $("#" + nombreToltip);
		toltip.css("display", "block");
	}, function() {
		x = $(this);
		nombreToltip = "Toltip" + x.attr("id");
		toltip = $("#" + nombreToltip);
		toltip.css("display", "none");
	});
	/**
	 * validacion rut suscribe bolet�n
	 */
	txtRut = $('#txtRut');
	txtRut.keyup(function() {
		sRut = $(this).attr('value');
		sRutFormateado = '';
		sRut = quitaFormato(sRut);
		sDV = sRut.substring(sRut.length - 1);
		sRut = sRut.substring(0, sRut.length - 1);
		while (sRut.length > 3) {
			sRutFormateado = "." + sRut.substr(sRut.length - 3)
					+ sRutFormateado;
			sRut = sRut.substring(0, sRut.length - 3);
		}
		sRutFormateado = sRut + sRutFormateado;
		if (sRutFormateado != '') {
			sRutFormateado = sRutFormateado + "-";
		}
		sRutFormateado = sRutFormateado + sDV;
		if ($(this).attr('value') != sRutFormateado) {
			$(this).attr('value', sRutFormateado);
		}
	});
	/**
	 * * validacion e-mail formulario suscribe bolet�n *
	 * $("form").validate(function()
	 */

	$("form[name^='FormularioInscripcion']").submit(
			function() {
				var error = "";
				if ($("input[name^='sNombre']").attr("value") == "")
					error+="> Debes introducir tus nombres.\n";
				
				
				if ($("input[name^='sApePat']").attr("value") == "")
					error+="> Debes introducir tu apellido paterno.\n";
					
				if ($("input[name^='sApeMat']").attr("value") == "")
					error+="> Debes introducir tu apellido materno.\n";
				
				if ($("input[name^='sEmail']").attr("value") == "")
					error+="> Debes introducir tu e-mail.\n";
				else
					if (validarEmail($("#txtMail").attr("value")) == false)
						error+="> El e-mail ingresado es incorrecto.\n";
				
				if(error.length > 0){
					alert(error);
					return false;
				}
					
				/*if ($("input[name^='txtNombres']").attr("value") == ""
						|| $("input[name^='txtApellidos']").attr("value") == ""
						|| $("input[name^='txtRut']").attr("value") == ""
						|| $("input[name^='txtPais']").attr("value") == "") {
					alert("Ingrese todos los campos solicitados");
					return false;
				}
				if (validarEmail($("#txtMail").attr("value")) == false) {
					alert("el email es incorrecto!.");
					return false;
				}*/
				$.ajax( {
					type : "POST",
					url : "/lib/clases/formant/index2.php",
					data : $(this).serialize(),
					success : function(html) {	
						if(html=="¡Felicidades! \n Ahora recibirás mensualmente nuestras novedades en tu correo electrónico!")
						{	
							//Limpiar campos				
							$("input[name^='sNombre']").attr("value","");
							$("input[name^='sApePat']").attr("value","");
							$("input[name^='sApeMat']").attr("value","");
							$("#txtMail").attr("value","");
							//Cerrar modal
							//$('#Conteido').dialog('close');
							$("#msg_ok").html("<strong>¡Felicidades! Ahora recibirás mensualmente nuestras novedades en tu correo electrónico!</strong>");
						}
						else
						{
							//Mensaje
							alert(html);
							$("#msg_ok").html("");
						}
						/**
						 * if (html.indexOf("Ahora recibir")){ alert("Ahora
						 * recibirás mensualmente nuestras novedades en tui
						 * correo electrónico"); }
						 */
					}
				});
				return false;
			});

	$('#FlechaSelect')
			.click(
					function() {

						var inputIdExposicion = $('#inputIdExposicion').val();
						$
								.ajax( {
									type : "post",
									url : "/arte-y-tecnologia/educacion-a-traves-del-arte/recursos-didacticos/listarNiveles.php",
									data : "inputIdExposicion="
											+ inputIdExposicion,
									beforeSend : function(html) {
									},
									success : function(html) {

										$('#listaNiveles').html(html);

										$('#opciones a').click(
												function() {
													curso = $(this).attr('title');
													nivel = $(this).attr('nivel');
													$('#ListaCursos input').attr('value',curso);
													$('#inputIdCurso').attr('value', nivel);
												});

										$('#opciones').click(guardarOpciones);
										$('#opciones').mouseleave(guardarOpciones);
										$('#opciones').css('display', 'block');
									}
								});

					});
	$('#MenuLetras a')
			.click(
					function(e) {
						$('#opciones_exposiciones').hide();
						$
								.ajax( {
									type : "post",
									url : "/arte-y-tecnologia/educacion-a-traves-del-arte/recursos-didacticos/listarExposiciones.php",
									data : "inputLetra="
											+ $(this).attr('letra'),
									beforeSend : function(html) {
									},
									success : function(html) {
										$('#opciones_exposiciones').show();
										/**
										 * $('#opciones_exposiciones').css("top", *
										 * e.pageY - 330); *
										 * $('#opciones_exposiciones').css("left", *
										 * e.pageX - 250);
										 */
										var p = $('#MenuLetras'); /*
																	 * var
																	 * position =
																	 * p.position();
																	 */
										var position = p.offset();
										/**
										 * * alert("e.pageX:"+e.pageX); *
										 * alert("position.left:"+position.left);
										 */
										$('#opciones_exposiciones').css("top",
												+110);
										$('#opciones_exposiciones')
												.css(
														"left",
														e.pageX
																- (position.left - 215));
										$('#listaExposiciones').html(html);
									}
								});
					});
	$('#submenuE a').click(
			function(e) {
				$('#opciones_exposiciones').hide();
				$.ajax( {
					type : "post",
					url : "/arte-y-tecnologia/exposiciones/listarMuestra.php",
					data : "inputYear=" + $(this).attr('year'),
					beforeSend : function(html) {
					},
					success : function(html) {
						/**
						 * XXX $('#opciones_exposiciones').show(); *
						 * $('#opciones_exposiciones').css("top", e.pageY - *
						 * 330); $('#opciones_exposiciones').css("left", e.pageX - *
						 * 250); $('#listaExposiciones').html(html);
						 */
						$('#opciones_exposiciones').show();
						var p = $('#submenuE'); /* var position = p.position(); */
						var position = p.offset();					
						
						$('#opciones_exposiciones').css("top", position.top - 450);
						$('#opciones_exposiciones').css("left",
								e.pageX - (position.left - 230));
						$('#listaExposiciones').html(html);
					}
				});
			});
	function checkKey(e) {
		$('#opciones_exposiciones').hide();
		$
				.ajax( {
					type : "post",
					url : "/arte-y-tecnologia/educacion-a-traves-del-arte/recursos-didacticos/listarExposiciones.php",
					data : "inputLetra=" + $('#inputBuscador').val(),
					beforeSend : function(html) {
					},
					success : function(html) {
						$('#opciones_exposiciones').show();
						/**
						 * $('#opciones_exposiciones').css("top", "492px"); *
						 * $('#opciones_exposiciones').css("left", "495px");
						 */
						$('#opciones_exposiciones').css("top", 163);
						$('#opciones_exposiciones').css("left", 235);
						$('#listaExposiciones').html(html);
					}
				});
	}
	$('#inputBuscador').click(function() {
		$('#inputBuscador').val('');
		$('#inputIdExposicion').val('');
	});
	$('#inputtxtCursos').click(function() {
		$('#inputtxtCursos').val('');
		$('#inputIdCurso').val('');
	});

	$('.linkExpo').click(function() {
		alert($(this).text);
	});
	if ($.browser.mozilla) {
		$('#inputBuscador').keyup(checkKey);
	} else {
		$('#inputBuscador').keyup(checkKey);
	}

}
function quitaFormato(Nro) {
	var strNro = new String(Nro);
	while (strNro.indexOf(".") != -1)
		strNro = strNro.replace(".", "");
	strNro = strNro.replace("-", "");
	return strNro;
}
function guardarOpciones() {
	$('#opciones').css('display', 'none');
}
/**
 * * funcion con expresiones regulares que valida que un email sea correcto *
 * devuelve un alert
 */
function validarEmail(valor) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(valor)) {
		return (true);
	} else {
		return (false);
	}
}

function validar(e) {
    tecla = (document.all) ? e.keyCode : e.which;
    if (tecla==8) return true;
    patron =/[A-Za-zÑñÁÉÍÓÚáéíóú\s]/; 
    te = String.fromCharCode(tecla);
    return patron.test(te);
} 

