<?php
include("session.php");
include("../db.php");

//elenco vie
if($_GET['type'] == 'via' && !empty($_REQUEST['cod'])){
	$result = mysql_query("SELECT * FROM civici_provbi WHERE Codice_Com = '".$_REQUEST['cod']."' GROUP BY ID_STRADA ORDER BY Via");
	if(mysql_num_rows($result) > 0){
		echo 'Via <select id="ID_STRADA" name="ID_STRADA" onChange="stradario(\'civico\',this.value,\'strad_civico\')">';
		echo '<option value=0></option>';
		while($row = mysql_fetch_array($result))
			echo '<option value="'.$row['ID_STRADA'].'">'.utf8_encode($row['Via']).'</option>';
		echo '</select>';		
		
		//scrivo le coordinate della via
		echo '<input type="hidden" name="COORD_X_comune" id="COORD_X_comune" value="'.mysql_result($result,0,'COORD_X').'" />';
		echo '<input type="hidden" name="COORD_Y_comune" id="COORD_Y_comune" value="'.mysql_result($result,0,'COORD_Y').'" />';
		echo '<input type="hidden" name="cant_comune" id="cant_comune" value="'.mysql_result($result,0,'Nome_Comun').'" />';
		
	}else{
		echo 'Elenco vie non disponibile';
	}
}

//elenco civici
if($_GET['type'] == 'civico' && !empty($_REQUEST['cod'])){
	$result = mysql_query("/*!40101 set names 'utf8' */");
	$result = mysql_query("SELECT * FROM civici_provbi WHERE ID_STRADA = '".$_REQUEST['cod']."' GROUP BY NUMERO ORDER BY NUMERO*1");
	if(mysql_num_rows($result) > 0){
		echo 'n. <select name="ID_NUMERO" id="ID_NUMERO" onChange="stradario(\'coord\',this.value,\'strad_coord\')">';
		echo '<option value=0></option>';
		while($row = mysql_fetch_array($result))
			echo '<option value="'.$row['NUMERO'].'">'.utf8_encode($row['NUMERO']).'</option>';
		echo '</select>';
	
		//scrivo le coordinate della via
		echo '<input type="hidden" name="COORD_X_via" id="COORD_X_via" value="'.mysql_result($result,0,'COORD_X').'" />';
		echo '<input type="hidden" name="COORD_Y_via" id="COORD_Y_via" value="'.mysql_result($result,0,'COORD_Y').'" />';
		echo '<input type="hidden" name="cant_strada" id="cant_strada" value="'.mysql_result($result,0,'Via').'" />';
	}else{
		echo 'Elenco civici non disponibile';
	}
}

//coordinate
if($_GET['type'] == 'coord' && !empty($_REQUEST['cod']) && !empty($_REQUEST['id'])){
	$result = mysql_query("SELECT * FROM civici_provbi WHERE ID_STRADA = '".$_REQUEST['id']."' AND NUMERO = '".$_REQUEST['cod']."' LIMIT 1");
	if(mysql_num_rows($result) == 1){
		//scrivo le coordinate del civico
		echo '<input type="hidden" name="COORD_X" id="COORD_X" value="'.mysql_result($result,0,'COORD_X').'" />';
		echo '<input type="hidden" name="COORD_Y" id="COORD_Y" value="'.mysql_result($result,0,'COORD_Y').'" />';
		
	}
}

?>