function detectarCookie(){
var cookieEnabled=(navigator.cookieEnabled)? true : false

//if not IE4+ nor NS6+
if (typeof navigator.cookieEnabled=="undefined" && !cookieEnabled){ 
document.cookie="testcookie"
cookieEnabled=(document.cookie.indexOf("testcookie")!=-1)? true : false
}

 if (!cookieEnabled){ 
    //document.getElementById("cookie_si").style.display="none";
    return false;
 }
 else{
   // document.getElementById("cookie_no").style.display="none";
   // document.getElementById("cookie_si").style.display="block";
    return true;
   }
}

function esFecha(fecha){
//calculo la fecha que recibo 
    //La descompongo en un array 
    var array_fecha = fecha.split("/") 
    //si el array no tiene tres partes, la fecha es incorrecta 
    if (array_fecha.length!=3) 
       return false 

    //compruebo que los ano, mes, dia son correctos 
    var ano 
    ano = parseInt(array_fecha[2],10); 
    if (isNaN(ano)) 
       return false 

    var mes 
    mes = parseInt(array_fecha[1],10); 
    if (isNaN(mes)) 
       return false 

    var dia 
    dia = parseInt(array_fecha[0],10); 
    if (isNaN(dia)) 
       return false 

    //si el año de la fecha que recibo solo tiene 2 cifras hay que cambiarlo a 4 
    if ((ano<1)||(ano>9999)) {return false;} 
    if ((mes<1)||(mes>12)) {return false;}
    if ((dia<1)||(dia>31)) {return false;}
	return true;

}

function abrirPopup(url, ancho, alto){
	window.open(url,"Ventana","width="+ancho+",height="+alto+",left=0,top=0,scrollbars=yes,resizable=no");	
}

function abrirPopup2(url, ancho, alto){
	window.open(url,"Ventana","width="+ancho+",height="+alto+",left=0,top=0,scrollbars=yes,resizable=yes");	
}

function si_enter_buscar_portada(e){
	if(document.all)
		tecla=e.keyCode;
	else
	{
		tecla=e.which;
	}
	if(tecla==13)
		{
			buscar_portada();
		}
}

//funcion que inicia a vacio todos los campos de un formulario
function resetear(which){
	for (i=0;i<which.length;i++){
		var tempobj=which.elements[i];
		if((tempobj.type!="button")&&(tempobj.type!="submit")&&(tempobj.type!="select-one")&&(tempobj.type!="hidden")){			
			tempobj.value="";
		}
		if(tempobj.type=="select-one"){	
			tempobj.selectedIndex =0;
		}
	}
	
}


//////////////////////////////////////////////////AJAX//////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////
<!-- Parte de AJAX -->
function nuevoAjax()
{ 
	/* Crea el objeto AJAX. Esta funcion es generica para cualquier utilidad de este tipo, por
	lo que se puede copiar tal como esta aqui */
	var xmlhttp=false; 
	try 
	{ 
		// Creacion del objeto AJAX para navegadores no IE
		xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); 
	}
	catch(e)
	{ 
		try
		{ 
			// Creacion del objet AJAX para IE 
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
		} 
		catch(E) { xmlhttp=false; }
	}
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') { xmlhttp=new XMLHttpRequest(); } 

	return xmlhttp; 
}

//////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////// COMPROBAR ENCUESTA PORTADA /////////////////////////////////

function comprobarEncuestaPortada(element_id){
	var element = document.getElementById(element_id);
        var ajax=nuevoAjax();
	var resultado="";
        url="modulos/encuestaportada.php";
        cpost="";
        if(!detectarCookie()) cpost="vista=resultados";
        else cpost="vista=formulario";       
	
        //////////////////////
        ajax.open("POST", url, true);
        ajax.onreadystatechange = function() {
                if (ajax.readyState == 4) {
                        resultado=ajax.responseText;
                        //alert(resultado);
                        element.innerHTML = resultado;
                }
        }
        ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        ajax.send(cpost);
        ///////////////////////
}

//////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////// VOTAR ENCUESTA PORTADA //////////////////////////////////

