// ___________________________________   [  BoxI.js ]  _______________________________________________

/*
   /!\
       Si 1 boite contient un formulaire mais qu'on l'envoie via une fonction :
       <form doir avoir onsubmit='return false'   ou  onsubmit='return fonction_retourtnant_false()'
*/

document.write( '<s'+"cript src='/js/lib.form.js' type='text/javascript'></s"+'cript>' );
document.write( '<s'+"cript src='/js/Box.Mask.js' type='text/javascript'></s"+'cript>' );
document.write( '<s'+"cript src='/js/Popeye.lib/popeye.dom.js' type='text/javascript'></s"+'cript>' );
document.write( '<s'+"cript src='/js/Popeye.lib/popeye.tools.js' type='text/javascript'></s"+'cript>' );

var BOX = new Object();
    BOX.iRefEC = 1;
    BOX.boxes = Array();

document.write('<style>\
                        .BxP * {font-size:11px; font-family:verdana}\
                        .BxP {z-index:500; cursor:default; }\
                        .BxPBar {-moz-user-select:normal; color:white; cursor:default; background:#1D310A}\
                          .BxPBar td {padding: 4px 2px 4px 2px;}\
                        .BxP_bd {border-top: 1px solid #808080}\
                                .BxP_t {font: bold 13px verdana; padding-right:6px}\
                                .BxP_x {cursor:pointer; vertical-align:middle}\
                        .BxP_in {padding:4px}\
                        .BxPBut {background:#F0F0F0 url(/images/button.bg.gif) repeat-x scroll 0%; padding: 2px 3px; margin:0px 4px; font-weight:bold; cursor:pointer}\
               </style>\
               ');

function blur_all()
{
         var a = document.createElement('A');
             with(a.style) { position='absolute', left = document.body.scrollLeft; top = document.body.scrollTop; }
             document.body.appendChild(a);
             a.focus(); a.blur();
         removeElement(a);
}


function display_elt( el, is )
{
         el.style.display = (is)? '' : 'none';
         if(is)
         {
             var _y = parseInt( Math.max( is['top']  || is['y'] || 0, 0) );
             var _x = parseInt( Math.max( is['left'] || is['x'] || 0, 0) );
             with(el.style) { top = _y; left = _x;  visibility='visible';}
         }
         return el;
}

function boxi_button_att( color )
{        return "color='"+color+"' style='font: normal 11px verdana; border:1px solid "+color+"; color:"+color+"'";
}

// NB : scrollBox doit avoir 1 margin de 0px, sinon IE créé une bande blanche sortie de nulle part lors d'un appendChild d'une <FORM>
document.write("\
  <DIV id='BxPModel' class='BxP' moveable='1' style='display:none; position:absolute; overflow:auto' >\
  <TABLE cellSpacing='0' cellPadding='0' resizable='1' style=' border: 1px solid #1D310A; background:#FFFFFF'>\
     <TR onSelectStart='return false' class='BxPBar' onMouseDown='box_move_MDown(event,this)' onDblClick='BoxI_Kill(this)'>\
         <td class='BxP_t'>titre</td><td align='right'><img src='/img/pics/BoxI/xclose.gif' onClick='BoxI_Kill(this)' class='BxP_x' /></td>\
     </TR>\
     <TR>\
        <TD colSpan='2' style='padding:0px; baground:none'>\
            <DIV class='BxP_bd'>\
              <DIV scrollBox='1' style='border:none'>\
                 <DIV class='BxP_in'>\
                 </DIV>\
              </DIV>\
            </DIV>\
        </TD>\
     </TR>\
  </TABLE>\
  </DIV>"
);


document.write("\
            <DIV id='BoxIButsModel:DIV' style='display:none'>\
            <DIV butsBox='1' style='text-align:center; padding:8px; border:none'>\
            </DIV>\
            </DIV>"
);

