	    
		var isNumeric = function(strString)
		   //  check for valid numeric strings	
		   {
		   var strValidChars = "0123456789.-";
		   var strChar;
		   var blnResult = true;
		
		   if (strString.length == 0) return false;
		
		   //  test strString consists of valid characters listed above
		   for (i = 0; i < strString.length && blnResult == true; i++)
		      {
		      strChar = strString.charAt(i);
		      if (strValidChars.indexOf(strChar) == -1)
		         {
		         blnResult = false;
		         }
		      }
		   return blnResult;
		}	    
	    
	    var onAddCategorySuccess = function(dialogId, pluginUrl, postId){
	         return function(resp){
	             document.getElementById('restMenuEditableDiv').innerHTML = resp.responseText;
	             YAHOO.util.Dom.setStyle(dialogId+'_c','visibility', 'hidden'); 
	             alert('Category created');
	             addRestMenuBehaviour(pluginUrl, postId);
	         };
	    };
	    
	    var onUpdateCategorySuccess = function(dialogId, pluginUrl, postId){
	         return function(resp){
	             document.getElementById('restMenuEditableDiv').innerHTML = resp.responseText;
	             YAHOO.util.Dom.setStyle(dialogId+'_c','visibility', 'hidden'); 	         
	             alert('Category changed');
	             addRestMenuBehaviour(pluginUrl, postId);	             
	         };
	    };	
	    
	    var onUpdateCategoryFailure = function(){
	         return function(resp){
	             alert(resp.status);
	         };
	    }; 	
	    
	    var onUpdateItemSuccess = function(dialogId, pluginUrl, postId){
	         return function(resp){
	             document.getElementById('restMenuEditableDiv').innerHTML = resp.responseText;
	             YAHOO.util.Dom.setStyle(dialogId+'_c','visibility', 'hidden'); 	         
	             alert('Item changed');
	             addRestMenuBehaviour(pluginUrl, postId);	             
	         };
	    };	
	    
	    var onUpdateItemFailure = function(){
	         return function(resp){
	             alert(resp.status);
	         };
	    }; 
	    
	    var onRemoveCategorySuccess = function(pluginUrl, postId){
	         return function(resp){
	             document.getElementById('restMenuEditableDiv').innerHTML = resp.responseText;
	             alert('Category removed');
	             addRestMenuBehaviour(pluginUrl, postId);	             
	         };
	    };	    
	    
	    var onRemoveCategoryFailure = function(){
	         return function(resp){
	             alert(resp.status);
	         };
	    };   
	    
	    var onRemoveSelectedItemsSuccess = function(pluginUrl, postId){
	         return function(resp){
	            document.getElementById('restMenuEditableDiv').innerHTML = resp.responseText;
	            alert('Selected items removed');
	            addRestMenuBehaviour(pluginUrl, postId);	            
	         };
	    };	    
	    
	    var onRemoveSelectedItemsFailure = function(){
	         return function(resp){
	             alert(resp.status);
	         };
	    }; 	    	    
	    
	    var onAddItemSuccess = function(dialogId, pluginUrl, postId){
	         return function(resp){
	             document.getElementById('restMenuEditableDiv').innerHTML = resp.responseText;
	             YAHOO.util.Dom.setStyle(dialogId+'_c','visibility', 'hidden'); 		         
	             alert('Item created');
                 addRestMenuBehaviour(pluginUrl, postId);		             
              };
	    };	    
	    
	    var onAddCategoryFailure = function(){
	         return function(resp){
	             alert(resp.status);
	         };
	    };   
	    
	    var onAddItemFailure = function(){
	         return function(resp){
	             alert(resp.status);
	         };
	    };	    
    
	   var onAddCategorySave = function(){
	         return function(e){
		         this.submit();
	         };
	    }; 
	    
	   var onUpdateCategorySave = function(){
	         return function(e){
		         this.submit();
	         };
	    };	   
	    
	   var onUpdateItemSave = function(){
	         return function(e){
		         this.submit();
	         };
	    };	     
	    
	   var onAddItemSave = function(){
	         return function(e){
	             if(isNumeric(document.getElementById('price').value)){
		            this.submit();
		         }
		         else{
		            alert('Price must be a numeric value');
		         }
	         };
	    }; 	    
    
	    var onCancel = function(dialogId){
	       return function(e){
	          this.hide();
	          YAHOO.util.Dom.setStyle(dialogId+'_c','visibility', 'hidden');  
	       }
	    };
	    
	      function tagFunc(tag){
	         try{
		         return function(){
		             var attrs, children;
		             if(arguments.length > 0){
		                 if(arguments[0].nodeName || typeof arguments[0] == "string"){
		                     children = arguments;
		                 }
		                 else{
		                     attrs = arguments[0];
		                     children = Array.prototype.slice.call(arguments,1);
		                 };
		             }
		             return create(tag, attrs, children);
		         };
	         }
	         catch(err){
	             alert(err + " Dom tagFunc()");
	         }
	      }
	      
	      function create(tag, attrs, children){
	         try{
	             attrs = attrs || {};
	             children = children || {};
	             var el = document.createElement(tag);
	             var attrName = null;
	             for(var attr in attrs){
	                attrName = attr;
	                if(attrName.substr(0,1)=="_"){
	                   attrName = attrName.substr(1);
	                }
	                el.setAttribute(attrName, attrs[attr].toString());
	             }
	             for(var i=0; i< children.length; i++){
	                if(typeof children[i]=="string"){
	                    children[i] = document.createTextNode(children[i]);
	                }
	                el.appendChild(children[i]);
	             }
	             return el;
	         }
	         catch(err){
	             alert(err + " DOM create()");
	         }
	      }
	          
		/* Dom self-executing support function */
		(function(){
		   var els = ("frm|img|br|span|a|h1|h2|h3|h4|h4|h6|hr|ul|li|form|label|p|fieldset|legend|option|textarea|select|form|div|input|fieldset").split("|");
		   var el, i=0;
		   while(el = els[i++]){
		      window['$' + el]= tagFunc(el);
		   }
		})();
    
        function dialog(dialogId, frmContent, buttons, submitUrl, submitSuccess, submitFailure, isModal){
           try{
           
                if(typeof isModal == "undefined"){
                   isModal=false;
                }
                
                var dialog = new YAHOO.widget.Dialog(dialogId, {width:"400px",
                												fixedcenter:true,
                												postmethod: "async",
                												close:false,
                												effect:{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.15},
                												modal:isModal}); 
                
                dialog.setBody("");

                dialog.cfg.queueProperty("buttons", buttons); 
                dialog.callback.success = submitSuccess;
                dialog.callback.failure = submitFailure;
                dialog.render(document.body);   
                dialog.show();    
           
                            
                var frm = dialog.form;
                frm.setAttribute("method", "POST");
                frm.setAttribute("id", "frm_dialog");
                frm.setAttribute("action", submitUrl);
                frm.appendChild(frmContent);

                YAHOO.util.Dom.setStyle(dialogId+'_c','background', '#ffffff');                
                YAHOO.util.Dom.setStyle(dialogId+'_c','padding', '10px');                                
                YAHOO.util.Dom.setStyle(dialogId+'_c','border', '1px solid #474747');                                
                YAHOO.util.Dom.setStyle(dialogId+'_c','visibility', 'visible');                
                            
           }
           catch(err){
              alert(err + " restmenu.php");
           }       
       }    
       
       
      function getCategoryFormContent(heading){
          var frmContent = $div({id:'popup'},
          						 $h1({}, heading),
          						 $ul({},
          						    $li({},
          						        $label({_for:'categoryName'}, 'Category name'),
          						        $input({id:'categoryName', type:'text', name:'category_name'})
          						    ),
          						    $li({},
          						        $label({_for:'description'}, 'Description'),
          						        $textarea({id:'description', name:'description'})
          						    )          						    
          						 )
          					   );
          return frmContent;
      }
      
      function getItemFormContent(heading){
          var frmContent = $div({id:'popup'},
          						 $h1({}, heading),
          						 $ul({},
          						    $li({},
          						        $label({_for:'itemName'}, 'Item name'),
          						        $input({id:'itemName', type:'text', name:'item_name'})
          						    ),
          						    $li({},
          						        $label({_for:'price'}, 'Price'),
          						        $input({id:'price', type:'text', name:'price'})
          						    ),          						    
          						    $li({},
          						        $label({_for:'description'}, 'Description'),
          						        $textarea({id:'description', name:'description'})
          						    )          						    
          						 )
          					   );
          return frmContent;
      }      
      
      function addCategoryHandler(pluginUrl, postId){
          return (function(e){
          	  var buttons = [ { text:"Save", handler:onAddCategorySave(), isDefault:true },
					                   { text:"Cancel", handler:onCancel("addCategoryDialog") } ]; 
              dialog("addCategoryDialog", getCategoryFormContent('Add category'), buttons, pluginUrl + "/xrestmenu/callbacks.php?run=addCategory&postId=" + postId, onAddCategorySuccess("addCategoryDialog", pluginUrl, postId), onAddCategoryFailure());
          });
       }

	  var onEditCategorySuccess = function(dialogId){
	         return function(resp){
	             var categoryObj = 	YAHOO.lang.JSON.parse(resp.responseText);
	             document.getElementById('categoryName').value = categoryObj[0]['category_name'];
	             document.getElementById('description').value = categoryObj[0]['description'];                       
	         };
	   };	
	   
	  var onEditItemSuccess = function(dialogId){
	         return function(resp){
	             var itemObj = 	YAHOO.lang.JSON.parse(resp.responseText);
	             document.getElementById('itemName').value = itemObj[0]['item_name'];
	             document.getElementById('price').value = itemObj[0]['price'];
	             document.getElementById('description').value = itemObj[0]['description'];                       
	         };
	   };	   
	   
	    var onEditCategoryFailure = function(){
	         return function(resp){
	             alert(resp.status);
	         };
	    }; 	  
	    
	    var onEditItemFailure = function(){
	         return function(resp){
	             alert(resp.status);
	         };
	    }; 
             
      function editCategoryHandler(pluginUrl, postId){
          return (function(e){
              YAHOO.util.Event.stopEvent(e);
              var btn = YAHOO.util.Event.getTarget(e);
              var categoryId = btn.getAttribute('name');
              // Get the category info and then show the dialog
         	  var buttons = [ { text:"Save", handler:onUpdateCategorySave(), isDefault:true },
					                   { text:"Cancel", handler:onCancel("editCategoryDialog") } ]; 
              dialog("editCategoryDialog", getCategoryFormContent('Edit category'), buttons, pluginUrl + "/xrestmenu/callbacks.php?run=editCategory&categoryId=" + categoryId + '&postId='+postId, onUpdateCategorySuccess("editCategoryDialog", pluginUrl, postId), onUpdateCategoryFailure());
              
              send(onEditCategorySuccess(), onEditItemFailure(), pluginUrl + "/xrestmenu/callbacks.php?run=getCategoryInfo", "categoryId="+categoryId, "POST"); 
         });
       }    
       
      function editItemHandler(pluginUrl, postId){
          return (function(e){
              YAHOO.util.Event.stopEvent(e);
              var btn = YAHOO.util.Event.getTarget(e);
              var itemId = btn.getAttribute('name');
              var itemName = btn.getAttribute('tag');
              // Get the category info and then show the dialog
         	  var buttons = [ { text:"Save", handler:onUpdateItemSave(), isDefault:true },
					                   { text:"Cancel", handler:onCancel("editItemDialog") } ]; 
              dialog("editItemDialog", getItemFormContent('Edit item ' + itemName), buttons, pluginUrl + "/xrestmenu/callbacks.php?run=editItem&itemId=" + itemId + '&postId='+postId, onUpdateItemSuccess("editItemDialog", pluginUrl, postId), onUpdateItemFailure());
              send(onEditItemSuccess(), onEditItemFailure(), pluginUrl + "/xrestmenu/callbacks.php?run=getItemInfo", "itemId="+itemId, "POST"); 
         });
       }             
       
	 function send(successcallback, failurecallback, uri, query, method){
	
	   try{
	        var callback =
		    {
		      success:successcallback,
		      failure:failurecallback
		    };
		
		    // Do the ajax call.
		    if(method.toUpperCase()=="GET"){
		        uri = uri+"?"+query;
		    }
		    YAHOO.util.Connect.asyncRequest(method, uri, callback, query);
	    }
	    catch(err){
	        alert(err + ': send');
	    } 
	          
      }
       
      function removeCategoryHandler(pluginUrl, postId){
          return (function(e){
              var btn = YAHOO.util.Event.getTarget(e);
              var categoryId = btn.getAttribute('name');
              var categoryName = btn.getAttribute('tag');
              if(confirm("Remove category '" + categoryName + "'?")){
                  send(onRemoveCategorySuccess(pluginUrl, postId), onRemoveCategoryFailure(), pluginUrl + "/xrestmenu/callbacks.php?run=removeCategory&postId=" + postId, "categoryId="+categoryId, "POST"); 
              }
          });
       }       
       
      function addItemHandler(pluginUrl, postId){
          return (function(e){
              var btn = YAHOO.util.Event.getTarget(e);
              var categoryId = btn.getAttribute('name');
              var categoryName = btn.getAttribute('tag');
          	  var buttons = [ { text:"Save", handler:onAddItemSave(), isDefault:true },
					                   { text:"Cancel", handler:onCancel("addItemDialog") } ]; 
              dialog("addItemDialog", getItemFormContent('Add item to ' + categoryName), buttons, pluginUrl + "/xrestmenu/callbacks.php?run=addItem&postId=" + postId + "&categoryId=" + categoryId, onAddItemSuccess("addItemDialog", pluginUrl, postId), onAddItemFailure());

           });
       }    
       
      function removeSelectedItemsHandler(pluginUrl, postId){
          return (function(e){
              var btn = YAHOO.util.Event.getTarget(e);
              var categoryId = btn.getAttribute('name');
              var categoryName = btn.getAttribute('tag');
              var categoryChecks = YAHOO.util.Dom.getElementsByClassName('chk' + categoryId);
              var max = categoryChecks.length;
              var i=0;
              var itemIds = new Array();
              var j=0;
              for(i=0;i<max;i++){
                  if(categoryChecks[i].checked){
                      itemIds[j]=categoryChecks[i].getAttribute("id");
                      j++;
                  }
              }
              var itemIdsStr = YAHOO.lang.JSON.stringify(itemIds);
              if(confirm("Remove selected '" + categoryName + "' items?")){
                  send(onRemoveSelectedItemsSuccess(pluginUrl, postId), onRemoveSelectedItemsFailure(), pluginUrl + "/xrestmenu/callbacks.php?run=removeSelectedItems&postId=" + postId, "categoryId="+categoryId + "&itemIds=" + itemIdsStr, "POST"); 
              }
          });
       }     
       
       function addRestMenuBehaviour(pluginUrl, postId){
           //alert(postId);
           // Add behaviours
           // Add category
           YAHOO.util.Event.on('addCategory', 'click', addCategoryHandler(pluginUrl, postId));
           // Remove category
           YAHOO.util.Event.on(YAHOO.util.Dom.getElementsByClassName('removeCategory'), 'click', removeCategoryHandler(pluginUrl, postId));
           // Add item
           YAHOO.util.Event.on(YAHOO.util.Dom.getElementsByClassName('addItem'), 'click', addItemHandler(pluginUrl, postId));
           // Remove selected items
           YAHOO.util.Event.on(YAHOO.util.Dom.getElementsByClassName('removeSelectedItems'),'click', removeSelectedItemsHandler(pluginUrl, postId));
           // Category links
           YAHOO.util.Event.on(YAHOO.util.Dom.getElementsByClassName('categoryNameLink'), 'click', editCategoryHandler(pluginUrl, postId));
           // Item links
           YAHOO.util.Event.on(YAHOO.util.Dom.getElementsByClassName('itemNameLink'), 'click', editItemHandler(pluginUrl, postId));
       }      
      
       function initRestMenu(pluginUrl, postId){

           return function(e){
              var restMenus = document.getElementsByClassName('restMenu');
              if(document.getElementById('restMenuEditableDiv')){ // We're editing a post
                  // Move the editable restaurant menu to after 'postdivrich' rich editor element
                  YAHOO.util.Dom.setStyle('restMenuEditableDiv', 'display', 'block');
                  YAHOO.util.Dom.insertAfter('restMenuEditableDiv', 'postdivrich');
                  addRestMenuBehaviour(pluginUrl, postId);
              }
	          else if(restMenus.length>0 && postId!=-1){ // Viewing a post
	           	  var restMenu = restMenus[0];
	           	  YAHOO.util.Dom.insertAfter(restMenu, 'post-' + postId);
	          }              
           }

       }
