<?php require_once('Connections/poblets.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $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_insert"])) && ($_POST["MM_insert"] == "n_usuario")) {
  $insertSQL = sprintf("INSERT INTO users (`user`, nombre, pass, email) VALUES (%s, %s, %s, %s)",
                       GetSQLValueString($_POST['user'], "text"),
                       GetSQLValueString($_POST['nombre'], "text"),
                       GetSQLValueString($_POST['pass'], "text"),
                       GetSQLValueString($_POST['mail'], "text"));

  mysql_select_db($database_poblets, $poblets);
  $Result1 = mysql_query($insertSQL, $poblets) or die(mysql_error());

  $insertGoTo = "user_reg.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}

mysql_select_db($database_poblets, $poblets);
$query_Destacado = "SELECT Titulo, Autor, Resumen, Publicada FROM noticias WHERE destacada LIKE '%true%'";
$Destacado = mysql_query($query_Destacado, $poblets) or die(mysql_error());
$row_Destacado = mysql_fetch_assoc($Destacado);
$totalRows_Destacado = mysql_num_rows($Destacado);

$maxRows_noticias = 4;
$pageNum_noticias = 0;
if (isset($_GET['pageNum_noticias'])) {
  $pageNum_noticias = $_GET['pageNum_noticias'];
}
$startRow_noticias = $pageNum_noticias * $maxRows_noticias;

mysql_select_db($database_poblets, $poblets);
$query_noticias = "SELECT Id, Titulo, Autor, Fecha, Resumen, Noticia, Publicada, destacada FROM noticias WHERE destacada LIKE '%false%' AND Publicada LIKE '%true%' ORDER BY Fecha DESC";
$query_limit_noticias = sprintf("%s LIMIT %d, %d", $query_noticias, $startRow_noticias, $maxRows_noticias);
$noticias = mysql_query($query_limit_noticias, $poblets) or die(mysql_error());
$row_noticias = mysql_fetch_assoc($noticias);

if (isset($_GET['totalRows_noticias'])) {
  $totalRows_noticias = $_GET['totalRows_noticias'];
} else {
  $all_noticias = mysql_query($query_noticias);
  $totalRows_noticias = mysql_num_rows($all_noticias);
}
$totalPages_noticias = ceil($totalRows_noticias/$maxRows_noticias)-1;

mysql_select_db($database_poblets, $poblets);
$query_Recordset1 = "SELECT * FROM users WHERE registro LIKE '%Si%'";
$Recordset1 = mysql_query($query_Recordset1, $poblets) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?><!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=utf-8" />
<title>Portal de castellar-Oliveral</title>
<link href="index.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
<!--
function MM_validateForm() { //v4.0
  if (document.getElementById){
    var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
    for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]);
      if (val) { nm=val.name; if ((val=val.value)!="") {
        if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
          if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
        } else if (test!='R') { num = parseFloat(val);
          if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
          if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
            min=test.substring(8,p); max=test.substring(p+1);
            if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
      } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
    } if (errors) alert('The following error(s) occurred:\n'+errors);
    document.MM_returnValue = (errors == '');
} }
//-->
</script>
</head>

<body>
<div id="container">
<div id="top_header"><a href="index.php"><img src="img/header_top_ico.png" alt="Poblets" width="102" height="25" /></a></div>
<div id="header"></div>
<table id="sub_header" width="100%" cellpadding="2" cellspacing="0">
<tr>
<?php include("menutop.php"); ?>
</tr>
</table>
<span id="menuizcont">
<?php include("menu.php"); ?> 
</span>
<span id="dercont">
<form action="<?php echo $editFormAction; ?>" method="POST" name="n_usuario" id="n_usuario" onsubmit="MM_validateForm('nombre','','R','user','','R','pass','','R','mail','','RisEmail');return document.MM_returnValue">
<table width="100%" border="0" cellspacing="1" cellpadding="0" id="notder"> 
<tr>
<td colspan="2" class="cabeceranoticias">REGISTRO DE NUEVO USUARIO</td>
</td>
  <tr>
    <td>Nombre</td>
    <td>
      <input name="nombre" type="text" class="tablafill" id="nombre" size="50%" />
    
    </td>
  </tr>
  <tr>
    <td>Nombre de usuario</td>
    <td><input name="user" type="text" class="tablafill" id="user" size="50%" /></td>
  </tr>
  <tr>
    <td>Contraseña</td>
    <td><input name="pass" type="password" class="tablafill" id="pass" size="50%" /></td>
  </tr>
  <tr>
    <td>E-mail</td>
    <td><input name="mail" type="text" class="tablafill" id="mail" size="50%" /></td>
  </tr>
   <tr>
    <td colspan="2" align="right" style="margin-right:10px;"><input name="registro" type="submit" class="tablafill" value="Registrarse" /></td>
  </tr>
</table>
<input type="hidden" name="MM_insert" value="n_usuario" />
</form>
</span>



<div id="footer">
   <div align="center">Portal de la pedanía Castellar-Oliveral. CMSPoblets, creado por Javier Julián Olmos.
  </div>
</div>
</div>

</body>
</html>
<?php
mysql_free_result($Destacado);

mysql_free_result($noticias);

mysql_free_result($Recordset1);
?>
