// variabili detect browser
var browser = navigator.appName;
var b_versione = navigator.appVersion;
var versione = parseFloat(b_versione);
if (browser == "Netscape" || browser == "Opera")
  br = "ns";
else if (versione == 5)
	br = "ns";
else
  br = "ie";
// Variabili per effetto fade
var pos = 0;
var numero_elementi = 0;
var elemento_fade = "";
var elementi = new Array();
var vel_fade = 3;
var min_fade = 0;
var max_fade = 99;
var lim_min = min_fade+vel_fade;
var lim_max = max_fade-vel_fade;
// Variabili per cambio foto
var foto_max = 0;
var slide_prev = 0;
var slide_next = 0;
var status_fade = "spento";
// variabili gestione comuni
var zona_sel = "vuota";
var errore = "";
// variabili gestione sottomenu
var smenu_aperto = 0;

function controlla_zona(z) {
	with (document.forms["ricerca_semplice"]) {
		nomi_comuni.value = nomi_comuni.defaultValue;
     if (z == 0)
	  	zona_sel = "vuota";
     if (z == "BIELLA") {
	  	zona_sel = "biella";
			nomi_comuni.value = "Biella";
  	}
  	if (z == "PROVINCIA DI BIELLA")
	  	zona_sel = "provincia_biella";
  	if (z == "FUORI PROVINCIA DI BIELLA")
	  	zona_sel = "fuori_provincia";
	}
}
function apri_comuni() {
	with (document) {
  	if (errore != "")
      getElementById('errore_zona_'+errore).style.display = "none";
  	if (zona_sel == "vuota" || zona_sel == "biella") {
    	getElementById('errore_zona_'+zona_sel).style.display = "block";
  	  errore = ""+zona_sel;
  	  getElementById('avviso_errore').style.zIndex = "50";
  	  accendi('avviso_errore');
			setTimeout("spegni('avviso_errore')",6000);
  	} else {
			if (getElementById('avviso_errore').style.display == "block")
        spegni('avviso_errore');
      forms["ricerca_semplice"].valori_comuni.value = "";
      forms["ricerca_semplice"].nomi_comuni.value = forms["ricerca_semplice"].nomi_comuni.defaultValue;
      getElementById('box_comuni').style.zIndex = "50";
	  	accendi('box_comuni');
		  errore = "";
		}
	}
}
function posiziona(pg) {
	with (document) {
    getElementById('coda').style.top = getElementById('contenuti').offsetTop+getElementById('contenuti').clientHeight+"px";
    getElementById('coda').style.display = "block";
		if ((getElementById('coda').offsetTop+getElementById('coda').clientHeight) < getElementById('schermo').clientHeight)
			getElementById('coda').style.top = getElementById('schermo').clientHeight-getElementById('coda').clientHeight-3+"px";
		if (pg == "index") {
      getElementById('box_credits').style.top = getElementById('coda').offsetTop+61+"px";
      getElementById('box_credits').style.left = getElementById('schermo').clientWidth/2-(getElementById('box_credits').clientWidth)/2+"px";
      getElementById('box_credits').style.display = "none";
      getElementById('box_credits').style.visibility = "visible";
		}
	}
}

// funzione per far apparire gli elementi
function accendi() {
	with (document) {
	  numero_elementi_a = 0;
  	var elementi_a = [];
  	var elementi_a = new Array();
  	for (i=0;i<arguments.length;i++) {
	  	numero_elementi_a = numero_elementi_a+1;
		  elementi_a[i] = ""+arguments[i];
  	}
  	for (k=0;k<numero_elementi_a;k++)
      getElementById(''+elementi_a[k]).style.display = "block";
    var fade_start = 0;
    var fade_end = 99;
    var tempo_totale = 100;
  	var frequenza = (1 / tempo_totale);
    var partenza = new Date().getTime();
  	var tempo = setInterval(
      function() {
        var trascorso = new Date().getTime() - partenza;
        if (trascorso < tempo_totale) {
          var f = trascorso * frequenza;
          fade_temp = Math.round(f * fade_end - fade_start);
          if (br == "ns") {
			      if (fade_temp >= 10)
			        for (k=0;k<numero_elementi_a;k++)
  	  		      getElementById(''+elementi_a[k]).style.opacity = "."+fade_temp;
    			  else
      			  for (k=0;k<numero_elementi_a;k++)
    	  		    getElementById(''+elementi_a[k]).style.opacity = ".0"+fade_temp;
        	} else
            for (k=0;k<numero_elementi_a;k++)
       			  getElementById(''+elementi_a[k]).style.filter = "alpha(opacity="+fade_temp+")";
			  } else {
			    clearInterval(tempo);
			    if (br == "ns")
            for (k=0;k<numero_elementi_a;k++)
              getElementById(''+elementi_a[k]).style.opacity = "."+fade_end;
					else
  					for (k=0;k<numero_elementi_a;k++)
	  				  getElementById(''+elementi_a[k]).style.filter = "alpha(opacity="+fade_end+")";
  			}
      }, 10
    );
	}
}

