<?php session_start();
	//Impide que la p�gina sea vista desde el navegador.
	if (!isset($_SESSION['MM_Username'])) {
      	header ("Location: modulos/inicio/index.php");
	  }
  if (isset($_GET["order"])) $order = @$_GET["order"];
  if (isset($_GET["type"])) $ordtype = @$_GET["type"];

  if (isset($_POST["filter"])) $filter = @$_POST["filter"];
  if (isset($_POST["filter_field"])) $filterfield = @$_POST["filter_field"];
  $wholeonly = false;
  if (isset($_POST["wholeonly"])) $wholeonly = @$_POST["wholeonly"];

  if (!isset($order) && isset($_SESSION["order"])) $order = $_SESSION["order"];
  if (!isset($ordtype) && isset($_SESSION["type"])) $ordtype = $_SESSION["type"];
  if (!isset($filter) && isset($_SESSION["filter"])) $filter = $_SESSION["filter"];
  if (!isset($filterfield) && isset($_SESSION["filter_field"])) $filterfield = $_SESSION["filter_field"];

?>

<html>
<head>
<title>sadce -- aranceles</title>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
  body {
    background-color: #FFFFFF;
    color: #004080;
    font-family: Arial;
    font-size: 12px;
  }
  .bd {
    background-color: #FFFFFF;
    color: #004080;
    font-family: Arial;
    font-size: 12px;
  }
  .tbl {
    background-color: #FFFFFF;
  }
  a:link { 
    color: #FF0000;
    font-family: Arial;
    font-size: 12px;
  }
  a:active { 
    color: #0000FF;
    font-family: Arial;
    font-size: 12px;
  }
  a:visited { 
    color: #800080;
    font-family: Arial;
    font-size: 12px;
  }
  .hr {
    background-color: #336699;
    color: #FFFFFF;
    font-family: Arial;
    font-size: 12px;
  }
  a.hr:link {
    color: #FFFFFF;
    font-family: Arial;
    font-size: 12px;
  }
  a.hr:active {
    color: #FFFFFF;
    font-family: Arial;
    font-size: 12px;
  }
  a.hr:visited {
    color: #FFFFFF;
    font-family: Arial;
    font-size: 12px;
  }
  .dr {
    background-color: #FFFFFF;
    color: #000000;
    font-family: Arial;
    font-size: 12px;
  }
  .sr {
    background-color: #FFFFCF;
    color: #000000;
    font-family: Arial;
    font-size: 12px;
  }
</style>
</head>
<body>
<table class="bd" width="100%"><tr><td class="hr"><h2>Gesti&oacute;n de Aranceles</h2></td></tr></table>
<?php
  require_once('Connections/conn.php');
  mysql_select_db("siga_pregrado");
  mysql_query ("SET NAMES 'utf8'");
  
  $showrecs = 10;
  $pagerange = 10;

  $a = @$_GET["a"];
  $recid = @$_GET["recid"];
  $page = @$_GET["page"];
  if (!isset($page)) $page = 1;

  $sql = @$_POST["sql"];

  switch ($sql) {
    case "insert":
      sql_insert();
      break;
    case "update":
      sql_update();
      break;
    case "delete":
      sql_delete();
      break;
  }

  switch ($a) {
    case "add":
      addrec();
      break;
    case "view":
      viewrec($recid);
      break;
    case "edit":
      editrec($recid);
      break;
    case "del":
      deleterec($recid);
      break;
    default:
      select();
      break;
  }

  if (isset($order)) $_SESSION["order"] = $order;
  if (isset($ordtype)) $_SESSION["type"] = $ordtype;
  if (isset($filter)) $_SESSION["filter"] = $filter;
  if (isset($filterfield)) $_SESSION["filter_field"] = $filterfield;
  if (isset($wholeonly)) $_SESSION["wholeonly"] = $wholeonly;

  mysql_close($conn);
?>
<table class="bd" width="100%"><tr><td class="hr"></td></tr></table>
</body>
</html>

