// $Id:$
if (Drupal.jsEnabled) {
  $(document).ready(function() {
    
		// Validate form.
		$('#jasons-fishbowl-subscribe-form, #jasons-fc-subscribe-form').submit( function() {
			var errors = false;
			var alerts = '';
			// Remove errors.
			$(this).find('input').removeClass('error');
			var email = $(this).find('#edit-mail');
			var emailVal = email.val();
			var verify = $(this).find('#edit-verify-mail');
			var verifyVal = verify.val();
			
			if (emailVal && !validEmail(emailVal)) {
				errors = true;
				email.addClass('error');
				verify.addClass('error');
        alerts += "Please enter a valid e-mail address. \n";
			}
			
			if ((emailVal && verifyVal) && (emailVal != verifyVal)) {
				errors = true;
				email.addClass('error');
				verify.addClass('error');
				alerts += "Email addresses do not match. \n";
			}
			
			// Verify required fields.
			$('input.required').each( function() {
				if (!$(this).val()) {
					errors = true;
					$(this).addClass('error');
					var fieldName = $(this).prev('label').html().split(':', 1)[0];
					alerts += fieldName +" field is required. \n";
				}
			});
			
			if (errors) {
				alert(alerts);
				return false;
			}
			else {
				return true;
			}
			
		});
		
		// Verify valid email before submit.
		$('#edit-mail').blur( function() {
			var mailVal = $(this).val();
			
			if (mailVal && !validEmail(mailVal)) {
				$(this).addClass('error');
				alert ("Please enter a valid e-mail address.");
			}
	  });
		
		// Verify matching emails before submit.
		$('#edit-mail, #edit-verify-mail').blur( function() {
			var email = $('#edit-mail');
			var emailVal = email.val();
			var verify = $('#edit-verify-mail');
			var verifyVal = verify.val();
			
			if ((emailVal && verifyVal) && (emailVal != verifyVal)) {
				email.addClass('error');
				verify.addClass('error');
				alert ("Email addresses do not match.");
			}
			else {
				email.removeClass('error');
				verify.removeClass('error');
			}
		});
		
		// Hide location select dropdown
		$('#edit-store-select-wrapper').hide();
		
		// Load a list of nearby deli locations
		$('#edit-zip').blur( function () {
			
      var postalCode = $(this).val();
      
			// Validation
      if (postalCode.length == 0) {
				return false;
      }
			
			if (postalCode.length != 5 || postalCode != parseInt(postalCode)) {
				alert('Please enter a valid zip code.');
				return false;
			}
			
			// Remove an existing dropdown
      $('#edit-store-select-wrapper').hide();
			// Add a throbber
			$(this).after('<div id="location-select-throbber"></div>');
			
			
			// AJAX success function
			var limitLocResults = function(data) {
				$('#edit-store-select').html(data.options);
				$('#location-select-throbber').remove();
				$('#edit-store-select-wrapper').fadeIn();
			}
      
      var xhr = $.ajax({
				type: 'POST',
				url: '/ajax/jasons-fishbowl/locations',
				dataType: 'json',
				success: limitLocResults,
				data: {
					'ajax' : true,
					'postal_code' : postalCode
				}
			});
			
			// If someone attempts to resubmit while an existing ajax query is running
			// stop what we've started
			$(this).change( function() {
				// Abort the existing query
				xhr.abort();
			});
      
      return false;
      
    });
		
		function validEmail(email) {
			// Note: This regular expression is identical to the one used by Fishbowl
		  // for e-mail address validation.
			var regExp = /^\s*[a-zA-Z\d][a-zA-Z\d\.!#$%&'*+\-\/=?^_`{|}~]*@([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}\s*$/;
			
			// Verify valid email.
			if (email.length <= 200 && regExp.test(email)) {  
				return true;
      }
      else {
				return false;
      }
		}
		
  });
}
