
function getElementsByClassName(clsName, source){
   if (source == undefined) {
      source = document;
   }

   var retVal = new Array();
   var elements = document.getElementsByTagName("*");

   for (var i=0;i < elements.length;i++) {
      if (elements[i].className.indexOf(" ") >= 0) {
      
         var classes = elements[i].className.split(" ");
         for (var j = 0;j < classes.length;j++) {
            if (classes[j] == clsName) { retVal.push(elements[i]); }
         }
      }
      else if(elements[i].className == clsName) {
         retVal.push(elements[i]);
      }
   }
   return retVal;
};

// Default to a WordPress comment form if we find it.
var ActionComments = {

   confirmPrompt : "Do you want to subscribe to the newsletter?",

   enable : true, // connect a function to this for conditional submissions

   formsSubmitted : 0,
   mainForm : null,

   box : null,
   formList : null,

   emailSource : null,
   nameSource : null,

   ready : false,

   init : function(nameSource, emailSource) {

      ActionComments.box = document.getElementById("actioncomments-form");

      ActionComments.nameSource = nameSource;
      ActionComments.emailSource = emailSource;

      if (!document.getElementById(nameSource) || !document.getElementById(emailSource)) { return false; }

      ActionComments.formList = ActionComments.box.getElementsByTagName("form");

      // Hide the box...
      ActionComments.box.style.display = "none";

      // Tell all forms to submit inside that invisible iframe...
      for (i=0;i<ActionComments.formList.length;i++) {

         // Add a frame to the document to "catch" the form submission
         var catcherName = "catcher[" + i + "]";
         document.write('<iframe id="' + catcherName + '" name="' + catcherName + '" width="1" height="1" style="display:none" src="about:blank" onload="ActionComments.formDone()"></iframe>');

         ActionComments.formList[i].target = catcherName;
      }

      ActionComments.mainForm = document.getElementById(ActionComments.nameSource).form;

      ActionComments.mainForm.onsubmit = function() {
         // First, find out if we should be doing this.
         ActionComments.ready = true;

         if (typeof ActionComments.enable == "function" && !ActionComments.enable()) { return true; }
         else if (!ActionComments.enable) { return true; }

         ActionComments.formsSubmitted = 0;

         // Tell all catcher iframes to hit formDone when finished loading
         for (i=0;i<ActionComments.formList.length;i++) {
            var catcherName = "catcher[" + i + "]";
            document.getElementById(catcherName).onload = ActionComments.formDone;
         }

         // Copy just the first name over (not the last name)         
         var firstname = document.getElementById(ActionComments.nameSource).value.replace(/ .*$/, '');

         // Copy the email address over.
         var email = document.getElementById(ActionComments.emailSource).value;

         // Fill in all "firstname" fields
         var nameFields = getElementsByClassName("firstname", ActionComments.box);
         for (i=0;i<nameFields.length;i++) {
            nameFields[i].value = firstname;
         }

         // Fill in all "email" fields
         var emailFields = getElementsByClassName("email", ActionComments.box);
         for (i=0;i<emailFields.length;i++) {
            emailFields[i].value = email;
         }

         // Submit all the forms
         for (i=0;i<ActionComments.formList.length;i++) {
            ActionComments.submitForm(ActionComments.formList[i]);
         }

         return false;
      };

      // Later: submit all forms with class "autosubmit"
   },

   submitForm : function(theForm) {
      if (theForm.submit && theForm.submit.click) { theForm.submit.click(); }
      else if (theForm.submit) { theForm.submit(); }
   },

   formDone : function() {
      if (!ActionComments.ready) { return false; }
      ActionComments.formsSubmitted++;

      if (ActionComments.formsSubmitted >= ActionComments.formList.length) {
         ActionComments.mainForm.onsubmit = function() { return true; }

         ActionComments.submitForm(ActionComments.mainForm);
      }
   }

};

// Cookie functions
var Cookie = {

   set : function(name, value, days) {
      var date = new Date();
      if (days != undefined && typeof days == "object") {
         date = days;
      }
      else {

      // Default to a 1 year cookie
      if (days == undefined) { days = 365; }

      // Format date string
      var date = new Date();
      date.setTime(date.getTime() + (days * 86400000));

      }

      // Set cookie name, value, and expiration date
      //window.status = name + "=" + value + "; expires=" + date.toGMTString() + "; path=/";
      document.cookie = name + "=" + value + "; expires=" + date.toGMTString() + "; path=/";
   },

   get : function(name) {
      // Find the cookie's value in the document cookie string
      var results = document.cookie.match(
         new RegExp("(?:^|; )" + name + "=" + "(.*?)(?:$|;)")
      );

      // Return the value if a match was found, undefined otherwise
      if (results && results.length > 1) return results[1];
      return undefined;
   },

   clear : function(name) {
      // Erase a cookie
      Cookie.set(name, "", -1);
   }

};