<?php function select()
  {
  global $a;
  global $showrecs;
  global $page;
  global $filter;
  global $filterfield;
  global $wholeonly;
  global $order;
  global $ordtype;


  if ($a == "reset") {
    $filter = "";
    $filterfield = "";
    $wholeonly = "";
    $order = "";
    $ordtype = "";
  }

  $checkstr = "";
  if ($wholeonly) $checkstr = " checked";
  if ($ordtype == "asc") { $ordtypestr = "desc"; } else { $ordtypestr = "asc"; }
  $res = sql_select();
  $count = sql_getrecordcount();
  if ($count % $showrecs != 0) {
    $pagecount = intval($count / $showrecs) + 1;
  }
  else {
    $pagecount = intval($count / $showrecs);
  }
  $startrec = $showrecs * ($page - 1);
  if ($startrec < $count) {mysql_data_seek($res, $startrec);}
  $reccount = min($showrecs * $page, $count);
?>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr><td>N&uacute;mero de Aranceles <?php echo $startrec + 1 ?> - <?php echo $reccount ?> de <?php echo $count ?></td></tr>
</table>
<hr size="1" noshade>
<form action="aranceles.php" method="post">
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<td><b>Filtrar por</b>&nbsp;</td>
<td><input type="text" name="filter" value="<?php echo $filter ?>"></td>
<td><select name="filter_field">
<option value="">Todos los Campos</option>
<option value="<?php echo "codigo" ?>"<?php if ($filterfield == "codigo") { echo "selected"; } ?>><?php echo htmlspecialchars("C�digo") ?></option>
<option value="<?php echo "descripcion" ?>"<?php if ($filterfield == "descripcion") { echo "selected"; } ?>><?php echo htmlspecialchars("Descripcion") ?></option>
<option value="<?php echo "costo_UT" ?>"<?php if ($filterfield == "costo_UT") { echo "selected"; } ?>><?php echo htmlspecialchars("Costo UT") ?></option>
<option value="<?php echo "costo_Bs" ?>"<?php if ($filterfield == "costo_Bs") { echo "selected"; } ?>><?php echo htmlspecialchars("costo_Bs") ?></option>

</select></td>
<td><input type="checkbox" name="wholeonly"<?php echo $checkstr ?>>Palabra Completa</td>
</td></tr>
<tr>
<td>&nbsp;</td>
<td><input type="submit" name="action" value="Aplicar Filtro"></td>
<td><a href="aranceles.php?a=reset">Quitar Filtro</a></td>
</tr>
</table>
</form>
<hr size="1" noshade>
<?php showpagenav($page, $pagecount); ?>
<br>
<table class="tbl" border="0" cellspacing="1" cellpadding="5"width="100%">
<tr>
<td class="hr" align="center">Ver</td>
<td class="hr" align="center">Editar</td>
<td class="hr" align="center">Borrar;</td>
<td class="hr" align="center"><a class="hr" href="aranceles.php?order=<?php echo "codigo" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("Código") ?></a></td>
<td class="hr" align="center"><a class="hr" href="aranceles.php?order=<?php echo "descripcion" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("Descripción del Arancel") ?></a></td>
<td class="hr" align="center"><a class="hr" href="aranceles.php?order=<?php echo "costo_UT" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("Costo (U.T.)") ?></a></td>
<td class="hr" align="center"><a class="hr" href="aranceles.php?order=<?php echo "costo_Bs" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("Costo (Bs.)") ?></a></td>
<td class="hr" align="center"><a class="hr" href="aranceles.php?order=<?php echo "fecha_vigencia" ?>&type=<?php echo $ordtypestr ?>"><?php echo htmlspecialchars("Fecha de Vigencia") ?></a></td>
</tr>
<?php
  for ($i = $startrec; $i < $reccount; $i++)
  {
    $row = mysql_fetch_assoc($res);
    $style = "dr";
    if ($i % 2 != 0) {
      $style = "sr";
    }
?>
<tr>
<td class="<?php echo $style ?>" align="center"><a href="aranceles.php?a=view&recid=<?php echo $i ?>"><img src="imagenes/search.gif" border="0" </a></td></a></td>
<td class="<?php echo $style ?>"align="center" ><a href="aranceles.php?a=edit&recid=<?php echo $i ?>"><img src="imagenes/edit.gif" border="0" </a></td></a></td>
<td class="<?php echo $style ?>" align="center"><a href="aranceles.php?a=del&recid=<?php echo $i ?>"><img src="imagenes/delete.png" border="0" </a></td></a></td>
<td class="<?php echo $style ?>" align="center"><?php echo htmlspecialchars($row["codigo"]) ?></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["descripcion"]) ?></td>
<td class="<?php echo $style ?>" align="right"><?php echo htmlspecialchars($row["costo_UT"]) ?></td>
<td class="<?php echo $style ?>" align="right"><?php echo htmlspecialchars($row["costo_Bs"]) ?></td>
<td class="<?php echo $style ?>" align="center"><?php echo htmlspecialchars(join ("-",(array_reverse(split("-",$row["fecha_vigencia"]))))) ?></td>
</tr>
<?php
  }
  mysql_free_result($res);
