
function add_emails()
{

  transfer_emails('optional_emails','selected_emails');
}

function remove_emails()
{
  transfer_emails('selected_emails','optional_emails');
}


function transfer_emails (source_id,target_id)
{

  haschanged=true;


  source_id=document.getElementById(source_id);
  target_id=document.getElementById(target_id);
  teller_max=target_id.options.length-1;
  teller_target=0;
  target_id.options.selectedIndex=-1;

  while (source_id.options.selectedIndex!=-1) // loop while there is a selected index
  {
    if (source_id.options[source_id.options.selectedIndex].className=="") {source_id.options[source_id.options.selectedIndex].className="single";}
    var newoption = new Option(source_id.options[source_id.options.selectedIndex].text, source_id.options[source_id.options.selectedIndex].value, true,true); //copy option as "selected"
    var newoptionClass=source_id.options[source_id.options.selectedIndex].className;

    //  alert(newoptionClass+newoption.text.toUpperCase()+' '+(target_id.options[teller_target].className+target_id.options[teller_target].text.toUpperCase()));
    while ((teller_target<=teller_max)&&((newoptionClass.substr(0,1)+newoption.text.toUpperCase())>(target_id.options[teller_target].className.substr(0,1)+target_id.options[teller_target].text.toUpperCase())))
    {
      teller_target++;
    }
    
    if (1==0&&2 == target_id.add.length) //DOM specification
    { 
      target_id.add( newoption,target_id.childNodes[teller_target]);
    }
    else // IE does not like DOM specifications, so instead of child handle, just provide element number
    {
      target_id.options.add( newoption,teller_target);
    }
    target_id.options[teller_target].className=newoptionClass;
    teller_target++;//new option added, so add one to teller
    teller_max++;//new option added, so add one to max
    source_id.options[source_id.options.selectedIndex] = null; // delete from source
  }
     

} // end function transfer_emails



function send_emailform(form_id)
{

  var selected = "";

  if (document.getElementById("selected_emails"))
  {
    sel=document.getElementById("selected_emails");
    for (var intLoop=0; intLoop < sel.length; intLoop++) 
    {
      selected += ";"+ sel[intLoop].value;
      sel[intLoop].selected=true;
    }


  document.getElementById("emailarray").value=selected.substring(1,selected.length); //save all selected values in array
  }
  //alert(form_id);
  document.getElementById(form_id).submit();
}


function OnLoadEmailadressen()
{

var selected = document.getElementById("emailarray").value;

opt=document.getElementById("optional_emails");

  for (var intLoop=0; intLoop < opt.length; intLoop++) {
         var endemail=selected.indexOf(";");
         if (endemail==-1){endemail=selected.length};
         if (opt[intLoop].value==selected.substring(0,endemail)) {
         opt[intLoop].selected=true;
            selected = selected.substring(endemail+1,selected.length);
         }
      }

document.getElementById("emailarray").value=selected.substring(endemail+1,selected.length); //save all selected values in array

add_emails();
document.getElementById("selected_emails").selectedIndex=-1;

}
