/*
@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)
*/

function createBoxConfirmDelete()
{
   if (document.getElementById('boxConfirmDelete') != undefined) return true;
   var a = new jElement();
   
   // create form
   a.theElement = 'form';
   a.setElementOn = 'subcontent-element';
   a.attribute = new Array('id|boxConfirmDelete','name|formConfirmDelete','class|xhr_form std_form','method|POST');
   a.style='';
   a.createElement();
   
   // 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;';
   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|submit','onclick|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('subcontent-element');
   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 - (105 + (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('subcontent-element');
   var chld = document.getElementById('boxConfirmDelete');
   
   prnt.removeChild(chld);
   
   return true;
}
