<?php

if ( ! @include_once( './mainfile.php' ) ) { exit( '!! INCLUDE ERROR. ACCESS DENIED !!' ); }
error_reporting( _E_VALUE );


/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | myPHPNuke Version 188_8 RC2                                          |
// +----------------------------------------------------------------------+
// | License: GNU/GPL - http://www.gnu.org/copyleft/gpl.html              |
// +----------------------------------------------------------------------+
// | Support: http://myphpnuke.com/                                       |
// +----------------------------------------------------------------------+
// | Author: myPHPNuke Dev Team                                           |
// +----------------------------------------------------------------------+
// | Copyright: © 2001-2004 myPHPNuke Dev Team                            |
// +----------------------------------------------------------------------+
// $Id: eventcaladmin.php,v 1.7.2.5 2004/08/27 15:58:46 kirilt Exp $



$Cal_Ver    = "EventCal 2.0a v BlackV_Life";     # Program version
/************************************************************************/
/* myPHPNuke: Event Calendar Module                                     */
/* ==================================================================== */
/*  Displays calendar of events and allows admin maintenance            */
/*                                                                      */
/* Copyright (c) 2001 by Daniel C. Raney (Daniel@Raney.net)             */
/* All rights reserved                                                  */
/* New Version by BlackV_Life for myphpnuke 1.8 (www.black-voodoo.de)   */
/* Adapted by BlackV_Life for myphpnuke 1.8                             */
/* Adapted by MiDnighT www.midnight.fr.st for phpnuke 5.0               */
/* ==================================================================== */
/* This program 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.       */
/************************************************************************/
/* Change Log                                                           */
/*  v2.0a BlackV_Life (6-29-01)                                         */
/*  now eventfunctions.php exist                                        */
/*  add javascript for eventform to add easy url & co.                  */
/*  fix some small bugs                                                 */
/*                                                                      */
/*  v2.0 BlackV_Life (6-22-01)                                          */
/*  Add admin page to easy activate Events                              */
/*  Seperate Configuration File, German Languge File for myphpnuke, more*/
/*  fields now, month / year selection, registrated User can add        */
/*  events, more settingoptions, more admin funktions                   */
/*                                                                      */
/*  v1.3 BlackV_Life (5-27-01)                                          */
/*  eventcal is now fully operationnal as a myphpnuke 1.8  module       */
/*  Added  german translation, Added more automatic translations        */
/*  Added timefunction, start use myphpnuke var                         */
/*                                                                      */
/*  v MiDnighT (5-10-01)                                                */
/*  eventcal is now fully operationnal as a phpnuke 5.0 module          */
/*  Added  translation                                                  */
/*                                                                      */
/*  v1.22b (4-30-01) DCR                                                */
/*       Fixed typo (missing "$" in variable name) in FieldsOkay() func */
/*                                                                      */
/*  v1.21b (4-29-01) DCR                                                */
/*       Fixed problem when no records exist in table which caused an   */
/*       early exit w/incomplete page output                            */
/*       - replaced "exit" w/"return" in DisplayEvent()                 */
/*       Also fixed typo on a closing tag ">/font>" to "</font>"        */
/*                                                                      */
/*  v1.2b (4-28-01) DCR                                                 */
/*       Fixed security hole                                            */
/*                                                                      */
/*  v1.1 (4-27-01) DCR                                                  */
/*       Replaced echo <<< with echo "... for PHP3 compatibility        */
/*       Added some missing </endtag>'s (Netscape's not as forgiving!)  */
/*       Changed page arg from "op" to "cal_op" (Nuke5 conflict)        */
/*                                                                      */
/*  v1.0 (2-26-01) DCR                                                  */
/*       Original version                                               */
/************************************************************************/
#-------------------------------------------------------------------------
# include myphpNuke files
#-------------------------------------------------------------------------
//include("extention.inc");
//include("config.$phpEx");
include("EventsConfig.$phpEx");
include("eventfunctions.$phpEx");

//include_once ("mainfile.$phpEx");
if ($mpn_set_timezone==1) {
   if ($mpn_host_use_safemode==1) {
           putenv("PHP_TZ=$mpn_timezone");
   } else {
           putenv("TZ=$mpn_timezone");
   }
}
include("header.$phpEx");

