/*******************/
/*      JQUERY     */
/*******************/
q = jQuery.noConflict();

q(document).ready(function(){
				
	if (q('#jGrowl2').length){
		q.jGrowl.defaults.closer = false;
	
		if ( !q.browser.safari ) {
			q.jGrowl.defaults.animateOpen = {
				width: 'show'
			};
			q.jGrowl.defaults.animateClose = {
				width: 'hide'
			};
		}
		
		q.jGrowl("<h3>&iquest;Qui&eacute;n y C&oacute;mo puede comprar con Descuentos?</h3> <div class='acceso amarillo'><a href='./requisitos.php' rel='moodalbox 600 352'>ENTRAR</a></div>", { sticky: true });
	}
	
	/***
	 * Función para detectar el botón pulsado del formulario de borrado de
	 * Carnets de Socio.
	 ***/
	if (q('#Form_EliminarCarnets .elemento .boton2').length){
		q('#Form_EliminarCarnets .elemento .boton2').click(function(){
			
			var nombre = (q(this).attr('name')).split("-")[1];
			
			jConfirm('\xBFDesea continuar?', 'Di\xE1logo de Confirmaci\xF3n', function(r){ 
																		if(r){
																			q('#Form_EliminarCarnets #idElemento').val(nombre);
																			q('#Form_EliminarCarnets').submit();
																		}
			});
		});
	}
});


/*********************************/
/*        PETICIONES AJAX        */
/*********************************/
function peticionAjax(url, str, div){
	q.ajax({
		type: "POST",
		url: url,
		data: str,
		success: function(html){
			q(div).html(html);
			return true;
		} 
	});
}

/*************************************************************************/
/*           FUNCIÓN JAVASCRIPT DE CONFIMARCIÓN.                         */
/*************************************************************************/
function confirmacion(){
	jConfirm('¿Desea continuar?', 'Diálogo de Confirmación', 
				function(r) { if(r){
								document.Form_Actualizar.submit();
								document.getElementById("Eliminar").value = "Eliminar";
							}}
	);
}

/*************************************************************************/
/*           FUNCIÓN JAVASCRIPT PARA MAXIMIZAR UNA VENTANA.              */
/*************************************************************************/
function maximizar(){
	window.moveTo(0,0);
	if (document.all) {
		top.window.resizeTo(screen.availWidth,screen.availHeight);
	}
	else if (document.layers||document.getElementById) {
		if (top.window.outerHeight<screen.availHeight||top.window.outerWidth<screen.availWidth){
			top.window.outerHeight = screen.availHeight;
			top.window.outerWidth = screen.availWidth;
		}
	}
}

/****************************************************************************/
/*          FUNCIÓN JAVASCRIPT PARA VALIDAR UN FORMULARIO DE CONTACTO       */
/****************************************************************************/
function validar_Form_mail() {
	
	//valido el nombre 
	if (document.Form_contactar.nombre.value.length==0){ 
	   alert("Tiene que escribir su nombre") 
	   document.Form_contactar.nombre.focus() 
	   return 0; 
	} 

	//valido el email.
	if (document.Form_contactar.email.value.length!=0){ 
			
		var email = document.Form_contactar.email.value;
		var filtro=/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		
		if (!filtro.test( email )){
			alert("Debe Introducir un email válido \"TuCorreo@...com/.es/...\"") 
			document.Form_contactar.email.focus() 
			return 0;
		}
			
	}
	//valido el Comentario. 
	if (document.Form_contactar.comentario.value.length==0){ 
	   alert("Debe Introducir el motivo de su contacto.") 
	   document.Form_contactar.comentario.focus() 
	   return 0; 
	} 
	
	var url = "./script/mail.php";
	var str = "nombre="+document.Form_contactar.nombre.value+"&email="+document.Form_contactar.email.value+"&comentario="+document.Form_contactar.comentario.value+"&oculto="+document.Form_contactar.oculto.value+"&web_origen="+document.Form_contactar.web_origen.value;
	var div = "#mensaje";
	
	//La llamada a la función javascript+ajax.
	peticionAjax(url, str, div);
	
	q('#mensaje').css('visibility', 'visible');
}

