function exForm(event, elm, ajax){

   event.preventDefault();

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

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

   xhr.onreadystatechange = function(){

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

         // alert(this.responseText);

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

         elm.querySelector('.submit input').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 loadMore(elm, ajax, records){

   let btnDefault = elm.innerText;
   
   elm.innerText = 'Loading ...';

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

   let formData = new FormData();
   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 = btnDefault;

         }else{

            elm.innerText = 'No More';

         }

      }

   };

   formData.append('id', id);
   formData.append('offset', offset);

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

}

function xClick(ajax, id){

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

   formData.append('id', id);

   xhr.open('POST', ajax, true);
   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 fileName(elm){

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

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

   });

}

function orderPoints(elm){

   var price = document.getElementById('price');
   var total = document.getElementById('total');

   total.value = (elm.value * price.value).toFixed(2);

}

function addPoints(elm){

   var left = document.getElementById('left');
   var updated = document.getElementById('updated');

   updated.value = parseInt(elm.value) + parseInt(left.value);

}
  
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;

      });
   
   });

}