#----------------------------------------------------
# calendar variables
#----------------------------------------------------
$Cal_MoNames = Array(translate("Jan"), translate("Feb"), translate("Mar"), translate("Apr"), translate("May"), translate("Jun"), translate("Jul"), translate("Aug"), translate("Sep"), translate("Oct"), translate("Nov"), translate("Dec"));
$Cal_MoNamesF = Array(translate("January"), translate("February"), translate("March"), translate("April"), translate("May"), translate("June"), translate("July"), translate("August"), translate("September"), translate("October"), translate("November"), translate("December"));

$Cal_CurYear = date("Y",mktime(date("H")+$serv_offset));
$Cal_CurMonth = date("m",mktime(date("H")+$serv_offset));

if (!isset($Cal_Year)) $Cal_Year = $Cal_CurYear;       # default Year to display is current
if (!isset($Cal_Month)) $Cal_Month = $Cal_CurMonth;    # default Month to display is current

$Cal_CurDate = date("Y-m-d H:i",mktime(date("H")+$serv_offset));
$Cal_CurDate = strftime(translate("forumdatestring2"),strtotime($Cal_CurDate));


#==================================================================
#   Function:       GetMonthEvents
#   Purpose:        Retrieves a events for a month and stores in array
#   Arguments:      $yr Year to retrieve
#                   $mo Month to retrieve
#   Returns         Array(Array())
#==================================================================
function GetMonthEvents ( $yr, $mo )
{
    global $mpnTables, $Cal_AllowHTML;


    $q = "SELECT * FROM ".$mpnTables['eventcal']." WHERE eventgo='0' and (((Year(date_from) = $yr) and (Month(date_from) = $mo)) or ((Year(date_to) = $yr) and (Month(date_to) = $mo))) ORDER BY date_from ASC";


    $result = mysql_query($q) or die(mysql_error());
    $recs = array();
    $html_data = array( 'eventname1', 'eventdesc' );
    $click_it = array( 'eventmail', 'eventweb' );

    for( $i = 0; $i <= mysql_num_rows( $result ); $i++ )
    {
        $rowdata = mysql_fetch_assoc( $result );

        if ( (bool)FALSE !== $rowdata )
        {
            $_SERVER['SAN'] -> slashIT( $rowdata );

            if ( $Cal_AllowHTML )
            {
                foreach( $rowdata AS $key => $v )
                {
                    if ( in_array( $key, $html_data ) )
                    {
                        $_SERVER['SAN'] -> adjustFormat( $rowdata[ $key ], 3, 10 );
                    }

                    if ( in_array( $key, $click_it ) )
                   {
                        $_SERVER['SAN'] -> makeClickable( $rowdata[ $key ] );
                    }
                }
            }

            $recs[ $i ] = $rowdata;
        }
    }


    /*while ($row = mysql_fetch_array($result) )
    {
        $recs[$r++] = Array(
        "id" => $row["id"],
        "date_from" => $row["date_from"],
        "date_to" => $row["date_to"],
        "eventname" =>$row["eventname"],
        "eventname1" =>$row["eventname1"],
        "eventmail" =>$row["eventmail"],
        "eventweb" =>$row["eventweb"],
        "eventdesc" =>$row["eventdesc"],
        "eventgo" => $row["eventgo"],
        "uid" => ($row["uid"]));
    }*/

    return $recs;
}

