<?php
    ///////////////////////////////////////////////////////////////////////////////
    // Copyright © 2003 Michael Bird. All Rights Reserved                        //
    //                                                                           //
    // This file is part of Help Center Live.                                    //
    //                                                                           //
    // Help Center Live is free software; you can redistribute it and/or modify  //
    // it under the terms of the GNU General Public License as published by      //
    // the Free Software Foundation; either version 2 of the License, or         //
    // (at your option) any later version.                                       //
    //                                                                           //
    // Help Center Live is distributed in the hope that it will be useful,       //
    // but WITHOUT ANY WARRANTY; without even the implied warranty of            //
    // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             //
    // GNU General Public License for more details.                              //
    //                                                                           //
    // You should have received a copy of the GNU General Public License         //
    // along with Help Center Live; if not, write to the Free Software           //
    // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA //
    ///////////////////////////////////////////////////////////////////////////////



    include_once("../inc/config.php");
    include_once("../inc/database.php");
    include_once("../lang/".$LANG_setting.".php");

    // Notes: Mac doesnt seem to recognise any properties of images,
    // such as its width, that are loaded in browsers cache but not
    // displayed on the page therefore we need to write an image on
    // the page if the visitor is using a mac.

    if (substr($URL_maindir, 0, 5) == "https") {
        $LIVE_ssl = "yes";
    } else {
        $LIVE_ssl = "no";
    }

    if (isset($_GET["email"])) {

        if (isset($_GET["department"])) {
            $LIVE_specific = rawurlencode($_GET["department"]);
        } else {
            $LIVE_specific = "null";
        }

        $LIVE_ip = $_SERVER["REMOTE_ADDR"];
        $LIVE_hostname = @gethostbyaddr($LIVE_ip);
        $LIVE_uid = md5($LIVE_hostname);


?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title><?php echo($COMPANY_name); ?> - Launching</title>
<script type="text/javascript" language="javascript">
<!--

    var openwin;

    function launch(x, y)
    {

        if (x == 'main') {
            if (y !== 'null') {
                openwin = window.open('<?php echo($URL_maindir); ?>/lh/live_main.php?chatid=<?php echo($LIVE_uid); ?>&department='+y,'guest_<?php echo($LIVE_uid); ?>','toolbar=no,scrollbars=yes,status=<?php echo($LIVE_ssl); ?>,location=no,menubar=no,resizable=yes,directories=no,width=<?php echo($CHAT_g_width); ?>,height=<?php echo($CHAT_g_height); ?>');
            } else {
                openwin = window.open('<?php echo($URL_maindir); ?>/lh/live_main.php?chatid=<?php echo($LIVE_uid); ?>','guest_<?php echo($LIVE_uid); ?>','toolbar=no,scrollbars=yes,status=<?php echo($LIVE_ssl); ?>,location=no,menubar=no,resizable=yes,directories=no,width=<?php echo($CHAT_g_width); ?>,height=<?php echo($CHAT_g_height); ?>');
            }
        }

        openwin.focus();
        window.close();

    }
//-->
</script>
</head>
<body ondragstart="return false;" oncontextmenu="return false;" onselectstart="return false;" onload="launch('main','<?php echo($LIVE_specific); ?>');">
<br /><br /><div align="center"><?php echo($LANG_please_wait); ?></div>
</body>
</html>
<?
    } else {

    if (isset($_GET["department"])) {
        $LIVE_specific = rawurlencode($_GET["department"]);
    }

    if (isset($_GET["refurl"])) {
        $LIVE_refurl = $_GET["refurl"];
    }

    // If department specified then find operators status for that department only
    if (isset($LIVE_specific)) {

        $LIVE_time = time();
        $LIVE_idle = $LIVE_time - $LIVE_timeout;

        $result_dep = DATABASE_query("SELECT operator FROM ".$DB_prefix."departments WHERE department='$LIVE_specific' ORDER BY id ASC");
        while($get = DATABASE_fetch($result_dep)) {
            $LIVE_operator = $get["operator"];
            $result_op = DATABASE_query("SELECT status FROM ".$DB_prefix."operators WHERE operator='$LIVE_operator' AND time>$LIVE_idle ORDER BY id ASC");

                if ($find = DATABASE_fetch($result_op)) {
                    if ($find["status"] == 1) {
                        $LIVE_online = 1;
                    }
                }
        }

        if (isset($LIVE_online)) {
            $IMAGE_live = $URL_maindir."/lh/icon.php?status=1&department=".$LIVE_specific;
            $TEXT_live = $LIVE_text." [".$LIVE_specific."] ONLINE!";
            $LIVE_launch = "main";
        } else {
            $IMAGE_live = $URL_maindir."/lh/icon.php?status=0&department=".$LIVE_specific;
            $TEXT_live = $LIVE_text." [".$LIVE_specific."] Offline";
            $LIVE_launch = "mail";
        }

            $IMAGE_online = $URL_maindir."/lh/icon.php?status=1&department=".$LIVE_specific;
            $IMAGE_offline = $URL_maindir."/lh/icon.php?status=0&department=".$LIVE_specific;


    // If no department specified then find all departments/operators status
    } else {

        $LIVE_time = time();
        $LIVE_idle = $LIVE_time - $LIVE_timeout;

        $result_dep = DATABASE_query("SELECT operator FROM ".$DB_prefix."departments ORDER BY id ASC");
        while($get = DATABASE_fetch($result_dep)) {
            $LIVE_operator = $get["operator"];
            $result_op = DATABASE_query("SELECT status FROM ".$DB_prefix."operators WHERE operator='$LIVE_operator' AND time>$LIVE_idle ORDER BY id ASC");

                if ($find = DATABASE_fetch($result_op)) {
                    if ($find["status"] == 1) {
                        $LIVE_online = 1;
                    }
                }
        }

        if (isset($LIVE_online)) {
            $LIVE_online = "1";
            $IMAGE_live = $URL_maindir."/lh/icon.php?status=1";
            $TEXT_live = $LIVE_text." ONLINE!";
            $LIVE_launch = "main";
            $LIVE_specific = "null";
        } else {
            $LIVE_online = "0";
            $IMAGE_live = $URL_maindir."/lh/icon.php?status=0";
            $TEXT_live = $LIVE_text." Offline";
            $LIVE_launch = "mail";
            $LIVE_specific = "null";
        }
            $IMAGE_online = $URL_maindir."/lh/icon.php?status=1";
            $IMAGE_offline = $URL_maindir."/lh/icon.php?status=0";

    }

        $LIVE_ip = $_SERVER["REMOTE_ADDR"];
        $LIVE_hostname = @gethostbyaddr($LIVE_ip);
        $LIVE_uid = md5($LIVE_hostname);
?>
<!--

    var date;

    function timer()
    {
        date = new Date();
        return date.getTime();
    }

    var status = "<?php echo($LIVE_online); ?>";
    var online_image = "<?php echo($IMAGE_online); ?>";
    var offline_image = "<?php echo($IMAGE_offline); ?>";
    var starttime = timer();
    var time;
    var ismac = navigator.platform.indexOf('Mac');
    var image;
    var imgwidth;
    var refurl = escape(document.referrer);
    var page = escape(document.location.toString());
    var useragent = navigator.appName + " - " + navigator.userAgent;
    var initiateflag = 0;


        function statusbar(text)
        {
            window.status=text;
        }

        function initiate_accept()
        {
            window.open('<?php echo($URL_maindir); ?>/lh/live_request.php?initiate=y&chatid=<?php echo($LIVE_uid); ?>','initiate_<?php echo($LIVE_uid); ?>','toolbar=no,scrollbars=yes,status=no,location=no,menubar=no,directories=no,width=<?php echo($CHAT_g_width); ?>,height=<?php echo($CHAT_g_height); ?>');
            document.getElementById("HCLInitiate").style.visibility='hidden';
        }

        function initiate_decline()
        {
            time = timer();
            image = new Image;
            image.src = "<?php echo($URL_maindir); ?>/lh/image_function.php?type=decline_initiate&chatid=<?php echo($LIVE_uid); ?>";
            image.onload = document.getElementById("HCLInitiate").style.visibility='hidden';
        }

        function check()
        {
            time = timer();

            if (ismac > -1) {
                imgwidth = document.getElementById("doimgreload").width;
            } else {
                imgwidth = image.width;
            }

                if (imgwidth == 2) {
                    launch('initiate','null');
                }
                if (imgwidth == 3) {
                    status = "1";
                }
                if (imgwidth == 4) {
                    status = "0";
                }
<?php
    if (!isset($_GET["text"])) {

        if (!isset($_GET["invisible"])) {
?>
            if (status == "1") {
                if (document.getElementById("live_image").src == offline_image) {
                    document.getElementById("live_image").src = online_image;
                }
            }

            if (status == "0") {
                if (document.getElementById("live_image").src == online_image) {
                    document.getElementById("live_image").src = offline_image;
                }
            }
<?php
        }
    }
?>
        }

        function start()
        {
            time = timer();

            if (ismac > -1) {
                document.getElementById("doimgreload").src = "<?php echo($URL_maindir); ?>/lh/image_live.php?footprint=yes&chatid=<?php echo($LIVE_uid); ?>&page="+page+"&refurl="+refurl+"&useragent="+escape(useragent)+"&time="+time;
                check();
            } else {
                image = new Image;
                image.src = "<?php echo($URL_maindir); ?>/lh/image_live.php?footprint=yes&chatid=<?php echo($LIVE_uid); ?>&page="+page+"&refurl="+refurl+"&useragent="+escape(useragent)+"&time="+time;
                image.onload = check;
            }

<?php if ($REQUEST_monitor_traffic == "yes") { ?>
            setTimeout("tracker();",<?php echo($TRACKER_refresh); ?>);
<?php } ?>
        }

        function tracker()
        {
            time = timer();

            if (ismac > -1) {
<?php if (isset($_GET["department"])) { ?>
                document.getElementById("doimgreload").src = "<?php echo($URL_maindir); ?>/lh/image_live.php?chatid=<?php echo($LIVE_uid); ?>&page="+page+"&refurl="+refurl+"&department=<?php echo(rawurlencode($_GET["department"])); ?>&time="+time;
<?php } else { ?>
                document.getElementById("doimgreload").src = "<?php echo($URL_maindir); ?>/lh/image_live.php?chatid=<?php echo($LIVE_uid); ?>&page="+page+"&refurl="+refurl+"&time="+time;
<?php } ?>
                check();
            } else {
                image = new Image;
<?php if (isset($_GET["department"])) { ?>
                image.src = "<?php echo($URL_maindir); ?>/lh/image_live.php?chatid=<?php echo($LIVE_uid); ?>&page="+page+"&refurl="+refurl+"&department=<?php echo(rawurlencode($_GET["department"])); ?>&time="+time;
<?php } else { ?>
                image.src = "<?php echo($URL_maindir); ?>/lh/image_live.php?chatid=<?php echo($LIVE_uid); ?>&page="+page+"&refurl="+refurl+"&time="+time;
<?php } ?>
                image.onload = check;
            }

<?php if ($REQUEST_monitor_traffic == "yes") { ?>
            if ((time - starttime) < 3600000) {
                setTimeout("tracker();",<?php echo($TRACKER_refresh); ?>);
            }
<?php } ?>

        }

        function launch(x, y)
        {
            time = timer();
                if (x == 'initiate') {
                    if (initiateflag !== 1) {
                        if (document.getElementById("HCLInitiate") !== null) {
                                document.getElementById("HCLInitiate").style.visibility="visible";
                            setTimeout("document.getElementById('HCLInitiate').style.visibility='hidden'", 15000);
                            initiateflag = 1;
                        } else {
                            window.open('<?php echo($URL_maindir); ?>/lh/live_initiate.php?chatid=<?php echo($LIVE_uid); ?>','initiate_<?php echo($LIVE_uid); ?>','toolbar=no,scrollbars=yes,status=<?php echo($LIVE_ssl); ?>,resizable=yes,location=no,menubar=no,directories=no,width=<?php echo($CHAT_g_width); ?>,height=<?php echo($CHAT_g_height); ?>');
                            initiateflag = 1;
                        }
                    }
                } else {
                    if (y !== "null") {
                        window.open('<?php echo($URL_maindir); ?>/lh/live_main.php?chatid=<?php echo($LIVE_uid); ?>&department='+y,'guest_<?php echo($LIVE_uid); ?>','toolbar=no,scrollbars=yes,status=<?php echo($LIVE_ssl); ?>,resizable=yes,location=no,menubar=no,directories=no,width=<?php echo($CHAT_g_width); ?>,height=<?php echo($CHAT_g_height); ?>');
                    } else {
                        window.open('<?php echo($URL_maindir); ?>/lh/live_main.php?chatid=<?php echo($LIVE_uid); ?>','guest_<?php echo($LIVE_uid); ?>','toolbar=no,scrollbars=yes,status=<?php echo($LIVE_ssl); ?>,location=no,resizable=yes,menubar=no,directories=no,width=<?php echo($CHAT_g_width); ?>,height=<?php echo($CHAT_g_height); ?>');
                    }
                }
        }

<?php
    if (!isset($_GET["invisible"])) {

        if (isset($_GET["text"])) {
?>
            document.write("<a href=\"javascript:launch('<?php echo($LIVE_launch); ?>', '<?php echo($LIVE_specific); ?>')\" onMouseOver=\"statusbar('Click here for Live Help - Powered by Help Center Live');return true;\" onMouseOut=\"statusbar('');return true;\"><?php echo($TEXT_live); ?></a>");
<?php
        } else {
?>
            document.write("<a href=\"javascript:launch('<?php echo($LIVE_launch); ?>', '<?php echo($LIVE_specific); ?>')\" onMouseOver=\"statusbar('Click here for Live Help - Powered by Help Center Live');return true;\" onMouseOut=\"statusbar('');return true;\"><img alt=\"Click here for support\" src=\"<?php echo($IMAGE_live); ?>\" alt=\"image\" border=\"0\" id=\"live_image\" /></a>");
<?php
        }

    }
?>


<?php if (isset($_GET["department"])) { ?>
    if (ismac > -1) {
        document.write("<img src=\"<?php echo($URL_maindir); ?>/lh/image_live.php?chatid=<?php echo($LIVE_uid); ?>&page="+page+"&refurl="+refurl+"&department=<?php echo(rawurlencode($_GET["department"])); ?>&time="+time+"\" id=\"doimgreload\" alt=\"image\" />");
    }
<?php } else { ?>
    if (ismac > -1) {
        document.write("<img src=\"<?php echo($URL_maindir); ?>/lh/image_live.php?chatid=<?php echo($LIVE_uid); ?>&page="+page+"&refurl="+refurl+"&time="+time+"\" id=\"doimgreload\" alt=\"image\" />");
    }
<?php } ?>


<?php if (!isset($_GET["x"])) { ?>
    start();
<?php } ?>
//--><?php
    }
?>