#!/usr/bin/perl 
#---------------------------------------------------
#  C R O O K E D B U S H . C O M  -  Internet TreePad
#  Designed by Jason Silver.  Please don't
#  remove this copyright notice.  Thanks!
#
# Visit www.crookedbush.com to register this script
# Registration is only $10.00 U.S.A Currency.
# Includes latest version, updates, support, and more.
#
#---------------------------------------------------
# User Defined Variables
#---------------------------------------------------
# this is the path, not the URL to the TreePad file (make sure it ends with /)
# (it's a good idea to place iTreePad in a new directory)
  $datapath	   = '';
  $image_directory = '/itree/'; # no trailing slash, URL, not path.

# this is the password to use for editing your file online
  $password     = 'password';
  $folders_open = 1; # Set this to 1 to view all folders open upon first viewing.

# this is the body info which gives your iTreePad the right 'look.'
  $background_image   = "";
  $background_color   = "white";
  $visited_link_color = "#990000";
  $text_color	      =	"#000000";
  $link_color	      =	"#cc0000";
  $active_link_color  = "#cc0000";

  $title_image_location = 'logo_itree.gif';
  $image_description    = 'xxc的閱讀筆記';
  $home_page		= 'http://www.xxc.idv.tw/itree/';     #	full url link for left pane
  $home_page_text       = 'http://www.xxc.idv.tw/itree/	';
  $CSS_style		= 'style.css';


# S T O P    E D I T I N G  -  Y O U ' R E   A L L   S E T !
#___________________________________________________
#---------------------------------------------------
# Program
#---------------------------------------------------

  print "Content-type: text/html\n\n";		     # set up print type for browser

  $copyright_year = "2000";
  $version	  = "5.03";

#---------------------------------------------------
# READ VARIABLES - take info from web environment
#---------------------------------------------------

$referring_page	 = $ENV{'HTTP_REFERER'};
$url		 = $ENV{'SERVER_NAME' };
$script_url	 = $ENV{'SCRIPT_NAME' };

#---------------------------------------------------
# Parse Form - take info from get ('QUERY_STRING' ?asdjkl=asjkl)
#---------------------------------------------------

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); 
   @input_chunks = split(/&/, $buffer); 
    foreach $chunk (@input_chunks) { 
     ($name, $value) = split(/=/, $chunk);
      $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
      $name =~ tr/+/ /;
      $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
      $value =~ tr/+/ /;
      $FROMWEB{$name} = $value;
    } # end of foreach

   @input_chunks = split(/&/, $ENV{'QUERY_STRING'}); 
    foreach $chunk (@input_chunks) { 
     ($name, $value) = split(/=/, $chunk);
      $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
      $name =~ tr/+/ /;
      $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
      $value =~ tr/+/ /;
      $FROMWEB{$name} = $value;
    } # end of foreach $chunk


if($FROMWEB{'password'} eq $password){
  $edit = "true";
} # end of if

if($FROMWEB{'password'} eq 'login'  ){
  &do_login;
} # end of if

if($FROMWEB{'action'}   eq 'save'   ){
  &save_edit;
} # end of if

if($FROMWEB{'action'}   eq 'search' ){
  &search;
} # end of if


#____________________________________________
# check for a file name in URL (query_string)

$treepad_file = $FROMWEB{'file'};

if ($treepad_file eq ""){

  #________________________________________
  # no file name? go to subroutine to show
  # open file dialog
  &list_directory;
  &open_dialog;
} # end of of $treepad_file eq ""

#_________________________________________
# set folder level to 0 for the JavaScript

$folder_level = 0;

#___________________________
# set spacing string length

$spacing_nbsp = '    ';

#____________________
# Fill TreePad array

&open_file;

#---------------------------------------------------
# If ?link= contains data, then print that data page.
#---------------------------------------------------

if ($FROMWEB{'link'} ne ""){
   &print_link_data;
   } # end of from web link

#________________________
# Set up Title, Div, etc.

  if ($folders_open == 1){
    $folder_display_status = "";
  }
  else{
    $folder_display_status = "None";
  }

&print_header_html;

#-------------------------------------------
# Go line by line
#-------------------------------------------
for $count (0..$#treepad_line){

  chomp($treepad_line[$count]); # remove carriage returns from end of line

  if ($treepad_line[$count] eq "<node>"){
     &print_node;
  } # end of if $treepad_line[$count] eq "<node>"

} # end of for $count (1..$#treepad_line)

