/**
 * This file is written in parallel with app/helpers/application_helper.rb
* to support the fselect and fmultiselect functions. Wild assumptions are made on document element ids below.
 */

/*
 * Switch from Select pull-down to a Text Field, by selecting a list entry with value OTHER
 */
function hideselect(formElement) {
  d_hideselect(formElement,false);
}

function d_hideselect(formElement,nofocus) {
  if ( formElement.value == "OTHER" ) {
    formElement.options[formElement.selectedIndex].selected=false;
    f=$("t_"+formElement.id);
    f.style.display="inline";
    f.value="";
    if(!nofocus){f.focus();}
    formElement.style.display="none";
  }
  if(formElement.additionalChange){formElement.additionalChange();}
}

/*
 * After text is typed-in within the Text box that replaces the Select pull-down upon selecting a list item of value OTHER,
 * update the Select pull-down's first list item accordingly
 */
function transfertexttoselect(formElementId) {
  f = $(formElementId);
  g = $("t_"+formElementId);
  f.options[0].value = f.options[0].text = g.value;
  f.options[0].disabled = false;
  f.selectedIndex=0;
  g.value = "";
  g.style.display="none";
  f.style.display="inline";
}

/*** ALL METHODS BELOW ARE FOR MULTI-SELECT ONLY ***/

/*
 * After click in a multi-select pull-down, copy over the value to the UL/LI list on the right of the pull-down
 */
var savedheader=new Array();
function transfertoside(value,text,destElementId,formElementName,limit) {
  if ( (value != "OTHER") && (value != "") ) {
    liElement=null;index=-1;cnt=0;
    // look up next hidden li
    for ( i=0; i<limit;i++) {
      if ($("v_"+destElementId+"_"+i).value==value) return;
      if ($("li_"+destElementId+"_"+ i).style.display == "block" ) cnt++;
      else if (index==-1) index=i;
    }
    liElement=$("li_"+destElementId+"_"+ index);
    if (liElement==null) return; // should never happen
    // change style to display for this li
    liElement.style.display="block";
    // display new text value
    $("tli_"+destElementId+"_"+index).firstChild.data=text.replace(/\u00a0/g," "); //.replace(/\&/,"&amp;").
    // set new value in hidden field
    f=$("v_"+destElementId+"_"+index);
    f.value=value;
    f.name=formElementName;
    f.disabled=false;
    // save Select pull-down header if someone [remove] an existing value before adding a new one
    f = $("tmp_"+destElementId);
    savedheader[destElementId]=f.options[0].text;
    // freeze Select pull-down
    if (++cnt==limit) {
      f.options[0].text="-- You already have " + limit + " selections --";
      f.selectedIndex=0;
      f.disabled=true;
    }
  }
}

/*
 * After text is typed-in within the Text box that replaces the Select pull-down upon selecting a list item of value OTHER,
 * update the Select pull-down's first list item accordingly
 */
function transfertexttoside(formElement,destElementId,formElementName,limit) {
  transfertoside(formElement.value,formElement.value,destElementId,formElementName,limit);
  f = $("tmp_"+destElementId);
  g = $("t_tmp_"+destElementId);
  f.selectedIndex=0;
  g.value = null;
  g.style.display="none";
  f.style.display="inline";
}

/*
 * After click on [remove] next to the UL/LI list on the right of a multi-select pull-down, remove the value from the form
 */
function removevalue(formElementId,idPart,formElementName) {
  // remove value in hidden field
  f=$("v_"+formElementId+idPart);
  fs = $("tmp_"+formElementId);
  if(fs.onremove){fs.onremove(f.value);}
  f.value="";
  // hide li element
  $("li_"+formElementId+idPart).style.display="none";
  // unfreeze Select pull-down
  fs.options[0].text=savedheader[formElementId];
  fs.disabled=false;
}