<?php

##################################################################################
# PHP-RESIDENCE
# Copyright (C) 2001-2009 by Marco Maria Francesco De Santis (marco@digitaldruid.net)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version accepted by Marco Maria Francesco De Santis, which
# shall act as a proxy as defined in Section 14 of version 3 of the
# license.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
##################################################################################

$pag = "modifica_contratto.php";
$titolo = "Pagina di modifica documento";

include("./dati/dati_connessione.php");
include("./includes/funzioni_$PHPR_DB_TYPE.php");
$numconnessione = connetti_db($PHPR_DB_NAME,$PHPR_DB_HOST,$PHPR_DB_PORT,$PHPR_DB_USER,$PHPR_DB_PASS,$PHPR_LOAD_EXT);
include("./includes/funzioni.php");
include("./includes/sett_gio.php");
include("./includes/funzioni_costi_agg.php");
$tablenometariffe = $PHPR_TAB_PRE."ntariffe".$anno;
$tableperiodi = $PHPR_TAB_PRE."periodi".$anno;
$tableanni = $PHPR_TAB_PRE."anni";
$tablepersonalizza = $PHPR_TAB_PRE."personalizza";
$tablecontratti = $PHPR_TAB_PRE."contratti";


$id_utente = controlla_login($numconnessione,$PHPR_TAB_PRE,$id_sessione,$nome_utente_phpr,$password_phpr,$anno);
if ($id_utente and $id_utente == 1) {


if ($tema[$id_utente] and $tema[$id_utente] != "base" and @is_dir("./themes/".$tema[$id_utente]."/php")) include("./themes/".$tema[$id_utente]."/php/head.php");
else include("./includes/head.php");


$Euro = nome_valuta();
$stile_soldi = stile_soldi();
$stile_data = stile_data();

if (defined("C_CARTELLA_CREA_MODELLI") and C_CARTELLA_CREA_MODELLI != "") {
if (C_CARTELLA_DOC != "" and @is_dir(C_CARTELLA_CREA_MODELLI."/".C_CARTELLA_DOC)) $dir_salva_home = C_CARTELLA_DOC;
else $dir_salva_home = "";
} # fine if (defined("C_CARTELLA_CREA_MODELLI") and C_CARTELLA_CREA_MODELLI != "")
else $dir_salva_home = "./dati";


if (controlla_num_pos($num_contratto) == "NO") $num_contratto = "1";
if ($num_contratto < 1 or controlla_num_pos($num_contratto) == "NO") $num_contratto = "1";
if (strlen($num_contratto) == 1) $num_contratto = "0".$num_contratto;
$num_contratto_int = (integer) $num_contratto;




if ($cambia_qualcosa) {


function formatta_input_var_x_file ($input_utente) {
if (@get_magic_quotes_gpc()) $input_utente = stripslashes($input_utente);
$input_utente = str_replace("\\","\\\\",$input_utente);
$input_utente = str_replace("\"","\\\"",$input_utente);
return $input_utente;
} # fine function formatta_input_var_x_file

if ($cambia_formato) {
$formato = "";
if ($nuovo_formato == "HTML") $formato = "contrhtm";
if ($nuovo_formato == "EMAIL") $formato = "contreml";
if ($nuovo_formato == "RTF") $formato = "contrrtf";
if ($nuovo_formato == "TXT") $formato = "contrtxt";
if ($formato) {
$tabelle_lock = array("$tablecontratti");
$tabelle_lock = lock_tabelle($tabelle_lock);
$vecchio_formato = esegui_query("select tipo from $tablecontratti where numero = '$num_contratto' and tipo $LIKE 'contr%'");
if (numlin_query($vecchio_formato) == 1) {
$mostra_form_iniziale = "NO";
$vecchio_formato = risul_query($vecchio_formato,0,'tipo');
if ($vecchio_formato == "contreml") {
esegui_query("delete from $tablecontratti where numero = '$num_contratto' and tipo = 'oggetto'");
$num_all = esegui_query("select * from $tablecontratti where numero = '$num_contratto' and tipo = 'allegato'");
$num_all = risul_query($num_all,0,'testo');
esegui_query("delete from $tablecontratti where numero = '$num_contratto' and tipo = 'allegato'");
if ($num_all) {
$altri_all = esegui_query("select * from $tablecontratti where testo = '$num_all' and tipo = 'allegato'");
if (!numlin_query($altri_all)) {
esegui_query("delete from $tablecontratti where numero = '$num_all' and tipo = 'file_all' ");
} # fine if (!numlin_query($altri_all))
echo "".mex("Allegato eliminato",$pag).".<br>";
} # fine if ($num_all)
esegui_query("delete from $tablecontratti where numero = '$num_contratto' and tipo = 'bcc'");
} # fine if ($vecchio_formato == "contreml")
if ($formato == "contreml") {
esegui_query("delete from $tablecontratti where numero = '$num_contratto' and tipo = 'dir'");
esegui_query("insert into $tablecontratti (numero,tipo,testo) values ('$num_contratto','oggetto','') ");
esegui_query("insert into $tablecontratti (numero,tipo,testo) values ('$num_contratto','allegato','') ");
esegui_query("insert into $tablecontratti (numero,tipo,testo) values ('$num_contratto','bcc',';') ");
} # fine if ($formato == "contreml")
esegui_query("update $tablecontratti set tipo = '$formato' where numero = '$num_contratto' and tipo $LIKE 'contr%'");
echo "".mex("Formato del documento cambiato",$pag).".<br>";
} # fine if (numlin_query($vecchio_formato) == 1)
unlock_tabelle($tabelle_lock);
} # fine if ($formato)
} # fine if ($cambia_formato)

if ($cambia_dir_salva) {
if (@get_magic_quotes_gpc()) $nuova_dir_salva = stripslashes($nuova_dir_salva);
$nuova_dir_salva = htmlspecialchars($nuova_dir_salva);
$nuova_dir_salva = aggslashdb($nuova_dir_salva);
$tabelle_lock = array($tablecontratti);
$tabelle_lock = lock_tabelle($tabelle_lock);
$tipo_contratto = esegui_query("select tipo from $tablecontratti where numero = '$num_contratto' and tipo $LIKE 'contr%'");
$tipo_contratto = risul_query($tipo_contratto,0,tipo);
if ($tipo_contratto == "contrrtf" or $tipo_contratto == "contrhtm" or $tipo_contratto == "contrtxt") {
if (defined("C_CARTELLA_CREA_MODELLI") and C_CARTELLA_CREA_MODELLI != "") {
$nuova_dir_salva = str_replace("..","",$nuova_dir_salva);
$dir_salva = C_CARTELLA_CREA_MODELLI."/$nuova_dir_salva";
} # fine if (defined("C_CARTELLA_CREA_MODELLI") and C_CARTELLA_CREA_MODELLI != "")
else $dir_salva = $nuova_dir_salva;
if ($dir_salva) {
if (substr($nuova_dir_salva,-1) == "/") $nuova_dir_salva = substr($nuova_dir_salva,0,-1);
$mostra_form_iniziale = "NO";
if (!@is_dir($dir_salva)) echo mex("La cartella inserita <div style=\"display: inline; color: red;\">non esiste</div>","personalizza.php").".<br>";
else {
$fileaperto = @fopen("$dir_salva/prova.tmp","a+");
if (!$fileaperto) echo mex("Non ho i permessi di scrittura sulla cartella","personalizza.php")." <div style=\"display: inline; color: red;\">$dir_salva/</div>.<br>";
else {
fclose($fileaperto);
unlink("$dir_salva/prova.tmp");
if ($salva_contr == "SI") {
if ($nuova_dir_salva == $dir_salva_home) $nuova_dir_salva = "~";
$dir_esistente = esegui_query("select testo from $tablecontratti where numero = '$num_contratto' and tipo = 'dir'");
if (numlin_query($dir_esistente) >= 1) esegui_query("update $tablecontratti set testo = '$nuova_dir_salva' where numero = '$num_contratto' and tipo = 'dir'");
else esegui_query("insert into $tablecontratti (numero,tipo,testo) values ('$num_contratto','dir','$nuova_dir_salva')");
echo "".mex("I documenti verranno salvati nella cartella",$pag)." $dir_salva.<br>";
} # fine if ($salva_contr == "SI")
else {
esegui_query("delete from $tablecontratti where numero = '$num_contratto' and (tipo = 'dir' or tipo = 'num_prog' or tipo = 'nomefile' or tipo = 'autosalv') ");
esegui_query("delete from $tablecontratti where testo = '$num_contratto_int' and tipo = 'num_prog' ");
echo "".mex("I documenti non verranno salvati",$pag).".<br>";
} # fine else if ($salva_contr == "SI")
} # fine else if (!$fileaperto)
} # fine else if (!@is_dir($nuova_dir_salva))
} # fine if ($dir_salva)
} # fine if ($tipo_contratto == "contrrtf" or..
unlock_tabelle($tabelle_lock);
} # fine if ($cambia_dir_salva)

if ($cambia_autosalv) {
$tabelle_lock = array($tablecontratti);
$tabelle_lock = lock_tabelle($tabelle_lock);
$dir_esistente = esegui_query("select * from $tablecontratti where numero = '$num_contratto' and tipo = 'dir'");
if (numlin_query($dir_esistente)) {
if (!$n_autosalv) {
$mostra_form_iniziale = "NO";
esegui_query("delete from $tablecontratti where numero = '$num_contratto' and tipo = 'autosalv' ");
echo "".mex("Il documento non verrà salvato automaticamente",$pag).".<br>";
} # fine if (!$n_autosalv)
else {
if ($n_autosalv == "checkin" or $n_autosalv == "checkout") {
$mostra_form_iniziale = "NO";
$autosalv_esistente = esegui_query("select testo from $tablecontratti where numero = '$num_contratto' and tipo = 'autosalv' ");
if (numlin_query($autosalv_esistente)) esegui_query("update $tablecontratti set testo = '$n_autosalv' where numero = '$num_contratto' and tipo = 'autosalv' ");
else esegui_query("insert into $tablecontratti (numero,tipo,testo) values ('$num_contratto','autosalv','$n_autosalv') ");
echo "".mex("Il documento verrà salvato automaticamente",$pag)." ";
if ($n_autosalv == "checkin") echo mex("alla registrazione dell'entrata",$pag);
if ($n_autosalv == "checkout") echo mex("alla registrazione dell'uscita",$pag);
echo ".<br>";
} # fine if ($n_autosalv == "checkin" or $n_autosalv == "checkout")
} # fine else if (!$n_autosalv)
} # fine if (numlin_query($dir_esistente))
unlock_tabelle($tabelle_lock);
} # fine if ($cambia_autosalv)

if ($cambia_num_prog) {
$tabelle_lock = array($tablecontratti);
$tabelle_lock = lock_tabelle($tabelle_lock);
$dir_esistente = esegui_query("select * from $tablecontratti where numero = '$num_contratto' and tipo = 'dir'");
if (numlin_query($dir_esistente)) {
if (!strcmp($contr_num_prog,"")) {
$mostra_form_iniziale = "NO";
esegui_query("delete from $tablecontratti where numero = '$num_contratto' and tipo = 'num_prog' ");
echo ucfirst(mex("il documento",$pag))." $num_contratto ".mex("non condividerà il numero progressivo con nessun documento",$pag).".<br>";
} # fine if (!strcmp($contr_num_prog,""))
else {
if ($contr_num_prog >= 1 and controlla_num_pos($contr_num_prog) != "NO") {
$dir_esistente = esegui_query("select * from $tablecontratti where numero = '$contr_num_prog' and tipo = 'dir'");
if (numlin_query($dir_esistente)) {
$num_prog_esistente = esegui_query("select * from $tablecontratti where numero = '$contr_num_prog' and tipo = 'num_prog' ");
if (!numlin_query($num_prog_esistente)) {
$mostra_form_iniziale = "NO";
$num_prog_esistente = esegui_query("select testo from $tablecontratti where numero = '$num_contratto' and tipo = 'num_prog' ");
if (numlin_query($num_prog_esistente)) esegui_query("update $tablecontratti set testo = '$contr_num_prog' where numero = '$num_contratto' and tipo = 'num_prog' ");
else {
esegui_query("insert into $tablecontratti (numero,tipo,testo) values ('$num_contratto','num_prog','$contr_num_prog') ");
esegui_query("update $tablecontratti set testo = '$contr_num_prog' where testo = '$num_contratto_int' and tipo = 'num_prog' ");
} # fine else if (numlin_query($num_prog_esistente))
echo ucfirst(mex("il documento",$pag))." $num_contratto ".mex("condividerà il numero progressivo con il documento",$pag)." $contr_num_prog.<br>";
} # fine if (!numlin_query($num_prog_esistente))
} # fine if (numlin_query($dir_esistente))
} # fine if ($contr_num_prog >= 1 and controlla_num_pos($contr_num_prog) != "NO")
} # fine else if (!strcmp($contr_num_prog,""))
} # fine if (numlin_query($dir_esistente))
unlock_tabelle($tabelle_lock);
} # fine if ($cambia_num_prog)

if ($cambia_nome_file) {
$tabelle_lock = array($tablecontratti);
$tabelle_lock = lock_tabelle($tabelle_lock);
$dir_esistente = esegui_query("select * from $tablecontratti where numero = '$num_contratto' and tipo = 'dir'");
if (numlin_query($dir_esistente)) {
if ($tipo_nome_file != "pers") {
$mostra_form_iniziale = "NO";
esegui_query("delete from $tablecontratti where numero = '$num_contratto' and tipo = 'nomefile' ");
echo "".mex("I file verranno salvati con il nome del documento",$pag).".<br>";
} # fine if ($tipo_nome_file != "pers")
else {
if (strcmp($nome_file_salva,"")) {
$mostra_form_iniziale = "NO";
$nome_file_salva = htmlspecialchars($nome_file_salva);
$nomefile_esistente = esegui_query("select testo from $tablecontratti where numero = '$num_contratto' and tipo = 'nomefile' ");
if (numlin_query($nomefile_esistente)) esegui_query("update $tablecontratti set testo = '".aggslashdb($nome_file_salva)."' where numero = '$num_contratto' and tipo = 'nomefile' ");
else esegui_query("insert into $tablecontratti (numero,tipo,testo) values ('$num_contratto','nomefile','".aggslashdb($nome_file_salva)."') ");
echo "".mex("I file verranno salvati con il nome",$pag)." \"$nome_file_salva\".<br>";
} # fine if (strcmp($nome_file_salva,""))
} # fine else if ($tipo_nome_file != "pers")
} # fine if (numlin_query($dir_esistente))
unlock_tabelle($tabelle_lock);
} # fine if ($cambia_nome_file)

if ($cambia_oggetto) {
if (@get_magic_quotes_gpc()) $nuovo_oggetto = stripslashes($nuovo_oggetto);
if (strlen($nuovo_oggetto) > 120) $nuovo_oggetto = substr($nuovo_oggetto,0,120);
$nuovo_oggetto = htmlspecialchars($nuovo_oggetto);
$nuovo_oggetto = aggslashdb($nuovo_oggetto);
$tabelle_lock = array("$tablecontratti");
$tabelle_lock = lock_tabelle($tabelle_lock);
$tipo_contratto = esegui_query("select tipo from $tablecontratti where numero = '$num_contratto' and tipo $LIKE 'contr%'");
$tipo_contratto = risul_query($tipo_contratto,0,'tipo');
if ($tipo_contratto == "contreml") {
$mostra_form_iniziale = "NO";
esegui_query("update $tablecontratti set testo = '$nuovo_oggetto' where numero = '$num_contratto' and tipo = 'oggetto'");
echo "".mex("Oggetto cambiato",$pag).".<br>";
} # fine if ($tipo_contratto == "contreml")
unlock_tabelle($tabelle_lock);
} # fine if ($cambia_oggetto)

if ($upload_allegato) {
$errore = "NO";
$file_tmp = "./dati/allegato_contr$num_contratto.tmp";
if (!$file_allegato_upload) {
if ($HTTP_POST_FILES['file_allegato_upload']['tmp_name']) {
$file_allegato_upload = $HTTP_POST_FILES['file_allegato_upload']['tmp_name'];
$file_allegato_upload_name = $HTTP_POST_FILES['file_allegato_upload']['name'];
$file_allegato_upload_type = $HTTP_POST_FILES['file_allegato_upload']['type'];
} # fine if ($HTTP_POST_FILES['file_allegato_upload']['tmp_name'])
else {
if ($_FILES['file_allegato_upload']['tmp_name']) {
$file_allegato_upload = $_FILES['file_allegato_upload']['tmp_name'];
$file_allegato_upload_name = $_FILES['file_allegato_upload']['name'];
$file_allegato_upload_type = $_FILES['file_allegato_upload']['type'];
} # fine if ($_FILES['file_allegato_upload']['tmp_name'])
} # fine else if ($HTTP_POST_FILES['file_allegato_upload']['tmp_name'])
} # fine if (!$file_allegato_upload)
if (!move_uploaded_file($file_allegato_upload,$file_tmp)) $errore = "SI";
if ($errore == "NO") {
if (!defined("C_MASSIMO_NUM_BYTE_UPLOAD") or C_MASSIMO_NUM_BYTE_UPLOAD == 0 or filesize($file_tmp) <= C_MASSIMO_NUM_BYTE_UPLOAD) {
$allegato = "";
$f_allegato = fopen($file_tmp, 'r');
while (true) {
$dati = fread($f_allegato, 8192);
if (strlen($dati) == 0) break;
$allegato .= $dati;
} # fine while (true)
fclose($f_allegato);
unlink($file_tmp);
$tabelle_lock = array("$tablecontratti");
$tabelle_lock = lock_tabelle($tabelle_lock);
$tipo_contratto = esegui_query("select tipo from $tablecontratti where numero = '$num_contratto' and tipo $LIKE 'contr%'");
$tipo_contratto = risul_query($tipo_contratto,0,'tipo');
if ($tipo_contratto == "contreml") {
$mostra_form_iniziale = "NO";
$allegato = chunk_split(base64_encode($allegato));
if (!$file_allegato_upload_type) $file_allegato_upload_type = "application/unknown";
if (!$file_allegato_upload_name) $file_allegato_upload_name = mex("Allegato",$pag);
$allegato = htmlspecialchars($file_allegato_upload_name).",".$file_allegato_upload_type.",".$allegato;
$max_fa = esegui_query("select max(numero) from $tablecontratti where tipo = 'file_all' ");
if (numlin_query($max_fa)) $max_fa = risul_query($max_fa,0,0) + 1;
else $max_fa = 1;
esegui_query("insert into $tablecontratti (numero,tipo,testo) values ('$max_fa','file_all','".aggslashdb($allegato)."')");
esegui_query("update $tablecontratti set testo = '$max_fa' where numero = '$num_contratto' and tipo = 'allegato'");
echo "".mex("Allegato salvato",$pag).".<br>";
} # fine if ($tipo_contratto == "contreml")
unlock_tabelle($tabelle_lock);
} # fine if (!defined("C_MASSIMO_NUM_BYTE_UPLOAD") or...
else unlink($file_tmp);
} # fine if ($errore == "NO")
} # fine if ($upload_allegato)

if ($aggiungi_allegato) {
$all_esistente = @esegui_query("select * from $tablecontratti where numero = '".aggslashdb($num_allegato)."' and tipo = 'file_all' ");
if (numlin_query($all_esistente)) {
$mostra_form_iniziale = "NO";
esegui_query("update $tablecontratti set testo = '$num_allegato' where numero = '$num_contratto' and tipo = 'allegato'");
echo "".mex("Allegato salvato",$pag).".<br>";
} # fine if(numlin_query($all_esistente))
} # fine if ($aggiungi_allegato)

if ($elimina_allegato) {
$tabelle_lock = array("$tablecontratti");
$tabelle_lock = lock_tabelle($tabelle_lock);
$tipo_contratto = esegui_query("select tipo from $tablecontratti where numero = '$num_contratto' and tipo $LIKE 'contr%'");
$tipo_contratto = risul_query($tipo_contratto,0,'tipo');
if ($tipo_contratto == "contreml") {
$mostra_form_iniziale = "NO";
$num_all = esegui_query("select * from $tablecontratti where numero = '$num_contratto' and tipo = 'allegato'");
$num_all = risul_query($num_all,0,'testo');
if ($num_all) {
esegui_query("update $tablecontratti set testo = '' where numero = '$num_contratto' and tipo = 'allegato'");
$altri_all = esegui_query("select * from $tablecontratti where testo = '$num_all' and tipo = 'allegato'");
if (!numlin_query($altri_all)) {
esegui_query("delete from $tablecontratti where numero = '$num_all' and tipo = 'file_all' ");
} # fine if (!numlin_query($altri_all))
echo "".mex("Allegato eliminato",$pag).".<br>";
} # fine if ($num_all)
} # fine if ($tipo_contratto == "contreml")
unlock_tabelle($tabelle_lock);
} # fine if ($elimina_allegato)

if ($cambia_bcc) {
$tabelle_lock = array("$tablecontratti");
$tabelle_lock = lock_tabelle($tabelle_lock);
$tipo_contratto = esegui_query("select tipo from $tablecontratti where numero = '$num_contratto' and tipo $LIKE 'contr%'");
$tipo_contratto = risul_query($tipo_contratto,0,'tipo');
if ($tipo_contratto == "contreml") {
$mostra_form_iniziale = "NO";
$bcc = "";
if ($bcc_mittente == "SI") $bcc = "SI";
$bcc .= ";";
if ($bcc_indirizzo and preg_match('/^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,4}$/i',$email_bcc)) $bcc .= "$email_bcc";
esegui_query("update $tablecontratti set testo = '$bcc' where numero = '$num_contratto' and tipo = 'bcc'");
echo "".mex("Copie bcc da spedire cambiate",$pag).".<br>";
} # fine if ($tipo_contratto == "contreml")
unlock_tabelle($tabelle_lock);
} # fine if ($cambia_bcc)

if ($salva_modifiche) {
$mostra_form_iniziale = "NO";
if (@get_magic_quotes_gpc()) $n_contratto = stripslashes($n_contratto);
if (C_RESTRIZIONI_DEMO_ADMIN == "SI") $n_contratto = htmlspecialchars($n_contratto);
$contr_controlla_int = str_replace("[r4]","[r4\\]",$n_contratto);
$contr_controlla_int = preg_replace("/\\[r4 array=\"[A-Za-z]+[A-Za-z0-9_]*\"\\]/","[r4]",$contr_controlla_int);
$contr_controlla = explode("[",$contr_controlla_int);
$num_contr_controlla = count($contr_controlla);
$livello = 0;
unset($tipo_livello);
if (str_replace("[r]","",$contr_controlla_int) == $contr_controlla_int) $tipo_livello[0] = "r";
$errore = "NO";
for ($num1 = 1 ; $num1 < $num_contr_controlla ; $num1++) {
$parte = $contr_controlla[$num1];
$apertura = "";
$chiusura = "";
if (substr($parte,0,2) == "r]") $apertura = "r";
if (substr($parte,0,3) == "r2]") $apertura = "r2";
if (substr($parte,0,3) == "r3]") $apertura = "r3";
if (substr($parte,0,3) == "r4]") $apertura = "r4";
if (substr($parte,0,3) == "/r]") $chiusura = "r";
if (substr($parte,0,4) == "/r2]") $chiusura = "r2";
if (substr($parte,0,4) == "/r3]") $chiusura = "r3";
if (substr($parte,0,4) == "/r4]") $chiusura = "r4";
if ($apertura) {
if ($apertura == "r" and $livello != 0 and ($livello != 1 or $tipo_livello[1] != "r4" or $tipo_livello[0] == "r")) $errore = "SI";
if (($apertura == "r2" or $apertura == "r3") and $tipo_livello[$livello] != "r") $errore = "SI";
if ($apertura == "r4" and $tipo_livello[$livello] != "r" and $livello != 0 and ($livello != 1 or $tipo_livello[1] != "r4" or $tipo_livello[0] == "r")) $errore = "SI";
$livello++;
$tipo_livello[$livello] = $apertura;
} # fine ($apertura)
if ($chiusura) {
if ($tipo_livello[$livello] != $chiusura) $errore = "SI";
$tipo_livello[$livello] = "";
$livello--;
if ($livello < 0) $errore = "SI";
} # fine if ($chiusura)
if ($errore == "SI") break;
} # fine for $num1
if ($livello != 0) $errore = "SI";
$contr_controlla_int = str_replace("[c]","[c\\]",$contr_controlla_int);
$contr_controlla_int = preg_replace("/\\[c +[A-Za-z]+[A-Za-z0-9_]* *!?= *\"[^\"]*\" *\\]/","[c]",$contr_controlla_int);
$contr_controlla = explode("[c]",$contr_controlla_int);
$num_contr_controlla = count($contr_controlla);
for ($num1 = 1 ; $num1 < $num_contr_controlla ; $num1++) {
$parte = explode("[/c]",$contr_controlla[$num1]);
if (count($parte) != 2) $errore = "SI";
if (preg_replace("|\\[/?r[1234]\\]|","",$parte[0]) != $parte[0]) $errore = "SI";
} # fine for $num1
if ($errore == "SI") echo "<span style=\"color: red;\">".mex("Errore nelle ripetizioni e condizioni annidate",$pag)."</span>.<br>";
else {
$n_contratto = aggslashdb($n_contratto);
esegui_query("update $tablecontratti set testo = '$n_contratto' where numero = '$num_contratto' and tipo $LIKE 'contr%'");
echo "".mex("Documento salvato",$pag).".<br>";
} # fine else if ($errore == "SI")
} # fine if ($salva_modifiche)

if ($aggiungi_var or $aggiungi_var2) {
if ($aggiungi_var2) $var_agg = $var_agg2;
if ($var_agg and preg_replace("/[A-Za-z0-9\(\)_]/","",$var_agg) == "") {
if (@get_magic_quotes_gpc()) $n_contratto = stripslashes($n_contratto);
if (!strcmp($pos_curs,"")) $testo_contratto_mod = $n_contratto."[".$var_agg."]";
else {
$pos_curs = strlen(utf8_encode(substr(utf8_decode($n_contratto),0,$pos_curs)));
$testo_contratto_mod = substr($n_contratto,0,$pos_curs)."[".$var_agg."]".substr($n_contratto,$pos_curs);
} # fine else if (!strcmp($pos_curs,""))
} # fine if ($var_agg and preg_replace("/[A-Za-z0-9\(\)_]/","",$var_agg) == "")
} # fine if ($aggiungi_var or $aggiungi_var2)

if ($aggiungi_ripetizione or $aggiungi_ripetizione2) {
if (@get_magic_quotes_gpc()) $n_contratto = stripslashes($n_contratto);
if ($aggiungi_ripetizione2) $tipo_rip = $tipo_rip2;
$testo_agg1 = "";
$testo_agg2 = "";
$testo_agg3 = "";
if ($tipo_rip == 1) {
$testo_agg1 = "[r]";
$testo_agg2 = "\n".mex("SOSTITUISCI CON PARTE DA RIPETERE PER OGNI PRENOTAZIONE",$pag)."\n";
$testo_agg3 = "[/r]";
} # fine if ($tipo_rip == 1)
if ($tipo_rip == 2) {
$testo_agg1 = "[r2]";
$testo_agg2 = "\n".mex("SOSTITUISCI CON LISTA OSPITI IN PRENOTAZIONE",$pag)."\n";
$testo_agg3 = "[/r2]";
} # fine if ($tipo_rip == 2)
if ($tipo_rip == 3) {
$testo_agg1 = "[r3]";
$testo_agg2 = "\n".mex("SOSTITUISCI CON LISTA COSTI IN PRENOTAZIONE",$pag)."\n";
$testo_agg3 = "[/r3]";
} # fine if ($tipo_rip == 3)
if (substr($tipo_rip,0,1) == "a") {
$testo_agg1 = "[r4 array=\"".substr($tipo_rip,1)."\"]";
$testo_agg2 = "\n".mex("SOSTITUISCI CON PARTE DA RIPETERE PER OGNI VALORE DELL'ARRAY",$pag)."\n";
$testo_agg3 = "[/r4]";
} # fine if (substr($tipo_rip,0,1) == "a")
if ($tipo_rip == 4) {
$testo_agg1 = "[c ".mex("nome_variabile",$pag)."=\"".mex("valore",$pag)."\"]";
$testo_agg2 = "\n".mex("SOSTITUISCI CON PARTE DA MOSTRARE SOLO SE E' SODDISFATTA LA CONDIZIONE PRECEDENTE",$pag)." (".mex("nome_variabile",$pag)." = ".mex("OPPURE",$pag)." != ".mex("valore",$pag).")\n";
$testo_agg3 = "[/c]";
} # fine if ($tipo_rip == 4)
if (!strcmp($pos_curs,"")) $testo_contratto_mod = $n_contratto.$testo_agg1.$testo_agg2.$testo_agg3;
else {
$pos_curs = strlen(utf8_encode(substr(utf8_decode($n_contratto),0,$pos_curs)));
if (!strcmp($pos_fine_sel,"")) $testo_contratto_mod = substr($n_contratto,0,$pos_curs).$testo_agg1.$testo_agg2.$testo_agg3.substr($n_contratto,$pos_curs);
else {
$pos_fine_sel = strlen(utf8_encode(substr(utf8_decode($n_contratto),0,$pos_fine_sel)));
$testo_contratto_mod = substr($n_contratto,0,$pos_curs).$testo_agg1.substr($n_contratto,$pos_curs,($pos_fine_sel - $pos_curs)).$testo_agg3.substr($n_contratto,$pos_fine_sel);
} # fine else if (!strcmp($pos_fine_sel,""))
} # fine else if (!strcmp($pos_curs,""))
} # fine if ($aggiungi_ripetizione or $aggiungi_ripetizione2)


if ($mostra_form_iniziale == "NO") {
echo "<br><form accept-charset=\"utf-8\" method=\"post\" action=\"$pag\"><div>
<input type=\"hidden\" name=\"anno\" value=\"$anno\">
<input type=\"hidden\" name=\"id_sessione\" value=\"$id_sessione\">
<input type=\"hidden\" name=\"num_contratto\" value=\"$num_contratto\">
<input class=\"sbutton\" type=\"submit\" name=\"torna\" value=\"".mex("Torna indietro",$pag)."\">
</div></form>";
} # fine if ($mostra_form_iniziale == "NO")

} # fine if ($cambia_qualcosa)





if ($mostra_form_iniziale != "NO") {

include("./includes/funzioni_contratti.php");
$nomi_contratti = trova_nomi_contratti($max_contr,$id_utente,$tablecontratti,$tablepersonalizza,$LIKE,$pag);
if (!strcmp($nomi_contratti['pers'][$num_contratto_int],"")) $nome_contratto = "";
else $nome_contratto = " (".$nomi_contratti['pers'][$num_contratto_int].")";

$trad_var = "";
function mex2 ($messaggio) {
global $trad_var,$lingua_mex;
if (!$trad_var and $lingua_mex != "ita") include("./includes/lang/$lingua_mex/visualizza_contratto_var.php");
if ($trad_var[$messaggio]) $messaggio = $trad_var[$messaggio];
return $messaggio;
} # fine function mex2


echo "<h3>".ucfirst(mex("documento",$pag))." $num_contratto$nome_contratto</h3><br>
<br>";
$dati_contratto = esegui_query("select * from $tablecontratti where numero = '$num_contratto' and tipo $LIKE 'contr%'");
if (numlin_query($dati_contratto) != 1) exit();

echo "<form accept-charset=\"utf-8\" method=\"post\" action=\"visualizza_contratto.php\"><div>
<input type=\"hidden\" name=\"anno\" value=\"$anno\">
<input type=\"hidden\" name=\"id_sessione\" value=\"$id_sessione\">
<input type=\"hidden\" name=\"numero_contratto\" value=\"$num_contratto\">
<input type=\"hidden\" name=\"origine\" value=\"./$pag?num_contratto=$num_contratto\">
<input class=\"sbutton\" type=\"submit\" name=\"torna\" value=\"".mex("Visualizza il documento",$pag)."\">
</div></form><br>";

$tipo_contratto = substr(risul_query($dati_contratto,0,'tipo'),5);
$sel_HTML = "";
$sel_EMAIL = "";
$sel_RTF = "";
$sel_TXT = "";
if ($tipo_contratto == "htm") $sel_HTML = " selected";
if ($tipo_contratto == "eml") $sel_EMAIL = " selected";
if ($tipo_contratto == "rtf") $sel_RTF = " selected";
if ($tipo_contratto == "txt") $sel_TXT = " selected";
echo "<form accept-charset=\"utf-8\" method=\"post\" action=\"$pag\"><div>
<input type=\"hidden\" name=\"anno\" value=\"$anno\">
<input type=\"hidden\" name=\"id_sessione\" value=\"$id_sessione\">
<input type=\"hidden\" name=\"num_contratto\" value=\"$num_contratto\">
<input type=\"hidden\" name=\"cambia_qualcosa\" value=\"SI\">
<input type=\"hidden\" name=\"cambia_formato\" value=\"SI\">
".mex("Formato del documento",$pag).":
<select name=\"nuovo_formato\">
<option value=\"HTML\"$sel_HTML>HTML</option>
<option value=\"EMAIL\"$sel_EMAIL>EMAIL</option>
<option value=\"RTF\"$sel_RTF>RTF</option>
<option value=\"TXT\"$sel_TXT>TXT</option>
</select>
<input class=\"sbutton\" type=\"submit\" value=\"".mex("Cambia",$pag)."\">
</div></form><br>";


if ($tipo_contratto == "rtf" or $tipo_contratto == "htm" or $tipo_contratto == "txt") {
$dir_salva = esegui_query("select * from $tablecontratti where numero = '$num_contratto' and tipo = 'dir'");
if (numlin_query($dir_salva) == 1) {
$dir_salva = risul_query($dir_salva,0,'testo');
if ($dir_salva == "~") $dir_salva = $dir_salva_home;
$checked = " checked";
} # fine if (numlin_query($dir_salva) == 1)
else {
if (defined("C_CARTELLA_CREA_MODELLI") and C_CARTELLA_CREA_MODELLI != "") $dir_salva = "";
else $dir_salva = $dir_salva_home;
$checked = "";
} # fine else if (numlin_query($dir_salva) == 1)
echo "<form accept-charset=\"utf-8\" method=\"post\" action=\"$pag\"><div>
<input type=\"hidden\" name=\"anno\" value=\"$anno\">
<input type=\"hidden\" name=\"id_sessione\" value=\"$id_sessione\">
<input type=\"hidden\" name=\"num_contratto\" value=\"$num_contratto\">
<input type=\"hidden\" name=\"cambia_qualcosa\" value=\"SI\">
<input type=\"checkbox\" name=\"salva_contr\" value=\"SI\"$checked>
".mex("Salva i documenti con numero progressivo nella cartella",$pag)." ";
if (defined("C_CARTELLA_CREA_MODELLI") and C_CARTELLA_CREA_MODELLI != "") echo C_CARTELLA_CREA_MODELLI."/";
echo "<input type=\"text\" name=\"nuova_dir_salva\" size=\"20\" value=\"$dir_salva\">
<input class=\"sbutton\" type=\"submit\" name=\"cambia_dir_salva\" value=\"".mex("Cambia",$pag)."\">
</div></form>";

if ($checked) {
echo "<table cellspacing=\"0\" cellpadding=\"0\"><tr><td style=\"width: 50px;\"></td><td>";

$autosalv = esegui_query("select * from $tablecontratti where numero = '$num_contratto' and tipo = 'autosalv' ");
if (!numlin_query($autosalv)) $autosalv = "";
else $autosalv = risul_query($autosalv,0,'testo');
if (!$autosalv) $sel_mai = " selected";
else ${"sel_".$autosalv} = " selected";
echo "<form accept-charset=\"utf-8\" method=\"post\" action=\"$pag\"><div>
<input type=\"hidden\" name=\"anno\" value=\"$anno\">
<input type=\"hidden\" name=\"id_sessione\" value=\"$id_sessione\">
<input type=\"hidden\" name=\"num_contratto\" value=\"$num_contratto\">
<input type=\"hidden\" name=\"cambia_qualcosa\" value=\"SI\">
<input type=\"hidden\" name=\"cambia_autosalv\" value=\"SI\">
".mex("Salva automaticamente il documento",$pag).":
<select name=\"n_autosalv\">
<option value=\"\"$sel_mai>".mex("mai",$pag)."</option>
<option value=\"checkin\"$sel_checkin>".mex("alla registrazione dell'entrata",$pag)."</option>
<option value=\"checkout\"$sel_checkout>".mex("alla registrazione dell'uscita",$pag)."</option>
</select>
<input class=\"sbutton\" type=\"submit\" value=\"".mex("Cambia",$pag)."\">
</div></form>";

$num_prog = esegui_query("select * from $tablecontratti where numero = '$num_contratto' and tipo = 'num_prog' ");
if (!numlin_query($num_prog)) $num_prog = "";
else $num_prog = risul_query($num_prog,0,'testo');
$contr_salva = esegui_query("select * from $tablecontratti where tipo = 'dir'");
$num_contr_salva = numlin_query($contr_salva);
$opt_num_prog = "";
for ($num_c = 0 ; $num_c < $num_contr_salva ; $num_c++) {
$num_contr = risul_query($contr_salva,$num_c,'numero');
if ($num_contr != $num_contratto) {
$num_prog_esistente = esegui_query("select * from $tablecontratti where numero = '$num_contr' and tipo = 'num_prog' ");
if (!numlin_query($num_prog_esistente)) {
$nome_contr = mex("il documento",$pag)." ".$num_contr;
if (strcmp($nomi_contratti['pers'][$num_contr],"")) $nome_contr .= " (".$nomi_contratti['pers'][$num_contr].")";
if ($num_prog != $num_contr) $sel = "";
else $sel = " selected";
$opt_num_prog .= "<option value=\"$num_contr\"$sel>$nome_contr</option>";
} # fine if (!numlin_query($num_prog_esistente))
} # fine if ($num_contr != $num_contratto)
} # fine for $num_c
if ($opt_num_prog) {
if ($num_prog) $sel = "";
else $sel = " selected";
echo "<form accept-charset=\"utf-8\" method=\"post\" action=\"$pag\"><div>
<input type=\"hidden\" name=\"anno\" value=\"$anno\">
<input type=\"hidden\" name=\"id_sessione\" value=\"$id_sessione\">
<input type=\"hidden\" name=\"num_contratto\" value=\"$num_contratto\">
<input type=\"hidden\" name=\"cambia_qualcosa\" value=\"SI\">
<input type=\"hidden\" name=\"cambia_num_prog\" value=\"SI\">
".mex("Condividi il numero progressivo con",$pag)."
<select name=\"contr_num_prog\">
<option value=\"\"$sel>----</option>
$opt_num_prog</select>
<input class=\"sbutton\" type=\"submit\" value=\"".mex("Cambia",$pag)."\">
</div></form>";
} # fine if ($opt_num_prog)

$nome_file_salva = esegui_query("select testo from $tablecontratti where numero = '$num_contratto' and tipo = 'nomefile' ");
if (numlin_query($nome_file_salva)) {
$nome_file_salva = risul_query($nome_file_salva,0,'testo');
$checked_ncontr = "";
$checked_npers = " checked";
} # fine if (numlin_query($nome_file_salva))
else {
$nome_file_salva = "";
$checked_ncontr = " checked";
$checked_npers = "";
} # fine else if (numlin_query($nome_file_salva))
echo "<form accept-charset=\"utf-8\" method=\"post\" action=\"$pag\"><div>
<input type=\"hidden\" name=\"anno\" value=\"$anno\">
<input type=\"hidden\" name=\"id_sessione\" value=\"$id_sessione\">
<input type=\"hidden\" name=\"num_contratto\" value=\"$num_contratto\">
<input type=\"hidden\" name=\"cambia_qualcosa\" value=\"SI\">
<input type=\"hidden\" name=\"cambia_nome_file\" value=\"SI\">
<table cellspacing=0 cellpadding=0><tr><td valign=\"top\">".mex("Usa come nome del file",$pag).":
</td><td onclick=\"document.getElementById('fncontr').checked='1'\">
<input type=\"radio\" name=\"tipo_nome_file\" id=\"fncontr\" value=\"contr\"$checked_ncontr>".mex("il nome del documento",$pag)."
</td></tr><tr><td></td><td onclick=\"document.getElementById('fnpers').checked='1'\">
<input type=\"radio\" name=\"tipo_nome_file\" id=\"fnpers\" value=\"pers\"$checked_npers>".mex("un altro nome",$pag).":
<input type=\"text\" name=\"nome_file_salva\" size=\"30\" maxlength=\"60\" value=\"$nome_file_salva\">
</td></tr><tr><td></td><td>
<input class=\"sbutton\" type=\"submit\" value=\"".mex("Cambia",$pag)."\">
</td></tr></table></div></form>
</td></tr></table>";
} # fine if ($checked)

echo "<br>";
} # fine if ($tipo_contratto == "rtf" or $tipo_contratto == "htm" or...


if ($tipo_contratto == "eml") {
$oggetto = esegui_query("select * from $tablecontratti where numero = '$num_contratto' and tipo = 'oggetto'");
$oggetto = risul_query($oggetto,0,'testo');
echo "<form accept-charset=\"utf-8\" method=\"post\" action=\"$pag\"><div>
<input type=\"hidden\" name=\"anno\" value=\"$anno\">
<input type=\"hidden\" name=\"id_sessione\" value=\"$id_sessione\">
<input type=\"hidden\" name=\"num_contratto\" value=\"$num_contratto\">
<input type=\"hidden\" name=\"cambia_qualcosa\" value=\"SI\">
".mex("Oggetto",$pag).":
<input type=\"text\" name=\"nuovo_oggetto\" size=\"50\" maxlength=\"60\" value=\"$oggetto\">
<input class=\"sbutton\" type=\"submit\" name=\"cambia_oggetto\" value=\"".mex("Cambia",$pag)."\">
</div></form>";
$allegato = esegui_query("select * from $tablecontratti where numero = '$num_contratto' and tipo = 'allegato'");
$allegato = risul_query($allegato,0,'testo');
if ($allegato) {
$allegato = esegui_query("select * from $tablecontratti where numero = '$allegato' and tipo = 'file_all'");
$allegato = risul_query($allegato,0,'testo');
$allegato = explode(",",$allegato);
echo "<form accept-charset=\"utf-8\" method=\"post\" action=\"$pag\"><div>
<input type=\"hidden\" name=\"anno\" value=\"$anno\">
<input type=\"hidden\" name=\"id_sessione\" value=\"$id_sessione\">
<input type=\"hidden\" name=\"num_contratto\" value=\"$num_contratto\">
<input type=\"hidden\" name=\"cambia_qualcosa\" value=\"SI\">
".mex("Allegato",$pag).": <b>".$allegato[0]."</b>
<input class=\"sbutton\" type=\"submit\" name=\"elimina_allegato\" value=\"".mex("Elimina",$pag)."\">
</div></form>";
} # fine if ($allegato)
else {
echo "<table cellspacing=\"0\"><tr><td>
".mex("Allegato",$pag).":</td><td>
<form accept-charset=\"utf-8\" enctype=\"multipart/form-data\" method=\"post\" action=\"$pag\"><div>
<input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"50000000\">
<input type=\"hidden\" name=\"anno\" value=\"$anno\">
<input type=\"hidden\" name=\"id_sessione\" value=\"$id_sessione\">
<input type=\"hidden\" name=\"num_contratto\" value=\"$num_contratto\">
<input type=\"hidden\" name=\"cambia_qualcosa\" value=\"SI\">
<input name=\"file_allegato_upload\" type=\"file\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input class=\"sbutton\" type=\"submit\" name=\"upload_allegato\" value=\"".mex("Aggiungi",$pag)."\">
</div></form></td></tr>";
$allegati = esegui_query("select * from $tablecontratti where tipo = 'file_all'");
$num_allegati = numlin_query($allegati);
for ($num1 = 0 ; $num1 < $num_allegati ; $num1++) {
$num_all = risul_query($allegati,$num1,'numero');
$nome_all = explode(",",risul_query($allegati,$num1,'testo'));
$opt_allegati .= "<option value=\"$num_all\">".$nome_all[0]."</option>";
} # fine for $num1
if ($opt_allegati) {
echo "<tr><td></td><td>
<form accept-charset=\"utf-8\" enctype=\"multipart/form-data\" method=\"post\" action=\"$pag\"><div>
<input type=\"hidden\" name=\"anno\" value=\"$anno\">
<input type=\"hidden\" name=\"id_sessione\" value=\"$id_sessione\">
<input type=\"hidden\" name=\"num_contratto\" value=\"$num_contratto\">
<input type=\"hidden\" name=\"cambia_qualcosa\" value=\"SI\">
<input type=\"hidden\" name=\"aggiungi_allegato\" value=\"SI\">
<select name=\"num_allegato\">
$opt_allegati</select>
<input class=\"sbutton\" type=\"submit\" value=\"".mex("Aggiungi",$pag)."\">
</div></form></td></tr>";
} # fine if ($opt_allegati)
echo "</table>";
} # fine else if ($allegato)
$bcc = esegui_query("select * from $tablecontratti where numero = '$num_contratto' and tipo = 'bcc'");
$bcc = risul_query($bcc,0,'testo');
$bcc = explode(";",$bcc);
if ($bcc[0] == "SI") $checked_mittente = " checked";
else $checked_mittente = "";
if ($bcc[1]) {
$checked_email = " checked";
$email_bcc = $bcc[1];
} # fine if ($bcc[1])
else {
$checked_email = "";
$email_bcc = esegui_query("select * from $tablepersonalizza where idpersonalizza = 'dati_struttura' and idutente = '1'");
$email_bcc = risul_query($email_bcc,0,'valpersonalizza');
$email_bcc = explode("#@&",$email_bcc);
$email_bcc = $email_bcc[2];
} # fine else if ($bcc[1])
echo "<form accept-charset=\"utf-8\" method=\"post\" action=\"$pag\"><div>
<input type=\"hidden\" name=\"anno\" value=\"$anno\">
<input type=\"hidden\" name=\"id_sessione\" value=\"$id_sessione\">
<input type=\"hidden\" name=\"num_contratto\" value=\"$num_contratto\">
<input type=\"hidden\" name=\"cambia_qualcosa\" value=\"SI\">
<table cellspacing=0 cellpadding=0><tr><td valign=\"top\">".mex("Mandare una copia bcc",$pag).":</td><td>
<input type=\"checkbox\" name=\"bcc_mittente\" value=\"SI\"$checked_mittente>".mex("al mittente",$pag)."<br>
<input type=\"checkbox\" name=\"bcc_indirizzo\" value=\"SI\"$checked_email>".mex("all'indirizzo",$pag)."
<input type=\"text\" name=\"email_bcc\" size=\"50\" maxlength=\"60\" value=\"$email_bcc\"><br>
<input class=\"sbutton\" type=\"submit\" name=\"cambia_bcc\" value=\"".mex("Cambia",$pag)."\">
</td></tr></table></div></form><br>";
} # fine ($tipo_contratto == "eml")


echo mex("Modifica il documento",$pag).":<br>
<table><tr><td style=\"height: 3px;\"></td></tr></table>";
if ($testo_contratto_mod) $testo_contratto = $testo_contratto_mod;
else $testo_contratto = risul_query($dati_contratto,0,'testo');
echo "<div style=\"text-align: center;\">
<form accept-charset=\"utf-8\" method=\"post\" action=\"$pag#contr_txtbox\" onSubmit=\"agg_pos_curs()\"><div>
<input type=\"hidden\" name=\"anno\" value=\"$anno\">
<input type=\"hidden\" name=\"id_sessione\" value=\"$id_sessione\">
<input type=\"hidden\" name=\"num_contratto\" value=\"$num_contratto\">
<input type=\"hidden\" name=\"cambia_qualcosa\" value=\"SI\">
<input type=\"hidden\" name=\"pos_curs\" value=\"\" id=\"pos_curs\">
<input type=\"hidden\" name=\"pos_fine_sel\" value=\"\" id=\"pos_fine_sel\">
<table style=\"margin-left: auto; margin-right: auto;\">";
$linea_mod = "<tr><td><a name=\"contr_txtbox\"></a>
".mex("Variabili",$pag).":
<select name=\"var_agg\">";

include("./includes/variabili_contratto.php");
$num_var_predef--;
$num_int_contr = $num_contratto;
while ((string) substr($num_int_contr,0,1) == (string) "0") $num_int_contr = substr($num_int_contr,1);
$variabili_pers = esegui_query("select * from $tablecontratti where tipo = 'var' or tipo = 'var$num_int_contr' order by tipo, numero");
$num_variabili_pers = numlin_query($variabili_pers);
for ($num1 = 0 ; $num1 < $num_variabili_pers ; $num1++) {
$var_pers = risul_query($variabili_pers,$num1,'testo');
$num_var_pers = risul_query($variabili_pers,$num1,'numero');
$var_predef[$num_var_predef] = $var_pers;
$num_var_predef++;
} # fine for $num1
$array_pers = esegui_query("select * from $tablecontratti where tipo = 'vett' or tipo = 'vett$num_int_contr' order by tipo, numero");
$num_array_pers = numlin_query($array_pers);
for ($num1 = 0 ; $num1 < $num_array_pers ; $num1++) {
$arr_pers_vett = explode(";",risul_query($array_pers,$num1,'testo'));
$arr_pers = $arr_pers_vett[0];
$var_arr_pers = $arr_pers_vett[1];
$num_arr_pers = risul_query($array_pers,$num1,'numero');
$var_predef[$num_var_predef] = "$arr_pers(".mex2($var_arr_pers).")";
$num_var_predef++;
$option_array .= "<option value=\"a$arr_pers\">".mex("ripetizione array",$pag)." $arr_pers</option>";
} # fine for $num1

for ($num1 = 0 ; $num1 < $num_var_predef ; $num1++) {
$linea_mod .= "<option value=\"".mex2($var_predef[$num1])."\">".mex2($var_predef[$num1])."</option>";
} # fine for $num1
$linea_mod .= "</select>
<input class=\"sbutton\" type=\"submit\" name=\"aggiungi_var\" value=\"".mex("Aggiungi",$pag)."\">
</td><td style=\"width: 30px;\"></td><td>
<input class=\"sbutton\" type=\"submit\" name=\"salva_modifiche\" value=\"".mex("Salva le modifiche",$pag)."\">
</td><td style=\"width: 30px;\"></td><td>".mex("Limiti",$pag)." <select name=\"tipo_rip\">
<option value=\"1\">".mex("ripetizione prenotazioni",$pag)."</option>
<option value=\"2\">".mex("ripetizione ospiti",$pag)."</option>
<option value=\"3\">".mex("ripetizione costi aggiuntivi",$pag)."</option>
$option_array
<option value=\"4\">".mex("parte sotto condizione",$pag)."</option>
</select>
<input class=\"sbutton\" type=\"submit\" name=\"aggiungi_ripetizione\" value=\"".mex("Aggiungi",$pag)."\">
</td></tr>";
if ($tipo_contratto == "rtf" or $tipo_contratto == "txt") $wrap = "wrap=\"off\"";
else $wrap = "style=\"white-space: pre; overflow: auto;\"";
echo "$linea_mod<tr><td colspan=\"5\" align=\"center\">
<textarea id=\"contr_txta\" name=\"n_contratto\" rows=120 cols=135 $wrap>$testo_contratto</textarea></td></tr>";
$linea_mod = str_replace("<a name=\"contr_txtbox\"></a>","",$linea_mod);
$linea_mod = str_replace("<select name=\"var_agg\">","<select name=\"var_agg2\">",$linea_mod);
$linea_mod = str_replace("type=\"submit\" name=\"aggiungi_var\"","type=\"submit\" name=\"aggiungi_var2\"",$linea_mod);
$linea_mod = str_replace("<select name=\"tipo_rip\">","<select name=\"tipo_rip2\">",$linea_mod);
$linea_mod = str_replace("type=\"submit\" name=\"aggiungi_ripetizione\"","type=\"submit\" name=\"aggiungi_ripetizione2\"",$linea_mod);
echo "$linea_mod</table>
</div></form></div><br>
<script type=\"text/javascript\">
<!--
function resize_contr_txta() {
var height = Math.round(document.body.clientHeight - 80);
var width = Math.round(document.body.clientWidth - 60);
if (height < 300) height = 300;
if (width < 300) width = 300;
document.getElementById('contr_txta').style.height = height + 'px';
document.getElementById('contr_txta').style.width = width + 'px';
}
resize_contr_txta();
window.onresize = new Function(\"resize_contr_txta()\");

function agg_pos_curs() {
var pos_curs = 0;
var txtbox = document.getElementById('contr_txta');
if (txtbox.selectionStart) {
pos_curs = txtbox.selectionStart;
var len1 = txtbox.value.substring(0,pos_curs).length;
var len2 = txtbox.value.substring(0,pos_curs).replace(/(\\n|\\r)/g,'').length;
pos_curs = pos_curs + len1 - len2;
if (txtbox.selectionEnd && txtbox.selectionStart != txtbox.selectionEnd) {
var pos_fine_sel = txtbox.selectionEnd;
len1 = txtbox.value.substring(0,pos_fine_sel).length;
len2 = txtbox.value.substring(0,pos_fine_sel).replace(/(\\n|\\r)/g,'').length;
pos_fine_sel = pos_fine_sel + len1 - len2;
document.getElementById('pos_fine_sel').value = pos_fine_sel;
}
}
else if (document.selection) {
txtbox.focus();
var sel = document.selection.createRange();
var dup = sel.duplicate();
dup.moveToElementText(txtbox);
sel.text = \"\\001\";
pos_curs = dup.text.indexOf(\"\\001\");
sel.moveStart('character',-1);
sel.text = '';
}
document.getElementById('pos_curs').value = pos_curs;
}
-->
</script>";


echo "<hr style=\"width: 95%\"><br><div style=\"text-align: center;\">
<form accept-charset=\"utf-8\" method=\"post\" action=\"./modifica_var_contr.php\"><div>
<input type=\"hidden\" name=\"anno\" value=\"$anno\">
<input type=\"hidden\" name=\"id_sessione\" value=\"$id_sessione\">
<input type=\"hidden\" name=\"contr_cond\" value=\"$num_int_contr\">
<input class=\"sbutton\" type=\"submit\" name=\"torna\" value=\"".mex("Variabili personalizzate e condizioni del documento",$pag)." $num_contratto\">
</div></form><br>
<form accept-charset=\"utf-8\" method=\"post\" action=\"personalizza.php#contratti\"><div>
<input type=\"hidden\" name=\"anno\" value=\"$anno\">
<input type=\"hidden\" name=\"id_sessione\" value=\"$id_sessione\">
<input class=\"sbutton\" type=\"submit\" name=\"torna\" value=\"".mex("Torna indietro",$pag)."\">
</div></form><br></div>";


} # fine if ($mostra_form_iniziale != "NO")




if ($tema[$id_utente] and $tema[$id_utente] != "base" and @is_dir("./themes/".$tema[$id_utente]."/php")) include("./themes/".$tema[$id_utente]."/php/foot.php");
else include("./includes/foot.php");


} # fine if ($id_utente and $id_utente == 1)



?>