// 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-container').bind({
        toggle: function() {
          $(this).css('padding', '60px 0px 0px 80px').toggle('slow');
        },
        showSuccess: function() {
          $('form', this).slideUp('slow');
          $(this).append('<div id="signup-thanks" style="padding: 30px; border-radius: 10px; background-color: #000; z-index: 1000;"><p>Thank you for signing up. We\'ll be in touch soon with news from Ghost Stories. <a class="signup-close" href="#">Close</a></p></div>');
        },
        showError: function() {
          $('form', this).slideUp('slow');
          $(this).append('<div id="signup-thanks" style="padding: 30px; border-radius: 10px; background-color: #000;z-index: 1000;"><p>Sorry, but an error occurred trying to submit your registration. Please try again later. <a class="signup-close" href="#">Close</a></p></div>');
        },
        showForm: function() {
          $('#signup-thanks', this).slideUp().remove();
          $('form', this).show();
        }

      });

    $("#signup-button").live('click', function() {
        $("#signup-container").css('padding', '60px 0px 0px 80px').toggle('slow', function() {
            $('#signup-icon').toggleClass('formshown', $(this).is(':visible'));
          });
        return false;
      });

    $('#signup-container a.signup-close').live('click', function() {
        $('#signup-container').trigger('toggle').trigger('showForm');
      });
});

$(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"));

    $('#signup-container form').validate({
        errorPlacement: $.noop,
        debug: true,
        rules: {
          name: {
            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';
          var theurl = $(form).attr('action');
          $.ajax({
              processData: false,
              type:        'post',
              url:         theurl,
              data:        postdata,
              success:     function(d, t) {
                $('#signup-container').trigger('showSuccess');
                //$('#signup-container form div').slideUp(100);
                //$('#signup-container form div').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($('#signup-container form'));
                    //return false;
                  //});
              },
              error:       function(d, t) {
                $('#signup-container').trigger('showError');
                //$('#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;


  });

