
// Ã·ºÎÆÄÀÏ ¾÷·Îµå Ã³¸®¿ë Å¸ÀÔ Å¬·¡½º
// flag (»èÁ¦¿©ºÎ): Á¤»ó N, »èÁ¦ D

function CItem(uid, type, folder, sfname, ofname, fsize, flag) { 
  
  this.uid = uid;
  this.type = type;
  this.folder = folder;
  this.sfname = sfname;
  this.ofname = ofname;
  this.fsize = fsize;
  this.flag = flag;

}




var FItem = new Array();

function Attack(type, folder, sfname, ofname, fsize) {

	var f = document.bform1;
	var len = FItem.length;

   FItem[len] = new CItem('', type, folder, sfname, ofname, fsize, 'N');

	addList(f.attack, ofname, len);

   show_file_size();

}



function AttackUp(uid, folder, sfname, ofname, fsize) {

	var f = document.bform1;
	var len = FItem.length;

   FItem[len] = new CItem(uid, 'F', folder, sfname, ofname, fsize, 'U');

	addList(f.attack, ofname, len);

}



function attach_delete(cnt) {
  
  var f = document.bform1;
  var idx = f.attack.selectedIndex;
	
  if (idx < 0) {
      window.alert('»èÁ¦ÇÒ ÆÄÀÏÀ» ¼±ÅÃÇØ ÁÖ¼¼¿ä.');
	   return;
  }
  
  var n = eval(f.attack.options[idx].value);

  removeList(f.attack, idx);

  FItem[n].flag = "D";

  cal_upload_limit (cnt);

  show_file_size();

}



function show_file_size () {

  var totalsize = 0;

  for (var i=0; i < FItem.length; i++) {
    if (FItem[i].flag == 'N' || FItem[i].flag == 'U') totalsize += eval(FItem[i].fsize);
  }

  document.getElementById("attack_size").innerHTML = show_FileSize(totalsize);

}



// Á¦ÇÑ°¹¼ö ÀÌ»ó ¾÷·Îµå½Ã ¾÷·Îµå ÀÔ·ÂÃ¢ Á¦°Å

function cal_upload_limit (limit) {

  var cnt = 0;

  for (var i=0; i < FItem.length; i++) {
	 if (FItem[i].flag == 'N' || FItem[i].flag == 'U') cnt = cnt + 1;
  }

  if (cnt >= limit) document.getElementById("myupload").style.display = 'none';
  else document.getElementById("myupload").style.display = 'block';

}



function addList(target, text, value){

  var newOpt = document.createElement("OPTION");
  newOpt.text = text;	
  newOpt.value = value;
  target.options.add(newOpt);

}



function removeList(target, i) {
  target.remove(i);
}



// Ã·ºÎÆÄÀÏ °¹¼ö _ »èÁ¦´Â Á¦¿Ü

function upload_file_cnt () {

  var cnt = 0;

  if (FItem.length > 0) {

		for (var i=0; i < FItem.length; i++) {
        if (FItem[i].flag == "N" || FItem[i].flag == "U") cnt = cnt + 1;
		}

  }

  return cnt;

}



// ¿ë·®Ç¥½Ã

function show_FileSize (fsize) {
  
  var result = 0;

  if (eval(fsize) > 0) {
      if (eval(fsize) > 1024000) {
			 result = parseInt(Math.round((eval(fsize / 1024000) * 1000) / 1000, 2)) + " MB";
      }
		else if (eval(fsize) > 1024) {
			result = parseInt(Math.round((eval(fsize / 1024) * 10) / 10, 2)) + " KB";
      }
		else {
			result = fsize + " Byte";
      }
  }
  else {
      result = "0 Byte";
  }

  return result;

}




// htmlarea »ç¿ëÇÏ´Â Æû¿¡¼­ ¾÷·ÎµåµÈ ÀÌ¹ÌÁö¸¦ ÅØ½ºÆ®¹Ú½º¿¡ ´ã¾Æ¼­ ¾×¼Ç ÆäÀÌÁö·Î Àü¼ÛÇÑ´Ù.

function get_image (objname) {

  var myimage = document.getElementById("id"+objname).contentWindow.document.images;

  for (i=0; i < myimage.length; i++) {

	 var input = document.createElement('input');
	 input.type = "hidden";
	 input.name = "myimges[]";
	 input.value = myimage[i].src;

	 document.bform1.appendChild(input);

  } 

}




