<?
include("lb_functions.php");

//My first global vars
$global_public_car = "../public/fagiano/";

$global_preventivo_email = "info@brandfactory.it";				//	email da cui viene inviato il preventivo
$global_invio_email = "SI";									//	abilita invio email (SI, NO)
$global_nome_azienda = "Brand Factory s.r.l.";				//	nome azienda per Bolle
$global_indirizzo_azienda = "Via Dante, 13/F";				//	indirizzo azienda per Bolle
$global_citta_azienda = "52028 - Terranuova B.ni (AR)";		//	città, cap e provincia per Bolle
$global_piva_azienda = "01765840515";						//	partita iva
$global_telefono_azienda = "tel.055 9737541 / 055 9194947";	//	telefono
$global_fax_azienda = "fax.055 9738132";					//	fax
$global_paese_azienda = "Italia";							//	paese
$global_email_azienda = "m.lessi@brandfactory.it";			//	mail per invii

//Variabili globali
// Limite globale per cui lascia vedere solo i record da una certa data in poi (cartelle, preventivi, etc)
//$global_filtrodata = date ("Y-m-d", mktime (0,0,0,(date("m")-2),1,date("Y")));
$global_filtrodata = "2007-01-01";
$global_lista_account = array("Massimiliano", "Marco", "Fagiano", "Angelica", "Mauro", "Cinzia");
$global_giornoiniziofatturazione = 1;


//#################################################################################
//Questa funzione genera il codice libero per la tabella $tabella
//specificando anche in nome del campo codice
function codicelibero($tabella, $campocodice){
include 'db_conn.php';

$sql = "SELECT * FROM ".$tabella." ORDER BY ".$campocodice." DESC LIMIT 1";
$query = mysql_query($sql,$connessione) or die("Err.");
$nris = mysql_num_rows($query);
	if ($nris != 0)
	{
		while ($querytab = mysql_fetch_array($query))
		{
			$codmax = $querytab[$campocodice];
			return str_pad($codmax+1,5,"0",STR_PAD_LEFT);
		}
	}
	else {
		return "00001";
	}
}//end function

//#################################################################################
//Questa funzione genera il codice libero per la tabella $tabella
//specificando anche in nome del campo codice
function codiceliberoplus($tabella, $campocodice, $campodata, $anno){
include 'db_conn.php';

$sql = "SELECT * FROM ".$tabella." WHERE DATE_FORMAT(".$campodata.",'%Y')='".$anno."' ORDER BY ".$campocodice." DESC LIMIT 1";
$query = mysql_query($sql,$connessione) or die("Err.");
//echo $sql;
//break;
$nris = mysql_num_rows($query);
	if ($nris != 0)
	{
		while ($querytab = mysql_fetch_array($query))
		{
			$codmax = $querytab[$campocodice];
			return str_pad($codmax+1,5,"0",STR_PAD_LEFT);
		}
	}
	else {
		return "00001";
	}
}//end function



//#################################################################################
//Questa funzione restituisce il valore del campo in riferimento dell id nella tabella
function TrovaVoce($tabella,$campo_id,$valore_id,$campovoce){
include 'db_conn.php';
$sql = "SELECT * FROM $tabella WHERE $campo_id=$valore_id";
$query = mysql_query($sql,$connessione) or die("Err");
$n = mysql_num_rows($query);
	if (mysql_num_rows($query) != 0)
	{
		while ($LISTA = mysql_fetch_array($query))
		{
			$dato = $LISTA[$campovoce];
			return $dato;
		}
	}
}//end function


function Tasto($testo,$colore,$class,$width,$link,$target)
{
	if($colore==""){$colore="grigio";}
	if($class==""){$class="titolobianco";}
	//if($width==0){$width=250;}
	$risultante = '<table cellpadding="0" cellspacing="0"';
	if($width!=0) $risultante .= ' width="'.$width.'"';
	$risultante .= '>
	
	<tr><td width="18" align="left"><img src="../graph/'.$colore.'_01.gif" width="18" height="40"></td>
	<td align="center" valign="middle" background="../graph/'.$colore.'_02.gif" class="'.$class.'">';
	if($link!=""){$risultante .= '<a href="'.$link.'" target="'.$target.'" class="'.$class.'">';}
	$risultante .= $testo;
	if($link!=""){$risultante .= '</a>';}
	$risultante .= '</td>
	<td width="18" align="right"><img src="../graph/'.$colore.'_03.gif" width="18" height="40"></td></tr>
	</table>';
	return $risultante;
}



//All this function are created by Madde Gianluca Ventani - feb 2005

// Da una data in ingresso la formatta secondo i criteri del PHP date()
function mydatetime($format,$datetime)
{
	if($datetime == "" || $datetime == "0000-00-00 00:00:00"){
		return "";
	}
	else {
		//$datetime = "2004-07-30 12:34:56";
		list($anno, $mese, $giorno, $ore, $minuti, $secondi) = split("[-/:. ]",$datetime);
		//-----------------------------------ore---minuti--secondi-mese---giorno--anno----------------------------;
		$newdate = date($format,mktime($ore,$minuti,$secondi,$mese,$giorno,$anno));
		return $newdate;
	}
} //end function



// Da una data in ingresso la formatta secondo i criteri del PHP date()
function mydata($data,$format)
{
	if($data == "" || $data == "0000-00-00"){
		echo "non disponibile";
	}
	else {
		//$data = "2004-07-30";
		$explose = explode("-",$data);
		//------------------------------------mese--------giorno------anno--------;
		$newdate = date($format,mktime(0,0,0,$explose[1],$explose[2],$explose[0]));
		echo "$newdate";
	}
} //end function

