<?php
/*
Plugin Name: Speedy Cat
Version: 1.1
Plugin URI: http://www.slightlyremarkable.com/2005/06/speedy-cat
Author: Jonathan Fenocchi
Author URI: http://www.slightlyremarkable.com/
Description: Speedy Cat adds a new section to the "Create a new post" page that allows you to create a new category to post in without having to go to another page to create it. It automatically adds a new checkbox/category name pair to the list of categories.
*/

function addSpeedyCat(){
 if(
preg_match('/(post.php|page-new.php)/i'$_SERVER['SCRIPT_NAME'])) {
 if(
function_exists('wp_admin_tiger_css')){
 echo 
'<div class="wrap" id="speedyCatDiv"><h3>Add Category</h3>
  <form action="categories.php" method="post">
  <p>Name:<br />
   <input type="text" name="cat_name" value="" /></p>
  <p class="submit">
   <input type="hidden" name="action" value="addcat" />
   <input type="submit" value="Add »"></p>
  </form></div>
 '
;
} else {
 echo 
'<div id="speedyCatDiv">
  <form action="categories.php" method="post"><fieldset>
  <legend>Add Category</legend>
  <p>Name:<br />
   <input type="text" name="cat_name" value="" /></p>
  <p class="submit">
   <input type="hidden" name="action" value="addcat" />
   <input type="submit" value="Add »"></p>
  </fieldset></form></div>
 '
;
}
 echo 
' <script type="text/javascript"><!--
  var speedyCatDiv = document.getElementById("speedyCatDiv");
  var speedyCatForm = speedyCatDiv.getElementsByTagName("form")[0];
  var speedyCatReq = null;'
;
 if(
function_exists('wp_admin_tiger_css')){
   echo 
'
    document.getElementById("postpassworddiv").appendChild(speedyCatDiv);'
;
 } else {
   echo 
'
    document.getElementById("poststuff").appendChild(speedyCatDiv);'
;
 }
 echo 
'
   speedyCatForm.onsubmit = function(){
   if(window.XMLHttpRequest){
     speedyCatReq = new XMLHttpRequest();
   } else if(window.ActiveXObject){
     speedyCatReq = new ActiveXObject("Microsoft.XMLHTTP");
   } else {
    return false;
  }
    speedyCatReq.open("POST", "categories.php", false);
    speedyCatReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    speedyCatReq.send("cat_name="+this.elements["cat_name"].value+"&action=addcat");
   updateSpeedyCats();
   return false;
  }
  function updateSpeedyCats(){
    var lastCatNum = 0, inputObj = document.getElementById("categorydiv").getElementsByTagName("input"), inputLen = inputObj.length;
    for(var i=0; i<inputLen; i++){
      if(parseInt(inputObj[i].value) > lastCatNum){
         lastCatNum = inputObj[i].value;
      }
    }
   lastCatNum++;
   var catDiv = document.getElementById("categorydiv").getElementsByTagName("div")[0];
   var newCat = document.createElement("label");
       newCat.setAttribute("class","selectit");
   var catInput = document.createElement("input");
       catInput.setAttribute("value", lastCatNum);
       catInput.setAttribute("type", "checkbox");
       catInput.setAttribute("checked", "checked");
       catInput.setAttribute("name", "post_category[]");
       catInput.setAttribute("id", "category-"+lastCatNum);
  var spanNest = document.createElement("span");
       spanNest.setAttribute("class", "cat-nest");
  var catName = document.createTextNode(speedyCatForm.elements["cat_name"].value);
  newCat.appendChild(catInput);
  newCat.appendChild(catName);
  catDiv.appendChild(spanNest);
  catDiv.appendChild(newCat);
  speedyCatForm.elements["cat_name"].value = "";
  }
 //--></script>
 '
;
 }
}

add_action('admin_footer''addSpeedyCat');
?>