// JavaScript Document
var p_town = '';
var p_mq = 0;
var p_rendita = 0;
var p_minvalue = 0;
var p_maxvalue = 0;
function scegliTown() {
	nocache = Math.floor(Math.random()*100);
	if( 
	   	(document.getElementById('city') != null) &&
		(document.getElementById('mq') != null) &&
		(document.getElementById('rendita') != null) &&
		(document.getElementById('modeasearch_minvalue') != null) &&
		(document.getElementById('modeasearch_maxvalue') != null)
	   ) 
	{		
		town = document.getElementById('city').value;
		mq = document.getElementById('mq').value;
		rendita = document.getElementById('rendita').value;
		minvalue = document.getElementById('modeasearch_minvalue').value;
		maxvalue = document.getElementById('modeasearch_maxvalue').value;
		// alert('maxvalue: '+maxvalue+' minvalue: '+minvalue+' rendita: '+rendita);
		qry_string = '?nocache='+nocache;
		if( town != "" ) qry_string += '&town='+town;
		if( mq != 0 ) qry_string += '&mq='+mq;
		if( rendita != 0 ) qry_string += '&rendita='+rendita;
		if( minvalue != 0 ) qry_string += '&p_min='+minvalue;
		if( maxvalue != 0 ) qry_string += '&p_max='+maxvalue;
		var ricerche_incorso=("spazio","rendita","p_min","p_max");
		if( mq == 0 ) completeAHAH.ahah('/spazio.php'+qry_string, 'div_spazio', '', 'GET', '');
		if( rendita == 0 ) completeAHAH.ahah('/rendita.php'+qry_string, 'div_rendita', '', 'GET', '');
		if( minvalue == "" ) completeAHAH.ahah('/prezzo_min.php'+qry_string, 'prezzo_min', '', 'GET', '');
		if( maxvalue == "" ) completeAHAH.ahah('/prezzo_max.php'+qry_string, 'prezzo_max', '', 'GET', '');
		p_town = town;
	}
	else
	{
		ritorno = 0;
		if( p_town != "" )
			ritorno = trovaValore(document.getElementById('city').length, document.getElementById('city'), p_town);
		alert('Attendere caricamento dati');
		document.getElementById('city').selectedIndex = ritorno;
	}	
}
function scegliSpazio() {
	nocache = Math.floor(Math.random()*100);
	if( 
	   	(document.getElementById('city') != null) &&
		(document.getElementById('mq') != null) &&
		(document.getElementById('rendita') != null) &&
		(document.getElementById('modeasearch_minvalue') != null) &&
		(document.getElementById('modeasearch_maxvalue') != null)
	   ) 
	{
		town = document.getElementById('city').value;
		mq = document.getElementById('mq').value;
		rendita = document.getElementById('rendita').value;
		minvalue = document.getElementById('modeasearch_minvalue').value;
		maxvalue = document.getElementById('modeasearch_maxvalue').value;	
		qry_string = '?nocache='+nocache;
		if( town != "" ) qry_string += '&town='+town;
		if( mq != 0 ) qry_string += '&mq='+mq;
		if( rendita != 0 ) qry_string += '&rendita='+rendita;
		if( minvalue != 0 ) qry_string += '&p_min='+minvalue;
		if( maxvalue != 0 ) qry_string += '&p_max='+maxvalue;
		if( town == "" ) completeAHAH.ahah('/citta.php'+qry_string, 'div_citta', '', 'GET', '');
		if( rendita == 0 ) completeAHAH.ahah('/rendita.php'+qry_string, 'div_rendita', '', 'GET', '');
		if( minvalue == "" ) completeAHAH.ahah('/prezzo_min.php'+qry_string, 'prezzo_min', '', 'GET', '');
		if( maxvalue == "" ) completeAHAH.ahah('/prezzo_max.php'+qry_string, 'prezzo_max', '', 'GET', '');	
		p_mq = mq;
	}
	else
	{	
		ritorno = 0;
		if( p_mq != "" )
			ritorno = trovaValore(document.getElementById('mq').length, document.getElementById('mq'), p_mq);
		alert('Attendere caricamento dati');
		document.getElementById('mq').selectedIndex = ritorno;		
	}
}
function scegliRendita() {
	nocache = Math.floor(Math.random()*100);
	if( 
	   	(document.getElementById('city') != null) &&
		(document.getElementById('mq') != null) &&
		(document.getElementById('rendita') != null) &&
		(document.getElementById('modeasearch_minvalue') != null) &&
		(document.getElementById('modeasearch_maxvalue') != null)
	   ) 
	{	
		town = document.getElementById('city').value;
		mq = document.getElementById('mq').value;
		rendita = document.getElementById('rendita').value;
		minvalue = document.getElementById('modeasearch_minvalue').value;
		maxvalue = document.getElementById('modeasearch_maxvalue').value;	
		// alert('town: '+town+' mq: '+mq+' rendita: '+rendita);
		qry_string = '?nocache='+nocache;
		
		if( town != "" ) qry_string += '&town='+town;
		if( mq != 0 ) qry_string += '&mq='+mq;
		if( rendita != 0 ) qry_string += '&rendita='+rendita;
		if( minvalue != 0 ) qry_string += '&p_min='+minvalue;
		if( maxvalue != 0 ) qry_string += '&p_max='+maxvalue;
	
		if( town == "" ) completeAHAH.ahah('/citta.php'+qry_string, 'div_citta', '', 'GET', '');
		if( mq == 0 ) completeAHAH.ahah('/spazio.php'+qry_string, 'div_spazio', '', 'GET', '');	
		if( minvalue == "" ) completeAHAH.ahah('/prezzo_min.php'+qry_string, 'prezzo_min', '', 'GET', '');
		if( maxvalue == "" ) completeAHAH.ahah('/prezzo_max.php'+qry_string, 'prezzo_max', '', 'GET', '');	
		p_rendita = rendita;
	}
	else
	{
		ritorno = 0;
		if( p_rendita != "" )
			ritorno = trovaValore(document.getElementById('rendita').length, document.getElementById('rendita'), p_rendita);
		alert('Attendere caricamento dati');
		document.getElementById('rendita').selectedIndex = ritorno;	
	}		
}
function scegliPrezzoMin() {
	if( 
	   	(document.getElementById('city') != null) &&
		(document.getElementById('mq') != null) &&
		(document.getElementById('rendita') != null) &&
		(document.getElementById('modeasearch_minvalue') != null) &&
		(document.getElementById('modeasearch_maxvalue') != null)
	   ) 
	{	
		nocache = Math.floor(Math.random()*100);
		town = document.getElementById('city').value;
		mq = document.getElementById('mq').value;
		rendita = document.getElementById('rendita').value;
		minvalue = document.getElementById('modeasearch_minvalue').value;
		maxvalue = document.getElementById('modeasearch_maxvalue').value;	
		// alert('town: '+town+' mq: '+mq+' rendita: '+rendita);
		qry_string = '?nocache='+nocache;
		
		if( town != "" ) qry_string += '&town='+town;
		if( mq != 0 ) qry_string += '&mq='+mq;
		if( rendita != 0 ) qry_string += '&rendita='+rendita;
		if( minvalue != 0 ) qry_string += '&p_min='+minvalue;
		if( maxvalue != 0 ) qry_string += '&p_max='+maxvalue;
		
		if( town == "" ) completeAHAH.ahah('/citta.php'+qry_string, 'div_citta', '', 'GET', '');
		if( mq == 0 ) completeAHAH.ahah('/spazio.php'+qry_string, 'div_spazio', '', 'GET', '');	
		if( rendita == 0 ) completeAHAH.ahah('/rendita.php'+qry_string, 'div_rendita', '', 'GET', '');
		if( minvalue == "" ) completeAHAH.ahah('/prezzo_min.php'+qry_string, 'prezzo_min', '', 'GET', '');
		if( maxvalue == "" ) completeAHAH.ahah('/prezzo_max.php'+qry_string, 'prezzo_max', '', 'GET', '');
		p_minvalue = minvalue;
	}
	else
	{
		ritorno = 0;
		if( p_minvalue != "" )
			ritorno = trovaValore(document.getElementById('modeasearch_minvalue').length, document.getElementById('modeasearch_minvalue'), p_minvalue);
		alert('Attendere caricamento dati');
		document.getElementById('modeasearch_minvalue').selectedIndex = ritorno;		
	}			
}
function scegliPrezzoMax() {
	nocache = Math.floor(Math.random()*100);
	if( 
	   	(document.getElementById('city') != null) &&
		(document.getElementById('mq') != null) &&
		(document.getElementById('rendita') != null) &&
		(document.getElementById('modeasearch_minvalue') != null) &&
		(document.getElementById('modeasearch_maxvalue') != null)
	   ) 
	{
		town = document.getElementById('city').value;
		mq = document.getElementById('mq').value;
		rendita = document.getElementById('rendita').value;
		minvalue = document.getElementById('modeasearch_minvalue').value;
		maxvalue = document.getElementById('modeasearch_maxvalue').value;	
		qry_string = '?nocache='+nocache;
	
		// alert(qry_string);
		if( town != "" ) qry_string += '&town='+town;
		if( mq != 0 ) qry_string += '&mq='+mq;
		if( rendita != 0 ) qry_string += '&rendita='+rendita;
		if( minvalue != 0 ) qry_string += '&p_min='+minvalue;
		if( maxvalue != 0 ) qry_string += '&p_max='+maxvalue;
		
		if( town == "" ) completeAHAH.ahah('/citta.php'+qry_string, 'div_citta', '', 'GET', '');
		if( mq == 0 ) completeAHAH.ahah('/spazio.php'+qry_string, 'div_spazio', '', 'GET', '');	
		if( rendita == 0 ) completeAHAH.ahah('/rendita.php'+qry_string, 'div_rendita', '', 'GET', '');
		if( minvalue == "" ) completeAHAH.ahah('/prezzo_min.php'+qry_string, 'prezzo_min', '', 'GET', '');
		if( maxvalue == "" ) completeAHAH.ahah('/prezzo_max.php'+qry_string, 'prezzo_max', '', 'GET', '');	
		p_maxvalue = maxvalue;
	}
	else
	{
		ritorno = 0;
		if( p_maxvalue != "" )
			ritorno = trovaValore(document.getElementById('modeasearch_maxvalue').length, document.getElementById('modeasearch_maxvalue'), p_maxvalue);
		alert('Attendere caricamento dati');
		document.getElementById('modeasearch_maxvalue').selectedIndex = ritorno;	
	}	
}
function trovaValore(lenght_array, array, valore)
{
	ritorno = 0;
	for(i=0; i < lenght_array; i++)
	{
		if(array.options[i].value == valore)
			ritorno = i;
	}
	return ritorno;
}
