var testo_form_dettaglio_immobile = "Scrivi qui le tue richieste per ricevere informazioni più dettagliate";
function check_boxes(form, field) {
	checked = true;
    for (i=0; i<form.length; i++){
		if (form.elements[i].name == field && form.elements[i].checked == true){
			checked = false;
			break;
		}
    }
    for (i=0; i<form.length; i++){
    	if (form.elements[i].name == field)
    		form.elements[i].checked = checked;
    }
}

/* Procedura per eliminazione bottone nel backoffice */
function reset_button(btn_num){

$("imgact"+btn_num).value="del";
$("image"+btn_num).src="";
$("linkTitolo"+btn_num).value="";

if (btn_num.substr(0,2)!='BR'){
	$("titolo"+btn_num).value="";
	$("testoPrinc"+btn_num).value="";
	$("linkTestoPrinc"+btn_num).value="";
	$("testoSec"+btn_num).value="";
	$("linkTestoSec"+btn_num).value="";
}


alert("Conferma modifiche per completare la cancellazione");
}



function popupCartina(url){
  window.open(url,"popupcartina","location=no,width=600,height=400");
}

function popupPrivacy(){
  window.open("/politica-di-privacy.php","privacy","location=no,width=600,height=400,scrollbars=yes,resizable=yes,top=" + ((screen.height-400)/2) + ",left=" + ((screen.width-600)/2));
}

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';
}

//RICCARDO: due funzioni per la gestione della scritta di default nella
// form delle newsletter e il controllo se l'utente ci ha scritto dentro
function showFormText(oEl, sText) {
	var formContent = oEl.value;
	if(formContent.length == 0)
	{
		oEl.value = sText;
	}
}

function hideFormText(oEl, sText) {
	var formContent = oEl.value;
	if(formContent == sText)
	{
		oEl.value = '';
	}
}

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,5})+$/.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 (formObj.privacy.value=="" || formObj.privacy.value!="S" )
		errore = errore + " - devi acconsentire al trattamente dei tuoi dati personali \n";

	if (errore!=""){
		errore = "Errori nei dati:\n" + errore;
		alert(errore);
		return false;
	}else{
		if (formObj.commento.value==testo_form_dettaglio_immobile || formObj.commento.value=="")
		 formObj.commento.value="";
		return true;
	}
}
var objOmbra;
var objImage;
var objImageOver;
var objImg;

var position = "out";
var idFoto = 0;
var aperta = false;

function fotoplus(indice,imageid,op,outmin){

	//settiamo i valori per fa vedere la fotoplus
	if (op=='none' && position == "over" && idFoto != 0){
		if (!aperta || !outmin || (idFoto != indice))
		{
			position = "out";
			idFoto = 0;
			hideFotoPlus(indice,imageid);
		}
	}
	else{
		position = "over";
		idFoto = indice;
		setTimeout("showFotoPlus(" + indice + "," + imageid + ")", 1000);
	}
}

function showFotoPlus(indice,imageid){

	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 (position == "over" && idFoto == indice && !aperta){
		objImage.style.display='block';
		objOmbra.style.display='block';
		objImageOver.style.display='block';
		objImg.onload=initImage;
		objImg.src = "http://media.casaclick.it/image/" + imageid + "/print.jpg";
		aperta = true;
		
	}
}

function hideFotoPlus(indice,imageid){

	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 (aperta){
		objImage.style.display='none';
		objOmbra.style.display='none';
		objImageOver.style.display='none';
		objImg.style.visibility='hidden';
		aperta = false;
	}
}

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 checkMessageForm(num)
{
        var hasError = false;
        var cform = document.forms[ num ];
        if( cform.nome.value.length == 0 )
        {
                alert( "Il campo NOME e' obbligatorio" );
                hasError = true;
        }
        else if(!cform.telefono.value && !cform.email.value)
        {
                alert( "I campi TELEFONO ed EMAIL non possono essere entrambi vuoti" );
                hasError = true;
        }
        else if( cform.telefono.value && !cform.telefono.value.match( /^[0-9]{5,}$/ ) )
        {
                alert( "Il valore inserito nel campo TELEFONO non e' valido" );
                hasError = true;
        }
        else if( cform.email.value && !cform.email.value.match( /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/ ) )
        {
                alert( "Il valore inserito nel campo EMAIL non e' valido" );
                hasError = true;
        }
        else if( cform.commento.value.length == 0 )
        {
                alert( "Il campo MESSAGGIO e' obbligatorio" );
                hasError = true;
        }
        else if( !cform.privacy.checked )
        {
                alert( "E' necessario autorizzare il trattamento dei dati personali" );
                hasError = true;
        }       
        if( !hasError )
                cform.submit();
}

function hideContratto(id){
	var oDivContratto = window.document.getElementById(id);
	if (oDivContratto && oDivContratto.style.display){
		if (oDivContratto.style.display != 'none'){
			oDivContratto.style.display = 'none';
			setTimeout (function() {window.document.getElementById('idContratto2').checked= 'checked'; }, 1000); 
			xajax_getAjaxFascePrezzoRicerca('2');
		}

	}
}

function showContratto(id, contratto){
	if (window.document.getElementById(id) == null)
		return false;

	var oDivContratto = window.document.getElementById(id);
	if (oDivContratto && oDivContratto.style.display){
		if (oDivContratto.style.display == 'none'){
			oDivContratto.style.display = 'block';
			if (contratto && contratto != "")
			{
				if (window.document.getElementById('idContratto' + contratto) == null)
					return false;

				window.document.getElementById('idContratto' + contratto).checked= 'checked';
				xajax_getAjaxFascePrezzoRicerca(contratto);
			}
			
		}

	}
}


