<?php
$izero = 0;
$athere = '<tr style="background-color: yellow;"><th>Book Number</th><th>Title</th><th>Subtitle</th><th>Author</th><th>Published By</th><th>Published Date</th><th>Total Pages</th><th>Total Chapters</th><th>Comments</th></tr>';
if (isset($_POST['clear'])) {
  if (file_exists(dirname(__FILE__) . "/" . $_SERVER['REMOTE_ADDR'] . "_books.txt")) {
    unlink(dirname(__FILE__) . "/" . $_SERVER['REMOTE_ADDR'] . "_books.txt");
  }
} else if (isset($_POST['bbih'])) {
  $athere = str_replace(" myObject.highlight(", " highlight(", str_replace("\\", "", $_POST['bbih']));
  file_put_contents(dirname(__FILE__) . "/" . $_SERVER['REMOTE_ADDR'] . "_books.txt", $_POST['bbih']);
  $izero = 0;
  while (strpos($athere, ">" . ($izero + 1) . "<") !== false) {
    $izero = $izero + 1;
  }
} else if (file_exists(dirname(__FILE__) . "/" . $_SERVER['REMOTE_ADDR'] . "_books.txt")) {
  $athere = str_replace(" myObject.highlight(", " highlight(", str_replace("\\", "", file_get_contents(dirname(__FILE__) . "/" . $_SERVER['REMOTE_ADDR'] . "_books.txt")));
  $izero = 0;
  while (strpos($athere, ">" . ($izero + 1) . "<") !== false) {
    $izero = $izero + 1;
  }
}
?>
<html>
<head>
<title>Book Structure Primer Tutorial from RJM Programming ... 2014</title>
<script>
var cur_book = <?php echo $izero; ?>;
var book_array = []; 

function highlight(whichone) {
  var thisone = document.getElementById('tr' + whichone);
  if (thisone.style.backgroundColor == 'pink') {
    thisone.style.backgroundColor = 'white';
  } else {
    thisone.style.backgroundColor = 'pink';
  }
}

function book_ask() {
  cur_book = cur_book + 1;
  var bookb = document.getElementById('book_body');
  var obbih = document.getElementById('bbih');
  var title = prompt("Please enter your Book " + cur_book +  "'s Title (use Cancel to quit)", ""); 

  if (title != null && title != "") {
    var subtitle = prompt("Please enter an optional Subtitle for " + title + " (" + cur_book +  ")", ""); 
    var author = prompt("Please enter the Author for " + title + " (" + cur_book +  ")", ""); 
    var published = prompt("Please enter the Publisher for " + title + " (" + cur_book +  ")", ""); 
    var publishing_date = prompt("Please enter the Publishing Date for " + title + " (" + cur_book +  ")", "31/12/1960"); 
    var total_pages = prompt("Please enter the Total Pages in " + title + " (" + cur_book +  ")", "0"); 
    var total_chapters = prompt("Please enter the Total Chapters in " + title + " (" + cur_book +  ")", "0"); 
    var comments = prompt("Please enter Comments for " + title + " (" + cur_book +  ")", ""); 

    book_array.push({ 
     title: title, 
     subtitle: subtitle, 
     total_pages: total_pages, 
     total_chapters: total_chapters, 
     author: author, 
     publishing_date: publishing_date, 
     published: published,
     comments: comments
     });

    bookb.innerHTML = bookb.innerHTML + "<tr id='tr" + cur_book + "'><td><a href='#' onclick=' highlight(" + cur_book + ");' >" + cur_book + "</a></td><td>" + title + "</td><td>" + subtitle + "</td><td>" + author + "</td><td>" + published + "</td><td>" + publishing_date + "</td><td>" + total_pages + "</td><td>" + total_chapters + "</td><td>" + comments + "</td></tr>";
    obbih.value = bookb.innerHTML;
    title = "";
    book_ask();
  } else {
    var formx = document.getElementById('xform');
    formx.style.display = 'block';
  }
}

</script>
</head>
<body id="xbod" onload=" book_ask(); ">
<div align="center" style="width: 100%;">
<h1>Your Book List goes below ...</h1>
<table id="book_table" border=1 cellpadding=5>
<tbody id="book_body">
<?php echo $athere; ?>
</tbody>
</table>
<form id="xform" action="./book_structure.php" style="display: none;" method="POST">
<?php
  echo '<input id="bbih" name="bbih" type="hidden" value=""></input>' . "\n";
?>
<input type="submit" id="submit" name="submit" value="Save"></input><input type="submit" id="clear" name="clear" value="Clear"></input>

</form>
</div>
</body>
</html>
