       # implement progress indicator
       $(e.target).siblings('.progress').find('.document-progress-bar').css('width', Math.round((data.loaded * 100.0) / data.total) + '%')
 
-  docIdx = 0
   $('.cloudinary-fileupload.survey-document-upload').bind 'cloudinarydone', (e, data) ->
-    #callback = ->
+    # Im setting the upload's index based on the count of existing attachements
+    docCount = $(e.target).closest('.file-upload-input-button').find(".document-preview, .upload-view-mode").length
+    if docCount == 0
+      docIdx = 1
+    else
+      docIdx = docCount + 1
+
+    console.log docIdx + " document"
+
     $(e.target).siblings('.progress').find('.document-progress-bar').removeAttr("style")
     $(e.target).siblings('.progress').addClass('hidden')
-    #setTimeout callback, 1000
 
     # this if statement sets the pdf file's extension to png for a preview in upload
     # publicId = data.result.public_id

      # implement progress indicator
      $(e.target).siblings('.progress').find(".photo-progress-bar").css('width', Math.round((data.loaded * 100.0) / data.total) + '%')

  photoIdx = 0
  $('.cloudinary-fileupload.survey-photo-upload').bind 'cloudinarydone', (e, data) ->
    #callback = ->
    $(e.target).siblings('.progress').find('.photo-progress-bar').removeAttr("style")
    $(e.target).siblings('.progress').addClass('hidden')
    #setTimeout callback, 1000

    $photoPreviewContainer = $(e.target).siblings('.photo-preview-container')
    $photoPreviewContainer.append "<div class='photo-preview'>" + $.cloudinary.image(data.result.public_id, format: data.result.format, version: data.result.version, crop: 'fill', width: 350).prop('outerHTML') + "</div>"
    addTexareaForUpload("photo", data, photoIdx, $photoPreviewContainer)

    photoIdx += 1

  # handle errors
  $('.cloudinary-fileupload.survey-photo-upload').bind 'fileuploadfail', (e, data) ->

  $previewContainer.find("."+file+"-preview").last().append "<br>"
  $previewContainer.find("."+file+"-preview").last().append "<hr>"

  # this code is setting the sort_order every time we upload. It takes care of both scenearios, survey new ands survey edit
  $($previewContainer).closest('.file-upload-input-button').find("."+file+"-preview, .upload-view-mode:visible").each (idx, element) ->
    $(element).find('.upload-sort-order').val(idx+1)