// funzione per far scomparire gli elementi
function spegni() {
	with (document) {
	  numero_elementi_s = 0;
  	var elementi_s = [];
  	var elementi_s = new Array();
  	for (i=0;i<arguments.length;i++) {
	  	numero_elementi_s = numero_elementi_s+1;
		  elementi_s[i] = ""+arguments[i];
  	}
    var fade_start = 0;
    var fade_end = 99;
    var tempo_totale = 100;
  	var frequenza = (1 / tempo_totale);
    var partenza = new Date().getTime();
  	var tempo = setInterval(
      function() {
        var trascorso = new Date().getTime() - partenza;
        if (trascorso < tempo_totale) {
          var f = trascorso * frequenza;
          fade_temp = 99-Math.round(f * fade_end - fade_start);
          if (br == "ns") {
            if (fade_temp >= 10)
              for (k=0;k<numero_elementi_s;k++)
                getElementById(''+elementi_s[k]).style.opacity = "."+fade_temp;
            else
              for (k=0;k<numero_elementi_s;k++)
                getElementById(''+elementi_s[k]).style.opacity = ".0"+fade_temp;
          } else
            for (k=0;k<numero_elementi_s;k++)
              getElementById(''+elementi_s[k]).style.filter = "alpha(opacity="+fade_temp+")";
			  } else {
			    clearInterval(tempo);
          if (br == "ns")
            for (k=0;k<numero_elementi_s;k++)
              getElementById(''+elementi_s[k]).style.opacity = ".0";
          else
            for (k=0;k<numero_elementi_s;k++)
              getElementById(''+elementi_s[k]).style.filter = "alpha(opacity=0)";
          for (k=0;k<numero_elementi_s;k++)
            getElementById(''+elementi_s[k]).style.display = "none";
  			}
      }, 10
    );
	}
}

// funzione per cambio tra due foto
function cambia_slide(n,lim) {
	if (status_fade == "spento" && n != slide_prev) {
		status_fade = "acceso";
		var foto_max = lim;
		if (n == "p")
			slide_next = slide_prev-1;
	  else if (n == "n")
	    slide_next = slide_prev+1;
		else
			slide_next = n;
		with (document) {
	    if (slide_next != 0)
	      getElementById('prev').style.display = "block";
	    else
	      getElementById('prev').style.display = "none";
	    if (slide_next != foto_max-1)
	      getElementById('next').style.display = "block";
	    else
	      getElementById('next').style.display = "none";
      getElementById('link_non_attivo_'+slide_prev).style.display = "none";
      getElementById('link_attivo_'+slide_next).style.display = "none";
      getElementById('link_attivo_'+slide_prev).style.display = "block";
      getElementById('link_non_attivo_'+slide_next).style.display = "block";
		  var fade_start = 0;
	    var fade_end = 99;
	    var tempo_totale = 1500;
	  	var frequenza = (1 / tempo_totale);
	    var partenza = new Date().getTime();
	  	var tempo = setInterval(
	      function() {
	        var trascorso = new Date().getTime() - partenza;
	        if (trascorso < tempo_totale) {
	          var f = trascorso * frequenza;
	          fade_temp_s = 99-Math.round(f * fade_end - fade_start);
	          fade_temp_a = Math.round(f * fade_end - fade_start);
	          if (br == "ns") {
	            if (fade_temp_s >= 10)
	              getElementById('foto_slide_'+slide_prev).style.opacity = "."+fade_temp_s;
	            else
	              getElementById('foto_slide_'+slide_prev).style.opacity = ".0"+fade_temp_s;
				      if (fade_temp_a >= 10)
	    		      getElementById('foto_slide_'+slide_next).style.opacity = "."+fade_temp_a;
	    			  else
	   	  		    getElementById('foto_slide_'+slide_next).style.opacity = ".0"+fade_temp_a;
	          } else {
	              getElementById('foto_slide_'+slide_prev).style.filter = "alpha(opacity="+fade_temp_s+")";
	              getElementById('foto_slide_'+slide_next).style.filter = "alpha(opacity="+fade_temp_a+")";
	          }
				  } else {
				    clearInterval(tempo);
	          if (br == "ns") {
	            getElementById('foto_slide_'+slide_prev).style.opacity = ".0";
	            getElementById('foto_slide_'+slide_next).style.opacity = "."+fade_end;
	          } else {
	            getElementById('foto_slide_'+slide_prev).style.filter = "alpha(opacity=0)";
	            getElementById('foto_slide_'+slide_next).style.filter = "alpha(opacity="+fade_end+")";
						}
						slide_prev = slide_next;
						status_fade = "spento";
	  			}
	      }, 10
	    );
		}
	}
}

