document.observe("dom:loaded", function() {
   // *************************************************************************
   // Chatbox auto refresh
   // *************************************************************************

   if ($("xchatbox_posts")) {
      function spyonChatBox() {
         setTimeout(function() {
            $("cmessage").observe("keyup", function(ev) {
               if (ev.element().getValue().length > 0 && !chatUpdater.stopped) {
                  chatUpdater.stopped = true;
                  chatUpdater.stop();
               } else if (ev.element().getValue().length == 0 && chatUpdater.stopped) {
                  chatUpdater.stopped = false;
                  chatUpdater.start();
               }
            });
            $("chat_submit").observe("click", function(ev) {
               chatUpdater.stopped = false;
               setTimeout(function() {
                  spyonChatBox();
                  chatUpdater.start();
               }, 2500);
            });
         }, 1500);
      }
      var params = {chat_submit: "Submit", cmessage: ""};
      if ($("chatbox_ajax")) {
         params.chatbox_ajax = $("chatbox_ajax").getValue();
      }
      var chatUpdater = new Ajax.PeriodicalUpdater(
         "chatbox_posts",
         "http://localhost/plugins.e107.org/e107_plugins/chatbox_menu/chatbox_menu.php",
         {
            parameters: params,
            frequency:  1,
            decay:      2,
            onSuccess: function() {
               console.log(new Date()+"jshelper onSuccess");
               spyonChatBox();
            }
         }
      );
   }

   // *************************************************************************
   // JSCaptcha
   // *************************************************************************
   // See if we have a form we need to observe
   var forms = $w("chatbox searchform")
   forms.each(function(id) {
      var url = "http://localhost/plugins.e107.org/e107_plugins/jshelpers/ajax_jscaptcha.php";
      var f = $(id);
      if (f) {
         var did = id+"_jscaptcha";
         f.insert({top: new Element("div", {id:did})});
         new Ajax.Updater(did, url, {
            method:"get",
            parameters: "get."+id
         });

         f.observe("submit", function(ev) {
            ev.stop();

            new Ajax.Request(url, {
               method:     "get",
               parameters: "check."+$F(id+'_code')+"."+$F(id+'_verify'),
               onSuccess: function(transport) {
                  if (transport.responseText == "true") {
                     //TODO - duplicate submit button as some forms check for it being submitted and it isn't if you don't actually click it
                     //TODO - what if more none?
                     //TODO - what if more than 1?
                     var subbutton = f.select("input[type=submit]")[0];
                     f.insert({top: new Element("input", {type:"hidden",name:subbutton.name,value:subbutton.value})});
                     f.submit();
                  } else {
                     new Ajax.Updater(did, url, {
                        method:"get",
                        parameters: "get."+id+"."+transport.responseText
                     });
                  }
               },
            });
         });
      }
   });

});
