// Requires jQuery and the jQuery Validate plugin.
// http://www.jquery.com
// http://bassistance.de/jquery-plugins/jquery-plugin-validation/
//

function clearForm(ele) {
  $(ele).find(':input').each(function() {
      switch(this.type) {
      case 'password':
      case 'select-multiple':
      case 'select-one':
      case 'text':
      case 'textarea':
        $(this).val('');
        break;
      case 'checkbox':
      case 'radio':
        this.checked = false;
      }
    });
  
}

$(document).ready(function() {

    // Custom validator method - disallow certain strings
    jQuery.validator.addMethod("forbiddenValue", function(value, element, params) { 
        return this.optional(element) || ($(element).val() != params); 
      }, jQuery.validator.format("This field is required"));

    $('#sb-signupform').validate({
        errorPlacement: $.noop,
        debug: true,
        rules: {
          name: {
            required: true
          },
          email: {
            required: true,
            email: true
          }
        },
        submitHandler: function(form) {
          var reptext = '<form class="newsletter_form" id="signup-thanks"><span class="signup-thanks">Thank you for signing up. We\'ll be in touch soon with news from The Woman In Black. <a class="signup-close" href="#">Close</a></span></form>';
          var errtext = '<form class="newsletter_form" id="signup-thanks"><span class="signup-thanks">Sorry, but an error occurred trying to submit your registration. Please try again later. <a class="signup-close" href="#">Back to form</a></span></form>';
          var postdata = $(form).serialize();
          var theurl = supref + 'umspost.php';
          $.ajax({
              processData: false,
              type:        'post',
              url:         theurl,
              data:        postdata,
              success:     function(d, t) {
                $('#sb-signupform').slideUp(100);
                $('#sb-signupform').parent().append(reptext);
                $('.signup-close').click( function() {
                    $('#signup-thanks').remove();
                    $('#sb-signupform').slideDown(100);
                    clearForm($('#sb-signupform'));
                    return false;
                  });
              },
              error:       function(d, t) {
                $('#sb-signupform').slideUp(100);
                $('#sb-signupform').parent().append(errtext);
                $('.signup-close').click( function() {
                    $('#signup-thanks').remove();
                    $('#sb-signupform').slideDown(100);
                    return false;
                  });
              }

            }); // $.ajax
          return false;
        }

      });

    return;


  });