function cambia_contratto()
{
	var contratto = document.getElementsByName('contratto');
	for( i = 0; i < contratto.length; i++ )
	{
		if( contratto[i].checked == true )
		contratto_val = contratto[i].value;
	}
	
	var submitTo = './includes/ajax_request.php';
	http('POST', submitTo, aggiorna_prezzi,{'mode':'prezzi','contratto':contratto_val});
}

function aggiorna_prezzi(data)
{
	var prezzo = document.getElementsByName('prezzo');
	
	document.forms["ricerca_semplice"].prezzo.options.length=0;
	document.forms["ricerca_semplice"].prezzo.options[0] = new Option('Seleziona fascia di prezzo',0);
	
	for (var key in data) {  
		document.forms["ricerca_semplice"].prezzo.options[document.forms["ricerca_semplice"].prezzo.options.length]= new Option(data[key],key); 
	}
}

function pulisci_codice() {
	document.forms["ricerca_semplice"].codice_immobile.value = "";
}

function cerca_comuni() {
	var zona = document.getElementsByName('zona');
	var categoria = document.getElementsByName('categoria');
	var contratto = document.getElementsByName('contratto');
	var turistico = document.getElementsByName('turistico');
	var turistico_val = "";
	var contratto_val = "";
	
	for( i = 0; i < contratto.length; i++ )
	{
		if( contratto[i].checked == true )
		contratto_val = contratto[i].value;
	}
	
	for( i = 0; i < turistico.length; i++ )
	{
		if( turistico[i].checked == true )
		turistico_val = turistico[i].value;
	}	


	var submitTo = './includes/ajax_request.php';
	http('POST', submitTo, aggiorna_comuni,{'zona':zona[0].value,'categoria':categoria[0].value,'contratto':contratto_val,'turistico':turistico_val});
}

