Index: trunk/admin/modules/Elezioni/ele_associazioni.php
===================================================================
--- trunk/admin/modules/Elezioni/ele_associazioni.php	(revision 221)
+++ trunk/admin/modules/Elezioni/ele_associazioni.php	(revision 226)
@@ -335,4 +335,5 @@
 			$res_del = mysql_query("delete from ".$prefix."_ele_voti_gruppo where id_cons=$idcns" ,$dbi);
 			$res_del = mysql_query("delete from ".$prefix."_ele_voti_parziale where id_cons=$idcns" ,$dbi);
+			$res_del = mysql_query("delete from ".$prefix."_ele_controllosez where id_cons=$idcns" ,$dbi);
 			if ($pwd4==1){
 			$res_del = mysql_query("delete from ".$prefix."_ele_candidati where id_cons=$idcns" ,$dbi);
Index: trunk/admin/modules/Elezioni/ele_voti.php
===================================================================
--- trunk/admin/modules/Elezioni/ele_voti.php	(revision 221)
+++ trunk/admin/modules/Elezioni/ele_voti.php	(revision 226)
@@ -73,25 +73,46 @@
 
 function stato_rilevazione($id_sez,$solosede){
-global $id_cons, $prefix, $dbi, $id_cons_gen,$status,$genere,$votol, $votog, $idrefgruppo;
-
-if($idrefgruppo) $perref= "and id_gruppo='$idrefgruppo'";
+global $id_cons, $prefix, $dbi, $id_cons_gen,$status,$genere,$votol, $votog, $votoc,$idrefgruppo,$num_ril;
+
+if($idrefgruppo and $genere==0) $perref= "and id_gruppo='$idrefgruppo'";
 else $perref="";
 $ops=0;
 $status=0;
+$stret=0; $contains=0;
+###########
+$res = mysql_query("SELECT data,orario FROM ".$prefix."_ele_rilaff where id_cons_gen=$id_cons_gen order by data asc,orario asc", $dbi);
+$num_ril=mysql_num_rows($res);
+$res3 = mysql_query("SELECT stato_aff, stato_val,stato_gru,stato_lis,stato_can,status FROM ".$prefix."_ele_controllosez where id_sez='$id_sez' and id_cons='$id_cons'", $dbi);
+						
+if(mysql_num_rows($res3)){
+	list($aff,$val,$gru,$lis,$can,$status)=mysql_fetch_row($res3); 
+	if ($can) {$ops=-5;}
+	elseif ($lis) {$ops=-3;}
+	elseif($gru) {$ops=-4;}
+	elseif ($val) {$ops=-2;}
+	elseif($aff) {$ops=-1;}
+#	if($can==1) $status=1;
+}else{
+$aff=0;$lis=0;$val=0;$gru=0;$can=0;$stret=0;$status=0;
+##########################
 	if($ops==0){
 #controllo congruenza affluenze
-		$res = mysql_query("SELECT orario,data FROM ".$prefix."_ele_rilaff where id_cons_gen=$id_cons_gen order by data desc,orario desc", $dbi);
+		$res3= mysql_query("SELECT count(num_sez) FROM ".$prefix."_ele_sezioni where id_cons='$id_cons'", $dbi);
+		list($numtot) = mysql_fetch_row($res3);
+		$test_ril=0;$pre_rilc=0;$pre_rilm=0;$pre_rilf=0;
 		if($res)
-		while (list($ora,$giorno) = mysql_fetch_row($res)){
+		while (list($giorno,$ora) = mysql_fetch_row($res)){
+			$test_ril++;
+			$query="SELECT voti_complessivi FROM ".$prefix."_ele_voti_parziale where id_sez='$id_sez' $perref and orario='$ora' and data='$giorno'";
+			$res2 = mysql_query("$query", $dbi);
+			$numscru=mysql_num_rows($res2);
 			$query="SELECT sum(voti_complessivi) FROM ".$prefix."_ele_voti_parziale where id_sez='$id_sez' $perref and orario='$ora' and data='$giorno'";
 			$res2 = mysql_query("$query", $dbi);
 			list($voti)=mysql_fetch_row($res2);
-			if($voti) {
+			if($numscru) {
 				if(isset($ora)){
 					$query="SELECT count(voti_complessivi) FROM ".$prefix."_ele_voti_parziale where id_cons='$id_cons' $perref and orario='$ora' and data='$giorno'";
-					$res3 = mysql_query("$query", $dbi);
-					list($numscru)=mysql_fetch_row($res3);
-					$res3= mysql_query("SELECT count(num_sez) FROM ".$prefix."_ele_sezioni where id_cons='$id_cons'", $dbi);
-					list($numtot) = mysql_fetch_row($res3);
+#					$res3 = mysql_query("$query", $dbi);
+#					list($numscru)=mysql_fetch_row($res3);
 					$res3= mysql_query("SELECT maschi+autorizzati_m,femmine+autorizzati_f FROM ".$prefix."_ele_sezioni where id_sez='$id_sez'", $dbi);
 					list($iscrittim,$iscrittif) = mysql_fetch_row($res3);
@@ -101,30 +122,37 @@
 					$res3 = mysql_query("$query", $dbi);
 					list($voti, $uomini, $donne)=mysql_fetch_row($res3);
-#					if($voti>0) {#echo $query;
-						if ((($uomini+$donne)==0 and ($voti)<=$iscritti) or ((($uomini+$donne)==$voti) and ($uomini<=$iscrittim) and ($donne<=$iscrittif))){
-							#if($numscru<$numtot and $numscru>0)
-								{$status=1;$ops=-1;break;}#
-#							else
-#								{$status=0;$ops=-1;break;}
-						}else {return 1;}
-#					} else return 0;
+						if (((($uomini+$donne)==0 and ($voti<=$iscritti) and $voti>=$pre_rilc) or ((($uomini+$donne)==$voti) and ($uomini<=$iscrittim) and ($donne<=$iscrittif) )) and $uomini>=$pre_rilm and $donne>=$pre_rilf and $voti>=$pre_rilc)
+						{
+							$ops=-1;$aff=1; $status=$test_ril;
+						}elseif($numscru) {$aff=-1;$stret=1;break;}
+						else {$status=0;$ops=-1;break;}
 				}
-			} else {return -1;}
-		}
-		# return 0;
+			} else {$stret=-1;$aff=0;}
+		$pre_rilc=$voti; $pre_rilm=$uomini;$pre_rilf=$donne;
+		}
+		#if(isset($num_ril) and $num_ril==$test_ril) $status=1;
+
 		#controllo congruenza voti espressi
 			#per referendum
-		if($genere==0){
+		if($genere==0){ 
+			$resref = mysql_query("SELECT id_gruppo FROM ".$prefix."_ele_gruppo where id_cons='$id_cons'", $dbi);
+			$numscru=mysql_num_rows($resref); $rifscru=0;
+			while(list($idrefgruppo)=mysql_fetch_row($resref)) {
 			$res2= mysql_query("SELECT si,no,validi,nulli,bianchi,contestati FROM ".$prefix."_ele_voti_ref where id_sez='$id_sez' and id_gruppo='$idrefgruppo'", $dbi);
-			$refscru=mysql_num_rows($res2);
+			$refscru=mysql_num_rows($res2); 
 				if($refscru){
-#					echo "$voti - $ora - $giorno :<br>";
+					$query="SELECT voti_complessivi FROM ".$prefix."_ele_voti_parziale where id_sez='$id_sez' and id_gruppo='$idrefgruppo' order by data desc, orario desc";
+					$res3 = mysql_query("$query", $dbi);
+					list($voti)=mysql_fetch_row($res3);
+				
+					$rifscru++;
 					list($si,$no,$validi,$nulli,$bianchi,$contestati)=mysql_fetch_row($res2);
-					if(($si+$no==$validi) and ($validi+$nulli+$bianchi+$contestati==$voti)) {$status=1;return (-4);}
-					else return (4);
-				}	else {$status=0; return (-4);}
-		}
+					if(($si+$no==$validi) and ($validi+$nulli+$bianchi+$contestati==$voti)) { if($rifscru==$numscru) $status=5; else $status=4; $ops=-4;$val=1;} #{$status=1;return (-4);}
+					else {$val=-1;$stret=4;break;} #return (4);
+				}	else { $stret=4;break;}
+			}
+		}else{
 			#per le altre consultazione
-		$res2= mysql_query("SELECT sum(validi) FROM ".$prefix."_ele_sezioni where id_cons='$id_cons'", $dbi);
+		$res2= mysql_query("SELECT validi FROM ".$prefix."_ele_sezioni where id_sez='$id_sez' and id_cons='$id_cons' ", $dbi);
 		if($res2){
 		list($validi) = mysql_fetch_row($res2);
@@ -136,108 +164,105 @@
 					if($validi>0) {
 						if (($validi+$nulli+$bianchi+$contestati+$votinulli+$sololista)==$voti)
-							{$status=1;$ops=(-4+$votog);}
-						else return (4-$votog);
+							{$status=2;$ops=(-4+$votog);$val=1;}
+						else {$val=-1;$stret=(4-$votog);}
 					} #else return 0;
 			}
 		}
 		#controllo congruenza voti di gruppo
-		$res3= mysql_query("SELECT sum(voti) FROM ".$prefix."_ele_voti_gruppo where id_cons='$id_cons'", $dbi);
-		if($res3 and !$votog){$status=0;$ops=-4;
-			list($votig) = mysql_fetch_row($res3);
-			if($votig  and $genere!=2) {  
-					$status=0;
-					$query="SELECT sum(voti) FROM ".$prefix."_ele_voti_gruppo where id_sez='$id_sez'";
-					$res4 = mysql_query("$query", $dbi);
-					list($votigs)=mysql_fetch_row($res4);
+		if($genere!=4){
+		$res3= mysql_query("SELECT sum(voti) FROM ".$prefix."_ele_voti_gruppo where id_sez='$id_sez' and id_cons='$id_cons'", $dbi);
+		if($res3 and !$votog){#$status=0;$ops=-4;
+			list($votigs) = mysql_fetch_row($res3);
+			if($votigs  and $genere!=2) {  
+				#	$status=0;
+#					$query="SELECT sum(voti) FROM ".$prefix."_ele_voti_gruppo where id_sez='$id_sez'";
+#					$res4 = mysql_query("$query", $dbi);
+#					list($votigs)=mysql_fetch_row($res4);
 					if($votigs>0) {
 						if (($votigs)==$validi)
-							{$status=1;$ops=-3;}
-						else return 4;
+							{$status=3;$ops=-3;$gru=1;}
+						else {$gru=-1; $stret=4;}
 					} #else return 0;
 			}
 		}
+		}#else {$gru=1;echo "--$votigs -- $votog";}
+		if(($genere>1 and !$votol)){#$status=0;
 		$res4= mysql_query("SELECT t1.preferenze,t1.id_fascia,t2.limite FROM ".$prefix."_ele_cons_comune as t1 left join ".$prefix."_ele_conf as t2 on t1.id_conf=t2.id_conf where t1.id_cons='$id_cons'", $dbi);
 		if($res4) list($testpref,$fascia,$limite)=mysql_fetch_row($res4); else {$testpref=0;$fascia=0;$limite=0;}
 
 		#controllo congruenza voti di lista
-		$res3= mysql_query("SELECT sum(voti) FROM ".$prefix."_ele_voti_lista where id_cons='$id_cons'", $dbi);
-		if($res3 and ($genere>1 and !$votol)){$status=0;
-			list($votil) = mysql_fetch_row($res3);
-			if($votil) {
-					$query="SELECT sum(voti) FROM ".$prefix."_ele_voti_lista where id_sez='$id_sez'";	
-					$res4 = mysql_query("$query", $dbi);
+#		$res3= mysql_query("SELECT sum(voti) FROM ".$prefix."_ele_voti_lista where id_cons='$id_cons'", $dbi);
+#			list($votil) = mysql_fetch_row($res3);
+					$tablg=$fascia<=$limite ? "gruppo":"lista";
+					$query="SELECT sum(voti) FROM ".$prefix."_ele_voti_$tablg where id_sez='$id_sez' and  id_cons='$id_cons'";	
+					$res4 = mysql_query($query, $dbi);
 					list($votils)=mysql_fetch_row($res4);
+					$query="SELECT voti FROM ".$prefix."_ele_voti_$tablg where id_sez='$id_sez' and  id_cons='$id_cons'";	
+					$res4 = mysql_query($query, $dbi);
+					$sezscru=mysql_num_rows($res4);
+			if($sezscru) {
+					if($genere==4 or $votog) $contrlis= ($validilista==$validi) ? 1:0;
+					else $contrlis=($sologruppo+$validilista+$contestatilista+$votinullilista-$sololista)==$validi ? 1:0;
+					if((($contrlis) and $validilista==$votils) or ($fascia<=$limite and $fascia)) {$status=4;$lis=1;$ops=-4;}
+					elseif($sezscru) {$stret=3;$lis=-1;}
+					
 #					if($votils>0) {
 					if(mysql_num_rows($res4)>0) {
-						$query="SELECT stato_aff, stato_val,stato_gru,stato_lis,stato_can FROM ".$prefix."_ele_controllosez where id_sez='$id_sez' and id_cons='$id_cons'";
-						$res3 = mysql_query("$query", $dbi);
-							$res4= mysql_query("SELECT count(0) FROM ".$prefix."_ele_voti_candidati as t1 left join ".$prefix."_ele_candidati as t2 on t1.id_cand=t2.id_cand where t1.id_cons='$id_cons' and  t1.id_sez='$id_sez' and t2.id_lista='$testlista'", $dbi);
-							list($contains)=mysql_fetch_row($res4);
-							if($contains==0) $tutte=1; else $tutte=0;
-						if(mysql_num_rows($res3)){
-							list($aff,$val,$gru,$lis,$can)=mysql_fetch_row($res3); 
-						}else{
+#						$res4= mysql_query("SELECT count(0) FROM ".$prefix."_ele_voti_candidati where id_cons='$id_cons' and  id_sez='$id_sez' ", $dbi);
+#						list($contains)=mysql_fetch_row($res4);
+#						if($contains==0) $tutte=1; else $tutte=0;
+							
 							$query="SELECT id_lista FROM ".$prefix."_ele_lista where id_cons='$id_cons'";
 							$res3 = mysql_query("$query", $dbi); 
-							$testcand=0;
-							
+							$contaliste=mysql_num_rows($res3);
 							$res4= mysql_query("SELECT preferenze FROM ".$prefix."_ele_cons_comune where id_cons='$id_cons'", $dbi);
 							if($res4) list($testpref)=mysql_fetch_row($res4); else $testpref=0;
 							while(list($testlista)=mysql_fetch_row($res3)){
-								$res4= mysql_query("SELECT sum(voti) FROM ".$prefix."_ele_voti_lista where id_cons='$id_cons' and id_sez='$id_sez' and id_lista='$testlista'", $dbi);
+								if($fascia<=$limite)
+								{
+									$res4= mysql_query("SELECT id_gruppo FROM ".$prefix."_ele_lista where id_cons='$id_cons' and id_lista='$testlista'", $dbi);
+									list($idgruppo)=mysql_fetch_row($res4);
+									$res4= mysql_query("SELECT sum(voti) FROM ".$prefix."_ele_voti_gruppo where id_cons='$id_cons' and id_sez='$id_sez' and id_gruppo='$idgruppo'", $dbi);
+
+								}else{
+									$res4= mysql_query("SELECT sum(voti) FROM ".$prefix."_ele_voti_lista where id_cons='$id_cons' and id_sez='$id_sez' and id_lista='$testlista'", $dbi);
+								}
 								if($res4) list($testvotil)=mysql_fetch_row($res4); else $testvotil=0;
 								$res4= mysql_query("SELECT sum(t1.voti) FROM ".$prefix."_ele_voti_candidati as t1 left join ".$prefix."_ele_candidati as t2 on t1.id_cand=t2.id_cand where t1.id_cons='$id_cons' and  t1.id_sez='$id_sez' and t2.id_lista='$testlista'", $dbi);
 								if($res4) list($testvotic)=mysql_fetch_row($res4); else $testvotic=0;
-
-								if(($testvotil*$testpref)<$testvotic) { $testcand=1;break;}
 								$res4= mysql_query("SELECT t1.voti FROM ".$prefix."_ele_voti_candidati as t1 left join ".$prefix."_ele_candidati as t2 on t1.id_cand=t2.id_cand where t1.id_cons='$id_cons' and  t1.id_sez='$id_sez' and t2.id_lista='$testlista'", $dbi);
+								if(mysql_num_rows($res4)) $contains++;
+								$totvotic=0;
 								while(list($testvotic)=mysql_fetch_row($res4))
-									if($testvotil<$testvotic) { $testcand=1;break;}
+								{
+									$totvotic+=$testvotic;
+									if($testvotil<$testvotic) { $can=-1;break;}
+								}
+								if(($testvotil*$testpref)<$totvotic) { $can=-1;break;}
 							}
-						}
-						if(!isset($can)){ 
-							$can=($testcand==0) ? 1:-1;
-							if($genere==4 or $votog) $contestatilista=0;
-							$val=(($votils+$sologruppo+$contestatilista+$votinullilista)==($validi+$sololista))?1:-1;
-							$lis=($validilista==$votils) ? 1:-1;
-							$res4=mysql_query("insert into ".$prefix."_ele_controllosez values('$id_sez','$id_cons','0','$val','0','$lis','$can')",$dbi);
-						}
-						#		$res4= mysql_query("SELECT sum(voti) FROM ".$prefix."_ele_voti_lista where  id_sez='$id_sez' and id_lista='$testlista' and id_cons='$id_cons' ", $dbi);
-				#		$res4= mysql_query("SELECT sum(t1.voti) FROM ".$prefix."_ele_voti_candidati as t1 left join ".$prefix."_ele_candidati as t2 on t1.id_cand=t2.id_cand where t1.id_cons='$id_cons' and  t1.id_sez='$id_sez' and t2.id_lista='$testlista'", $dbi);
-						if($genere==4 or $votog) $contestatilista=0;
-#						if (($votils+$sologruppo+$contestatilista+$votinullilista)==($validi+$sololista) and $validilista==$votils and $testcand==0)
-						if ($val==1 and $lis==1 and $can==1)
-							{$status=$tutte;$ops=-3;}
-						else {return 3;}
-					} #else return 0;
-			}elseif($genere==5 and $fascia<=$limite){
-/*				$testcand=0;
-				$query="SELECT id_lista,id_gruppo FROM ".$prefix."_ele_lista where id_cons='$id_cons'";
-				$res3 = mysql_query("$query", $dbi);
-				$tutte=1;
-				while(list($testlista,$testgruppo)=mysql_fetch_row($res3)){				
-				$res4= mysql_query("SELECT sum(voti) FROM ".$prefix."_ele_voti_gruppo where id_cons='$id_cons' and id_sez='$id_sez' and id_gruppo='$testgruppo'", $dbi);
-				if($res4) list($testvotit)=mysql_fetch_row($res4); else $testvotit=0;
-				$res4= mysql_query("SELECT sum(t1.voti) FROM ".$prefix."_ele_voti_candidati as t1 left join ".$prefix."_ele_candidati as t2 on t1.id_cand=t2.id_cand where t1.id_cons='$id_cons' and  t1.id_sez='$id_sez' and t2.id_lista='$testlista'", $dbi);
-				if($res4) list($testvotic)=mysql_fetch_row($res4); else $testvotic=0;
-				$res4= mysql_query("SELECT count(0) FROM ".$prefix."_ele_voti_candidati as t1 left join ".$prefix."_ele_candidati as t2 on t1.id_cand=t2.id_cand where t1.id_cons='$id_cons' and  t1.id_sez='$id_sez' and t2.id_lista='$testlista'", $dbi);
-				list($contains)=mysql_fetch_row($res4);
-                                if($contains==0) $tutte=0;
-
-
-				if(($testvotit*$testpref)<$testvotic) {$testcand=1;break;}
-				$res4= mysql_query("SELECT t1.voti FROM ".$prefix."_ele_voti_candidati as t1 left join ".$prefix."_ele_candidati as t2 on t1.id_cand=t2.id_cand where t1.id_cons='$id_cons' and  t1.id_sez='$id_sez' and t2.id_lista='$testlista'", $dbi);
-				while( list($testvotic)=mysql_fetch_row($res4))
-					if(($testvotit)<$testvotic) {$testcand=1;break;}
-				}*/
-				if($testcand==0)
-					{$status=$tutte;$ops=-3;}
-				else {return 3;}
-			}
-		}elseif($ops==-3){ $ops=-4;}	
-
-
+							if($contains==$contaliste and $can==0) {$status=5;$can=1;$ops=-3;}
+							elseif($can==-1) {$stret=3;}
+					}						
+				}
+		}elseif($ops==-3){ $ops=-4;}
+		}
+					if(isset($can)){ 
+						$res4=mysql_query("insert into ".$prefix."_ele_controllosez values('$id_sez','$id_cons','$aff','$val','$gru','$lis','$can','$status')",$dbi);
+					}
+}
+} 
+if($aff==-1) {$stret=1;}
+elseif($val==-1) {$stret=(4-$votog);}
+elseif($gru==-1) {$stret=4;}
+elseif($lis==-1) {$stret=3;}
+elseif($can==-1) {$stret=3;}
+elseif($can==1) {$stret=-3;}
+elseif($lis==1) {$stret=-3;}
+elseif($gru==1) {$stret=-4;}
+elseif($val==1) {$stret=-(4-$votog);}
+else ($stret=-1);
+if($stret) return($stret);
 return ($ops);
-	}
+	
 
 }
