<?php

    function writepagetitle($title = "", $onBodyLoad = "", $onBodyResize = "", $additionalJavaScript = "")
    {
        global $siteroot, $autorefresh;
        if (!isset($title)) $title = "Calcul compensări";
        if (strlen($title) == 0) $title = "Calcul compensări";
        ?>
        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
        <html>
        <head>
            <title><?php echo $title; ?></title>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8;">
            <link href="<?=$siteroot?>css/main.css" rel="stylesheet" type="text/css">
			<script type="text/javascript" src="<?=$siteroot?>js/main.js"></script>
            <script type="text/javascript" src="<?=$siteroot?>js/datetimepicker.js"></script>
			<?
			if ($additionalJavaScript) echo "<script type='text/javascript' src='{$siteroot}js/{$additionalJavaScript}'></script>";
			?>
        </head>
        <body <? if ($onBodyLoad) echo "onload='$onBodyLoad'";
                 if ($onBodyResize) echo " onresize='$onBodyResize'";
              ?>>
        <?
    }

    function writepageheader()
    {
        global $sitepath, $imagesroot, $authenticated, $errorMessage;
        ?>
        <div class='container' id='container'>
            <div class='header'>
                <img src="<?=$imagesroot?>logo.png" width="1000" height="96">
            </div>
            <iframe id='ajax_frame' style="width:0; height:0; border:none;"></iframe>
            <div class='content'>
            <?
            if ($errorMessage) echo "<center><div class='error'>$errorMessage</div></center>";
    }

    function writepagefooter()
    {
        global $imagesroot;
        ?>
            </div>
            <div class='footer'>
                Copyright &copy;&nbsp;&nbsp;<a href='http://www.setrio.ro' target='blank'><img src='<?=$imagesroot ?>setrio.png' alt='Setrio Soft' style='border: none; position: relative; top: 10px' valign='middle'></a>&nbsp;,&nbsp; 2010
            </div>
            </div>
        </div>
        </body>
        </html>
        <?
    }

    function mysql_datetime($datetime = 0)
    {
        if (!$datetime) $datetime = time();
        return date('Y-m-d H:i:s', $datetime);
    }

    function mysql_date($date)
    {
        if (!$date) $date = time();
        return date('Y-m-d', $date);
    }

    function get_mysql_datetime($datetime)
    {
        return mktime(substr($datetime, 11, 2), substr($datetime, 14, 2), substr($datetime, 17, 2),
                      substr($datetime, 5, 2), substr($datetime, 8, 2), substr($datetime, 0, 4));
    }

    function get_formatted_mysql_datetime($datetime, $show_seconds = true)
    {
        if ($datetime)
            return date("d.m.Y H:i".(($show_seconds)?":s":""),
                        mktime(substr($datetime, 11, 2), substr($datetime, 14, 2), substr($datetime, 17, 2),
                               substr($datetime, 5, 2), substr($datetime, 8, 2), substr($datetime, 0, 4)));
        else
            return "-";
    }

    function get_mysql_date($date)
    {
        return mktime(0, 0, 0, substr($date, 5, 2), substr($date, 8, 2), substr($date, 0, 4));
    }

    function get_formatted_mysql_date($date)
    {
        if ($date)
            return date("d.m.Y", mktime(0, 0, 0, substr($date, 5, 2), substr($date, 8, 2), substr($date, 0, 4)));
        else
            return "-";
    }

    function status_name($status)
    {
        $result = "Unknown";
        switch ($status)
        {
            case "N": $result = lang('statOpen'); break;
            case "F": $result = lang('statRet'); break;
            case "A": $result = lang('statAck'); break;
            case "W": $result = lang('statWork'); break;
            case "V": $result = lang('statTBV'); break;
            case "K": $result = lang('statOK'); break;
            case "C": $result = lang('statClosed'); break;
        }
        return $result;
    }

    function has_right($right)
    {
        $rights_array = explode(';', $right);
        $rights = "'{$rights_array[0]}'";
        for ($i = 1; $i < count($rights_array); $i++) $rights .= ",'".$rights_array[$i]."'";
        $q = mysql_query("SELECT IFNULL(MAX(enabled), 0) AS enabled FROM group_users g ".
                         "JOIN group_rights gr ON gr.id_group = g.id_group ".
                         "JOIN rights r ON r.id = gr.id_right ".
                         "WHERE (g.id_user = {$_SESSION['user_id']}) AND (r.right IN ($rights))");
        if ($r = mysql_fetch_assoc($q)) $result = ($r['enabled'] == 1);
                                   else $result = 0;
        return $result;
    }

    function save_operation($operation_type, $id_element, $description)
    {
        mysql_query("INSERT INTO operations (id_user, id_type, id_element, description) ".
                    "SELECT {$_SESSION['user_id']}, id, $id_element, '$description' ".
                    "FROM operation_types ".
                    "WHERE type = '$operation_type'");
        $result = mysql_error() == '';
        return $result;
    }
    
    function luna($luna)
    {
        switch ($luna)
        {
            case  1: return 'ian.';
            case  2: return 'feb.';
            case  3: return 'mar.';
            case  4: return 'apr.';
            case  5: return 'mai';
            case  6: return 'iun.';
            case  7: return 'iul.';
            case  8: return 'aug.';
            case  9: return 'sep.';
            case 10: return 'oct.';
            case 11: return 'noi.';
            case 12: return 'dec.';
        }
    }
?>