<?php
/*
 ########################################################################
#                                                                        #
#           Version 4       /                        /   /               #
#          -----------__---/__---__------__----__---/---/-               #
#           | /| /  /___) /   ) (_ `   /   ) /___) /   /                 #
#          _|/_|/__(___ _(___/_(__)___/___/_(___ _/___/___               #
#                       Free Content / Management System                 #
#                                   /                                    #
#                                                                        #
#                                                                        #
#   Copyright 2005-2006 by webspell.org                                  #
#                                                                        #
#   visit webSPELL.org, webspell.info to get webSPELL for free           #
#   - Script runs under the GNU GENERAL PUBLIC LICENSE                   #
#   - It's NOT allowed to remove this copyright-tag                      #
#   -- http://www.fsf.org/licensing/licenses/gpl.html                    #
#                                                                        #
#   Code based on WebSPELL Clanpackage (Michael Gruber - webspell.at),   #
#   Far Development by Development Team - webspell.org                   #
#                                                                        #
#   visit webspell.org                                                   #
#                                                                        #
 ########################################################################
*/

if($_GET['action']=="savewar") {
	include("_mysql.php");
	include("_settings.php");
	include("_functions.php");
  if(!isclanwaradmin($userID)) die('No access!');

  $hour = $_POST['hour'];
  $minute = $_POST['minute'];
  $day = $_POST['day'];
  $month = $_POST['month'];
  $year = $_POST['year'];
  $squad = $_POST['squad'];
  $opponent = $_POST['opponent'];
  $opptag = $_POST['opptag'];
  $opphp = $_POST['opphp'];
  $oppcountry = $_POST['oppcountry'];
  $maps = $_POST['maps'];
  $server = $_POST['server'];
  $league = $_POST['league'];
  $leaguehp = $_POST['leaguehp'];
  $warinfo = $_POST['warinfo'];

  $date=mktime($hour,$minute,0,$month,$day,$year);
	safe_query("INSERT INTO ".PREFIX."upcoming ( date, type, squad, opponent, opptag, opphp, oppcountry, maps, server, league, leaguehp, warinfo )
                values( '$date', 'c', '$squad', '$opponent', '$opptag', '$opphp', '$oppcountry', '$maps', '$server', '$league', '$leaguehp', '$warinfo' ) ");
	    
	if($chID) safe_query("DELETE FROM ".PREFIX."challenge WHERE chID='$chID'");
	
	if($messages) {
		$title='new upcoming war in calendar!';
		$message='new upcoming clanwar on '.date("d.m.Y", $date).':[br][br]Opponent: [flag]'.$oppcountry.'[/flag] [url='.$opphp.']'.$opponent.'[/url] [br]League: [url='.$leaguehp.']'.$league.'[/url] [br]Info: '.$warinfo.'[br][br][url=index.php?site=calendar]link to calendar[/url]';
		
		$ergebnis=safe_query("SELECT userID FROM ".PREFIX."squads_members WHERE squadID='$squad'");
		while($ds=mysql_fetch_array($ergebnis)) {
			$id=$ds[userID];
			safe_query("INSERT INTO ".PREFIX."messenger (userID, date, fromuser, touser, title, message, viewed)
		                 values('$id', '$date', '$userID', '$id', '$title', '$message', '0')");  // eintrag für empfänger
						 
			safe_query("UPDATE ".PREFIX."user SET pmsent=pmsent+1 WHERE userID='$userID'");
			safe_query("UPDATE ".PREFIX."user SET pmgot=pmgot+1 WHERE userID='$id'");
		}
	}
	header("Location: index.php?site=calendar&tag=$day&month=$month&year=$year");
}
elseif($_GET['action']=="delete") {
	include("_mysql.php");
	include("_settings.php");
	include("_functions.php");
  if(!isclanwaradmin($userID)) die('No access!');
  $upID = $_GET['upID'];

	safe_query("DELETE FROM ".PREFIX."upcoming WHERE upID='$upID'");
	safe_query("DELETE FROM ".PREFIX."upcoming_announce WHERE upID='$upID'");
	header("Location: index.php?site=calendar");
}
elseif($_GET['action']=="saveannounce") {
	include("_mysql.php");
	include("_settings.php");
	include("_functions.php");
  if(!isclanmember($userID)) die('No access!');

  $tag = $_POST['tag'];
  $month = $_POST['month'];
  $year = $_POST['year'];

    $ergebnis=safe_query("SELECT * FROM ".PREFIX."upcoming_announce WHERE upID='".$_POST['upID']."' AND userID='$userID'");
	if(mysql_num_rows($ergebnis)) {
	    $ds=mysql_fetch_array($ergebnis);
		safe_query("UPDATE ".PREFIX."upcoming_announce SET status='".$_POST['status']."' WHERE annID='$ds[annID]' ");
	}
	else safe_query("INSERT INTO ".PREFIX."upcoming_announce ( upID, userID, status ) values( '".$_POST['upID']."', '$userID', '".$_POST['status']."' ) ");
	header("Location: index.php?site=calendar&tag=$tag&month=$month&year=$year");
}
elseif($_GET['action']=="saveeditdate") {
	include("_mysql.php");
	include("_settings.php");
	include("_functions.php");
  if(!isclanwaradmin($userID)) die('No access!');

  $hour = $_POST['hour'];
  $minute = $_POST['minute'];
  $day = $_POST['day'];
  $month = $_POST['month'];
  $year = $_POST['year'];

  $date=mktime($hour,$minute,0,$month,$day,$year);
	$enddate=mktime($_POST['endhour'],$_POST['endminute'],0,$_POST['endmonth'],$_POST['endday'],$_POST['endyear']);
	safe_query("UPDATE ".PREFIX."upcoming SET date='$date', 
                                 enddate='$enddate', 
								 short='".$_POST['short']."',
								 title='".$_POST['title']."',
								 country='".$_POST['country']."',
								 location='".$_POST['location']."',
								 locationhp='".$_POST['locationhp']."',
								 dateinfo='".$_POST['dateinfo']."' WHERE upID='".$_POST['upID']."' ");
	    
	header("Location: index.php?site=calendar&tag=$day&month=$month&year=$year");
}
elseif($_GET['action']=="savedate") {
	include("_mysql.php");
	include("_settings.php");
	include("_functions.php");
  if(!isclanwaradmin($userID)) die('No access!');

  $date=mktime($_POST['hour'],$_POST['minute'],0,$_POST['month'],$_POST['day'],$_POST['year']);
	$enddate=mktime($_POST['endhour'],$_POST['endminute'],0,$_POST['endmonth'],$_POST['endday'],$_POST['endyear']);
	if($date>$enddate) {
		$temp=$date;
		$date=$enddate;
		$enddate=$temp;
		unset($temp);
	}

	safe_query("INSERT INTO ".PREFIX."upcoming ( date, type, enddate, short, title, country, location, locationhp, dateinfo  )
                values( '$date', 'd', '".$enddate."', '".$_POST['short']."', '".$_POST['title']."', '".$_POST['country']."', '".$_POST['location']."', '".$_POST['locationhp']."', '".$_POST['dateinfo']."' ) ");
  redirect("index.php?site=calendar&tag=".$_POST['day']."&month=".$_POST['month']."&year=".$_POST['year'],"",0);
}
elseif($_GET['action']=="saveeditwar") {
	include("_mysql.php");
	include("_settings.php");
	include("_functions.php");
  if(!isclanwaradmin($userID)) die('No access!');

  $upID = $_POST['upID'];
  $hour = $_POST['hour'];
  $minute = $_POST['minute'];
  $day = $_POST['day'];
  $month = $_POST['month'];
  $year = $_POST['year'];
  $squad = $_POST['squad'];
  $opponent = $_POST['opponent'];
  $opptag = $_POST['opptag'];
  $opphp = $_POST['opphp'];
  $oppcountry = $_POST['oppcountry'];
  $maps = $_POST['maps'];
  $server = $_POST['server'];
  $league = $_POST['league'];
  $leaguehp = $_POST['laeguehp'];
  $warinfo = $_POST['warinfo'];

    $date=mktime($hour,$minute,0,$month,$day,$year);
	safe_query("UPDATE ".PREFIX."upcoming SET date='$date',
                                 type='c', 
								 squad='$squad', 
								 opponent='$opponent', 
								 opptag='$opptag', 
								 opphp='$opphp', 
								 oppcountry='$oppcountry', 
								 maps='$maps', 
								 server='$server', 
								 league='$league', 
								 leaguehp='$leaguehp', 
								 warinfo='$warinfo' WHERE upID='$upID' ");
	    
	header("Location: index.php?site=calendar&tag=$day&month=$month&year=$year");
}

$month = $_GET['month'];
$year = $_GET['year'];
$tag = $_GET['tag'];

eval ("\$title_calendar = \"".gettemplate("title_calendar")."\";");
echo $title_calendar;

if(isclanwaradmin($userID)) echo'<input type="button" class="button" onClick="MM_goToURL(\'parent\',\'index.php?site=calendar&action=addwar\');return document.MM_returnValue" value="Add Clanwar">
                                                             <input type="button" class="button" onClick="MM_goToURL(\'parent\',\'index.php?site=calendar&action=adddate\');return document.MM_returnValue" value="Add Event"><br><br>';


function print_calendar($mon,$year) {
    global $dates, $first_day, $start_day;
	
	$pagebg=PAGEBG;
	$border=BORDER;
	$bghead=BGHEAD;
	$bgcat=BGCAT;

	$first_day = mktime(0,0,0,$mon,1,$year);
	$start_day = date("w",$first_day);
	$res = getdate($first_day);
	$month_name = $res["month"];
	$no_days_in_month = date("t",$first_day);

	//If month's first day does not start with first Sunday, fill table cell with a space
	for ($i = 1; $i <= $start_day;$i++) $dates[1][$i] = " ";

	$row = 1;
	$col = $start_day+1;
	$num = 1;
	while($num<=31)	{
	    if ($num > $no_days_in_month) break;
		else {
		    $dates[$row][$col] = $num;
			if (($col + 1) > 7)	{
			    $row++;
				$col = 1;
			}
			else  $col++;
			
		    $num++;
		}
	}
	
	$mon_num = date("n",$first_day);
	$temp_yr = $next_yr = $prev_yr = $year;

	$prev = $mon_num - 1;
	if ($prev<10) $prev="0".$prev;
	$next = $mon_num + 1;
	if ($next<10) $next="0".$next;		

	//If January is currently displayed, month previous is December of previous year
	if ($mon_num == 1){
	    $prev_yr = $year - 1;
		$prev = 12;
	}

	//If December is currently displayed, month next is January of next year
	if ($mon_num == 12)	{
	    $next_yr = $year + 1;
		$next = 1;
	}

	echo'<table width="100%" border="00" cellspacing="1" cellpadding="2" bgcolor="'.BORDER.'">
	       <tr>
		     <td bgcolor="'.BGCAT.'" align="center"><a class="category" href="index.php?site=calendar&month=01">jan</a></td>
			 <td bgcolor="'.BGCAT.'" align="center"><a class="category" href="index.php?site=calendar&month=02">feb</a></td>
			 <td bgcolor="'.BGCAT.'" align="center"><a class="category" href="index.php?site=calendar&month=03">mar</a></td>
			 <td bgcolor="'.BGCAT.'" align="center"><a class="category" href="index.php?site=calendar&month=04">apr</a></td>
			 <td bgcolor="'.BGCAT.'" align="center"><a class="category" href="index.php?site=calendar&month=05">may</a></td>
			 <td bgcolor="'.BGCAT.'" align="center"><a class="category" href="index.php?site=calendar&month=06">jun</a></td>
		   </tr>
		   <tr>
		     <td bgcolor="'.BGCAT.'" align="center"><a class="category" href="index.php?site=calendar&month=07">jul</a></td>
			 <td bgcolor="'.BGCAT.'" align="center"><a class="category" href="index.php?site=calendar&month=08">aug</a></td>
			 <td bgcolor="'.BGCAT.'" align="center"><a class="category" href="index.php?site=calendar&month=09">sep</a></td>
			 <td bgcolor="'.BGCAT.'" align="center"><a class="category" href="index.php?site=calendar&month=10">oct</a></td>
			 <td bgcolor="'.BGCAT.'" align="center"><a class="category" href="index.php?site=calendar&month=11">nov</a></td>
			 <td bgcolor="'.BGCAT.'" align="center"><a class="category" href="index.php?site=calendar&month=12">dec</a></td>
		   </tr>
		 </table><br>';	 
		
	echo'
			<table width="100%" cellspacing="1" cellpadding="2" bgcolor="'.BORDER.'">
				<tr>
					<td class="title" align="center">&laquo; <a class="titlelink" href="index.php?site=calendar&month='.$prev.'&year='.$prev_yr.'">prev</a></td>
					<td class="title" align="center" colspan="5">'.date("F",$first_day).' '.$temp_yr.'</td>
					<td class="title" align="center"><a class="titlelink" href="index.php?site=calendar&month='.$next.'&year='.$next_yr.'">next</a> &raquo;</td>
				</tr>
				<tr>
					<td colspan="7" bgcolor="'.PAGEBG.'"></td>
				</tr>
				<tr>
					<td bgcolor="'.BGCAT.'" width="16%" align="center">Sun</td>
					<td bgcolor="'.BGCAT.'" width="14%" align="center">Mon</td>
					<td bgcolor="'.BGCAT.'" width="14%" align="center">Tue</td>
					<td bgcolor="'.BGCAT.'" width="14%" align="center">Wed</td>
					<td bgcolor="'.BGCAT.'" width="14%" align="center">Thu</td>
					<td bgcolor="'.BGCAT.'" width="14%" align="center">Fri</td>
					<td bgcolor="'.BGCAT.'" width="14%" align="center">Sat</td>
				</tr>
				<tr>
					<td colspan="7" bgcolor="'.BG_1.'"></td>
				</tr>';

	$end = ($start_day > 4)? 6:5;
	for ($row=1;$row<=$end;$row++) {
	    for ($col=1;$col<=7;$col++) {
		    if ($dates[$row][$col] == "") $dates[$row][$col] = " ";
			if (!strcmp($dates[$row][$col]," ")) $count++;
			
			$t = $dates[$row][$col];
			if($t < 10) $tag = "0$t";
			else $tag = $t;

			// DATENBANK ABRUF
			$start_date = mktime(0, 0, 0, $mon, $t, $year);
			$end_date = mktime(23, 59, 59, $mon, $t, $year);
			unset($termin);
			
			$ergebnis = safe_query("SELECT * FROM ".PREFIX."upcoming");
			$anz = mysql_num_rows($ergebnis);
			if($anz) {
			    while ($ds = mysql_fetch_array($ergebnis)) {
				    if($ds[type]=="d") {
					    if(($start_date<=$ds[date] && $end_date>=$ds[date]) || ($start_date>=$ds[date] && $end_date<=$ds[enddate]) || ($start_date<=$ds[enddate] && $end_date>=$ds[enddate])) 
						    $termin.='<a href="index.php?site=calendar&tag='.$t.'&month='.$mon.'&year='.$year.'">'.$ds[short].'</a><br>';
					}	
					else {
					    if($ds[date]>=$start_date && $ds[date]<=$end_date) {
						    $begin = date("H:i", $ds[date]);
							$termin.='<a href="index.php?site=calendar&tag='.$t.'&month='.$mon.'&year='.$year.'">'.$begin.' '.$ds[opptag].'</a><br>';
						}
					}	
				}
			}
			else $termin="<br><br>";
		    // DB ABRUF ENDE

			//If date is today, highlight it
			if (($t == date("j")) && ($mon == date("n")) && ($year == date("Y"))) echo'<td height="40" valign="top" bgcolor="'.BG_4.'"><b>'.$t.'</b><br>'.$termin.'</td>';
            //  If the date is absent ie after 31, print space
			else {
				if($t==' ') echo'<td height="40" valign="top" bgcolor="'.BG_1.'">&nbsp;</td>';
				else echo'<td height="40" valign="top" bgcolor="'.BG_2.'">'.$t.'<br>'.$termin.'</td>';
			}
						
		}
		if (($row + 1) != ($end+1)) echo'</tr><tr>';
		else echo'</tr>';
	}
	echo'<tr><td colspan="7" bgcolor="'.PAGEBG.'"></td></tr>
	     <tr>
	       <td bgcolor="'.BGCAT.'" colspan="7" align="center"><a class="category" href="index.php?site=calendar"><b>show today\'s events</b></a></td>
		 </tr>
		 </table><br><br>';
}

function print_termine($tag,$month,$year) {
	global $wincolor;
	global $loosecolor;
	global $drawcolor;
	global $userID;
	
	$pagebg=PAGEBG;
	$border=BORDER;
	$bghead=BGHEAD;
	$bgcat=BGCAT;
		
	$start_date = mktime(0, 0, 0, $month, $tag, $year);
	$end_date = mktime(23, 59, 59, $month, $tag, $year);
	unset($termin);
			
	$ergebnis = safe_query("SELECT * FROM ".PREFIX."upcoming");
	$anz = mysql_num_rows($ergebnis);
	if($anz) {
		while ($ds=mysql_fetch_array($ergebnis)) {
			if($ds[type]=="c") {
			    if($ds[date]>=$start_date && $ds[date]<=$end_date) {
			        $date = date("d.m.Y", $ds[date]);
			    	$time = date("H:i", $ds[date]);
					$squad=getsquadname($ds[squad]);
					$oppcountry="[flag]".$ds[oppcountry]."[/flag]";
					$oppcountry=flags($oppcountry);
					$opponent=$oppcountry.' <a href="'.$ds[opphp].'" target="_blank">'.$ds[opptag].' / '.$ds[opponent].'</a>';
					$maps=$ds[maps];
					$server=$ds[server];
					$league='<a href="'.$ds[leaguehp].'" target="_blank">'.$ds[league].'</a>';
					if(isclanmember($userID)) $warinfo=cleartext($ds[warinfo]);
					else $warinfo='You have to be a clanmmeber to see this.';

          unset($players);
          if(isclanmember($userID) or isanyadmin($userID)) {
 					 $anmeldung=safe_query("SELECT * FROM ".PREFIX."upcoming_announce WHERE upID='".$ds[upID]."'");
			        if(mysql_num_rows($anmeldung)) {
			            $i=1;
						while ($da = mysql_fetch_array($anmeldung)) {
				        	if ($da[status] == "y") $fontcolor = $wincolor;
					    	elseif ($da[status] == "n") $fontcolor = $loosecolor;
					    	else $fontcolor = $drawcolor;
										
				        	if($i>1) $players.=', <a href="index.php?site=profile&id='.$da[userID].'"><font color="'.$fontcolor.'">'.getnickname($da[userID]).'</font></a>';
							else $players.='<a href="index.php?site=profile&id='.$da[userID].'"><font color="'.$fontcolor.'">'.getnickname($da[userID]).'</font></a>';
							$i++;
				    	}
			    	}
			    	else $players='no players announced';
					
					 if(issquadmember($userID, $ds[squad])) $announce='&#8226; <a href="index.php?site=calendar&action=announce&upID='.$ds[upID].'&tag='.$tag.'&month='.$month.'&year='.$year.'">announce here</a>';
					
					 if(isclanwaradmin($userID))
	                  $adminaction='<div align="right">
					  				<input type="button" class="button" onClick="MM_openBrWindow(\'clanwars.php?action=new&upID='.$ds[upID].'\',\'Clanwars\',\'toolbar=no,status=no,scrollbars=yes,width=800,height=490\')" value="add to Clanwars">  				
									<input type="button" class="button" onClick="MM_goToURL(\'parent\',\'index.php?site=calendar&action=editwar&upID='.$ds[upID].'\');return document.MM_returnValue" value="edit">
									<input type="button" class="button" onClick="MM_confirm(\'really delete this upcoming action?\', \'calendar.php?action=delete&upID='.$ds[upID].'\')" value="delete"></div>';
          } else $players = 'You have to be a clanmember to see this.';
          
					$bg1=BG_1;
					$bg2=BG_2;
					$bg3=BG_3;
					$bg4=BG_4;
					
					eval ("\$upcoming_war_details = \"".gettemplate("upcoming_war_details")."\";");
                	echo $upcoming_war_details;
				}
			}
			else {
			    if(($start_date<=$ds[date] && $end_date>=$ds[date]) || ($start_date>=$ds[date] && $end_date<=$ds[enddate]) || ($start_date<=$ds[enddate] && $end_date>=$ds[enddate])) {
				    $date = date("d.m.Y", $ds[date]);
			    	$time = date("H:i", $ds[date]);
					$enddate = date("d.m.Y", $ds[enddate]);
			    	$endtime = date("H:i", $ds[enddate]);
					$title=$ds[title];
					$location='<a href="'.$ds[locationhp].'" target="_blank">'.$ds[location].'</a>';
					$dateinfo=cleartext($ds[dateinfo]);
					$dateinfo = toggle($dateinfo, $ds[upID]);
					$country="[flag]".$ds[country]."[/flag]";
					$country=flags($country);

				  unset($players);
          if(isclanmember($userID)) {
					 $anmeldung=safe_query("SELECT * FROM ".PREFIX."upcoming_announce WHERE upID='$ds[upID]' ");
			        if(mysql_num_rows($anmeldung)) {
			            $i=1;
						while ($da = mysql_fetch_array($anmeldung)) {
				        	if ($da[status] == "y") $fontcolor = $wincolor;
					    	elseif ($da[status] == "n") $fontcolor = $loosecolor;
					    	else $fontcolor = $drawcolor;
										
				        	if($i>1) $players.=', <a href="index.php?site=profile&id='.$da[userID].'"><font color="'.$fontcolor.'">'.getnickname($da[userID]).'</font></a>';
							else $players.='<a href="index.php?site=profile&id='.$da[userID].'"><font color="'.$fontcolor.'">'.getnickname($da[userID]).'</font></a>';
							$i++;
				    	}
			    	}
			    	else $players='no players announced.';
					
					 $announce='&#8226; <a href="index.php?site=calendar&action=announce&upID='.$ds[upID].'&tag='.$tag.'&month='.$month.'&year='.$year.'">announce here</a>';
					
					 if(isclanwaradmin($userID))
	                  $adminaction='<div align="right"><input type="button" class="button" onClick="MM_goToURL(\'parent\',\'index.php?site=calendar&action=editdate&upID='.$ds[upID].'\');return document.MM_returnValue" value="edit">
									<input type="button" class="button" onClick="MM_confirm(\'really delete this upcoming action?\', \'calendar.php?action=delete&upID='.$ds[upID].'\')" value="delete"></div>';
          } else $players = 'You have to be a clanmember to see this.';
				
			    	$bg1=BG_1;
					$bg2=BG_2;
					$bg3=BG_3;
					$bg4=BG_4;
						
					eval ("\$upcoming_date_details = \"".gettemplate("upcoming_date_details")."\";");
                	echo $upcoming_date_details;
				}
			}
	    }
	}
	else echo 'no entries for today';

}

if($_GET['action']=="addwar") {
   if(isclanwaradmin($userID)) {
   	
   	$chID=$_GET['chID'];
		for($i=1; $i<32; $i++) {
		    if($i==date("d", time())) $day.='<option selected>'.$i.'</option>';
			else $day.='<option>'.$i.'</option>';
		}
		for($i=1; $i<13; $i++) {
		    if($i==date("n", time())) $month.='<option value="'.$i.'" selected>'.date("M", time()).'</option>';
			else $month.='<option value="'.$i.'">'.date("M", mktime(0,0,0,$i,1,2000)).'</option>';
		}
		for($i=2000; $i<2010; $i++) {
		    if($i==date("Y", time())) $year.='<option value="'.$i.'" selected>'.date("Y", time()).'</option>';
			else $year.='<option value="'.$i.'">'.$i.'</option>';
		}
		$squads=getgamesquads();
		$hours="20";
		$minutes="00";
		
		$opphp="http://";
		
		if($chID) {
			$ergebnis=safe_query("SELECT * FROM ".PREFIX."challenge WHERE chID='$chID'");
			$ds=mysql_fetch_array($ergebnis);
			$day=str_replace("<option selected>", "<option>", $day);
			$day=str_replace("<option>".date("d", $ds[cwdate])."</option>", "<option selected>".date("d", $ds[cwdate])."</option>", $day);
			
			$month=str_replace(" selected", "", $month);
			$month=str_replace(' value="'.date("n", $ds[cwdate]).'"', ' value="'.date("n", $ds[cwdate]).'" selected', $month);
			
			$year=str_replace(" selected", "", $year);
			$year=str_replace(' value="'.date("Y", $ds[cwdate]).'"', ' value="'.date("Y", $ds[cwdate]).'" selected', $year);
			
			$hours=date("H", $ds[cwdate]);
			$minutes=date("i", $ds[cwdate]);
			
			$squads=str_replace(" selected", "", $squads);
			$squads=str_replace('<option value="'.$ds[squadID].'">', '<option value="'.$ds[squadID].'" selected>', $squads);

			$countries=str_replace(" selected", "", $countries);
			$countries=str_replace('<option value="'.$ds[oppcountry].'">', '<option value="'.$ds[oppcountry].'" selected>', $countries);			
			
			$opphp=$ds[opphp];
		}
		
		$bg1=BG_1;
		eval ("\$upcoming_war_new = \"".gettemplate("upcoming_war_new")."\";");
	    echo $upcoming_war_new;  
	}
	else redirect('index.php?site=calendar', 'no access!');
}
elseif($_GET['action']=="editwar") {
   if(isclanwaradmin($userID)) {
    $upID = $_GET['upID'];
		$ds=mysql_fetch_array(safe_query("SELECT * FROM ".PREFIX."upcoming WHERE upID='$upID'"));
	    for($i=1; $i<32; $i++) {
		    if($i==date("d", $ds[date])) $day.='<option selected>'.$i.'</option>';
			else $day.='<option>'.$i.'</option>';
		}
		for($i=1; $i<13; $i++) {
		    if($i==date("n", $ds[date])) $month.='<option value="'.$i.'" selected>'.date("M", $ds[date]).'</option>';
			else $month.='<option value="'.$i.'">'.date("M", mktime(0,0,0,$i,1,2000)).'</option>';
		}
		for($i=2000; $i<2010; $i++) {
		    if($i==date("Y", $ds[date])) $year.='<option selected>'.$i.'</option>';
			else $year.='<option>'.$i.'</option>';
		}
		$squads=getgamesquads();
		$squads=str_replace('value="'.$ds[squad].'"', 'value="'.$ds[squad].'" selected', $squads);
		$league=$ds[league];
		$leaguehp=$ds[leaguehp];
		$opponent=$ds[opponent];
		$opptag=$ds[opptag];
		$opphp=$ds[opphp];
		$maps=$ds[maps];
		$server=$ds[server];
		$warinfo=$ds[warinfo];		
		$countries=str_replace('value="at" selected', 'value="at"', $countries);
		$countries=str_replace('value="'.$ds[oppcountry].'"', 'value="'.$ds[oppcountry].'" selected', $countries);
		$hour=date("H", $ds[date]);
		$minutes=date("i", $ds[date]);		
		
		$bg1=BG_1;
		eval ("\$upcoming_war_edit = \"".gettemplate("upcoming_war_edit")."\";");
	    echo $upcoming_war_edit;  
	}
	else redirect('index.php?site=calendar', 'no access!');
}
elseif($_GET['action']=="adddate") {
   if(isclanwaradmin($userID)) {
		for($i=1; $i<32; $i++) {
		    if($i==date("d", time())) $day.='<option selected>'.$i.'</option>';
			else $day.='<option>'.$i.'</option>';
		}
		for($i=1; $i<13; $i++) {
		    if($i==date("n", time())) $month.='<option value="'.$i.'" selected>'.date("M", time()).'</option>';
			else $month.='<option value="'.$i.'">'.date("M", mktime(0,0,0,$i,1,2000)).'</option>';
		}
		for($i=2000; $i<2010; $i++) {
		    if($i==date("Y", time())) $year.='<option value="'.$i.'" selected>'.date("Y", time()).'</option>';
			else $year.='<option value="'.$i.'">'.$i.'</option>';
		}
		$squads=getgamesquads();
		
		$bg1=BG_1;
		eval ("\$upcoming_date_new = \"".gettemplate("upcoming_date_new")."\";");
	    echo $upcoming_date_new;  
	}
	else redirect('index.php?site=calendar', 'no access!');
}
elseif($_GET['action']=="editdate") {
   if(isclanwaradmin($userID)) {
   $upID = $_GET['upID'];
		$ds=mysql_fetch_array(safe_query("SELECT * FROM ".PREFIX."upcoming WHERE upID='$upID'"));
	    for($i=1; $i<32; $i++) {
		    if($i==date("d", $ds[date])) $day.='<option selected>'.$i.'</option>';
			else $day.='<option>'.$i.'</option>';
		}
		for($i=1; $i<13; $i++) {
		    if($i==date("n", $ds[date])) $month.='<option value="'.$i.'" selected>'.date("M", $ds[date]).'</option>';
			else $month.='<option value="'.$i.'">'.date("M", mktime(0,0,0,$i,1,2000)).'</option>';
		}
		for($i=2000; $i<2010; $i++) {
		    if($i==date("Y", $ds[date])) $year.='<option selected>'.$i.'</option>';
			else $year.='<option>'.$i.'</option>';
		}
	    for($i=1; $i<32; $i++) {
		    if($i==date("d", $ds[enddate])) $endday.='<option selected>'.$i.'</option>';
			else $endday.='<option>'.$i.'</option>';
		}
		for($i=1; $i<13; $i++) {
		    if($i==date("n", $ds[enddate])) $endmonth.='<option value="'.$i.'" selected>'.date("M", $ds[enddate]).'</option>';
			else $endmonth.='<option value="'.$i.'">'.date("M", mktime(0,0,0,$i,1,2000)).'</option>';
		}
		for($i=2000; $i<2010; $i++) {
		    if($i==date("Y", $ds[enddate])) $endyear.='<option selected>'.$i.'</option>';
			else $endyear.='<option>'.$i.'</option>';
		}
		$countries=str_replace(' selected', '', $countries);
		$countries=str_replace('value="'.$ds[country].'"', 'value="'.$ds[country].'" selected', $countries);
		
		$hour=date("H", $ds[date]);
		$endhour=date("H", $ds[enddate]);
		$minute=date("i", $ds[date]);
		$endminute=date("i", $ds[enddate]);
		
		$short=$ds[short];
		$title=$ds[title];
		$location=$ds[location];
		$locationhp=$ds[locationhp];
		$dateinfo=cleartext($ds[dateinfo]);
		$dateinfo = toggle($dateinfo, $upID);
		
		$bg1=BG_1;
		eval ("\$upcoming_date_edit = \"".gettemplate("upcoming_date_edit")."\";");
	    echo $upcoming_date_edit;  
	}
	else redirect('index.php?site=calendar', 'no access!');
}
elseif($_GET['action']=="announce") {
    echo'<form method="post" action="calendar.php?action=saveannounce">
	     Announce to this Clanwar/Date: <select name="status">
		                                  <option value="y" selected>Yes</option>
		                                  <option value="n">No</option>
									      <option value="p">Perhaps</option>
										</select>
		<input type="hidden" name="upID" value="'.$_GET['upID'].'">
		<input type="hidden" name="tag" value="'.$_GET['tag'].'">
		<input type="hidden" name="month" value="'.$_GET['month'].'">
		<input type="hidden" name="userID" value="'.$_GET['userID'].'">
		<input type="hidden" name="year" value="'.$_GET['year'].'"><input type="submit" name="submit" value="save announcement"></form>';
}
else {
    // If $month is not present, set it to current month.
  $month = $_GET['month'];
	$month = (empty($month)) ? date("m") : $month;
	// If $year is not present, set it to current year.
  $year = $_GET['year'];
	$year = (empty($year)) ? date("Y") : $year;
	// If $year is not present, set it to current year.
  $tag = $_GET['tag'];
	$tag = (empty($tag)) ? date("d") : $tag;
	print_calendar($month,$year);
	print_termine($tag, $month, $year);
}
	
?>


