<?
session_start();
require("conectar.php");
$id=$_POST['usu'];
$des=$_POST['destino'];
$msg=$_POST['mensaje'];
$ver=$_POST['ver'];
$idmsg=$_POST['idmsg'];
if($ver==1){
      $sql="INSERT INTO chat (id_chat,mensaje,fecha,hora,emisor,destino,status,notificacion) VALUES ('','$msg',CURDATE(),CURTIME(),'$id','$des','','')";
	 mysql_query($sql);
	
/*	  echo"<script>alert();</script>";
 $sql="SELECT * FROM chat WHERE (emisor='$id' and destino='$des') OR (destino='$id' AND emisor='$des') order by fecha, hora";
		 $con=mysql_query($sql);
		 while($ls=mysql_fetch_array($con)){
		     $sql="SELECT alias FROM usuchat WHERE id_usu='$id'";
			 $em=mysql_query($sql);
			     $le=mysql_fetch_array($em);
				 $yo=$le["alias"];
			 $sql="SELECT alias FROM usuchat WHERE id_usu='$des'";
			 $em=mysql_query($sql);
			     $le=mysql_fetch_array($em);
				 $tu=$le["alias"];
		     if($ls["emisor"]==$id){$align="left";$usu="Tu";$s="escribiste";}
		     if($ls["destino"]==$id){$align="right";$usu=$tu; $s="escribi&oacute;";}
		    ?>
			<div class="<?if($ls["status"]==""){?>msgbnl<?}else{?>msgb<?}?>"  align="<?echo $align?>" id="m<?echo $ls["id_chat"]?>" name="<?$ls["status"]?>"name="">
			   <p style="font-size:10px;color:#747474"><?echo $usu." ".$s;?>:</p>
			    <p><?echo $ls["mensaje"]?></p>
				<font class="hora" style="color:#999999"><?echo $ls["hora"]?></font>
			</div>
			<?
			}*/
	 }
	 if($ver==2){
	     $sql="SELECT * FROM chat WHERE (emisor='$id' and destino='$des') OR (destino='$id' AND emisor='$des') order by fecha, hora ";
		 $con=mysql_query($sql);
		 while($ls=mysql_fetch_array($con)){
		     $sql="SELECT alias FROM usuchat WHERE id_usu='$id'";
			 $em=mysql_query($sql);
			     $le=mysql_fetch_array($em);
				 $yo=$le["alias"];
			 $sql="SELECT alias FROM usuchat WHERE id_usu='$des'";
			 $em=mysql_query($sql);
			     $le=mysql_fetch_array($em);
				 $tu=$le["alias"];
		     if($ls["emisor"]==$id){$align="left";$usu="Tu";$s="escribiste";}
		     if($ls["destino"]==$id){$align="right";$usu=$tu;$s="escribi&oacute;";}
			 
		    ?>
			<div class="<?if($ls["status"]==""){?>msgbnl<?}else{?>msgb<?}?>" align="<?echo $align?>" id="m<?echo $ls["id_chat"]?>" name="">
			     <p style="font-size:10px;color:#747474"><?echo $usu." ".$s;?>:</p>
			     <p><?echo $ls["mensaje"]?></p>
				 <font class="hora" style="color:#999999"><?echo $ls["hora"]." ".$ls["fecha"]?></font>
		</div>
			<?
		 }
	
	}
if($ver==3){
      $sql="UPDATE chat SET status='leído' WHERE id_chat='$idmsg' AND emisor='$des'";
	  $con=mysql_query($sql);
}
if($ver==4){
      $sql="UPDATE chat SET notificacion='notificado' WHERE id_chat='$idmsg'";
	  $con=mysql_query($sql);
}		 
?>