function getDir(query) {
    var path = "dirQuery=set";
    process(path, 1);
}

function launch(fle) {
    var comm = "launchComm=set&file=" + fle;
    process(comm, 2);
}


function process(toRun, state) {
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
        if (this.readyState === 4 && this.status === 200) {
            if (state == 1) {
                updatePage(this.responseXML);
            }
        }
    };
    xhttp.open("POST", "/resources/phpProc/listDir.php", true);   // Open the connection
    xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xhttp.overrideMimeType('application/xml');            // Force return to be XML
    xhttp.send(toRun);                                     // Start the process
}

function updatePage(returnData) {
    var dirPath = returnData.getElementsByTagName('PATH_HEAD')[0];
    var dirs = returnData.getElementsByTagName('DIR');
    var files = returnData.getElementsByTagName('FILE');
    var images = returnData.getElementsByTagName('IMG_FILE');

    var insertArea = document.getElementById('Desktop');

    // Insert dirs
    var size = dirs.length;
    for (i = 0; i < size; i++) {
        // Need to attach launch code for file
        insertArea.innerHTML +=
            "<span ondblclick=\"launch(this.innerHTML)\" class=\"dirStyle\">" +
                dirs[i].innerHTML +
            "</span>";
    }

    // Insert files
    size = files.length;
    for (i = 0; i < size; i++) {
        insertArea.innerHTML +=
            "<span ondblclick=\"launch(this.innerHTML)\" class=\"fileStyle\">" +
                files[i].children[1].innerHTML +
            "</span>";
    }

    // Insert images
    size = images.length
    for (i = 0; i < size; i++) {
        insertArea.innerHTML +=
                "<img ondblclick=\"launch(this.src)\" class=\"iconImg\" src=\"" + images[i].children[0].innerHTML +
                "\" alt=\"" + images[i].children[1].innerHTML + "\"/>";
    }
}
