<?php

/********************************************************************************
	- MemHT Portal -
	
	Copyright (C) 2007-2008 by Miltenovik Manojlo
	http://www.memht.com
	
	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, or
	(at your opinion) any later version.
	
	This program 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 this program; if not, see <http://www.gnu.org/licenses/> (GPLv2)
	or write to the Free Software Foundation, Inc., 51 Franklin Street,
	Fifth Floor, Boston, MA02110-1301, USA.
		
********************************************************************************/

if (!defined("_LOAD_PAGE_")) {
	die("<table style='padding: 2px; border: 1px solid #999; background-color: #EEE; font-family: Verdana; font-size: 10px;' align='center'><tr><td><b>Error:</b> This file cannot be opened directly!</td></tr></table>");
}

if (isSet($_GET['op'])) { $op = inCode($_GET['op']); } else { $op = ""; }
if (isSet($_GET['id'])) { $id = intval($_GET['id']); } else { $id = 0; }
if (isSet($_GET['ok'])) { $ok = inCode($_GET['ok']); } else { $ok = false; }
if (isSet($_POST['mostra'])) { $mostra = inCode($_POST['mostra']); } else { $mostra = 0; }
if (isSet($_GET['dir'])) { $dir = inCode($_GET['dir']); } else { $dir = ""; }
if (isSet($_POST['titolo'])) { $titolo = inCode($_POST['titolo']); } else { $titolo = ""; }
if (isSet($_POST['titolo_on'])) { $titolo_on = inCode($_POST['titolo_on']); } else { $titolo_on = ""; }
if (isSet($_POST['tipo'])) { $tipo = inCode($_POST['tipo']); } else { $tipo = ""; }
if (isSet($_POST['zona'])) { $zona = inCode($_POST['zona']); } else { $zona = ""; }
if (isSet($_POST['changePosition'])) { $changePosition = inCode($_POST['changePosition']); } else { $changePosition = ""; }
if (isSet($_POST['nomefile'])) { $nomefile = inCode($_POST['nomefile']); } else { $nomefile = ""; }
if (isSet($_POST['contenuto'])) { $contenuto = mysql_real_escape_string($_POST['contenuto']); } else { $contenuto = ""; }
if (isSet($_POST['flag'])) { $flag = intval($_POST['flag']); } else { $flag = 0; }
if (isSet($_POST['cflag'])) { $cflag = intval($_POST['cflag']); } else { $cflag = 0; }
if (isSet($_POST['rank'])) { $rank = inCode($_POST['rank']); } else { $rank = ""; }
if (isSet($_POST['enabled'])) { $enabled = intval($_POST['enabled']); } else { $enabled = 0; }
if (isSet($_POST['posizione'])) { $posizione = inCode($_POST['posizione']); } else { $posizione = 0; }

