$(function() {
  if ($('.messages.status').length > 0) {
    $('.messages.status').hide().slideDown("slow");
  }
  
  //$('input#join-start-search').inputLabel("School or group name");
  $('input#login-email').inputLabel("Email");
  //$('input#join-search-name').inputLabel("School or group name");
  
  $('input#login-password').blur(function() {
    if (!$('input#login-password').val()) {
      $(this).addClass('password-hint');
    }
  });
  
  // If user begins logging in or they click into the password field, remove the hint
  $('input#login-email').keyup(removePasswordHint);
  $('input#login-password').focus(removePasswordHint);
  
  // Show the hint initially
  $('input#login-password').blur();
  
  $('form#download-quote-form input[type=image]').click(function() {
    if ($('input#copies').hasClass('error') || $('input#pages').hasClass('error')) {
      if ($('#container .messages').length) {
        $('#container .messages').html('Please correct the number of copies and pages before trying to download a quote');
        $('#container .messages').fadeOut("fast").fadeIn("fast");
      }
      else {
        $('#container').prepend('<div class="messages error" style="display: none;">Please correct the number of copies and pages before trying to download a quote</div>');
        $('#container .messages').slideDown();
      }
      
      return false;
    }
    
    if (quote_details == false) {
      // Remove all hidden input elements on parent form
      $('form#download-quote-form > input[type=hidden]').remove();
      
      // Show modal
      $('body').append('<div id="overlay"></div><div id="modal"><p><strong>Please provide some details for your yearbook quote:</strong></p><p><label for="email">Email</label><br/><input id="email" size="30" type="text" class="form-text-small" /></p><p><label for="group-name">Group name</label><br/><input id="group-name" size="30" type="text" class="form-text-small" /></p><p><label for="group-type">Group type</label><br/><select id="group-type" class="form-text-small"></select></p><div id="modal-close"/></div>');
      
      $('#modal-close').click(close_modal);
      $(document).keyup(function(e) {
        if (e.keyCode == 27) {
          close_modal();
        }
      });
      
      // Group types
      $('#modal #group-type').append('<option value="">-- Please select --</option>');
      $('#modal #group-type').append('<option value="primary">Primary or junior</option>');
      $('#modal #group-type').append('<option value="year11">Year 11</option>');
      $('#modal #group-type').append('<option value="year13">Year 13 or sixth form</option>');
      $('#modal #group-type').append('<option value="s4">S4 (Scotland)</option>');
      $('#modal #group-type').append('<option value="s6">S6 (Scotland)</option>');
      $('#modal #group-type').append('<option value="university">University/college</option>');
      $('#modal #group-type').append('<option value="business">Company or business</option>');
      $('#modal #group-type').append('<option value="club">Club or society</option>');
      $('#modal #group-type').append('<option value="friends">Friendship group</option>');
      $('#modal #group-type').append('<option value="other">Other group</option>');
      
      $('#modal').append('<div id="button"><input type="submit" value="Download quote" class="form-button"></div>');
      
      $('#modal #button input').click(function() {
        var errors = 0;
        
        if ($.trim($('#modal #email').val()) == '' || !IsEmail($.trim($('#modal #email').val()))) {
          errors++;
          $('#modal #email').addClass('error');
        }
        else {
          $('#modal #email').removeClass('error');
        }
        
        if ($.trim($('#modal #group-name').val()) == '') {
          errors++;
          $('#modal #group-name').addClass('error');
        }
        else {
          $('#modal #group-name').removeClass('error');
        }
        
        if ($.trim($('#modal #group-type').val()) == '') {
          errors++;
          $('#modal #group-type').addClass('error');
        }
        else {
          $('#modal #group-type').removeClass('error');
        }
        
        if (errors == 0) {
          $('form#download-quote-form').append('<input type="hidden" name="email" value="' + $('#modal #email').val() + '">');
          $('form#download-quote-form').append('<input type="hidden" name="group_name" value="' + $('#modal #group-name').val() + '">');
          $('form#download-quote-form').append('<input type="hidden" name="group_type" value="' + $('#modal #group-type').val() + '">');
          $('#overlay, #modal').fadeOut("fast", function () {
            $('#overlay, #modal').remove();
            $('#container .messages').slideUp("fast", function() {
              $('#container .messages').remove();
            });
            $('form#download-quote-form').submit();
          });
        }
        else {
          if ($('#modal .messages').length) {
            $('#modal .messages').fadeOut("fast").fadeIn("fast");
          }
          else {
            $('#modal').prepend('<div class="messages error" style="display: none;">There were errors found, please correct them</div>');
            $('#modal .messages').slideDown();
          }
        }
      });
      
      $('#overlay').css('opacity', '0.5');
      $('#overlay, #modal').fadeIn("fast");
      
      return false;
    }
  });
  
});

function removePasswordHint() {
  if ($('input#login-password').hasClass('password-hint')) $('input#login-password').removeClass('password-hint');
}

function IsEmail(email) {
  var regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  if (regex.test(email)) return true;
  else return false;
}

function close_modal() {
  $('#overlay, #modal').fadeOut("fast", function () {
    $(document).unbind('keyup');
    $('#overlay, #modal').remove();
  });
}
