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 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://media.eurekasa.it/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);
    }
}

function dynWriteFooter(sId,sUrl){
	oFooter = document.getElementById(sId);
	if (!oFooter || !sUrl)
		return;

	if (sUrl == "http://casa_local.msn.it/")
		var smallCobrand = "";
	else
		var smallCobrand = "di avere visionato e ";

	copyrightFooterYear = new Date;
	oFooter.innerHTML="Gruppo Immobiliare.it P.IVA 08435221000 <a onclick=\"datiSoc('http://www.immobiliare.it/dati_societari.html','450','280');\">dati societari</a>. Accedendo al servizio, l'utente dichiara "+smallCobrand+" di accettare le <a href='"+sUrl+"casa/informazioni/politica-di-privacy.php' class='link_blu underline'>regole sulla privacy</a>.";
}

function datiSoc(file,w,h) {
	var l = Math.floor((screen.width-w)/2);
	var t = Math.floor((screen.height-h)/2);
	window.open(file,"","width=" + w + ",height=" + h + ",top=" + t + ",left=" + l +",");
}

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

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

function resetHeader(tip) {
	
	var i;
	for (i=1;i<5;i++) {
		if ( document.getElementById('idCategoria'+i)){
			document.getElementById('idCategoria'+i).style.textDecoration='none';
			document.getElementById('idCategoria'+i).style.fontColor='';
		}	
		if ( tip != "" ) {
			if ( document.getElementById('idCategoria'+i))
				document.getElementById('idCategoria'+i).href="/";
		}
	}
	
}

function setHeader(categoria) {
	
	idCat = 0;
	switch(categoria){
		case "residenziale": idCat=1; break;
		case "commerciale": idCat=2; break;
		case "turistico": idCat=3; break;
		case "stanze": idCat=4; break;
	}
	if (idCat!='0') {
		element='idCategoria'+idCat;
		if (document.getElementById(element)){
			document.getElementById(element).style.textDecoration='underline';
			document.getElementById(element).style.color='#FFFFFF';
		}
	}
	
}

function checkCookie() {
	
	if (getCookie('tip') == "") {
		setCookie('tip','residenziale',1);
	}
	return ;
}