function mydata2($data,$format)
{
	if($data == "" || $data == "0000-00-00"){
		return "";
	}
	else {
		$explose = explode("-",$data);
		//------------------------------------mese--------giorno------anno--------;
		$newdate = date($format,mktime(0,0,0,$explose[1],$explose[2],$explose[0]));
		return "$newdate";
	}
} //end function

function dow($data)
{
		$newdate = explode("-",$data);
		$gds = date("w", mktime(0,0,0,$newdate[1],$newdate[2],$newdate[0]));
			switch ($gds['w']) {
					case 0:
						return "domenica";
					break;
					case 1:
						return "lunedì";
					break;
					case 2:
						return "martedì";
					break;
					case 3:
						return "mercoledì";
					break;
					case 4:
						return "giovedì";
					break;
					case 5:
						return "venerdì";
					break;
					case 6:
						return "sabato";
					break;
			}
}

















function mow($data)
{
	if($data == "" || substr($data,0,10) == "0000-00-00"){
		return "";
	}else {
		$newdate = explode("-",$data);
		$gds = date("m", mktime(0,0,0,$newdate[1],$newdate[2],$newdate[0]));
		switch ($gds) {
				case '01':
					return "gennaio";
				break;
				case '02':
					return "febbraio";
				break;
				case '03':
					return "marzo";
				break;
				case '04':
					return "aprile";
				break;
				case '05':
					return "maggio";
				break;
				case '06':
					return "giugno";
				break;
				case '07':
					return "luglio";
				break;
				case '08':
					return "agosto";
				break;
				case '09':
					return "settembre";
				break;
				case '10':
					return "ottobre";
				break;
				case '11':
					return "novembre";
				break;
				case '12':
					return "dicembre";
				break;
		} // end switch
	} // end if
} // end function





function mese($numero){
	if($numero=="" || $numero==0){
		$numero=date("n");
	}
	$ARR_mesi = array("Mesi","Gennaio","Febbraio","Marzo","Aprile","Maggio","Giugno","Luglio","Agosto","Settembre","Ottobre","Novembre","Dicembre");
	return $ARR_mesi[$numero];
} //end function





function crealistamesi($nome_select,$ricerca){
	$arr_mesi = array('mesi','gennaio','febbraio','marzo','aprile','maggio','giugno','luglio','agosto','settembre','ottobre','novembre','dicembre');
	if($ricerca !="" && $ricerca>0){
		$ricerca = intval($ricerca);
	}else{
		$ricerca = date("n");
	}
?>
	<select name="<?=$nome_select?>" size="1">
<?		for($i=1; $i<13; $i++){?>
		<option value="<?=$i?>"<?if ($ricerca==$i) { echo " selected"; }?>><?echo $arr_mesi[$i]?></option>
<?		}?>
	</select>
<?
} // end function


















function crealistaanni($nome_select,$ricerca){
	$anno_attuale = date("Y");
	$daanno = 2007;
	$aanno = $anno_attuale + 5;
	if($ricerca !="" && $ricerca>0){
		$ricerca = intval($ricerca);
	}else{
		$ricerca = date("Y");
	}
?>
	<select name="<?=$nome_select?>" size="1">
<?		for($i=$daanno; $i<$aanno; $i++){?>
		<option value="<?=$i?>"<?if ($ricerca==$i) { echo " selected"; }?>><?echo $i?></option>
<?		}?>
	</select>
<?
} // end function





















//###################################################################################
function oreminuti($prefisso,$modora)
{
	if($modora != "")
	{
		$explose = explode(":",$modora);
		$ore = $explose[0];
		$min = $explose[1];
	}
?>
<!-- ORE -->
	<select name="<?=$prefisso?>ore" size="1">
<?		for($o=1; $o<25; $o++)
		{?>
		<option value="<?echo $o?>"<?if ($ore==$o) { echo " SELECTED"; }?>><?echo $o?></option>
<?		}?>
	</select>
	&nbsp;&nbsp;
<!-- MINUTI -->
	<select name="<?=$prefisso?>minuti" size="1">
<?		for($m=0; $m<60; $m++)
		{
		if(is_double($m/5)!=true)
			{?>
		<option value="<?echo $m?>"<?if ($min==$m) { echo " SELECTED"; }?>><?echo str_pad($m,2,"0",STR_PAD_LEFT)?></option>
<?			}
		}?>
	</select>
<?
}//end function

