function check_boxes(form, field, state) {
    for (i=0; i<form.length; i++){
		 if (form.elements[i].name == field)
		     form.elements[i].checked = state;
    }
}

function popupCartina(url){
  window.open(url,"popupcartina","location=no,width=600,height=400");
}

function popupfoto(imgURL){
  window.open(imgURL,"popupfoto","location=no,width=700,height=600");
}

function textCounter(field, countfield, maxlimit) {
if (field.value.length > maxlimit)
	field.value = field.value.substring(0, maxlimit);
else
	 countfield.value = maxlimit - field.value.length;
}

function centerThumb(img, width, height){
	if (img.width)
			imgwidth = img.width;
	else
			imgwidth=width;
	if (img.height)
			imgheight=img.height;
	else
			imgheight=height;
	moveleft = Math.round((width - imgwidth) / 2);
	if (moveleft>0)
			img.style.marginLeft= moveleft + "px";
	movetop = Math.round((height - imgheight) / 2);
	if (movetop>0)
			img.style.marginTop= movetop + "px";
}
function show(tagId){
	document.getElementById(tagId).style.display='block';
}
function hide(tagId){
	document.getElementById(tagId).style.display='none';
}

function toggleVisible(tagId){
	visibilityStatus = document.getElementById(tagId).style.display;
	if (visibilityStatus=='none')
		document.getElementById(tagId).style.display='block';
	else
		document.getElementById(tagId).style.display='none';
}

function switchText(node, tagId, showText, hideText){
	visibilityStatus = document.getElementById(tagId).style.display;
	if (visibilityStatus=='none')
		node.innerHTML = showText;
	else
		node.innerHTML = hideText;
	
}

function switchTextProv(node, tagId, showText, hideText,dim,tagDim){
	visibilityStatus = document.getElementById(tagId).style.display;
	if (visibilityStatus=='none'){
		node.innerHTML = showText;
		document.getElementById(tagDim).style.marginTop = dim;	
	}
	else{
		node.innerHTML = hideText;
		document.getElementById(tagDim).style.marginTop = '';	
	}	
	
}



function addToFavorites(url, title) {
	var sURL = window.location;
	var sText = title;

	try
	{	if (window.sidebar)
			window.sidebar.addPanel(sText, sURL, "");
		else if (window.external)
			window.external.AddFavorite(sURL, sText);
		else if (window.opera && window.print)
		{	var oA = document.createElement('a');
			oA.setAttribute('rel','sidebar');
			oA.setAttribute('href', sURL);
			oA.setAttribute('title', sText);
			oA.click();
		}
	}
	catch (e)
	{	
	}
}

function setAsHome(url){
	if (window.external){ //IE
		document.body.style.behavior='url(#default#homepage)';
		document.body.setHomePage(url);
	}
		else alert("Spiacenti, il tuo browser non supporta questa funzione");
}

function clickCounter(counterID){
	counterImg = new Image;
	counterImg.src='/img2/blank.gif?counter=' + counterID + '&amp;ts=' + (new Date()).getTime();
}

function validateFormContatti(formObj){
	formObj.nome.value=formObj.nome.value.replace(/obbligatorio/, '');
	formObj.nome.value=formObj.nome.value.replace(/^\s+|\s+$/, '');
	formObj.telefono.value=formObj.telefono.value.replace(/obbligatorio tel. o email/, '');
	formObj.telefono.value=formObj.telefono.value.replace(/^\s+|\s+$/, '');
	formObj.email.value=formObj.email.value.replace(/obbligatorio tel. o email/, '');
	formObj.email.value=formObj.email.value.replace(/^\s+|\s+$/, '');
	errore ="";
	if (formObj.nome.value=="")
		errore = errore + " - e' obbligatorio indicare il nome\n";
	if (formObj.email.value=="" && formObj.telefono.value=="")
		errore = errore + " - e' obbligatorio indicare almeno uno tra email e telefono\n";
	if (formObj.email.value!="" && !/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(formObj.email.value))
		errore = errore + " - indirizzo email non valido\n";
	if (formObj.telefono.value!="" && !/^\d+$/.test(formObj.telefono.value))
		errore = errore + " - il telefono deve contenere solo caratteri numerici\n";

	if (errore!=""){
		errore = "Errori nei dati:\n" + errore;
		alert(errore);
		return false;
	}else{
		//if (formObj.commento.value=="scrivi qui la tua richiesta" || formObj.commento.value=="")
		//	formObj.commento.value="vorrei maggiori informazioni sull'immobile";
		return true;
	}
}
var objOmbra;
var objImage;
var objImageOver;
var objImg;

function fotoplus(indice,imageid,op){
	objOmbra = window.document.getElementById('popupOmbra_'+indice);
	objImage = window.document.getElementById('popupImage_'+indice);
	objImageOver = window.document.getElementById('popupImageOverlay_'+indice);
	objImg = window.document.getElementById('img_'+indice);
	if (op=='none'){
		objImage.style.display='none';
		objOmbra.style.display='none';
		objImageOver.style.display='none';
		objImg.style.visibility='hidden';
	}else{
		objImage.style.display='block';
		objOmbra.style.display='block';
		objImageOver.style.display='block';
		
		objImg.onload=initImage;
		objImg.src = "http://mediacasa.quotidiano.net/image/" + imageid + "/print.jpg";
	}
}

function initImage() {
	objImg.style.marginLeft = Math.round((600-objImg.width)/2) + "px";
	objImg.style.marginTop = Math.round((440-objImg.height)/2) + "px";
	setOpacity(0);
	objImg.style.visibility='visible';
	fadeIn(0);
}

function setOpacity(opacity) {
  opacity = (opacity == 100)?99.999:opacity;
  // IE/Win
  objImg.style.filter = "alpha(opacity:"+opacity+")";
  
  // Safari<1.2, Konqueror
  objImg.style.KHTMLOpacity = opacity/100;
  
  // Older Mozilla and Firefox
  objImg.style.MozOpacity = opacity/100;
  
  // Safari 1.2, newer Firefox and Mozilla, CSS3
  objImg.style.opacity = opacity/100;
}

function fadeIn(opacity) {
    if (opacity <= 100) {
      setOpacity(opacity);
      opacity += 10;
      window.setTimeout("fadeIn("+opacity+")", 50);
    }
}

