<?php
include_once ("config.php");
$UTENTE = login();
view_top();

if (isset ($_POST['id'])) {
	$modifica = 2;
	$id 		= $_POST['id'];
	$utente 	= $_POST['utente'];
	$password1 	= $_POST['password1'];
	$password2	= $_POST['password2'];
	$mail		= $_POST['mail'];

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

	$testo = "L'utente e' stato correttamente modificato";
	$query = "UPDATE utenti SET $querypwd modifica = NOW(), mail='$mail'  WHERE `id`=$id";

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

	if (mysql_query( $query, $DB_ID )) { 
		$testo = "L'utente e' stato correttamente modificato";
	} 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'] ?>/index.php';"></td></tr>
<?php 
	$tabella->riga(); 
	$tabella->close();
} else {

	$bottone = "Modifica";
	$modifica = 1;
	$query = "SELECT * FROM utenti WHERE utente = '".$UTENTE['utente']."'";
	$res = mysql_query( $query, $DB_ID );
	$dato = mysql_fetch_array ( $res );
	$dato['password'] = "ahg5t!frtb5@u&^!";
}

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">
$(document).ready(function() 
{
	$("#modulo").validate( {
		rules: {
			mail: {
				required: true,
				email: true,
				minlength: 4
			},
			password1: {
				password: "#utente",
				minlength: 0
			},
			password2: {
				equalTo: "#password1"
			}
		},
		messages: {
			mail: {
				required: "Inserire un indirizzo e-mail valido",
			},
			password2: {
				equalTo: "Le due password devono essere uguali"
			}

		},

		// the errorPlacement has to take the table layout into account
		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="chkpasswd.php" method="post" id="modulo">
<?php $tabella = new html (0,"90%", array (2, 20,2,20,0)); ?>
<input type="hidden" name="id" value="<?php print $dato['id'] ?>">
<tr>
	<td><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
	<td>Nome Utente</td>
	<td><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
	<td><?php print $dato['utente']?></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 class="col1" colspan="5">

<?php $tabella2 = new html (0,"90%",array(2,10,2,35,40,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" width="30"><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">Password</label></td>
			<td><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
			<td class="field" width="30"><input id="password2" name="password2" type="password" size="30" maxlength="32" value="<?php print $dato['password'] ?>"/></td>
			<td class="status">&nbsp;</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="9">
	<input type="submit" value="<?php print $bottone ?>">
	&nbsp;&nbsp;&nbsp;
	<input type="button" value="Annulla" onclick="location.href = '<?php print $CONF['base_url'] ?>/index.php';"></button>
</td></tr>

<?php 
$tabella->riga(); 
$tabella->close();
?>
</form>
<?php 
} 
view_footer(); 
?>
