var Autosubmit = {

   init : function() {
      var theForm;

      // We found a form with an e-mail address
      if (theForm = Autosubmit.findEmailForm()) { }

      // Otherwise, we didn't find the form, so give up.
      else { return; }

      // Only submit if the form is NOT an Aweber form.
      //if (!Autosubmit.isAweber(theForm)) {
         Autosubmit.sendForm(theForm);
      //}
   },

   sendForm : function(form) {
      document.write('<iframe name="catcher" width="1" height="1" style="display:none"></iframe>');
      form.target = "catcher";
      form.style.display = "none";
      form.submit();
   },

   findEmailForm : function() {
      for (i=0;i<document.forms.length;i++) {
         var form = document.forms[i];
         if (Autosubmit.hasEmail(form)) { return form; }
      }
      return null;
   },

   hasEmail : function(form) {
      // Step through each form field in the form
      for (i=0;i<form.elements.length;i++) {
         var element = form.elements[i];

         // If this form field is a text box that contains the @ symbol,
         // assume this form has an e-mail address typed in, and stop looking.
         if (element.type == "text" && element.value.match("@")) {
            return element;
         }
      }

      // Otherwise, we never found an @ symbol in the whole form.
      return false;
   },

   isAweber : function(form) {
      return form.action.match("aweber.com");
   }

};

var Cookie = {

   set : function(name, value, days) {
      // 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
      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);
   }
};

window.onunload = function() {
   var theForm;
   if (theForm = Autosubmit.findEmailForm()) {
      var formField = Autosubmit.hasEmail(theForm);
      Cookie.set("email", formField.value);
      Cookie.set("name", document.getElementById('CustomFields_2_7').value);
   }
}
