
function setCookie(c_name,value,expiredays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+";expires="+exdate.toGMTString()+"; path=/";
}

function getCookie(c_name) {
if (document.cookie.length>0) {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1) {
    c_start=c_start + c_name.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    }
  }
return "";
}

function toggle_div(div_id) {
	theDiv = document.getElementById(div_id);
	if (theDiv.style.display == 'block') {
		theDiv.style.display = 'none';
	} else {
		theDiv.style.display = 'block';
	}
}


function add_favorites(id) {
	newFav = parseInt(id);
	newFavorites = new Array();
	if (!isNaN(newFav)) {
		sFavorites = getCookie('MerryGoRound_fav');
		aFavorites = sFavorites.split(",");
		for (i=0; i < aFavorites.length; i++) {
			if (parseInt(aFavorites[i]) == newFav) {
				return;
			} else if (!(isNaN(parseInt(aFavorites[i])))) {
				newFavorites.push(aFavorites[i]);
			}
		}
		newFavorites.push(newFav);
		sFavorites = newFavorites.join(",");
		setCookie('MerryGoRound_fav',sFavorites,30);
		alert(LABEL_ADDED_FAV);
		divFav = document.getElementById('preferiti-'+id);
		divFav.innerHTML = "<a class=\"button\" href=\"javascript:remove_favorites("+id+")\" style=\"background-image: url(/img/annulla.gif);\">"+LABEL_RIMUOVI_PREFERITI+"</a>";
		divFav2 = document.getElementById('preferiti2-'+id);
		if (divFav2 != null) {
			divFav2.innerHTML = "<a class=\"button\" href=\"javascript:remove_favorites("+id+")\" style=\"background-image: url(/img/annulla.gif);\">"+LABEL_RIMUOVI_PREFERITI+"</a>";
		}
		divFavTitle = document.getElementById('display-fav-'+id);
		if (divFavTitle != null) {
			divFavTitle.style.display = 'block';
		}
	}
}


function remove_favorites(id) {
	if (confirm(LABEL_CONFIRM_FAV_REMOVE)) {
		remFav = parseInt(id);
		newFavorites = new Array();
		if (!isNaN(remFav)) {
			sFavorites = getCookie('MerryGoRound_fav');
			aFavorites = sFavorites.split(",");
			for (i=0; i < aFavorites.length; i++) {
				if (parseInt(aFavorites[i]) != remFav) {
					newFavorites.push(aFavorites[i]);
				}
			}
			sFavorites = newFavorites.join(",");
			setCookie('MerryGoRound_fav',sFavorites,30);
			alert(LABEL_REMOVED_FAV);
			divFav = document.getElementById('preferiti-'+id);
			divFav.innerHTML = "<a class=\"button\" href=\"javascript:add_favorites("+id+")\">"+LABEL_AGGIUNGI_PREFERITI+"</a>";
			divFav2 = document.getElementById('preferiti2-'+id);
			if (divFav2 != null) {
				divFav2.innerHTML = "<a class=\"button\" href=\"javascript:add_favorites("+id+")\">"+LABEL_AGGIUNGI_PREFERITI+"</a>";
			}
			
			divFavTitle = document.getElementById('display-fav-'+id);
			if (divFavTitle != null) {
				divFavTitle.style.display = 'none';
			}
			if (window.location.href == 'http://www.merrygoround.org' + LABEL_LINGUA + LABEL_PREFERITI + '.p6.html') {
				window.location.reload();
			}
		}
	}
}