?>
</table>
<br>
<?php } ?>

<?php function showrow($row, $recid)
  {
?>
<table class="tbl" border="0" cellspacing="1" cellpadding="5" width="60%">
<tr>
<td class="hr"><?php echo htmlspecialchars("Código")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["codigo"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Descripción del Arancel")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["descripcion"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Costo (U.T.)")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["costo_UT"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Costo (Bs.)")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["costo_Bs"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Número de Resolución")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars($row["resolucion"]) ?></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Fecha de Vigencia")."&nbsp;" ?></td>
<td class="dr"><?php echo htmlspecialchars(join ("-",(array_reverse(split("-",$row["fecha_vigencia"]))))) ?></td>
</tr>
</table>
<?php } ?>

<?php function showroweditor($row, $iseditmode)
  {
  global $conn;
?>
<table class="tbl" border="0" cellspacing="1" cellpadding="5"width="50%">
<tr>
<td class="hr"><?php echo htmlspecialchars("Código")."&nbsp;" ?></td>
<td class="dr"><input name="codigo" type="text" disabled="disabled" value="<?php echo str_replace('"', '&quot;', trim($row["codigo"])) ?>" size="5" maxlength="5"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Descripción del Arancel")."&nbsp;" ?></td>
<td class="dr"><input name="descripcion" type="text" value="<?php echo str_replace('"', '&quot;', trim($row["descripcion"])) ?>" size="60" maxlength="60"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Costo (U.T.)")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="costo_UT" value="<?php echo str_replace('"', '&quot;', trim($row["costo_UT"])) ?>" ></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Costo (Bs.)")."&nbsp;" ?></td>
<td class="dr"><input readonly="true" type="text" name="costo_Bs" value="<?php echo str_replace('"', '&quot;', trim($row["costo_Bs"])) ?>"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Número de Resolución")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="resolucion" value="<?php echo str_replace('"', '&quot;', trim($row["resolucion"])) ?>"></td>
</tr>

<tr>
<td class="hr"><?php echo htmlspecialchars("Fecha de Vigencia")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="fecha_vigencia" value="<?php echo str_replace('"', '&quot;', join ("-",(array_reverse(split("-",$row["fecha_vigencia"]))))) ?>"></td>
</tr>
</table>
<?php } ?>

<?php function showrowadd($row, $iseditmode)
  {
  global $conn;
  
    $count = sql_getrecordcount();
  $count++;
  $codigo='03-'.str_pad($count,2,0,STR_PAD_LEFT);

?>
<table class="tbl" border="0" cellspacing="1" cellpadding="5" width="100%">
<tr>
<td class="hr"><?php echo htmlspecialchars("Código")."&nbsp;" ?></td>
<td class="dr">
<input disabled="disabled" name="codigo" type="text" size="2" maxlength="5" value="<?php echo str_replace('"', '&quot;', trim($codigo)) ?>" onBlur="verificar_arancel(document.addArancel.tipo.value,this.value)"> <iframe name="frameArancel" id="frameArancel" with="0" height="0" frameborder="0"></iframe></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Descripción del Arancel")."&nbsp;" ?></td>
<td class="dr"><input name="descripcion" type="text" value="<?php echo str_replace('"', '&quot;', trim($row["descripcion"])) ?>" size="60" maxlength="60"></td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Costo (U.T.)")."&nbsp;" ?></td>
<td class="dr"><input name="costo_UT" id="costo_UT" type="text"  value="<?php echo $monto_pago ?>" onKeyPress="return NumCheck(event, this)">&nbsp;&nbsp;(Para ingresar decimales utilice punto)</td>
</tr>
<tr>
<td class="hr"><?php echo htmlspecialchars("Número de Resolución")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="resolucion" value="<?php echo str_replace('"', '&quot;', trim($row["resolucion"])) ?>"></td>
</tr>

<tr>
<td class="hr"><?php echo htmlspecialchars("Fecha de Resolución")."&nbsp;" ?></td>
<td class="dr"><input name="fecha_vigencia" type="text" size="10" maxlength="10" onKeyUp="mascara(this,'-',patron,true)" onBlur="return esFechaValida(this.value)">&nbsp;&nbsp;(dd/mm/aaaa)</td>
</tr>
</table>
<?php } ?>