/****************************************************************************/
/*		 OBTENCIÓN DE LOS DATOS DE UNA EMPRESA EN MODO FORMULARIO           */
/****************************************************************************/
function obtener_informacion_establecimiento(elem){
	var ide = elem.options[elem.selectedIndex].value;
	var url = "../script/funciones.php";
	var str = "method=infoEmpresa&idEstablecimiento="+ide;
	var div = "#resto_Formulario";
	peticionAjax(url, str, div);
}

function obtener_Encuesta(lista){
	if (document.getElementById('establecimiento').options[document.getElementById('establecimiento').selectedIndex].value > 0){
		if (lista.length){
			var id = lista.options[lista.selectedIndex].value;
			var idEstablecimiento = document.getElementById('establecimiento').options[document.getElementById('establecimiento').selectedIndex].value;
			var url = "../script/funciones.php";
			var str = "method=encuesta&idEstablecimiento="+idEstablecimiento+"&idEncuesta="+id;
			var div = "#resto_Formulario";
			peticionAjax(url, str, div);
		}
	}else{
		alert('Debe seleccionar una empresa antes de seguir.');
	}
}

function rellenarComboEstablecimientos(elemento){
	if(elemento != null){
		var url = "../script/funciones.php";
		var str = "method=comboEmpresas&idEncuesta="+elemento.options[elemento.selectedIndex].value;
		var div = "#divComboEmpresas";
		peticionAjax(url, str, div);
	}
}

function imprimirEncuesta(){
	var idEncuesta = q('#comboEncuestas').val();
	if (idEncuesta != null && idEncuesta > 0){
		var url = "../script/funciones.php";
		var str = "method=imprimirEncuesta&idEncuesta="+idEncuesta;
		var div = "#divEncuesta";
		peticionAjax(url, str, div);
	}	
}

/********************************
 *		 TEXTO PARPADEANTE      *
 ********************************/
var sw = 0;
        
function blinked(){        
	if (document.getElementById('blink')){
		if (sw){
	        document.getElementById("blink").style.color ="white";
	        sw = 0;
	    }
	    else{
	        document.getElementById("blink").style.color ="none";
	        sw = 1;
	    }       
	    setTimeout("blinked()", 800);
	}
}

/********************************************************************
 *		 OBTENCIÓN DE ENCUESTA PARA UN EMPRESARIO SELECCIONARO      *
 ********************************************************************/
function obtenerEncuesta(idEncuesta, idEmpresa){
		var url = "../script/funciones.php";
		var str = "method=imprimirEncuestaEmpresario&idEncuesta="+idEncuesta+"&idEmpresa="+idEmpresa;
		var div = ".empresarios";
		peticionAjax(url, str, div);
		q('.volver').css('display','block');
}

/**********************************************************/
/*       FUNCIÓN JAVASCRIPT PARA CERRAR UNA VENTANA.      */
/**********************************************************/
function cerrar(){
	window.close();
}

/******************************************************************/
/*       FUNCIÓN JAVASCRIPT PARA MOSTRAR UN CAMPO OCULTO.         */
/******************************************************************/
function mostrarCampo(id){
	var campo = id;
	if(q(campo).length > 0){
		q(campo).css('visibility','visible');
	}
}

function ocultarCampo(id){
	var campo = id;
	if(q(campo).length > 0){
		q(campo).css('visibility','hidden');
	}
}

function mostrarCamposSorteo(){

	if(q('#formulario #estado').length){
		if (q('#formulario #estado').val() == 1){
			mostrarCampo('#label_nombre_premiado');
			mostrarCampo('#text_nombre_premiado');
			mostrarCampo('#label_nombre_entrega_premio');
			mostrarCampo('#text_nombre_entrega_premio');
			mostrarCampo('#label_imagen_premiado');
			mostrarCampo('#imagen_premiado');
		}else{
			ocultarCampo('#label_nombre_premiado');
			ocultarCampo('#text_nombre_premiado');
			ocultarCampo('#label_nombre_entrega_premio');
			ocultarCampo('#text_nombre_entrega_premio');
			ocultarCampo('#label_imagen_premiado');
			ocultarCampo('#imagen_premiado');
			q('#text_nombre_premiado input').val('');
			q('#text_nombre_entrega_premio input').val('');
		}
	}
}