function votarEncuestaPortada(encuesta_id, element_id, form_id){
	var element = document.getElementById(element_id);
        var formulario = document.getElementById(form_id);        
        var ajax=nuevoAjax();
	var resultado="";
        url="modulos/procesarvoto.php";
        var cadenaFormulario = "encuesta_id="+encuesta_id;       
       
        var nopciones=0;
        for (var i=0; i <= formulario.elements.length-1;i++) {
            if(formulario.elements[i].checked){
                cadenaFormulario += "&"+formulario.elements[i].name+'='+formulario.elements[i].value;
                nopciones++;
            }            
        }
        if(nopciones>0){
            //////////////////////
            ajax.open("POST", url, true);
            ajax.onreadystatechange = function() {
                    if(ajax.readyState == 1) {
                            pintarVentanaProcesando();
                    }
                    
                    if (ajax.readyState == 4) {
                            // se deja de mostrar el procesando *******
                            dormir(1);
                            $('#ventanaprocesando').remove();
                            $('#ventanaprocesando_divfondo').remove();
                      
                            resultado=ajax.responseText;
                            //alert(resultado);
                            if(resultado!='OK') alert('ERROR al relizar la votación. Intentelo más tarde'); 
                            comprobarEncuestaPortada(element_id);
                    }
            }
            ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            ajax.send(cadenaFormulario);
            ///////////////////////
        }else{
            alert("Para votar en la encuesta debe selecionar una respuesta.");
        }
}

//////////////////////////////////////////////////////////////////////////////////////////////////


function muestra_capa(capa) {
   document.getElementById(capa).style.display="block";
}

function oculta_capa(capa) {
   document.getElementById(capa).style.display="none";
}


function dormir(naptime) {
	naptime = naptime * 1000;
	var sleeping = true;
	var now = new Date();
	var alarm;
	var startingMSeconds = now.getTime();

	while(sleeping) {
		alarm = new Date();
		alarmMSeconds = alarm.getTime();
		if(alarmMSeconds - startingMSeconds > naptime){ sleeping = false; }
	}      
}



////////////////////////////////////VENTANAS///////////////////////////////////////
function getPaginaScroll() {

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}

function getTamanioPag() {
	
	var xScroll, yScroll;
	
	if( window.innerHeight && window.scrollMaxY ) {
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if( document.body.scrollHeight > document.body.offsetHeight ) { // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if(self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if(document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if(document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight) 
	return arrayPageSize;
}

function pintarVentanaProcesando() {
	
	nombreVentana='ventanaprocesando';
	w=350;
	
	// ***********************************************************************
	// se obtiene la información de la ventana del navegador
	var arrayPageSize = getTamanioPag();
	var arrayPageScroll = getPaginaScroll();
	var posTop = arrayPageScroll[1] + (arrayPageSize[3] / 50) + 150;
	
	// ***********************************************************************
	// se crea la capa de fondo
	divfondo = document.createElement('div');
	divfondo.setAttribute('id', nombreVentana+'_divfondo');
	// ***********************************************************************
	// se crea el contenedor principal "ventana"
	ventana = document.createElement('div');
	ventana.setAttribute('id', nombreVentana);
	// ***********************************************************************
	// se añade el fondo y la nueva "ventana" al cuerpo de la página
	var body = document.getElementsByTagName('body').item(0);
	body.appendChild(divfondo);
	body.appendChild(ventana);
	// ***********************************************************************
	
	// ***********************************************************************
	// se aplican estilos a la capa "divfondo"
	$("#"+nombreVentana+'_divfondo').css({
		position:'absolute',
		zIndex:'90',
		top:'0',
		left:'0',
		width:'100%',
		height:'100%',
		opacity:'0.5',
		filter:'alpha(opacity=50)',
                background:'#CCC',
		width:arrayPageSize[0]+'px',
		height:arrayPageSize[1]+'px'
	});	

	// ***********************************************************************
	// se aplican estilos a la capa "ventana"...
	//Calculo la posicion de la ventana
	var izq=((arrayPageSize[0]+10)-w)/2;
	
	$("#"+nombreVentana).css({
		position:'absolute',
		zIndex:'100',
		top:posTop+'px',
		left:izq+'px',
		width:w+'px',                
		overflow:'hidden',
		background:'#FFF',
		border:'2px solid #09A4E1',
		color:'#333',
		textAlign:'center',
		padding:'50px 0'
	});
	
	$("#"+nombreVentana).html("<img src=\"img/lightbox-ico-loading.gif\" id=\"imgprocesando\" alt=\"procesando\" /> &nbsp; procesando...");
	
	// se lleva el foco a la ventana para evitar que al pulsar enter sigan lanzándose ventanas desde el enlace
	document.getElementById('imgprocesando').focus();
}

function limpiar_imput(sB)
{
		sB.value = '';
}




