function exForm(event, elm, ajax){

   event.preventDefault();

   let btnDefault = elm.querySelector('.submit').value;
   elm.querySelector('.submit').value = 'Loading ...';

   let formData = new FormData(elm);
   let xhr = new XMLHttpRequest();

   xhr.onreadystatechange = function(){

      if(this.readyState == 4 && this.status == 200){

         let response = JSON.parse(this.responseText);

         // alert(this.responseText);

         elm.querySelector('.submit').value = btnDefault;

         if(response.redirect != undefined){
            window.location.href = response.redirect;
         }else{
            elm.querySelector('.ERRORS').style.display = 'block';
            elm.querySelector('.ERRORS').innerText = response.message;
         }

      }

   };

   xhr.open('POST', ajax);
   xhr.send(formData);
 
}

function charLeft(elm){

    let len = elm.value.length;
    let max = elm.getAttribute('maxlength');
    let left = max - len;

    elm.nextElementSibling.innerText = left;

}
  
function selectMenu(elm){

    var selectState;

    selectState = elm.nextElementSibling;

    selectState.style.display = selectState.style.display == 'block' ? 'none' : 'block';

    children = elm.nextElementSibling.childNodes;

    children.forEach(function(a){

        a.addEventListener('click', function(e){

            elm.innerText = a.innerText;
            selectState.style.display = 'none';

            document.getElementById(elm.previousElementSibling.getAttribute('id')).value = a.innerText;

        });
    
    });

}

function fileName(elm){

    document.getElementById('image').addEventListener('change', function(e){

        let name = this.files[0].name;
        elm.innerText = name;

    });

}

function xClick(ajax, id){

    var xhr = new XMLHttpRequest();

    xhr.open('POST', ajax, true);
    xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
    xhr.send('id='+id);

}

function loadMore(elm, ajax, records){
  
    elm.innerText = 'Loading ...';

    var offset = document.querySelectorAll(records).length;
    var section = document.getElementById('section').innerText;

    var xhr = new XMLHttpRequest();

    xhr.onreadystatechange = function(){

      if(this.readyState == 4 && this.status == 200){

        if(this.responseText){

          document.querySelector('#append').innerHTML += this.responseText;
          
          elm.innerText = 'Load More';

        }else{

          elm.innerText = 'No More';

        }

      }

    };

    xhr.open('POST', ajax, true);
    xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
    xhr.send('offset='+offset+'&section='+section);
  
}