#==================================================================
#   Function:   DispMonthEvents
#   Purpose:    Displays a month of event records
#   Arguments:  $mo Month number events are for
#               $recs   Array of record data
#   Returns     nothing
#==================================================================
function DispMonthEvents ( $mo, $recs )
{
    global $Cal_MoNamesF, $Cal_MoNames, $Cal_CurDate, $Cal_Year ;
    global $Cal_Month, $Cal_CurYear, $Cal_MaxEventDays, $Cal_MaxAdminAdvanceYears;
    global $bgcolor1, $bgcolor2, $bgcolor3, $bgcolor4, $textcolor1, $textcolor2, $textcolor3;
    global $user, $userinfo, $cookie, $mpnTables, $phpEx;

    if ( empty( $recs ) || $recs == '' )
    {
        echo translate( 'No Events Found' );
        return;
    }

    for ($r = 0; $r < Count($recs); $r++) {
        if ($r % 2) {
            $bgcolor = $bgcolor3;
        } else {
            $bgcolor = $bgcolor4;
        }
        $id = $recs[$r]["id"];
        $name = $recs[$r]["eventname"];
        $desc = $recs[$r]["eventdesc"];
        $eventmail = $recs[$r]["eventmail"];
        $eventweb = $recs[$r]["eventweb"];
        $eventname1 = $recs[$r]["eventname1"];
        $eventuid = $recs[$r]["uid"];
        $eventgo = $recs[$r]["eventgo"];

# get the dates and separate months/days


        $from = $recs[$r]["date_from"];
        $to = $recs[$r]["date_to"];
        $m = GetDatePart($from, "m");
        $m2 = GetDatePart($to, "m");
        $d = GetDatePart($from, "d");
        $d2 = GetDatePart($to, "d");

# if start month is not same as current month
# Say it's continued from...
        if ($m <> $mo)
        {
            $monidx = ( ( $m - 1 ) == -1 ) ? ( 0 ) : ( $m - 1 );

            $name = '<small>[cont from '.$Cal_MoNames[ $monidx ].' '.$d.']</small> '.$name;
            $dates = '01';
            if ($d2 > 1) $dates = $dates.' - '.$d2;

# if end month is not same as current month
# include ending month name in date range
        } elseif ($m2 <> $mo)
        {
            $dates = $d.' - '.$Cal_MoNames[$m2-1].' '.$d2;

# else the months are the same as current
# modify for range of days or a single day
        } else {
            if ($d <> $d2)
            {
                $dates = $d.' - '.$d2;
            } else {
                $dates = $d;
            }
        }
            echo "<table border='0' cellpadding='3' cellpadding='0' align='center' valign='top' width='100%'><tr><td>\n";
            echo "<table border='0' cellpadding='2' cellpadding='1' width='100%' bgcolor='$bgcolor3'>\n";
            echo "<tr bgcolor='$bgcolor2' valign='top' align='left'>\n";
            echo "<td width='30%'><b><font color='$textcolor1'>$dates</font></b></td>\n";
            echo "<td width='70%'><b><font color='$textcolor1'>$name</font></b></td></tr>";
            echo "<tr bgcolor='$bgcolor1' valign='top' align='left'>\n";
            echo "<td width='30%'><b><font color='$textcolor2'><b>$eventmail</b></font></b></td>\n";
            echo "<td width='70%'><b><font color='$textcolor2'><b>$eventname1</b></font></b></td></tr>";
            echo "<tr bgcolor='$bgcolor1' valign='top' align='left'>\n";
            if ($eventweb <> "")
            {
            echo "<td width='30%'><b><font color='$textcolor2'><b>$eventweb</b></font></b></td>\n";
              } else {
             echo "<td bgcolor='$bgcolor1' width='30%'><b><font color='$textcolor2'><b>$eventweb</b></font></b></td>\n";
          }
            echo "<td bgcolor='$bgcolor1' width='70%'><b><font color='$textcolor3'><b>$desc</b></font></b></td></tr>";
            echo "</td></tr></table>";

            echo "<table width=\"100%\" border=0 cellpadding=2 cellspacing=0><tr bgcolor='$bgcolor3'><td>";

             if ($eventgo==0 ){
                      echo "[ <a href=eventcaladmin.$phpEx?cal_op=edit&id=$id>".Translate("Event Activate")."</a> ]</td>";
                    } else {
                       echo "<td>[ <a href=eventcaladmin.$phpEx?cal_op=edit&id=$id>".Translate("Event deactivate")."</a> ]</td>\n";
                    }

                       echo "<td align='right'>";
                       echo "[ <a href=eventcaladmin.$phpEx?cal_op=edit&id=$id>".Translate("Edit")."</a> | ";
                       echo "<a href=eventcaladmin.$phpEx?cal_op=del&id=$id>".Translate("Delete")."</a> | ";
                       echo "<a href=eventcaladmin.$phpEx?cal_op=dupe&id=$id>".Translate("Copy")."</a> ]</td>\n";

                       echo "</tr></table>";
        echo "</td></tr></table>";

    }
}

