var activar = new Object() ;

function favOn(id, txt) {
    favObj = document.getElementById("favorito"+id) ;
    favObj.src = path+"web/i_favorito.gif" ;
    favObj.title = txt ;

    //también hemos de cambiar el texto
    favTextObj = document.getElementById("favoritoText"+id) ;
    favTextObj.innerHTML = txt ;
    favTextObj.title = txt ;
}

function favOff(propiedad_id, txt) {
	favObj = document.getElementById("favorito"+id) ;
    favObj.src = path+"web/i_favorito_off.gif" ;
    favObj.title = txt ;
    
    //también hemos de cambiar el texto
    favTextObj = document.getElementById("favoritoText"+id) ;
    favTextObj.innerHTML = txt ;
    favTextObj.title = txt ;
}

function changeFavIcon(propiedad_id, txt_sub, txt_add) {
    if (activar["key_"+propiedad_id] == undefined) activar["key_"+id] = false ;
    activar["key_"+propiedad_id] = !activar["key_"+propiedad_id] ;

    if (activar["key_"+propiedad_id]) {
        favOn(propiedad_id, txt_sub) ;
        meteEnCookie(propiedad_id) ;

        //sumamos 1 al total
        add2total() ;

    } else {
        favOff(propiedad_id, txt_add) ;
        sacaDeCookie(propiedad_id) ;

        //sumamos 1 al total
        sub2total() ;
    }
}

/*
function favOn(id) {
    favObj = document.getElementById("favorito"+id) ;
    favObj.src = "/images/web/i_favorito.gif" ;
    favObj.title = "Sacar de mi lista" ;
    
    //también hemos de cambiar el texto
    favTextObj = document.getElementById("favoritoText"+id) ;
    favTextObj.innerHTML = "Sacar de mi lista" ;
    favTextObj.title = "Sacar de mi lista" ;
}

function favOff(id) {
	favObj = document.getElementById("favorito"+id) ;
    favObj.src = "/images/web/i_favorito_off.gif" ;
    favObj.title = "Añadir a mi lista" ;
    
    //también hemos de cambiar el texto
    favTextObj = document.getElementById("favoritoText"+id) ;
    favTextObj.innerHTML = "Añadir a mi lista" ;
    favTextObj.title = "Añadir a mi lista" ;
}

function changeFavIcon(id) {
    if (activar["key_"+id] == undefined) activar["key_"+id] = false ;
    activar["key_"+id] = !activar["key_"+id] ;
    
    if (activar["key_"+id]) {
        favOn(id) ;
        meteEnCookie(id) ;
        
        //sumamos 1 al total
        add2total() ;
        
    } else {
        favOff(id) ;
        sacaDeCookie(id) ;
        
        //sumamos 1 al total
        sub2total() ;
    }
}
*/

/* FUNCIONES PARA COOKIE */
function sacaDeCookie(propiedad_id) {
    //sacamos de la cookie
    strFav = readCookie("fav");
    finder = ","+propiedad_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(propiedad_id) {
    //guardamos en la cookie
    strFav = readCookie("fav");
    if (strFav != null) strFav += propiedad_id+"," ;
    else strFav = ","+propiedad_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(propiedad_id, texto_sub) {
    strFav = readCookie("fav") ;
    if (strFav.indexOf(","+propiedad_id+",") != -1)
    {
        activar["key_"+propiedad_id] = true ;
        favOn(propiedad_id, texto_sub) ;
    }
}

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) ;
                }
            }
	    }
    }
}