#____________________________
# Now print HTML ending tags

print "</DIV></DIV></DIV></TD></TR></TABLE></body></HTML>";


exit;

#-------------------------------------------
# SUB Print Node
#-------------------------------------------

sub print_node{

      $level = $count + 2;

      #___________________________________
      # Set up ID tags for JavaScript to work

      $folder_level_i   =  "Out$folder_level i";
      $folder_level_i   =~ s/ //;
      $folder_level_s   =  "Out$folder_level s";
      $folder_level_s   =~ s/ //;
      $folder_level_t   =  "Out$folder_level t";
      $folder_level_t   =~ s/ //;
      $folder_level_top =  "Top$folder_level";

      #________________________________
      # Figure out indentation spacing

      $indent_factor = $treepad_line[$level] + 1;

      #__________________
      # Now print indent

      $indent = 0;
      print "<!-- Folder -->\n";
      while ($indent < $indent_factor){
      print $spacing_nbsp;
      $indent++;
      } # end of while $count < $indent_factor
      print"\n";

      #_________________________________
      # Calculate Link info for <a> tag

      $target_link = $folder_level+1;
      chomp ($treepad_line[$count+1]);

      &find_child;

      #_________________________________
      # Print Folder and link to browser

      if($child eq "false"){
      print <<HTML;
      <IMG align=middle src="$image_directory/nofolder.gif" id=$folder_level_i >&nbsp;
      <A id=$folder_level_top class=Topic href="$script_url?link=$target_link&file=$FROMWEB{'file'}" TARGET="MAIN">$treepad_line[$count+1]</A>
      <BR>
      <DIV id=$folder_level_s style="display:$folder_display_status" >

HTML

      } # end of if child is false

      else {

      print <<HTML;
      <IMG align=top src="$image_directory/folder.gif" id=$folder_level_i class=Outline>&nbsp;
      <A id=$folder_level_top class=Topic href="$script_url?link=$target_link&file=$FROMWEB{'file'}" TARGET="MAIN">$treepad_line[$count+1]</A>
      <BR>
      <DIV id=$folder_level_s style="display:$folder_display_status">

HTML
     } # end of else

 &count_and_print_divs;

 $folder_level++;

} # end of sub print_node

#---------------------------------------------------
# Determine if <node> has children
#---------------------------------------------------

sub find_child{

  $child = "true";

  #__________________________________
  # Look ahead to find the next node.

  $a = $count + 1 ; # Don't want to count the current "<node>"

  for $new_count ($a..$#treepad_line){

    chomp ($treepad_line[$new_count]);
    if ($treepad_line[$new_count] eq "<node>"){

       $new_level = $new_count + 2;

      #_______________________________________________________
      # If the node has a lower number, it doesn't have a child.
      # Print a </DIV>, then subtract parent number from child
      # number and print that many additional </DIV>s
      # If the next number is equal, just print a </DIV>

      if ($treepad_line[$new_level] <= $treepad_line[$level]){
        # That Node has no children
	 $child	= "false";
        } # end of while $divs_to_print < 0
        $done = 1;

    if ($done == 1){
      last;
    } # end of if done

    } # end of if found <NODE>

   if ($done == 1){
     last;
   } # end of if done

  } # end of for $new_count

if ($done == 0){
    $child = "false";
   }

$done = 0;

} # end of sub find child

#---------------------------------------------------
# Count and Print </DIVS>
#---------------------------------------------------

