#!/usr/bin/perl
require "wiz_resources";
require "wiz_creator";
$action=param("action");
$page_to_edit=param("page");

cookie_read();

if ($user ne "") {
  if (($page_to_edit ne "") && (-e "${homesite}$page_to_edit")) {

    if ($action eq "submit") {    
      load_paragraphs();
      header("Adding paragraph to '".page_name($page_to_edit)."'","","edit_page");  
      $new_para_type=param("style");
       $new_para_pos=param("add_to");
      headline("Adding paragraph","","","Adding a new paragraph of the style '$new_para_type' to the $new_para_pos of '".page_name($page_to_edit)."'...<br>&nbsp;");
      
      push @para_title, "Headline";
      push @para_text, "example text";
      push @para_type, $new_para_type;
      push @para_pic, "";
      push @para_align, "";
      push @para_caption, "";
      push @para_morelink, "";
    
      $para_number=@para_title;
      if (($new_para_pos eq "top") && ($para_number > 1)) {    
        for($shift_loop = ($para_number-1); $shift_loop>0; $shift_loop--) {
          $para_title[$shift_loop]   =$para_title[$shift_loop-1];
          $para_text[$shift_loop]    =$para_text[$shift_loop-1];
          $para_type[$shift_loop]    =$para_type[$shift_loop-1];
          $para_pic[$shift_loop]     =$para_pic[$shift_loop-1];
          $para_align[$shift_loop]   =$para_align[$shift_loop-1];
          $para_caption[$shift_loop] =$para_caption[$shift_loop-1];
          $para_morelink[$shift_loop]=$para_morelink[$shift_loop-1];
        }  
        $para_title[0]="Headline";
        $para_text[0]="example text";
        $para_type[0]=$new_para_type;
        $para_pic[0]="";
        $para_align[0]="";
        $para_caption[0]="";
        $para_morelink[0]="";    
      }
      creator($page_to_edit);
        print "You can now <a href=\"$baseurl$page_to_edit\" target=\"_blank\">view your page</a>, <a href=\"/cgi-bin/edit_page?page=$page_to_edit\">return to editing your page</a> or <a href=\"/cgi-bin/edit\">go back to the main edit page</a>";
    
    } else {
      header("Add paragraph to '".page_name($page_to_edit)."'","","edit_page");
      headline("Add paragraph","","","Add a new paragraph to '".page_name($page_to_edit)."'.  You can delete this paragraph again using the <a href=\"/cgi-bin/edit_page?page=$page_to_edit\">page editing section</a>.");
      
      print "<form method=\"post\" action=\"$ENV{SCRIPT_NAME}\">\n";
      print "<input type=\"hidden\" name=\"action\" value=\"submit\">\n";
      print "<input type=\"hidden\" name=\"page\" value=\"$page_to_edit\">\n";
      
      print "<br>&nbsp;\n";
      
      print "<table border=\"0\" cellspacing=\"0\" cellpadding=\"2\">\n";
      print "<tr><td>\n";
      
      print "<h3>Add to...</h3>\n";
      print "<input type=\"radio\" name=\"add_to\" value=\"top\" checked>&nbsp;Top<br>\n";
      print "<input type=\"radio\" name=\"add_to\" value=\"bottom\">&nbsp;Bottom\n";
      
      print "<br>&nbsp;\n";
      print "<h3>What style...</h3>\n";
      print "<input type=\"radio\" name=\"style\" value=\"paragraph\" checked>&nbsp;Plain Text<br>\n";
      print "<input type=\"radio\" name=\"style\" value=\"address\">&nbsp;Address<br>\n";
      print "<input type=\"radio\" name=\"style\" value=\"mailform\">&nbsp;Email form<br>\n";
      print "<input type=\"radio\" name=\"style\" value=\"news\">&nbsp;'Latest News' item\n";
      
      
      print "<br>&nbsp;\n";
      print "</td></tr>\n";
      print "<tr><td align=\"center\">\n";
      print "<input type=\"submit\" value=\"continue\">\n";
      print "</td></tr></table>\n";
      print "</form>\n";
    }

  } else {
    header("Error: Incorrect Page ('".page_name($page_to_edit)."')","","edit");
    headline("Error: incorrect page","red","","The page requested does no exist.  Please <a href=\"/cgi-bin/edit\">try again</a>.");
  }

}

if ($user eq "") {
  header("Access Denied: Edit Page","","edit");
  headline("Error: Access denied","red","","You should not use this page unless you have logged into the system.  Please <a href=\"/cgi-bin/login\">log in</a> and try again.");
}

footer();