<?php
include_once ("../core/config.php");
$UTENTE = login();
$DIRITTI = diritti('Admin');
view_top();

if (isset($_GET['id'])) {
	$bottone = "Modifica";
	$modifica = 1;
	$query = "SELECT * FROM utenti WHERE id =".$_GET['id'];
	$res = mysql_query( $query, $DB_ID );
	$dato = mysql_fetch_array ( $res );
	$dato['password'] = "ahg5t!frtb5@u&^!";
} else if (isset ($_POST['id'])) {
	$modifica = 2;
	$id 		= $_POST['id'];
	$rm 		= $_POST['rm'];
	$utente 	= $_POST['utente'];
	$password1 	= $_POST['password1'];
	$password2	= $_POST['password2'];
	$admin		= $_POST['admin'];
	$attivo		= $_POST['attivo'];
	$mail		= $_POST['mail'];

	if (!$admin) $admin = 0;

	if (!$attivo) $attivo = 0;

	if (($password1 != "ahg5t!frtb5@u&^!") && ($password1 == $password2)) {
		$querypwd = "password = MD5('$password1'),";
	} else {
		$querypwd = "";
	}

	if ( $rm == 1) {
		$testo = "L'utente e' stato correttamente rimosso";
		$query = "	DELETE FROM 
					permessi 
				WHERE 
					id_utenti = $id
		";
		
		$query2 = "	DELETE FROM 
					utenti 
				WHERE 
					id = $id
		";

	} elseif ($id > 0) {
		$testo = "L'utente e' stato correttamente modificato";
		$query = "	UPDATE 
					utenti 
				SET 
					$querypwd 
					utente = '$utente', 
					admin = $admin, 
					attivo=$attivo, 
					modifica = NOW(), 
					mail='$mail'  
				WHERE 
					id=$id
		";
	} else {
		$testo = "L'utente e' stato correttamente inserito";
		$query = "	INSERT INTO 
					utenti 
				SET 
					$querypwd 
					utente = '$utente', 
					admin = $admin, 
					attivo=$attivo, 
					modifica = NOW(), 
					creazione = NOW(), 
					mail='$mail'
		";
	}

$tabella = new html (0,"90%", array (0));
$tabella->riga();

	if (mysql_query( $query, $DB_ID ) && ((!is_null ($query2) && mysql_query( $query2, $DB_ID )) || (is_null ($query2)))) { 
	} else {
		$testo = "Non e' stato possibile eseguire l'operazione richiesta a causa di un errore: ".mysql_error();
	}
?>
	<tr>
		<td align="center"><?php print $testo ?></td>
	</tr>
	<tr>
		<td align="center"><input type="button" value="Ok" onclick="location.href = '<?php print $CONF['base_url'] ?>/utenti/index.php';"></button></td>
	</tr>
<?php
$tabella->close();
} else {
	$bottone = "Aggiungi";
	$modifica = 0;
}

