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

function setCookie(NameOfCookie, value, expiredays) {
var ExpireDate = new Date ();
ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));

//  document.cookie = NameOfCookie + "=" + escape(value) + 
  document.cookie = NameOfCookie + "=" + value + 
  ((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString());
}

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

function clearFavoritos(){ delCookie("favoritos"); }
function setFavoritos(f){
  if( f == null || f == "" )
    delCookie("favoritos");
  else
    setCookie("favoritos", f, 10000); 
}
function getFavoritoStr(){ return getCookie("favoritos");}
function getFavoritoArray(){ 
  var s = getFavoritoStr();
  if( s == null || s == "" ) return null;
  return s.split(",");
}

function existeFavorito(idi)
{
  var af = getFavoritoArray();
  if( af == null ) return false;
  for(i=0; i < af.length; ++i)
    if( af[i] == idi ) return true;
  return false;
}

function addFavorito(idi)
{
  if( !existeFavorito(idi) ){
    var f = getFavoritoStr();
    f = ( f == null )? idi: f+","+idi;
    setFavoritos(f);
	return true;
  }
  return false;
}

function delFavorito(idi)
{
try
{
  var af = getFavoritoArray();
  if( af != null ){
	var f = "";
	var i = 0;
    for(; i < af.length; ++i)
	{
      if( af[i] != idi )
	  {
        f = af[i];
		break;
	  }
	}
    for(++i; i < af.length; ++i)
	{
      if( af[i] != idi )
        f += ","+af[i];
	}
	setFavoritos(f);
	return true;
  }
}catch(e)
{
	alert("error en delFaborito");
}
  return false;
}
