var progress_status="";
var progress_timer;
function update_progress(stat,html){
  progress_status=stat;
  var obj=document.getElementById('PROGRESS_BAR');
  window.scroll(0,0);  
  obj.style.visibility='visible';
  obj.innerHTML='';
  obj.innerHTML=html;
  if(stat!="done")  progress_timer=setTimeout('progress()',1000);  
}
function stop_upload(){
  if(confirm("Please wait while your file is being transferred. Would you like to end this file transfer session? Click the OK button to end the file transfer session, or click the Cancel button to return to the browser window.")){   
    clearTimeout(progress_timer);
    document.body.onclick="";
    hide('PROGRESS_BAR');
    frames['PROGRESS_BAR_FRAME'].location.href="blank.html";
    frames['LOADIN_FRAME'].location.href="blank.html";   
    return true;
  }
  return false;
}
function uploadDone(){
    clearTimeout(progress_timer);
    document.body.onclick="";
    hide('PROGRESS_BAR');
    frames['PROGRESS_BAR_FRAME'].location.href="blank.html";
    frames['LOADIN_FRAME'].location.href="blank.html";   
}
function progress(){
  document.body.onclick=stop_upload;
  frames['PROGRESS_BAR_FRAME'].location.href="progress2.iml?template_name=<iml_display template_name>&site_name=<iml_display -a Shop_Name>&status="+progress_status;
}

var ProgressBarList=new Array();

function ProgressBar(bar,stat){
  this.index=ProgressBarList.length;
  ProgressBarList[this.index]=this;
  this.totalBytes=1;
  this.uploadedBytes=0;
  this.statusEl=stat;
  this.progressEl=bar;
  this.waiting=1;
  this.done=0;
  this.waitTimeout=10;
  this.abortProgress=false;
  var instance=this;
//  this.timeOut=window.setTimeout("ProgressBarList["+this.index+"].refresh('');",500);
  ajaxCallFunction('ajax.iml?mdl=progress.aj&'+Date(),function(sc,st,rt,rxml){ instance.update(rxml); });
}

ProgressBar.prototype.abort=function(){
  this.abortProgress=true;
  window.clearTimeout(this.timeOut);
}
ProgressBar.prototype.update=function(rxml){
   if(this.abortProgress) return;
   var status=getTagValue(rxml,'status');
   if(status=='wait'){
     this.waiting=1;
     this.waitTimeout--;
     this.done=0;
     if(this.statusEl) this.statusEl.innerHTML="Waiting...";
     if(this.progressEl) this.progressEl.style.width="0px";
   }else if(status=='done'){
     this.waiting=0; this.done=1;
     if(this.statusEl) this.statusEl.innerHTML="Done!";
     if(this.progressEl) this.progressEl.style.width="100%";
   }else{
     this.totalBytes=getTagValue(rxml,'total')*1;
     this.uploadedBytes=getTagValue(rxml,'current')*1;
     if(this.totalBytes==0)this.totalBytes=1;
     if(this.uploadedBytes>this.totalBytes) this.uploadedBytes=this.totalBytes;     
     var percent=Number(this.uploadedBytes/this.totalBytes*100).toFixed(1);
     if(this.statusEl){
        var dis=this.totalBytes;
        var suf="Bytes";
        if(dis>1024){
          dis=Number(dis/1024).toFixed(1);
          suf="KB";          
        }
        if(dis>1024){
          dis=Number(dis/1024).toFixed(1);
          suf="MB";          
        }
        this.statusEl.innerHTML=percent+"% of "+dis+" "+suf;
     }
     percent=Number(percent).toFixed();
     if(this.progressEl) this.progressEl.style.width=percent+"%";
   }
   if(!this.done&&this.waitTimeout>0){
      this.timeOut=window.setTimeout("ProgressBarList["+this.index+"].refresh('"+status+"');",500);
  //  this.refresh(status);
   }  
}

ProgressBar.prototype.refresh=function(status){
   var instance=this;
   ajaxCallFunction('ajax.iml?mdl=progress.aj&status='+status+'&'+Date(),function(sc,st,rt,rxml){ instance.update(rxml); });
}