//####################################################################################
//Questa funzione mi genera tre campi di tipo <select option>
//per l'inserimento della data. Utilizza il prefisso per definire
//il nome dei campi (es."da" daanno damese dagiorno etc.)
//se esiste il campo moddata allora allinea la data in ingresso
//attivando le SELECTED ad ogni campo corrispondente
function tridate($prefisso,$moddata)
{
	if($moddata != "")
	{
		$explose = explode("-",$moddata);
		$giorno = intval($explose[2]);
		$mese = intval($explose[1]);
		$anno = intval($explose[0]);
	}
?>
	<!-- Giorno: -->
	<select name="<?=$prefisso?>giorno" class="form0" size="1">
<?		for($g=1; $g<32; $g++)
		{?>
		<option value="<?echo $g?>"<?if ($giorno==$g) { echo " SELECTED"; }?>><?echo $g?></option>
<?		}?>
	</select>
	&nbsp;&nbsp;
	<!-- Mese: -->
		<select name="<?=$prefisso?>mese" class="form0" size="1">
		<option value="01"<?if (intval($mese)=="01") { echo " SELECTED"; }?>>Gennaio</option>
		<option value="02"<?if (intval($mese)=="02") { echo " SELECTED"; }?>>Febbraio</option>
		<option value="03"<?if (intval($mese)=="03") { echo " SELECTED"; }?>>Marzo</option>
		<option value="04"<?if (intval($mese)=="04") { echo " SELECTED"; }?>>Aprile</option>
		<option value="05"<?if (intval($mese)=="05") { echo " SELECTED"; }?>>Maggio</option>
		<option value="06"<?if (intval($mese)=="06") { echo " SELECTED"; }?>>Giugno</option>
		<option value="07"<?if (intval($mese)=="07") { echo " SELECTED"; }?>>Luglio</option>
		<option value="08"<?if (intval($mese)=="08") { echo " SELECTED"; }?>>Agosto</option>
		<option value="09"<?if (intval($mese)=="09") { echo " SELECTED"; }?>>Settembre</option>
		<option value="10"<?if (intval($mese)=="10") { echo " SELECTED"; }?>>Ottobre</option>
		<option value="11"<?if (intval($mese)=="11") { echo " SELECTED"; }?>>Novembre</option>
		<option value="12"<?if (intval($mese)=="12") { echo " SELECTED"; }?>>Dicembre</option>
	</select>
	&nbsp;&nbsp;
	<!-- Anno: -->
	<select name="<?=$prefisso?>anno" size="1" class="form0">
<?
		for($a=2004; $a<2025; $a++)
		{?>
		<option value="<?echo $a?>"<?if ($anno==$a) { echo " SELECTED"; }?>><?echo $a?></option>
<?
		}?>
</select>
<?
} //end function





//####################################################################################
//Questa funzione mi genera due campi di tipo <select option>
//per l'inserimento della data. Utilizza il prefisso per definire
//il nome dei campi (es."da" daanno damese etc.)
//se esiste il campo moddata allora allinea la data in ingresso
//attivando le SELECTED ad ogni campo corrispondente
function select_meseanno($prefisso,$moddata)
{
	if($moddata != "")
	{
		$explose = explode("-",$moddata);
		$giorno = intval($explose[2]);
		$mese = intval($explose[1]);
		$anno = intval($explose[0]);
	}
?>
	<!-- Mese: -->
		<select name="<?=$prefisso?>mese" class="form0" size="1">
		<option value="01"<?if (intval($mese)=="01") { echo " SELECTED"; }?>>Gennaio</option>
		<option value="02"<?if (intval($mese)=="02") { echo " SELECTED"; }?>>Febbraio</option>
		<option value="03"<?if (intval($mese)=="03") { echo " SELECTED"; }?>>Marzo</option>
		<option value="04"<?if (intval($mese)=="04") { echo " SELECTED"; }?>>Aprile</option>
		<option value="05"<?if (intval($mese)=="05") { echo " SELECTED"; }?>>Maggio</option>
		<option value="06"<?if (intval($mese)=="06") { echo " SELECTED"; }?>>Giugno</option>
		<option value="07"<?if (intval($mese)=="07") { echo " SELECTED"; }?>>Luglio</option>
		<option value="08"<?if (intval($mese)=="08") { echo " SELECTED"; }?>>Agosto</option>
		<option value="09"<?if (intval($mese)=="09") { echo " SELECTED"; }?>>Settembre</option>
		<option value="10"<?if (intval($mese)=="10") { echo " SELECTED"; }?>>Ottobre</option>
		<option value="11"<?if (intval($mese)=="11") { echo " SELECTED"; }?>>Novembre</option>
		<option value="12"<?if (intval($mese)=="12") { echo " SELECTED"; }?>>Dicembre</option>
	</select>
	&nbsp;&nbsp;
	<!-- Anno: -->
	<select name="<?=$prefisso?>anno" size="1" class="form0">
<?
		for($a=2004; $a<2025; $a++)
		{?>
		<option value="<?echo $a?>"<?if ($anno==$a) { echo " SELECTED"; }?>><?echo $a?></option>
<?
		}?>
</select>
<?
} //end function



//####################################################################################
//Questa funzione mi genera una select con i mesi.
//per l'inserimento della data. Utilizza il prefisso per definire
//il nome dei campi (es."da" daanno damese etc.)
//se esiste il campo moddata allora allinea la data in ingresso
//attivando le SELECTED ad ogni campo corrispondente
function select_mese($prefisso,$modmese)
{
	$mese="";
	if($modmese=="attuale"){$modmese=date("n");}
	if($modmese==""){$modmese="";}
	$mese=$modmese;?>
	<select name="<?=$prefisso?>mese" class="form0" size="1">
		<option value="">Seleziona... </option>
		<option value="01"<?if (intval($mese)=="01") { echo " SELECTED"; }?>>Gennaio</option>
		<option value="02"<?if (intval($mese)=="02") { echo " SELECTED"; }?>>Febbraio</option>
		<option value="03"<?if (intval($mese)=="03") { echo " SELECTED"; }?>>Marzo</option>
		<option value="04"<?if (intval($mese)=="04") { echo " SELECTED"; }?>>Aprile</option>
		<option value="05"<?if (intval($mese)=="05") { echo " SELECTED"; }?>>Maggio</option>
		<option value="06"<?if (intval($mese)=="06") { echo " SELECTED"; }?>>Giugno</option>
		<option value="07"<?if (intval($mese)=="07") { echo " SELECTED"; }?>>Luglio</option>
		<option value="08"<?if (intval($mese)=="08") { echo " SELECTED"; }?>>Agosto</option>
		<option value="09"<?if (intval($mese)=="09") { echo " SELECTED"; }?>>Settembre</option>
		<option value="10"<?if (intval($mese)=="10") { echo " SELECTED"; }?>>Ottobre</option>
		<option value="11"<?if (intval($mese)=="11") { echo " SELECTED"; }?>>Novembre</option>
		<option value="12"<?if (intval($mese)=="12") { echo " SELECTED"; }?>>Dicembre</option>
	</select>
<?} //end function