document.write("\
            <DIV id='BoxIButsModel:TABLE' style='display:none'>\
            <table butsBox='1' width='100%' border='0' cellSpacing='0' cellPadding='0'>\
              <tr>\
               <td align='center' vAlign='center' style='padding:8px'>\
                </td>\
                <td onMouseDown='box_rsize_MDown(event,this)' style='width:13px; background-image:url(/img/pics/BoxI/resize_corner_bleu.gif); background-position: bottom right; background-repeat:no-repeat; cursor:se-resize' Align='right'>\
                </td>\
              </tr>\
            </table>\
            </DIV>"
);


document.write("\
            <DIV id='BoxIButsModel:buts' style='display:none'>\
                  <font onClick='box_bClick(this,0)' actif='1' isBoxIBut='1' class='BxPBut' "+boxi_button_att('#C60000')+" >Annuler</font>\
                  <font onClick='box_bClick(this,1)' actif='1' isBoxIBut='1' class='BxPBut' "+boxi_button_att('#00A600')+">Valider</font>\
            </DIV>"
);




function getBoxI( ref )
{
         if( ref instanceof BoxI ) return ref;

         if( typeof(ref) != 'string' )
         {
                 while( ref.className != 'BxP' ) ref = ref.parentNode;
                 ref = ref.getAttribute('idBox');
         }
         return BOX.boxes[ref];
}

function boxI_HTML( ref )
{
         var div = document.createElement("DIV");
         var ob = getBoxI( ref );
             div.appendChild( ob.box.cloneNode(true) );
         ob = new BoxI('htm', "<TEXTAREA style='border: 1px solid #808080; color:#808080; width:500px; height:350px'>"+div.innerHTML+"</TEXTAREA>",
                        { titre: "BoxI ("+ob.idBox+") : outerHTML" }
         );
         div.innerHTML = '';
         ob.place();
}

function box_bClick( el, act )
{
         if( ! el.getAttribute('actif') ) return;

         if( ! act ) { BoxI_Kill( el ); return; } // Annuler
         else // Valider
         {       var oB = getBoxI( el );
                 if( act = oB.onValid ) {
                     var form =  oB.box.getElementsByTagName('FORM')[0];
                              if( typeof(act) == 'function' ) { oB.onValid( form, oB ); }
                         else if( act.slice(0,6) == 'form->' ) BoxIForm_Submit( form );
                 }
         }
}

function str2func( str )
{
         if( str ) try{ str = eval(str); if( typeof(str) == 'function' ) return str; } catch(e) {} ;
}

/*
function get_boxDScroll( box )
{
        var D = box.getElementsByTagName('DIV');
        var i=0; while( D[i] && ! D[i].getAttribute('scrollBox') ) { i++; }
        return D[i];
}
*/

/*
function get_boxDButs( box )
{
         var T = box.getElementsByTagName('TABLE');
         return T[T.length-1];

        var D = box.getElementsByTagName('DIV');
        var i=0; while( D[i] && ! D[i].getAttribute('butsBox') ) { i++; }
        return D[i];
}
*/
function box_get_opts( _OPT, oB )
{
     var i=0; var EO = Array();
     var val = oB.getAttribute('opts'); if(val) return val;
     while( opt = _OPT[i] ) { EO[opt] = oB.getAttribute(opt); i++; }
     return EO;
}

/*
function boxBy_Open(oB, oPlace)
{                                 if( typeof(oB) == 'string' ) oB = document.getElementById(oB);

      var B = box_create( oB.innerHTML, box_get_opts(oB) );
              box_place( B, oPlace );
}
*/
function BoxIAjax( page, func_retour, data, _2O ) // _2O := { BoxI : {}, BoxI2 : {}, ajax : {} }
{
         data = (data)? '&'+data : '';
         var oa = new PAjax( '/index_ajax.php', 'ajax='+page+data );
             if( _2O['ajax'] ) { for(var p in _2O['ajax']) oa[p] = _2O['ajax'][p] ; }
             oa.mask = 'LoadBar';
             oa.kook = 'auto';
             oa.onFailure = 'auto';
             oa.onCreated  = func_retour;
             oa._2O  = _2O;  // options standard de oB ( prototype )
             oa.onComplete = BoxIAjax_Retour;
             oa.execute();
        return oa;
}