<?php function showpagenav($page, $pagecount)
{
?>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<td><a href="aranceles.php?a=add">Agregar Arancel</a>&nbsp;</td>
<?php if ($page > 1) { ?>
<td><a href="aranceles.php?page=<?php echo $page - 1 ?>">&lt;&lt;&nbsp;Anterior</a>&nbsp;</td>
<?php } ?>
<?php
  global $pagerange;

  if ($pagecount > 1) {

  if ($pagecount % $pagerange != 0) {
    $rangecount = intval($pagecount / $pagerange) + 1;
  }
  else {
    $rangecount = intval($pagecount / $pagerange);
  }
  for ($i = 1; $i < $rangecount + 1; $i++) {
    $startpage = (($i - 1) * $pagerange) + 1;
    $count = min($i * $pagerange, $pagecount);

    if ((($page >= $startpage) && ($page <= ($i * $pagerange)))) {
      for ($j = $startpage; $j < $count + 1; $j++) {
        if ($j == $page) {
?>
<td><b><?php echo $j ?></b></td>
<?php } else { ?>
<td><a href="aranceles.php?page=<?php echo $j ?>"><?php echo $j ?></a></td>
<?php } } } else { ?>
<td><a href="aranceles.php?page=<?php echo $startpage ?>"><?php echo $startpage ."..." .$count ?></a></td>
<?php } } } ?>
<?php if ($page < $pagecount) { ?>
<td>&nbsp;<a href="aranceles.php?page=<?php echo $page + 1 ?>">Siguiente&nbsp;&gt;&gt;</a>&nbsp;</td>
<?php } ?>
</tr>
</table>
<?php } ?>

<?php function showrecnav($a, $recid, $count)
{
?>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<td><a href="aranceles.php">Regresar</a></td>
<?php if ($recid > 0) { ?>
<td><a href="aranceles.php?a=<?php echo $a ?>&recid=<?php echo $recid - 1 ?>">Anterior</a></td>
<?php } if ($recid < $count - 1) { ?>
<td><a href="aranceles.php?a=<?php echo $a ?>&recid=<?php echo $recid + 1 ?>">Siguiente</a></td>
<?php } ?>
</tr>
</table>
<hr size="1" noshade>
<?php } ?>

<?php function addrec()
{
?>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<td><a href="aranceles.php">Regresar</a></td>
</tr>
</table>
<hr size="1" noshade>
<form name="addArancel" enctype="multipart/form-data" action="aranceles.php" method="post">
<p><input type="hidden" name="sql" value="insert"></p>
<?php
$row = array(
  "codigo" => "",
  "descripcion" => "",
  "costo_UT" => "",
  "costo_Bs" => "",
  "resolucion" => "",
  "fecha_vigencia" => "",
  "tipo" => "",
  "xcodigo" => "");
showrowadd($row, false);
?>
<p><input type="submit" name="action" value="Guardar"></p>
</form>
<?php } ?>

<?php function viewrec($recid)
{
  $res = sql_select();
  $count = sql_getrecordcount();
  mysql_data_seek($res, $recid);
  $row = mysql_fetch_assoc($res);
  showrecnav("view", $recid, $count);
?>
<br>
<?php showrow($row, $recid) ?>
<br>
<hr size="1" noshade>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<td><a href="aranceles.php?a=add">Agregar Arancel</a></td>
<td><a href="aranceles.php?a=edit&recid=<?php echo $recid ?>">Editar</a></td>
<td><a href="aranceles.php?a=del&recid=<?php echo $recid ?>">Borrar</a></td>
</tr>
</table>
<?php
  mysql_free_result($res);
} ?>