#==================================================================
#   Function:       DisplayEvents
#   Purpose:        gets count of events for a year and if any exist
#                   calls DispMonthEvents to display every month.
#   Arguments:      $year, $month
#   Display all events for given year or year/month
#==================================================================
function DisplayEvents ($year, $month)
{
    global $Cal_MoNamesF, $Cal_CurDate, $Cal_Year, $Cal_Month, $Cal_CurYear, $Cal_CurMonth;
    global $Cal_MaxEventDays, $Cal_MaxAdminAdvanceYears, $mpnTables, $phpEx;
    global $bgcolor1, $bgcolor2, $textcolor1, $textcolor2;

    $active=0;
    $q = "SELECT id FROM ".$mpnTables['eventcal']." WHERE eventgo = 0 && date_to >= CURDATE()";
    $result = mysql_query($q) or die(mysql_error());
    $ct = mysql_num_rows( $result );

    if ($ct == 0)
    {
        echo "<center><br><p><b>".translate("Sorry, NO Event records were found who need to activate")."</b><br>";
        return;
    }

    echo '<h1>' . translate ( 'The Following Events Have Been Submitted For Approval' ) . '</h1>' . "\n";
    echo '<hr class="w80" /><br />' . "\n";

    for ($mo = 1; $mo <= 12; $mo++)
    {
        $recs = GetMonthEvents($year, $mo);
        if (Count($recs) > 0)
        {
            $mname = $Cal_MoNamesF[$mo-1];

            if ($textcolor1 == $bgcolor1)
            {
                $mcolor = $textcolor2;
            } else {
                $mcolor = $textcolor1;
            }
            echo "<table border=\"0\" width=\"100%\" CELLSPACING=0>\n";
            echo "<tr bgcolor=\"$bgcolor2\" align=\"left\">\n";
            echo "<td width=\"100%\"><font size=2 color=\"$textcolor1\"><b>&nbsp; $mname</b></font></td>\n";
            echo "</tr>\n";
            echo "</table>";
            OpenTable();
            DispMonthEvents($mo, $recs);
            CloseTable();
            echo "<br>";
            echo "<br>";
        }
    }
}


#==================================================================
#   Function:   DisplayCal
#   Purpose:        Display all events for given year
#   Arguments:  $year, $month
#   Returns:        nothing
#==================================================================
function DisplayCal ($year, $month)
{
    global $Cal_CurDate, $Cal_Year, $Cal_Month, $Cal_CurYear, $Cal_MaxEventDays;
    global $Cal_MaxAdminAdvanceYears, $Cal_Ver, $serv_offset;
    global $user, $userinfo, $cookie, $mpnTables,$phpEx;
    global $Cal_OnlyAdmin;


    $maxyear = $Cal_CurYear + $Cal_MaxAdminAdvanceYears;

    echo "<center>
    <font size=3><b>" . translate("Event Calender Admin") . " $year</b></font>";

    echo "<p><font size=1>".translate("Today is ").translate(date("l")).date(", $Cal_CurDate")."<br>";

    #year to display
    echo "<form method=\"POST\" action=\"" . $_SERVER['PHP_SELF'] . "\">
    ".translate("Year to display").":  <select name=\"Cal_Year\" size=\"1\">";
    for ($yr = $Cal_CurYear; $yr <= $maxyear; $yr++) {
        echo '<option ';
        if ($yr == $year) echo 'selected ';
        echo 'value="'.$yr.'">'.$yr.'</option>';
    }
    echo "</select>";
    echo " - ";

    echo "<input class=\"button\" type=\"submit\" value=\"Go\" name=\"B1\"></form></font>";

                echo "<br>[ <a href=eventcaladmin.$phpEx?cal_op=new>" . translate("Add EVENT") . "</a> ] ";
                echo "[ <a href=eventcal.php>" . translate("Event Calender") . "</a> ]";
                echo "</td></tr><tr><td>";

    DisplayEvents($year, $month);   # Display all events for year


}