function BoxIAjax_Retour( err, typ, res, oa )
{
         //document.body.style.overflow = oa.over0;
         var oB = new BoxI( 'htm', res, oa._2O['BoxI'] );
         if( oa._2O['BoxI2'] ) { for(var el in oa._2O['BoxI2']) oB[el] = oa._2O['BoxI2'][el] ; }
             oa.onCreated( oB, oa );
}


function BoxI( mode, ref, options )
{                                      /*
                                          idRef : id de la boite appelée en mode 'elt'
                                          idBox : identifiant pour instance unique
                                       */

    var _OPT = Array('titre', 'bar', 'bar_bg', 'bar_col', 'border', 'color', 'bgCol', 'bd', 'H','W','minH','minW','maxH','maxW', 'scroll', 'buts', 'mask', 'onClose', 'onValid' );

    var O = Array();  var i=0;  var opt,v,el,nd,EL;
    var box = document.getElementById('BxPModel').cloneNode(true); box.id=null;
    var inB = box_getIN( box ); var el,p;
    var inS = inB.parentNode;  var eS = inS;
    var bBd = inS.parentNode;
    var iFrame = 0;

    switch( mode )
    {
        case 'elt' : {
                        if( typeof(ref) == 'string' ){ ref = document.getElementById(ref);
                                                       //ref.style.display = '';
                                                       O = box_get_opts(_OPT, ref);
                        }
                        if( ref.nodeName.toUpperCase() == 'IFRAME' ){  eS = ref;
                                                                       iFrame = ref;
                        }
              break;
        }
        case 'htm'  : {
                        inB.innerHTML = ref;
              break; }
    }

    if( opts = options )
    {
        if( typeof(opts) == 'string' )
        {      opts = opts.replace(/\s/g,'').split(',');
               var o; while(i<opts.length) { o = opts[i].split('=');
                                             O[o[0]] = o[1]; i++;
               }
        } else O = opts; // Array || Object
    }

    if( mode == 'elt' )
    {
             var idRef = ref.getAttribute('id');
             box.setAttribute( 'idRef', idRef );
             if( ! O['idBox'] ) O['idBox'] = ref.getAttribute('idBox') || idRef;
    }

    var NUM = Array('H','W','minH','minW','maxH','maxW');
    i=0; while( el = NUM[i] ) { O[el] = (O[el])? eval(O[el]) : 0;  i++; }

    if( !O['move'] ) box.getElementsByTagName('TR')[0].onmousedown = null;
    //else             box.getElementsByTagName('TR')[0].getElementsByTagName('TD')[0].style.cursor = 'move';

 // idBox
    var idBox = O['idBox'];
    if( idBox ) { if( BOX.boxes[idBox] ) var wait = BoxI_Kill( idBox ); if(wait === '*pruimporte*' ) {} }
    else        { idBox = 'iB:box_'+BOX.iRefEC; BOX.iRefEC ++; }

    if( mode == 'elt' ) {
                           var el = (iFrame) ? inB.parentNode : inB;
                           el.appendChild( ref );
                           if( iFrame ) { removeElement(inB); inB = ref; }
                           ref.style.display = '';
     }

    box.setAttribute('idBox', idBox);
    BOX.boxes[idBox] = this;

 // Barre Titre
    var el = box.getElementsByTagName('TR')[0];
    if( '_'+O['bar'] ===  '_0' ) { removeElement(el);
                                   bBd.style.borderTop = 'none';
    }
    else
    {     box_setTitre( box, O['titre'] ? O['titre'] : '&nbsp;' );
          if( v = O['bar_bg'] )  el.style.background = v;
          if( v = O['bar_col'] ) el.style.color = v;
    }

 // Bordure exterieure
    if( (O['color']) || (v = O['border'])  )
    {
        el = box.getElementsByTagName('TABLE')[0];

        if( '_'+v == '_0' )  el.style.border = 'none';
        else{ 
               if( !v || '_'+v == '_1' )  v = O['color'];
               el.style.borderColor = v;
        }
    }

   //alert( byId() );

    if( el = O['eScroll'] )
    {
        // ??? ne marche pas :
        if( typeof(el) == 'string' ) el = byId(el);
        eS = el;
    }

 // bordure intérieure
    if( el = O['bd'] )  // padding;backgroundColor;borderColor
    {
        if( '_'+el == '_1' ) el = '10';
        el = el.split(';');
        if( el[0] && el[0] !== '=' ) bBd.style.padding    = el[0]+'px';
        if( el[1] && el[1] !== '=' ) bBd.style.background = el[1];
        if( el[2] ) eS.style.border = '1px solid '+( el[2] == '=' ? '#808080' : '#9B9B9B');
    }

  // Boutons
     //var DBut = get_boxDButs(box);
     var DBut = null;   this.buts = {};
     var opt = O['buts'];
     var _b = '_'+opt;
     if( opt && _b !== '0' )
     {
         var v = O['resizable'] ? 'TABLE' : 'DIV';
         DBut = byId('BoxIButsModel:'+v ).getElementsByTagName(v)[0].cloneNode(true);
         var BUT = byId('BoxIButsModel:buts').cloneNode(true).getElementsByTagName('FONT');

         var BOK = { 0:1, 1:1 }
         var DBut_Toolbar = ( opt && opt.toString().indexOf('toolbar') != -1 );
             if( DBut_Toolbar )
             {
                 with( DBut.style ) { background = '#EFEFE7'; borderTop = '1px solid #808080'; }
                 opt = opt.replace(/\s*toolbar\s*/,'');
                 _b = '_'+opt;
             }

              if( _b === '_*' ) {  /* les 2 en l'état */ }
         else if( _b === '_2' ) BOK[0]=0;
         else if( _b === '_1' ) BOK[1]=0;
         else  // annuler:color/borderColor-> + valider:color/borderColor
         {
              var BO = opt.split('+'); var but, css, txt;
              i=0;
              while( but = BO[i] )
              {
                    if( '_'+but === '_0' ) { BOK[i]=0; i++; continue; }

                    nd = BUT[i];
    
                    if( '_'+but !== '_1' )
                    {
                       but = but.split(/\s*:\s*/); css = but[1] || null;
                                                   txt = but[0] || null;
                       if( txt )   nd.innerHTML = txt;
                       if( css ) { css = css.split(/\s*\/\s*/); // ie: '/'
                                       if( css[0] ) { nd.style.color     = css[0]; if( ! css[1] ) css[1] = css[0]; }
                                       if( css[1] ) nd.style.borderColor = css[1];
                       }
                    }
                    i++;
              }
         }

     }
     if( DBut )
     {

         EL = ( DBut.nodeName.toUpperCase() == 'TABLE' ) ? DBut.getElementsByTagName('TD')[0] : DBut;
         i = 0;
         while( el = BUT[i] ) {
                                   if( BOK[i] )
                                   {
                                       el = el.cloneNode(true);
                                       EL.appendChild(el);
                                       this.buts[i] = el;
                                   }
                i++;
         }
         bBd.parentNode.appendChild(DBut);
     }

     if( v = O['bgCol'] )
     {   inS.style.background = v;
         if( DBut && ! DBut_Toolbar ) DBut.style.background = v;

        // alert(DBut.style.background)
     }

     if( O['keep_alive'] ) box.setAttribute( 'keep_alive', O['keep_alive'] );

     if( O['minH'] || O['minW'] )
     {
        if(O['minH']) inB.style.height = O['minH'];
        if(O['minW']) inB.style.width  = O['minW'];
     }
     else if(O['H'] || O['W'])
     {
        if(O['H']) inB.style.height = O['H'];
        if(O['W']) inB.style.width  = O['W'];
        inB.style.overflow = 'auto';
     }

     if( O['color'] )
     {
         box.getElementsByTagName('TR')[0].style.background = O['color'];
         bBd.style.borderTop = 'none';
     }

 // Initialiser taille/affichage
    box_place_init( box );
    if( eS && eS.nodeName.toUpperCase() != 'IFRAME' ) with( eS.style ) { overflow=''; height=''; }

 // initialiser formulaire(s)
    var nb_els = 0;
    EL = box.getElementsByTagName('FORM');
    if( EL.length > 0 )
    {                         nb_els = EL.length;
                              this.nbForms = nb_els;

             if( nb_els == 1 && (o = O['onValid']) && (typeof o == 'string') && o.slice(0,6)=='form->' )
             {
                 EL[0].setAttribute('action', o.slice(6) );
             }
         if( typeof(window.form_init) == 'function' )
         {
             i=0; while( el = EL[i] )
             {   if( (i==0 && nb_els == 1 && O['onValid']) || str2func(el[i].getAttribute('action')) ) { el.onkeypress = BoxIForm_KeyPress; }
                 if( form_init( el ) !== '...wait...' ) i++;
             }
         }
    }

    EL = box.getElementsByTagName('IFRAME');
    
    if( EL.length > 0 )
    {   i=0;
        while( el = EL[i] )
        {
               v = el.contentWindow || el;
               v.parentBox = this;
               i++;
        }

    }

    this.idBox = idBox;
    this.box = box;
    this.inB = inB;
    this.inS = inS;
    this.eS = eS;
    this.bBd = bBd;
    this.H = O['H'];  this.W = O['W'];
    this.maxH = O['maxH'];  this.maxW = O['maxW'];
    this.scroll = O['scroll'];
    this.mask   = O['mask'];
    if( O['onClose'] ) this.onClose = O['onClose'];
    if( O['onValid'] ) this.onValid = O['onValid'];

    return this;
}

