/* Filename: Upload.js
 * Purpose: Hold UploadForm, UploadBar static function set
 */

function UploadForm(){}
function UploadBar(){}


// Set these to values of your liking:
UploadForm.containerDivId  = 'form_container_div'; // This div should exist
UploadForm.formDivPrefix = 'div_form';
UploadForm.formIdPrefix  = 'the_form';
UploadForm.formAction    = 'upload.php';
UploadForm.formTarget    = 'iframe_upload';
//UploadForm.formTarget    = 'test';
UploadForm.checkStartUploadSleep = 1000; // 1 second

UploadBar.containerDivId   = 'progress_bar_container_div'; // This div should exist
UploadBar.barDivPrefix     = 'div_bar';
UploadBar.recheckSleep     = 1000; // 1 second
UploadBar.progressColor    = '#00007F';

// Leave the rest alone
UploadForm.formCount        = 0;
UploadForm.queuePointer     = 1;

// Hides existing file picker's DIV/FORM and creates a new DIV/FORM 
// for the next upload.
// @returns void
UploadForm.createNewForm = function()
{
  if ( UploadForm.formCount > 0 )
    document.getElementById(UploadForm.formIdPrefix+UploadForm.formCount).style.display = 'none';
  UploadForm.formCount++;
  var tempDiv = document.createElement('div');
  tempDiv.setAttribute('id', UploadForm.formDivPrefix+UploadForm.formCount);
  document.getElementById(UploadForm.containerDivId).appendChild( tempDiv );

  document.getElementById(UploadForm.formDivPrefix+UploadForm.formCount).innerHTML += '<form id="'+UploadForm.formIdPrefix+UploadForm.formCount+'" action="'+UploadForm.formAction+'" target="'+UploadForm.formTarget+'" method="POST" enctype="multipart/form-data"><input type="hidden" name="UPLOAD_METTER_ID"><input type="file" name="filename" onchange="UploadForm.queueUpload();"></form>';
}

// Puts the currently active form in queue for upload.   
// @requires UploadBar
// @returns void
UploadForm.queueUpload = function()
{
  var upload_metter_id = Math.floor(Math.random()*1999999999);
  var form = document.getElementById(UploadForm.formIdPrefix+UploadForm.formCount); 
  var filename = form.filename.value;
  if ( filename.lastIndexOf("\\") > -1 )
    filename = filename.substring(filename.lastIndexOf("\\")+1, filename.length);
  else if ( filename.lastIndexOf("/") > -1 )
    filename = filename.substring(filename.lastIndexOf("/")+1, filename.length);
  filename = filename.replace(/'/g, ""); // strip single quotes
  form.UPLOAD_METTER_ID.value = upload_metter_id;
  UploadBar.createNewDiv(UploadForm.formCount, filename);
  UploadForm.checkStartUpload(UploadForm.formCount, filename);
  UploadForm.createNewForm();
}

// Checks if it's time to start this upload, if so, submit the form.
// otherwise, sleep, then check again.
UploadForm.checkStartUpload = function(formNum, filename)
{
  if ( UploadForm.queuePointer == formNum )
  {
    document.getElementById(UploadForm.formIdPrefix+formNum).submit();
    UploadBar.startProgressBar(formNum, filename);
  }
  else
  {
    setTimeout("UploadForm.checkStartUpload("+formNum+", '"+filename+"')", UploadForm.checkStartUploadSleep);
  }
}

/******************************************************************************************/
/************************************UploadBar functions************************************/
/******************************************************************************************/

// Create a new DIV to display the upload progress in:
UploadBar.createNewDiv = function(divNum, filename)
{
  var tempDiv=document.createElement('div');
  tempDiv.setAttribute('id',UploadBar.barDivPrefix+divNum);
  tempDiv.style.width='100%';
  document.getElementById(UploadBar.containerDivId).appendChild(tempDiv);
  var div =document.getElementById(UploadBar.barDivPrefix+divNum);
  div.innerHTML = 'Queued for upload: '+filename;
}

UploadBar.startProgressBar = function(divNum, filename)
{
  var form = document.getElementById(UploadForm.formIdPrefix+divNum);
  var upload_metter_id = form.UPLOAD_METTER_ID.value;
  xajax_fetchProgress(upload_metter_id, divNum, filename);
}

UploadBar.drawProgress = function(divNum, filename, total, upl, eta, speed)
{
  var str = '';
  var color = '';
  var width = 0;
  if ( upl > total ) upl = total;
  var percent = total>0 ? Math.round(upl/total*100*10)/10 : 0;
  str += '<table border=1 WIDTH="100%" cellpadding=0 cellspacing=0 style="BORDER-BOTTOM: 0px inset; BORDER-LEFT: 0px inset; BORDER-RIGHT: 0px inset; BORDER-TOP: 0px inset"><tr><td><table border=0 WIDTH="100%" COLS="34"><tr>';
  for (var i=0; i<100; i+=3)
  {
    color = (i<percent) ? " bgcolor='"+UploadBar.progressColor+"' " : '';
    width = (i+3<100) ? 3 : 100-i;
    str += '<td '+color+' style="font-size: 1px;" WIDTH="'+width+'%">&nbsp;</td>';
  }
  str += '</tr></table></td></tr></table>';
  if ( eta != 'Finished' )
    eta += ' remaining';
  str += '<table width=100%><tr><td>'+filename+', '+eta+' ('+speed+'KB/sec)</td><td align=right>'+UploadBar.nice_value(upl)+'/'+UploadBar.nice_value(total)+' ('+percent+'%)</td></tr></table>';
  document.getElementById(UploadBar.barDivPrefix+divNum).innerHTML = str;
  if ( upl < total )
    setTimeout("UploadBar.startProgressBar("+divNum+", '"+filename+"')", UploadBar.recheckSleep);
  else
    UploadForm.queuePointer++;
}

UploadBar.nice_value = function( x )
{
  if (x < 100)  x;
  if (x < 1024*10)   return Math.round((x/1024)*100)/100 + "KB"; //  sprintf("%.2fKB", x/1000);
  if (x < 1024*1024) return Math.round(x/1000) + "KB"; // sprintf("%dKB", x/1000);
  return Math.round((x/1024/1024)*100)/100 + "MB"; // sprintf("%.2fMB", x/1000/1000);
}

