function getexpirydate( nodays){
var UTCstring;
Today = new Date();
nomilli=Date.parse(Today);
Today.setTime(nomilli+nodays*24*60*60*1000);
UTCstring = Today.toUTCString();
return UTCstring;
}

function getcookie (name) {
  var InCookie=document.cookie; 
	var prop = name + "="; // propiedad buscada
	var plen = prop.length;
	var clen = InCookie.length;
	var i=0;
	if (clen>0) { // Cookie no vacío
		i = InCookie.indexOf(prop,0); // aparición de la propiedad
		if (i!=-1) { // propiedad encontrada
			// Buscamos el valor correspondiente
			j = InCookie.indexOf(";",i+plen);
			if(j!=-1) // valor encontrado
				return unescape(InCookie.substring(i+plen,j));
			else //el último no lleva ";"
				return unescape(InCookie.substring(i+plen,clen));
		}
		else
			return "";
	}
	else
		return "";
}


function setcookie(name,value,duration){
  cookiestring =name+"="+escape(value)+";EXPIRES="+getexpirydate(duration)+";path=/";
  document.cookie=cookiestring;
  if(!getcookie(name)){
   return false;
  }
   else{
   return true;
  }
}


document.getElementsByClassName = function(clsName){
    var retVal = new Array();
    var elements = document.getElementsByTagName("*");
    for(var i = 0;i < elements.length;i++){
        if(elements[i].className.indexOf(" ") >= 0){
            var classes = elements[i].className.split(" ");
            for(var j = 0;j < classes.length;j++){
                if(classes[j] == clsName)
                    retVal.push(elements[i]);
            }
        }
        else if(elements[i].className == clsName)
            retVal.push(elements[i]);
    }
    return retVal;
}


function img_seleccion(id) {
  if (parseInt(id)==0) return;
	
	//borrar todas
	if (id==-1) {
  	 setcookie('misimgsel','',30);
	   actualiza_bloque('divmisimgsel');
		 var marcos = document.getElementsByClassName('marco-sel');
		 if (marcos){
		    for(var i in marcos){
    	      marcos[i].className='marco';
				}		  
		 }
		 var cheq = document.getElementsByClassName('checksel');
		 if (cheq){
		    for(var i in cheq){
    	      cheq[i].checked=false;
				}		  
		 }
	   return false;
	}
	
	//leemos las cookies guardadas
	cookselec =getcookie('misimgsel');
	var imgs = cookselec.split(",");

	var borrar=0;
	//las que se borrar desmarcando del area de seleccion y desde la imagen
	if (!document.getElementById('img-'+id) || document.getElementById('img-'+id).checked==false) borrar=1; 

	if (borrar==1) {
    	for (a=0;a<imgs.length;a++) {
		  	  if (id==imgs[a]){
				       imgs[a]=0;
							 if (document.getElementById('marco-'+id)) document.getElementById('marco-'+id).className='marco';
			  	}			 
      }
			if (document.getElementById('marco-'+id)) document.getElementById('marco-'+id).className='marco';
	}else {
    	   if (imgs.length >= maxseleccion) {
	         alert('Ha llegado al límite de selección de imágenes. No se guardarán más imágenes');
		       return false; 	
			     document.getElementById('img-'+id).checked=false;
			   }
			   imgs[imgs.length]=id;
			   document.getElementById('marco-'+id).className='marco-sel';	 
	}		
	
	var valcook='';
	var buenas= new Array();
	var i=0;

	//quitamos los posibles ceros
	for (a=0;a<imgs.length;a++) {
	   if (imgs[a]>0) buenas[i++] = imgs[a];  
	}
	valcook = buenas.join(",");
	setcookie('misimgsel',valcook,30);
	actualiza_bloque('divmisimgsel');
	return false;
}



function borra_img_sel(id){
   if (parseInt(id)==0) return;
	 if (document.getElementById('img-'+id)) {
       document.getElementById('img-'+id).checked=false;
	 }		 
	 img_seleccion(id);
	 return false;
}


function actualiza_bloque(unbloque) {

   var listaimg = getcookie('misimgsel');
	 var url = '/cgi-bin/apsesp/fototeca/actualiza_seleccion.php?rnd='+Math.random()+'&listaimagenes='+listaimg+'&nada=0';
   var req = new XMLHttpRequest();
   req.open('GET', url);
	 req.onreadystatechange = function() {
      if (req.readyState == 4 && req.status == 200 && req.responseText!="") {
			  var texto = req.responseText;
		    var d1 = document.getElementById(unbloque);
				if (d1==null) alert(unbloque);
        d1.innerHTML = texto;
      }
   }
	 req.send(null);
	 return false;
}


