<?php
function parse_text_to_pseudo_html($text)
{
	// convert tags:
	$text = ereg_replace("<b>", "[b]", $text);									// [b]	bold
	$text = ereg_replace("</b>", "[/b]", $text);
	$text = ereg_replace("<i>", "[i]", $text);									// [i]	bold
	$text = ereg_replace("</i>", "[/i]", $text);
	$text = ereg_replace("<h2>", "[h2]", $text);								// [h2] title2
	$text = ereg_replace("</h2>", "[/h2]", $text);
	$text = ereg_replace("<p class=\"a_left\">", "[align=left]", $text);		// [p]	normal
	$text = ereg_replace("<p class=\"a_center\">", "[align=center]", $text);	// [p]	center
	$text = ereg_replace("</p>", "[/align]", $text);
	$text = ereg_replace("<img src=\"", "[img]", $text);						// [img]
	$text = ereg_replace("\">", "[/img]", $text);



	return $text;
}

function js_editor_menu()
{
	echo "<input type='button' value='B' onclick=\"addPTag(document.getElementById('doc_body'),'b')\")'>";
	echo "<input type='button' value='I' onclick=\"addPTag(document.getElementById('doc_body'),'i')\")'>";
	echo "<input type='button' value='T' onclick=\"addPTag(document.getElementById('doc_body'),'h2')\")'>";
	echo "<input type='button' value='Left' onclick=\"addPTags(document.getElementById('doc_body'),'align=left','/align')\")'>";
	echo "<input type='button' value='Center' onclick=\"addPTags(document.getElementById('doc_body'),'align=center','/align')\")'>";
	echo "<input type='button' value='Imagen' onclick=\"img()\">";
	echo "<input type='button' value='Adjuntar archivos' 
		   onclick=\"javascript:window.open('ftp.php?op=1&id=$_GET[id]','popup','width=300, height=360, scrollbars=NO, top=200, left=200')\">";
	echo "<button type='button' onclick=\"javascript:window.open('ftp.php?op=2&id=$_GET[id]','popup','width=300, height=360, scrollbars=NO, top=200, 			   left=200')\">
		  <img src='files/pics/atach.png'></button>";
}