BoxI.prototype =
{
     box : null , bBd : null, inB : null, inS : null, eS : null,
     nbForms:0,
     H:0, W:0, minH:0, minW:0, mawH:0, maxW:0,
     scroll : '',
     mask : '',
     ePlace : null, eLight : null, eLightCSS:null,
     place : function( ePlace, O ) { box_place(this, ePlace, O); return this; } ,
     setHTM : function(htm) { this.inB.innerHTML = htm; } ,
     setScroll : function()
     {                       if( ! this.eS ) return;
                 var maxH = this.maxH || this.H;
                 var maxW = this.maxW || this.W;   if( ! maxH && ! maxW ) return;

                 var box = this.box; var eS  = this.eS;
               
               if( maxH = this.maxH )
               {
                   var sH1 = maxH - (box.offsetHeight - eS.offsetHeight );

                   if( sH1 < eS.offsetHeight  ) with(eS.style) { height = sH1; overflow = 'auto'; }
                   else                         with(eS.style) { height = ''; overflow = ''; }
               }
               if( maxW = this.maxW )
               {
                   var sH1 = maxH - (box.offsetWidth - eS.offsetWidth );

                   if( sH1 < eS.offsetWidth  )  with(eS.style) { width = sH1; overflow = 'auto'; }
                   else                         with(eS.style) { width = ''; overflow = ''; }
               }
     },

     kill    : function() { return BoxI_Kill(this); },
     onValid : null, // -> function() {},
     onClose : null, // -> function() {}
     //butClick : Array( function(){}, function(){} )
     setButton : function( iBut, state )
     {
                 var el = this.buts[iBut];
                 if( ! el ) return;
                 //var state0 = state+' ('+ typeof(state)+')';
                     if( typeof(state) != 'string' )
                     {   
                         if( isNaN(state) ) state = (state)? '1' : '0';
                         else state = state.toString();
                     }
                 //alert( state0+' => '+state.slice(0,1) );
                 switch( state.slice(0,1) )
                 {
                         case 'd' : { /*display*/ el.style.display    = '';       break; }
                         case 'n' : { /*none*/    el.style.display    = 'none';   break; }
                         case 'v' : { /*visible*/ el.style.visibility = '';       break; }
                         case 'h' : { /*hidden*/  el.style.visibility = 'hidden'; break; }
                         case '0' : { with(el.style) { color = '#808080'; borderColor=''; backgroundImage='none'; ; }
                                      try{el.style.cursor='not-allowed';} catch(e){};
                                      el.setAttribute('actif','');
                                      break; }
                         case '1' : { var col = el.getAttribute('color');
                                      with(el.style) { color = col; borderColor=col; backgroundImage=''; cursor=''; }
                                      el.setAttribute('actif','1');
                                      break; }
                 }
                 return el;
     }
}