<?php function editrec($recid)
{
  $res = sql_select();
  $count = sql_getrecordcount();
  mysql_data_seek($res, $recid);
  $row = mysql_fetch_assoc($res);
  showrecnav("edit", $recid, $count);
?>
<br>
<form name="editArancel" enctype="multipart/form-data" action="aranceles.php" method="post">
<input type="hidden" name="sql" value="update">
<input type="hidden" name="xcodigo" value="<?php echo $row["codigo"] ?>">
<?php showroweditor($row, true); ?>
<p><input type="submit" name="action" value="Guardar Cambios"></p>
</form>
<?php
  mysql_free_result($res);
} ?>

<?php function deleterec($recid)
{
  $res = sql_select();
  $count = sql_getrecordcount();
  mysql_data_seek($res, $recid);
  $row = mysql_fetch_assoc($res);
  showrecnav("del", $recid, $count);
?>
<br>
<form action="aranceles.php" method="post">
<input type="hidden" name="sql" value="delete">
<input type="hidden" name="xcodigo" value="<?php echo $row["codigo"] ?>">
<?php showrow($row, $recid) ?>
<p><input type="submit" name="action" value="Confirmar"></p>
</form>
<?php
  mysql_free_result($res);
} ?>

<?php function connect()
{
  
}


function sqlvalue($val, $quote)
{
  if ($quote)
    $tmp = sqlstr($val);
  else
    $tmp = $val;
  if ($tmp == "")
    $tmp = "NULL";
  elseif ($quote)
    $tmp = "'".$tmp."'";
  return $tmp;
}

function sqlstr($val)
{
  return str_replace("'", "''", $val);
}

function sql_select()
{
  global $conn;
  global $order;
  global $ordtype;
  global $filter;
  global $filterfield;
  global $wholeonly;

  $filterstr = sqlstr($filter);
  if (!$wholeonly && isset($wholeonly) && $filterstr!='') $filterstr = "%" .$filterstr ."%";
  $sql = "SELECT `codigo`, `descripcion`, `costo_UT`, `costo_Bs`, `resolucion`,`fecha_vigencia` FROM `aranceles`";
  if (isset($filterstr) && $filterstr!='' && isset($filterfield) && $filterfield!='') {
    $sql .= " where " .sqlstr($filterfield) ." like '" .$filterstr ."'";
  } elseif (isset($filterstr) && $filterstr!='') {
    $sql .= " where (`codigo` like '" .$filterstr ."') or (`descripcion` like '" .$filterstr ."') or (`costo_UT` like '" .$filterstr ."') or (`costo_Bs` like '" .$filterstr ."') or (`fecha_vigencia` like '" .$filterstr ."')";
  }
  if (isset($order) && $order!='') $sql .= " order by `" .sqlstr($order) ."`";
  if (isset($ordtype) && $ordtype!='') $sql .= " " .sqlstr($ordtype);
  $res = mysql_query($sql, $conn) or die(mysql_error());
  return $res;
}

function sql_getrecordcount()
{
  global $conn;
  global $order;
  global $ordtype;
  global $filter;
  global $filterfield;
  global $wholeonly;

  $filterstr = sqlstr($filter);
  if (!$wholeonly && isset($wholeonly) && $filterstr!='') $filterstr = "%" .$filterstr ."%";
  $sql = "SELECT COUNT(*) FROM `aranceles`";
  if (isset($filterstr) && $filterstr!='' && isset($filterfield) && $filterfield!='') {
    $sql .= " where " .sqlstr($filterfield) ." like '" .$filterstr ."'";
  } elseif (isset($filterstr) && $filterstr!='') {
    $sql .= " where (`codigo` like '" .$filterstr ."') or (`descripcion` like '" .$filterstr ."') or (`costo_UT` like '" .$filterstr ."') or (`costo_Bs` like '" .$filterstr ."') or (`fecha_vigencia` like '" .$filterstr ."')";
  }
  $res = mysql_query($sql, $conn) or die(mysql_error());
  $row = mysql_fetch_assoc($res);
  reset($row);
  return current($row);
}

