var ID_TIPO_MEDIO_OTROS = "6";
var redirect = "";

jQuery.noConflict();

jQuery(document).ready(function($){


	$("input[type='text'], textarea").each(function(){
		$(this).attr("params", $(this).val());
	})

	$("input[type='text'], textarea").focus(function(){
													 
		if($(this).attr("id") == "clave_dummy"){
			$(this).hide();
			$("#clave").show();
			$("#clave").focus();
		} else if($(this).attr("id") == "clave2_dummy"){
			$(this).hide();
			$("#clave2").show();
			$("#clave2").focus();
		} else {
	
			if($(this).attr("params") == $(this).val())
				$(this).val("");
			else
				this.select();
		}
	})
	
	
	$("input[type='text'], input[type='password'], textarea").blur(function(){
		if($(this).attr("id") == "clave" && $(this).val() == ""){
			$(this).hide();
			$("#clave_dummy").show();
		} else if($(this).attr("id") == "clave2" && $(this).val() == ""){
			$(this).hide();
			$("#clave2_dummy").show();
		} else {	
			if($(this).val() == "")
				$(this).val($(this).attr("params"));
		}
	})	
	
	
	$("select[name='medio1_tipo_id']").change(function(){
		if($(this).val() == ID_TIPO_MEDIO_PRENSA){
			$("select[name='grupo_id']").show();
			$("li.triple.terminos.notificaciones").hide();
		} else {
			$("select[name='grupo_id']").hide();
			$("li.triple.terminos.notificaciones").show();
		}
	});
	
	$("#mensaje").hide();
	$("#error_acepto").hide();
	$("#error_fecha").hide();

	$("#clave").hide();
	$("#clave2").hide();
	
	$("#gracias").hide();
	
	
	//recibir novedades
	
	$("input[name='recibir_novedades_todos']").click(function(){
															  
		if($(this).is(":checked")){
			$("input[name='recibir_novedades_familia']").attr("checked", "");
			$("input[name='recibir_novedades_terceros']").attr("checked", "");
			$("input[name='recibir_novedades_individuales']").attr("checked", "");
			$("input[name='recibir_alertas']").attr("checked", "");
			$("input[name='no_recibir_novedades']").attr("checked", "");
		}
	})
	
	$("input[name='recibir_novedades_familia']").click(deseleccionarTodos);
	$("input[name='recibir_novedades_terceros']").click(deseleccionarTodos);
	$("input[name='recibir_novedades_individuales']").click(deseleccionarTodos);
	$("input[name='recibir_alertas']").click(deseleccionarTodos);
	$("input[name='no_recibir_novedades']").click(function(){
		if($(this).is(":checked"))	{
			$("input[name='recibir_novedades_todos']").attr("checked", "");	
			$("input[name='recibir_novedades_familia']").attr("checked", "");
			$("input[name='recibir_novedades_terceros']").attr("checked", "");
			$("input[name='recibir_novedades_individuales']").attr("checked", "");			
			$("input[name='recibir_alertas']").attr("checked", "");
		}
													   
	});
	
	
	 var antesDeEnviar = function(){
		 $("#mensaje").html(IDIOMA_AGUARDE);
		 $("#mensaje").show();
		 $("button#enviar").attr("disabled", "disabled");
		 

	 }
	 
	 var despuesDeEnviar = function(resp){

		var mensaje = "";
		 
		

		$(".validar").each(function(){

			limpiarHolders($(this).attr("name"));							 
		});		 
		/*	
		$(".validar_2").each(function(){
			limpiarHolders($(this).attr("name"));							 
		});
		*/

		 
		respuesta = $.evalJSON(resp);
		 
		if(respuesta.ok){
			
			
			//$("#mensaje").css("color", "#000");
		 	//$("#mensaje").html("Registración completada");
			
			$("form#form_registro").hide();
			
			//$(".content").css("height", "400px")
			
			redirect = unescape($.query.get("redirect"));
			
			if(redirect == "")
				$("#gracias").show();												   
			else
				location.href = redirect;
			
			

		} else{
			
			errores = respuesta.errores;
			for(i = 0; i < errores.length; i++){
				prepararHolders(errores[i].campo);
				mostrarError("NO", errores[i].campo, encode_utf8(errores[i].error));
			}
			
			if(!respuesta.error_acepto.ok)
				$("#error_acepto").show();
			else
				$("#error_acepto").hide();
				
			if(!respuesta.error_fecha.ok)
				$("#error_fecha").show();
			else
				$("#error_fecha").hide();				
	
	
			if(!respuesta.error_terminos_adicionales.ok && $("#terminos_adicionales").is(":visible")) 
				$("#error_terminos_adicionales").show();
			else 
				$("#error_terminos_adicionales").hide();								
				
			if(errores.length == 0 && respuesta.error_fecha.ok && respuesta.error_acepto.ok) {
				$("#terminos_adicionales").slideDown();
				$("#mensaje").html("");
			} else  {
				$("#terminos_adicionales").hide();
				$("#mensaje").html(IDIOMA_COMPLETE);
			}
				

								
				
			
			
		}
			 


		 $("button#enviar").attr("disabled", "");
		 
	 }		
	
	$('#form_registro').ajaxForm({beforeSubmit: antesDeEnviar,  success: despuesDeEnviar}); 
	
	prepararHolders();
});

function prepararHolders(campo){
	//campo_derecha = campoDerecha(campo, $("input[name='"+campo+"']"));
	campo_derecha = jQuery("input[name='"+campo+"'], select[name='"+campo+"'], textarea[name='"+campo+"']");
	jQuery(campo_derecha).siblings("span").remove();
	jQuery(campo_derecha).after("<br /><span id=\""+campo+"_error\" style=\"color: #ff0000; margin-left: 0; margin-top: 4px; height: 40px\"></span>");
	//$(campo_derecha).after("<span id=\""+campo+"_indicador\" style=\"display:inline\; padding-top:4px;\"><img style=\"float:none; position:absolute; top:"+bordeImg(campo)+"px; margin-left:4px; border:none\" src=\"images/registro/proc.gif\" border=\"0\"></span>");							   		
}

function limpiarHolders(campo){
	var c = jQuery("input[name='"+campo+"'], select[name='"+campo+"'], textarea[name='"+campo+"']");



	jQuery(c).siblings("span").remove();
	jQuery(c).siblings("br").remove();
}


function mostrarError(valido, campo, descripcion){
	if(valido == "OK"){
		jQuery("#"+campo+"_error").html("");
		//$("#"+campo+"_indicador").html("<img style=\"float:none; position:absolute; top:"+bordeImg(campo)+"px; margin-left:4px; border:none\" src=\"images/registro/ok.gif\" border=\"0\">");
	} else {
		jQuery("#"+campo+"_error").html(decode_utf8(descripcion));
		
		//$("#"+campo+"_indicador").html("<img style=\"float:none; position:absolute; top:"+bordeImg(campo)+"px; margin-left:4px; border:none\" src=\"images/registro/error.gif\" border=\"0\">");					
	}		
	
}

var deseleccionarTodos = function(){
	if(jQuery(this).is(":checked"))	{
		jQuery("input[name='recibir_novedades_todos']").attr("checked", "");	
		jQuery("input[name='no_recibir_novedades']").attr("checked", "");	
	}
}


