﻿var testo_preferiti="I miei alloggi preferiti ([0])"
function AddPreferiti(Nome,Valore,strIdLink,reload){
	var vecchioValore=LeggiPreferiti(Nome)
	var strCookie=""
	if(vecchioValore.indexOf("=")>-1)
		if(!PresenteInPreferiti(vecchioValore,Valore))        
			strCookie = vecchioValore + escape("," +Valore);
		else{
			/*if(window.confirm(presentePreferiti)){
			    DelPreferiti(Nome,Valore)			 
        	    document.location=document.location
        	}*/
        	alert(aggiuntoPreferiti)
			return
		}		
	else
		strCookie = Nome + "=" + escape(Valore);
		strCookie=strCookie.replace(Nome+"=%2C",Nome+"=")
	var Scadenza=new Date()
	Scadenza.setMonth(Scadenza.getMonth()+1); // fra un mese
	strCookie += "; expires=" + Scadenza.toGMTString();
	document.cookie = strCookie;
	AggiornaPreferiti(Nome,strIdLink)
	//if(reload)
	    alert(aggiuntoPreferiti)
}
function SvuotaPreferiti(){
	document.cookie="ListaPreferiti=";
	AggiornaPreferiti("ListaPreferiti")
}
function DelPreferiti(Nome,Valore){
	var vecchioValore=LeggiPreferiti(Nome)	
	var strCookie=""
	if(vecchioValore.indexOf("=")>-1)
		if(PresenteInPreferiti(vecchioValore,Valore)){
			var cookie=unescape(vecchioValore).toString()
			var array=cookie.split(",")
			for(var i=0;i<array.length;i++){
			    if(array[i].replace(Nome+"=","")!=Valore)
			        strCookie+="%2C"+array[i].replace(Nome+"=","")
			}
		}
		else{
			//alert("non presente")
			return
		}
	else
		strCookie=""
	//strCookie = Nome + "=" + escape(Valore);
	if(strCookie!="")
	    strCookie=strCookie.substring(3,strCookie.length)
	var Scadenza=new Date()
	Scadenza.setMonth(Scadenza.getMonth()+1); // fra un mese
	strCookie=Nome+"="+strCookie
	strCookie += "; expires=" + Scadenza.toGMTString();
	document.cookie = strCookie;
    AggiornaPreferiti(Nome)
	//document.location=document.location
	//AggiornaPreferiti(Nome,"")
}

function LeggiPreferiti(Nome){
	var strCookies=document.cookie
	if(strCookies.indexOf(Nome)>-1){
		var tmp1=strCookies.indexOf(Nome)
		var tmp2=strCookies.indexOf(";",tmp1)       
		if(tmp2==-1)
			tmp2=strCookies.length
		return strCookies.substring(tmp1,tmp2)
	}
	else{
		return ""
	}
}
function NumeroPreferiti(Nome){	
	var strCookies=document.cookie
	if(strCookies.indexOf(Nome)>-1){
		var tmp1=strCookies.indexOf(Nome)
		var tmp2=strCookies.indexOf(";",tmp1)       
		if(tmp2==-1)
			tmp2=strCookies.length
		var strCookiesCut=strCookies.substring(tmp1,tmp2)
		if((strCookies==Nome)||(strCookies==Nome+"=")||(strCookies=="")||(strCookiesCut==Nome)||(strCookiesCut==Nome+"=")||(strCookiesCut==""))
		    return 0
		else
		    return strCookies.substring(tmp1,tmp2).split("%2C").length
	}
	else{
		return 0
	}
}
function AggiornaPreferiti(Nome,strIdLink){
	var strCookies=document.cookie
	if(strCookies.indexOf(Nome)==-1){
		alert(errorePreferiti)
	}
	else{
		//inserire qui il codice sul link dei preferiti
		//document.getElementById("ctl00_Menu_dx1_lnkPreferiti").innerHTML=("I miei alloggi preferiti ([0])").toString().replace("[0]","1")
		document.getElementById("ctl00_lnkPreferiti").innerHTML=testo_preferiti.toString().replace("[0]",NumeroPreferiti(Nome))
    }
}
function PresenteInPreferiti(cookie,valore){
	cookie=unescape(cookie)
	cookie=","+cookie.substring(cookie.indexOf("=")+1)+","
	return (cookie.indexOf(","+valore+",")>-1)
}

function preferiti_open(){
    elenco_strutture_box_open();
}