function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}


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('ITDream_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('ITDream_fav',sFavorites,30);
		alert(LABEL_ADDED_FAV);
		divFav = document.getElementById('preferiti-'+id);
		divFav.innerHTML = "<a 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 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('ITDream_fav');
			aFavorites = sFavorites.split(",");
			for (i=0; i < aFavorites.length; i++) {
				if (parseInt(aFavorites[i]) != remFav) {
					newFavorites.push(aFavorites[i]);
				}
			}
			sFavorites = newFavorites.join(",");
			setCookie('ITDream_fav',sFavorites,30);
			alert(LABEL_REMOVED_FAV);
			divFav = document.getElementById('preferiti-'+id);
			divFav.innerHTML = "<a href=\"javascript:add_favorites("+id+")\">"+LABEL_AGGIUNGI_PREFERITI+"</a>";
			divFav2 = document.getElementById('preferiti2-'+id);
			if (divFav2 != null) {
				divFav2.innerHTML = "<a 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.italydreamvillas.com' + LABEL_LINGUA + LABEL_PREFERITI + '.p6.html') {
				window.location.reload();
			}
		}
	}
}


function tool_email() {

	toolBar = document.getElementById('toolbar');
	XY = findPos(toolBar);

	posx = XY[0] - 281;
	posy = XY[1] + 57;

	_toolbox_email = document.getElementById('toolbox_email');
	_toolbox_email.style.width = '416px';
	_toolbox_email.style.display = 'block';
	_toolbox_email.style.position = 'absolute';
	_toolbox_email.style.left = posx + 'px';
	_toolbox_email.style.top = posy + 'px';
	

}

function tool_email_send(form) {
	if (!checkEmail(form.sender.value)) {
		alert(LABEL_ERROR_EMAIL_FORMAT);
		form.sender.focus();
		return;
	}
	if (!checkEmail(form.recipient.value)) {
		alert(LABEL_ERROR_EMAIL_FORMAT_RECIPIENT);
		form.recipient.focus();
		return;
	}
	
	params = 'name=' + encodeURIComponent(form.name.value);
	params += '&sender=' + encodeURIComponent(form.sender.value);
	params += '&recipient=' + encodeURIComponent(form.recipient.value);
	params += '&message=' + encodeURIComponent(form.message.value);
	params += '&url=' + encodeURIComponent(form.url.value);
	params += '&lingua=' + encodeURIComponent(form.lingua.value);

	AjaxRequest('/include/sendafriend.php','toolbox_email','POST',params);
			
}

function tool_email_cancel() {
	_toolbax_email = document.getElementById('toolbox_email');
	_toolbax_email.style.display = 'none';
}


function tool_print(lingua,url) {
	urlparts = url.split('/');
	staticurl = urlparts[urlparts.length-1];
	
	winurl = '/detail/print.php?lang=' + lingua + '&page=' + encodeURIComponent(staticurl);
	
	winPrint = window.open(winurl,'WindowPrint', 'width=830,height=500,scrollbars=yes,statusbar=no,toolbar=no');
	winPrint.focus();

}


function tool_pdf(lingua,url) {
	urlparts = url.split('/');
	staticurl = urlparts[urlparts.length-1];
	
	winurl = '/detail/pdf.php?lang=' + lingua + '&page=' + encodeURIComponent(staticurl);
	
	winPDF = window.open('','WindowPDF', 'width=830,height=500,scrollbars=yes,statusbar=no,toolbar=no');
	
	winPDF.document.open();
	winPDF.document.writeln('<html><head><title>Loading...</title></head>');
	winPDF.document.writeln('<body bgcolor="#DBC488" style="background-color: #DBC488"><center><br><br><br><img src="/img/loader.gif" alt="Loading..."/></center></body>');
	winPDF.document.writeln('<html>');
	winPDF.document.close();
	winPDF.focus();
	
	setTimeout('winPDF.location.href = winurl',2000);
	
}
