<?php
	include 'conectardb.php';
?>

<table align="center" width="650" style="table-layout:fixed;" >
<tr>
  <td width="650" style="color:#003333; font-size:18pt;">
    <b>
      <div align="center">Administrar Novedades</div>
    </b>
    <br>
</td>
</tr>
<tr>
  <td colspan="2">
  <?php agregar_novedades(); ?>
  <?php modificar_novedades(); ?>
  </td>
</tr>
<br><br>
</table>
<br>


<?php

		
    function agregar_novedades() {
	echo '
	
	<div id="procesar"></div><div id="mensaje">
	<div id="show_insert_button" align="right"><input type="button" onclick="mostrar_insertar();" value="Insertar una Novedad"></div>
		<form name="insert_formu" id="insert_formu" onsubmit="insertar()" style="display:none;" method="post">
		<br>
		<br>
		<br>
			<table rules="none" border="1">
				<tr>
					<td width="400" style="padding-right:20px;"><input type="text" class="nobord" name="insert_titulo" id="insert_titulo" width="400px" style="padding-left:10px; padding-right:10px; color:#666666; font-weight:bold;font-size: 120%;" onclick="this.value=(this.value!=\'Título\')?this.value:\'\';" onblur="this.value=(this.value==\'\')?\'Título\':this.value;" value="Título">
					</td>
					<td align="right" style="padding-left:10px; padding-right:10px;" width="200"><h3>'.date("d/m/Y").'</h3>
					</td>
				</tr>
				<tr>
					<td><textarea rows="5" cols="55" name="insert_descripcion" id="insert_descripcion" style="color:#666666; resize:none;" onclick="this.value=(this.value!=\'Descripción\')?this.value:\'\';" onblur="this.value=(this.value==\'\')?\'Descripción\':this.value;">Descripción</textarea>
						<a name="insert_paglink" id="insert_paglink" style="color: #ACACAC; padding-left:4px;">
							<input type="text" class="nobord" name="insert_pagina" id="insert_pagina" onclick="this.value=(this.value!=\'Agregue un link aquí\')?this.value:\'\';" onblur="this.value=(this.value==\'\')?\'Agregue un link aquí\':this.value;" style="width: 400px; color:#666666;" value="Agregue un link aquí">
						</a>
						<br>
						<br>
					</td>
					<td>
						<div align="right" style="padding-left:10px; padding-right:10px;">
							<table rules="none" border="0" width="150px">
								<tr>
									<td>Mostrar&nbsp;en&nbsp;Novedades:</td></tr>
								<tr>
									<td>
										<input type="radio" name="insert_hab" value="1" checked style="margin-left :15px;"> S&iacute;
										&nbsp; &nbsp; 
										<input type="radio" name="insert_hab" value="0" 		style="margin-left :15px;"> No
									</td>
								</tr>
							</table>
							<br>
							<br>
							
							<input type="button" onclick="insertar();" value="Insertar">
						</div>
						<br>
					</td>
				</tr>
			</table>
		<br>
		<br>
		<br>
		<br>
		</form>
	</div>
	<br>';
	
	
	
	
	
	
	
	
	
	
	
	
	
		/*
		$dbh = conectar();
		
		
		if($inserta){
			$consulta1 = 'SELECT * FROM novedades ORDER BY fecha DESC';
			$resultado_op = mysql_query($consulta1, $dbh) or die(mysql_error());
			$total_registro = mysql_num_rows( $resultado_op );
		}
		*/
	}
    
	function modificar_novedades() {
		$dbh = conectar();
		$consulta1 = 'SELECT * FROM novedades ORDER BY fecha DESC';
		$resultado_op = mysql_query($consulta1, $dbh) or die(mysql_error());
		$total_registro = mysql_num_rows( $resultado_op );
		
		if ($total_registro == 0){
		echo "<br><br><br><br>No hay noticias cargadas respecto al Centro de C&oacute;mputos. Presione el bot&oacute;n \"Agregar\" y cargue una nueva.";
		}
		else{
			while($row = mysql_fetch_array($resultado_op)){
				$id  = $row['id'];
				$titulo  = $row['titulo'];
				$descripcion = $row['descripcion'];
				$pagina = $row['pagina'];
				$fecha = $row['fecha'];
				
				echo '<div id="procesar"></div><div id="mensaje">';
				echo '<form name="formu'.$id.'" id="formu'.$id.'" onsubmit="enviar()" method="post">';
				echo '<table rules="none" border="1">';
				echo '<tr>';
				echo '	<td width="400"><input type="text" class="nobord" name="idtitulo'.$id.'" id="idtitulo'.$id.'" width="400px" readonly="readonly" style="padding-left:10px; padding-right:10px; width: 400px; color:#666666; font-weight:bold;font-size: 120%;" value="'.$row["titulo"].'"></td>';
				echo '	<td align="right" style="padding-left:10px; padding-right:10px;" width="200"><h3>'.date("d/m/Y", strtotime($row["fecha"])).'</h3></td></tr>';
				echo '<tr>';
				echo '<td>';
				//Descripcion: <input type="text" name="descripcion" id="descripcion">
				echo '<textarea rows="5" cols="55" name="iddescripcion'.$id.'" id="iddescripcion'.$id.'" readonly="readonly" style="color:#666666; resize:none;">'.$descripcion.'</textarea>';



				if($pagina==""){
					echo '<a name="idlinkpagina'.$id.'" id="idlinkpagina'.$id.'" style="color: #ACACAC;">';
					echo '	<input type="text" class="nobord" name="idpagina'.$id.'" id="idpagina'.$id.'" readonly="readonly" onclick="this.value=\'\';" onblur="this.value=(this.value==\'\')?\'No tiene link asociado. Agregue uno.\':this.value;"   style="padding-left:4px; width: 400px; color:#666666;" value="No tiene link asociado. Agregue uno.">';
					echo '</a>';
				}
				else{
					echo '<a name="idlinkpagina'.$id.'" id="idlinkpagina'.$id.'" style="color: #ACACAC;" class="fancybox fancybox.iframe" href='.$pagina.'>';
					echo '	<input type="text" class="nobord" name="idpagina'.$id.'" id="idpagina'.$id.'" readonly="readonly" onblur="this.value=(this.value==\'\')?this.value;"   style="padding-left:4px; width: 400px; color:#666666;" value="'.$pagina.'">';
					echo '</a>';
				}


				echo '
					<br><br></td><td>
					<div align="right" style="padding-left:10px; padding-right:10px;">
					<table rules="none" border="0" width="150px">
					<tr><td>Mostrar&nbsp;en&nbsp;Novedades:</td></tr>
					<tr><td>
						<input type="radio" name="habilitadex'.$id.'" id="idocultar1'.$id.'" value="1"';
						if($row['habilitado'] == 1){ echo 'checked'; };
						echo ' disabled style="margin-left :15px;"> S&iacute;
						&nbsp; &nbsp; 
						<input type="radio" name="habilitadex'.$id.'" id="idocultar2'.$id.'" value="0"';
						if($row['habilitado'] == 0){ echo 'checked'; };
						echo ' disabled style="margin-left :15px;"> No
					</td></tr>
					</table>
					<br>';
					
					
				echo '<input type="button" style="display:block;" name="idmodif'.$id.'" id="idmodif'.$id.'" onclick="modificar(document.formu'.$id.'.id.value);" value="Modificar">
					  <input type="button" style="display:none;" name="idguardar'.$id.'" id="idguardar'.$id.'" onclick="guardar('.$id.'); enviar('.$id.');" value="Guardar">
					  <input type="button" onclick="borrar('.$id.');" value="Borrar">
					</div>						

					<input type="hidden" name="id" id="id" value="'.$id.'">
					<input type="hidden" name="pagina'.$id.'" id="pagina'.$id.'" value="'.$pagina.'">
					<br>
					</td></tr>
					</table>
					</form>
				</div>
				<br>';
			}
		}
    }
