// 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() {
    $("#signup-icon, #signup-container a.signup-close").click(function() {
        $("#signup-container").toggle('slow', function() {
            $('#signup-icon').toggleClass('formshown', $(this).is(':visible'));
          });
        return 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"));

    $('#signupform').validate({
        errorPlacement: $.noop,
        debug: true,
        rules: {
          firstname: {
            required: true
          },
          surname: {
            required: true
          },
          email: {
            required: true,
            email: true
          }
        },
        submitHandler: function(form) {
          var reptext = '<div id="signup-thanks"><p>Thank you for signing up. We\'ll be in touch soon with news from Dirty Dancing - The Classic Story On Stage. <a class="signup-close" href="#">Close</a></p></div>';
          var errtext = '<div id="signup-thanks"><p>Sorry, but an error occurred trying to submit your registration. Please try again later. <a class="signup-close" href="#">Back to form</a></p></div>';
          var postdata = $(form).serialize();
          var theurl = supref + 'umspost.php';
          $.ajax({
              processData: false,
              type:        'post',
              url:         theurl,
              data:        postdata,
              success:     function(d, t) {
                $('#signup-container #sform').slideUp(100);
                $('#signup-container #sform').parent().append(reptext);
                $('#signup-container .signup-close').click( function() {
                    $('#signup-thanks').remove();
                    $('#signup-container #sform').slideDown(100);
                    $("#signup-container").toggle('slow', function() {
                        $('#signup-icon').toggleClass('formshown', $('#signup-container').is(':visible'));
                      });
                    clearForm($('#signupform'));
                    return false;
                  });
              },
              error:       function(d, t) {
                $('#signup-container #sform').slideUp(100);
                $('#signup-container #sform').parent().append(errtext);
                $('#signup-container .signup-close').click( function() {
                    $('#signup-thanks').remove();
                    $('#signup-container #sform').slideDown(100);
                    $("#signup-container").toggle('slow', function() {
                        $('#signup-icon').toggleClass('formshown', $('#signup-container').is(':visible'));
                      });
                    return false;
                  });
              }

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

      });

    return;


  });