function box_place_init( el )
{
    box2Body(el);
    with(el.style) { top=0; left=0; visibility='hidden'; display=''; }  return el;
}

function box2Body( el ) {  if( el.parentNode != document.getElementsByTagName('BODY')[0] ) document.getElementsByTagName('BODY')[0].appendChild(el); return el; }


// txt - txt - txt - txt - txt - txt - txt - txt

  function box_getIN( box )
  {
           var D = box.getElementsByTagName('DIV');
           var i=0; var div;
           while( div = D[i] ) { if(div.className == 'BxP_in') return div;  i++;  }
           //return [0].getElementsByTagName('DIV')[0].getElementsByTagName('DIV')[0];
  }

  function box_setTitre( box, titre )
  {
           box.getElementsByTagName('TD')[0].innerHTML = titre;
  }


function BoxI_Kill( ref ){
                              var oB = getBoxI( ref );  if( ! oB ) return;
                              var o;

         var idBox = oB['idBox'];
         if( oB.eLight ){  var i=0; while( o = oB.eLightCSS[i] ) { oB.eLight.style[o] = ''; i++; } }
         if( oB.onClose ) oB.onClose();
         blur_all();
         //var keep = ( oB.box.getAttribute('keep_alive') === '1' ) ;
         //if( keep ) oB.box.style.display = 'none';
         //else{
               var idRef = oB.box.getAttribute('idRef');
                   if(idRef){
                              idRef = document.getElementById( idRef );
                              idRef.style.display = 'none';
                              document.body.appendChild( idRef );
                   }
               removeElement(oB.box);
               if( ! idRef ) oB.box = null;
         if( oB.mask ) { BMask_set(0); }
               oB = null;  delete BOX.boxes[idBox];
         //}

}

