function putFile(lang_remove)
{
    // get first input element, save it in 'file' and remove
    var inputSpace = document.getElementById('input_space');
    var file = inputSpace.childNodes[0];
//    alert(file);
    var filename = file.value;
    if (checkIfExists(filename) == '1')
    {
        var newInput = document.createElement('input');
            newInput.type = 'file';
            newInput.name = 'upload_filename';
            newInput.onchange = function(){putFile(lang_remove);};
            newInput.className = 'fileButton';
            inputSpace.replaceChild(newInput, file);
    }
    else
    {
        file.style.position = 'absolute';
        file.style.left = '-1000px';
        re = /.*(\/|\\)(.*)$/;
        new_name = filename.replace(re, "$2");
        if(new_name.length > 20)
        {
            reg = /^(.{35}).*(\..{1,4})$/;
            new_name = new_name.replace(reg, "$1[...]$2");
            if(new_name.length > 45)
            {
                reg = /^(.{40}).*$/;
                new_name = new_name.replace(reg, "$1[...]");
            }
        }
        filename = new_name;
        var file_name = document.createTextNode(filename);

        // invoke a new input element and replace it with the first one
        var newInput = document.createElement('input');
            newInput.type = 'file';
            newInput.name = 'upload_filename';
            newInput.onchange = function(){putFile(lang_remove);};
            newInput.className = 'fileButton';
            inputSpace.replaceChild(newInput, file);
            
        // get the files_table element and place 'file', the file's name and a remove button within a new div
        var filesTable = document.getElementById('files_table');
        var filesTableBody = filesTable.getElementsByTagName('tbody')[0];

        filesTable.style.display = 'block';
    
        var newRow = document.createElement('tr');
        var newCol = document.createElement('td');
        newCol.className="r3";

        var newNameCol = document.createElement('td');
        newNameCol.className="r1";

        var newRemoveCol = document.createElement('td');
        newRemoveCol.className="r2";

        newRow.appendChild(newNameCol);
        newRow.appendChild(newRemoveCol);
        newRow.appendChild(newCol);
        filesTableBody.appendChild(newRow);



        var removeButton = document.createElement('a');
        removeButton.onclick = function(){removeFile(this);};
        removeButton.title = lang_remove;
        removeButton.className = 'ql';
        removeButton.href="#";
        removeButton.innerHTML = '&nbsp;';

        newCol.appendChild(file);
        newNameCol.appendChild(file_name);
        newRemoveCol.appendChild(removeButton);
    }
}

function removeFile(obj)
{
    var x = obj.parentNode.parentNode;
    x.parentNode.removeChild(x);
}

function checkIfExists(filename)
{
    var i;
    var allFiles = getElementsByName_iefix('input', 'upload_filename');

    if (allFiles.length == 1)
    {
        return '0';
    }
    for(i=1; i < allFiles.length; i++)
    {   
        if (allFiles[i].value == filename)
        {
            return '1';
        }
    }
    return '0';

}