function aggiorna_comuni(data)
{
	var span = document.getElementById('lista_comuni');

	var table = document.createElement('table');

	mydiv = document.getElementById('lista_comuni');
	while ( mydiv.firstChild ) mydiv.removeChild( mydiv.firstChild );	
	
	var tbody = document.createElement('tbody');
	
	table.appendChild(tbody);
	
	for (var i=0; i<data.length; i=i+3)
	{
		var comune = document.createElement('tr');
		
		tbody.appendChild(comune);
		
		var next1 = i+1;
		var next2 = i+2;

		if (data[i])
		{
			var  colum = document.createElement('td');
			comune.appendChild(colum);
			colum.setAttribute('width', '150');
			
			var span_off = document.createElement('span');
			colum.appendChild(span_off);
			span_off.setAttribute('id', 'comune_off_'+data[i]['cod']);
			span_off.setAttribute('class', 'voce_comune_off');

			var anchor = document.createElement('a');
			span_off.appendChild(anchor);
			
//			anchor.setAttribute('class', 'link_00');
      anchor.className = 'link_00';
			anchor.setAttribute('href', 'javascript: inserisci_comune('+data[i]['cod']+',\''+data[i]['comune']+'\')');			
			anchor.appendChild(document.createTextNode(data[i]['comune']+' ('+data[i]['n_immobili']+')'));
			
			var span_on = document.createElement('span');
			colum.appendChild(span_on);
			span_on.setAttribute('id', 'comune_on_'+data[i]['cod']);
			span_on.setAttribute('class', 'voce_comune_on');
			span_on.appendChild(document.createTextNode(data[i]['comune']+' ('+data[i]['n_immobili']+')'));
      span_on.style.cssText = 'display:none;';
//			span_on.setAttribute('style', 'display:none;');
			
		} else {

			var colum = document.createElement('td');
			comune.appendChild(colum);
			colum.setAttribute('width', '150');		
		}
		if (data[next1])
		{
			var colum = document.createElement('td');
			comune.appendChild(colum);
			colum.setAttribute('width', '150');
			
			var span_off = document.createElement('span');
			colum.appendChild(span_off);
			span_off.setAttribute('id', 'comune_off_'+data[next1]['cod']);
			span_off.setAttribute('class', 'voce_comune_off');

			var anchor = document.createElement('a');
			span_off.appendChild(anchor);
			
//			anchor.setAttribute('class', 'link_00');
      anchor.className = 'link_00';
			anchor.setAttribute('href', 'javascript: inserisci_comune('+data[next1]['cod']+',\''+data[next1]['comune']+'\')');			
			anchor.appendChild(document.createTextNode(data[next1]['comune']+' ('+data[next1]['n_immobili']+')'));

			var span_on = document.createElement('span');
			colum.appendChild(span_on);
			span_on.setAttribute('id', 'comune_on_'+data[next1]['cod']);
			span_on.setAttribute('class', 'voce_comune_on');
			span_on.appendChild(document.createTextNode(data[next1]['comune']+' ('+data[next1]['n_immobili']+')'));
      span_on.style.cssText = 'display:none;';
//			span_on.setAttribute('style', 'display:none;');
		
		} else {
		
			var colum = document.createElement('td');
			comune.appendChild(colum);
			colum.setAttribute('width', '150');		
		}
		if (data[next2])
		{
			var colum = document.createElement('td');
			comune.appendChild(colum);
			colum.setAttribute('width', '150');

			var span_off = document.createElement('span');
			colum.appendChild(span_off);
			span_off.setAttribute('id', 'comune_off_'+data[next2]['cod']);
			span_off.setAttribute('class', 'voce_comune_off');

			var anchor = document.createElement('a');
			span_off.appendChild(anchor);
			
//			anchor.setAttribute('class', 'link_00');
      anchor.className = 'link_00';
			anchor.setAttribute('href', 'javascript: inserisci_comune('+data[next2]['cod']+',\''+data[next2]['comune']+'\')');			
			anchor.appendChild(document.createTextNode(data[next2]['comune']+' ('+data[next2]['n_immobili']+')'));
			
			var span_on = document.createElement('span');
			colum.appendChild(span_on);
			span_on.setAttribute('id', 'comune_on_'+data[next2]['cod']);
			span_on.setAttribute('class', 'voce_comune_on');
			span_on.appendChild(document.createTextNode(data[next2]['comune']+' ('+data[next2]['n_immobili']+')'));
      span_on.style.cssText = 'display:none;';
//		span_on.setAttribute('style', 'display:none;');
		
		} else {
			var colum = document.createElement('td');
			comune.appendChild(colum);
			colum.setAttribute('width', '150');
		}

			
	}
	
	mydiv.appendChild(table);
}

function cerca_avanzata_comuni(){
	var zona = document.getElementsByName('zona');

	var submitTo = './includes/ajax_request.php';
	http('POST', submitTo, aggiorna_avanzata_comuni,{'zona':zona[0].value});
}

function aggiorna_avanzata_comuni(data){
	alert(data);
}
function invia_semplice() {
  document.forms["ricerca_semplice"].submit();
}

function inserisci_comune(v,n) {
	with (document) {
    forms["ricerca_semplice"].valori_comuni.value += ""+v+",";
    forms["ricerca_semplice"].nomi_comuni.value += ""+n+", ";
    getElementById('comune_off_'+v).style.display = "none";
    getElementById('comune_on_'+v).className = "comune_selezionato";
    getElementById('comune_on_'+v).style.display = "block";
  }
}
function azzera_comuni() {
	with (document) {
    forms["ricerca_semplice"].valori_comuni.value = "";
    forms["ricerca_semplice"].nomi_comuni.value = forms["ricerca_semplice"].nomi_comuni.defaultValue;
    cerca_comuni();
  }
}
function invia_ordina() {
	with (document.forms["ordina_esito"]) {
		if (ordina_prezzo.value != 0) {
			submit();
			//alert ('avvia ordinamento');
		}
	}
}
function apri_sottomenu(n) {
	with (document) {
	  if (n != smenu_aperto) {
      getElementById('sottomenu_'+n).style.visibility = "visible";
      smenu_aperto = n;
    } else {
      getElementById('sottomenu_'+n).style.visibility = "hidden";
      smenu_aperto = 0;
		}
  }
}           