function box_place( oB, ePlace, O ) // O : ePlace, eLiglh, css;
{                              if( ! O ) O = Array();  var el, opt, o;
                               if(ePlace) oB.ePlace = ePlace;
                               var dB = document.body;
                               var box = oB.box;
                               var eS = oB.eS;

        if( oB.mask ) BMask_set( oB.mask );

        if( el = O['eLight'] )
        {                      if( ! el.nodeName ) el = ePlace;  oB.eLight = el;
            opt = O['css'] || { backgroundColor: '#DFFFFF' }; oB.eLightCSS = Array();
            for( o in opt ) { el.style[o] = opt[o];
                              oB.eLightCSS.push(o);
            }
        }

        var oH = box.offsetHeight;
        var oW = box.offsetWidth;

            if( oB.maxH && oH > oB.maxH ){
                with( eS.style ) { height = ( oB.maxH + eS.offsetHeight - oH )+'px'; overflow = 'auto'; }
                oH = oB.maxH ;
            }
            if( oB.maxW && oW > oB.maxW ){
                with( eS.style ) { width = ( oB.maxW  + eS.offsetWidth  - oW )+'px'; overflow = 'auto'; }
                oW = oB.maxW ;
            }
        if( ! ePlace || typeof(ePlace) == 'string' ) { return box_place_abs(oB, ePlace, oH, oW); }

        var XY = get_offsetXY(ePlace);  var offX = XY[0]; var offY = XY[1]; delete XY;
     // variables de positionnement
        var eH = ePlace.offsetHeight;   var bH = dB.clientHeight;
        var eW = ePlace.offsetWidth ;   var bW = dB.clientWidth;
        var sY = dB.scrollTop; var sX = dB.scrollLeft;
        var not_above = 0;
     // Positionner (p/ défaut : basé s/ angle bas-gauche de ePlace )
        offY = offY - sY + eH;
        offX -= sX;
       var margT = offY - eH;    var margB = bH - offY;

        // Vertical
           find_pos = false;
           offY +=1;
           if( margB >= oH) {  }  // vertical ok
           else
           {
               if( margT >= oH +1 ) offY -= (eH + oH +1); // au-dessus
               else // >= Scroll
               {  //alert(margB)
                  var allowedH = Math.max(margT, margB);
                  //offY = sY+1 ; //-= (eH + allowedH +1)
                  if( margT > margB ) offY = sY+1;
                  //if(bH > oH +1) { offY = (bH-oH)/2;  not_above=1; } // body
                  //else offY -= (eH + oH +1); // au-dessus
                  //if( ! dS ) dS = get_boxDScroll( box );
                  //if( ! dS ) dS = oB.inB.parentNode;
                  with( eS.style )
                  {
                            overflow = 'auto';
                            height = ( allowedH + eS.offsetHeight - oH -3 ) +'px';
                  }

               }
           }
           /*alert(not_above)*/
                  //else if( margT < oH ) { alert('margB < oH'); offY -= (eH + oH +3) ;}
        // Horizontal
           var margR = bW - offX;    //oW = get_box_Dim(box,'offsetWidth'); // recalculer oW ICI
               if(not_above) margR -= (eW +1);
           if( margR < oW )   { offX = (not_above)? (offX - oW - 1) : (bW - oW -1) ; }
           else if(not_above) { offX += eW +1; }
        // => Do : positionner
           //with(box.style){ top  = sY + offY;  left = sX + offX;  visibility = 'visible'; };
           return BoxI_affi( oB, offY, offX );
}