#==================================================================
#   Function:   GetCalRec
#   Purpose:        Retrieve a record from the database
#   Arguments:  $id     record id to retrieve
#   Returns:        Associative Array
#                   "id"            => id,
#                   "date_from" => date_from,
#                   "date_to"   => date_to,
#                   "eventname" => eventname
#                   "eventdesc" => eventdesc
#==================================================================
function GetCalRec ($id)
{
    global $mpnTables;
    $ret = array();

    $q = "SELECT * FROM ".$mpnTables['eventcal']." WHERE id = $id";
    $result = mysql_query($q) or die (mysql_error());
    $ret = mysql_fetch_array($result);

    $_SERVER['SAN'] -> slashIT( $ret );

    //$ret["eventname"] = stripslashes($ret["eventname"]);
    //$ret["eventdesc"] = stripslashes($ret["eventdesc"]);

    return $ret;
}

#================================================================
#   Function:   ShowEventForm
#   Purpose:        Displays form to add/edit/del an event record
#   Arguments:
#       $op     cal_op code for hidden field
#       Following fields may be void (when adding new record)
#       $id     Record ID for hidden field
#       $from       Event Date FROM
#       $to     Event Date TO
#       $name       Event Name
#       $desc       Event Description
#       $eventgo    Event Active
#       $eventmail  Event Mail address
#       $eventweb   Event Web address
#       $eventname1 Evnet Name 1
#   Returns:        nothing
#================================================================