sub count_and_print_divs{

  #__________________________________
  # Look ahead to find the next node.

  $a = $count + 1 ; # Don't want to count the current "<node>"

  for $new_count ($a..$#treepad_line){

    chomp ($treepad_line[$new_count]);
    if ($treepad_line[$new_count] eq "<node>"){

       $new_level = $new_count + 2;

      #_______________________________________________________
      # If the node has a lower number, it doesn't have a child.
      # Print a </DIV>, then subtract parent number from child
      # number and print that many additional </DIV>s
      # If the next number is equal, just print a </DIV>

      if ($treepad_line[$new_level] > $treepad_line[$level]){
        # Higher level, so don't print a </DIV> and stop loop.
        $done = 1;
      } # end of if

      if ($treepad_line[$new_level] == $treepad_line[$level]){
        # Same level, so print a </DIV> and stop loop.
        print "</DIV>\n";
        $done = 1;
      } # end of if

      if ($treepad_line[$new_level] < $treepad_line[$level]){
        # That Node has no children so print a </DIV>
        print "</DIV>\n";

        # Now calculate how many additional </DIVS> to print
        $divs_to_print = $treepad_line[$level] - $treepad_line[$new_level];
        while ($divs_to_print > 0){
	 print "</DIV>\n";
        $divs_to_print = $divs_to_print - 1;
        } # end of while $divs_to_print > 0
        $done = 1;
        $divs_to_print = 0;
      } # end of if

    if ($done == 1){
      last;
    } # end of if done

    } # end of if found <NODE>

   if ($done == 1){
     last;
   } # end of if done

  } # end of for $new_count

$done = 0;

} # end of sub count_and_print_divs

#---------------------------------------------------
# SUB Print Link Data
#---------------------------------------------------

sub print_link_data{

print <<HTML;

<HTML>
<HEAD>
<TITLE>$treepad_line[2]</TITLE>
<link rel="stylesheet" href="$CSS_style" type="text/css">
<STYLE type="text/css">
  DIV { font-family:Arial; color:$text_color; }
  .Outline { font-size:9pt; line-height:9pt; color:$text_color; cursor:hand; text-decoration:none; font-weight:bold; }
  .Topic { font-size:9pt; line-height:9pt; color:$text_color; cursor:hand; text-decoration:none; font-weight:bold; }
</STYLE>

</HEAD>
<body background="$background_image" bgcolor="$background_color" vlink="$visited_link_color" text="$text_color" link="$link_color" alink="$active_link_color">

HTML

for $count (0..$#treepad_line){

  chomp($treepad_line[$count]); # remove carriage returns from end of line

  if ($treepad_line[$count] eq "<node>"){
     $folder_level++;
     if ($folder_level == $FROMWEB{'link'}){
       &show_info;
     } # end of if
  } # end of if $treepad_line[$count] eq "<node>"
} # end of for $count (1..$#treepad_line)

} # end of sub print_link_data

#---------------------------------------------------
# Print Text to Browser
#---------------------------------------------------

sub show_info{

$line_break = "<br>";

#_______________________
# Display Node Heading

 $view_down_node = $FROMWEB{'link'} + 1;
 $view_up_node	 = $FROMWEB{'link'} - 1;

 # TEMP
 $id = "Out$FROMWEB{'link'} s";
 $id =~ s/ //;

print <<HTML;
 <table width="100%" border="0"><tr>

 <td width="25%" align="left" valign="top" nowrap><font size="1" face="Tahoma,Arial">Powered by <a href="http://www.crookedbush.com">CrookedBush.com</a><br>
  and <a href="http://www.freebyte.com">FreeByte.com</a> </font></td>
 <td width="75%" align="right" valign="top"><font size="1" face="Tahoma,Arial">
   <a href="$script_url?file=$FROMWEB{'file'}&action=search">Search</a> |
   <a href="$script_url?link=$FROMWEB{'link'}&file=$FROMWEB{'file'}&hide=true" target="_top">Hide Tree</a> |
   <a href="$script_url?file=$FROMWEB{'file'}&self=$FROMWEB{'link'}" target="_top">Show Tree</a> |
   <a href="$script_url?link=$view_up_node&file=$FROMWEB{'file'}&hide=$FROMWEB{'hide'}">View&nbsp;Up&nbsp;Node</a> |
   <a href="$script_url?link=$view_down_node&file=$FROMWEB{'file'}&hide=$FROMWEB{'hide'}">View&nbsp;Next&nbsp;Node</a> |
   <a href="$script_url?password=login&link=$FROMWEB{'link'}&file=$FROMWEB{'file'}&hide=$FROMWEB{'hide'}">Edit</a>

 </font></td>
 </tr>
 </table>
 <hr>
 <b>$treepad_line[$count+1]</b><br>
HTML

if ($edit eq "true"){
   print "<form method='POST'><p>Title: <input type='text' length='15' name='node_title' value=\"$treepad_line[$count+1]\"><br>";
   print "<textarea name='edited_text' rows='25' cols='40'>";
   $line_break = "";
   } # End of if $edit eq true

      $i = $count + 3;
      chomp ($treepad_line[$i]);
      while ($treepad_line[$i] ne "<end node> 5P9i0s8y19Z"){

        &check_for_node_link;

        if ($edit ne "true") {
	  $treepad_line[$i] =~ s/\"node:\/\//<a	href=\"$script_url?link=$link_number&file=$FROMWEB{'file'}\">click to follow this link<br><\/a>/;

	  #___________________________________________
	  # Now	check for http:// hyperlinks

	  # beginning:
	  $treepad_line[$i] =~ s/Click http:\/\//<a href=\"http:\/\//;

	  # end:
	  $treepad_line[$i] =~ s/%-%/\"><b>Click here<\/b><\/a>/;

        } # end of if $edit ne true

        print "$treepad_line[$i]$line_break\n";
        $i++;
        chomp ($treepad_line[$i]);
      } # end of while

if ($edit eq "true"){
     print <<HTML;
</textarea>
     <br>
     <input type="hidden" name="password"     value="$FROMWEB{'password'}">
     <input type="hidden" name="file"	      value="$FROMWEB{'file'}">
     <input type="hidden" name="link"	      value="$FROMWEB{'link'}">
     <input type="hidden" name="child_number" value="$treepad_line[$count+2]">
     <input type="hidden" name="edit"	      value="false">
     <input type="hidden" name="action"	      value="save">
     <input type="submit"		      value="Save">
     </p>
     </form>
HTML

   } # End if if $edit eq true

 #____________________________
 # Now print HTML ending tags

 print <<HTML;

 </body>
 </HTML>

HTML

 exit;

} # end of sub

