window.addEvent("domready", function() {

  if ($('newsletter')) {
    // hide mailing list form
    var fm_height = $('form_mailinglist_wrapper').getSize().y+20;
    $('form_mailinglist_wrapper').setStyle('height', '0px');
    $('form_newsletter').addEvent('submit', function(e) {
      e = new Event(e).stop();
      var why = "";
      why += checkEmail(this.email.value);
      if (!this.name.value.length) {
        why += "Please enter your Name.";
      }
      if (why != "") {
         alert(why);
         return false;
      }

      this.set('send', {
        onSuccess: function(obj) {
          if (obj.length > 5) {
            alert(obj);
          }
          else {
            // show mailing list form
            $('form_newsletterthankyou').setHTML('<p>Thankyou, your email has been added to our e-Newsletter.</p>');
            $('form_mailinglist').name.value = $('form_newsletter').name.value;
            $('form_mailinglist_wrapper').tween('height', fm_height);
            $('form_newsletter_wrapper').tween('height', 0);
          }
        },
        onFailure: function(obj) {
          alert('Unknown error occurred');
        }
      });
      this.send();
    });

    $('form_mailinglist').addEvent('submit', function(e) {
      e = new Event(e).stop();
      var why = "";
      if (!this.name.value.length) { why += "Please enter your Name.\n"; }
      if (!this.address.value.length) { why += "Please enter your Street Address.\n"; }
      if (!this.city.value.length) { why += "Please enter your City.\n"; }
      if (!this.postcode.value.length) { why += "Please enter your Postcode.\n"; }
      if (!this.country.value.length) { why += "Please enter your Country.\n"; }
      if (!this.phone.value.length) { why += "Please enter your Phone.\n"; }
      if (why != "") {
         alert(why);
         return false;
      }
      this.set('send', {
        onSuccess: function(obj) {
          if (obj.length > 5) {
            alert(obj);
          }
          else {
            // show mailing list form
            $('form_mailinglist_wrapper').tween('height', 0);
            $('form_mailingthankyou').setHTML('<p>Thankyou, your address has been added to our mail-out list.</p>');
          }
        },
        onFailure: function(obj) {
          alert('Unknown error occurred');
        }
      });
      this.send();
    });

    $('newsletter').getElements('input[type=text]').each(function(el) {
      el.addEvent('focus', function() {
        if (this.value == this.defaultValue) {
          this.value = '';
        }
      });
      el.addEvent('blur', function() {
        if (this.value.length == 0) {
          this.value = this.defaultValue;
        }
      });
    });
  }
});