var JPath = "";
var theUniqueID = (new Date()).getTime() % 1000000000;
var gQuery;

function MultiFileUpload_Delete(theFormName, theElement) {
	if(!confirm('Are you sure?')) return;
  var theDoc = document.getElementById('MultiUpload_Table_' + theFormName);  
  for(var i = 0; i <= 10; i ++ ) {
    theElement = theElement.parentNode;
    if(theElement.tagName == 'TR') break;
  }
  if(theElement.tagName == 'TR') {
    theDoc.deleteRow(theElement.rowIndex);
  }  
}

function AddFileList(FormName, ListBoxName, TheName, TheValue, ShowUploadedImage, FullPath, ImageType, MoreInfo) {
  var Box = document.forms[FormName][ListBoxName]
  var MultiUpload = document.getElementById('MultiUpload_Table_' + ListBoxName);
  if(MultiUpload) {
  	var NoFile = document.getElementById('MultiUpload_NoFile_' + ListBoxName);
  	if(NoFile) NoFile.style.display='none';
  	MultiUpload.style.display='block';
  	var Row = MultiUpload.insertRow(MultiUpload.rows.length);
  	var TheCell= Row.insertCell(0)
  	TheCell.innerHTML = 
  	'<a href="#" onclick="MultiFileUpload_Delete(\'' +ListBoxName + '\', this); return false;">' +
  	'<img border="0" alt="Delete this File" src="' + JPath  + 'qBook/icons/delete.gif" style="float:right"></a>' +
  	'<b>' + TheValue + '</b><br>\n' +
  	MoreInfo + 
  	'<input type="hidden" name="' + ListBoxName + '" value="' + TheValue + '">';  	
  } else if(Box && Box.tagName == 'SELECT') {
    var NItem = new Option(TheName, TheValue)
    if(document.all) {
      Box.add (NItem)
      var Index = Box.options.length-1;
      Box.options[Index].value = TheValue;      
    } else {
      Box.appendChild(NItem);
    }
  } else {
    Box.value = TheName;
  }  
}


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;  
  //window.alert(FORM.action);
  MyWin = window.open(JPath + 'Upload/ProgressBar.asp?ID=' + theUniqueID, 'MyWin', 'width=500,height=200,resizable=no');  
  if(MyWin) {
    MyWin.focus();
    FORM.submit();
    MyWin.focus();
  } else {
    window.alert('can not continue because of the popups are blocked');
  }
}

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,scrollbars=no,resizable=0");
  if(MyWindow) MyWindow.focus();
}

/* function to show the error upload screen */
function UploadError(Query) {
  top.frames['main'].location.href = 'UploadError.asp?' + Query;
}