#-------------------------------------------
# Check For Node Link
#-------------------------------------------

sub check_for_node_link{

@check_for_links = split(/\"/, $treepad_line[$i]);

foreach $item (@check_for_links){

  #_______________________
  # take out the 'node://'

  $item =~ s/node:\/\///;

  #____________________________________________
  # now split the rest of the link on the slash

  @node_breakdown = split (/\//, $item);

  #________________
  # Reset Variables

  $link_number = 0;
  $id = 0;
  $start_point = 0;

  #___________________________________
  # now follow the link down the file.

    for $c ($start_point..$#treepad_line){
      chomp($treepad_line[$c]); # remove carriage returns from end of line

      if ($treepad_line[$c] eq "<node>"){
      $link_number++;

        if ($treepad_line[$c+1] eq $node_breakdown[$id]){
	  # Found child
	  $id++;
	  $start_point = $c;
	  if ($id == scalar(@node_breakdown)){
	  last;
	  } # end of if
        } # end of if
      } # end of if
    } # end of for $c


 } # end of foreach $item


} # end of sub check_for_node_link

#-------------------------------------------
# SUB Print Header HTML
#-------------------------------------------

sub print_header_html{

print <<HTML;

<HTML>
<HEAD>
<TITLE>$treepad_line[2]</TITLE>

<link rel="stylesheet" href="$CSS_style" type="text/css">
<STYLE type="text/css">
  DIV { font-family:Arial; color:$text_color; }
  .Outline { font-size:9pt; line-height:9pt; color:$text_color; cursor:hand; text-decoration:none; font-weight:bold; }
  .Topic { font-size:9pt; line-height:9pt; color:$text_color; cursor:hand; text-decoration:none; font-weight:bold; }
</STYLE>

<SCRIPT LANGUAGE="JavaScript">
var img1, img2;
img1 = new Image();
img1.src = "$image_directory/folder.gif";
img2 = new Image();
img2.src = "$image_directory/ofolder.gif";
function doOutline() {
  var targetId, srcElement, targetElement;
  srcElement = window.event.srcElement;
  if (srcElement.className == "Outline") {
      srcElement = srcElement.id
      srcElement = srcElement.substr(0, srcElement.length-1);
      targetId = srcElement + "s";
      srcElement = srcElement + "i";
      srcElement = document.all(srcElement);
      targetElement = document.all(targetId);
      if (targetElement.style.display == "none") {
	  targetElement.style.display =	"";
	  srcElement.src = "$image_directory/ofolder.gif";
      } else {
        targetElement.style.display = "none";
        srcElement.src = "$image_directory/folder.gif";
     }
  }
}
document.onclick = doOutline;
document.onmouseover = doDocumentOnMouseOver ;
document.onmouseout = doDocumentOnMouseOut ;
// CHANGE HEADLINE COLOR ONMOUSEOVER
function doDocumentOnMouseOver() {
    var eSrc = window.event.srcElement ;
    if (eSrc.className == "Outline"||eSrc.className == "Topic")
        { document.all(eSrc.id).style.color= "$active_link_color" }
  }
// CHANGE HEADLINE COLOR ONMOUSEOUT
function doDocumentOnMouseOut() {
    var eSrc = window.event.srcElement ;
    if (eSrc.className == "Outline"||eSrc.className == "Topic")
	 { document.all(eSrc.id).style.color= "$text_color" }
  }

</SCRIPT>

</HEAD>

HTML

if (($FROMWEB{'self'} != 1) and ($FROMWEB{'hide'} ne "true")){

  if ($FROMWEB{'self'}){
    $link_for_main_window = $FROMWEB{'self'};
  } # end of if
  else {
    $link_for_main_window = 1;
  } # end of else

  print <<HTML;

<FRAMESET ROWS="100%" COLS="30%,70%" >
  <FRAME NAME="EXPLORER" src="$script_url?file=$FROMWEB{'file'}&self=1"	 SCROLLING="auto">
  <FRAME NAME="MAIN"	 src="$script_url?file=$FROMWEB{'file'}&link=$link_for_main_window"  SCROLLING="auto">
   <noframes>
     <body>
     <font face="Tahoma, Arial"><b>This TreePad Browser requires frame compatibility, but your browser doesn't understand them. </b></font>
     </body>
   </noframes>
</FRAMESET>

HTML

}# end of if

print <<HTML;

<body background="$background_image" bgcolor="$background_color" vlink="$visited_link_color" text="$text_color" link="$link_color" alink="$active_link_color">
<center><IMG SRC="$image_directory/$title_image_location" alt="$image_description"><br>
<a href="$home_page" target="_top">$home_page_text</a><br>
<!--Make sure you <a href="javascript:location\.reload()">REFRESH</a> your browser!-->
</center><br><br>
<TABLE WIDTH="100%" HEIGHT="100%" CELLSPACING=0 CELLPADDING=0 BORDER=0>
<TR VALIGN="top">
 <TD nowrap>
 <DIV id=$folder_level_s style="display:$folder_display_status" >

HTML

} # end of sub print_header_html

#---------------------------------------------------
# sub Login - Display Password Box
#---------------------------------------------------
sub do_login{

print <<HTML;

<HTML>
<HEAD>
<TITLE>Password Login</TITLE>
</HEAD>
<link rel="stylesheet" href="$CSS_style" type="text/css">
<body background="$background_image" bgcolor="$background_color" vlink="$visited_link_color" text="$text_color" link="$link_color" alink="$active_link_color">

<br><br><br>
<form method="POST" action="$script_url">
 <div align="center"><center><table border="0" width="100%" cellspacing="0" cellpadding="0">
   <tr>
    <td width="100%" align="center">
     <font face="Arial" size="3">Please enter a valid password: <br>
     <input type="password" name="password" size="20">
     <input type="hidden"   name="file"	    value="$FROMWEB{'file'}">
     <input type="hidden"   name="link"	    value="$FROMWEB{'link'}">
     <input type="submit" value="Submit"></font>
    </td>
   </tr>
  </table>
  </center></div><p>&nbsp;</p>
</form>
<br><br><br>

HTML

exit;

} # End of sub login

#---------------------------------------------------
# Search Directory for HJT Files
#---------------------------------------------------
sub list_directory{

 opendir (FILELIST, "$datapath");
 rewinddir (FILELIST);
 @dirlist =  grep(!/^\.\.?$/, readdir (FILELIST));
 closedir (FILELIST);

 $num_in_dir = @dirlist; # the number of items in @dirlist
 $counter = 0;

 while ($num_in_dir > $counter) {

  @check = split(/\./, $dirlist[$counter]);

   if (($check[1] eq "hjt")|($check[1] eq "HJT")){
   push (@directory, $dirlist[$counter]);
   }  # end of if

   $counter++;

 } # end of while


} # end of sub list_directory

#---------------------------------------------------
# Display 'open file' dialog if multiple files exist
#---------------------------------------------------
sub open_dialog{


# @durectory contains all HJT files on server.

 if (scalar(@directory) > 1){

   print <<HTML;

<HTML>
<HEAD>
<TITLE>Choose TreePad File</TITLE>

</HEAD>
<link rel="stylesheet" href="$CSS_style" type="text/css">
<body background="$background_image" bgcolor="$background_color" vlink="$visited_link_color" text="$text_color" link="$link_color" alink="$active_link_color">

<br><br><br>
<form method="GET" action="$script_url">
 <div align="center"><table border="0" width="600" cellspacing="0" cellpadding="0">
   <tr>
    <td width="100%">
     <font face="Arial" size="3">Please select a file:<br>

HTML

  foreach $item(@directory){

    print "<input type=\"radio\"  name=\"file\" value=\"$item\"> $item<br>\n";

} # end of foreach $item

    print <<HTML;

     <input type="hidden" name="link" value="$FROMWEB{'link'}">
     <br>
     <input type="submit" value="Load"></font>
    </td>
   </tr>
  </table>
  </div><p>&nbsp;</p>
</form>
<br><br><br>

</body>
</html>

HTML

exit;

 } # end of if $#directory > 1

 else {

 $treepad_file = $directory[0];

 } # end of else

}# end of sub open_dialog

#---------------------------------------------------
# Open File on Server
#---------------------------------------------------
sub open_file{

 open (server_file, "$datapath$treepad_file")
  or &dienice("I can't open the TreePad file: $datapath$treepad_file. <br>Error Code: $!");
  @treepad_line = <server_file>;
  $FROMWEB{'file'} = $treepad_file;

 close (server_file);

} # end of sub open_file

#---------------------------------------------------
# Save File to Server
#---------------------------------------------------
sub save_edit{

 $treepad_file = $FROMWEB{'file'};
 &open_file;

 #______________________________________
 # Count Nodes to find $FROMWEB{'LINK'}

  $count_node = 0;

  for $edit_node_line_number (0..$#treepad_line){

  chomp($treepad_line[$edit_node_line_number]); # remove carriage returns from end of line

  if ($treepad_line[$edit_node_line_number] eq "<node>"){
     $count_node++;
     if ($count_node == $FROMWEB{'link'}){
       &replace_node;
       exit;
     } # end of if $count_node...
  } # end of if $treepad_line[$count] eq "<node>"
 } # end of for $count (1..$#treepad_line)

 print <<HTML;
 <pre>
 Error -  Node not found

 Looking for node number $FROMWEB{'link'}
 Search loop got to node $count_node

 Variables:
   Treepad Line Length: $#treepad_line - this should be the number of lines in the treepad file.
   Edit Node Line Number: $edit_node_line_number - this is how many lines were counted through during the search
   Actual Treepad File: @treepad_line

 </pre>

HTML

 exit;

} # end of sub save_edit

sub replace_node{

 # $edit_node_line_number is the line number to replace with new edits.
 # $FROMWEB{'edited_text'} contains new edits to replace.
 # We need to insert the edits at $treepad_line[$edit_node_line_number],
 # and then empty all the lines after $edit_node_line_number until we come
 # to <end node>
 #_________________________________________
 # First Step: find <end node> 5P9i0s8y19Z.
 $done = 0;
 for $endnode_line_number ($edit_node_line_number..$#treepad_line){
   chomp($treepad_line[$endnode_line_number]); # remove carriage returns from end of line
   if ($treepad_line[$endnode_line_number] eq "<end node> 5P9i0s8y19Z" && $done == 0){

     # Found <end node>,
     # so now place edited data in position at $edit_node_line_number + 1.

     $treepad_line[$edit_node_line_number + 1] = "$FROMWEB{'node_title'}\n$FROMWEB{'child_number'}\n$FROMWEB{'edited_text'}\n<end node> 5P9i0s8y19Z";

     # Now empty the treepad lines up to the <end node> which we found at $endnode_line_number.

     $later_text = $endnode_line_number + 1;
     for $count ($edit_node_line_number + 2..$#treepad_line){
       $treepad_line[$count] = $treepad_line[$later_text];
       chomp $treepad_line[$count];
       $later_text++;
     }#end of for.

     $done = 1;
   } # end of if $treepad_line[$count] eq "<node>"
 } # end of for $count (1..$#treepad_line)

 # Now save @treepad_line to server as the new treepad file.
 $file = join("\n", @treepad_line);

 open (server_file, ">$datapath$treepad_file")
   or &dienice("I can't open the TreePad file: $datapath$treepad_file. <br>Error Code: $!");
	 print server_file "$file";
 close (server_file);

#-------------------------------------
# Print save confirmation to browser

print <<HTML;

<html>

<head><meta http-equiv="Content-Type" content="text/html; charset=big5">

<title>Changes Have Been Saved.</title>
</head>

<body background="$background_image" bgcolor="$background_color" vlink="$visited_link_color" text="$text_color" link="$link_color" alink="$active_link_color">

 <h1><font face="Arial" >Edit Saved</font></h1>
 <p><font face="Arial" >Your changes have been saved on the server.<br>
 <a href="$script_url?file=$FROMWEB{'file'}&link=$FROMWEB{'link'}"><b>Click here</b></a> to view your changes.
 </font></p>


</body>
</html>

HTML
exit;

} # end of sub replace_node

#---------------------------------------------------
# Search Definitions
#---------------------------------------------------
# 1. Find a Node
# 2. Search it for keyword
# 3. Display positive results (if any) using node number
#    with a link to start search from these results
#    for next results.


sub search{

  #____________________
  # Display Search Box

  print <<HTML;
    <HTML>
    <HEAD>
    <TITLE>Search iTreePad</TITLE>
	
    </HEAD>
    <body background="$background_image" bgcolor="$background_color" vlink="$visited_link_color" text="$text_color" link="$link_color" alink="$active_link_color">
    <center><IMG SRC="$image_directory/$title_image_location" alt="$image_description"><br>
    <font face="Arial" size="3"><b>Search Form</b>
    <form method="GET" action="$script_url">
      <p>Type your search here: <input type="text" name="search" size="20" value="$FROMWEB{'search'}">
      <input type="submit" value="Search"></p>
      <input type="hidden" name="file"	 value="$FROMWEB{'file'}">
      <input type="hidden" name="action" value="search">
    </form></font>
    </center>
    </body>
    </html>
HTML

if ($FROMWEB{'search'} eq ""){exit;}

  #___________
  # Open File

   $treepad_file = $FROMWEB{'file'};
   &open_file;

  #______________________________
  # Prepare Variables for search

  $FROMWEB{'search'} =~ tr/[A-Z]/[a-z]/; # changes case of search from upper to lower
  chomp($FROMWEB{'search'});		 # remove trailing returns
  @results = ();			 # empty results variable

  #_______________
  # Begin looking

  $joined_array = join("\n",@treepad_line);
  @search_array = split(/\<end node\> 5P9i0s8y19Z/, $joined_array);
  $start=$FROMWEB{'start'}+1;

  for $node ($start..$#search_array){
    $look = grep(/$FROMWEB{'search'}/i,$search_array[$node]);
    if($look){

      $link = $node + 1;
      $search_array[$node] =~ tr/[A-Z]/[a-z]/; # changes case of search from upper to lower
      $search_array[$node] =~ s/$FROMWEB{'search'}/<font color=\"$active_link_color\"><b>$FROMWEB{'search'}<\/b><\/font>/;

      print <<HTML;

      <center>
      <a href="$script_url?file=$FROMWEB{'file'}&search=$FROMWEB{'search'}&action=search&start=$node">Next Results</a> |
      <a href="$script_url?file=$FROMWEB{'file'}&link=$link">Display Node</a>
      </center>
      <br>
      <div align="center"><center>
      <table width="450">
        <tr>
	  <td>
	    <font face="arial" size="2">
HTML

      print $search_array[$node];

print <<HTML;
	    </font>
	  </td>
        </tr>
      </table>
      </center></div>
HTML

      exit;
    }
  }
 print "<br><br><center><b><font face=\"arial\">No Results</font></b></center>";

 $totalresults = scalar(@results);
exit;
 } # end of sub search

#---------------------------------------------------
# Upload files To Sever
#---------------------------------------------------
sub upload_HJT_file{

} # End of sub upload_HJT_file

#---------------------------------------------------
# dienice
# Displays error messages (beautify this later)
#---------------------------------------------------

sub dienice {
    my($errmsg) = @_;
    print <<HTML;

<div align="center"><center>
<table border="0" width="600"><tr><td width="100%">
   <hr width="600">
    <font color="black"> $errmsg</font><p>\n
     <b><hr width="600"><p align="center">
  </tr>
</table>
</center></div>
HTML
 exit;

} # End of sub dienice