function sql_insert()
{
  global $conn;
  global $_POST;
  
  $count = sql_getrecordcount();
  $count++;
  $codigo='03-'.str_pad($count,2,0,STR_PAD_LEFT);

  
  // Busca el valor actual de la Unidad Tributaria.
  $sqlUnidad = "SELECT valor FROM unidad_tributaria";
  $rstUnidad = mysql_query($sqlUnidad, $conn) or die(mysql_error());
  $rowUnidad = mysql_fetch_assoc($rstUnidad);
  $valor_ut=$rowUnidad["valor"];

  $costo_Bs=@$_POST["costo_UT"]*$valor_ut;
    	
  $fecha_vigencia=join ("-",(array_reverse(split("-",@$_POST["fecha_vigencia"]))));
  $sql = "insert into `aranceles` (`codigo`, `descripcion`, `costo_UT`, `costo_Bs`, `resolucion`,`fecha_vigencia`) values ('$codigo', " .sqlvalue(@$_POST["descripcion"], true).", " .sqlvalue(@$_POST["costo_UT"], true).", '$costo_Bs', " .sqlvalue(@$_POST["resolucion"], true).",'$fecha_vigencia')";
  mysql_query($sql, $conn) or die(mysql_error());
}

function sql_update()
{
  global $conn;
  global $_POST;

  // Busca el valor actual de la Unidad Tributaria.
  $sqlUnidad = "SELECT valor FROM unidad_tributaria";
  $rstUnidad = mysql_query($sqlUnidad, $conn) or die(mysql_error());
  $rowUnidad = mysql_fetch_assoc($rstUnidad);
  $valor_ut=$rowUnidad["valor"];

  $costo_Bs=@$_POST["costo_UT"]*$valor_ut;
  $fecha_vigencia=join ("-",(array_reverse(split("-",@$_POST["fecha_vigencia"]))));


  $sql = "update `aranceles` set `descripcion`=" .sqlvalue(@$_POST["descripcion"], true).", `costo_UT`=" .sqlvalue(@$_POST["costo_UT"], true).", `costo_Bs`='$costo_Bs', `resolucion`=" .sqlvalue(@$_POST["resolucion"], true).", `fecha_vigencia`='$fecha_vigencia' where " .primarykeycondition();
  mysql_query($sql, $conn) or die(mysql_error());
}

function sql_delete()
{
  global $conn;

  $sql = "delete from `aranceles` where " .primarykeycondition();
  mysql_query($sql, $conn) or die(mysql_error());
}
function primarykeycondition()
{
  global $_POST;
  $pk = "";
  $pk .= "(`codigo`";
  if (@$_POST["xcodigo"] == "") {
    $pk .= " IS NULL";
  }else{
  $pk .= " = " .sqlvalue(@$_POST["xcodigo"], true);
  };
  $pk .= ")";
  return $pk;
}
 ?>
 
 
  <script type="text/javascript">
	var nav4 = window.Event ? true : false;
	function soloNum(evt)
	{	
	// NOTE: Backspace = 8, Enter = 13, '0' = 48, '9' = 57, ','= 44, '.'= 46
		var key = nav4 ? evt.which : evt.keyCode;	
		//var str=obj.value;
	
		return (key == 8 || key == 13 || (key >= 48 && key <= 57));
	}
	
	function validar_datos()
	{
	 	if(document.addAlumno.tipodoc.value==0){
			alert("El campo <<Tipo Documento Id>> es obligatorio");
			document.addAlumno.tipodoc.focus();
			return false;
			}
		if(document.addAlumno.nacionalidad.value==0){
			alert("El campo <<Nacionalidad>> es obligatorio");
			document.addAlumno.nacionalidad.focus();
			return false;
			}
			
		if(document.addAlumno.cedula.value==""){
			alert("El campo <<Documento Id>> es obligatorio");
			document.addAlumno.cedula.focus();
			return false;
			}
				
		if(document.addAlumno.apellidos.value==""){
			alert("El campo <<Apellidos>> es obligatorio");
			document.addAlumno.apellidos.focus();
			return false;
			}
		if(document.addAlumno.nombres.value==0){
			alert("El campo <<Nombres>> es obligatorio");
			document.addAlumno.nombres.focus();
			return false;
			}
		if(document.addAlumno.genero.value==0){
			alert("El campo <<G�nero>> es obligatorio");
			document.addAlumno.genero.focus();
			return false;
			}
			if(document.addAlumno.edocivil.value==0){
			alert("El campo <<Estado Civil>> es obligatorio");
			document.addAlumno.edocivil.focus();
			return false;
			}
		if(document.addAlumno.fechanac.value==""){
			alert("El campo <<Fecha de Nacimiento>> es obligatorio");
			document.addAlumno.fechanac.focus();
			return false;
			}
			return true;
	}
	
	function mostrar_foto(ruta) 
	{
		if(ruta!="")
		{
		//document.addAlumno.fotop.src="";
		document.form2.submit();
		}
	}
	
	function cargaMunicipio(idEstado)
{
    
 		 url="viewMuni.php?idEstado"+idEstado;
	 iframeMunicipio.location=url;
	
    
}