@@ -250,7 +275,6 @@
 function numeri_sezione($nums) {
 // tabella visualizzazione sezioni per numero
-global $aid,$bgcolor1, $bgcolor2, $prefix, $dbi, $offset, $min, $tipo_cons, $genere,$id_cons_gen,$id_cons,$id_lista,$ops, $perms,$id_sez;
-//	$ressup = mysql_query("select permessi from ".$prefix."_ele_operatori where aid='$aid'",$dbi);
-//	list ($perms)=mysql_fetch_row($ressup);
+global $aid,$bgcolor1, $bgcolor2, $prefix, $dbi, $offset, $min, $tipo_cons, $genere,$id_cons_gen,$id_cons,$id_lista,$ops, $perms,$id_sez,$num_ril,$status,$idrefgruppo,$votog,$votol,$votoc; 
+$idrefgruppo='';
 	$solosede='';
 	if ($perms<32) {
@@ -277,42 +301,48 @@
 			//colora la sezione
 			$sezstat=0; #die($ops);
-			global $status;
-			global $idrefgruppo; $idrefgruppo='';
-			if($genere==0){
-				$resref = mysql_query("SELECT id_gruppo FROM ".$prefix."_ele_gruppo where id_cons='$id_cons'", $dbi);
-				while(list($idrefgruppo)=mysql_fetch_row($resref)) {
-					$sezstat=stato_rilevazione($sez_id,$solosede);
-					if ($sezstat>0) break;
-				}
-			}else
-				$sezstat=stato_rilevazione($sez_id,$solosede);
+			$sezstat=stato_rilevazione($sez_id,$solosede);
 			$cursez="style=\" background: #bbbbbb;";
 			$bgsez="";
-#			if ($sezstat==(-1)) $bgsez="#99CC33"; #verde
-
-#			if ($status==1 and $sezstat<0) { $ops=abs($sezstat);$bgsez="#99CC33";} #verde
-			if ($status==1 and $sezstat<0) { $ops=abs($sezstat);$cursez="style=\" background: #99CC33;";} #verde
-			elseif ($sezstat<0) {$ops=abs($sezstat);}
-			elseif ($sezstat>0) {
-#				$bgsez="#FF3300";#rosso;
+			if ($sezstat>0) { 
 				$cursez="style=\" background: #FF3300;";#rosso;
-				if ($sezstat) $ops=$sezstat; else $ops=1;
+			}else{ 
+					if($sezstat==-5 or ($sezstat==-3 and $votoc))
+						if($status<$num_ril or ($votoc and $status==0) or ($genere==5 and $status==2)) {$cursez="style=\" background: #ADFF2F;";}#verde-giallo
+						else $cursez="style=\" background: #99CC33;"; #verde
+					elseif($sezstat==-3){ 
+						if($status==0) {$cursez="style=\" background: #B0E0E6;";}#powder-blue
+						elseif(($genere==4 and $status==5) or $votoc or $status==5 or ($genere==3 and $status==4)) $cursez="style=\" background: #99CC33;"; #verde
+						else $cursez="style=\" background: #00BFFF;"; #deepsky-blue
+						}
+					elseif($sezstat==-4){
+						if($status==2) {$cursez="style=\" background: #F5F5F5;";}#whitesmoke
+						elseif ($genere>1 or ($genere==0 and $status<5)) $cursez="style=\" background: #98FB98;"; #palegreen
+						elseif ($genere==0 and $status==0) {$cursez="style=\" background: #DAA520;";} #gold
+						else $cursez="style=\" background: #99CC33;"; #verde
+						}
+					elseif($sezstat==-2){
+						if($status==0) {$cursez="style=\" background: #B0E0E6;";}#powder-blue
+						else $cursez="style=\" background: #00BFFF;"; #deepsky-blue
+						}
+					elseif($sezstat==-1){
+						if($status==0 and $status<$num_ril) {$cursez="style=\" background: #F5F5DC;";}#beige
+						elseif($status==1 and $status<$num_ril) {$cursez="style=\" background: #D3D3D3;";}#lightgray
+						elseif($status==2 and $status<$num_ril) {$cursez="style=\" background: #E0FFFF;";}#lightcyan
+						elseif($status==3 and $status<$num_ril) {$cursez="style=\" background: #F0E68C;";}#kaki
+						else {$sezstat=-4;$cursez="style=\" background: #DAA520;";} #gold
+						}
 				}
-			else{$cursez="style=\" background: #8CC8C7;";}
+							
+			if ($sezstat) $ops=abs($sezstat); else $ops=1;
+			
 			if(!$ops) $ops=1;
 			$res2= mysql_query("SELECT num_sez FROM ".$prefix."_ele_sezioni where id_sez='$id_sez'", $dbi);
 			list($numero_sez) = mysql_fetch_row($res2);
-#			if ($sez_num==$numero_sez) {$cursez= "style=\" border-color: YELLOW; border-width: 5px;\"";}
 			if ($sez_num==$numero_sez) {$cursez.= " border: solid; border-color: YELLOW; border-width: 4px; border-left: solid gray 4px; border-top: solid gray 4px;\"";} else $cursez.="\"";
-#			echo "\n<td align=\"center\" width=\"5%\" bgcolor=\"$bgsez\" $cursez><b><a href=\"admin.php?op=voti&amp;id_cons_gen=$id_cons_gen&amp;id_circ=$circ_id&amp;id_sede=$sede_id&amp;id_sez=$sez_id&amp;ops=$ops&amp;do=spoglio&amp;id_lista=$id_lista\">$sez_num</a></b></td>\n";
 			echo "\n<li $cursez><b><a href=\"admin.php?op=voti&amp;id_cons_gen=$id_cons_gen&amp;id_circ=$circ_id&amp;id_sede=$sede_id&amp;id_sez=$sez_id&amp;ops=$ops&amp;do=spoglio&amp;id_lista=$id_lista\">$sez_num</a></b></li>\n";
-#			if (($i%$nums) ==0) echo "</ul></div></td></tr></table>\n"; #<tr bgcolor=\"$bgcolor1\">
 		}
 		//}
 echo "</ul></div></td></tr></table>\n";		
-#	echo"</td></tr></table>";
 	//}// fine tabella per numero sezioni
-	// inizio tabella centrale
- #       echo "\n</td><td valign=\"top\" align=\"left\">";
 }
 ///////////////////////////////////////////////////////////////////////////////////
@@ -324,9 +354,5 @@
    	echo "<table border=\"0\" width=\"770\" align=\"left\"><tr>";
     	echo "\n<td bgcolor=\"$bgcolor1\" align=\"left\">";
-	//echo "<h6>";
-#		echo "\n<table align=\"left\" width=\"100%\" border=\"3\"><tr bgcolor=\"$bgcolor1\"><td>";
     		numeri_sezione(22);
-#		echo "<td></td></tr></table>\n";
-#		echo "</td></tr></table>\n";
     		echo "\n</td></tr><tr><td>\n";
 
@@ -416,56 +442,5 @@
 	///////////////////////
 	if ($do == "spoglio") {
-/*		echo "\n<form name=\"sezione\" action=\"admin.php\">";
-		OpenTable();
-		echo "<tr><td>";
-		$res= mysql_query("SELECT descrizione FROM ".$prefix."_ele_circoscrizione where id_cons='$id_cons' AND id_circ='$id_circ' order by num_circ", $dbi);
-        	list($descr_circ) = mysql_fetch_row($res);
-        	$res= mysql_query("SELECT indirizzo FROM ".$prefix."_ele_sede where id_cons='$id_cons' AND id_sede='$id_sede' order by indirizzo", $dbi);
-        	list($indir) = mysql_fetch_row($res);
-		echo "<input type=\"hidden\" name=\"pag_sez\" value=\"admin.php?op=voti&amp;do=spoglio&amp;ops=$ops&amp;id_cons_gen=$id_cons_gen&amp;id_circ=$id_circ&amp;id_sede=$id_sede&amp;id_lista=$id_lista&amp;id_sez=\">";
-		echo "<a href=\"admin.php?op=voti&amp;id_cons_gen=$id_cons_gen&amp;do=circo\">"._CIRCO."</a> $descr_circ ->
-		<a href=\"admin.php?op=voti&amp;id_cons_gen=$id_cons_gen&amp;id_circ=$id_circ&amp;do=sede\">"._INDIRIZZO."</a> $indir ->
-			<input type=\"hidden\" name=\"op\" value=\"voti\">
-		<input type=\"hidden\" name=\"do\" value=\"spoglio\">"
-	// Sezioni:lista
-	//**************************************
-		."<b>"._SEZIONE."</b>: "
-		."<select name=\"id_sez\" onChange=\"top.location.href=this.form.pag_sez.value+this.form.id_sez.options[this.form.id_sez.selectedIndex].value;return false\">";
-		$res= mysql_query("SELECT id_sez,num_sez FROM ".$prefix."_ele_sezioni where id_cons='$id_cons' and id_sede='$id_sede' order by num_sez", $dbi);
-		$rif_sez=0;
-		$next_sez=0;
-		unset($prev_sez);
-		while(list($id,$num) = mysql_fetch_row($res)) {
-			if (!($id_sez>0)) {$id_sez=$id;}
-			if ($id == $id_sez) {
-				$sel = "selected";
-				$prev_sez=$rif_sez;
-			} else {
-				$sel = "";
-				if (isset($prev_sez) and ($next_sez==0)) { $next_sez=$id;}
-				$rif_sez=$id;
-			}
-			echo "<option value=\"$id\" $sel>$num";
-		}
-		echo "</select>"
-	//*************************************
-		."<input type=\"hidden\" name=\"id_circ\" value=\"$id_circ\">\n"
-		."<input type=\"hidden\" name=\"id_cons_gen\" value=\"$id_cons_gen\">\n"
-		."<input type=\"hidden\" name=\"id_sede\" value=\"$id_sede\">\n"
-		."<input type=\"hidden\" name=\"ops\" value=\"$ops\">\n"
-		."&nbsp;&nbsp;<input type=\"submit\" name=\"update\" value=\""._OK."\">\n";
-		if ($prev_sez) {
-			echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"admin.php?op=voti&amp;id_cons_gen=$id_cons_gen&amp;do=spoglio&amp;id_circ=$id_circ&amp;id_sede=$id_sede&amp;id_sez=$prev_sez&amp;ops=$ops&amp;id_lista=$id_lista\">"._PREV."</a>&nbsp;&nbsp;&nbsp;&nbsp;\n";
-		}else {echo "      <span style=\"color: rgb(204, 204, 204);\">"._PREV."</span>      ";}
-		if ($next_sez) {
-			echo "<a href=\"admin.php?op=voti&amp;id_cons_gen=$id_cons_gen&amp;do=spoglio&amp;id_circ=$id_circ&amp;id_sede=$id_sede&amp;id_sez=$next_sez&amp;ops=$ops&amp;id_lista=$id_lista\">"._NEXT."</a>\n";
-		}else {echo "&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"color: rgb(204, 204, 204);\">"._NEXT."</span>\n";}
-     		echo "\n</td></tr>";
-		CloseTable();
-
-      		echo "</form>\n";
-    		echo "\n</td></tr><tr><td>\n";
-//    		echo "\n</td></tr></table>\n";
-*/
+
 
 	//************************************
@@ -475,6 +450,4 @@
      		list($id_cons2,$id_sez2,$id_sede,$num_sez, $maschi, $femmine) = mysql_fetch_row($result);
 
-		//$bgcolor1="#b0b0b0";
-#   		echo "<hr><table border=\"0\" width=\"60%\"  align=\"center\"><tr>";
                 echo "<hr><table><tr><td  width=\"200\"><font size =\"4\" color=\"black\" align=\"left\"><b>Sezione N. $num_sez</b></font size></td><td><table><tr>";
                 echo "\n<td bgcolor=\"$bgcolor1\" align=\"center\"><p style=\"margin-left:10px;margin-right:10px;\"><b><a href=\"admin.php?op=voti&amp;id_cons_gen=$id_cons_gen&amp;id_sez=$id_sez&amp;id_circ=$id_circ&amp;id_sede=$id_sede&amp;do=spoglio&amp;ops=1\">"._AFFLUENZE."</a></b></p></td>\n";
@@ -486,16 +459,5 @@
                 }
                 echo "</tr></table></td></tr></table><hr>\n";
-/*
-########################
-   		echo "<hr><table class=\"table-menu\"><tr>";
-    		echo "\n<td bgcolor=\"$bgcolor1\" align=\"center\"><b><a href=\"admin.php?op=voti&amp;id_cons_gen=$id_cons_gen&amp;id_sez=$id_sez&amp;id_circ=$id_circ&amp;id_sede=$id_sede&amp;do=spoglio&amp;ops=1\">"._AFFLUENZE."</a></b></td>\n";
-		if(!($genere==4) and !($votog)){ //if(!($genere==4) and !($tipo_cons==10 or $tipo_cons==11)){
-			echo "<td bgcolor=\"$bgcolor1\" align=\"center\"><b><a href=\"admin.php?op=voti&amp;id_cons_gen=$id_cons_gen&amp;id_sez=$id_sez&amp;id_circ=$id_circ&amp;id_sede=$id_sede&amp;do=spoglio&amp;ops=4\">"._GRUPPO."</a></b></td>\n";
-		}
-		if(($genere>2 or $votog)){ //if(($genere>2 or $tipo_cons==10 or $tipo_cons==11)){
-			echo "<td bgcolor=\"$bgcolor1\" align=\"center\"><b><a href=\"admin.php?op=voti&amp;id_cons_gen=$id_cons_gen&amp;id_sez=$id_sez&amp;id_circ=$id_circ&amp;id_sede=$id_sede&amp;do=spoglio&amp;ops=3\">"._PREFLISTA."</a></b></td>\n";
-		}
-    		echo "</tr></table><hr>\n"; */
-//    		echo "\n</td></tr></table>\n";
+
 #############
 # posizionamento in mancanza di scelta
@@ -510,6 +472,4 @@
 		if (!$voti) $ops=1;
 		else {
-#			$res= mysql_query("SELECT validi from ".$prefix."_ele_sezioni where id_sez='$id_sez'", $dbi);
-#        	list($voti) = mysql_fetch_row($res);
 			if($genere<4){
 				$res= mysql_query("SELECT sum(voti) from ".$prefix."_ele_voti_gruppo where id_sez='$id_sez'", $dbi);
@@ -544,9 +504,4 @@
      echo "</script>\n"; 
 		}
-	/*
-   		if ($ops == 2) {
-     			finale($id_cons,$do,$id_circ,$id_sede,$id_sez,$ops);
-   		}
-	*/
     		if ($ops == 3) {
      			preferenze($id_cons,$do,$id_circ,$id_sede,$id_sez,$ops);
@@ -584,7 +539,5 @@
 else
 	$result = mysql_query("select validi,nulli,bianchi,contestati,voti_nulli from ".$prefix."_ele_sezioni where id_cons='$id_cons' and id_sez='$id_sez' ", $dbi);
-#e
     list($validi,$nulli,$bianchi,$contestati,$votinulli) = mysql_fetch_row($result);
-#	if ($validi+$nulli+$bianchi) die("entra($genere==3) OR ($genere==5)) and !$id_lista and ($fascia>$limite)");
 		echo "<table class=\"table-menu\" style=\"width: 100%;\"><tr>";
 		$res_lis = mysql_query("SELECT id_lista, descrizione,num_lista from ".$prefix."_ele_lista where id_cons=$id_cons $circo order by num_lista",$dbi);
@@ -616,19 +569,20 @@
 			list($votisezcand)=mysql_fetch_row($result);
 			if(($fascia>$limite or !$id_conf) or $genere==4)
-                                $result = mysql_query("SELECT sum(voti) from ".$prefix."_ele_voti_lista where id_sez='$id_sez' and id_lista='$id_rif'", $dbi);
-                        else{
+                $result = mysql_query("SELECT sum(voti) from ".$prefix."_ele_voti_lista where id_sez='$id_sez' and id_lista='$id_rif'", $dbi);
+            else{
 
 				$result = mysql_query("SELECT id_gruppo from ".$prefix."_ele_lista where id_lista='$id_rif'", $dbi);
 				list($id_gruppo)=mysql_fetch_row($result);
 				$result = mysql_query("SELECT sum(voti) from ".$prefix."_ele_voti_gruppo where id_sez='$id_sez' and id_gruppo='$id_gruppo'", $dbi);
-                        }
+            }
 			list($votisezlis)=mysql_fetch_row($result);
 			if($votisezcand>($votisezlis*$numprefs))
-				$segna="style=\"background-color: #dd0000;\"";
+				{$segna="style=\"background-color: #dd0000;\"";}
 			elseif($ctrvoticand)
 				$segna="style=\"background-color: #99CC33;\"";
-			$res4= mysql_query("SELECT t1.voti FROM ".$prefix."_ele_voti_candidati as t1 left join ".$prefix."_ele_candidati as t2 on t1.id_cand=t2.id_cand where t1.id_cons='$id_cons' and  t1.id_sez='$id_sez' and t2.id_lista='$id_rif'", $dbi);
-			while(list($testvotic)=mysql_fetch_row($res4))
-				if($votisezlis<$testvotic) { $segna="style=\"background-color: #dd0000;\"";}
+			$res4= mysql_query("SELECT t1.voti,t2.num_cand FROM ".$prefix."_ele_voti_candidati as t1 left join ".$prefix."_ele_candidati as t2 on t1.id_cand=t2.id_cand where t1.id_cons='$id_cons' and  t1.id_sez='$id_sez' and t2.id_lista='$id_rif'", $dbi);
+			$errcand[$id_rif]=array();
+			while(list($testvotic,$numcand)=mysql_fetch_row($res4))
+				if($votisezlis<$testvotic) { $segna="style=\"background-color: #dd0000;\"";$errcand[$id_rif][]=$numcand;}
                         
 			$sel = ($id_rif == $id_lista) ? "selected" : "";
@@ -645,7 +599,7 @@
 		if ((!$id_lista)){$tab="_ele_voti_lista";} else {$tab="_ele_voti_candidati";}
 if(($genere==4 or ($genere==5 and $votog)) and !$id_lista)	
-		$result = mysql_query("SELECT sum(t1.voti),t2.validi, t2.solo_gruppo,t2.contestati_lista,t2.voti_nulli,t2.bianchi,t2.nulli from ".$prefix."_ele_sezioni as t2 left join ".$prefix.$tab." as t1 on (t1.id_sez=t2.id_sez) where t2.id_sez=$id_sez group by t1.id_sez",$dbi);
+		$result = mysql_query("SELECT sum(t1.voti),t2.validi, t2.solo_gruppo,t2.contestati_lista,t2.voti_nulli,t2.bianchi,t2.nulli,0,0 from ".$prefix."_ele_sezioni as t2 left join ".$prefix.$tab." as t1 on (t1.id_sez=t2.id_sez) where t2.id_sez=$id_sez group by t1.id_sez",$dbi);
 else
-		$result = mysql_query("SELECT sum(t1.voti),t2.validi_lista, t2.solo_gruppo,t2.contestati_lista,t2.voti_nulli_lista,t2.bianchi,t2.nulli,t2.contestati,t2.voti_nulli from ".$prefix."_ele_sezioni as t2 left join ".$prefix.$tab." as t1 on (t1.id_sez=t2.id_sez) where t2.id_sez=$id_sez group by t1.id_sez",$dbi);
+		$result = mysql_query("SELECT sum(t1.voti),t2.validi_lista, t2.solo_gruppo,t2.contestati_lista,t2.voti_nulli_lista,t2.bianchi,t2.nulli,t2.contestati,t2.voti_nulli from ".$prefix."_ele_sezioni as t2 left join ".$prefix.$tab." as t1 on (t1.id_sez=t2.id_sez) where t2.id_sez='$id_sez' group by t1.id_sez",$dbi);
 		list( $voti_sez, $validi2, $sg,$cont2,$vnulli2,$bia2,$nul2,$con2,$vnul2) = mysql_fetch_row($result);
 		$valista=$validi2;
@@ -664,11 +618,11 @@
 			list($votisezcand)=mysql_fetch_row($result);
 			if($fascia>$limite or !$id_conf)
-                                $result = mysql_query("SELECT sum(voti) from ".$prefix."_ele_voti_lista where id_sez='$id_sez' and id_lista='$id_lista'", $dbi);
-                        else{
+                 $result = mysql_query("SELECT sum(voti) from ".$prefix."_ele_voti_lista where id_sez='$id_sez' and id_lista='$id_lista'", $dbi);
+            else{
 
 				$result = mysql_query("SELECT id_gruppo from ".$prefix."_ele_lista where id_lista='$id_lista'", $dbi);
 				list($id_gruppo)=mysql_fetch_row($result);
 				$result = mysql_query("SELECT sum(voti) from ".$prefix."_ele_voti_gruppo where id_sez='$id_sez' and id_gruppo='$id_gruppo'", $dbi);
-                        }
+            }
 			list($votisezlis)=mysql_fetch_row($result);
 			if($votisezcand>($votisezlis*$numprefs))
@@ -730,5 +684,6 @@
 				$res = mysql_query("SELECT * FROM ".$prefix."$tab where id_cons='$id_cons' and id_sez='$id_sez' $cond_sele", $dbi);
 				$pro= mysql_fetch_array($res, 3);
-				echo "<td align=\"right\"><input  name=\"voti$i\" value=\"".$pro['voti']."\" size=\"7\"  style=\"text-align:right\">";
+				if(in_array($num_cand,$errcand[$id_lista])) $errcolor="style=\"background-color: rgb(255, 0, 0);\""; else $errcolor='';
+				echo "<td align=\"right\" $errcolor><input  name=\"voti$i\" value=\"".$pro['voti']."\" size=\"7\"  style=\"text-align:right\">";
 				echo "<input type=\"hidden\" name=\"id_cand$i\" value=\"$id_cand\"></td></tr>\n";
 				$i++;
@@ -1416,4 +1371,5 @@
 			mysql_query("delete from ".$prefix."_ele_voti_lista where id_cons='$id_cons' and id_sez='$id_sez'", $dbi);
 			mysql_query("delete from ".$prefix."_ele_voti_candidati where id_cons='$id_cons' and id_sez='$id_sez'", $dbi);
+			mysql_query("delete from ".$prefix."_ele_voti_ref where id_cons='$id_cons' and id_sez='$id_sez'", $dbi);
 			mysql_query("update  ".$prefix."_ele_sezioni set validi_lista='',contestati_lista='',voti_nulli_lista='',solo_gruppo='',solo_lista='' where id_cons='$id_cons' and id_sez='$id_sez'", $dbi);
 	