function blocksList() {
	global $dblink;

	$n = 0;
	echo "<table width='100%' align='center' cellspacing='1' cellpadding='0' class='std_nicetable'>";
	echo "<thead>\n";
	echo "<tr><td>"._BLOCKTITLE_."</td><td>"._FILE_."</td><td width='1%'>&nbsp;</td></tr>\n";
	echo "</thead>\n";
	echo "<tbody>\n";
	$result = $dblink->get_list("SELECT * FROM memht_blocks ORDER BY titolo");
	foreach ($result as $row_block) {
		$id = intval($row_block['id']);
		$titolo = outCode($row_block['titolo']);
		$titolo_on = intval($row_block['titolo_on']);
		$nomefile = outCode($row_block['nomefile']);
		$flag = intval($row_block['flag']);
		$rank = intval($row_block['rank']);
		$enabled = intval($row_block['enabled']);
		
		$rankimg = $info1 = $info2 = "";
		switch ($rank) {
			case 0: $rankimg = "guest"; $ranktitle = _LEVEL_.": "._GUEST_; break;
			case 1: $rankimg = "user"; $ranktitle = _LEVEL_.": "._USER_; break;
			case 2: $rankimg = "poweruser"; $ranktitle = _LEVEL_.": "._POWERUSER_; break;
			case 3: $rankimg = "admin"; $ranktitle = _LEVEL_.": "._ADMIN_; break;
			case 4: $rankimg = "superadmin"; $ranktitle = _LEVEL_.": "._SUPERADMIN_; break;
		}
		$info1 .= ($titolo_on=="0") ? " <img src='images/title_off.gif' alt='"._BLOCKTITLE_."' title='"._BLOCKTITLE_." "._OFF_."'>" : " <img src='images/title.gif' title='"._BLOCKTITLE_." "._ON2_."'>" ;
		$info2 .= ($enabled=="0") ? " <img src='images/off.gif' alt='"._OFF_."' title='"._OFF_."'>" : " <img src='images/on.gif' alt='"._ON2_."' title='"._ON2_."'>" ;
		$info2 .= ($flag==0) ? " - <img src='images/bigstar_off.gif' alt='"._STANDARD_."' title='"._STANDARD_."'>" : " - <img src='images/bigstar.gif' alt='"._HIGHLIGHTED_.": $flag' title='"._HIGHLIGHTED_.": $flag'>" ;
		
		if ($titolo=="") { $titolo = $nomefile; }
		
		$class = (($n++%2)!=0) ? "hlight" : "clean" ;
		
		echo "<tr><td class='$class'><a href='admin.php?page=blocks&op=showBlock&id=$id' title='$titolo'><b>$titolo</b></a></td><td width='30%' class='$class'>$nomefile</td><td width='5%' class='$class' nowrap><a href='admin.php?page=blocks&op=editBlock&id=$id' title='"._MODIFY_."'><img src='images/edit.gif' alt='Edit' border='0'></a> <a href='admin.php?page=blocks&op=deleteBlock&id=$id' title='"._DELETE_."'><img src='images/delete.gif' alt='Delete' border='0'></a> - <img src='images/rank_$rankimg.gif' title='$ranktitle' border='0'> - <a href='admin.php?page=blocks&op=switchTitle&id=$id' title='"._BLOCKTITLE_." "._ON_OFF_."'><img src='images/on_off.gif' alt='On off' border='0'></a> $info1 - <a href='admin.php?page=blocks&op=switch&id=$id' title='"._ON_OFF_."'><img src='images/on_off.gif' border='0'></a> $info2</td></tr>\n";
	}
	echo "</tbody>\n";
	echo "</table>";
	echo "<br>";	
	//changePosition blocchi
	echo "<table width='100%' align='center' cellspacing='0' cellpadding='0'><tr valign='top'><td width='33%'>";
		echo "<table width='100%' align='center' cellspacing='1' cellpadding='0' class='std_nicetable'>";
		echo "<thead>\n";
			echo "<tr><td colspan='3'>"._BLOCKPOSINNAV_."</td></tr>";
		echo "</thead>\n";
		echo "<tbody>\n";
			$n = 0;
			$result = $dblink->get_list("SELECT * FROM memht_blocks WHERE zona=0 ORDER BY posizione");
			foreach ($result as $row_block2) {
				$id2 = intval($row_block2['id']);
				$titolo2 = outCode($row_block2['titolo']);
				$nomefile2 = outCode($row_block2['nomefile']);
				$posizione2 = intval($row_block2['posizione']);
				
				if ($titolo2=="") { $titolo2 = $nomefile2; }
				$class = (($n++%2)!=0) ? "hlight" : "clean" ;
				
				echo "<tr><td width='5%' class='$class'>$posizione2</td><td class='$class'><b>$titolo2</b></td><td width='1%' class='$class' nowrap><a href='admin.php?page=blocks&op=changePosition&id=$id2&dir=up' title='"._UP_."'><img src='images/up.gif' border='0'></a> <a href='admin.php?page=blocks&op=changePosition&id=$id2&dir=down' title='"._DOWN_."'><img src='images/down.gif' alt='Down' border='0'></a></div></td></tr>\n";
			}
		echo "</tbody>\n";
		echo "</table>";
	echo "</td><td width='2%'>&nbsp;</td><td width='32%'>\n";
		echo "<table width='100%' align='center' cellspacing='1' cellpadding='0' class='std_nicetable'>";
		echo "<thead>\n";
			echo "<tr><td colspan='3'>"._BLOCKPOSINMAIN_."</td></tr>";
		echo "</thead>\n";
		echo "<tbody>\n";
			$n = 0;
			$result = $dblink->get_list("SELECT * FROM memht_blocks WHERE zona=2 ORDER BY posizione");
			foreach ($result as $row_block3) {
				$id3 = intval($row_block3['id']);
				$titolo3 = outCode($row_block3['titolo']);
				$nomefile3 = outCode($row_block3['nomefile']);
				$posizione3 = intval($row_block3['posizione']);
				
				if ($titolo3=="") { $titolo3 = $nomefile3; }
				$class = (($n++%2)!=0) ? "hlight" : "clean" ;
				
				echo "<tr><td width='5%' class='$class'>$posizione3</td><td class='$class'><b>$titolo3</b></td><td width='1%' class='$class' nowrap><a href='admin.php?page=blocks&op=changePosition&id=$id3&dir=up' title='"._UP_."'><img src='images/up.gif' border='0'></a> <a href='admin.php?page=blocks&op=changePosition&id=$id3&dir=down' title='"._DOWN_."'><img src='images/down.gif' alt='Down' border='0'></a></td></tr>\n";
			}
		echo "</tbody>\n";
		echo "</table>";
	echo "</td><td width='2%'>&nbsp;</td><td width='32%'>\n";
		echo "<table width='100%' align='center' cellspacing='1' cellpadding='0' class='std_nicetable'>";
		echo "<thead>\n";
			echo "<tr><td colspan='4'>"._BLOCKPOSINEXTRA_."</td></tr>";
		echo "</thead>\n";
		echo "<tbody>\n";
			$n = 0;
			$result = $dblink->get_list("SELECT * FROM memht_blocks WHERE zona=1 ORDER BY posizione");
			foreach ($result as $row_block4) {
				$id4 = intval($row_block4['id']);
				$titolo4 = outCode($row_block4['titolo']);
				$nomefile4 = outCode($row_block4['nomefile']);
				$posizione4 = intval($row_block4['posizione']);
				
				if ($titolo4=="") { $titolo4 = $nomefile4; }
				$class = (($n++%2)!=0) ? "hlight" : "clean" ;
				
				echo "<tr><td width='5%' class='$class'>$posizione4</td><td class='$class'><b>$titolo4</b></td><td width='1%' class='$class' nowrap><a href='admin.php?page=blocks&op=changePosition&id=$id4&dir=up' title='"._UP_."'><img src='images/up.gif' border='0'></a> <a href='admin.php?page=blocks&op=changePosition&id=$id4&dir=down' title='"._DOWN_."'><img src='images/down.gif' alt='Down' border='0'></a></td></tr>\n";
			}
		echo "</tbody>\n";
		echo "</table>";
	
	echo "</td></tr></table>";
	
	//Conflitti di posizione
	for ($zona=0;$zona<=3;$zona++) {
		$conflitto = false;
		$result_conflitto = $dblink->get_list("SELECT * FROM memht_blocks WHERE zona='$zona' ORDER BY posizione");
		foreach ($result_conflitto as $row_conflitto) {
			$cid = intval($row_conflitto['id']);
			$pos = intval($row_conflitto['posizione']);
			
			if ($dblink->get_num("SELECT * FROM memht_blocks WHERE id!='$cid' AND zona='$zona' AND posizione='$pos'")>0) {
				$conflitto = true;
				break;
			}
		}
		if ($conflitto) {
			echo "<br>";
			echo "<div align='center' id='errorText'><b>"._CONFLICTINBLOCKS_."</b><br><a href='admin.php?page=blocks&op=reset&zona=$zona' title='"._ZONERESETPOSITION_." $zona'>"._ZONERESETPOSITION_." $zona</a></div>";
		}
	}
}	