// Ã·ºÎÆÄÀÏ Á¤º¸ Àü¼ÛÀ§ÇØ ÅØ½ºÆ®¹Ú½º¿¡ ´ã¾Æ¼­ ¾×¼Ç ÆäÀÌÁö·Î Àü¼ÛÇÑ´Ù.

function get_upload_files () {

  if (FItem.length > 0) {

		for (var i=0; i < FItem.length; i++) {

		  var input = document.createElement('input');
		  input.type = 'hidden';
		  input.name = 'up_uid';
		  input.value = FItem[i].uid;
		  document.bform1.appendChild(input);

		  var input = document.createElement('input');
		  input.type = 'hidden';
		  input.name = 'up_type';
		  input.value = FItem[i].type;
		  document.bform1.appendChild(input);

		  var input = document.createElement('input');
		  input.type = 'hidden';
		  input.name = 'up_folder';
		  input.value = FItem[i].folder;
		  document.bform1.appendChild(input);

		  var input = document.createElement('input');
		  input.type = 'hidden';
		  input.name = 'up_sfname';
		  input.value = FItem[i].sfname;
		  document.bform1.appendChild(input);

		  var input = document.createElement('input');
		  input.type = 'hidden';
		  input.name = 'up_ofname';
		  input.value = FItem[i].ofname;
		  document.bform1.appendChild(input);

		  var input = document.createElement('input');
		  input.type = 'hidden';
		  input.name = 'up_fsize';
		  input.value = FItem[i].fsize;
		  document.bform1.appendChild(input);

		  var input = document.createElement('input');
		  input.type = 'hidden';
		  input.name = 'up_flag';
		  input.value = FItem[i].flag;
		  document.bform1.appendChild(input);

		}  //  for (var i=0; i

  }  //  if (FItem.length > 0) {

}





// ¾÷·Îµå ÁøÇà»óÅÂ Á¤º¸ °¡Á®¿À±â

function getProgressInfo(progressID, tawidth) {

  var params = "progressID=" + progressID + "&tawidth=" + tawidth;

  new ajax.xhr.Request("/ajax_script/resp_upload_progress.php", params, dispProgress, "POST");

}



// ¾÷·Îµå ÁøÇà¹Ù Ãâ·Â

function dispProgress(req) {

  if (req.readyState == 4) {
	   if (req.status == 200) {

		   var docXML = req.responseXML;
			 
		   var progress = {
			   'id'            : docXML.getElementsByTagName("id")[0].firstChild.nodeValue,
			   'percent'       : docXML.getElementsByTagName("percent")[0].firstChild.nodeValue,
			   'transferBytes' : docXML.getElementsByTagName("transferBytes")[0].firstChild.nodeValue,
			   'totalBytes'    : docXML.getElementsByTagName("totalBytes")[0].firstChild.nodeValue,
			   'bytesPer'      : docXML.getElementsByTagName("bytesPer")[0].firstChild.nodeValue,
			   'bolWorking'    : docXML.getElementsByTagName("bolWorking")[0].firstChild.nodeValue,
			   'tawidth'       : docXML.getElementsByTagName("tawidth")[0].firstChild.nodeValue
		   };

           if (progress.percent < 100) {   // ¾÷·Îµå ÁßÀÎ°æ¿ì

			   setTimeout (
				 function() {
				   document.getElementById('show_wait').innerHTML = makeProgress(progress);						
				   getProgressInfo(progress.id, progress.tawidth);
				 }, 500
			   );
           }
		   else {
			   document.getElementById('show_wait').innerHTML = '<b><font size=2 color=red>¾÷·Îµå°¡ ¿Ï·á µÇ¾ú½À´Ï´Ù.</font><b>';  
			   show_wait_hide();
		   }
 
      }
  }

}



// ÁøÇà¹Ù, @param progress: ÁøÇà»óÈ² Á¤º¸  

function makeProgress(progress) {

  var str = '<div style="width:' + progress.tawidth + 'px;">'          
	 	  + '<div style="width:' + progress.tawidth + 'px; border:1px solid #000000;">'          
          + '<div align="left" style="width:'+ progress.percent +'%;height:20px; background-color:#336699;"></div>'
		  + '<div style="width:' + progress.tawidth + 'px;position:absolute;float:left;text-align:center;">'
          + ' <span style="background-color:#ffffff;font-size:12px;">'+ progress.percent +' %</span>'
          + '</div>'          
		  + '</div>'
          + '<div style="text-align:center;"><span style="font-size:11px;">'+ progress.transferBytes +' KB</span></div>'
          + '</div>';

  return str;

}
