 $(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);
		//alert(result);
		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 ($("#bill_to_co").is(":checked"))
			return $(element).parents(".subTable").length;
		return !this.optional(element);
	}, "");
	
	jQuery.validator.messages.required = "";
	$("form").validate({
		invalidHandler: function(e, validator) {
			var errors = validator.numberOfInvalids();
			if (errors) {
				var message = errors == 1
					? 'You missed 1 field. It has been highlighted below'
					: 'Please enter the highlighted ' + errors + ' fields.';
				$("div.error span").html(message);
				$("div.error").show();
				var message1 = errors == 1
					? 'You missed 1 field. It has been highlighted above'
					: 'Please enter the highlighted ' + errors + ' fields.';
					$("div.error1 span").html(message1);
				$("div.error1").show();
			} else {
				$("div.error").hide();
				$("div.error1").hide();
			}
		},
		onkeyup: false,
		submitHandler: function() {
			$("div.error").hide();
			$("div.error1").hide();
			//alert("submit! use link below to go to the other step");
			/*document.form1.action="process_member_register.php";*/
			document.forms[0].submit();
		},
		messages: {
			password2: {
				required: " ",
				equalTo: "<br>Password do not Match"	
			},
			email: {
				required: " ",
				email: "<br>Please enter a valid email address",
				remote: jQuery.format("<br>E-mail Id already exists.")
			},
			user_code: {
				remote: jQuery.format("<br>Invalid login code.")
			},
			organization_name: {
				required: " ",
				remote: jQuery.format("<br>Organization Name already exists.")
			},
			logoname: {
				required: " ",
				logoname:"Select"
			}
		},
		debug:true
	});
	
  $(".resize").vjustify();
  $("div.buttonSubmit").hoverClass("buttonSubmitHover");

  if ($.browser.safari) {
    $("body").addClass("safari");
  }
  
  $("input.phone").mask("(+99) 9999-99999999");
  $("input.zipcode").mask("99999");
  $("input.incubator_id").mask("999999999999999");
  var creditcard = $("#creditcard").mask("9999 9999 9999 9999");

  $("#cc_type").change(
    function() {
      switch ($(this).val()){
        case 'amex':
          creditcard.unmask().mask("9999 999999 99999");
          break;
        default:
          creditcard.unmask().mask("9999 9999 9999 9999");
          break;
      }
    }
  );

  // toggle optional billing address
  var subTableDiv = $("div.subTableDiv");
  var toggleCheck = $("input.toggleCheck");
  toggleCheck.is(":checked")
  	? subTableDiv.hide()
	: subTableDiv.show();
  $("input.toggleCheck").click(function() {
      if (this.checked == true) {
        subTableDiv.slideUp("medium");
        $("form").valid();
      } else {
        subTableDiv.slideDown("medium");
      }
  });


});

$.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);
	});
};
