// OPEN DOCUMENT EDITOR into a Pop-Up Window
function sk_document_upload(){
    window.open ("index.php?blank_popup=modules/system_documents/admin/list.php","DocumentUploads","status=0,toolbar=0,location=0, menubar=0, directories=0, resizable=1, scrollbars=1, height=600, width=900");
}

var numFilesInputTotal = 1;
function addMoreFilesToUpload(myId, numFiles){
    var myTd = myId + 'TD';

    // Add the numberOfFileUploadInputs
    for(var i=1; i<=numFiles; i++){
        numFilesInputTotal = numFilesInputTotal + 1;
        
        newInput = document.createElement('br');
        document.getElementById(myTd).appendChild(newInput);

        // Maximum Upload is 10 files
        if(numFilesInputTotal > 10){
            numFilesInputTotal = 10;

            newInput = document.createElement('div');
            newInput.setAttribute('id', 'divItemsMax10');
            newInput.style.textAlign = 'center';
            newInput.style.color = 'red';
            newInput.style.fontSize = '12px';
            document.getElementById(myTd).appendChild(newInput);
            document.getElementById('divItemsMax10').innerHTML = 'Maximum 10 uploads at the same time';
            
            document.getElementById('addFilesSelect').style.display = 'none';
            
            break;
        }
        else{
            newInput = document.createElement('input');
            newInput.setAttribute('type', 'file');
            newInput.setAttribute('name', 'items[' + numFilesInputTotal + ']');
            document.getElementById(myTd).appendChild(newInput);
        }
    }
}

function documents_changeDirList(myDir, defaultDirPath){
    // Change the name of folder for auto value to rename
    var name = myDir.split('/');
    document.getElementById('renameDir_input').value = name[(name.length) - 2];
    
    // Show or hide the folder options if we can or can not do them
    var pathActualFolder = document.getElementById('mySelectedFolder_select').value;
    if(pathActualFolder == defaultDirPath) showAndHide('renameOption1_div,renameOption2_div,deleteOption1_div', 'hide,hide,hide');
    else showAndHide('renameOption1_div,renameOption2_div,deleteOption1_div', 'show,show,show');
    
    // Proceed with action
    document.getElementById('which_directory_toUpload_input').value = myDir;
    document.getElementById('listFileInFolder_DIV').innerHTML = '<img src="images/ajax-loader.gif" alt="ajax-loader" border=0 />';
    ajax.replace('index.php', 'listFileInFolder_DIV', 'post', 'ajaxAction=modules/system_documents/ajax_actions.php&action=changeListDocuments&dir=' + myDir);
}


function checkDocumentsToManipulate(myValue, myChecked){
    var whatList = 'list_documents_checkedToManipulate';
    var actualListIds = document.getElementById(whatList).value
    
    // Remove ID so if it is there, we won't make it double
    actualListIds = actualListIds.replace(',' + myValue, '');
    
    // If we CHECKED true, we add the ID
    // Else : It's already been removed
    if(myChecked == true) actualListIds = actualListIds + ',' + myValue;
    
    document.getElementById(whatList).value = actualListIds;
}

function manipulateSelectedFiles(myValue, myEmptySelectionMessage){
    showAndHide('moveToListCategory_span,moveToListCategory_Rename_Overwrite_span,manipulateSelectedFiles_button_go_span', 'hide,hide,hide');
    document.getElementById('manipulateSelectedMoveToDir').value = 'NA';
    
    var whatList = 'list_documents_checkedToManipulate';
    var actualListIds = document.getElementById(whatList).value

    if(actualListIds == ""){
        alert(myEmptySelectionMessage);
        document.getElementById('myActionSelectBox').value = "NA";
    }
    else if(myValue != "NA"){
        if(myValue == "moveto" || myValue == "copyto"){
            showAndHide('moveToListCategory_span,moveToListCategory_Rename_Overwrite_span', 'show,show');
        }
        else if(myValue == "delete"){
            showAndHide('manipulateSelectedFiles_button_go_span', 'show');
        }
    }
}

/* DELETE FOLDER /**/
function systemDocuments_deleteFolder(confirmMessage, canNotRemoveDefaultMessage, defaultDirPath){
    var pathActualFolder = document.getElementById('mySelectedFolder_select').value;
    if(pathActualFolder == defaultDirPath) alert(canNotRemoveDefaultMessage)
    else{
        if(confirm(confirmMessage)){
            document.getElementById('buttonDeleteFolder_span').innerHTML = '<img src="images/ajax-loader.gif" alt="" height="15" width="30" />';
            ajax.replace('index.php', 'ajaxActions_div', 'post', 'ajaxAction=modules/system_documents/ajax_actions.php&action=deleteFolder&folderPath=' + pathActualFolder, doNothing, refreshSystemDocument);
        }
    }
}
/* RENAME FOLDER /**/
function systemDocuments_renameFolder(confirmMessage, myNameNotStraightMessage, canNotRenameDefaultMessage, defaultDirPath){
    var pathActualFolder = document.getElementById('mySelectedFolder_select').value;
    var newFolderName = document.getElementById('renameDir_input').value;

    if(pathActualFolder == defaultDirPath) alert(canNotRenameDefaultMessage)
    else{
        if(!checkIfAZaz09_straight(newFolderName)) alert(myNameNotStraightMessage);
        else if(newFolderName != ""){
            if(confirm(confirmMessage)){
                document.getElementById('buttonRenameFolder_span').innerHTML = '<img src="images/ajax-loader.gif" alt="" height="15" width="30" />';
                ajax.replace('index.php', 'ajaxActions_div', 'post', 'ajaxAction=modules/system_documents/ajax_actions.php&action=renameFolder&oldName=' + pathActualFolder + '&newName=' + newFolderName, doNothing, refreshSystemDocument);
            }
        }
    }
}