function box_place_abs(oB, yx, oH, oW) // oH, oW := variables interne
{                                         var box = oB.box;
                                          var eS = oB.eS;
                                          var abs = (yx);
    if( ! oH ) oH =  box.offsetHeight;
    if( ! oW ) oW =  box.offsetWidth;

    var dB = document.body;

    if( yx ) { yx = yx.split(','); if(yx.length==1) yx[1] = yx[0]; }
    else yx = Array(0,0);

    var avH = dB.clientHeight - yx[0];
    var avW = dB.clientWidth  - yx[1];

    if( oH > avH ){
        with( eS.style ) { height = ( avH + eS.offsetHeight - oH )+'px'; overflow = 'auto'; }
        oH = avH ;
    }
    if( oB.maxW && oW > oB.maxW ){
        with( eS.style ) { width  = ( avW  + eS.offsetWidth - oW )+'px'; overflow = 'auto'; }
        oW = avW ;
    }

    if( ! abs ) yx = Array( (dB.clientHeight - oH)/2 , (dB.clientWidth - oW)/2  );

    return BoxI_affi( oB, Math.max(0,yx[0]), Math.max(0,yx[1]) );
}

function BoxI_affi( oB, y, x )
{                              y += document.body.scrollTop; x += document.body.scrollLeft;

    if( display_elt( oB.box, {top:y, left:x} ) === '..pour attendre...' ) return;
    //if( oB.nbForms && window.input_1St ) input_1St( oB.box.getElementsByTagName('FORM')[0], 'select' );
    if( window.input_1St && ( oB.nbForms || oB.inB.getElementsByTagName('INPUT').length > 0 ) )
    {
         input_1St( oB.inB, 'select' );
    }
    return oB;
}


// ----------------------------     FORM   -----------------------------------------------

function BoxIForm_KeyPress(event)
{                                       if( ! event ) event = window.event;
                                        var el = event.target || event.srcElement;
                                        if( event.keyCode != 13 || el.nodeName.toUpperCase() != 'INPUT' ) return true;
         var ob = getBoxI(this); // this := form
         if( ob.onValid && typeof(ob.onValid) == 'function' )
         {
             ob.onValid( this, ob ); return false;
         }
         else
         {     var func = str2func(this.getAttribute('action'));
               if(func) { func(this, ob); return false; }
         }
}

function BoxIForm_Submit(form)
{                               if( ! form ) form = this;
                                if(form.nodeName.toUpperCase != 'FORM') while(form.nodeName.toUpperCase()!='FORM') form = form.parentNode;
         var action = form.getAttribute('action');
         var func = str2func(action);
         if( func ) { func(form, getBoxI(form) ); return false; }
         else return true;
}

// Common :: DOM    *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *
function getCursor(e)
{                     var c = new Object();

                if (e.pageX !== undefined) { c.x = e.pageX ; c.y = e.pageY; }
                else                       { c.x = e.x ; c.y = e.y; }
       return c;
}
//  *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *



