/* 
 * File to handle adding attachments on the page.
 */
var newAttachmentNumberToAdd = new Array();

function getNewAttachmentNumber(){
    // Set the result to zero for now
    var result = 0;
    
   // Loop through the result numbers to get the highest
   for (var loopAttachmentNumber in newAttachmentNumberToAdd){
       // Determine if the number is higher than the current number
       if (newAttachmentNumberToAdd[loopAttachmentNumber]>result){
           // make this the new high
           result = newAttachmentNumberToAdd[loopAttachmentNumber];
       }
   }
   
    // Return the highest result
    return result;
}

function removeNewAttachmentNumber(addressNumberToRemove){
    // Set the removal item to zero for now
    var addressLocationToRemove = -1;
    
   // Loop through the result numbers to get the highest
   for (var loopAttachmentNumber in newAttachmentNumberToAdd){
       // Determine if the number is higher than the current number
       if (newAttachmentNumberToAdd[loopAttachmentNumber]===addressNumberToRemove){
           // set this as the one to remove
           addressLocationToRemove = loopAttachmentNumber;
           
           // Break out of the list
           break;
       }
   }
   
   // Remove it from the list
   newAttachmentNumberToAdd.splice(addressLocationToRemove,1);
}

function createAttachmentNumberString(){
    // Set the result to zero for now
    var result = '';
    
   // Loop through the result numbers
   for (var loopAttachmentNumber in newAttachmentNumberToAdd){
       // Add it to the string
       result = result+newAttachmentNumberToAdd[loopAttachmentNumber].toString()+'|';
   }

    // Remove the last comma
    result=result.slice(0, (result.length-1));
    
    // Return the highest result
    return result;
}

function addNewAttachment() {
    console.log("in addAttachment");

    // Increment the amount that is currently in the array
    var newAttachmentAmountID=getNewAttachmentNumber()+1;
    
    // Create the attachment div
    var attachmentsDiv = document.createElement("div");
    attachmentsDiv.class = 'Table';
    attachmentsDiv.id = 'newAttachment' + newAttachmentAmountID;
    attachmentsDiv.name = 'newAttachment' + newAttachmentAmountID;

    // Create a div to hold the attachments
    var attachmentsLineDiv = document.createElement("div");
    attachmentsLineDiv.class = 'Row';
    attachmentsLineDiv.id = 'typeLineDiv' + newAttachmentAmountID;
    attachmentsLineDiv.name = 'typeLineDiv' + newAttachmentAmountID;

    // Add the attachment row to the attachment div
    attachmentsDiv.appendChild(attachmentsLineDiv);
    
    // Create the attachment 1 label
    var attachmentLabel = document.createElement("label");
    attachmentLabel.class = 'cell mainLabelCell';
    attachmentLabel.textContent="New Attachment: ";
    attachmentLabel.id = 'attachmentLabel' + newAttachmentAmountID;    

    // Add the attachment label to the attachment label div
    attachmentsLineDiv.appendChild(attachmentLabel);
    
    var attachmentText = document.createElement("input");
    attachmentText.class = 'cell fileInputCell';
    attachmentText.type="file";
    attachmentText.disabled = false;
    attachmentText.id = 'attachmentElement' + newAttachmentAmountID;    
    attachmentText.name = 'attachmentElement' + newAttachmentAmountID;    
    attachmentText.onselect = '';
    attachmentText.onchange = function(a) {  
        return function() {
            // Get the file element
            var fileElement = document.getElementById('attachmentElement' + a);
            
            // Check to see that the name is good or not
            if (doesFileNameHaveBadChars(fileElement.value)){
                // Give an alert that the file name has bad characters
                alert("Bad characters (currently only commas) were detected in the file name. Rename file and try again.");
                
                // Remove the file
                fileElement.files[0]=null;
                fileElement.value='';
            }
            else {
                // Check for the file size
                AlertFilesize(a);
            }
        };
    }(newAttachmentAmountID);

    // Add the attachment to the attachment line div
    attachmentsLineDiv.appendChild(attachmentText);
    
    // Create the remove attachment button
    var removeBtn = document.createElement("button");
    attachmentText.class = 'cell buttonCell';
    removeBtn.textContent="Remove Attachment";
    removeBtn.id = 'outRemoveBtn' + newAttachmentAmountID;    
    removeBtn.style = 'margin: auto 20px; align: right;';
    removeBtn.onclick = function(i) {  
        return function() {
           removeNewAttachment(i);
        };
    }(newAttachmentAmountID);

    // Add elements to the newly created DIV tags
    attachmentsLineDiv.appendChild(removeBtn);

    // Create the oversized text
    var oversized = document.createElement("textarea");
    oversized.id = 'oversized' + newAttachmentAmountID;    
    oversized.name = 'oversized' + newAttachmentAmountID;  
    oversized.style = 'display: none;';
    oversized.value = 0;

    // Add the note label to the note label div
    attachmentsLineDiv.appendChild(oversized);
    
    // Add the new div tag to the main div tag
    var container = document.getElementById("newAttachments");
    container.appendChild(attachmentsDiv);
    
    // Add this ID to the list
    newAttachmentNumberToAdd.push(newAttachmentAmountID);
    
    // Get the element and change the amount of attachments
    var addressAmountDiv = document.getElementById("newAttachmentNumbers");
    addressAmountDiv.value=newAttachmentNumberToAdd.join('|');
}