//####################################################################################
//Questa funzione mi genera due campi di tipo <select option>
//per l'inserimento della data. Utilizza il prefisso per definire
//il nome dei campi (es."da" daanno damese etc.)
//se esiste il campo moddata allora allinea la data in ingresso
//attivando le SELECTED ad ogni campo corrispondente
function select_anno($prefisso,$moddata)
{
	if($moddata != "")
	{
		$explose = explode("-",$moddata);
		$giorno = intval($explose[2]);
		$mese = intval($explose[1]);
		$anno = intval($explose[0]);
	}
?>
	<!-- Anno: -->
	<select name="<?=$prefisso?>anno" size="1" class="form0"><option value="">Seleziona...</option>
<?
		for($a=2004; $a<2025; $a++)
		{?>
		<option value="<?echo $a?>"<?if ($anno==$a) { echo " SELECTED"; }?>><?echo $a?></option>
<?
		}?>
</select>
<?
} //end function





//#################################################################################
//Questa funzione controlla che esista già un codice immobile passato come input
function codexist($tabella,$campo,$codice){
include 'db_conn.php';
$sql = "SELECT * FROM $tabella WHERE $campo LIKE '$codice'";
$query = mysql_query($sql,$connessione) or die("Err.");
$n = mysql_num_rows($query);
	if ($n != 0) {
		return true;
	}
	else {
		return false;
	}
}//end function





//#################################################################################
//Questa funzione controlla che esista già un codice immobile passato come input
function codexistplus($tabella,$campo,$codice,$campodata,$anno){
include 'db_conn.php';
$sql = "SELECT * FROM $tabella WHERE DATE_FORMAT(".$campodata.",'%Y')=".$anno." AND ".$campo." = '".$codice."'";
$query = mysql_query($sql,$connessione) or die("Err.");
$n = mysql_num_rows($query);
	if ($n != 0) {
		return true;
	}
	else {
		return false;
	}
}//end function


//####################################################################################
// questa funzione mi crea una lista dei campi da una tabella definita dalla variabile
// $tabella e crea un tag <SELECT OPTION>con nome definito da $nomeselect
// con il testo $campo e valore $id mettendo il SELECT nel campo definito dalla variabile $id
function crealistatabella($nomeselect,$tabella,$campo1,$campo2,$id,$ricerca_id){
include 'db_conn.php';
echo "<select name=\"".$nomeselect."\" class=\"form0\">\n";
if($ricerca_id=="Ricerca"){echo "<option value=\"all\">Tutti";}
$sql = "SELECT * FROM ".$tabella." ORDER BY ".$campo1;
if ($campo2 != "") { $sql .= ",".$campo2; }
$query = mysql_query($sql,$connessione) or die("Err. $tabella");
$nris = mysql_num_rows($query);
	if ($nris != 0)
	{
		echo "<option value=''> seleziona... </option>";
		while ($querytab = mysql_fetch_array($query))
		{
			$testo1 = $querytab[$campo1];
			if ($campo2 != "") {
				$testo2 = " ".$querytab[$campo2];
			}else{
				$testo2 = " ";
			}
			$value = $querytab[$id];
			echo "<option value=\"".$value."\" ";
			if ($ricerca_id!="") {
				if($value == $ricerca_id) echo "selected";
			}
			echo ">".lb_charcontrol($testo1.$testo2)."</option>\n";
		}
	}
	else {
		echo "<option value=\"0\">-no data-</option>";
	}
echo "</select>\n\n";


} //end function






//####################################################################################
// questa funzione mi crea una lista dei campi da una tabella definita dalla variabile
// $tabella e crea un tag <SELECT OPTION>con nome definito da $nomeselect
// con il testo $campo e valore $id mettendo il SELECT nel campo definito dalla variabile $id

function crealistatabella2($nomeselect,$tabella,$campo1,$campo2,$id,$chk_ricerca,$ricerca_id){
include 'db_conn.php';
echo "<select name=\"".$nomeselect."\" class=\"";
if($chk_ricerca=="Selezionare"){
	echo "form1";
}else{
	echo "form0";
}
echo "\">\n";
if($chk_ricerca=="Ricerca"){echo "<option value=\"all\">Tutti";}
if($chk_ricerca=="Selezionare"){echo "<option value=\"select\">Selezionare";}
$sql = "SELECT * FROM ".$tabella." ORDER BY ".$campo1;
if ($campo2 != "") { $sql .= ",".$campo2; }
$query = mysql_query($sql,$connessione) or die("Err. $tabella");
$nris = mysql_num_rows($query);
	if ($nris != 0)
	{
		while ($querytab = mysql_fetch_array($query))
		{
			$testo1 = $querytab[$campo1];
			if ($campo2 != "") {
				$testo2 = " ".$querytab[$campo2];
			}else{
				$testo2 = " ";
			}
			$value = $querytab[$id];
			echo "<option value=\"".$value."\" ";
			if ($ricerca_id!="" && $ricerca_id>0) {
				if($value == $ricerca_id) echo "selected";
			}
			echo " >".$testo1."/".$testo2."\n";
		}
	}
	else {
		echo "<option value=\"0\">-no data-\n";
	}
echo "</select>\n\n";
} //end function












