(function($) {
  'use strict';
  $.validator.setDefaults({
    submitHandler: function() {
      //alert("submitted!");
    }
  });
  $(function() {
    // validate the comment form when it is submitted
   /* $("#commentForm").validate({
      errorPlacement: function(label, element) {
        label.addClass('mt-2 text-danger');
        label.insertAfter(element);
      },
      highlight: function(element, errorClass) {
        $(element).parent().addClass('has-danger')
        $(element).addClass('form-control-danger')
      }
    });*/


    // validate signup form on keyup and submit
    /*$("#frm_reg").validate({
      rules: {
		sponsorID:{
		  required: true,
		  digits: true,
		  minlength: 9
		},
		placementID:{
		  digits: true,
		  minlength: 9
		},
        firstName: {
          required: true,
          minlength: 3
        },
        lastName: {
          required: true,
          minlength: 3
        },
	    mobileNumber:{
        required: true,
        digits: true,
	    minlength: 10,
		},
        password: {
          required: true,
          minlength: 6
        },
        confirmPassword: {
          required: true,
          minlength: 6,
          equalTo: "#password"
        },
        emailID: {
          required: true,
          email: true
        },
		txtKYC: {
          required: true,
        },
        kyc: {
          required: true,
		  extension: "png,jpeg,jpg,pdf"
        },
        terms: "required"
      },
      messages: {
		sponsorID:{
        required: 'Please enter a 9 digit valid sponsor id.',
        digits:   'Please enter a 9 digit valid sponsor id.',
		minlength: 'Please enter a 9 digit valid sponsor id.'
		},
		placementID:{
        digits: 'Please enter a 9 digit valid placement id.',
		minlength: 'Please enter a 9 digit valid sponsor id.'
		},
        firstName: {
          required: "Please enter a First Name",
          minlength: "First Name must consist of at least 3 characters"
        },
		lastName: {
          required: "Please enter a username",
          minlength: "Last Name must consist of at least 3 characters"
        },
	    mobile:{
          required: 'Please provide mobile number.',
          digits:'Please enter a valid mobile number.',
		  minlength: "Please enter 10 digit mobile number."
        },
        password: {
          required: "Please provide a password",
          minlength: "Your password must be at least 6 characters long"
        },
        confirmPassword: {
          required: "Please provide a password",
          minlength: "Your password must be at least 6 characters long",
          equalTo: "Please enter the same password as above"
        },
		txtKYC: {
			required: "Please enter your kyc info."
		},
		kyc: {
			required: "Please upload a file.",
			extension: "Only .png .jpeg .pdf file are allowed."
		},
        emailID: "Please enter a valid email address",
        agree: "Please accept our policy",
        topic: "Please select at least 2 topics"
      },
      errorPlacement: function(label, element) {
        label.addClass('mt-2 text-danger');
        label.insertAfter(element);
      },
      highlight: function(element, errorClass) {
        $(element).parent().addClass('has-danger')
        $(element).addClass('form-control-danger')
      }

    });*/

	jQuery.validator.addMethod("notEqualTo", function(value, element, param) {
	 return this.optional(element) || value != $(param).val();
	 }, "This has to be different...");

	 $.validator.addMethod(
            "regex",
            function(value, element, regexp) 
            {

			   if(element.id=="addressProofInfo")
				{
				   if($("input[name=addressProof]:checked").val() == 2 || $("input[name=addressProof]:checked").val() == 3)
					   return true;
				}
			   
			   if (regexp.constructor != RegExp)
                    regexp = new RegExp(regexp);
                else if (regexp.global)
                    regexp.lastIndex = 0;
                return this.optional(element) || regexp.test(value);
            },
            "Please check your input."
    );


    // propose username by combining first- and lastname
    $("#username").focus(function() {
      var firstname = $("#firstname").val();
      var lastname = $("#lastname").val();
      if (firstname && lastname && !this.value) {
        this.value = firstname + "." + lastname;
      }
    });


    //code to hide topic selection, disable for demo
    var newsletter = $("#newsletter");
    // newsletter topics are optional, hide at first
    var inital = newsletter.is(":checked");
    var topics = $("#newsletter_topics")[inital ? "removeClass" : "addClass"]("gray");
    var topicInputs = topics.find("input").attr("disabled", !inital);
    // show when newsletter is checked
    newsletter.on("click", function() {
      topics[this.checked ? "removeClass" : "addClass"]("gray");
      topicInputs.attr("disabled", !this.checked);
    });
  });
})(jQuery);