
    function addFile()
    {
        var oContent = $("fileBox_content");
        var oCommand = $("fileBox_moreCommand");
        var oButton = $("fileBox_commandButton");
        var oTitle    = "fileBox_title";
        var oPopup    = "fileBox_div";
        
        if(oPopup && oContent)
        {
            oContent.innerHTML = "";
            
            // like ifr2 in the uploadprogress example (server.php)
            var oFrame = document.createElement("iframe");
            oFrame.src = "/resources/templates/fileUploader.html";
            oFrame.style.border = 1;
            oFrame.width = "100px";
            oFrame.height= "100px";
            oFrame.id = "iFrame";
            oFrame.name = "iFrame";
            oFrame.style.width = '560px';
            oFrame.style.height = '430px';
            oContent.appendChild(oFrame);
            
            if(oButton)
            {
                oButton.style.display = 'none';
            }
            
            initFileCommand(oCommand);
            
            showPopup(oPopup);
            initPopup(oPopup, oTitle);
        }
    }
    
    function initFileCommand(oCommand)
    {
        if(oCommand)
        {
            oCommand.innerHTML = "";
            
            var oForm = document.createElement("form");
            oForm.id = 'uploadedForm';
            oForm.name = 'uploadedForm';
            
            var oMoved = document.createElement('input');
            oMoved.id = 'fileMoved';
            oMoved.name = 'fileMoved';
            oMoved.type = 'hidden';
            oMoved.value = 0;
            
            oForm.appendChild(oMoved);
            oCommand.appendChild(oForm);
        }    
    }
    
    function displayList()
    {
        if(oList = document.getElementById('fileList'))
        {
            oList.style.display = '';    
        }
        
        if(oThumbnails = document.getElementById('fileThumbnails'))
        {
            oThumbnails.style.display = 'none';    
        }
    }
    
    function displayThumbnails()
    {
        if(oList = document.getElementById('fileList'))
        {
            oList.style.display = 'none';    
        }
        
        if(oThumbnails = document.getElementById('fileThumbnails'))
        {
            oThumbnails.style.display = '';    
        }
    }
    
    function displayFile()
    {
        if(typeof(displayUploadedFile) == 'function')
        {
            displayUploadedFile();    
        }
        else
        {
            if(oView = document.getElementById('view'))
            {
                displayFileList('list');
                displayFileThumbnails('thumbnails');
            }   
        }
    }
    
    function displayFileThumbnails(sView)
    {
        if(oContent = document.getElementById('fileThumbnails'))
        {
            if(oCount = document.getElementById('countFiles'))
            {
                if(oForm = document.getElementById('uploadedForm'))
                {
                    var iMoved = oForm["fileMoved"].value;
                    for(iFile = 0; iFile < iMoved; iFile ++)
                    {
                        oCount.value ++;
                        
                        var sFile = oForm["fileUploaded_"+iFile].value;
                        var sPath = oForm["pathUploaded_"+iFile].value;
                        
                        var oDiv = document.createElement('div');
                        oDiv.className = "fileUploaded";
                        oDiv.id = "fileThumbnails_"+oCount.value;
                        
                        var oTable = document.createElement('table');
                        var oDeleteRow = oTable.insertRow(oTable.rows.length);
                        var oDeleteCell = oDeleteRow.insertCell(0);
                        oDeleteCell.className = "deleteUploaded";
                        
                        var oDeleteLink = document.createElement('a');
                        oDeleteLink.href = "javascript:removeFileUploaded("+oCount.value+")";
                        
                        var oDeleteImg = document.createElement('img');
                        oDeleteImg.border = 0;
                        oDeleteImg.src = '/resources/images/common/commands/delete.gif';
                        
                        oDeleteLink.appendChild(oDeleteImg);
                        oDeleteCell.appendChild(oDeleteLink);
                        
                        var oFileRow = oTable.insertRow(oTable.rows.length);
                        var oFileCell = oFileRow.insertCell(0);
                        
                        var oLink = document.createElement('a');
                        oLink.target = "_blank";
                        
                        var oImage = document.createElement('img');
                        oImage.border = 0;
                        
                        var arrFile = splitFileName(sFile);
                        var arrPath = splitFileName(sPath);
                        
                        if(arrPath[1].toLowerCase() == "gif" || arrPath[1].toLowerCase() == "png" || arrPath[1].toLowerCase() == "jpg")
                        {
                            oLink.href = "/getImage.php?path="+sPath;
                            oImage.src = "/getImage.php?path="+sPath+"&canvasWidth=100&canvasHeight=100";
                        }
                        else
                        {
                            oLink.href = "/download.php?filePath="+sPath;
                            oImage.src = "/getImage.php?path=resources/images/icons/"+getIcon(arrPath[1],sView)+"&canvasWidth=100&canvasHeight=100";
                        }
                        
                        oLink.appendChild(oImage);
                        oFileCell.appendChild(oLink);
                        
                        var oNameRow = oTable.insertRow(oTable.rows.length);
                        var oNameCell = oNameRow.insertCell(0);
                        oNameCell.className = "nameUploaded";
                        
                        var oLinkName = document.createElement('a');
                        oLinkName.title = sFile;
                        oLinkName.href = oLink.href;
                        oLinkName.appendChild(document.createTextNode(getSummaryName(arrFile[0], arrPath[1])));
                        
                        oNameCell.appendChild(oLinkName);
                        
                        var oHidden1 = document.createElement('input');
                        oHidden1.name = 'tempFile_'+oCount.value;
                        oHidden1.id = 'tempFile_'+oCount.value;
                        oHidden1.type = "hidden";
                        oHidden1.value = sPath;
                        
                        var oHidden2 = document.createElement('input');
                        oHidden2.name = 'fileName_'+oCount.value;
                        oHidden2.id = 'fileName_'+oCount.value;
                        oHidden2.type = "hidden";
                        oHidden2.value = sFile;
                        
                        var oHidden3 = document.createElement('input');
                        oHidden3.name = 'fileStatus_'+oCount.value;
                        oHidden3.id = 'fileStatus_'+oCount.value;
                        oHidden3.type = "hidden";
                        oHidden3.value = 'rename';
                        
                        oNameCell.appendChild(oHidden1);
                        oNameCell.appendChild(oHidden2);
                        oNameCell.appendChild(oHidden3);
                        oDiv.appendChild(oTable);
                        oContent.appendChild(oDiv);
                        
                        var oCommand = $("fileBox_moreCommand");
                        if(oCommand)
                        {
                            oCommand.style.display = '';    
                        }
                        
                        closePopup("fileBox_div");                            
                    }
                }
            }
        }    
    }
    
    function displayFileList(sView)
    {
        if(oContent = document.getElementById('fileContent'))
        {
            if(oCount = document.getElementById('countFiles'))
            {
                if(oForm = document.getElementById('uploadedForm'))
                {
                    var iMoved = oForm["fileMoved"].value;
                    for(iFile = 0; iFile < iMoved; iFile ++)
                    {
                        oCount.value ++;
                        
                        var sFile = oForm["fileUploaded_"+iFile].value;
                        var sPath = oForm["pathUploaded_"+iFile].value;
                        var sSize = oForm["sizeUploaded_"+iFile].value;
                        var sDate = oForm["dateUploaded_"+iFile].value;
                        
                        var arrFile = splitFileName(sFile);
                        var arrPath = splitFileName(sPath);
                        
                        if(arrPath[1].toLowerCase() == "gif" || arrPath[1].toLowerCase() == "png" || arrPath[1].toLowerCase() == "jpg")
                        {
                            sTarget = "/getImage.php?path="+sPath;
                        }
                        else
                        {
                            sTarget = "/download.php?filePath="+sPath;
                        }
                        sSource = "/getImage.php?path=resources/images/icons/"+getIcon(arrPath[1],sView);
                        
                        var oTable = document.getElementById('fileListContent');
                        
                        var iRow = oTable.rows.length;
                        var oRow = oTable.insertRow(iRow);
                        oRow.className = 'tableRow';
                        oRow.id = "fileList_"+oCount.value;
                        
                        var oIconCell = oRow.insertCell(-1);
                        oIconCell.className = 'optionCell';
                        
                        var oIconLink = document.createElement('a');
                        oIconLink.target = "_blank";
                        oIconLink.href = sTarget;
                        
                        var oIconImage = document.createElement('img');
                        oIconImage.border = 0;
                        oIconImage.src = sSource;
                        
                        oIconLink.appendChild(oIconImage);
                        oIconCell.appendChild(oIconLink);
                        
                        var oNameCell = oRow.insertCell(-1);
                        oNameCell.className = 'tableCell';
                        
                        var oNameLink = document.createElement('a');
                        oNameLink.target = "_blank";
                        oNameLink.href = sTarget;
                        oNameLink.appendChild(document.createTextNode(sFile));
                        oNameCell.appendChild(oNameLink);
                        
                        var oSizeCell = oRow.insertCell(-1);
                        oSizeCell.className = 'tableCell';
                        
                        var oSizeLink = document.createElement('a');
                        oSizeLink.target = "_blank";
                        oSizeLink.href = sTarget;
                        oSizeLink.appendChild(document.createTextNode(convertFileSize(sSize)));
                        oSizeCell.appendChild(oSizeLink);
                        
                        var oDateCell = oRow.insertCell(-1);
                        oDateCell.className = 'tableCell';
                        
                        var oDateLink = document.createElement('a');
                        oDateLink.target = "_blank";
                        oDateLink.href = sTarget;
                        oDateLink.appendChild(document.createTextNode(sDate));
                        oDateCell.appendChild(oDateLink);
                        
                        var oDeleteCell = oRow.insertCell(-1);
                        oDeleteCell.className = 'optionCell';
                        
                        var oDeleteLink = document.createElement('a');
                        oDeleteLink.href = "javascript:removeFileUploaded("+oCount.value+")";
                        
                        var oDeleteImg = document.createElement('img');
                        oDeleteImg.border = 0;
                        oDeleteImg.src = '/resources/images/common/commands/delete.gif';
                        
                        oDeleteLink.appendChild(oDeleteImg);
                        oDeleteCell.appendChild(oDeleteLink);
                        
                        var oHidden1 = document.createElement('input');
                        oHidden1.name = 'tempFileList_'+oCount.value;
                        oHidden1.id = 'tempFileList_'+oCount.value;
                        oHidden1.type = "hidden";
                        oHidden1.value = sPath;
                        
                        var oHidden2 = document.createElement('input');
                        oHidden2.name = 'fileNameList_'+oCount.value;
                        oHidden2.id = 'fileNameList_'+oCount.value;
                        oHidden2.type = "hidden";
                        oHidden2.value = sFile;
                        
                        var oHidden3 = document.createElement('input');
                        oHidden3.name = 'fileStatusList_'+oCount.value;
                        oHidden3.id = 'fileStatusList_'+oCount.value;
                        oHidden3.type = "hidden";
                        oHidden3.value = 'rename';
                        
                        oDeleteCell.appendChild(oHidden1);
                        oDeleteCell.appendChild(oHidden2);
                        oDeleteCell.appendChild(oHidden3);
                        
                        if(oSpan = document.getElementById('nbFiles'))
                        {
                            oSpan.innerHTML = Number(oSpan.innerHTML) + 1;        
                            if(oSpan.innerHTML > 0)
                            {
                                if(oEmpty = document.getElementById('noFiles'))
                                {
                                    oEmpty.style.display = 'none';    
                                }    
                            }
                        }
                        
                        var oCommand = $("fileBox_moreCommand");
                        if(oCommand)
                        {
                            oCommand.style.display = '';    
                        }
                        
                        closePopup("fileBox_div");                            
                    }
                }
            }
        }    
    }
    
    function removeFileUploaded(idDiv)
    {
        if(oDiv = document.getElementById('fileThumbnails_'+idDiv))
        {
            oDiv.style.display = 'none';
            if(oHidden = document.getElementById('fileStatus_'+idDiv))
            {
                oHidden.value = "unlink";
            }
        }
        
        if(oDiv = document.getElementById('fileList_'+idDiv))
        {
            oDiv.style.display = 'none';
            if(oHidden = document.getElementById('fileListStatus_'+idDiv))
            {
                oHidden.value = "unlink";
            }
        }
        
        if(oView = document.getElementById('view'))
        {
            if(oSpan = document.getElementById('nbFiles'))
            {
                oSpan.innerHTML = Number(oSpan.innerHTML) - 1;        
                if(oSpan.innerHTML == 0)
                {
                    if(oEmpty = document.getElementById('noFiles'))
                    {
                        oEmpty.style.display = '';    
                    }    
                }
            }    
        }    
    }
    
    function splitFileName(sText)
    {
        var arrSplit1 = sText.split("/");
        var sEnd = arrSplit1[arrSplit1.length-1];
        var arrSplit2 = sEnd.split(".");
        
        if(arrSplit2.length > 2)
        {
            var sName = "";
            var sExtension = arrSplit2[arrSplit2.length-1].toLowerCase();
            for(iSplit = 0; iSplit < arrSplit2.length-2; iSplit ++)
            {
                if(sName != "")
                {
                    sName += ".";
                }
                sName += arrSplit2[iSplit];
            }
            var arrReturn = Array(sName,sExtension);
            return arrReturn;
        }
        else
        {
            return arrSplit2;
        }
    }
    
    function getSummaryName(p_filename, p_extension)
    {
        var s_filename = p_filename;
        if(p_filename.length > 10)
        {
            s_filename = p_filename.substr(0,10)+"...";
        }
        return s_filename+"."+p_extension;
    }
    
    function getIcon(p_extension, p_view)
    {
        if(p_view == 'thumbnails')
        {
            switch(p_extension.toLowerCase())
            {
                case "xls":
                    sIcon = "exell3.png";
                    break;
                case "ppt":
                    sIcon = "power_point3.png";
                    break;
                case "pdf":
                    sIcon = "pdf3.png";
                    break;
                case "zip":
                case "rar":
                    sIcon = "zip3.png";
                    break; 
                case "doc":
                    sIcon = "word3.png";
                    break;
                case "mdb":
                    sIcon = "Acces3.png";
                    break;
                case "txt":
                case "rtf":
                    sIcon = "Text3.png";
                    break;
                case "wav":
                case "mp3":
                case "wma":
                    sIcon = "audio3.png";
                    break; 
                case "flv":
                case "mpg":
                case "mpeg":
                case "avi":
                case "mov":
                case "qt":
                    sIcon = "Videos3.png";
                    break;
                default:
                    sIcon = "unknown3.png";
                    break;   
            }
        }
        else if(p_view == 'list')
        {
            switch(p_extension.toLowerCase())
            {
                case "png":
                case "gif":
                case "bmp":
                case "jpg":
                case "jpeg":
                    sIcon = "image.gif";
                    break;
                case "xls":
                    sIcon = "Excel.gif";
                    break;
                case "ppt":
                    sIcon = "powerpoint.gif";
                    break;
                case "pdf":
                    sIcon = "PDF.gif";
                    break;
                case "zip":
                case "rar":
                    sIcon = "ZIP.gif";
                    break; 
                case "doc":
                    sIcon = "Word.gif";
                    break;
                case "mdb":
                    sIcon = "Access.gif";
                    break;
                case "txt":
                case "rtf":
                    sIcon = "text.gif";
                    break;
                case "wav":
                case "mp3":
                case "wma":
                    sIcon = "audio.gif";
                    break; 
                case "flv":
                case "mpg":
                case "mpeg":
                case "avi":
                case "mov":
                case "qt":
                    sIcon = "video.gif";
                    break;
                default:
                    sIcon = "unknown.gif";
                    break;   
            }
        }
        
        return sIcon;    
    }
    
    function goUpload(vargetgoupload) 
    {
        //alert('goUpload');    
        document.getElementById('nasuploader').goUpload(vargetgoupload);
    }
    
    function Upload_File_Finished(p_error, p_index, p_count) 
    {
        //alert('Upload_File_Finished '+p_error);
        var arrInfo = p_error.split('~');
        window.parent.addUploadedFile(arrInfo[1], arrInfo[2], arrInfo[3], arrInfo[4]);    
        
        if(p_index == p_count)
        {
            window.parent.displayFile();    
        }
    }
    
    function Upload_Finished(param1, param2) 
    {
        //alert('Upload_Finished');
    }
    
    function Update_File(file) 
    {
        //alert('Update_File');    
    }
    
    function Reset_Uploader()
    {
        if(oCommand = window.parent.document.getElementById('fileBox_moreCommand'))
        {
            window.parent.initFileCommand(oCommand);
        }
    }
    
    function addUploadedFile(p_path, p_file, p_size, p_date)
    {
        if(oForm = document.getElementById('uploadedForm'))
        {
            iMoved = oForm.fileMoved.value;
            
            var oPath = document.createElement('input');
            oPath.id = 'pathUploaded_'+iMoved;
            oPath.name = 'pathUploaded_'+iMoved;
            oPath.type = 'hidden';
            oPath.value = p_path;
            
            var oFile = document.createElement('input');
            oFile.id = 'fileUploaded_'+iMoved;
            oFile.name = 'fileUploaded_'+iMoved;
            oFile.type = 'hidden';
            oFile.value = p_file;
            
            var oSize = document.createElement('input');
            oSize.id = 'sizeUploaded_'+iMoved;
            oSize.name = 'sizeUploaded_'+iMoved;
            oSize.type = 'hidden';
            oSize.value = p_size;
            
            var oDate = document.createElement('input');
            oDate.id = 'dateUploaded_'+iMoved;
            oDate.name = 'dateUploaded_'+iMoved;
            oDate.type = 'hidden';
            oDate.value = p_date;
            
            oForm.appendChild(oPath);
            oForm.appendChild(oFile);
            oForm.appendChild(oSize);
            oForm.appendChild(oDate);
            
            oForm.fileMoved.value = Number(iMoved) + 1;
        }        
    }