function AlertFilesize(number){
    // variable to hold the file element
    var fileElement = document.getElementById('attachmentElement' + number);
    var oversized = document.getElementById('oversized' + number);
    var sizeinbytes = 0;
    
//    // Determine if the window supports active x
//    if(window.ActiveXObject){
//        var fso = new ActiveXObject("Scripting.FileSystemObject");
//        filepath = fileElement.value;
//        var thefile = fso.getFile(filepath);
//        sizeinbytes = thefile.size;
//    }else{
        // Get the file size of what they want to upload
        sizeinbytes = fileElement.files[0].size;
//    }

    // get the mas file size from the element
    var maxFileSize = document.getElementById('maxFileSize').value;
    
    // Determine if the size is ok
    if (sizeinbytes>maxFileSize){
        // Let the user know that the file is not going to be taken
        alert( 'The file chosen is ' + getHumanReadableFileSize(sizeinbytes) + 
               '. That is larger than the alloted size of ' + getHumanReadableFileSize(maxFileSize) +
               '. This file will not be uploaded.');
       
        // Set the background to something indicative of what happened
        document.getElementById('newAttachment' + number).style.backgroundColor = 'Red';

        // reset the value to nothing for speeds sake
        fileElement.value="";
        
        // Set the variable to be true for oversized
        oversized.value = 1;
    }
    else {
        // Set the background to something showing it is GTG
        document.getElementById('newAttachment' + number).style.backgroundColor = 'Green';
        
        // Set the variable to be false for oversized
        oversized.value = 0;
    }
}

function removeNewAttachment(attachmentToRemove){
    console.log("in removeNewAttachment");

    // Get the element to remove
    var address = document.getElementById('newAttachment'+attachmentToRemove);

    // Remove it
    address.parentNode.removeChild(address);    
    
    // Remove this from the list
    removeNewAttachmentNumber(attachmentToRemove);
    
    // Determine if the address list is empty
    if (newAttachmentNumberToAdd.length===0){
        // Set it to nothing
        addressAmountDiv.value=0;
    }
    else {
        // Get the element and change the amount of attachments
        var addressAmountDiv = document.getElementById("newAttachmentNumbers");
        addressAmountDiv.value=createAttachmentNumberString();
    }
}

function handleAttachmentClick(cb){
    // Get the hidden field holding the values to remove
    var hiddenElement=document.getElementById('deleteCurrentAttachments');
    
    // Determine if the checkbox is checked
    if (cb.checked){
        // Append the value to the values to be deleted
        hiddenElement.value=hiddenElement.value+cb.value+',';
    }
    else{
        // Remove this value to be deleted
        var value=hiddenElement.value;
        hiddenElement.value=value.replace(cb.value+',', '');
    }
}