function ShowEventForm ($ope, $id, $from, $to, $name, $desc, $eventgo, $eventmail, $eventweb, $eventname1) {
    global $Cal_CurDate, $Cal_Year, $Cal_Month, $Cal_CurYear, $Cal_MaxEventDays;
    global $Cal_MaxAdminAdvanceYears, $Cal_AllowHTML, $Cal_Ver, $Cal_AdminCeck;
    global $bgcolor1, $bgcolor2, $bgcolor3, $textcolor1, $textcolor2;
    global $user, $userinfo, $cookie, $mpnTables,$phpEx;

            $lastyear = $Cal_CurYear+$Cal_MaxAdminAdvanceYears;

        if ($ope != "savedel") {
              $submit = "".translate("Submit")."";
        } else {
              $submit = "".translate("Event Delete")."";
        }

    echo "<form method=\"POST\" action=\"" . $_SERVER['PHP_SELF'] . "\" NAME=\"coolsus\">";
    echo "<TABLE BORDER=\"0\" CELLPADDING=\"1\" CELLPADDING=\"3\">\n
                <TR><TD><B><font COLOR=\"$textcolor2\">" . translate("Event Name") . " :</FONT></B></TD></TR>
                <TR><TD><input type=\"text\" name=\"eventname\" size=\"40\" value=\"$name\"></TD></TR>
                <TR><TD>&nbsp;</TD></TR>
                <TR><TD><B><font COLOR=\"$textcolor2\">" . translate("More Event Name") . " :</FONT></B></TD></TR>
                <TR><TD><textarea class=textbox wrap=virtual cols=70 rows=10 name=eventname1>$eventname1</textarea></TD></TR>";
if ($Cal_AllowHTML) {
       echo "<TR><TD>";
                HTMLbuttons_eventname1();
        echo"</TD></TR>";
}
        echo "<TR><TD>&nbsp;</TD></TR>
                <TR><TD><B><font COLOR=\"$textcolor2\">" . translate("Event Description") . " :</FONT></B></TD></TR>
                <TR><TD><textarea class=textbox wrap=virtual cols=70 rows=20 name=eventdesc>$desc</textarea></TD></TR>";
if ($Cal_AllowHTML) {
    echo "<TR><TD>";
                HTMLbuttons_eventdesc();
    echo"</TD></TR>";
}
echo "</TABLE><P>\n";
echo "<I>".translate("Be sure that the Event End date is the same or after the Event Start date!")."</I><br>";
echo "<TABLE BORDER=0>\n
    <TR><TD><B><font COLOR=\"$textcolor2\">" . translate("Event Start") . " :</FONT></B>&nbsp;\n
              <input type=\"text\" name=\"date_from\" size=\"12\" maxlength=\"10\" value=\"$from\"> " . translate("(yyyy-mm-dd)") . "</TD></TR>\n
    <TR><TD><B><font COLOR=\"$textcolor2\">" . translate("Event End") . " :</FONT></B>&nbsp;&nbsp;&nbsp;\n
             <input type=\"text\" name=\"date_to\" size=\"12\" maxlength=\"10\" value=\"$to\"> " . translate("(yyyy-mm-dd)") . "</TD></TR>\n
    <TR><TD>&nbsp;</TD></TR>
</TABLE>\n
<TABLE BORDER=0>\n
    <TR><TD><B><font COLOR=\"$textcolor2\">" . translate("Contact Event Mail") . " :</FONT></B></TD></TR>\n
    <TR><TD><INPUT class=textbox TYPE=\"text\" NAME=\"eventmail\" SIZE=40 VALUE=\"$eventmail\"></TD></TR>\n";
/*if ($Cal_AllowHTML) {
    echo"<TR><TD>";
                //HTMLbuttons_eventmail();
    echo"</TD></TR>\n";
}*/
    if ( $eventweb == '' ) { $eventweb = 'http://'; }

    echo"<TR><TD>&nbsp;</TD></TR>
    <TR><TD><B><font COLOR=\"$textcolor2\">" . translate("Contact Event Web") . " :</FONT></B></TD></TR>\n
    <TR><TD><INPUT class=textbox TYPE=\"text\" NAME=\"eventweb\" SIZE=40 VALUE=\"$eventweb\"></TD></TR>\n";
/*    <TR><TD>";
if ($Cal_AllowHTML)
{
   echo "<TR><TD>";
                //HTMLbuttons_eventweb();
    echo"</TD></TR>";
}*/
    echo "<TR><TD>&nbsp;</TD></TR>";

        if ($ope != "savedel") {
         if($eventgo==1) {
                      echo "<TR><TD><B><font COLOR=\"$textcolor2\">" . translate("Event deactivate") . " ? : </FONT></B>
                      <SELECT NAME=\"eventgo\">
                      <OPTION VALUE=\"0\"> " . translate("Yes") . "
                      <OPTION VALUE=\"1\"> " . translate("No") . "
                      </SELECT></TD></TR>\n\n";
          } else {
                      echo "<TR><TD><B><font COLOR=\"$textcolor2\">" . translate("Event Activate") . " ? : </FONT></B>
                      <SELECT NAME=\"eventgo\">
                      <OPTION VALUE=\"1\"> " . translate("Yes") . "
                      <OPTION VALUE=\"0\"> " . translate("No") . "
                      </SELECT></TD></TR>\n\n";
          }
}

echo "</TABLE>\n";

            // Kiril: New Sanitizer


            if ($Cal_AllowHTML)
            {
                addFormatOption();

                echo "<input type='hidden' name='clickit_form' value='eventname1|eventdesc' />\n";

                echo "<input type='hidden' name='stripit_form' value='eventname|date_from|date_to|eventmail|eventweb' />\n";
            }
            else
            {
                echo "<input type='hidden' name='stripit_form' value='eventname1|eventdesc|eventname|date_from|date_to|eventmail|eventweb' />\n";
            }

            echo "<input type='hidden' name='mpn_sectype' value='2' />\n";

            // End Sanitizer

            echo "<input type=\"hidden\" name=\"uid\" value=\"\">
            <input type=\"hidden\" name=\"cal_op\" value=\"$ope\">
            <input type=\"hidden\" name=\"id\" value=\"$id\">
            <input type=\"hidden\" name=\"Cal_Month\" value=\"$Cal_Month\">
            <input type=\"hidden\" name=\"Cal_Year\" value=\"$Cal_Year\">

     <br> <input class=\"button\" type=\"submit\" value=\"$submit\" name=\"B1\"> <input class='button' type=\"reset\" value=\"".translate("Reset")."\" name=\"B2\">

   </form><br>

   <small>".translate("The Event cant be longer then")." $Cal_MaxEventDays ".translate("Days").".<br>";
    if ($Cal_AllowHTML) {
        echo "".translate("HTML Formatting is allowed")."<br>";

    } else {
        echo "".translate("No HTML Formatting is allowed")."";
    }
}

