<?php

include("../bd/sql_variables.php");

$db_conexion= mysql_connect("$sql_host", "$sql_usuario", "$sql_pass") or die(header ("No Disponible Servidor de Base de Datos"));
mysql_select_db("$sql_db");

$ugids=$_GET['ugid'];
$idj=$_GET['idj'];
$ids=$_GET['ids'];
$msg=$_GET['msg'];

//pregunta por las jefaturas
$ugid='';
$sqljefe="SELECT ug_id as ugid FROM tb_usuprogdep where j_id='$idj' and s_id='$ids' and ug_id like '%JEF%'";
$resultjefe = mysql_query($sqljefe);
while($rowj= mysql_fetch_array($resultjefe)){
	$ugid= $rowj['ugid'];
}

$sqlask="select m_id as id from tb_mensajeria where j_id='$idj' and s_id='$ids' and ug_id='$ugid'";
$resultsql = mysql_query($sqlask);
	if(mysql_num_rows($resultsql)==0){
	$idm = 1;
	}else{
	while($row= mysql_fetch_array($resultsql)){
	$idm= $row['id'] + 1;
	}}

$sqlinsertmail="insert into tb_mensajeria values ('$idm', now(),'$idj','$ids','$ugid','$ugids',upper('$msg'))";
$resultconsult = mysql_query($sqlinsertmail);
//echo "<script type='text/javascript'>window.opener.location.reload(); window.close();</script>";
//echo $sqlinsertmail;

//armando grupo correo
$sqldepa="select j_id as codj, 0 as cods ,j_abrev as nom from tb_jefatura where j_id='$idj'";
$sqlserv="select j_id as codj, s_id as cods, s_abrev as nom from tb_servicio where j_id='$idj'";
$resultdepa = mysql_query($sqldepa);
$resultserv = mysql_query($sqlserv);

$cadenamail="<table align='center' class='FacetFormTABLE'>";
$cadenamail=$cadenamail."<tr><td class='FacetFieldCaptionTDT' colspan='2' align='center'>MENSAJERIA</td></tr>";
$cadenamail=$cadenamail."<tr><td><b>Seleccione Remitente:</b></td><td><b>Escribe Mensaje:</b></td></tr><tr><td>";
$cadenamail=$cadenamail."<table align='center' class='FacetFormTABLE' height='150'>";
while ($rowj= mysql_fetch_array($resultdepa)){
$cadenamail=$cadenamail."<tr><td><input type='radio' id='msn' name='msn' value='".$rowj['cods']."'>".$rowj['nom']."</td></tr>";
}
while ($rows= mysql_fetch_array($resultserv)){
$cadenamail=$cadenamail."<tr><td><input type='radio' id='msn' name='msn' value='".$rows['cods']."'>".$rows['nom']."</td></tr>";
}
$cadenamail=$cadenamail."</table></td><td>";
$cadenamail=$cadenamail."<table align='center' class='FacetFormTABLE' height='150'>";
$cadenamail=$cadenamail."<tr><td align='center'><textarea name='msntexto' id='msntexto' cols='28' rows='6'></textarea></td></tr>";
$cadenamail=$cadenamail."</table></td></tr><tr><td align='center' colspan='2'><input type='button' value='Enviar' class='FacetButton' onClick='enviarmailper()'></td></tr></table>";
//echo "<script type='text/javascript'>location.reload();</script>";

echo $cadenamail;
?>