$(document).ready(function() {
		setValorPorDefecto();
		/*
		* PopUp
		*/
		$.extend($.blockUI.defaults.overlayCSS, { backgroundColor: '#565554', cursor:"default" });	
		mensajeCargando = $('#cargando');
		 
		$(".rdBtnActividad").click(function (){	
			ocultaElementos(".pasoDos, .pasoTres, .pasoCuatro");		
			if(this.value == 1){//empresa
				$("#pasoDosEmpresa").slideDown("slow", setValorPorDefecto);
			}else if(this.value == 2){//particular
				$("#pasoDosParticular").slideDown("slow", setValorPorDefecto);
			}
		});
		//Paso dos empresa
		$(".rdBtnCliente").click(function (){	
			ocultaElementos(".pasoTres, .pasoCuatro");		
			if(this.value == 1){//empresa
				$("#pasoTresCliente").slideDown("slow", setValorPorDefecto);
			}else if(this.value == 2){//particular
				$("#pasoTresNoCliente").slideDown("slow", setValorPorDefecto);
			}
		});		
		//Paso tres empresa no cliente
		$(".rdBtnTipoEmpresa").click(function (){	
			ocultaElementos(".pasoCuatro");		
			if(this.value == 1){//empresa
				$("#pasoCuatroProf").slideDown("slow", setValorPorDefecto);
			}else if(this.value == 2){//particular
				$("#pasoCuatroOtro").slideDown("slow", setValorPorDefecto);
			}
		});	
		/*
		* Boton enviar
		*/		
		//
		$("input[@type=button]:not(#anunciar)").click(function (){	
			enviar();
		});	

		
});//ready
	
	function enviar(){
		
		if (	($("li:visible #txtMail").val() == '' && $("li:visible #txtTelefono").val() == '')	 || ($("li:visible #txtMail").val() == 'email@email.com' && $("li:visible #txtTelefono").val() == 'Teléfono')		
			|| $("li:visible #txtTelefono").val() == undefined && ($("li:visible #txtMail").val() == '' || $("li:visible #txtMail").val() == 'email@email.com')
			)	{
			alert('Por favor, indica tu email o teléfono poder contactar contigo');
			$("li:visible #txtMail").focus();
			return false;
		}		
		
		if(!validarEmail($("li:visible #txtMail").val())){
			alert('El e-mail que escribiste no es correcto');
			$("li:visible #txtMail").focus();
			return false;
		}		
		
		if ($("li:visible #txtPersona").val() == '' || $("li:visible #txtPersona").val() == 'Persona de contacto')	{
			alert('Por favor, rellena persona de contacto');
			$("li:visible #txtPersona").focus();
			return false;
		}		
		if($("li:visible #sltMotivo").val() == 0){
			alert('Debe especificar un motivo.');
			$("li:visible #sltMotivo").focus();
			return false;
		}
		
		datos =  $("li:visible input[@type=text], li:visible textarea, li:visible select, li:visible input[@type=radio], li:visible input[@type=hidden]").serialize();
		$.ajax({
			   type: "POST",
			   dataType: "json",
			   url: "/ajax/contacta/contacta.php",     // this is the path from above
			   data: datos,
			   beforeSend: function(xhr) {
				 $.blockUI(mensajeCargando);
			   },
			   success: function(data) {
			   		$.unblockUI();
					if(data.error == 0)	alert("Mensaje enviado correctamente.\n" + data.datos.msg);
					else				alert(data.error);
					if(	typeof(data.datos.url) != "undefined" &&	data.datos.url != ""){
						window.location.replace(data.datos.url);
					}
			   },
			   error: function (xhr, desc, exceptionobj) {
					$.unblockUI();	
			   }			   
		});	
		
	}
	
	function setValorPorDefecto(){
		return false;
		/*
		* Nombres defecto formulario
		*/	
		jQuery.each($("li:visible input[@type=text], li:visible textarea"), function() {
		  	//debug( this.id);
			switch(this.id){
				case "txtMail":
					$(this).attr( "value", "email@email.com" );
					$(this).focus(function () {//
						 if ($(this).attr( "value") == "email@email.com") 
							$(this).attr( "value", "" );
					}).blur(function(){
						if (typeof $(this).attr( "value") == "undefined") 
							$(this).attr( "value", "email@email.com" );
					});					
					break;
				case "txtTelefono":
					$(this).attr( "value", "Teléfono" );
					$(this).focus(function () {//
						 if ($(this).attr( "value") == "Teléfono") 
							$(this).attr( "value", "" );
					}).blur(function(){
						if (typeof $(this).attr( "value") == "undefined") 
							$(this).attr( "value", "Teléfono" );
					});						
					break;

				case "txtNomEmpresa":
					$(this).attr( "value", "Nombre empresa" );
					$(this).focus(function () {//
						 if ($(this).attr( "value") == "Nombre empresa") 
							$(this).attr( "value", "" );
					}).blur(function(){
						if (typeof $(this).attr( "value") == "undefined") 
							$(this).attr( "value", "Nombre empresa" );
					});							
					break;
				case "txtPersona":
					$(this).attr( "value", "Persona de contacto" );
					$(this).focus(function () {//
						 if ($(this).attr( "value") == "Persona de contacto") 
							$(this).attr( "value", "" );
					}).blur(function(){
						if (typeof $(this).attr( "value") == "undefined") 
							$(this).attr( "value", "Persona de contacto" );
					});					
					break;	
				case "txtComentarios":
					$(this).attr( "value", "Escribe aquí tus comentarios" );					
					$(this).focus(function () {//
						 if ($(this).attr( "value") == "Escribe aquí tus comentarios") 
							$(this).attr( "value", "" );
					}).blur(function(){
						if (typeof $(this).attr( "value") == "undefined") 
							$(this).attr( "value", "Escribe aquí tus comentarios" );
					});	
									
					break;																				
		  	}	
		});			
	}
	
	
	function ocultaElementos(elementos){
		$(elementos).slideUp("fast");
	}
	
	
	function debug(texto){		
		if(typeof(console) != 'undefined') console.info(texto);
	}	