//####################################################################################
// questa funzione mi crea una lista dei campi da una tabella definita dalla variabile
// $tabella e crea un tag <SELECT OPTION>con nome definito da $nomeselect
// con il testo $campo e valore $id mettendo il SELECT nel campo definito dalla variabile $id
// aggiungo la variabile CHR per sapere quanti caratteri visualizzare
function crealistatabella3($nomeselect,$tabella,$campo1,$campo2,$id,$chk_ricerca,$ricerca_id,$chr1,$chr2){
include 'db_conn.php';
echo "<select name=\"".$nomeselect."\" class=\"form0\">\n";
if($chk_ricerca=="Ricerca"){echo "<option value=\"all\">Tutti";}
$sql = "SELECT * FROM ".$tabella." ORDER BY ".$campo1;
if ($campo2 != "") { $sql .= ",".$campo2; }
$query = mysql_query($sql,$connessione) or die("Err. $tabella");
$nris = mysql_num_rows($query);
	if ($nris != 0)
	{
		while ($querytab = mysql_fetch_array($query))
		{
					if($chr1!=""){
						$testo1 = substr($querytab[$campo1], 0, $chr1);
					}else{
						$testo1 = $querytab[$campo1];
					}
					
			if ($campo2 != "") {
				if($chr2!=""){
					$testo2 = " ".substr($querytab[$campo2], 0, $chr2);
				}else{
					$testo2 = " ".$querytab[$campo2];
				}
			}else{
				$testo2 = " ";
			}
			
			$value = $querytab[$id];
			echo "<option value=\"".$value."\" ";
			if ($ricerca_id!="" && $ricerca_id>0) {
				if($value == $ricerca_id) echo "selected";
			}
			echo " >".$testo1."/".$testo2."\n";
		}
	}
	else {
		echo "<option value=\"0\">-no data-\n";
	}
echo "</select>\n\n";
} //end function


















function controllo_accesso_gestionale() {
	if(!isset($_SESSION['log_stato']))
	{
		$_SESSION['log_stato'] = "no";
		$_SESSION['log_permesso'] = "";
		header("Location: login.php");
		exit;
	}
	else
	{
		if ($_SESSION['log_stato'] != "ok")
		{
			$_SESSION['log_stato'] = "no";
			$_SESSION['log_permesso'] = "";
			header("Location: login.php");
			exit;
		}
	}
}

//restituisce :
//	-2 se è anteriore al range
//	-1 se è uguale alla data start
//	0 se è compresa tra start e end
//	1 se è uguale alla data end
//	2 se è posteriore al range
function check2date($start_date,$end_date,$date){
if($date > $start_date){
	if($date < $end_date){
		return 0;
	}
	else {
		if($date == $end_date){
			return 1;
		}
		else {
			return 2;
		}
	}
}
else {
	if($date == $start_date){
		return -1;
	}
	else {
		return -2;
	}
}
/*
$expl_start_date = explode("-",$start_date);
$expl_end_date = explode("-",$end_date);
$expl_date = explode("-",$date);

if($expl_date[0] > $expl_start_date[0] && $expl_date[1] > $expl_start_date[1] && $expl_date[2] > $expl_start_date[2]){
	
}
*/
}//end function




function viewImg($foto,$miniatura,$maxX,$maxY,$text)
{
	//E' una immagine o un file qualsiasi?
	$estFoto = strtolower(substr($foto,-3,3));
	$estMiniatura = strtolower(substr($miniatura,-3,3));
	
	if($estFoto=="jpg" || $estFoto=="gif" || $estFoto=="png" || $estMiniatura=="jpg" || $estMiniatura=="gif" || $estMiniatura=="png")
	{
	//E' una immagine
	
	if(file_exists($foto) || file_exists($miniatura)){
			//dimensione
			if(file_exists($foto)){
				$dim = getimagesize($foto);
				$dimX = $dim[0];
				$dimY = $dim[1];
			}else{
				if(file_exists($miniatura)) {
					$dim = getimagesize($miniatura);
					$dimX = $dim[0];
					$dimY = $dim[1];
				}
			}
			
			if($maxX!="" && $maxX!=0 && $maxY!="" && $maxY!=0){
				$DR = $dimX/$dimY;
				$DM = $maxX/$maxY;
				//orientamento
				if($DR >= 1) {
					// se orizzontale
					$Orientation = 'width="'.$maxX.'" alt="['.$DR.'/'.$DM.']"';
				}else{
					//se verticale
					$Orientation = 'height="'.$maxY.'" alt="['.$DR.'/'.$DM.']"';
				}
			}else{
				$Orientation="";
			}
		}

		if($miniatura!="" && file_exists($miniatura))
		{
			//La miniatura esiste
			if($foto!="" && file_exists($foto))
			{
				//Esiste tutto, foto e miniatura
				echo '<a href="JavaScript:zoom(\''.$foto.'\','.$dimX.','.$dimY.');">';
				echo '<img title="'.$text.'" alt="'.$text.'" border="0" src="'.$miniatura.'" '.$Orientation.'>';
				echo '</a>';
			}else{
				//Esiste solo la miniatura
				echo '<img title="'.$text.'" alt="'.$text.'" border="0" src="'.$miniatura.'" '.$Orientation.'>';
			}//end if foto
		}else{
			//La miniatura non esiste
			if($foto!="" && file_exists($foto))
			{
				//Esiste solo la foto
				echo '<a href="JavaScript:zoom(\''.$foto.'\','.$dimX.','.$dimY.');">';
				echo '<img title="'.$text.'" alt="'.$text.'" border="0" src="'.$foto.'" '.$Orientation.'>';
				echo '</a>';
			}else{
				//Non esiste niente, scrive il testo alternativo
				echo $text;
			}//end if foto
		}//end if miniatura
	}else{
		//Non è una immagine, quindi viene trattato come file
		echo '<a href="'.$foto.'">'.$text.'</a>';
	}
}//end functions


































