// JavaScript Document
var IMG_LOADING = '	<img src="images/ajax_loading.gif" />';
var envia 		= true;

$(document).ready(function(){
						   
			$(".numerico").numeric();
		
			$(".prev").click(function(e){
					if (this.id != 0){
							$(".calendar").hide();
							$("#calendar_"+this.id).show();
					}
			});
			
			$(".next").click(function(e){
					if (this.id != 0){
						$(".calendar").hide();
						$("#calendar_"+this.id).show();							  		
					}
			});
			
			screenshotPreview();
			tooltip();
			
			$(".select_dia").click(function(){
					$("#elegir_ano").toggle();			
			});
		
			$(".requiere_login").click(function(e){
					alert("Debes estar registrado !!");
					e.preventDefault();
			})
		
		
			$("#button_responder").click(function(){
				$("#responder").show();
			})
			
			$("#button_citar").click(function(){
				var info = $("#cita").text();
			})
			
			
			$(".citar_respuesta").click(function(e){
				$("#citar").slideDown();					   
				$("#id_respuesta").val(this.rel);
				var divOffset = $('#id_tema').offset();
			})
			
			
			$(".login").click(function(e){
				alert("Para utilizar este servicio debes estar registrado !!");
				e.preventDefault();
			});
			

			$(".new_post").click(function(){
					$("#new_post").show();					  
			})
			
			$(".select").change(function(){
				var valor_lista = 0 ;
					
				$(".select").each(function(){
					var value = $(this).val();
					value     = value.split(":");
					valor_lista = valor_lista + parseInt(value[1]);
				})				
					
				var valor_actual = parseInt($("#amount").val());
				var valores      = this.value.split(":");
				var valor_actual = valor_lista;
						
				var id_producto = this.id;
				$.ajax({
					type: "POST",
					url: "ajax.php",
					data: "opc=change_resolution&id_producto="+id_producto+"&id_resolucion="+valores[0],
					success: function(msg){
					}
				})
				$("#subtotal_div").html(valor_actual+"&#8364;");
				//$("#iva_div").html((valor_actual*0.16)+"&#8364;");
				$("#total_div").html(valor_actual+"&#8364;");
				$("#amount").val(valor_actual);	
				$("#valor"+this.id).html("<h1>"+valores[1]+"&euro;</h1>");
				
				bloquear();	
				
			})
			

			if ($("#staff_1")){
					$("#staff_0").show();
					$(".id_staff").eq(0).addClass('tabs_activa');
			}
		
		
		$(".id_staff").click(function(e){
				var id = this.rel;

                if (id){
					 $(".staff").hide(1);
					 $("#staff_"+id).show("slide", { direction: "up" }, 1000);
				}

        		$(".id_staff").css({"background":"url(images/background_h1_linea2.jpg) repeat-x"});
                $(".id_staff").eq(0).css({"background":"url(images/background_h1_linea.png) repeat-x"});

				$(this).css({"background":"url(images/background_h1_linea.png) repeat-x"});
				e.preventDefault();
		})

	/***************************************************************************/	
	/***************************************************************************/	
	/***************************************************************************/	

			$("#registro").submit(function(e){

				var error = true;
                 var check = $('#aceptar_politica').is(':checked');

                                    if(!check){
                                        error = false;
                                        alert("Debe aceptar la politica de privacidad para poder registrarse");
                                        $(this).focus();
                                        return false;
                                    }

					$(".obligatorio").each(function(){
							var value = $(this).val();
							
								if (!value || value ==0 ){
										$(this).css({
														"background":"#CC0000",
														"color":"#FFFFFF"
												   });
										error = false;
										$(this).focus();
										return false;
								}else{

									var alt = $(this).attr("alt");

									/* VALIDAR NIF */
									if (alt == "nif"){
										if  (value.length < 8){
												$(this).css({
														"background":"#CC0000",
														"color":"#FFFFFF"
												 });
												error = false;
												$(this).after(caja_info('El Nif/Cif es invalido.'));
												$(this).focus();
												return false;
											}
									}

									var p = $("#pass").val();
									var p2 = $("#pass2").val();

									if (p && p2){
											if (p!=p2){
												$("#pass , #pass2").css({
														"background":"#CC0000",
														"color":"#FFFFFF"
												 });
												error = false;
												$("#pass2").after(caja_info('Las contrase&ntilde;as no coinciden.'));
												$("#pass2").focus();
												return false;
											}
									}





									$(this).css({
												"background":"#FFFFFF",
												"color":"#000"
								   });
									caja_info('');
								}
						})

					
					$(".valid_mail").each(function(){
							var value = $(this).val();
							if (isEmailAddress(value)==false){
								$(this).css({
											"background":"#CC0000",
											"color":"#FFFFFF"
										   });
								error = false;
								$(this).focus();
							}
					})

					/*
					if (error == false){
						alert("Todos los campos resaltados son obligatorios !!");
					}
					*/

					return error;
			})

            $("#registro_mod").submit(function(e){

				var error = true;
                 

					$(".obligatorio").each(function(){
							var value = $(this).val();

								if (!value || value ==0 ){
										$(this).css({
														"background":"#CC0000",
														"color":"#FFFFFF"
												   });
										error = false;
										$(this).focus();
										return false;
								}else{

									var alt = $(this).attr("alt");

									/* VALIDAR NIF */
									if (alt == "nif"){
										if  (value.length < 8){
												$(this).css({
														"background":"#CC0000",
														"color":"#FFFFFF"
												 });
												error = false;
												$(this).after(caja_info('El Nif/Cif es invalido.'));
												$(this).focus();
												return false;
											}
									}

									var p = $("#pass").val();
									var p2 = $("#pass2").val();

									if (p && p2){
											if (p!=p2){
												$("#pass , #pass2").css({
														"background":"#CC0000",
														"color":"#FFFFFF"
												 });
												error = false;
												$("#pass2").after(caja_info('Las contrase&ntilde;as no coinciden.'));
												$("#pass2").focus();
												return false;
											}
									}





									$(this).css({
												"background":"#FFFFFF",
												"color":"#000"
								   });
									caja_info('');
								}
						})


					$(".valid_mail").each(function(){
							var value = $(this).val();
							if (isEmailAddress(value)==false){
								$(this).css({
											"background":"#CC0000",
											"color":"#FFFFFF"
										   });
								error = false;
								$(this).focus();
							}
					})

					/*
					if (error == false){
						alert("Todos los campos resaltados son obligatorios !!");
					}
					*/

					return error;
			})

	/***************************************************************************/
	/***************************************************************************/
	/***************************************************************************/

	});

	change_Trimestre = function(id){
			var capa = $("#capa_circuito");
			$.ajax({
				beforeSend : function (){
					capa.html(IMG_LOADING);
					$("#circuito").remove();
					$("#tanda").remove();
				},			   
				type: "POST",
				url: "ajax.php",
				data: "opc=change_Trimestre&id="+id,
				success: function(msg){
					capa.html(msg);
				}
		  });				
	}
	
	change_Tanda = function(id){
			var capa = $("#capa_Tanda");
			$.ajax({
				beforeSend : function (){
					capa.html(IMG_LOADING);
				},			   
				type: "POST",
				url: "ajax.php",
				data: "opc=change_Tanda&id="+id,
				success: function(msg){
					capa.html(msg);
				}
		  });				
	}	
	
	
	function caja_info(info){
		$(".informacion").remove();
		return '<div class="informacion">'+info+'</div>';
	}

	function validar_login(){
			var user   = $("#user_login").val();
			var pass   = $("#pass_login").val();
			
			if (!user){
						alert("Falta el campo Usuario");
						$("#user").focus();
						envia = false;
						return false;
					}else
						envia = true;

		   if (!pass){
						alert("Falta el campo Password");
						$("#pass").focus();
						envia = false;
						return false;
					}else
						envia = true;
		return envia;	
			
	}

	
	function meapunto(actividad){
		if(actividad!=0){
			$.ajax({
				type: "POST",
				url: "ajax.php",
				data: "opc=apuntarme&id="+actividad,
				success: function(msg){
					alert("Listo, se ha apuntado a esta actividad.");
				}
		  });
		}else{
			alert("Ups! Para apuntarte a una actividad debes estar registrado.");
		}
	}


	function validar_register(){
						
						var nom   		= $("#nombre").val();
						var apellidos   = $("#apellidos").val();
						var mail  		= $("#mail").val();
						var pass		= $("#pass").val();
						
						if (!nom){
							alert("Falta el campo nombre");
							$("#nombre").focus();
							envia = false;
							return false;
						}else
							envia = true;
						
						if (!apellidos){
							alert("Falta el campo apellidos");
							$("#apellidos").focus();
							envia = false;
							return false;
						}else
							envia = true;
						
						if ((!mail) || (!isEmailAddress(mail))){
							alert("Falta el campo email o es in correcto");
							$("#mail").focus();
							envia = false;
							return false;
						}else
							envia = true;
							
						if (!pass){
							alert("Falta el campo password");
							$("#pass").focus();
							envia = false;
							return false;
						}else
							envia = true;
						
					return envia;
	}





function isEmailAddress(correo){
		var s = correo;
		var filter=/^[A-Za-z._][A-Za-z0-9._]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/;
		if (s.length == 0 ) return true;
		if (filter.test(s))
		return true;
			else
		return false;
}



