﻿function l_addAttachItem(filename, path) {

    if (objAttachData.value.indexOf(path) != -1) {
        return;
    }

    var attach = new AttachItem();
    attach.name = filename;
    attach.path = path;

    if (objAttachData.value == "") {
        objAttachData.value = attach.getValue();
    } else {
        objAttachData.value += "||" + attach.getValue();
    }
    addAttachItem(attach);

}

function removeAttachSelect() {


} 
      var slCtl = null;

      //DO NOT FORGET TO REGISTER THIS FUNCTION WITH THE SILVERIGHT CONTROL
      // <param name="onload" value="pluginLoaded" />
      function pluginLoaded(sender) {

          //IMPORTANT: Make sure this is the same ID as the ID in your <OBJECT tag (<object id="MultiFileUploader" etc)
          slCtl = document.getElementById("MultiFileUploader");


          //Register All Files Finished Uploading event
          slCtl.Content.Files.AllFilesFinished = AllFilesFinished;

          //Register single file finished event
          //slCtl.Content.Files.SingleFileUploadFinished = SingleFileFinished;

          //Register Error occurred during uploading event
         // slCtl.Content.Files.ErrorOccurred = ShowErrorDiv;

          //Register MaximumFileSizeReached during selecting files
          //slCtl.Content.Control.MaximumFileSizeReached = ShowMaximumFileSizeDiv;

          slCtl.Content.Files.FileAdded = UpdateFileList;
          slCtl.Content.Files.FileRemoved = UpdateFileList;
          slCtl.Content.Files.StateChanged = UpdateFileList;

          //slCtl.Content.Files.TotalPercentageChanged = UpdateTotalPercentage;

          //Set your custom parameter using javascript
          //This parameter will be available in the webservice and you can use it for your business logic
          //Or use it to identity the upload to a sinle row in your database
          //slCtl.Content.Files.CustomParams = "custom_id=1"; 
      }


      function ShowNumberOfFilesUploaded() {
          if (slCtl != null) {
              alert("Total Files Uploaded: " + slCtl.Content.Files.TotalUploadedFiles);
          }
      }

      function ShowTotalNumberOfFilesSelected() {
          if (slCtl != null) {
              alert("Total Files Selected: " + slCtl.Content.Files.TotalFilesSelected);
          }
      }

      function ShowUploadProgress() {
          if (slCtl != null) {
              alert("Progress: " + slCtl.Content.Files.Percentage);
          }
      }



      //This function is registred in the pluginLoaded function (slCtl.Content.Files.AllFilesFinished = AllFilesFinished;)
      function AllFilesFinished() {
         // document.getElementById('AllFinishedDiv').style.display = 'block';
      }

      //This function is registred in the pluginLoaded function (slCtl.Content.Files.SingleFileUploadFinished = SingleFileFinished;)
      function SingleFileFinished() {
          //document.getElementById('SingleFileFinishedDiv').style.display = 'block';
      }

      //This function is registred in the pluginLoaded function (slCtl.Content.Files.ErrorOccurred = ShowErrorDiv;)
      function ShowErrorDiv() {
         // document.getElementById('ErrorDiv').style.display = 'block';
      }

      function ShowMaximumFileSizeDiv() {
          //document.getElementById('MaximumFileSizeDiv').style.display = 'block';
      }

      //Draws a list of files with the state of each file
      function UpdateFileList() {
          var list = "<table><tr><td>Name</td><td>Status</td><td></td></tr>";
          var userFile;

          var attachData;

          var i = 0;
          for (i = 0; i < slCtl.Content.Files.FileList.length; i++) {
              userFile = slCtl.Content.Files.FileList[i];
              list += "<tr><td>" + userFile.FileName + "," + userFile.FileGuidID + "</td><td>" + userFile.StateString + "</td><td></td></tr>";

              if (userFile.StateString == "Finished") {
                  l_addAttachItem(userFile.FileName, userFile.FileGuidID.replace("$", "\\").replace("$", "\\"));
              }

          }


         list += "</table>"
         //ClearList();

         // alert(list);
          //document.getElementById('FileList').innerHTML = list;
          //Update the other statistics...
          //UpdateTotalSelected();
         // UpdateTotalPercentage();
          //UpdateTotalUploaded();
      }

      //Updates the number of total files div element
      function UpdateTotalSelected() {
          document.getElementById('TotalSelected').innerHTML = slCtl.Content.Files.TotalFilesSelected + " files selected";
      }

      //Updates the total percentage div element
      function UpdateTotalPercentage() {
          document.getElementById('TotalPercentage').innerHTML = slCtl.Content.Files.Percentage * 100 + "% uploaded";
      }

      //Updates the number of uploaded files div element
      function UpdateTotalUploaded() {
          document.getElementById('TotalUploaded').innerHTML = slCtl.Content.Files.TotalUploadedFiles + " files uploaded";
      }

      //Actions
      function StartUpload() {
          if (slCtl != null) {
              slCtl.Content.Control.StartUpload();
          }
      }

      function ClearList() {
          if (slCtl != null) {
              slCtl.Content.Control.ClearList();
          }
      }

      function RemoveFile(index) {
          if (slCtl != null) {
              slCtl.Content.Control.RemoveAt(index);
          }
      }
 
        function onSilverlightError(sender, args) {

            var appSource = "";
            if (sender != null && sender != 0) {
                appSource = sender.getHost().Source;
            }
            var errorType = args.ErrorType;
            var iErrorCode = args.ErrorCode;

            var errMsg = "Unhandled Error in Silverlight 2 Application " + appSource + "\n";

            errMsg += "Code: " + iErrorCode + "    \n";
            errMsg += "Category: " + errorType + "       \n";
            errMsg += "Message: " + args.ErrorMessage + "     \n";

            if (errorType == "ParserError") {
                errMsg += "File: " + args.xamlFile + "     \n";
                errMsg += "Line: " + args.lineNumber + "     \n";
                errMsg += "Position: " + args.charPosition + "     \n";
            }
            else if (errorType == "RuntimeError") {
                if (args.lineNumber != 0) {
                    errMsg += "Line: " + args.lineNumber + "     \n";
                    errMsg += "Position: " + args.charPosition + "     \n";
                }
                errMsg += "MethodName: " + args.methodName + "     \n";
            }

            throw new Error(errMsg);
        }
    