function viewImg2($foto,$miniatura,$maxX,$maxY,$text){
$width = $maxX;
$height = $maxY;
			if($miniatura!="" && file_exists($miniatura) && !is_dir($miniatura)){
				$msize = getimagesize($miniatura);
				$mL = $msize[0];
				$mA = $msize[1];
				if($mL/$mA >= 1) {
					// se orizzontale width=200
					$mOrientation = 'width="'.$width.'"';
				}else{
					//se verticale height=200
					$mOrientation = 'height="'.$height.'"';
				}
			}
			
			
			if($foto!="" && file_exists($foto) && !is_dir($foto)){
				$size = getimagesize($foto);
				$L = $size[0];
				$A = $size[1];
				
				if($L/$A >= 1) {
					// se orizzontale width=200
					$Orientation = 'width="'.$width.'"';
				}else{
					//se verticale height=200
					$Orientation = 'height="'.$height.'"';
				}
			//Solo se esiste lo zoom, genero il DIV con lo zoom
			$linkZoom = '<a href="JavaScript:zoom(\''.$foto.'\','.$L.','.$A.');">';
			}

	
			//Indifferentemente se esiste la miniatura genero solo il DIV senza zoom
			$linkNoZoom ='<a href="#">';
	

			if($miniatura!="" && file_exists($miniatura) && !is_dir($miniatura)){
				if($foto!="" && file_exists($foto) && !is_dir($foto)){
					//ESISTE SIA LA MINIATURA CHE LO ZOOM
					if(isset($linkZoom) && $linkZoom!="") echo $linkZoom;
					echo '<img class="foto" border="0" src="'.$miniatura.'" ';
					echo $mOrientation;
					echo '>';
					if(isset($linkZoom) && $linkZoom!="") echo '</a>';
				}else{
					//ESISTE SOLO LA MINIATURA
					if(isset($linkNoZoom) && $linkNoZoom!="") echo $linkNoZoom;
					echo '<img class="foto" border="0" src="'.$miniatura.'" ';
					echo $mOrientation;
					echo '>';
					if(isset($linkNoZoom) && $linkNoZoom!="") echo '</a>';
				}
			}else{
				if($foto!="" && file_exists($foto) && !is_dir($foto)){
					//ESISTE SOLO LO ZOOM
					if(isset($linkZoom) && $linkZoom!="") echo $linkZoom;
					echo '<img class="foto" border="0" src="'.$foto.'" ';
					echo $Orientation;
					echo '>';
					if(isset($linkZoom) && $linkZoom!="") echo '</a>';
				}else{
					//NON ESISTE NIENTE, NE MINIATURA NE ZOOM
					if(isset($linkNoZoom) && $linkNoZoom!="") echo $linkNoZoom;
					echo '<img class="foto" border="0" src="../graph/no_foto.gif" ';
					echo $Orientation;
					echo '>';
					if(isset($linkNoZoom) && $linkNoZoom!="") echo '</a>';
				}
			}
}






























//#################################################################################
//Questa funzione restituisce il valore del campo in riferimento dell id nella tabella
function ContaRecord($tabella,$rif_id,$valore_id){
include 'db_conn.php';
$sql = "SELECT * FROM $tabella WHERE $rif_id=$valore_id";
$query = mysql_query($sql,$connessione) or die("Err");
$n = mysql_num_rows($query);
	return $n;
}//end function













//Calcola la percentuale dati due valori in ingresso
//ES: 7,6-10 (valore-valoremax) -> restituisce un numero senza %
function Percentualizza($val,$max){
	return intval(($val*100)/$max);
}

/** FUNZIONI DI SEGUITO BY RICCARDO **/

// Lista dei clienti per inserimento dei report delle ore lavorative
function crealistatabella4($nomeselect,$tabella,$campo1,$campo2,$id,$chk_ricerca,$ricerca_id, $data_report){
include 'db_conn.php';
echo "<select onChange=\"cambia_cliente_report('$data_report');\" id=\"$nomeselect\" name=\"".$nomeselect."\" class=\"";
if($chk_ricerca=="Selezionare"){
	echo "form1";
}else{
	echo "form0";
}
echo "\">\n";
if($chk_ricerca=="Ricerca"){echo "<option value=\"all\">Tutti";}
if($chk_ricerca=="Selezionare"){echo "<option value=\"0\">Selezionare";}
$sql = "SELECT * FROM ".$tabella." ORDER BY ".$campo1;
if ($campo2 != "") { $sql .= ",".$campo2; }
$query = mysql_query($sql,$connessione) or die("Err. $tabella");
$nris = mysql_num_rows($query);
	if ($nris != 0)
	{
		while ($querytab = mysql_fetch_array($query))
		{
			$testo1 = $querytab[$campo1];
			if ($campo2 != "") {
				$testo2 = " ".$querytab[$campo2];
			}else{
				$testo2 = " ";
			}
			$value = $querytab[$id];
			echo "<option value=\"".$value."\" ";
			if ($ricerca_id!="" && $ricerca_id>0) {
				if($value == $ricerca_id) echo "selected";
			}
			echo " >".$testo1."/".$testo2."\n";
		}
	}
	else {
		echo "<option value=\"0\">-no data-\n";
	}
echo "</select>\n\n";
} //end function

