/*Phone mask için input phoneValid classını almalı */
$.mask.definitions['~'] = ['1-9'];
$('.phoneValid').inputmask({"mask":"0 (999) 999 9999", greedy: false });

/*Email Validation  */
var patt = new RegExp("[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|asia|jobs|museum)");
var pattPassword = new RegExp("(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}");

function validateForm(element){
    var elemValue = element.val(),
        elemError = element.parent().find(".error_message"),
        elemForm = element.closest("form");
    if(elemValue.length == 0){
        elemError.addClass("error");
        elemForm.addClass("invalidForm");
        return false;
    }
    /*Email validate için email input emailValidate class ını almalı  */
    if(element.hasClass("emailValidate")){
        var validEmail = element.val();
        console.log("email");
        if (!patt.test(validEmail)) {
            console.log("email regex");
            elemError.addClass("error");
            elemForm.addClass("invalidForm");
            return false;
        }
        else{
            elemError.removeClass("error").addClass("check");
        }
    }
    if(element.hasClass("passwordValidate")){
        console.log("password");
        var validPassword = element.val();
        console.log("password");
        if (!pattPassword.test(validPassword)) {
            console.log("password regex");
            elemError.addClass("error");
            elemForm.addClass("invalidForm");
            return false;
        }
        else{
            elemError.removeClass("error").addClass("check");
        }
    }
    else{
        elemError.removeClass("error").addClass("check");
        elemForm.removeClass("invalidForm");
        return true;
    }
};

$(".submitForm").click(function (e) { 
    e.preventDefault();
    var formSubmit = $(this).closest('form'),
        formInput = formSubmit.find("input[type='text']"),
        formEmail = formSubmit.find("input[type='email']"),
        formPassword = formSubmit.find("input[type='password']"),
        formSelect = formSubmit.find("select.required"),
        formTextarea = formSubmit.find("textarea.required"),
        formInputError = formSubmit.next(".error_message");

        $(formInput).each(function(){
            validateForm($(this));
        });
        $(formPassword).each(function(){
            validateForm($(this));
        });
        $(formEmail).each(function(){
            validateForm($(this));
        });
        $(formSelect).each(function(){
            validateForm($(this));
        });
        $(formTextarea).each(function(){
            validateForm($(this));
        });
        if(formSubmit.hasClass("invalidForm")){
            console.log("false");
            e.preventDefault();
            e.stopPropagation();
            return;
        }
        if(!formSubmit.hasClass("invalidForm")) {
            console.log("true")
            submit_form(formSubmit);
        }
});

function submit_form(form) {
    var postData = new FormData($(form)[0]);
    $.ajax({
        type:form.attr('method'),
        url: $(form).attr('action'),
        data: form.serialize(),
        success: function (result) {
            console.log("result :" + result);
            form.removeClass("error");
            form.addClass("hide");
            form.next(".eBultenSuccess").addClass("active");
        },
        error: function() {
            console.log("error")
            form.addClass("error");
        }
    });
}
function seePassword() {
    var x = document.getElementById("password");
    if (x.type ==="password") {
      x.type ="text";
    } else {
      x.type ="password";
    }
  }
var myInput = document.getElementById("newPassword");
var letter = document.getElementById("letter");
var capital = document.getElementById("capital");
var number = document.getElementById("number");
var length = document.getElementById("length");

// When the user clicks on the password field, show the message box
myInput.onfocus = function() {
  document.getElementById("message").style.display ="block";
}

// When the user clicks outside of the password field, hide the message box
myInput.onblur = function() {
  document.getElementById("message").style.display ="none";
}

// When the user starts to type something inside the password field
myInput.onkeyup = function() {
  // Validate lowercase letters
  var lowerCaseLetters = /[a-z]/g;
  if(myInput.value.match(lowerCaseLetters)) {
    letter.classList.remove("invalid");
    letter.classList.add("valid");
  } else {
    letter.classList.remove("valid");
    letter.classList.add("invalid");
}

  // Validate capital letters
  var upperCaseLetters = /[A-Z]/g;
  if(myInput.value.match(upperCaseLetters)) {
    capital.classList.remove("invalid");
    capital.classList.add("valid");
  } else {
    capital.classList.remove("valid");
    capital.classList.add("invalid");
  }

  // Validate numbers
  var numbers = /[0-9]/g;
  if(myInput.value.match(numbers)) {
    number.classList.remove("invalid");
    number.classList.add("valid");
  } else {
    number.classList.remove("valid");
    number.classList.add("invalid");
  }

  // Validate length
  if(myInput.value.length >= 8) {
    length.classList.remove("invalid");
    length.classList.add("valid");
  } else {
    length.classList.remove("valid");
    length.classList.add("invalid");
  }
}