// JavaScript Document
//***************************************************************
// PREPARAR PARA GRABAR COOKIE
//***************************************************************
function busAntes(){
	var name = '_busca';
	var antes = getCookie(name);
	var enlaces = "";
	if(antes==null){		
		document.getElementById('listaBus').innerHTML = "";
		document.getElementById('divSusBus').style.display = "none";		
	}else{		
		document.getElementById('divSusBus').style.display = "block";
		var buscadas = "";
		var v_antes = antes.split("|");	
		var long = v_antes.length;
		for (i=0;i<long;i++){
			var valores = v_antes[i];
			var v_val = valores.split("_");
			if(v_val[2]=='1936'){var desde = ""; }
			else{var desde = "; desde: " + v_val[2]; }
			var texto = v_val[0] + ': ' + v_val[1] + desde;
			var enlace = '<a href="#" onClick="limpiar();buscar(\''+v_val[0]+'\',\''+addslashes(v_val[1])+'\',\''+v_val[2]+'\')" title="titulo">' + texto + '</a>';	
			enlaces+= enlace + '';
		}	
		document.getElementById('listaBus').innerHTML = enlaces;
	}
}

function preparaCookie(campo,dato,desde){	
	var name = '_busca';	
	if(!campo){
		alert("\nERROR al identificar el campo \nFunción preparaCookie(): cookies.js\n");
		var error = 0;//si
		return error;
	}else{var error = 1;}//no 
	//armar link buscar(campo,dato,desde);
	var lista = document.getElementById('listaBus').innerHTML;	
	if(desde=='1936'){var de = "";}
	else{var de="; desde: " + desde; }	
	var texto = campo + ': ' + dato + de;
	var enlace = '<a href="error.php" onClick="limpiar();buscar(\''+campo+'\',\''+addslashes(dato)+'\',\''+desde+'\'); return false; " title="titulo">' + texto + '</a>';
	document.getElementById('divSusBus').style.display = "block";
	document.getElementById('listaBus').innerHTML = enlace + '' + lista;
	//Ver si hay valores guardados en la cookie
	var antes = getCookie(name);//alert(antes);
	
	if(antes==null){
		var valor = campo + "_" + dato + "_" + desde;
	}else{
		var valor = campo + "_" + dato + "_" + desde + "|" + antes;
	}//alert(valor); return;
	
	//Grabar valor en cookie _busca
	var expires=null;
	//now.setTime(now.getTime() + 1000 * 60 * 60 * 24 * 365)
	//var son = 1000 * 60 * 60 * 24 * 365 ;//en un año
	//son = parseInt(son);	//alert(son); //return;	
	//var expires = new Date();
	//expires.setTime(expires.getTime() + son); //alert(expires); return;	
	
	var path=null;
	var domain=null;
	var secure=null;
	
	setCookie(name, valor, expires, path, domain, secure); 
	return error;
	
}


// RECUPERAR
// name - nombre de la cookie deseada
// devuelve un string conteniendo el valor de la cookie especificada o null si la cookie no existe

function getCookie(name){
  var cname = name + "=";               
  var dc = document.cookie;             
  if (dc.length > 0) {              
    begin = dc.indexOf(cname);       
    if (begin != -1) {           
      begin += cname.length;       
      end = dc.indexOf(";", begin);
      if (end == -1) end = dc.length;
        return unescape(dc.substring(begin, end));
    } 
  }
  return null;
}



// COLOCAR
// name - nombre de la cookie
// value - valor de la cookie
// [expires] - fecha de caducidad de la cookie (por defecto, el final de la sesión)
// [path] - camino para el cual la cookie es válida (por defecto, el camino del documento que hace la llamada)
// [domain] - dominio para el cual la cookie es válida (por defecto, el dominio del documento que hace la llamada)
// [secure] - valor booleano que indica si la trasnmisión de la cookie requiere una transmisión segura
// al especificar el valor null, el argumento tomará su valor por defecto

function setCookie(name, value, expires, path, domain, secure) {
  document.cookie = name + "=" + escape(value) + 
  ((expires == null) ? "" : "; expires=" + expires.toGMTString()) +
  ((path == null) ? "" : "; path=" + path) +
  ((domain == null) ? "" : "; domain=" + domain) +
  ((secure == null) ? "" : "; secure");  
}


// BORRAR 
// name - nombre de la cookie
// [path] - camino de la cookie (debe ser el mismo camino que el especificado al crear la cookie)
// [domain] - dominio de la cookie (debe ser el mismo dominio que el especificado al crear la cookie)
// se considera el camino y dominio por defecto si se especifica null o no se proporcionan argumentos

//delCookie('_busca',null,null);