function crealistalavori($id_cliente, $rif_id_cartella = NULL)
{
	include 'db_conn.php';
	$sql = "SELECT * FROM cartelle_fagiano WHERE rif_cliente_cartella = $id_cliente AND chiusa_cartella = 0  ORDER BY nomelavoro_cartella";
	$query = mysql_query($sql, $connessione) or die(mysql_error());
	echo '<select class="form1" id="rif_cartella_report" name="rif_cartella_report"  >';
	while ($row = mysql_fetch_array($query))
	{
		$id_cartella = $row["id_cartella"];
		$nome_cartella = $row["nomelavoro_cartella"];
		echo '<option value="'.$id_cartella.'" ';
		if (isset($rif_id_cartella)) {
			if ($rif_id_cartella == $id_cartella)
				echo 'selected="selected" ';
		}
		echo '>'.$nome_cartella.'</option>';
	}
	echo '<option value="0">Altro</option></select>';
	
} // end function

function crealistaore($name, $rif_ora = NULL)
{
	echo '<select class="form1" name="'.$name.'" id="'.$name.'" >';
	for ($i=8; $i<24; $i++)
	{
		for ($j=0; $j<60; $j=$j+15)
		{
			$ora = $i;
			$minuti = $j;
			if (strlen($ora) == 1)
				$ora = "0".$ora;
			if (strlen($minuti) == 1)
				$minuti = "0".$minuti;

			echo '<option ';
			if ($rif_ora!=NULL)
			{
				$tmp_ora = $ora.":".$minuti;
				if ($tmp_ora == $rif_ora)
					echo 'selected="selected" ';
			}
			
			echo 'value="'.$ora.':'.$minuti.'">'.$ora.':'.$minuti.'</option>';
		}
	}
	
	echo '</select>';
} // end function

function crealistasigle($name, $sigla = NULL)
{
	if (isset($sigla)) 
	{
		$F = $G = $C = $R = "";
		if ($sigla == "F") $F = 'selected="selected"';
		if ($sigla == "G") $G = 'selected="selected"';
		if ($sigla == "C") $C = 'selected="selected"';
		if ($sigla == "R") $R = 'selected="selected"';
	}
	echo '<select class="form1" name="'.$name.'" id="'.$name.'" >';
	echo '<option '.$F.' value="F">F</option>
				<option '.$G.' value="G">G</option>
				<option '.$C.' value="C">C</option>
				<option '.$R.' value="R">R</option>';
	echo '</select>';
} // end function

function calcolatotaleore ($start, $end)
{
	$part = explode(":", $start);
	$arr = explode(":", $end);
	$diff = mktime($arr[0], $arr[1], 0,1,1,2004) - mktime($part[0], $part[1],0,1,1,2004);
	$ore=floor($diff / (60*60));  
	$minuti=($diff / 60) % 60;
	
	$ore     = str_pad($ore,2,0,STR_PAD_LEFT);
	$minuti  = str_pad($minuti,2,0,STR_PAD_LEFT);
	
	$minuti_in_ore = round($minuti / 60, 2);
	//$tempo = $ore.":".$minuti; // 1:15
	$tempo = (float)$ore + (float)$minuti_in_ore;
	return $tempo;
} // end function

function crealistaclienti ($rif_cliente) {
	include 'db_conn.php';
	echo '<select class="form1" name="rif_cliente_cartella" id="rif_cliente_cartella">
					<option value="0">Selezionare</option>';
	$sql = "SELECT *
					FROM clienti_fagiano 
					ORDER BY nome_cliente";
	$query = mysql_query($sql, $connessione) or die(mysql_error());
	while ($row = mysql_fetch_array($query))
	{
		$id_cliente = $row["id_cliente"];
		$nome_cliente = $row["nome_cliente"];
		echo '<option value="'.$id_cliente.'" ';
		if ($rif_cliente == $id_cliente)
			echo 'selected="selected" ';
		echo '>'.$nome_cliente.'</option>';	
	}
	
	echo "</select>";
} // end function

function cercanomecliente ($rif_cliente)
{
	include 'db_conn.php';
	$sql = "SELECT nome_cliente 
					FROM clienti_fagiano 
					WHERE id_cliente = $rif_cliente LIMIT 1";
	$query = mysql_query($sql, $connessione) or die(mysql_error());
	$row = mysql_fetch_array($query);
	return $row["nome_cliente"];
} // end function

function cercaacronimocliente ($rif_cliente)
{
	include 'db_conn.php';
	$sql = "SELECT acronimo_cliente 
					FROM clienti_fagiano 
					WHERE id_cliente = $rif_cliente LIMIT 1";
	$query = mysql_query($sql, $connessione) or die(mysql_error());
	$row = mysql_fetch_array($query);
	return $row["acronimo_cliente"];
} // end function

function cercanomelavoro ($rif_lavoro)
{
	include 'db_conn.php';
	$sql = "SELECT nomelavoro_cartella 
					FROM cartelle_fagiano 
					WHERE id_cartella = $rif_lavoro LIMIT 1";
	$query = mysql_query($sql, $connessione) or die(mysql_error());
	$row = mysql_fetch_array($query);
	if ($rif_lavoro != 0)
		return $row["nomelavoro_cartella"];
	else
		return "Altro";
	
} // end function

/**** FILTRAGGIO ****/
function crealistalavoratorifiltri () {
	include 'db_conn.php';
	echo '<select class="form0" name="rif_user_report" id="rif_user_report">
					<option value="0">Tutti</option>';
	$sql = "SELECT *
					FROM users_fagiano 
					ORDER BY nome_user";
	$query = mysql_query($sql, $connessione) or die(mysql_error());
	while ($row = mysql_fetch_array($query))
	{
		$id_user = $row["id_user"];
		$nome_user = $row["nome_user"];
		echo '<option value="'.$id_user.'">'.$nome_user.'</option>';
	}
	echo "</select>";
} // end function

