function sendChat($to_enroll, $to_user, $msg, $enroll, $username)
{
    $t = time() - 180;
    // time to be used to check if the message can be concatinated.
    $i = 0;
    $msg = htmlentities($msg);
    $msg = mysql_real_escape_string($msg);
    $sql = "SELECT null FROM stud_data WHERE usr_roll=" . $enroll . " AND online='yes' LIMIT 1";
    $result = mysql_query($sql);
    $count = mysql_num_rows($result);
    if ($count == 0) {
        return;
    }
    $sql = "SELECT from_enroll,msg_id  FROM chat_messages WHERE ((to_enroll='" . $to_enroll . "' AND from_enroll='" . $enroll . "') OR (to_enroll='" . $enroll . "' AND from_enroll='" . $to_enroll . "')) AND time>=" . $t . " ORDER BY msg_id DESC LIMIT 1";
    $result = mysql_query($sql);
    $count = 1;
    while ($row = mysql_fetch_assoc($result)) {
        if ($row['from_enroll'] == $enroll) {
            $msg_id = $row['msg_id'];
            $newmsg = "<br>" . $msg;
            $sql = "UPDATE chat_messages SET msg=concat(msg,'" . $newmsg . "') ,time=" . time() . " WHERE msg_id=" . $msg_id . "";
            mysql_query($sql);
        } else {
            $chat_id = getLastChatId($to_enroll);
            $newsql = "INSERT INTO chat_messages (chat_id,to_enroll,to_user,from_enroll,from_user,msg,time) VALUES (" . $chat_id . ", " . $to_enroll . ", '" . $to_user . "', " . $enroll . ", '" . $username . "', '" . $msg . "', " . time() . ")";
            mysql_query($newsql);
        }
        $count = 0;
    }
    if ($count) {
        $chat_id = getLastChatId($to_enroll);
        $newsql = "INSERT INTO chat_messages (chat_id,to_enroll,to_user,from_enroll,from_user,msg,time) VALUES (" . $chat_id . ", " . $to_enroll . ", '" . $to_user . "', " . $enroll . ", '" . $username . "', '" . $msg . "', " . time() . ")";
        mysql_query($newsql);
    }
    setWritingStatus($enroll, "no");
    echo "<root success='yes'><user>" . $to_user . "</user></root>";
}
     $to_user = mysql_real_escape_string($_POST['name']);
     $t = time() - 3;
     $sql = "SELECT NULL FROM stud_data WHERE usr_roll=" . $to_roll . " AND time>=" . $t;
     $result = mysql_query($sql);
     $count = mysql_num_rows($result);
     if ($count == 1) {
         sendChat($to_roll, $to_user, $msg, $enroll, $username);
     } else {
         echo "<root success='no'><user>" . $to_user . "</user></root>";
     }
 } else {
     if ($action == "getChat") {
         getChat($to_roll, $enroll);
     } else {
         if ($action == "setWritingStatus") {
             setWritingStatus($enroll, "yes");
         } else {
             if ($action == "checkMyOnlineStatus") {
                 checkMyOnlineStatus($enroll);
             } else {
                 if ($action == "setOnlineStatus") {
                     $status = mysql_real_escape_string($_POST['status']);
                     setOnlineStatus($enroll, $status);
                 } else {
                     if ($action == "popUpChat") {
                         popUpChat($enroll);
                     }
                 }
             }
         }
     }