/*
#==================================================================
#   Function:   PrepString
#   Purpose:        Prepares string for saving to DB
#                   Adds slashes and removes HTML if not allowed
#   Arguments:  $target
#   Returns:        string
#==================================================================
function PrepString ($target)
{
    global $Cal_AllowHTML;

    $ret = addslashes($target);
    if (!$Cal_AllowHTML) $ret = strip_tags($ret);
    return $ret;
}
*/
#==================================================================
#   Function:   SaveNew
#   Purpose:        Saves new event record
#   Arguments:  $from, $to, $name, $desc, $eventgo, $eventmail, $eventweb, $eventname1, $uid
#   Returns:        nothing
#==================================================================
function SaveNew ($from, $to, $name, $desc, $eventgo, $eventmail, $eventweb, $eventname1, $uid)
{
    global $mpnTables, $phpEx;

    if (DatesOkay($from, $to) && FieldsOkay($name, $desc, $eventgo, $eventmail, $eventweb, $eventname1, $uid)) {
        $yr = GetDatePart($from, "Y");

        //$name = PrepString($name);
        //$desc = PrepString($desc);

        $q = "INSERT into ".$mpnTables['eventcal']." (date_from, date_to, eventname, eventdesc, eventgo, eventmail, eventweb, eventname1, uid)
                values ('$from','$to','$name','$desc','$eventgo', '$eventmail', '$eventweb', '$eventname1', '')";
        $rc = mysql_query($q) or die(mysql_error());
        if ($rc) {
            echo "<p align=\"center\"><b>".translate("NEW Event record successfully saved!")."</b></p>";
            echo "<CENTER>[ <a href=eventcaladmin.$phpEx?Cal_Year=$yr>".translate("Display")." $yr </a> ] ";
            echo "[ <a href=eventcaladmin.$phpEx?cal_op=new>".translate("Add new event")."</a> ]</CENTER><br>";
        }
    }
}

#==================================================================
#   Function:   SaveEdit
#   Purpose:        Saves Edited event record
#   Arguments:  $id, $from, $to, $name, $desc, $eventgo, $eventmail, $eventweb, $eventname1
#   Returns:        nothing
#==================================================================
function SaveEdit ($id, $from, $to, $name, $desc, $eventgo, $eventmail, $eventweb, $eventname1)
{
    global $mpnTables, $phpEx;

    if (DatesOkay($from, $to) && FieldsOkay($name, $desc)) {
        $yr = GetDatePart($from, "Y");
        //$name = PrepString($name);
       // $desc = PrepString($desc);

        $q = "UPDATE ".$mpnTables['eventcal']." SET date_from = '$from', date_to = '$to',
                eventname = '$name', eventdesc = '$desc',eventgo = '$eventgo',eventmail = '$eventmail',eventweb= '$eventweb',eventname1= '$eventname1' WHERE id = $id";

        $rc = mysql_query($q) or die(mysql_error());

        if ($rc) {
            echo "<p align=\"center\"><b>".translate("Event sucsesfully edited")."</b></p>";
            echo "<center>[ <a href=eventcaladmin.$phpEx?Cal_Year=$yr>".translate("Display")." $yr </a> ] ";
            echo "[ <a href=eventcaladmin.$phpEx?&new>".translate("Add new event")."</a> ]</center><br>";
        }
    }
}

#==================================================================
#   Function:   SaveDel
#   Purpose:        Actually Deletes event record
#   Arguments:  $id
#   Returns:        nothing
#==================================================================
function SaveDel ($id)
{
    global $Cal_Year, $mpnTables, $phpEx;

    $q = "DELETE FROM ".$mpnTables['eventcal']." WHERE id = $id";
    $rc = mysql_query($q) or die(mysql_error());
    if ($rc) {
        echo "<p align=\"center\"><b>".translate("Delete this event")." !</b></p>";
        echo "<P><CENTER>[ <a href=eventcaladmin.$phpEx?Cal_Year=$Cal_Year>".translate("Display")." $Cal_Year </a> ] ";
        echo "[ <a href=eventcaladmin.$phpEx?new>".translate("Add new event")."</a> ]</CENTER><br>";
    }
}