function add($titolo,$titolo_on,$tipo,$zona,$posizione,$nomefile,$contenuto,$flag,$cflag,$rank,$enabled,$ok=false) {
	global $dblink;
	
	if (!$ok) {
		echo "<table width='100%' align='center' cellspacing='0' cellpadding='1'>";
		echo "<form name='form_arg' method='post' action='admin.php?page=blocks&op=addBlock&ok=true'>";		
			echo "<tr><td width='20%'><b>"._BLOCKTITLE_."</b></td><td><input type='text' name='titolo' size='40' maxlength='255'></td></tr>\n";
			
			echo "<tr><td><b>"._BLOCKTITLE_."</b></td><td>";
			echo "<select name='titolo_on'>\n";
				echo "<option value='1' selected>"._YES_."</option>\n";
				echo "<option value='0'>"._NO_."</option>\n";
			echo "</select></td></tr>\n";
			
			echo "<tr><td><b>"._TYPE_."</b></td><td>";
			echo "<select name='tipo'>\n";
				echo "<option value='0' selected>File</option>\n";
				echo "<option value='1'>HTML</option>\n";
			echo "</select></td></tr>\n";
			
			echo "<tr><td><b>"._ZONE_."</b></td><td>";
			echo "<select name='zona'>\n";
				echo "<option value='0' selected>Navigator</option>\n";
				echo "<option value='1'>Extra</option>\n";
				echo "<option value='2'>Main</option>\n";
			echo "</select></td></tr>\n";
			
			//File
			echo "<tr><td><b>"._FILE_."</b></td><td>";
			echo "<select name='nomefile'>\n";
				echo "<option value=''><b>"._CONTENT_."</b> (NO FILE)</option>\n";
				unset($filearray);
				$filearray = array();
				$handle = opendir('blocks/');
				while (false !== ($file = readdir($handle))) {
					if ($file != "." AND $file != ".." AND !eregi("html",$file) AND $file!="block_admin.php") {
						$file = str_replace("block_","",$file);
						$file = str_replace(".php","",$file);
						
						$filearray[] = $file;
					}
				}
				closedir($handle);
				@sort($filearray);
				@reset($filearray);
				foreach($filearray as $file) {
					echo "<option value='$file'>$file</option>\n";
				}				
				echo "</select></td></tr>\n";				

			//HTML
			echo "<tr><td valign='top'><b>"._CONTENT_."</b></td><td>";
			
				textarea("contenuto","100%","400px",1,"fulladmin");
			
			echo "</td></tr>\n";
			
			echo "<tr><td><b>"._STYLE_."</b> <img src='images/info.gif' title=\"header=["._INFORMATIONS_."] body=["._INFO_BLOCK_STYLE_."]\"></td><td>";
			echo "<select name='flag'>\n";
				echo "<option value='0' selected>"._STANDARD_."</option>\n";
				echo "<option value='1'>"._HIGHLIGHTED_." 1</option>\n";
				echo "<option value='2'>"._HIGHLIGHTED_." 2</option>\n";
			echo "</select>";
			echo " <input type='text' name='cflag' size='5' maxlength='255'>";
			echo "</td></tr>\n";

			echo "<tr><td><b>"._REQUIREDRANK_."</b></td><td>";
			echo "<select name='rank'>\n";
				echo "<option value='0' selected>"._GUEST_."</option>\n";
				echo "<option value='1'>"._USER_."</option>\n";
				echo "<option value='2'>"._POWERUSER_."</option>\n";
				echo "<option value='3'>"._ADMIN_."</option>\n";
				echo "<option value='4'>"._SUPERADMIN_."</option>\n";
			echo "</select></td></tr>\n";
			
			echo "<tr><td><b>"._ENABLED_."</b></td><td>";
			echo "<select name='enabled'>\n";
				echo "<option value='1' selected>"._YES_."</option>\n";
				echo "<option value='0'>"._NO_."</option>\n";
			echo "</select></td></tr>\n";
			echo "<tr><td colspan='2'><input type='submit' name='Submit' value='"._ADD_."'></td></tr>\n";
		echo "</form>\n";
		echo "</table>\n";
	} else {	
		$save = true;
		$msg = "";
		if ($titolo=="") { $save = false; $msg = _TITLEMISSING_; }
		if ($tipo==0 AND $nomefile=="") { $save = false; }
		if ($tipo==1 AND $contenuto=="") { $save = false; $msg = _CONTENTMISSING_; }
		if ($cflag>2) { $flag = $cflag; }
	
		$row_pos = $dblink->get_row("SELECT * FROM memht_blocks WHERE zona='$zona' ORDER BY posizione DESC LIMIT 1");
		$posizione = (intval($row_pos['posizione'])+1);
	
		if ($save) {
			$dblink->query("INSERT INTO memht_blocks (id,titolo,titolo_on,tipo,zona,posizione,nomefile,contenuto,flag,rank,enabled)
							VALUES (null,'$titolo','$titolo_on','$tipo','$zona','$posizione','$nomefile','$contenuto','$flag','$rank','$enabled')");
			echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=blocks'>";
		} else {
			echo "<div align='center' id='errorText'><b>"._FIELDINVALID_."</b><br>$msg</div>";
		}
	}
}

function edit($id,$titolo,$titolo_on,$tipo,$zona,$posizione,$nomefile,$contenuto,$flag,$cflag,$rank,$enabled,$ok=false) {
	global $dblink;
	
	$id = intval($id);

	if (!$ok) {
		$row_block = $dblink->get_row("SELECT * FROM memht_blocks WHERE id='$id'");
		$titolo = outCode($row_block['titolo']);
		$titolo_on = intval($row_block['titolo_on']);
		$tipo = intval($row_block['tipo']);
		$zona = intval($row_block['zona']);
		$posizione = intval($row_block['posizione']);
		$nomefile = outCode($row_block['nomefile']);
		$contenuto = outCode($row_block['contenuto']);
		$flag = intval($row_block['flag']);
		$rank = intval($row_block['rank']);
		$enabled = intval($row_block['enabled']);
	
		echo "<table width='100%' align='center' cellspacing='0' cellpadding='1'>";
		echo "<form name='form_arg' method='post' action='admin.php?page=blocks&op=editBlock&id=$id&ok=true'>";		
			echo "<tr><td width='20%'><b>"._BLOCKTITLE_."</b></td><td><input type='text' name='titolo' size='40' maxlength='255' value=\"$titolo\"></td></tr>\n";
			
			echo "<tr><td><b>"._BLOCKTITLE_."</b></td><td>";
			echo "<select name='titolo_on'>\n";
				if ($titolo_on==1) {
					echo "<option value='1' selected>"._YES_."</option>\n";
					echo "<option value='0'>"._NO_."</option>\n";
				} else {
					echo "<option value='1'>"._YES_."</option>\n";
					echo "<option value='0' selected>"._NO_."</option>\n";
				}
			echo "</select></td></tr>\n";
			
			echo "<tr><td><b>"._TYPE_."</b></td><td>";
			echo "<select name='tipo'>\n";
				if ($tipo==1) {
					echo "<option value='0'>File</option>\n";
					echo "<option value='1' selected>HTML</option>\n";
				} else {
					echo "<option value='0' selected>File</option>\n";
					echo "<option value='1'>HTML</option>\n";
				}
			echo "</select></td></tr>\n";
			
			echo "<tr><td><b>"._ZONE_."</b></td><td>";
			echo "<select name='zona'>\n";
				switch($zona) {
					case "0":
						echo "<option value='0' selected>Navigator</option>\n";
						echo "<option value='1'>Extra</option>\n";
						echo "<option value='2'>Main</option>\n";
					break;
					case "1":
						echo "<option value='0'>Navigator</option>\n";
						echo "<option value='1' selected>Extra</option>\n";
						echo "<option value='2'>Main</option>\n";
					break;
					case "2":
						echo "<option value='0'>Navigator</option>\n";
						echo "<option value='1'>Extra</option>\n";
						echo "<option value='2' selected>Main</option>\n";
					break;
				}
			echo "</select></td></tr>\n";
			
			echo "<tr><td><b>"._POSITION_."</b></td><td><input type='text' name='posizione' size='10' maxlength='255' value=\"$posizione\"></td></tr>\n";
			
			//File
			echo "<tr><td><b>"._FILE_."</b></td><td>";
			echo "<select name='nomefile'>\n";
				if ($nomefile=="") {
					echo "<option value='' selected>"._CONTENT_." (NO FILE)</option>\n";
				} else {
					echo "<option value=''>"._CONTENT_." (NO FILE)</option>\n";
				}
				unset($filearray);
				$filearray = array();
				$handle = opendir('blocks/');
				while (false !== ($file = readdir($handle))) {
					if ($file != "." AND $file != ".." AND !eregi("html",$file)) {
						$file = str_replace("block_","",$file);
						$file = str_replace(".php","",$file);
						
						$filearray[] = $file;
					}
				}
				closedir($handle);
				@sort($filearray);
				@reset($filearray);
				foreach($filearray as $file) {
					if ($nomefile==$file) {
						echo "<option value='$file' selected>$file</option>\n";
					} else {
						echo "<option value='$file'>$file</option>\n";
					}
				}
				echo "</select></td></tr>\n";	

			//HTML
			echo "<tr><td valign='top'><b>"._CONTENT_."</b></td><td>";
			
				textarea("contenuto","100%","400px",1,"fulladmin",$contenuto);
			
			echo "</td></tr>";
			
			echo "<tr><td><b>"._STYLE_."</b> <img src='images/info.gif' title=\"header=["._INFORMATIONS_."] body=["._INFO_BLOCK_STYLE_."]\"></td><td>";
			echo "<select name='flag'>\n";
				$selected = ($flag==0) ? " selected" : "" ;
				echo "<option value='0'{$selected}>"._STANDARD_."</option>\n";
				$selected = ($flag==1) ? " selected" : "" ;
				echo "<option value='1'{$selected}>"._HIGHLIGHTED_." 1</option>\n";
				$selected = ($flag==2) ? " selected" : "" ;
				echo "<option value='2'{$selected}>"._HIGHLIGHTED_." 2</option>\n";
			echo "</select>";
			$cflag = ($flag<=2) ? 0 : $flag ;
			echo " <input type='text' name='cflag' size='5' maxlength='255' value='$cflag'>";
			echo "</td></tr>\n";
			
			echo "<tr><td><b>"._REQUIREDRANK_."</b></td><td>";
			echo "<select name='rank'>\n";
				$selected = ($rank==0) ? " selected" : "" ;
				echo "<option value='0'{$selected}>"._GUEST_."</option>\n";
				$selected = ($rank==1) ? " selected" : "" ;
				echo "<option value='1'{$selected}>"._USER_."</option>\n";
				$selected = ($rank==2) ? " selected" : "" ;
				echo "<option value='2'{$selected}>"._POWERUSER_."</option>\n";
				$selected = ($rank==3) ? " selected" : "" ;
				echo "<option value='3'{$selected}>"._ADMIN_."</option>\n";
				$selected = ($rank==4) ? " selected" : "" ;
				echo "<option value='4'{$selected}>"._SUPERADMIN_."</option>\n";
			echo "</select></td></tr>\n";
			
			echo "<tr><td><b>"._ENABLED_."</b></td><td>";
			echo "<select name='enabled'>\n";
				if ($enabled==1) {
					echo "<option value='1' selected>"._YES_."</option>\n";
					echo "<option value='0'>"._NO_."</option>\n";
				} else {
					echo "<option value='1'>"._YES_."</option>\n";
					echo "<option value='0' selected>"._NO_."</option>\n";
				}
			echo "</select></td></tr>\n";
			echo "<tr><td colspan='2'><input type='submit' name='Submit' value='"._MODIFY_."'></td></tr>\n";
		echo "</form>\n";
		echo "</table>\n";
	} else {	
		$save = true;
		$msg = "";
		if ($titolo=="") { $save = false; $msg = _TITLEMISSING_; }
		if ($tipo==1 AND $contenuto=="") { $save = false; $msg = _CONTENTMISSING_; }
		if ($cflag>2) { $flag = $cflag; }
		
		if ($dblink->get_num("SELECT * FROM memht_blocks WHERE zona=$zona AND posizione=$posizione AND id!=$id")>0) {
			$row_pos = $dblink->get_row("SELECT * FROM memht_blocks WHERE zona=$zona ORDER BY posizione DESC LIMIT 1");
			$posizione = (intval($row_pos['posizione'])+1);
		}
							
		if ($save) {
			$dblink->query("UPDATE memht_blocks SET titolo='$titolo',titolo_on='$titolo_on',tipo='$tipo',zona='$zona',posizione='$posizione',nomefile='$nomefile',contenuto='$contenuto',flag='$flag',rank='$rank',enabled='$enabled' WHERE id='$id'");
			echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=blocks'>";
		} else {
			echo "<div align='center' id='errorText'><b>"._FIELDINVALID_."</b><br>$msg</div>";
		}
	}
}


function delete($id,$ok=false) {
	global $dblink;
	
	if ($ok) {
		$dblink->query("DELETE FROM memht_blocks WHERE id='$id'");
		echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=blocks'>";		
	} else {
		echo "<div align='center'><b>"._SUREDELETEBLOCK_."</b><br><a href='admin.php?page=blocks&op=deleteBlock&id=$id&ok=true' title='"._YES_."'>"._YES_."</a> - <a href='admin.php?page=blocks' title='"._NO_."'>"._NO_."</a></div>";
	}
}

function changeStatus($id) {
	global $dblink;
	
	$id = intval($id);
	
	$row_stat = $dblink->get_row("SELECT * FROM memht_blocks WHERE id='$id'");
	$enabled = intval($row_stat['enabled']);
	
	if ($enabled=="1") {
		$newStatus = "0";
	} else {
		$newStatus = "1";
	}
	$dblink->query("UPDATE memht_blocks SET enabled='$newStatus' WHERE id='$id'");
	echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=blocks'>";
}

function changeTitleOn($id) {
	global $dblink;
	
	$id = intval($id);
	
	$row_stat = $dblink->get_row("SELECT * FROM memht_blocks WHERE id='$id'");
	$title_on = intval($row_stat['titolo_on']);
	
	if ($title_on=="1") {
		$newStatus = "0";
	} else {
		$newStatus = "1";
	}
	$dblink->query("UPDATE memht_blocks SET titolo_on='$newStatus' WHERE id='$id'");
	echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=blocks'>";
}

function changePos($id,$dir) {
	global $dblink;
	
	$id = intval($id);
	$row_pos = $dblink->get_row("SELECT * FROM memht_blocks WHERE id='$id'");
	$pos = intval($row_pos['posizione']);
	$zona = intval($row_pos['zona']);
	switch($dir) {
		case "up":
			$pos2 = $pos - 1;
			if ($dblink->get_num("SELECT * FROM memht_blocks WHERE zona='$zona' AND posizione<'$pos'")>0) {
				$dblink->query("UPDATE memht_blocks SET posizione=posizione+1 WHERE zona='$zona' AND posizione='$pos2'");
				$dblink->query("UPDATE memht_blocks SET posizione=posizione-1 WHERE zona='$zona' AND id='$id'");
			}
		break;
		case "down":
			$pos2 = $pos + 1;
			if ($dblink->get_num("SELECT * FROM memht_blocks WHERE zona='$zona' AND posizione>'$pos'")>0) {
				$dblink->query("UPDATE memht_blocks SET posizione=posizione-1 WHERE zona='$zona' AND posizione='$pos2'");
				$dblink->query("UPDATE memht_blocks SET posizione=posizione+1 WHERE zona='$zona' AND id='$id'");
			}
		break;
	}
	echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=blocks'>";
}

function resetPos($zona,$ok=false) {
	global $dblink;
	
	$zona = intval($zona);
	if ($ok) {
		$posizione = 0;
		$result_page = $dblink->get_list("SELECT * FROM memht_blocks WHERE zona='$zona' ORDER BY titolo");
		foreach ($result_page as $row_page) {
			$id = intval($row_page['id']);
			$dblink->query("UPDATE memht_blocks SET posizione='$posizione' WHERE id='$id'");
			$posizione++;
		}
		echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=blocks'>";		
	} else {
		echo "<div align='center'><b>"._SURERESETPOSITIONIZONE_." $zona?</b><br><a href='admin.php?page=blocks&op=reset&zona=$zona&ok=true' title='"._YES_."'>"._YES_."</a> - <a href='admin.php?page=blocks' title='"._NO_."'>"._NO_."</a></div>";
	}
}

function showBlock($id) {
	global $dblink;
	
	$id = intval($id);
	
	$row_blocks = $dblink->get_row("SELECT * FROM memht_blocks WHERE id='$id'");
	$titolo = outCode($row_blocks['titolo']);
	$titolo_on = intval($row_blocks['titolo_on']);
	$tipo = intval($row_blocks['tipo']);
	$nomefile = outCode($row_blocks['nomefile']);
	$contenuto = outCode($row_blocks['contenuto']);
	
	echo "<table width='200' style='padding: 2px; border: 1px solid #999; background-color: #FFF; font-family: Verdana; font-size: 10px;' align='center'><tr><td>";
	if ($titolo_on==1) { echo "<div><b>$titolo</b></div>"; }
	if ($tipo==0) {
		if (file_exists("blocks/block_$nomefile.php")) {
			include("blocks/block_$nomefile.php");
		} else {
			echo "<div id='errorText' align='center'><b>"._FILENOTFOUND_."</b></div>";
		}
	} else {
		echo $contenuto;			
	}
	echo "</td></tr></table>";
}

require_once("admin/inc/inc_header.php");
	admin_page_title($page);
			
	openTable();
		echo "<div align='center' class='box'>";
			echo "<a href='admin.php?page=blocks' title='"._MAIN_."'><img src='admin/icons/blocks.png' border='0' title='"._MAIN_."' align='top'> "._MAIN_."</a>";
			echo " - <a href='admin.php?page=blocks&op=addBlock' title='"._ADD_."'><img src='admin/icons/add.png' border='0' title='"._ADD_."' align='top'> "._ADD_."</a>";
		echo "</div>";
	closeTable();
	
	openTable();
	
	global $posizione;
		switch($op) {
			case "addBlock":
				add($titolo,$titolo_on,$tipo,$zona,$posizione,$nomefile,$contenuto,$flag,$cflag,$rank,$enabled,$ok);
			break;
			
			case "deleteBlock":
				delete($id,$ok);
			break;
			
			case "editBlock":
				edit($id,$titolo,$titolo_on,$tipo,$zona,$posizione,$nomefile,$contenuto,$flag,$cflag,$rank,$enabled,$ok);
			break;
			
			case "switch":
				changeStatus($id);
			break;
			
			case "switchTitle":
				changeTitleOn($id);
			break;
			
			case "changePosition":
				changePos($id,$dir);
			break;
			
			case "reset":
				resetPos($zona,$ok);
			break;
			
			case "showBlock":
				showBlock($id);
			break;
		
			default:
				blocksList();
			break;
		}
	
	closeTable();
require_once("admin/inc/inc_footer.php");

?>