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

$netmask = array(	30	=>	"/30 - 255.255.255.252",
			29	=>	"/29 - 255.255.255.248",
			28	=>	"/28 - 255.255.255.240",
			27	=>	"/27 - 255.255.255.224",
			26	=>	"/26 - 255.255.255.192",
			25	=>	"/25 - 255.255.255.128",
			24	=>	"/24 - 255.255.255.0",
			16	=>	"/16 - 255.255.0.0",
			8	=>	"/8  - 255.0.0.0",
	);

if (isset($_GET['id'])) {
	$bottone = "Modifica";
	$modifica = 1;
	$query = "SELECT * FROM route_linee WHERE id = ".$_GET['id'];
	$res = mysql_query( $query, $DB_ID );
	$dato = mysql_fetch_array ( $res );

	$query = "SELECT * FROM route_ip WHERE id_route_linee = ".$_GET['id'];
	$res = mysql_query( $query, $DB_ID );
	$quanti = mysql_num_rows ($res);

} 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 admin = $admin, attivo=$attivo, modifica = NOW(), mail='$mail'  WHERE `id`=$id";
	} else {
		$testo = "L'utente e' stato correttamente inserito";
		$query = "INSERT INTO utenti SET utente = '$utente', $querypwd admin = $admin, attivo=$attivo, modifica = NOW(), creazione = NOW(), mail='$mail'";
	}
?>
<table cellpadding="0" cellspacing="0" border="0" width="90%">
	<tr>
		<td><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif" width="1" height="10" alt=""><td>
	</tr>
	<tr>
		<td>
<?php	if (mysql_query( $query, $DB_ID ) && ((!is_null ($query2) && mysql_query( $query2, $DB_ID )) || (is_null ($query2)))) { ?>
<?php print $testo ?><br>
<?php	} else { ?>
Non e' stato possibile eseguire l'operazione richiesta a causa di un errore: <?php print mysql_error() ?><br>
<?php print $query ?><br>
<?php	} ?>
		</td>
	</tr>
	<tr>
		<td><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif" width="1" height="5" alt=""></td>
	</tr>
	<tr>
		<td><input type="button" value="Ok" onclick="location.href = '<?php print $CONF['base_url'] ?>/utenti/index.php';"></button></td>
	</tr>
	<tr>
		<td><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif" width="1" height="10" alt=""></td>
	</tr>
</table>
<?php
} else {
	$bottone = "Aggiungi";
	$modifica = 0;
}
?>

<?php 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;
}

var num=<?php print $quanti ?>;
function accoda(){
        if(document.createElement && document.getElementById && document.getElementsByTagName) {
                // crea elementi
                var oTr=document.createElement("TR");
                var oTd1=document.createElement("TD");
                var oTd2=document.createElement("TD");
                var oTd3=document.createElement("TD");
                var oTd4=document.createElement("TD");
                var oField1=document.createElement("INPUT");
                var oField2=document.createElement("INPUT");
                var oField3=document.createElement("INPUT");
                var oButt=document.createElement("INPUT");

                // setta attributi
                oField1.setAttribute("type","text");
                oField1.setAttribute("name","nome"+num);
                oField2.setAttribute("type","text");
                oField2.setAttribute("name","ip"+num);
                oField3.setAttribute("type","text");
                oField3.setAttribute("name","iface"+num);
                oButt.setAttribute("type","checkbox");
                oButt.setAttribute("name","check"+num);

                // appendi al relativo padre
                oTd1.appendChild(oField1);
                oTd2.appendChild(oField2);
                oTd3.appendChild(oField3);
                oTd4.appendChild(oButt);
                oTr.appendChild(oTd1);
                oTr.appendChild(oTd2);
                oTr.appendChild(oTd3);
                oTr.appendChild(oTd4);
                document.getElementById('tabella').getElementsByTagName('TBODY')[0].appendChild(oTr);

                // incrementa variabile globale
                num++;
                document.forms.modulo.quanti.value=num;
        }
}