if (($modifica == 0 || $modifica == 1) && !isset($_GET['rm'])) { ?>

<script type="text/javascript" src="<?php print $CONF['base_url'] ?>/jquery/jquery.js"></script>
<script type="text/javascript" src="<?php print $CONF['base_url'] ?>/jquery/jquery.validate.js"></script>
<script type="text/javascript" src="<?php print $CONF['base_url'] ?>/jquery/jquery.validate.password.js"></script>
<link rel="stylesheet" type="text/css" href="<?php print $CONF['base_url'] ?>/jquery/jquery.validate.password.css" />

<script type="text/javascript">
function DoSubmit(){
	document.getElementById("rm").value = '1';
	return true;
}

$(document).ready(function() 
{
	$("#modulo").validate( {
		rules: {
			utente: {
				required: true,
				minlength: 3
			},
			mail: {
				required: false,
				email: true,
				minlength: 4
			},
			password1: {
				password: "#utente",
				minlength: 0
			},
			password2: {
				equalTo: "#password1"
			}
		},
		messages: {
			utente: {
				required: "Inserire uno nome utente",
				minlength: jQuery.format("Inserire almeno {0} caratteri")
			},
			password2: {
				equalTo: "Le due password devono essere uguali"
			}

		},

		errorPlacement: function(error, element) {
			error.prependTo( element.parent().next() );
		},
		// specifying a submitHandler prevents the default submit, good for the demo
		//submitHandler: function() {
		//	alert("submitted!");
		//},
		// set this class to error-labels to indicate valid fields
		success: function(label) {
			// set &nbsp; as text for IE
			label.html("&nbsp;").addClass("checked");
		}
	});
});
</script>

<form action="modutenti.php" method="post" id="modulo">
<?php 
$tabella = new html (0,"90%", array (2,20,2,20,0));
if ($modifica == 1) { ?>
<input type="hidden" name="id" id="id" value="<?php print $dato['id'] ?>">
<input type="hidden" name="rm" id="rm" value="0">
<?php } else { ?>
<input type="hidden" name="id" d="id" value="-1">
<?php } ?>
<tr>
	<td><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
	<td class="label"><label id="lusername" for="username">Nome Utente</label></td>
	<td><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
	<td class="field"><input id="utente" name="utente" type="text" value="<?php print $dato['utente']?>" maxlength="50" /></td>
	<td><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
</tr>
<?php $tabella->riga(); ?>
<tr>
	<td><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
	<td class="label"><label id="lmail" for="mail">Indirizzo E-Mail</label></td>
	<td><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
	<td class="field"><input id="mail" name="mail" type="text" value="<?php print $dato['mail']?>" maxlength="50" /></td>
	<td><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
</tr>
<?php $tabella->riga(); ?>
<tr>
	<td><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
	<td>Attivo <input name="attivo" type="checkbox" value=1 <?php if ($dato['attivo']) { print "checked=\"checked\""; } ?>></td>
	<td><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
<?php	if ($UTENTE['admin']) { ?>
	<td>Admin <input name="admin" type="checkbox" value=1 <?php if ($dato['admin']) { print "checked=\"checked\""; } ?>></td>
<?php	} else { ?>
	<td>&nbsp;</td>
<?php	} ?>
	<td><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
</tr>
<?php $tabella->riga(); ?>
<tr>
	<td class="col1" colspan="6">
<?php $tabella2 = new html (0, "100%", array(2,20,2,30,30,0)); ?>
		<tr>
			<td><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
			<td class="label"><label id="lpassword" for="password">Password</label></td>
			<td><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
			<td class="field"><input id="password1" name="password1" type="password" size="30" maxlength="32" value="<?php print $dato['password'] ?>" /></td>
			<td class="status">
				<div class="password-meter">
					<div class="password-meter-message">&nbsp;</div>
					<div class="password-meter-bg">
						<div class="password-meter-bar"></div>
					</div>
				</div>
			</td>
			<td><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		</tr>
<?php $tabella2->riga(); ?>
		<tr>
			<td><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
			<td class="label"><label id="lpassword_confirm" for="password_confirm">Conferma Password</label></td>
			<td><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
			<td class="field"><input id="password2" name="password2" type="password" size="30" maxlength="32" value="<?php print $dato['password'] ?>" /></td>
			<td class="status"></td>
			<td><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		</tr>
<?php $tabella2->close(); ?>
	</td>
</tr>
<?php $tabella->riga(); ?>
<tr><td align="center" colspan="6">
	<input type="submit" value="<?php print $bottone ?>">
	&nbsp;&nbsp;&nbsp;
	<input type="button" value="Annulla" onclick="location.href = '<?php print $CONF['base_url'] ?>/utenti/index.php';"></button>
<?php	if ($modifica == 1) { ?>
	&nbsp;&nbsp;&nbsp;
	<input type="submit" value="Rimuovi" onclick="DoSubmit()">
<?php 	} ?>
</td></tr>
<?php 
$tabella->riga(); 
$tabella->close(); 
?>
</form>
<?php }
view_footer(); ?>