function load_form( $form )
{
	if ($form == "mk_sec")
	{
		// nivel de usuario requerido para publicar
		if ($_SESSION['user_type'] >= 4)
		{
			echo "<FORM method='post' ACTION='form_action.php'>";
			echo "<FIELDSET>";
		  	echo "<legend>Agregar seccion:</legend>";
			echo "<label>Nombre:</label>";
			echo "<input type='text' name='sec_name' size='25'>";
			echo "<label></label>";	
			echo "<input type='submit' value='Agregar' name='mk_sec'>";
			echo "<input type='submit' value='Cancelar'>";
			echo "</FIELDSET>";
			echo "</FORM>";
		}
		else
		{
			$error = "Acceso denegado.";
			echo "<META HTTP-EQUIV='Refresh' CONTENT='0; URL=./?info=$error'>";
			return 0;
		}
	}
	else if ($form == "del_sec")
	{
		// nivel de usuario requerido para eliminar
		if ($_SESSION['user_type'] >= 5)
		{
			$link = mysql_connect($_SESSION['BD_server'], $_SESSION['BD_user'], $_SESSION['BD_pass']);
			mysql_select_db($_SESSION['BD_name'], $link);

			$result1 = mysql_query("SELECT * FROM section", $link);
	
			$n_sec = 0;

			echo "<FORM method='post' ACTION='form_action.php'>";
			echo "<FIELDSET>";
			echo "<legend>Escoja las secciones que desee eliminar:</legend>";
			echo "<table>";
			echo "<tr class='title'><th></th><th>Seccion</th></tr>";	
			while ($row1 = mysql_fetch_row($result1))
			{
				$n_sec++;
				echo "<tr><th width='25'><input name='$n_sec' type='checkbox' value='$row1[1]'>
					  </th><th>$row1[1]</th></tr>";
			}
			echo "</table>";
			echo "<input type='hidden' name='n_sec' value='$n_sec'>";
			echo "<label></label>";
			echo "<input type='submit' value='Eliminar' name='del_sec'>";
			echo "<input type='submit' value='Cancelar'>";
			echo "</FIELDSET>";
			echo "</FORM>";
		}
		else
		{
			$error = "Acceso denegado.";
			echo "<META HTTP-EQUIV='Refresh' CONTENT='0; URL=./?info=$error'>";
			return 0;
		}
	}
	else if ($form == "mk_sub_sec")
	{
		// nivel de usuario requerido para publicar
		if ($_SESSION['user_type'] >= 4)
		{
			echo "<FORM method='post' ACTION='form_action.php'>";
			echo "<FIELDSET>";
			echo "<legend>Agregar sub-seccion en <b class='form'>$_GET[sec_ref]</b>:</legend>";
			echo "<input type='hidden' name='sec' value='$_GET[sec_ref]'>";
			echo "<label>Nombre:</label>";
			echo "<input type='text' name='sub_sec_name' size='25'>";
			echo "<label></label>";
			echo "<input type='submit' value='Agregar' name='mk_sub_sec'>";
			echo "<input type='submit' value='Cancelar'>";
			echo "</FIELDSET>";
			echo "</FORM>";
		}
		else
		{
			$error = "Acceso denegado.";
			echo "<META HTTP-EQUIV='Refresh' CONTENT='0; URL=./?info=$error'>";
			return 0;
		}
	}
	else if ($form == "del_sub_sec")
	{
		// nivel de usuario requerido para eliminar
		if ($_SESSION['user_type'] >= 5)
		{
			$link = mysql_connect($_SESSION['BD_server'], $_SESSION['BD_user'], $_SESSION['BD_pass']);
			mysql_select_db($_SESSION['BD_name'], $link);

			$result1 = mysql_query("SELECT id FROM section WHERE name = '$_GET[sec_ref]'", $link);
			$row1 = mysql_fetch_row($result1);

			$result2 = mysql_query("SELECT name FROM sub_section WHERE id_sec = '$row1[0]'", $link);
	
			$n_sub_sec = 0;

			echo "<FORM method='post' ACTION='form_action.php'>";
			echo "<FIELDSET>";
			echo "<legend>Escoja las sub-secciones que desee eliminar de <b class=form>$_GET[sec_ref]</b>:</legend>";
			echo "<table>";
			echo "<tr class='title'><th></th><th>Sub-seccion</th></tr>";	
			while ($row2 = mysql_fetch_row($result2))
			{
				$n_sub_sec++;
				echo "<tr><th width='25'><input name='$n_sub_sec' type='checkbox'  value='$row2[0]'>
					  </th><th>$row2[0]</th></tr>";
			}
			echo "</table>";
			echo "<input type='hidden' name='n_sub_sec' value='$n_sub_sec'>";
		   	echo "<input type='hidden' name='id_sec' value='$row1[0]'>";
			echo "<label></label>";
			echo "<input type='submit' value='Eliminar' name='del_sub_sec'>";
			echo "<input type='submit' value='Cancelar'>";
			echo "</FIELDSET>";
			echo "</FORM>";
		}
		else
		{
			$error = "Acceso denegado.";
			echo "<META HTTP-EQUIV='Refresh' CONTENT='0; URL=./?info=$error'>";
			return 0;
		}
	}
	else if ($form == "login")
	{
		if($_SESSION["user"])
		{
			$error = "Ya estas registrado.";
			echo "<META HTTP-EQUIV='Refresh' CONTENT='0; URL=./?info=$error'>";
			return 0;
		}
	   	echo "<FORM method='post' ACTION='form_action.php'>";
		echo "<FIELDSET>";
		echo "<legend>Identificandose:</legend>";
		echo "<label>Nombre:</label>";
		echo "<input type='text' name='name' size='15'>";
		echo "<label>Password:</label>";
		echo "<input type='password' name='pass' size='15'>";
		echo "<label></label>";
		echo "<input type='submit' value='Entrar' name='login'>";
		echo "<input type='button' value='Registrarse' name='register' onclick=\"location.href='?form=register'\">";
		echo "<input type='button' value='Cancelar' onclick=\"location.href='./index.php'\">";
		echo "</FIELDSET>";
    	echo "</FORM>";
	}
	else if ($form == "register")
	{
		if($_SESSION["user"])
		{
			$error = "Ya estas registrado.";
			echo "<META HTTP-EQUIV='Refresh' CONTENT='0; URL=./?info=$error'>";
			return 0;
		}
    	echo "<FORM method='post' ACTION='form_action.php'>";
		echo "<FIELDSET>";
		echo "<legend>Registrandose:</legend>";
		echo "<label>Nombre:</label>";
		echo "<input type='text' name='name' size='15'>";
		echo "<label>Password:</label>";
		echo "<input type='password' name='pass1' size='15'>";
		echo "<label>Repita el password:</label>";
		echo "<input type='password' name='pass2' size='15'>";
		echo "<label>E-mail:</label>";
		echo "<input type='text' name='email' size='15'>";
		echo "<label></label>";
		echo "<input type='submit' value='Registrarse' name='register'>";
		echo "<input type='submit' value='Cancelar'>";
		echo "</FIELDSET>";
    	echo "</FORM>";
	}
	else if ($form == "edit_doc")
	{
		$link = mysql_connect($_SESSION['BD_server'], $_SESSION['BD_user'], $_SESSION['BD_pass']);
		mysql_select_db($_SESSION['BD_name'], $link);

		$result1 = mysql_query("SELECT * FROM document WHERE id = '$_GET[id]'", $link);
		$row = mysql_fetch_assoc($result1);

		// editable solo si se es autor o se tiene el nivel necesario
		if ($_SESSION['user_type'] >= 3 || ($_SESSION['user'] == $row['autor']))
		{
			$body = parse_text_to_pseudo_html($row['body']);

			// editor script
			echo "<script language='JavaScript' type='text/javascript' src='javascript/text_editor.js'></script>";

			echo "<FORM name='f_edit' METHOD='post' ACTION='form_action.php'>";
			echo "<FIELDSET>";
			echo "<legend>Editando documento <b class='form'>$row[title]</b></legend>";
			echo "<label>Titulo: </label>";
			echo "<input type='text' name='title' value='$row[title]' size='50'>";
			echo "<label>Contenido: </label>";
			// editor menu
			js_editor_menu();
			echo "<textarea id='doc_body' name='body' rows='25' cols='100'>$body</textarea>";
		 	echo "<input type='hidden' name='id' value='$_GET[id]'>";
			echo "<label></label>";

			if ($row['state'] == 0 && $_SESSION['user'] == $row['autor'])
			{
				echo "<input type='submit' value='Publicar' name='publicate_doc'>";
				echo "<input type='submit' value='Guardar' name='edit_doc'>";
			}
			else if ($row['state'] > 0)
				echo "<input type='submit' value='Guardar' name='edit_doc'>";

			echo "<input type='submit' value='Cancelar' name='del_docs'>";
			echo "</FIELDSET>";	
			echo "</FORM>";
		}
		else
		{
			$error = "Acceso denegado.";
			echo "<META HTTP-EQUIV='Refresh' CONTENT='0; URL=./?info=$error'>";
			return 0;
		}
	}
	else if ($form == "del_doc")
	{
		// nivel de usuario requerido para eliminar
		if ($_SESSION['user_type'] >= 5)
		{
			$link = mysql_connect($_SESSION['BD_server'], $_SESSION['BD_user'], $_SESSION['BD_pass']);
			mysql_select_db($_SESSION['BD_name'], $link);

			$get_sec = mysql_query("SELECT id FROM section WHERE name = '$_GET[sec_ref]'", $link);
			$sec = mysql_fetch_assoc($get_sec);

			$get_docs = mysql_query("SELECT * FROM document WHERE id_sec = '$sec[id]' AND id_sub_sec = '$_GET[sub_sec_ref]'", $link);
	
			$n_docs = 0;

			echo "<FORM method='post' ACTION='form_action.php'>";
			echo "<FIELDSET>";
			echo "<legend>Escoja los documentos que desee eliminar de <b class='form'>$_GET[sec_ref]</b>:</legend>";
		   	echo "<input type='hidden' name='sec' value='$_GET[sec_ref]'>";
			echo "<table>";
			echo "<tr class='title'><th></th><th>Titulo</th><th>Sub-seccion</th><th>Autor</th></tr>";	
			while ($doc = mysql_fetch_assoc($get_docs))
			{
				$n_docs++;
				echo "<tr><th width='25'><input name='$n_docs' type='checkbox' value='$doc[id]'>
					  </th><th>$doc[title]</th><th>$doc[id_sub_sec]</th><th>$doc[autor]</th></tr>";
			}
			echo "</table>";
		   	echo "<input type='hidden' name='n_docs' value='$n_docs'>";
			echo "<label></label>";
			echo "<input type='submit' value='Eliminar' name='del_docs'>";
			echo "<input type='submit' value='Cancelar' name='del_docs'>";
			echo "</FIELDSET>";
			echo "</FORM>";
		}
		else
		{
			$error = "Acceso denegado.";
			echo "<META HTTP-EQUIV='Refresh' CONTENT='0; URL=./?info=$error'>";
			return 0;
		}
	}
	else if ($form == "activate")
	{
		$link = mysql_connect($_SESSION['BD_server'], $_SESSION['BD_user'], $_SESSION['BD_pass']);
		mysql_select_db($_SESSION['BD_name'], $link);

		$result1 = mysql_query("SELECT * FROM document WHERE id = '$_GET[id]'", $link);
		$row = mysql_fetch_assoc($result1);

		echo "<FORM METHOD='post' ACTION='form_action.php'>";
		echo "<FIELDSET>";
		echo "<legend>Introduce codigo de activacion: $_GET[user]";
    	echo "<label>Codigo: </label>";
    	echo "<input type='text' name='code' size='50'>";
    	echo "<input type='submit' value='Activar' name='activate'>";
    	echo "<input type='hidden' name='user' value='$_GET[user]'>";
		echo "</FIELDSET>";
		echo "</FORM>";
	}
}  
?> 
