// JavaScript Document
function deleteRow(option,id) {
    if(confirm('Are you sure you want to permanently delete this item?')){
       if(document.getElementById('Result')){
               document.getElementById("Result").style.display = "none";
               document.getElementById("Result").innerHTML = "";
               document.getElementById("Result").className = "";
        }
        window.scrollTo(0, 0);
        if(document.getElementById('Animation')){
                document.getElementById('Animation').style.display = 'block';
                if(document.getElementById('Submit_Paragraph')){
                        document.getElementById('Submit_Paragraph').style.display = 'none';
                } 
                setTimeout(function(){processDelete(option,id)},800);
        }else{
                processDelete(option,id);
        }
   }
}

function processDelete(option,id){
    var cssClass = '';
    var msg = '';
    var status = '';
    var rowClass = '';
    var totalRows = '';
    var newClass = '';
    var destination = 'includes/ajax/processing-scripts/process-delete.php';
    var data = new FormData();
    var now = new Date;
    data.append('Option',option);
    data.append('Now',now);
    data.append('Id',id);
    request = new XMLHttpRequest();		
    request.open("POST",destination,true);
    request.send(data);
    request.onreadystatechange = function(){              
        if(request.readyState == 4 && request.status == 200){                 
            if(document.getElementById('Result')){
                const result = JSON.parse(request.responseText);	
                cssClass = result.Class;
                msg = result.Msg;
                status = result.Status;
                if(option != 'Delete-Newsletter'){
                    document.getElementById("Result").className = cssClass;
                    document.getElementById("Result").innerHTML = msg;
                    document.getElementById("Result").style.display = "block";
                }
                if(status == 1){                                 
                    if(document.getElementById('Row_'+id)){
                        rowClass = document.getElementById('Row_'+id).className;
                        totalRows = document.getElementById('RowContainer').childElementCount;
                        $( "#Row_"+id ).fadeOut( "slow", 0.0, function(){
                            $( "#Row_"+id ).remove();                                         
                            for(var i=0;i<(totalRows-1);i++){
                                if(i == 0 || i%2 == 0){
                                    newClass = 'col-md-12 light-white';
                                }else{
                                    newClass = 'col-md-12';  
                                }
                                document.getElementById('RowContainer').childNodes[i].className = newClass;
                            }
                        });
                    }
                }
                if(document.getElementById('Animation')){
                    document.getElementById('Animation').style.display = 'none'; 
                }                                
            }
        }
    }  
}