function setSameHeight(sObjName, sClassName)
{
	try
	{
		if ($(sObjName) == null)
			return false;
		
		var nMaxHeight = 0;
		
		var aoClass = $(sObjName).select('[class="' + sClassName + '"]');
	
		aoClass.each(function(aoClassElement){
			if (aoClassElement.getHeight() > nMaxHeight)
				nMaxHeight = aoClassElement.getHeight();
			}
		);
	
		aoClass.each(function(aoClassElement){
			aoClassElement.setStyle({height: nMaxHeight+"px"});
		});
	}
	catch(e)
	{
		// errore non visualizzato in quanto la funzione non effettua operazioni essenziali
	}
}

function VediPassword(divId)
	{
		sDivStatus = eval("document.getElementById('pwd"+divId+"').style.display");
		if(sDivStatus == '')
			{
				sDivOp = "document.getElementById('pwd"+divId+"').style.display='none';";
				sDivOp2 = "document.getElementById('hide"+divId+"').style.display='';";
				
			}
		else
			{
				sDivOp = "document.getElementById('pwd"+divId+"').style.display='';";
				sDivOp2 = "document.getElementById('hide"+divId+"').style.display='none';";
			}
		
		eval(sDivOp);
		eval(sDivOp2);
	}
function NascondiPassword(divId)
	{
		sDivId = "document.getElementById('pwd"+divId+"').style.display='none';";
		eval(sDivId);
	}
function CaricaServizi(sUrl)
	{
		document.location.href="http://"+location.host+"/casa/informazioni/prestiti.php";
	}
function MostraDescrizioneSw(DivID)
	{
		sCommand = "document.getElementById('sw" + DivID + "').style.display='none'";
		eval(sCommand);
		sCommand = "document.getElementById('sw" + DivID + "_ext').style.display='block'";
		eval(sCommand);	
	}
function NascondiDescrizioneSw(DivID)
	{
		sCommand = "document.getElementById('sw" + DivID + "').style.display='block'";
		eval(sCommand);
		sCommand = "document.getElementById('sw" + DivID + "_ext').style.display='none'";
		eval (sCommand);	
	}

function switchText2(node, tagId, showText, hideText, hideTagId, heightTag){

	visibilityStatus = document.getElementById(tagId).style.display;

	if (visibilityStatus=='none'){
		node.innerHTML = showText;
		document.getElementById('contCitta').style.display='block';
	}
	else{
		node.innerHTML = hideText;
		document.getElementById('contCitta').style.display='none';
		
	}
}

function hideRicercaAvanzata(hideTag,hideText){
	
	if (document.getElementById(hideTag)){
		document.getElementById(hideTag).style.display = 'none';
		document.getElementById(hideText).style.display = 'none';
	}
}



function showRicercaAvanzata(hideTag,hideText,tagId,hideTagId,heightTag){
	
	visibilityStatus = document.getElementById(tagId).style.display;

	if (visibilityStatus=='block'){
		document.getElementById('contCitta').style.display='none';
		document.getElementById('linkRa').innerHTML = 'Ricerca base';
	}
	else{
		if (visibilityStatus!='none'){
			if (document.getElementById(hideTag)){
				document.getElementById(hideTag).style.display = 'block';
				
			}
		}
		document.getElementById('contCitta').style.display='block';
		document.getElementById('linkRa').innerHTML = 'Ricerca avanzata';
	}
	document.getElementById(hideText).style.display = 'block';

}

function checkCurrencys(evt) {
	evt = (evt) ? evt : event;
	var charCode = (evt.charCode) ? evt.charCode : 
		((evt.keyCode) ? evt.keyCode :
			((evt.which) ? evt.which : 0)
		);
	var carSpec = evt.keyCode;

	if (carSpec == 46 || carSpec ==37 || carSpec ==39) 
		return true;
	
	if (charCode > 31 && (charCode < 48 || charCode > 57 )) 
			return false;
	
	return true;
}

function checkCurrencysFloat(evt) {
	evt = (evt) ? evt : event;
	var charCode = (evt.charCode) ? evt.charCode : 
		((evt.keyCode) ? evt.keyCode :
			((evt.which) ? evt.which : 0)
		);
	var carSpec = evt.keyCode;

	if (carSpec == 46 || carSpec ==37 || carSpec ==39) 
		return true;

	if (charCode == 44) 
		return true;
	
	if (charCode > 31 && (charCode < 48 || charCode > 57 )) {
			alert("In questo campo sono ammessi solo numeri\nI decimali devono essere separati da virgola (es:124,58)")
			return false;
	}
	
	return true;
}

function operazioneAbbonamento(op, id){
	window.document.dati_abbonamento.idAbbonamento.value = id;
	window.document.dati_abbonamento.opAbbonamento.value = op;
	window.document.dati_abbonamento.submit();
}

function selectAbbonamento(id){
	var oContainer = document.getElementById("lista_abbonamenti");
	if (oContainer){
		var childDiv = oContainer.childNodes;
		for (var i=0; i< childDiv.length ; i++){
			if( childDiv[i].id && childDiv[i].id.indexOf("input_abbonamento_") > -1 ){
				var currId = childDiv[i].id.substring(18);
				if (currId == id){
					toggleVisible('div_abbonamento_'+id);
					toggleVisible('input_abbonamento_'+id);
				}else if(childDiv[i].style && childDiv[i].style.display == 'block') {
					toggleVisible('div_abbonamento_'+currId);
					toggleVisible('input_abbonamento_'+currId);
				}
			}
		}
	}
}