$(document).ready(function() 
{
	$("#modulo").validate( {
		rules: {
			utente: {
				required: true,
				minlength: 4
			},
			mail: {
				required: true,
				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")
			},
			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="modlinea.php" method="post" id="modulo">
<table cellpadding="0" cellspacing="0" border="0" width="90%">
<input type="hidden" name="quanti" value="<?php print $quanti ?>">
<?php 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" width="30" height="1" alt=""></td>
	<td class="label"><label id="lnome" for="nome">Nome Linea</label></td>
	<td class="field"><input id="nome" name="nome" type="text" value="<?php print $dato['nome']?>" maxlength="50" /></td>
	<td><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif" width="30" height="1" alt=""></td>
	<td class="label"><label id="lattivo" for="attivo">Attivo</label></td>
	<td><input name="attivo" type="checkbox" value=1 <?php if ($dato['attivo']) { print "checked=\"checked\""; } ?>></td>
	<td class="status" colspan="3"></td>
</tr>
<tr><td colspan="9"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif" width="1" height="5" alt=""></td></tr>
<tr>
	<td><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif" width="30" height="1" alt=""></td>
	<td class="label"><label id="lgw" for="gw">Gateway</label></td>
	<td class="field"><input id="gw" name="gw" type="text" value="<?php print $dato['gw']?>" maxlength="50" /></td>
	<td><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif" width="30" height="1" alt=""></td>
	<td class="label"><label id="lnm" for="gw">Netmask</label></td>
	<td class="field"><select name='nm' id="nm">
<?php 	foreach ($netmask as $i => $v) { ?>
		<option name='nm' value='<?php print $i ?>' <?php if ($i == $dato['nm']) { print "selected"; } ?>><?php print $v ?></option>
<?php	} ?>
	</select></td>
	<td class="status" colspan="3"></td>
</tr>
<tr><td colspan="9"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif" width="1" height="5" alt=""></td></tr>
<tr>
	<td><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif" width="30" height="1" alt=""></td>
	<td class="label"><label id="lch" for="ch">Check IP</label></td>
	<td class="field"><input id="ch" name="ch" type="text" value="<?php print $dato['ch']?>" maxlength="50" /></td>
	<td><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif" width="30" height="1" alt=""></td>
	<td class="label"><label id="lncheck" for="ncheck"># Check</label></td>
	<td class="field"><input id="ncheck" name="ncheck" type="text" value="<?php print $dato['ncheck']?>" maxlength="5" /></td>
	<td class="status" colspan="3"></td>
</tr>
<tr><td colspan="9"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif" width="1" height="5" alt=""></td></tr>
<tr>
	<td><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif" width="30" height="1" alt=""></td>
	<td class="label"><label id="lpri" for="pri">Priorita'</label></td>
	<td class="field"><input id="pri" name="pri" type="text" value="<?php print $dato['pri']?>" maxlength="50" /></td>
	<td class="status" colspan="6"></td>
</tr>
<tr><td colspan="9"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif" width="1" height="5" alt=""></td></tr>

<tr>
	<td colspan="9" align="center">
	<table class="col1" cellpadding="0" cellspacing="0" border="0" width="60%" id="tabella">
	<tbody>
		<tr>
			<td><b>Server</b></td>
			<td><b>Interfaccia</b></td>
			<td><b>IP</b></td>
			<td align="center"><img src="immagini/add.png" alt="" onclick="accoda()"></td>
		</tr>	
<?php	for ($conta = 0; $conta < $quanti; $conta++) {
		$scheda = mysql_fetch_array ( $res ); 
?>
		<tr>
			<input type="hidden" name="idscheda<?php print $conta?>" value="<?php print $scheda['id'] ?>">
			<td><input id="nome<?php print $conta?>" name="nome<?php print $conta?>" type="text" value="<?php print $scheda['nome'] ?>"></td>
			<td><input id="ip<?php print $conta?>" name="ip<?php print $conta?>" type="text" value="<?php print $scheda['ip'] ?>"></td>
			<td><input id="iface<?php print $conta?>" name="iface<?php print $conta?>" type="text" value="<?php print $scheda['iface'] ?>"></td>
			<td align="center"><input type="checkbox" name="check<?php print $conta?>" checked ></td>
		</tr>	
<?php	} ?>


	</tbody>
	</table>
	</td>
</tr>

<tr><td colspan="9"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif" width="1" height="5" alt=""></td></tr>
<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'] ?>/utenti/index.php';"></button>
<?php	if ($modifica == 1) { ?>
	&nbsp;&nbsp;&nbsp;
	<input type="submit" value="Rimuovi" onclick="DoSubmit()">
<?php 	} ?>
</td></tr>

<tr><td colspan="9"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif" width="1" height="10" alt=""></td></tr>
</table>
</form>
<?php } ?>

<?php view_footer(); ?>
