
$('#repassword').tooltip('hide');
window.onload = function () {
    document.getElementById("password").onchange = validate;
    document.getElementById("repassword").onchange = validate;
    document.getElementById("email").onchange = validateEmail;
    document.getElementById("username").onchange = validateUsername;
}

//isUsernameExist();
function isUsernameExist(){
    var exist = false;
    var username      = document.getElementById("username").value;
    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: baseURL + '/api/loadusername',
        async: false,
        data: {
            id  : null,
        },
        success: function(result){
            
            var data = result.data;
            let i = 0;
            for(i; i<data.length; i++){
                if(username === data[i].username){
                    exist = true;
                }
            }
            
        },
        error: function(xhr){
            //alert(xhr.status+'-'+xhr.statusText);
        }
    });
    console.log(exist);
    return exist;
}


function changetype() {
    var x = document.getElementById("password");
    if (x.type === "password") {
      x.type = "text";
    } else {
      x.type = "password";
    }
}

function validateUsername(){
    var validUsername = /^[a-z0-9_]{3,20}$/;
    var username      = document.getElementById("username").value;
    var usernameExist = 10;
    var usernameWrong = 20;
    var usernameOk    = 0;
    var code;
    
    //console.log(data[0].username);
    //console.log(validUsername.test(username));
    // console.log(exist);
    // if(!exist == validUsername.test(username)){
    //     return true;
    // } else {
    //     return false;
    // }
    return validUsername.test(username);
}

function validate(){
    var pass2=document.getElementById("repassword").value;
    var pass1=document.getElementById("password").value;
    
    if(pass1!=pass2){
        document.getElementById("repassword").setCustomValidity("Passwords Tidak Sama, Coba Lagi");
        return false;
    }else{
        document.getElementById("repassword").setCustomValidity('');
        return true;
    }
}

//validateEmail();
function validateEmail(){
    var validEmail = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
    var email = document.getElementById("email").value;
    //console.log(validEmail.test(email));

    return validEmail.test(email);  
}

function loginDirect(){
    window.location.href = "login";
}

$('#btnRegister').on('click', function(e){
    e.preventDefault();
    let $username    = $('#username').val();
    let $email       = $('#email').val();
    let $password    = $('#password').val();
    let $repassword    = $('#repassword').val();

    $('#wronginput').html('');
    if($username && $username.length < 3 ){
        $('#wronginput').html('Username minimal 3 character');
        $('#repassword').val('');
        $('#password').val('');
        return false;
    }else if($username && $username.length  > 20 ){
        $('#wronginput').html('Username maximum 20 character');
        $('#repassword').val('');
        $('#password').val('');

        return false;
    }else if($username && validateUsername() == false){
        $('#wronginput').html('Username must be contain a-z/0-9/_');
        $('#repassword').val('');

        return false;
    }else if($email && validateEmail() == false){
        $('#wronginput').html('Email wrong !')
        $('#repassword').val('');
        $('#password').val('');

        return false;
    }else if($repassword && validate() == false){
        $('#wronginput').html('Password not match !')
        $('#repassword').val('');
        $('#password').val('');

        return false;
    }else if($username && validateUsername() == true && validateEmail() == true && validate() == true){
        if(isUsernameExist()){
            $('#wronginput')("username  is exist");
            $('#repassword').val('');
            $('#password').val('');

            return false;
        }
        else{
            registrasi();
        }
    }
  
});
$(document).ready(function(){
    $('#password').on("cut copy paste",function(e) {
       e.preventDefault();
    });
 });

function registrasi(){
    let $username    = $('#username').val();
    let $email       = $('#email').val();
    let $password    = $('#password').val();
    let $repassword  = $('#repassword').val();
    //alert($username+$email+$password+$repassword);

    if($username == null || $username ==""){
        alert("username kosong");
        return false
    }else if($email == null || $email==""){
        alert("email kosong");
        return false
    } else if($password == null || $password=="" || $repassword==null || $repassword == "" ){
        alert("password kosong");
        return false
    }

    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: baseURL + '/api/registeruser',
        data: {
            username    : $username,
            email       : $email,
            password    : $password,
        },
        success: function(result){
           // alert("suksess regis")
            if(result.code != 0 ){
                //alert('Daftar berhasil!');
                //console.log('berhasil');
                alert("Registrasi Berhasil");
            }else{
                loginDirect();
                $('#username').val("");
                $('#email').val("");
                $('#password').val("");
                $('#repassword').val("");
            }
            
        },
        error: function(xhr){
            //alert(xhr.status+'-'+xhr.statusText);
        }
    });
}

    