var JPath = "";
var theUniqueID = (new Date()).getTime() % 1000000000;
var gQuery;

function Upload_MultiFileSave(FORM) {
  //Reset the Query ID
  theUniqueID = (new Date()).getTime() % 1000000000;
  
  FORM.target = "RemoveFrame";
  FORM.ID.value = theUniqueID;
  FORM.action = JPath + 'Upload/SaveToDisk.asp?ID=' + theUniqueID;  
  MyWin = window.open(JPath + 'Upload/ProgressBar.asp?ID=' + theUniqueID, 'MyWin', 'width=500,height=200');  
  MyWin.focus();
  FORM.submit();
  MyWin.focus();
}

function Upload(FormName, ListBoxName, Ext, MaxSize, DestDir, ExtraParam) {
  var URL = JPath + "Upload/Upload_Index.asp";
  //var FRM = document.forms[FormName];
  //var ListBox = document.forms[FormName].all[ListBox];
  URL = URL + "?" +
  "Form=" + FormName + "&" +
  "LB=" + ListBoxName + "&" +
  "Ext=" + Ext + "&" +
  "Max=" + MaxSize + "&" +
  "DestDir=" + DestDir.replace(/\//g, '~');
  if(ExtraParam) URL = URL + '&' + ExtraParam;
  uc_OpenFiles(URL);
  
}

function SelectAll(FormName, ListBoxName) {
  var ListBox = document.forms[FormName].all[ListBoxName];
  for(i=0; i< ListBox.options.length; i++) {
    ListBox.options[i].selected = true;
    //window.alert(ListBox.options[i].selected);
  }
}

function Remove(FormName, ListBoxName) {
  if(!window.confirm("The file will be deleted and will not be available\n" +
  "even if you cancel the form.\n\n" +
  "Are you sure?")) return false;

  var ListBox = document.forms[FormName].all[ListBoxName];

  //If no file is selected, ignore it
  var sIndex  = ListBox.selectedIndex;
  if (sIndex < 0) return false;

  //if value doesn't contain any valid filenames,
  //show the error and exit.
  var sOption = ListBox.options[ListBox.selectedIndex];
  if(sOption.value == "") {
    RemoveFromList(FormName, ListBoxName);
    //window.alert('Can\'t delete this file. Please contact administrator');
    return false;
  }

  var URL = '../Upload/Remove.asp' +
  '?FormName=' + FormName +
  '&LB=' + ListBoxName +
  '&FileName=' + sOption.value;

  RemoveFrame.document.location.href = URL;
}



function DoUpload(Query) {
  //var theFeats = TopLeft(550,200) + "status=no,resizable=no,scrollbars=no"
  //window.open("progressbar.asp?ID=" + theUniqueID, theUniqueID, theFeats);

  var File = document.FileUploadForm.FileUpload.value;
  if(File == '') {
    window.alert('Please select a file to upload');
    return false;
  }
  var myQuery = Query.toUpperCase()
  if(myQuery.indexOf('ID=') < 0) {
    Query = 'ID=' + theUniqueID + '&' + Query
  }
  gQuery = Query;    
  document.FileUploadForm.action = "SaveToDisk.asp?" + Query;
  document.FileUploadForm.submit();

  window.setTimeout('OpenProgress()', 50);
  return true;
}

function OpenProgress() {
  var uQuery = gQuery.toUpperCase();
  var myQuery = gQuery;
  if(uQuery.indexOf('ID=') < 0) {
    myQuery = 'ID=' + theUniqueID + '&' + myQuery
  }
  document.location.href="progressbar.asp?" + myQuery
}

/*-------------------------------------------------
Common functions for open a window with the URL
and set up its position in the center of the screen
-------------------------------------------------*/
function uc_TopLeft(Width, Height) {
  var winWidth, winHeight;
  winWidth = (screen.availwidth - Width)/2;
  winHeight = (screen.availheight  - Height)/2;
  if(isNaN(winWidth)) winWidth = 200;
  if(isNaN(winHeight)) winHeight = 200;
  var T =
  "top="    + winHeight + "," +
  "left="   + winWidth  + "," +
  "width="  + Width     + "," +
  "height=" + Height  
  return T;
}
function uc_OpenFiles(URL) {
  var MyWindow = window.open(URL, 'MyWindow', uc_TopLeft(550,200) + ",status=yes,resizable=yes,scrollbars=yes");
  if(MyWindow) MyWindow.focus();
}

/* function to show the error upload screen */
function UploadError(Query) {
  top.frames['main'].location.href = 'UploadError.asp?' + Query;
}
