$(document).ready(function(){

  // clear floats on block element
  //$('.clearfix').append('<div class="clearall"><!-- clear floats --></div>')
  
  // remove bottom border from previous anchor on current selected side nav
  $('#nav ul li.current').prev().addClass('no-border');

  // add first and last classes to all lists, table rows, table heads and table cells in the DOM
  $('ul li:first-child,table tr:first-child,tr td:first-child, tr th:first-child').addClass('first');
  $('ul li:last-child,table tr:last-child,tr td:last-child, tr th:last-child').addClass('last');

  $('.button-link, button').wrapInner('<span>');

  // add validation method for password checking
	//jQuery.validator.addMethod("password", function( value, element ) {
	//	var result = this.optional(element) || value.length >= 8 && /\d/.test(value) && /[a-z]/i.test(value);
	//	if (!result) {
	//		element.value = "";
	//		var validator = this;
	//		setTimeout(function() {
	//			validator.blockFocusCleanup = true;
	//			element.focus();
	//			validator.blockFocusCleanup = false;
	//		}, 1);
	//	}
	//	return result;
	//}, "Your password must be at least 8 characters long and contain at least one number and one character.");

	$.validator.addMethod("password",function(value,element){
    return this.optional(element) || /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}$/i.test(value);  
  },"It is recommended as web professionals that you use a secure password with a minimum length of 8 characters, and at least one uppercase, a number and a lowercase character. Basically the longer and more random the better, you know the drill.");

	
  // validation of form submissions
  $('#register-form, #comment_form, #award-entry-form form').validate({
		rules: {
			firstname: "required",
			lastname: "required",
			username: {
				required: true,
				minlength: 6
			},
			password: "required password",
			password_confirm: {
				required: true,
				equalTo: "#password"
			}
		},
		messages: {
      username: {
				required: "Please provide a username",
				minlength: "Your username must be at least 6 characters long"
			},
      password: {
				required: "Please provide a password",
				minlength: "Your password must be at least 8 characters long"
			},
			password_confirm: {
				required: "Please provide a confirmation password",
				equalTo: "Please enter the same password as above."	
			}
	  }
	});
	
  $('#cart_checkout_form').validate({
		messages: {
			first_name: "Really need your name for the payment.",
			last_name: "Really need your name for the payment.",
			address: "We are going to need an address too.",
			city: "Did you forget this one. It is important.",
			state: "An address needs a state.",
			zip: "Come on we know you know your post code.",
			phone: "We need your phone number if it goes wrong.",
			email_address: "Can we have your email, need to email you the receipt.",
			credit_card_number: "Its a pain, but we need the credit card number, no spaces please.",
			CVV2: "Oh and the pesky number on the back of the card too."
	  }
	});
  
  // display example input info or field names within input
  $('.input-example').example(function() {
      return $(this).attr('title');
  });

  if ($('#category_id_1').is(':checked')) {
    $('#student-entry-info').slideDown('fast',function(){
     $(this).children('input').addClass('required');
    });
  }
  
  $('#category_id_1').click(function() {
   if ($('#category_id_1').is(':checked')) {
     $('#student-entry-info').slideDown('fast',function(){
      $(this).children('input').addClass('required');
     });
   } else {
     $('#student-entry-info').slideUp('fast',function(){
      $(this).children('input').removeClass('required');
     });
   }
  });  
  
});