function verificar_arancel(tipo,cod)
{
   codigo=tipo+"-"+cod;
   url="validar_arancel.php?codigo="+codigo;
   frameArancel.location=url;
}

function calcular_costo(ut)
{
   alert(ut);
   url="calcular_bs.php?ut="+ut;
   frameCosto.location=url;
}

function calcular_costoE(ut)
{
   alert(ut);
   url="calcular_bsE.php?ut="+ut;
   frameCosto.location=url;
}
</script>

<script type="text/javascript">
function NumCheck(e, field) {
  key = e.keyCode ? e.keyCode : e.which

  // backspace
  if (key == 8) return true
  
  // 0-9
  if (key >= 48 && key <= 57) {

    if (field.value == "") return true
    
    regexp = /.[0-9]{2}$/
    return !(regexp.test(field.value))
  }

  // ,
  if (key == 46) {
  
    if (field.value == "") return false
    
    regexp = /^[0-9]+$/
    return regexp.test(field.value)
  }
  
  // other key
  return false

}

</script>

<script>
function esFechaValida(fecha){
    if (fecha != undefined && fecha != "" ){
       
    	var dia  =  parseInt(fecha.substring(0,2),10);
		var mes  =  parseInt(fecha.substring(3,5),10);
		var anio =  parseInt(fecha.substring(6,10),10);
	
		if (isNaN(dia) || isNaN(mes) || isNaN(anio)) {
		   alert("Formato de fecha no v�lido");
		   document.addAlumno.fechanac.focus();
       return false;
	 }
	
    switch(mes){
        case 1:
			numDias=31;
            break;
        case 3:
			numDias=31;
            break;
        case 5:
			numDias=31;
            break;
        case 7:
			numDias=31;
            break;
        case 8: 
			numDias=31;
            break;
        case 10:
			numDias=31;
            break;
        case 12:
            numDias=31;
            break;
        case 4: case 6: case 9: case 11:
            numDias=30;
            break;
        case 2:
           if (comprobarSiBisisesto(anio)){ numDias=29 }else{ numDias=28};
            break;
        default:
            alert("Error en fecha introducida (Mes Inv�lido)");
			document.addAlumno.fechanac.focus();
            return false;
    }
 
        if (dia>numDias || dia==0){
            alert("Error en fecha introducida (D�a Inv�lido)");
            document.addAlumno.fechanac.focus();
			return false;
        }
        return true;
    }
}
 
function comprobarSiBisisesto(anio){
if ( ( anio % 100 != 0) && ((anio % 4 == 0) || (anio % 400 == 0))) {
    return true;
    }
else {
    return false;
    }
}
</script>

<script type="text/javascript">
var patron = new Array(2,2)
var patron1= new Array(3,2)
var patron2 = new Array(1,3,3,3,3)

function mascara(d,sep,pat,nums){
if(d.valant != d.value){
	val = d.value
	largo = val.length
	val = val.split(sep)
	val2 = ''
	for(r=0;r<val.length;r++){
		val2 += val[r]	
	}
	if(nums){
		for(z=0;z<val2.length;z++){
			if(isNaN(val2.charAt(z))){
				letra = new RegExp(val2.charAt(z),"g")
				val2 = val2.replace(letra,"")
			}
		}
	}
	val = ''
	val3 = new Array()
	for(s=0; s<pat.length; s++){
		val3[s] = val2.substring(0,pat[s])
		val2 = val2.substr(pat[s])
	}
	for(q=0;q<val3.length; q++){
		if(q ==0){
			val = val3[q]
		}
		else{
			if(val3[q] != ""){
				val += sep + val3[q]
				}
		}
	}
	d.value = val
	d.valant = val
	}
}
</script>