?>

<script languaje="javascript">

function objetoAjax(){
        var xmlhttp=false;
        try {
                xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
                try {
                   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (E) {
                        xmlhttp = false;
                }
        }

        if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
                xmlhttp = new XMLHttpRequest();
        }
        return xmlhttp;
}


function enviar(id){
        //donde se mostrará el resultado
        divprocesar = document.getElementById('procesar');
        //tomamos el valor del formulario
        titulo		=document.getElementById('idtitulo'+id).value;
        descripcion	=document.getElementById('iddescripcion'+id).value;
        pagina		=document.getElementById('idpagina'+id).value;
			if(pagina == "No tiene link asociado. Agregue uno."){
				pagina = "";
			}
		
		var radios = document.getElementsByName('habilitadex'+id);

		for (var i = 0, length = radios.length; i < length; i++) {
			if (radios[i].checked) {	// do whatever you want with the checked radio
				habilitado=radios[i].value;
				break;					// only one radio can be logically checked, don't check the rest
			}
		}
		
        //instanciamos el objetoAjax
        ajax=objetoAjax();
        //usamos el medoto POST
        //archivo que realizará la operacion mostrar.php
        ajax.open("POST", "modificar_novedades.php",true);
        ajax.onreadystatechange=function() {
                if (ajax.readyState==4) {
                        //mostrar resultados en esta capa
                        divprocesar.innerHTML = ajax.responseText
                }
        }
        ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        //enviando los valores
		ajax.send("id="+id+"&titulo="+titulo+"&descripcion="+descripcion+"&pagina="+pagina+"&habilitado="+habilitado);
		
	   header("location: admin_novedades.php");
}

function borrar(id){
	var r=confirm("Seguro que desea eliminar?");
	if (r==true){
        divprocesar = document.getElementById('procesar');
        ajax=objetoAjax();
        ajax.open("POST", "borrar_novedades.php",true);
        ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		ajax.send("id="+id);
			window.location.reload();
	}
}

function insertar(){
	var radios = document.getElementsByName('insert_hab');
		for (var i = 0, length = radios.length; i < length; i++) {
			if (radios[i].checked) {
				habilitado=radios[i].value;
				break;					// only one radio can be logically checked, don't check the rest
			}
		}
	titulo = document.getElementById('insert_titulo').value;
	descripcion = document.getElementById('insert_descripcion').value;
	pagina = document.getElementById('insert_pagina').value;
		if(pagina == "Agregue un link aquí"){
			pagina = "";
		}
	
	divprocesar = document.getElementById('procesar');
	ajax=objetoAjax();
	ajax.open("POST", "insertar_novedades.php",true);
	ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	ajax.send("titulo="+titulo+"&descripcion="+descripcion+"&pagina="+pagina+"&habilitado="+habilitado);
			window.location.reload();
}
</script>
