<script language="JavaScript">
<!--

var Addresses = new Array (
  <?php
  if (is_array($addresses)) { 
    for ($i = 0; $i < sizeof($addresses); $i++) {
      if ($i > 0) { echo ' , '; }
      echo '"' . $addresses[$i][0] . '"';
    }
    echo "\n";
  }
  ?>
)

function ClearSelection() {
   window.document.addressbook.new_address.value = "";
   window.document.addressbook.new_nickname.value = "";      
   window.document.addressbook.new_fullname.value = "";      
}

function AlertMissingParent() {
   alert('<?php echo $lang->alert_no_compose; ?>');
}


function insert_addr() {
   
   var prefix = "";
   var t_parent = typeof parent.opener;
   var t_compose = typeof parent.opener.document.compose;
   
   if (t_compose != "undefined") {
      if (parent.opener.document.compose.to.value) {
         if (window.document.addressbook.new_address.value) {
	    prefix = ", ";
         }
      }
      
      if (window.document.addressbook.new_address.value) {
	 addop = " <";
	 addclo = ">";
      } else {
	 addop = "";  
	 addclo = ""; 
      }
      
      parent.opener.document.compose.to.value = parent.opener.document.compose.to.value + prefix + window.document.addressbook.new_fullname.value + addop + window.document.addressbook.new_address.value + addclo;
   } else {
      AlertMissingParent();    
   }
}

function insert_addr_cc() {
   
   var prefix = "";
   var t_parent = typeof parent.opener;
   var t_compose = typeof parent.opener.document.compose;

   if ( t_compose != "undefined" ) {
      if ( parent.opener.document.compose.cc.value ) {
         if ( window.document.addressbook.new_address.value ) {
           prefix = ", ";
         }
      }                

   if ( window.document.addressbook.new_address.value ) {
     addop = " <";
     addclo = ">";
   } else {
     addop = "";  
     addclo = ""; 
   }


parent.opener.document.compose.cc.value = parent.opener.document.compose.cc.value + prefix + window.document.addressbook.new_fullname.value + addop + window.document.addressbook.new_address.value + addclo

   } else {
      AlertMissingParent();    
   }

}

function insert_addr_bcc() {

   var prefix = "";
   var t_parent = typeof parent.opener;
   var t_compose = typeof parent.opener.document.compose;

   if ( t_compose != "undefined" ) {
      if ( parent.opener.document.compose.bcc.value ) {
         if ( window.document.addressbook.new_address.value ) {
           prefix = ", ";
         }
      }                
   if ( window.document.addressbook.new_address.value ) {
     addop = " <";
     addclo = ">";
   } else {
     addop = "";
     addclo = "";
   }

parent.opener.document.compose.bcc.value = parent.opener.document.compose.bcc.value + prefix + window.document.addressbook.new_fullname.value + addop + window.document.addressbook.new_address.value + addclo

   } else {
      AlertMissingParent();    
   }

}

var Nicknames = new Array (
  <?php
  if (is_array($addresses)) {
    for ($i = 0; $i < count($addresses); $i++) {
      if ($i > 0) { echo ' , '; }
      echo '"' . $addresses[$i][1] . '"';
    }
    echo "\n";
  }
  ?>
)

var Fullnames = new Array (
  <?php
  if (is_array($addresses)) {
    for ($i = 0; $i < count($addresses); $i++) {
      if ($i > 0) { echo ' , '; }
      echo '"' . $addresses[$i][2] . '"';
    }
    echo "\n";
  }
  ?>
)

function setFields_ins () {
   document.addressbook.new_address.value = Addresses[document.addressbook.old_address.selectedIndex];
   document.addressbook.new_nickname.value = Nicknames[document.addressbook.old_address.selectedIndex];
   document.addressbook.new_fullname.value = Fullnames[document.addressbook.old_address.selectedIndex];
}

function ClearAttr() {
   while (document.ldap_form.attr_type.options.length > 0) {
      document.ldap_form.attr_type.options[0] = null;
   }
}

function ClearOptions() {
   while (document.ldap_form.ldap_options.options.length > 0) {
      document.ldap_form.ldap_options.options[0] = null;
   }
}