function delCookie(name,path,domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
    ((path == null) ? "" : "; path=" + path) +
    ((domain == null) ? "" : "; domain=" + domain) +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

/* This code is Copyright (c) 1996 Nick Heinle and Athenia Associates, 
 * all rights reserved. In order to receive the right to license this 
 * code for use on your site the original code must be copied from the
 * Web site webreference.com/javascript/. License is granted to user to 
 * reuse this code on their own Web site if and only if this entire copyright
 * notice is included. Code written by Nick Heinle of webreference.com.
 */
/*
function getCookie (name) {
var dcookie = document.cookie; 
var cname = name + "=";
var clen = dcookie.length;
var cbegin = 0;
        while (cbegin < clen) {
        var vbegin = cbegin + cname.length;
                if (dcookie.substring(cbegin, vbegin) == cname) { 
                var vend = dcookie.indexOf (";", vbegin);
                        if (vend == -1) vend = clen;
                return unescape(dcookie.substring(vbegin, vend));
                }
        cbegin = dcookie.indexOf(" ", cbegin) + 1;
                if (cbegin == 0) break;
        }
return null;
}

function setCookie (name, value, expires) {
        if (!expires) expires = new Date();
document.cookie = name + "=" + escape (value) + 
"; expires=" + expires.toGMTString() +  "; path=/";
}

function delCookie (name) {
var expireNow = new Date();
document.cookie = name + "=" +
"; expires=Thu, 01-Jan-70 00:00:01 GMT" +  "; path=/";
}
*/


/*
function setCookieArray(name){
		this.length = setCookieArray.arguments.length - 1;
        for (var i = 0; i < this.length; i++) {
        this[i + 1] = setCookieArray.arguments[i + 1]
        setCookie (name + i, this[i + 1], expdate);
        }        
}


function getCookieArray(name){
		var i = 0;
        while (getCookie(name + i) != null) {
			
        	this[i + 1] = getCookie(name + i);
			alert(getCookie(name + i));
        	i++; this.length = i; 
			
        }
}


var expdate = new Date();
expdate.setTime (expdate.getTime() +  (24 * 60 * 60 * 1000 * 365)); 

var testArray = new setCookieArray("_jtotw", "my", "data", "is", "very", "important");

var testArray = new getCookieArray("_jtotw");


//set to expire on December 1, 2011 at 11:59:59
var expiration = new Date();
expiration.setTime(1322794799151);
expiration = "";
//set this cookie
//setCookie('bus_hechas');
//get this cookie
//getCookie('bus_hechas');
//delete this cookie
delCookie('bus_hechas');
*/


// ***** Cookies ***************************************************************

// ***** setCookie *****

/*

PARAMETERS:

name, value  - cookie name & value
expires      - cookie expiration date (defaults to end of current session)
path, domain - cookie path & domain (defaults to document's path & domain)
secure       - cookie sent only over secure connection

example CALLS:

setCookie("counter", "1");
setCookie("counter", "1", Date("January 01, 2010 00:00:01"));
setCookie("counter", "1", undefined, '/');



function setCookie(name, value, expires, path, domain, secure)
{
  document.cookie =
    name+"="+escape(value)+
    (expires ? "; expires="+expires.toGMTString() : "")+
    (path    ? "; path="   +path   : "")+
    (domain  ? "; domain=" +domain : "")+
    (secure  ? "; secure" : "");
}



// ***** setCookieLT *****

// PARAMETERS: lifetime - cookie lifetime in seconds

function setCookieLT(name, value, lifetime, path, domain, secure)
{
  if (lifetime) lifetime = new Date(Date.parse(new Date())+lifetime*1000);
  setCookie(name, value, lifetime, path, domain, secure);
}

// ***** getCookie *****

function getCookie(name)
{
  var cookie, offset, end;
  cookie  = " "+document.cookie;
  offset  = cookie.indexOf(" "+name+"=");
  if (offset == -1) return undefined;
  offset += name.length+2;
  end     = cookie.indexOf(";", offset)
  if (end    == -1) end = cookie.length;
  return unescape(cookie.substring(offset, end));
}

// ***** delCookie *****

// PARAMETERS:
//
// name         - cookie name
// path, domain - cookie path & domain (the same as those used to create cookie)

function delCookie(name, path, domain)
{
  if (getCookie(name))
    setCookie(name, "", new Date("January 01, 2000 00:00:01"), path, domain);
}

// ***** Examples **************************************************************

// ***** Set & Delete *****

// The cookie is set and then deleted.
//example1();
function example1()
{
  setCookie("example", "Delete cookie example!");
  alert(getCookie("example"));
  delCookie("example");
  alert(getCookie("example"));
}

// ***** Lifetime *****

// The cookie should expire between the first and the second alerts.
//example2();
function example2()
{
  setCookieLT("example", "Cookie lifetime example!", 1);
  alert(getCookie("example"));
  alert(getCookie("example"));
}

// ***** Delete & Visibility *****

/*

For this example the document should not be in the root directory.
The first cookie is set in the root directory.
The second cookie is set in the current directory and it hides the first one.
However, when the second cookie is deleted the first one becomes visible again.


//example3();
function example3()
{
  setCookie("example", "Cookie visibility example - 1!", undefined, '/');
  alert(getCookie("example"));
  setCookie("example", "Cookie visibility example - 2!");
  alert(getCookie("example"));
  delCookie("example");
  alert(getCookie("example"));
  delCookie("example", '/');
}

*/
