<?
$page = "choose_template.php";
include "manager_header.php";

if(isset($_POST['task'])) { $task = $_POST['task']; } elseif(isset($_GET['task'])) { $task = $_GET['task']; } else { $task = "main"; }
if($owner_group_info[templates] == "") { exit(); }
if($user_status == "member") { exit(); }



if($task == "dochange") {
$t_id = $_POST['t_id'];
$template_array = explode(",", $owner_group_info[templates]);
if(!in_array($t_id, $template_array)) { exit(); }

$newtemplate = mysql_fetch_assoc(mysql_query("SELECT * FROM bhost_templates WHERE t_id='$t_id'"));
$html = str_replace("'", "\'", $newtemplate[html]);
mysql_query("UPDATE bhost_weblogs SET template='$html' WHERE w_id='$weblog_info[w_id]'");
echo $head;
echo "
<h2>$choose_template1</h2>
$choose_template2
<br><br>
<table cellpadding='0' cellspacing='0'>
<form action='choose_template.php' method='POST'>
<tr>
<td>
<input type='submit' class='button' value='$choose_template3'>&nbsp;
</td>
</form>
";
if($owner_group_info[allow_edittemplate] == "1") {
echo "
<form action='edit_template.php' method='POST'>
<td>
<input type='submit' class='button' value='$choose_template4'>
</td>
";
}
echo "
</tr>
</table>
";
echo $foot;
exit();
}






if($task == "main") {
echo $head;
echo "
<h2>$choose_template5</h2>
$choose_template6
";
if($owner_group_info[allow_edittemplate] == "1") {
echo "$choose_template10";
}

$count = 0;
$templates = mysql_query("SELECT * FROM bhost_templates");
$template_array = explode(",", $owner_group_info[templates]);
while($template = mysql_fetch_assoc($templates)) {
 if(in_array($template[t_id], $template_array)) {
  $count = $count + 1;
 }
}

if($count == 0) {
echo "
<br><br>
<b>$choose_template7</b>
";
} else {
echo "

<table cellpadding='0' cellspacing='0' width='100%'>
<tr>
<td valign='top'>
";
}

$templates = mysql_query("SELECT * FROM bhost_templates");
$template_array = explode(",", $owner_group_info[templates]);
while($template = mysql_fetch_assoc($templates)) {
 if(in_array($template[t_id], $template_array)) {
  if($template[image] != "" AND $template[image] != "http://") { 
   $image = "<img src='$template[image]' border='0' class='template'>"; 
  } else {
   $image = "<img src='../images/noimage.gif' border='0' class='template'>"; 
  }

  $image = "<a href='../example.php?t_id=$template[t_id]' target='_blank'>$image</a>"; 


  echo "
  <div class='float'><form onsubmit='return confirm(\"$choose_template8\");' action='choose_template.php' method='POST'>
  <b>$template[name]</b>
  <div class='templatebox' valign='top' align='center'>$image<br>
  <input type='submit' class='button' value='$choose_template9' style='margin-top: 5px;'>
  <input type='hidden' name='t_id' value='$template[t_id]'>
  <input type='hidden' name='task' value='dochange'>
  </div>
  </form>
  </div>
  ";
 }
}

if(mysql_num_rows($templates) != 0) {
echo "
</td>
</tr>
</table>";
}
echo $foot;
}


?>