function crealistaclientifiltri () {
	include 'db_conn.php';
	echo '<select class="form0" name="rif_cliente_cartella" id="rif_cliente_cartella">
					<option value="0">Tutti</option>';
	$sql = "SELECT *
					FROM clienti_fagiano 
					ORDER BY acronimo_cliente";
	$query = mysql_query($sql, $connessione) or die(mysql_error());
	while ($row = mysql_fetch_array($query))
	{
		$id_cliente = $row["id_cliente"];
		$acronimo_cliente = $row["acronimo_cliente"];
		$nome_cliente = stripslashes($row["nome_cliente"]);
		echo '<option value="'.$id_cliente.'">'.$acronimo_cliente.'/'.$nome_cliente.'</option>';	
	}
	echo "</select>";
} // end function


function crealistalavorifiltri()
{
	include 'db_conn.php';
	$sql = "SELECT * FROM cartelle_fagiano A, clienti_fagiano B WHERE A.chiusa_cartella = 0 AND A.rif_cliente_cartella = B.id_cliente ORDER BY B.acronimo_cliente, A.nomelavoro_cartella";
	$query = mysql_query($sql, $connessione) or die(mysql_error());
	echo '<select class="form0" id="rif_cartella_report" name="rif_cartella_report"  >';
	echo '<option value="-1">Tutti</option>';
	while ($row = mysql_fetch_array($query))
	{
		$id_cartella = $row["id_cartella"];
		$nome_cartella = $row["nomelavoro_cartella"];
		$acronimo_cliente = $row["acronimo_cliente"];
		echo '<option value="'.$id_cartella.'">'.$acronimo_cliente.'/'.$nome_cartella.'</option>';
	}
	echo '<option value="0">Altro</option></select>';
	
} // end function

function crealistaperiodifiltri()
{
	echo '<select class="form0" id="periodo_report" name="periodo_report">
					<option value="0">Tutti</option>
					<option value="7">Ultima Settimana</option>
					<option value="14">Ultime 2 Settimane</option>
					<option value="31">Ultimo Mese</option>
					<option value="62">Ultimi 2 Mesi</option>
					<option value="93">Ultimi 3 Mesi</option>
					<option value="186">Ultimi 6 Mesi</option>
					<option value="365">Ultimo Anno</option>
				</select>';
}

// Funzione per estrarre i tags
function extract_tags( $html, $tag, $selfclosing = null, $return_the_entire_tag = false, $charset = 'ISO-8859-1' ){
 
	if ( is_array($tag) ){
		$tag = implode('|', $tag);
	}
 
	//If the user didn't specify if $tag is a self-closing tag we try to auto-detect it
	//by checking against a list of known self-closing tags.
	$selfclosing_tags = array( 'area', 'base', 'basefont', 'br', 'hr', 'input', 'img', 'link', 'meta', 'col', 'param' );
	if ( is_null($selfclosing) ){
		$selfclosing = in_array( $tag, $selfclosing_tags );
	}
 
	//The regexp is different for normal and self-closing tags because I can't figure out 
	//how to make a sufficiently robust unified one.
	if ( $selfclosing ){
		$tag_pattern = 
			'@<(?P<tag>'.$tag.')			# <tag
			(?P<attributes>\s[^>]+)?		# attributes, if any
			\s*/?>					# /> or just >, being lenient here 
			@xsi';
	} else {
		$tag_pattern = 
			'@<(?P<tag>'.$tag.')			# <tag
			(?P<attributes>\s[^>]+)?		# attributes, if any
			\s*>					# >
			(?P<contents>.*?)			# tag contents
			</(?P=tag)>				# the closing </tag>
			@xsi';
	}
 
	$attribute_pattern = 
		'@
		(?P<name>\w+)							# attribute name
		\s*=\s*
		(
			(?P<quote>[\"\'])(?P<value_quoted>.*?)(?P=quote)	# a quoted value
			|							# or
			(?P<value_unquoted>[^\s"\']+?)(?:\s+|$)			# an unquoted value (terminated by whitespace or EOF) 
		)
		@xsi';
 
	//Find all tags 
	if ( !preg_match_all($tag_pattern, $html, $matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE ) ){
		//Return an empty array if we didn't find anything
		return array();
	}
 
	$tags = array();
	foreach ($matches as $match){
 
		//Parse tag attributes, if any
		$attributes = array();
		if ( !empty($match['attributes'][0]) ){ 
 
			if ( preg_match_all( $attribute_pattern, $match['attributes'][0], $attribute_data, PREG_SET_ORDER ) ){
				//Turn the attribute data into a name->value array
				foreach($attribute_data as $attr){
					if( !empty($attr['value_quoted']) ){
						$value = $attr['value_quoted'];
					} else if( !empty($attr['value_unquoted']) ){
						$value = $attr['value_unquoted'];
					} else {
						$value = '';
					}
 
					//Passing the value through html_entity_decode is handy when you want
					//to extract link URLs or something like that. You might want to remove
					//or modify this call if it doesn't fit your situation.
					$value = html_entity_decode( $value, ENT_QUOTES, $charset );
 
					$attributes[$attr['name']] = $value;
				}
			}
 
		}
 
		$tag = array(
			'tag_name' => $match['tag'][0],
			'offset' => $match[0][1], 
			'contents' => !empty($match['contents'])?$match['contents'][0]:'', //empty for self-closing tags
			'attributes' => $attributes, 
		);
		if ( $return_the_entire_tag ){
			$tag['full_tag'] = $match[0][0]; 			
		}
 
		$tags[] = $tag;
	}
 
	return $tags;
}
?>