/*
@author     gabri.ns@gmail.com
@version    0.2.1
@copyright  2008

Fungsi Global untuk membuat box confirm delete menggunakan JavaScript.
Untuk menggunakannya kita perlu memodifikasi tombol hapus. Terlebih dulu,
hapus nama class yang mengandung xhr, misalnya 'xhr' sendiri, 'xhr_form', dll.
Kemudian, tambahkan attribute:
---------
onClick='return showBoxConfirmDelete(<id>, <name>, <url>, this);'
---------
dimana,
id    : id dari data yang ingin dihapus
name  : nama yang ingin ditampilkan pada interface boxConfirmDelete
url   : alamat modul yang akan memproses penghapusan data

@Modified by Galih(Galih.xp@gmail.com)
@version    0.3
@copyright  2008
Perubahan terletak di tujuan delete box. Langsung di arahkan
ke id content. Ini berguna untuk peletakan yang dinamic box delete nantinya.

@version 0.3.1
- bugfix mengenai container
- memperbaiki performance

@version 0.4
- menambah unsur dinamis pada posisi layout boxConfirmDeleteUI
  berdasarkan letak tombol delete

@version 0.4.1
- add support untuk firefox (dan IE)

@version 0.4.1 - modified by galih
- menggabungkan dengan lib janime
- masih belum support untuk browser IE
@version 0.4.2
- menentukan auto hight document
- memberikan z-index lebih supaya menutup object yang lain
*/

function createBoxConfirmDelete()
{
   if (document.getElementById('boxConfirmDelete') != undefined) return true;
   var a = new jElement();
    
   width = document.getElementById('body-application').clientWidth;
   //height = document.getElementById('body-application').clientHeight;
   height = (document.documentElement.scrollHeight > document.body.scrollHeight) ? document.documentElement.scrollHeight : document.body.scrollHeight;
   
   a.theElement = 'div';
   a.setElementOn = 'body-application';
   a.attribute = new Array('id|bgApplication','name|bgApplication');
   a.style='background:#000000; width:100%;height:'+height+'px; position:absolute;top:0px;left:0px;z-index:999999';
   a.createElement();
   
   anime.element = 'bgApplication';
   anime.alpha(30);
    
   // create form
   a.theElement = 'form';
   a.setElementOn = 'body-application';
   a.attribute = new Array('id|boxConfirmDelete','name|formConfirmDelete','class|xhr_form std_form','method|POST');
   a.style='';
   a.createElement();
      
   anime.element = 'boxConfirmDelete';
   anime.speed=5;
   anime.alpha(0);
   anime.fadeIn();
   
   // add form field
   a.theElement = 'input';
   a.setElementOn = 'boxConfirmDelete';
   a.attribute = new Array('id|inputIdDelete','name|idDelete','type|hidden');
   a.style='';
   a.createElement();
   
   // create UI
   a.theElement = 'div';
   a.setElementOn = 'boxConfirmDelete';
   a.attribute = new Array('id|boxConfirmDeleteUI');
   a.style='position: absolute; top: 200px; left: 115px; padding: 0px 10px 0px 40px; width:300px; height:85px; border: 1px solid #EEEBDD; background-color: #FFFDEF; background-image: url("images/alert.gif"); background-position: 10px 20px; background-repeat: no-repeat;z-index:9999999';
   a.createElement();
   
   // create text
   a.theElement = 'p';
   a.setElementOn = 'boxConfirmDeleteUI';
   a.attribute = new Array('id|boxConfirmDeleteText');
   a.style='font-family:Arial, monospace;font-size:14px;';
   a.createElement();
   document.getElementById('boxConfirmDeleteText').innerHTML = "Benarkah data dengan nama <span id='boxConfirmDeleteName' style='font-weight: bold'></span> akan dihapus?";
   
   // create toolbar container
   a.theElement = 'div';
   a.setElementOn = 'boxConfirmDeleteUI';
   a.attribute = new Array('id|boxConfirmDeleteUIToolbar','class|toolbar');
   a.style='margin-top: -10px';
   a.createElement();
   
   // create Yes button
   a.theElement = 'input';
   a.setElementOn = 'boxConfirmDeleteUIToolbar';
   a.attribute = new Array('class|inputButton','value|   Hapus   ','type|button','onclick|document.getElementById(\'boxConfirmDelete\').onsubmit();javascript: hideBoxConfirmDelete()');
   a.style='';
   a.createElement();
   
   //create No button
   a.theElement = 'a';
   a.setElementOn = 'boxConfirmDeleteUIToolbar';
   a.attribute = new Array('id|boxConfirmDeleteNoButton','onclick|javascript: hideBoxConfirmDelete()');
   a.style='';
   a.createElement();
   document.getElementById('boxConfirmDeleteNoButton').innerHTML = " &nbsp; &nbsp;Batal&nbsp; &nbsp; ";
   
   return true;
}

function showBoxConfirmDelete (id, name, url, ndNode)
{
	
   var apply = false;
   if (!document.getElementById("boxConfirmDelete"))
   {
      if (!createBoxConfirmDelete()) return true;
      apply = true;
   }
   
   // calculate position
   prnt = document.getElementById('body-application');
   chld = document.getElementById('boxConfirmDeleteUI');
   
   if (ndNode.nodeName == '#text') ndNode = ndNode.parentNode;
   
   var nTop = 0, nLeft = 0;
   if (ndNode.offsetParent)
   {
      nTop = ndNode.offsetTop;
      nLeft = ndNode.offsetLeft;

      while (ndNode.offsetParent)
      {
         ndNode = ndNode.offsetParent;

         nTop += ndNode.offsetTop;
         nLeft += ndNode.offsetLeft;
      }
   }
   
   posX = (prnt.clientWidth - chld.clientWidth) / 2;
   posY = nTop - (chld.clientHeight / 2);
   
   chld.style.left = posX + 'px';
   chld.style.top = posY + 'px';
   // ---------
   
   document.getElementById("inputIdDelete").value = id;
   document.getElementById("boxConfirmDeleteName").innerHTML = name;
   document.getElementById("boxConfirmDelete").action = url;
   
   if (apply) Behaviour.apply();
   return false;
}

function hideBoxConfirmDelete()
{
   var prnt = document.getElementById('body-application');
   var chld = document.getElementById('boxConfirmDelete');
   var chld1 = document.getElementById('bgApplication');
     
   prnt.removeChild(chld);
   prnt.removeChild(chld1);
	
   return true;
}
