var activar = new Object() ;

function favOn(id, txt, path) {
    favObj = document.getElementById("favorito"+id) ;
    favObj.src = path+"imagen/i_favorito.gif" ;
    favObj.title = txt ;

}

function favOff(id, txt, path) {
	favObj = document.getElementById("favorito"+id) ;
    favObj.src = path+"imagen/i_favorito_off.gif" ;
    favObj.title = txt ;
    
}

function changeFavIcon(id, txt_sub, txt_add, path) {
    if (activar["key_"+id] == undefined) activar["key_"+id] = false ;
    activar["key_"+id] = !activar["key_"+id] ;

    if (activar["key_"+id]) {
        favOn(id, txt_sub, path) ;
        meteEnCookie(id) ;

        //sumamos 1 al total
        add2total() ;

    } else {
        favOff(id, txt_add, path) ;
        sacaDeCookie(id) ;

        //sumamos 1 al total
        sub2total() ;
    }
}

/* FUNCIONES PARA COOKIE */
function sacaDeCookie(id) {
    //sacamos de la cookie
    strFav = readCookie("fav");
    finder = ","+id+"," ;
    pos = strFav.indexOf(finder);
    fav = strFav.substr(0, (pos+1)) + strFav.substr((pos+finder.length), strFav.length) ;
    if (fav == ",") deleteCookie("fav") ;
    else saveCookie("fav", fav, 365) ;
}

function meteEnCookie(id) {
    //guardamos en la cookie
    strFav = readCookie("fav");
    if (strFav != null) strFav += id+"," ;
    else strFav = ","+id+"," ;
    saveCookie("fav", strFav, 365) ;
}
/* FIN DE FUNCIONES PARA COOKIE */

function add2total() {
    total = document.getElementById("total_lista") ;
    if (total) 
    {
        value = parseInt(total.innerHTML)+1 ;
        total.innerHTML = value ;
    }
}

function sub2total() {
    total = document.getElementById("total_lista") ;
    if (total) 
    {
        value = parseInt(total.innerHTML)-1 ;
        total.innerHTML = value ;
    }
}

function loadtotal(value) {
    total = document.getElementById("total_lista") ;
    if (total) 
    {
        total.innerHTML = value ;
    }
}

function load_if_fav(id, texto_sub, path) {
    strFav = readCookie("fav") ;
    if (strFav.indexOf(","+id+",") != -1)
    {
        activar["key_"+id] = true ;
        favOn(id, texto_sub, path) ;
    }
}

function initOnLoad(texto_sub, path) {
    strFav = readCookie("fav") ;
    if (strFav != null)
    {
        aFav = strFav.split(",");
        for (var id in aFav)
	    {
            if (document.getElementById("favorito"+aFav[id])) load_if_fav(aFav[id], texto_sub, path) ;
        }
        loadtotal(aFav.length-2) ;
    }
}

function vacia_mi_lista(formulario) {
    if (formulario == "") 
    {
        deleteCookie("fav") ;
    } else {
        for (i = 0 ; i < formulario.elements.length ; i++)
        {   
		    if (formulario.elements[i].type == "checkbox") {
                if (formulario.elements[i].checked && formulario.elements[i].value != -1) {
                    sacaDeCookie(formulario.elements[i].value) ;
                }
            }
	    }
    }
}
