<?php 
session_start();
require_once('../Connections/paivaconex.php');
require_once 'funcoes.php';
require_once 'controls/verifylogin.php';


?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}


if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form2")) {
  $updateSQL = sprintf("UPDATE mfin_clientes SET Tipo=%s, Cnpj=%s, NomeFantasia=%s, Telefone=%s, Conta=%s, ContaResultado=%s WHERE Id=%s",
                       GetSQLValueString($_POST['Tipo'], "text"),
                       GetSQLValueString($_POST['Cnpj'], "text"),
                       GetSQLValueString($_POST['NomeFantasia'], "text"),
                       GetSQLValueString($_POST['Telefone'], "text"),
                       GetSQLValueString($_POST['Conta'], "text"),
                       GetSQLValueString($_POST['ContaResultado'], "int"),
                       GetSQLValueString($_POST['Id'], "int"));

  mysql_select_db($database_paivaconex, $paivaconex);
  $Result1 = mysql_query($updateSQL, $paivaconex) or die(mysql_error());
}

mysql_select_db($database_paivaconex, $paivaconex);
$query_contas = "SELECT * FROM mfin_contas ORDER BY Numero ASC";
$contas = mysql_query($query_contas, $paivaconex) or die(mysql_error());
$row_contas = mysql_fetch_assoc($contas);
$totalRows_contas = mysql_num_rows($contas);

mysql_select_db($database_paivaconex, $paivaconex);
$query_contas2 = "SELECT * FROM mfin_contas ORDER BY Numero ASC";
$contas2 = mysql_query($query_contas2, $paivaconex) or die(mysql_error());
$row_contas2 = mysql_fetch_assoc($contas2);
$totalRows_contas2 = mysql_num_rows($contas2);

$colname_clientes = "-1";
if (isset($_GET['id'])) {
  $colname_clientes = $_GET['id'];
}
mysql_select_db($database_paivaconex, $paivaconex);
$query_clientes = sprintf("SELECT * FROM mfin_clientes WHERE Id = %s", GetSQLValueString($colname_clientes, "int"));
$clientes = mysql_query($query_clientes, $paivaconex) or die(mysql_error());
$row_clientes = mysql_fetch_assoc($clientes);
$totalRows_clientes = mysql_num_rows($clientes);


//USING



?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>M&#63464;dulo Financeiro</title>

<?php include_once('controls/import.php'); ?>

<script src="../SpryAssets/SpryValidationTextField.js" type="text/javascript"></script>
<link href="../SpryAssets/SpryValidationTextField.css" rel="stylesheet" type="text/css" />
</head>
<body>

<?php include_once('controls/header.php'); ?>

<div class="content">

<div class="form"><?php include_once('controls/menu.php'); ?>

<div id="conteudo-site">

<div class="titulo-conteudo-site"><img src="images/applications.png"
	title="" alt="" /> Altera&Ccedil;&Atilde;O de Clientes</div>
<p>&nbsp;</p>
<p>&nbsp;</p>
<form action="<?php echo $editFormAction; ?>" method="post" name="form2" id="form2">
<table align="center">
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Tipo:</td>
      <td><select name="Tipo">
        <option value="pf" <?php if (!(strcmp("pf", htmlentities($row_clientes['Tipo'], ENT_COMPAT, '')))) {echo "SELECTED";} ?>>Pessoa Fisica</option>
        <option value="pj" <?php if (!(strcmp("pj", htmlentities($row_clientes['Tipo'], ENT_COMPAT, '')))) {echo "SELECTED";} ?>>Pessoa Juridica</option>
      </select></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">CPF / CNPJ:</td>
      <td><span id="sprytextfield2">
        <input type="text" name="Cnpj" value="<?php echo htmlentities($row_clientes['Cnpj'], ENT_COMPAT, ''); ?>" size="32" />
        <span class="textfieldInvalidFormatMsg">Formato inválido.</span></span></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Nome:</td>
      <td><span id="sprytextfield1">
        <input type="text" name="NomeFantasia" value="<?php echo htmlentities($row_clientes['NomeFantasia'], ENT_COMPAT, ''); ?>" size="32" />
        <span class="textfieldRequiredMsg">Um valor é necessário.</span></span></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Telefone:</td>
      <td><span id="sprytextfield3">
        <input type="text" name="Telefone" value="<?php echo htmlentities($row_clientes['Telefone'], ENT_COMPAT, ''); ?>" size="32" />
</span></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Conta:</td>
      <td><select name="Conta">
        <?php 
do {  
?>
        <option value="<?php echo $row_contas['Id']?>" <?php if (!(strcmp($row_contas['Id'], htmlentities($row_clientes['Conta'], ENT_COMPAT, '')))) {echo "SELECTED";} ?>><?php echo $row_contas['Numero'].' '.$row_contas['Nome']?></option>
        <?php
} while ($row_contas = mysql_fetch_assoc($contas));
?>
      </select></td>
    </tr>
    <tr> </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Conta Resultado:</td>
      <td><select name="ContaResultado">
        <?php 
do {  
?>
        <option value="<?php echo $row_contas2['Id']?>" <?php if (!(strcmp($row_contas2['Id'], htmlentities($row_clientes['ContaResultado'], ENT_COMPAT, '')))) {echo "SELECTED";} ?>><?php echo $row_contas2['Numero'].' '.$row_contas2['Nome']?></option>
        <?php
} while ($row_contas2 = mysql_fetch_assoc($contas2));
?>
      </select></td>
    </tr>
    <tr> </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">&nbsp;</td>
      <td><input type="submit" value="Atualizar registro" /></td>
    </tr>
  </table>
  <input type="hidden" name="Id" value="<?php echo $row_clientes['Id']; ?>" />
  <input type="hidden" name="MM_update" value="form2" />
  <input type="hidden" name="Id" value="<?php echo $row_clientes['Id']; ?>" />
</form>
<p>&nbsp;</p>
<p>&nbsp;</p>
</div>

</div>

</div>

			<?php include_once('controls/footer.php'); ?>

<script type="text/javascript">
var sprytextfield1 = new Spry.Widget.ValidationTextField("sprytextfield1");
var sprytextfield2 = new Spry.Widget.ValidationTextField("sprytextfield2", "integer", {isRequired:false});
var sprytextfield3 = new Spry.Widget.ValidationTextField("sprytextfield3", "none", {isRequired:false});
</script>
</body>
</html>
<?php
mysql_free_result($contas);

mysql_free_result($contas2);

mysql_free_result($clientes);
?>