/* CREATE A NEW FOLDER /**/
function createNewFolder(myNameNotStraightMessage){
    var path = document.getElementById('mySelectedFolder_select').value;
    var newFolder = document.getElementById('createDir_input').value;

    if(!checkIfAZaz09_straight(newFolder)) alert(myNameNotStraightMessage);
    else if(newFolder != ""){
        document.getElementById('buttonCreateFolder_span').innerHTML = '<img src="images/ajax-loader.gif" alt="" height="15" width="30" />';
        ajax.replace('index.php', 'ajaxActions_div', 'post', 'ajaxAction=modules/system_documents/ajax_actions.php&action=createNewFolder&newDir=' + path + newFolder + '/', doNothing, refreshSystemDocument);
    }
}

function proceedWithManipulationOfFiles(){
    document.getElementById('manipulateSelectedFiles_button_go_span').innerHTML = '<img src="images/ajax-loader.gif" alt="" />';
    var myAction = document.getElementById('myActionSelectBox').value;
    var myDestinationIfAny = document.getElementById('manipulateSelectedMoveToDir').value;
    
    if(myAction == "moveto" && myDestinationIfAny != "NA") documents_moveFilesTo(myDestinationIfAny);
    else if(myAction == "copyto" && myDestinationIfAny != "NA") documents_moveFilesTo(myDestinationIfAny);
    else if(myAction == "delete") documents_deleteSelectedFiles();
    else if(myAction == "createdir") documents_deleteSelectedFiles();
}

/* DELETE A FILE /**/
function documents_deleteSelectedFiles(){
    var filesToDelete = document.getElementById('list_documents_checkedToManipulate').value.replace(',', '');
    ajax.replace('index.php', 'ajaxActions_div', 'post', 'ajaxAction=modules/system_documents/ajax_actions.php&action=deleteSelectedFiles&files=' + filesToDelete, doNothing, refreshSystemDocument);
}

/* MOVE A FILE FROM PATH A TO PATH B /**/
function documents_moveFilesTo(myDestinationIfAny){
    var filesToMove = document.getElementById('list_documents_checkedToManipulate').value.replace(',', '');
    if(document.getElementById('doWhat2').checked == true) var doWhat = 2;
    else var doWhat = 1;
    
    if(document.getElementById('myActionSelectBox').value == "copyto") var moveOrCopy = "copy";
    else var moveOrCopy = "move";

    ajax.replace('index.php', 'ajaxActions_div', 'post', 'ajaxAction=modules/system_documents/ajax_actions.php&action=moveSelectedFilesTo&files=' + filesToMove + '&pathTo=' + myDestinationIfAny + '&doWhat=' + doWhat + '&moveOrCopy=' + moveOrCopy, doNothing, refreshSystemDocument);
}
function refreshSystemDocument(){
    window.location = 'index.php?' + getQuerystring();
}

/* DOCUMENTS TO UPLOAD /**/
var nth_numfiletoupload = 1;
function system_documents_changeNumFiles2Upload(myAlertMsg){
    nth_numfiletoupload = nth_numfiletoupload + 1;
    
    if(document.getElementById('numFiles2UploadInput').value < 10){
        showAndHide('systemDoc_plusIcon_swap_1,systemDoc_plusIcon_swap_2', 'hide,show');
        
        document.getElementById('numFiles2UploadInput').value = parseInt(document.getElementById('numFiles2UploadInput').value) + 1;
        
        var newInput = document.createElement('div');
        newInput.setAttribute('id', 'filetoUpload_nth_div_' + nth_numfiletoupload);
        document.getElementById('listOfInputFile_div').appendChild(newInput);
        
        ajax.replace("index.php", "filetoUpload_nth_div_" + nth_numfiletoupload, "post", "ajaxAction=modules/system_documents/ajax_actions.php&action=systemDocuments_changeNumUpload&nthFile=" + nth_numfiletoupload, doNothing, swapPlusFileUploadIcon);
    }
    else alert(myAlertMsg);
}
function swapPlusFileUploadIcon(){
    if(document.getElementById('numFiles2UploadInput').value >= 10) showAndHide('systemDoc_plusIcon_swap_2,systemDoc_plusIcon_swap_1', 'hide,hide');
    else showAndHide('systemDoc_plusIcon_swap_2,systemDoc_plusIcon_swap_1', 'hide,show');
}
