// $Id: nicechoices.js,v 1.4 2007-06-26 14:48:43 patrick Exp $ 

var nicechoices_tracka=new Object;
var nicechoices_trackc=0;

// Parameters
// - id of select box with all choices available (on the left normally)
// - id of select box with current selected choices (on the right normally)
// - javascript array of Option objects for all available choices
// - javascript array of Option objects for current selected choices (default)
//          WARNING : defopt options MUST BE in same order as same options in allopt
// - maximal number of choices to select
// - id of HTML input type=button for available to selected move (left to right)
// - id of HTML input type=button for selected to available move (right to left)
// - id of HTML span to show number of currently selected options
// - id of HTML span to show number of maximum selected options
function nicechoices_setup(idsg,idsd,allopt,defopt,nboptmax,idblr,idbrl,idsnbc,idsnbm)
{
 var fg=document.getElementById(idsg);
 var fd=document.getElementById(idsd);
 if (fg==null || fd==null) { return; }

 var obj=new Object;
 obj.idsg=idsg;
 obj.idsd=idsd;
 obj.allopt=allopt;
 obj.nboptmax=nboptmax;
 obj.idblr=idblr;
 obj.idbrl=idbrl;
 obj.idsnbc=idsnbc;
 obj.idsnbm=idsnbm;
 
 fg.options.length=0;
 fd.options.length=0;
 var nbopt=0;
 for(var i=0;i<allopt.length;i++) { fg.add(allopt[i],null); }
 fg.selectedIndex=-1;
 fg.nicechoicescid=nicechoices_trackc;
 fg.ondblclick=nicechoices_move;

 if (defopt != null)
 {
  var done=0;
  for(var ai=0;ai<fg.options.length;ai++)
  {
   if (done == defopt.length) { break ; }
   for(var di=0;di<defopt.length;di++)
   {
    if (fg.options[ai].value==defopt[di].value && fg.options[ai].text==defopt[di].text)
    {
     defopt[di].pos=ai;
     fd.add(defopt[di],null);
     done=di;
    }
   }
  }
  nbopt=fd.options.length;
 }
 fd.selectedIndex=-1;
 fd.nicechoicescid=nicechoices_trackc;
 fd.ondblclick=nicechoices_move;

 document.getElementById(idsnbm).firstChild.data=nboptmax;
 document.getElementById(idsnbc).firstChild.data=nbopt;

 var blr=document.getElementById(idblr);
 var brl=document.getElementById(idbrl);

 blr.nicechoicescid=nicechoices_trackc;
 blr.onclick=nicechoices_move;
 brl.nicechoicescid=nicechoices_trackc;
 brl.onclick=nicechoices_move;

 nicechoices_endis(fd,nboptmax,obj);

 nicechoices_tracka[nicechoices_trackc]=obj;
 nicechoices_trackc++;
}

function nicechoices_move()
{
 var cid=this.nicechoicescid;
 var obj=nicechoices_tracka[cid];

 var from;
 var to;

 if (this.id == obj.idblr || this.id == obj.idsg) // LEFT TO RIGHT
 {
  from=document.getElementById(obj.idsg);
  to=document.getElementById(obj.idsd);  

  var toadd=new Array();
  for(var j=0;j<from.options.length;j++)
  {
   if (! from.options[j].selected) { continue; }
   var o=new Option(from.options[j].text,from.options[j].value);
   o.pos=j;
   toadd.push(o);
  }
  nicechoices_multiadd_nodups(to,toadd,obj.nboptmax);
 } else if (this.id == obj.idbrl || this.id == obj.idsd) // RIGHT TO LEFT
 {
  from=document.getElementById(obj.idsd);
  to=document.getElementById(obj.idsd);  

  if (from.options.length > 0)
  {
   for(var jj=from.options.length-1;jj>=0;jj--) // very important to start from the end !!
   {
    if (from.options[jj].selected) { from.remove(jj); }
   }
  }
 }
 
 nicechoices_endis(to,obj.nboptmax,obj);
 document.getElementById(obj.idsnbc).firstChild.data=document.getElementById(obj.idsd).options.length;
 from.selectedIndex=-1;
 to.selectedIndex=-1;
 return false;
}

// Add an array of option to current selected choices, but preserve order and make sure no dups
function nicechoices_multiadd_nodups (to,a,nboptmax)
{
 var cj=0;
 for(var j=0;j<a.length;j++)
 {
  var done=0;
  if (to.options.length >= nboptmax) { break; }
  for(var jj=cj;jj<to.options.length;jj++)
  {
   if (a[j].pos == to.options[jj].pos) { done=1; break; } // doublon
   if (a[j].pos > to.options[jj].pos) { continue; }
   to.add(a[j],to.options[jj]);
   cj=jj;
   done=1;
   break;
  }
  if (! done) to.add(a[j],null);
 }
 return false;
}

function nicechoices_endis(w,nboptmax,obj)
{
 var l=w.options.length;
 document.getElementById(obj.idblr).removeAttribute('disabled');
 document.getElementById(obj.idbrl).removeAttribute('disabled');

 if (l==0)        document.getElementById(obj.idbrl).setAttribute('disabled','disabled');
 if (l==nboptmax) document.getElementById(obj.idblr).setAttribute('disabled','disabled');
}

function nicechoices_form_submit()
{
 for (var i=0;i<nicechoices_trackc;i++)
 {
  var f=document.getElementById(nicechoices_tracka[i].idsd);
  for(var j=0;j<f.options.length;j++) f.options[j].selected=1;
 }
}

