/**
 * This script checks whether or not selected folder is read only.  If it is, the user is prompted whether they want to upload to /home instead.
 *
 * Then they are forwarded to the upload page
 *
 * author: Simon
 * date: 16.09.2009
 *
 * */

function initUploadDialog()
{
  YAHOO.util.Event.addListener("button_upload", "click", checkROAndForward);
}


function checkROAndForward()
{
  var folderId = $('#folderId').val();

  //if folderId is empty the folder is read only
  if (folderId === "")
  {
    var handleYes = function()
    {
      this.hide();

      //reset the fields to be /
      $('#folderId').val("/");
      $('#folderName').val("/");

      //send the user to the upload page
      window.location = '../secure/upload.jsp?folderId=' + $('#folderId').val() + '&folderName=' + encodeURI($('#folderName').val());
    };

    //hide the dialog
    var handleNo = function()
    {
      this.hide();
    };

    // Ask the user whether they want to upload to home instead
    var readOnlyDialog = new YAHOO.widget.SimpleDialog("si",
    { width: "300px",
      fixedcenter: true,
      visible: false,
      draggable: false,
      close: true,
      text: "That folder is Read Only, would you like to upload to /home instead?",
      icon: YAHOO.widget.SimpleDialog.ICON_HELP,
      constraintoviewport: true,
      buttons: [
        {
          text:"Yes",
          handler:handleYes,
          isDefault:true
        },
        {
          text:"No",
          handler:handleNo
        }
      ]
    });
    readOnlyDialog.setHeader("Folder is Read Only");

    // Render the Dialog
    readOnlyDialog.render("dialog");

    //display the dialog
    readOnlyDialog.show();
  }
  else
  {
    //if folder not read only, upload to there
    window.location = '../secure/upload.jsp?folderId=' + $('#folderId').val() + '&folderName=' + encodeURI($('#folderName').val());
  }
}
