function setCookie(name) {
  var value = "";
  if (getCookie(name)) { 
    var misConsultas = getCookie(name);
    var consultasSeparadas = misConsultas.split("&");
    var location = "";
    var actualLocation = getURL();
    var existe = false;
    for (loop = 0; loop < consultasSeparadas.length; loop++) {
      var consulta = consultasSeparadas[loop];
      var amp = consulta.indexOf(":"); 
      location = consulta.substring(amp+1);
      if (location == actualLocation) {
        loop = consultasSeparadas.length;
        existe = true;
        alert("La página se añadió exitosamente a sus favoritos.");
      }
    }

    if (!existe) {
    //Aqui modificamos el numero de ligas que queremos aparezcan en la sección de Favoritos
      if (consultasSeparadas.length == 10) {
        var elementoIndex = misConsultas.indexOf("&");
        var elementosRestantes = misConsultas.substring(elementoIndex+1);
        value = elementosRestantes;
      } else {
        value = getCookie(name);
      }

      value = value + "&" + getValue(); 
      var now = new Date();
      now.setTime(now.getTime() + 1000 * 60 * 60 * 24 * 365);
      var curCookie = name + "=" + escape(value) + ";expires=" + now + ";path=/";
      //((domain) ? "; domain=" + domain : "") +
      //((secure) ? "; secure" : "");
      document.cookie = curCookie;
      if (!getCookie(name)) { alert('Your browser did not accept the cookie.'); }
    }
  } else { 
    value = getValue();
    var now = new Date();
    now.setTime(now.getTime() + 1000 * 60 * 60 * 24 * 365);
    var curCookie = name + "=" + escape(value) + ";expires=" + now + ";path=/";
    //((domain) ? "; domain=" + domain : "") +
    //((secure) ? "; secure" : "");
    document.cookie = curCookie;
    if (!getCookie(name)) { alert('Your browser did not accept the cookie.'); }
  }
}


function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 5;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
    //return unescape(dc.substring(begin + prefix.length, end));
    return unescape(dc.substring(dc.indexOf(prefix) + prefix.length, end));
}


function deleteCookie (name) {
  var exp  = new Date();
  exp.setTime (exp.getTime() - 1);  
  var cval = getCookie (name);
  if (cval == "*")
     window.location.replace("delete_notfound.htm");
  else {   
     //document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
     document.cookie = name + "=" + cval + "; path=/" + "; expires=" + exp.toGMTString(); 
  }
}


function getURL() { 
  var url = document.URL;
  return (url);
}

function getTitle() { 
  var title = document.title;
  return (title);
}

function getValue() { 
  var value = getTitle() + ":" + getURL();
  return (value);
}


function readTheCookie() {
  if (getCookie('asuntos')){
    var misConsultas = getCookie('asuntos');
    var consultasSeparadas = misConsultas.split("&");
    var titulo = "";
    var location = "";
    var elementos = consultasSeparadas.length;
    for (loop=elementos; loop>0;loop--) {
      var consulta = consultasSeparadas[loop-1];
      var amp = consulta.indexOf(":"); 
      titulo = consulta.substring(0,amp);
      location = consulta.substring(amp+1);
      document.write("<div style='border-left:1px solid #CCCCCC; border-right:1px solid #CCCCCC;'><table width='100%' border='0' cellspacing='0' cellpadding='0'><tr>");
      document.write("<td width='5'> </td>");
      document.write("<td>&#8226; ");
      document.write("<a href='"+location+"' class='favlink1'>"+titulo+"</a></td></tr>");			
      document.write("</table></div>");
    }

    cTexto = "<div align='right' class='favlink2'><img src='images/tache.png' width='12' height='12' /><a href='javascript:location.reload();' onClick='";
    cTexto += "deleteCookie(";
    cTexto +='"asuntos");';
    cTexto +="'>Borrar Mis favoritos</a></div>";
    //alert (cTexto);
    document.write(cTexto);
  }
}