function AddOption(attr) {
   
   if (attr == "uid") {
      var uid = new Option("<?php echo $lang->uid; ?>", uid, defaultSelected=0, selected=0);
      document.ldap_form.attr_type.options[document.ldap_form.attr_type.options.length] = new Option("<?php echo $lang->uid; ?>");
   }
   
   else if (attr ==  "mail") {
      var mail = new Option("<?php echo $lang->mail; ?>", mail, defaultSelected=0, selected=0);
      document.ldap_form.attr_type.options[document.ldap_form.attr_type.options.length] = new Option("<?php echo $lang->mail; ?>");
   }
   
   else if (attr == "sn") {
      var sn = new Option("<?php echo $lang->sn; ?>", sn, defaultSelected=0, selected=0);
      document.ldap_form.attr_type.options[document.ldap_form.attr_type.options.length] = new Option("<?php echo $lang->sn; ?>");
   }
   
   else if (attr == "givenname") {
      var givenname = new Option("<?php echo $lang->givenname; ?>", givenname, defaultSelected=0, selected=0);
      document.ldap_form.attr_type.options[document.ldap_form.attr_type.options.length] = new Option("<?php echo $lang->givenname; ?>");
   }
   
   else if (attr == "ou") {
      var ou = new Option("<?php echo $lang->ou; ?>", ou, defaultSelected=0, selected=0);
      document.ldap_form.attr_type.options[document.ldap_form.attr_type.options.length] = new Option("<?php echo $lang->ou; ?>");
   }
      
   else if (attr == "cn") {
      var cn = new Option("<?php echo $lang->cn; ?>", cn, defaultSelected=1, selected=1);
      document.ldap_form.attr_type.options[document.ldap_form.attr_type.options.length] = new Option("<?php echo $lang->cn; ?>");
   }
   
   else if (attr == "begins") {
      var begins = new Option("<?php echo $lang->begins; ?>", begins, defaultSelected=0, selected=0);
      document.ldap_form.ldap_options.options[document.ldap_form.ldap_options.options.length] = new Option("<?php echo $lang->begins; ?>");
   }
   
   else if (attr == "contains") {
      var contains = new Option("<?php echo $lang->contains; ?>", contains, defaultSelected=0, selected=0);
      document.ldap_form.ldap_options.options[document.ldap_form.ldap_options.options.length] = new Option("<?php echo $lang->contains; ?>");
   }
   
   else if (attr == "ends") {
      var ends = new Option("<?php echo $lang->ends; ?>", ends, defaultSelected=0, selected=0);
      document.ldap_form.ldap_options.options[document.ldap_form.ldap_options.options.length] = new Option("<?php echo $lang->ends; ?>");
   }
   
   else if (attr == "is_strict") {
      var is_strict = new Option("<?php echo $lang->is_strict; ?>", is_strict, defaultSelected=0, selected=0);
      document.ldap_form.ldap_options.options[document.ldap_form.ldap_options.options.length] = new Option("<?php echo $lang->is_strict; ?>");
   }
   
   else if (attr == "is") {
      var is = new Option("<?php echo $lang->is; ?>", is, defaultSelected=1, selected=1);
      document.ldap_form.ldap_options.options[document.ldap_form.ldap_options.options.length] = new Option("<?php echo $lang->is; ?>");
   }
   
   else if (attr == "sounds") {
      var sounds = new Option("<?php echo $lang->sounds; ?>", sounds, defaultSelected=0, selected=0);
      document.ldap_form.ldap_options.options[document.ldap_form.ldap_options.options.length] = new Option("<?php echo $lang->sounds; ?>");
   }
}

function StandardOptions() {
<?php

$i = 0;
reset($LDAPServers);
while (list($key, $lserver) = each($LDAPServers)) {
   $options = explode(',', $lserver->options);

   if ($i == 0) echo "if (document.ldap_form.ldap_serv.options[$i].selected) {\n";
   else echo "else if (document.ldap_form.ldap_serv.options[$i].selected) {\n"; 
   echo "   ClearOptions();\n";
   for ($j = 0; $j < count($options); $j++)
     echo "   AddOption(\"$options[$j]\");\n";

   echo "}\n\n";
   $i++;
}

?>
}


function UpdateSelect() {

<?php

$i = 0;
reset($LDAPServers);
while (list($key, $lserver) = each($LDAPServers)) {
   $options = explode(',', $lserver->attribs);

   if ($i == 0) echo "if (document.ldap_form.ldap_serv.options[$i].selected) {\n";
   else echo "else if (document.ldap_form.ldap_serv.options[$i].selected) {\n";

   echo "   ClearAttr();\n";
   for ($j = 0; $j < count($options); $j++)
     echo "   AddOption(\"$options[$j]\");\n";

   echo "}\n\n";
   $i++;
}

?>

   StandardOptions();
}


function UpdateOptions() {

   StandardOptions();

   for (i=0; i < 6; i++) {
      if (document.ldap_form.attr_type.options[i]) {
         if (document.ldap_form.attr_type.options[i].selected) {
	    
	    attr = document.ldap_form.attr_type.options[i].text;
            
            if (attr == "<?php echo $lang->mail; ?>") {
               if (document.ldap_form.ldap_serv.options[0].selected ) {
                  ClearOptions();
                  AddOption("is_strict");
               }
	       
               if (document.ldap_form.ldap_serv.options[1].selected ) {
                  ClearOptions();
                  AddOption("is_strict");
               }
       
               if (document.ldap_form.ldap_serv.options[2].selected ) {
                  ClearOptions();
                  AddOption("is_strict");
               }
	    }
	    
	    if (attr == "<?php echo $lang->sn; ?>") {
               if (document.ldap_form.ldap_serv.options[1].selected ) {
                  ClearOptions();
                  AddOption("is_strict");
               }
	    }
	 }
      }
   }
   document.ldap_form.ldap_options.options[0].selected = true;
}


function expand_new_address () {
  document.compose.new_address.value = expand(document.compose.new_address.value.split(',')).join(', ');
}

function expand_new_fullname() {
  document.compose.new_fullname.value = expand(document.compose.new_fullname.value.split(',')).join(', ');
}

function expand_new_nickname() {
  document.compose.new_nickname.value = expand(document.compose.new_nickname.value.split(',')).join(', ');
}


//-->
</script>
