 $(document).ready(function(){
 	
	jQuery.validator.addMethod("password", function( value, element ) {
		var result = this.optional(element) || value.length >= 6 && /\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 6 characters long and contain at least one number and one character.");
	
	// a custom method making the default value for companyurl ("http://") invalid, without displaying the "invalid url" message
	//jQuery.validator.addMethod("defaultInvalid", function(value, element) {
	//	return value != element.defaultValue;
	//}, "");

	jQuery.validator.addMethod("billingRequired", function(value, element) {
		
		if ($("#p_ccard").is(":checked"))
			return !this.optional(element);
			return $(element).parents(".subTable").length;
		
		
		//return !this.optional(element);
	}, "");


	jQuery.validator.addMethod("billingRequired2", function(value, element) {

		if ($("#p_debit").is(":checked"))
			return !this.optional(element);
			return $(element).parents(".subTable2").length;
		
		}, "");
	
	
	
	jQuery.validator.messages.required = "";
	$("#myform").validate({
		invalidHandler: function(e, validator) {
			var errors = validator.numberOfInvalids();
			if (errors) {
				var message = errors == 1
					? 'You missed 1 field. It has been highlighted below'
					: 'You missed ' + errors + ' fields.  They have been highlighted below';
				$("div.error span").html(message);
				$("div.error").show();
			} else {
				$("div.error").hide();
			}
		},
		onkeyup: false,
		submitHandler: function() {
			$("div.error").hide();
			document.myform.submit();
		},
		messages: {
			
			email: {
				required: " ",
				email: "Please enter a valid email address, example: you@yourdomain.com",
				remote: jQuery.validator.format("{0} is already taken, please enter a different address.")	
			}
		},
		debug:true
	});
	
  $(".resize").vjustify();
  $("div.buttonSubmit").hoverClass("buttonSubmitHover");

  if ($.browser.safari) {
    $("body").addClass("safari");
  }

  
  var creditcard = $("#creditcard").mask("9999 9999 9999 9999");


  // toggle optional billing address
  var subTableDiv = $("div.subTableDiv");
  var toggleCheck = $("input.toggleCheck");
   var subTableDiv2 = $("div.subTableDiv2");
  var toggleCheck2 = $("input.toggleCheck2");
  toggleCheck.is(":checked")
  	? subTableDiv.show()
	: subTableDiv.hide();
  $("input.toggleCheck").click(function() {
	  
      if (this.checked == true) {
        subTableDiv.show();

        $("#myform").valid();
      } else {
        subTableDiv.hide();
		
      }
  });

    toggleCheck2.is(":checked")
  	? subTableDiv2.show()
	: subTableDiv2.hide();
  $("input.toggleCheck2").click(function() {
	  
      if (this.checked == true) {
        subTableDiv2.show();

        $("#myform").valid();
      } else {
        subTableDiv2.hide();
		
		
      }
  });


});

$.fn.vjustify = function() {
    var maxHeight=0;
    $(".resize").css("height","auto");
    this.each(function(){
        if (this.offsetHeight > maxHeight) {
          maxHeight = this.offsetHeight;
        }
    });
    this.each(function(){
        $(this).height(maxHeight);
        if (this.offsetHeight > maxHeight) {
            $(this).height((maxHeight-(this.offsetHeight-maxHeight)));
        }
    });
};

$.fn.hoverClass = function(classname) {
	return this.hover(function() {
		$(this).addClass(classname);
	}, function() {
		$(this).removeClass(classname);
	});
};
