﻿var testo_preferiti="I miei alloggi preferiti ([0])"
function AddPreferiti(Nome,Valore,strIdLink,strNome,strLinkDettaglio,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;
	if(strLinkDettaglio!="")
	    AggiornaPreferiti(Nome,strIdLink,strNome,strLinkDettaglio,Valore)
	else
	    alert(aggiuntoPreferiti)
	//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
		if(((strCookies==Nome)||(strCookies==""))||(strCookies.indexOf(Nome+";")>-1)||(strCookies.indexOf(Nome+"=;")>-1))
		    return 0
		else
		    return strCookies.substring(tmp1,tmp2).split("%2C").length
	}
	else{
		return 0
	}
}
function AggiornaPreferiti(Nome,strIdLink,strNome,strLinkDettaglio,Valore){
	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")
		if(idLnkPreferiti=="")
		    return;
		document.getElementById(idLnkPreferiti).innerHTML=testo_preferiti.toString().replace("[0]",NumeroPreferiti(Nome))
		if(NumeroPreferiti(Nome)>0)
		    document.getElementById(idLnkPreferiti).href=pathUrlPreferiti
		else
		    document.getElementById(idLnkPreferiti).href=""
		if(strNome){
		    var objLink = document.createElement("a");
		    //objLink.setAttribute('href',pathLnkPreferiti+strLinkDettaglio);
		    objLink.setAttribute('href',strLinkDettaglio);
		    objLink.setAttribute('alt',strNome);

		    var objImg = document.createElement("img");
		    objImg.setAttribute('src',pathImgPreferiti+"R"+Valore+"-Ap.jpg");
		    //objImg.setAttribute('onerror','alert(1);this.src=\''+pathImgPreferiti+'NoImgasa.png\';');
		    objImg.setAttribute('onerror','fotoMancante();');

            objLink.appendChild(objImg)
            
		    document.getElementById("preferiti").appendChild(objLink);		
        }
        else{
            document.getElementById("preferiti").innerHTML="";
        }
    }
}
function PresenteInPreferiti(cookie,valore){
	cookie=unescape(cookie)
	cookie=","+cookie.substring(cookie.indexOf("=")+1)+","
	return (cookie.indexOf(","+valore+",")>-1)
}

function InviaPreferiti(){
    var w = 400;
    var h = 250;
    var l = Math.floor((screen.width-w)/2);
    var t = Math.floor((screen.height-h)/2);
    var winInvia=window.open("invia_preferiti"+lingua_codice_path+".aspx?id="+unescape(LeggiPreferiti("ListaPreferiti").replace('ListaPreferiti=','')),"Preferiti","width="+w+",height="+h+",top="+t+",left="+l)
}