// ----------------------------    DEPLACER    --------------------------------------------

var oMovEC = null;

function box_move_MDown(event, el)  // Initie le déplacement
{                                      if( ! event ) event = window.event;
                                       if(oMovEC || event.button == 2) return false;
     oMovEC = new Object();
  // no-select
     if(document.all){ oMovEC.docSelStart = document.body.onselectstart;
                       document.body.onselectstart = function() { return false; }
     } else          {
                       oMovEC.docSelStyle = ( getStyle(document.body, '-moz-user-select') == 'none' ) ? 'none' : '';
                       document.body.style.MozUserSelect = 'none';
     }

     var box = el;  while( ! box.getAttribute('moveable') ) box = box.parentNode;
     var XY = get_offsetXY( box );
     var  c = getCursor(event);

     oMovEC.el = box;
     oMovEC.dX = c.x - XY[0];
     oMovEC.dY = c.y - XY[1];

     addEvent(document.body, "mousemove", oMovEC_DocMmove);
     addEvent(document.body, "mouseup", oMovEC_Kill);
}

function oMovEC_Kill()
{
         delEvent(document.body, "mousemove", oMovEC_DocMmove);
         delEvent(document.body, "mouseup"  , oMovEC_Kill);

         if(document.all) document.body.onselectstart = oMovEC.docSelStart;
         else             document.body.style.MozUserSelect = oMovEC.docSelStyle;

         oMovEC = null;
}

function oMovEC_DocMmove(event)
{                                 if( ! event ) event = window.event;  var c = getCursor(event);
         with(oMovEC.el.style){
                                left= c.x - oMovEC.dX +'px';
                                top = c.y - oMovEC.dY +'px';
         }
}


// ----------------------------    REDIMENSIONNER    --------------------------------------------

var oSizeEC = null;

function box_rsize_MDown(event, el)  // Initie le déplacement
{                                      if( ! event ) event = window.event;
                                       if(oSizeEC || event.button == 2) return false;
     oSizeEC = new Object();
  // no-select
     if(document.all){ oSizeEC.docSelStart = document.body.onselectstart;
                       document.body.onselectstart = function() { return false; }
     } else          {
                       oSizeEC.docSelStyle = ( getStyle(document.body, '-moz-user-select') == 'none' ) ? 'none' : '';
                       document.body.style.MozUserSelect = 'none';
     }

     //var box = el;  while( ! box.getAttribute('resizable') ) box = box.parentNode;
     //var XY = get_offsetXY( box );

     var ob = getBoxI(el);
     var eS = ob.eS;

     var  c = getCursor(event);

     oSizeEC.el = eS;
     oSizeEC.w0 = eS.offsetWidth;
     oSizeEC.h0 = eS.offsetHeight;
     oSizeEC.x0 = c.x ;
     oSizeEC.y0 = c.y;

     addEvent(document.body, "mousemove", oSizeEC_DocMmove);
     addEvent(document.body, "mouseup", oSizeEC_Kill);
     return false;
}

function oSizeEC_Kill()
{
         delEvent(document.body, "mousemove", oSizeEC_DocMmove);
         delEvent(document.body, "mouseup"  , oSizeEC_Kill);

         if(document.all) document.body.onselectstart = oSizeEC.docSelStart;
         else             document.body.style.MozUserSelect = oSizeEC.docSelStyle;

         oSizeEC = null;
}

function oSizeEC_DocMmove(event)
{                                 if( ! event ) event = window.event;  var c = getCursor(event);
         //window.status = oSizeEC.w0 + ( c.x - oSizeEC.x0 );
        // alert(oSizeEC.el.outerHTML);

         var w = Math.max( 0, oSizeEC.w0 + ( c.x - oSizeEC.x0 ) );
         var h = Math.max( 0, oSizeEC.h0 + ( c.y - oSizeEC.y0 ) );
         with(oSizeEC.el.style){
                                //overflow = 'hidden';
                                width  = w +'px';
                                height = h +'px';
         }
}


// ___________________________________   [ / BoxI.js ]  _______________________________________________