#==================================================================
#   Function:   EditEvent
#   Purpose:        Edit given event record id
#   Arguments:  $id
#   Returns:        nothing
#==================================================================
function EditEvent ($id)
{
    $rec = GetCalrec($id);
    echo "<p align=\"center\"><font size=5><b>".translate("Edit")."</b></font></p>";
      ShowEventForm ("saveedit", $id, $rec["date_from"], $rec["date_to"], $rec["eventname"], $rec["eventdesc"] ,$rec["eventgo"], $rec["eventmail"], $rec["eventweb"], $rec["eventname1"]);
}

#==================================================================
#   Function:   DupeEvent
#   Purpose:        Dupe given event record id to following year
#   Arguments:  $id
#   Returns:        nothing
#==================================================================
function DupeEvent ($id)
{
    echo "<p align=\"center\"><font size=3><b>".translate("Copy Event")."</b></font></p>";
    echo "".translate("A copy of this Event will be created")."<br>";
    $rec = GetCalrec($id);
    $yr = GetDatePart($rec["date_from"], "y");
    $yr ++;
    $rec["date_from"] = $yr.substr($rec["date_from"],4);
    $rec["date_to"] = $yr.substr($rec["date_to"],4);
    ShowEventForm ("savenew", 0, $rec["date_from"], $rec["date_to"], $rec["eventname"], $rec["eventdesc"] ,$rec["eventgo"], $rec["eventmail"], $rec["eventweb"], $rec["eventname1"]);
}

#==================================================================
#   Function:   NewEvent
#   Purpose:        Add a new event record
#   Arguments:  none
#   Returns:        nothing
#==================================================================
function NewEvent ()
 {
    global $serv_offset;

    $dt = date("Y-m-d",mktime(date("H")+$serv_offset));
    echo "<p align=\"center\"><font size=3><b>".translate("Add new event")."</b></font></p>";
    ShowEventForm ("savenew", 0, $dt, $dt, "", "", "", "", "", "");
}

#==================================================================
#   Function:   DelEvent
#   Purpose:        Delete an event record
#   Arguments:  $id
#   Returns:        nothing
#==================================================================
function DelEvent ($id)

{
    global $phpEx;
    $rec = GetCalrec($id);
    $yr = GetDatePart($rec["date_from"], "y");
    echo "<p align=\"center\"><font size=3><b>".translate("Delete this event?")."</b></font></p>";
    ShowEventForm ("savedel", $id, $rec["date_from"], $rec["date_to"], $rec["eventname"], $rec["eventdesc"], $rec['eventgo'], $rec['eventmail'], $rec['eventweb'], $rec['eventname1']);
    echo "<P><CENTER>[ <a href=eventcaladmin.$phpEx?Cal_Year=$yr>".translate("Display")." $yr</a>] ";
    echo "[<a href=eventcaladmin.$phpEx?new>".translate("Add new event")."</a> ]</CENTER><br>";
}


OpenTable();

#############################################################################
#   Page options dispatcher
#############################################################################

if (_MPN_ADMIN) {
    if (!isset($cal_op))
    {
        $cal_op = null;
    }

    switch ($cal_op) {
        case 'new':
            NewEvent();
            break;

        case 'savenew':
        SaveNew($date_from, $date_to, $eventname, $eventdesc, $eventgo, $eventmail, $eventweb, $eventname1, $uid);
        break;

        case 'edit':
        EditEvent($id);
        break;

        case 'saveedit':
        SaveEdit($id, $date_from, $date_to, $eventname, $eventdesc, $eventgo, $eventmail, $eventweb, $eventname1);
        break;

        case 'del':
        DelEvent($id);
        break;

        case 'savedel':
        SaveDel($id);
        break;

        case 'dupe':
        DupeEvent($id);
        break;

        default:
        DisplayCal($Cal_Year, $Cal_Month);
        break;
    }

} else {
$_SERVER['F_LIB'] -> redirectRequest( 'eventcal.php' );
}

CloseTable();
echo "<P>";
OpenTable();
echo "<DIV ALIGN=\"right\">$Cal_Ver</DIV>";
CloseTable();
include("footer.$phpEx");
?>
