var ldap_step = 500, ldap_from = 0, ldap_to = ldap_from + ldap_step, ldap_filter = '', ldap_url = '', state = 'init', isPab, tfr, addrs = 0, target,searchResults =0,tfr;


function setUp() {
}

function setFrames() {
  ctrl = window.frames[0];
  data = window.frames[1];
  idx = window.frames[2];
  btn = window.frames[3];
}

function ctrlHTML() {

   var s = '<body bgcolor="' + main.chrome2 + '" marginwidth="0" marginheight="0" leftmargin="0" topmargin="0">' +
          '<form Accept-Charset="UTF-8" name="form" method="post" action="javascript:parent.noop()" onSubmit="return parent.doSearch()" >' +
          '<table width="100%" border="0" cellspacing="0" height="100%" align="default" cellpadding="0" bgcolor="#ffffff">' +
          '<tr bgcolor="' + main.chrome2 + '">' +
      '<td>' +
        '<table width="100%" border="0" cellspacing="0" cellpadding="0">' +
          '<tr>' +
            '<td colspan="3"><img src="imx/spacer.gif" width="1" height="12"></td>' +
          '</tr>' +
          '<tr>' +
            '<td>&nbsp;</td>' +
            '<td nowrap>' +
              '<table border="0" cellspacing="0" cellpadding="0">' +
                '<tr>' +
                  '<td><img src="imx/spacer.gif" width="10" height="1"></td>' +
                  '<td>' + main.font() + main.i18n_select_directory_options(1) + '</td>' +
                '</tr>' +
                '<tr>' +
                  '<td colspan="2"><img src="imx/spacer.gif" width="1" height="9"></td>' +
                '</tr>' +
                '<tr>' +
                  '<td>&nbsp;</td>' +
                  '<td nowrap>' + main.font() + main.i18n_search_options() +
                    '</font>&nbsp;<input type="text" name="string">' +
                    '&nbsp;' + main.nbutton('search', i18n['search'], "parent.doSearch()") +
                  '</td>' +
                '</tr>' +
                '<tr>' +
                  '<td colspan="2"><img src="imx/spacer.gif" width="1" height="12"></td>' +
                '</tr>' +
              '</table>' +
            '</td>' +
            '<td>&nbsp;</td>' +
            '</tr>' +
        '</table>' +
      '</td>' +
    '</tr>' +
    '</table>' + 
    '</form>'
 return s
}


function dataHTML() {
  return main.getBody(main.chrome2, true);
}

function idxHTML() {
  return main.getBody(main.white, true, main.black) + getSearchResults();
}

function btnHTML() {

  var s = '<body bgcolor="' + main.chrome2 + '" marginwidth="0" marginheight="0" leftmargin="0" topmargin="0">' +
          '<form Accept-Charset="UTF-8" name="FORM">' +
  '<table width="100%" border="0" cellspacing="0" height="100%" align="default" cellpadding="0" bgcolor="#ffffff">' +
    '<tr bgcolor="' + main.chrome0 + '" align="right">' +
      '<td height="40">' +
        '<table border="0" cellspacing="0" cellpadding="0">' +
          '<tr>' +
            '<td>' + main.nbutton('', (isPab ? i18n_lu['new msg'] : i18n['SetPermissions ok']), "parent.ok()") + '</td>' +
            '<td><img src="imx/spacer.gif" width="6" height="1"></td>' +
             '<td>' + main.nbutton('', (isPab ? i18n_lu['new msg'] : i18n['SetPermissions apply']), "parent.apply()") + '</td>' +
             '<td><img src="imx/spacer.gif" width="6" height="1"></td>' +
            '<td>' + main.nbutton('', i18n['SetPermissions cancel'], "parent.cancel()") + '</td>' +
            '<td><img src="imx/spacer.gif" width="6" height="1"></td>' +
            '<td>' + main.nbutton('', i18n['SetPermissions Help'], "main.help(1005136)") + '</td>' +
            '<td><img src="imx/spacer.gif" width="17" height="1"></td>' +
          '</tr>' +
        '</table>' +
      '</td>' +
    '</tr>' +
  '</table></form>'

  return s
}

function getSingleFilter(rule, a, v) {
  var s = new String()
  if (rule == '$a=*$v*')
    s += v != '' ? '(' + a + '=*' + v + '*)' : s += '(' + a + '=*)';
  else if (rule == '$a=$v')
    s += '(' + a + '=' + v + ')'
  else if (rule == '$a~=$v')
    s += '(' + a + '~=' + v + ')'
  else if (rule == '$a=*$v')
    s += '(' + a + '=*' + v + ')'
  else if (rule == '$a=$v*')
    s += '(' + a + '=' + v + '*)'
  return s
}


function getFilter(rule, a, v, not, ldapurl) {
  var s = new String()
  v = main.escape_crlf(v);
  if (not)
    s += '(!'
  var x = new Array();
  /* PAB doesn't like composite filters */
  if (ldapurl != "3 200 1") {
      if (a == "mail") {
          /* if searching for email also search for aliases */
          x = new Array("mailalternateaddress", "unused");
      } else {
          /* if searching for lang attribute also search for
           * non language-tagged version */
          x = a.split(";");
      }
  }
  if (x.length > 1) {
    s += "(|";
    s += getSingleFilter(rule, x[0], v);
    s += getSingleFilter(rule, a, v);
    s += ")";
  } else {
    s += getSingleFilter(rule, a, v);
  }
  if (not)
    s += ')'
  return s
}

function doSearch() {
  if (orphaned()) {
    alert(i18n['timeout']);
    self.close();
  }
 
  var f = ctrl.form;
  var rule = f.rule.options[f.rule.selectedIndex].value;
  var a = f.attr.options[f.attr.selectedIndex].value;
  var v = f.string.value;

  if( v.length == 0) return;

  ldap_url = "3 200";

  if(ldap_url==i18n['localdirsearch']){a=a.split(";")[0]} 
  if (rule == 'contains')
    ldap_filter = getFilter('$a=*$v*', a, v, false, ldap_url);
  else if (rule == 'does not')
    ldap_filter = getFilter('$a=*$v*', a, v, true, ldap_url);
  else if (rule == 'equal')
    ldap_filter = getFilter('$a=$v', a, v, false, ldap_url);
  else if (rule == 'not equal')
    ldap_filter = getFilter('$a=$v', a, v, true, ldap_url);
  else if (rule == 'sound')
    ldap_filter = getFilter('$a~=$v', a, v, false, ldap_url);
  else if (rule == 'begin')
    ldap_filter = getFilter('$a=$v*', a, v, false, ldap_url);
  else if (rule == 'end')
    ldap_filter = getFilter('$a=*$v', a, v, false, ldap_url);
  else
    ldap_filter = getFilter('$a=*$v*', a, v, false, ldap_url);

  ldap_from = 0;
  submit();
  return false;
}

function getSearchResults() {
  
  var s = '&nbsp;'

  if(searchResults == 0)
     return s;
  if (orphaned()) {
    self.close();
  }
  
  var f = ctrl.form;
  var list = data.addr;
  var blank = '<td>' + nbsp + '</td>\n';
  var a = split(ldap_url, ' ');
  var pab = a.length == 3 && ldap_url.indexOf('ldap://') == -1;

  if (!data.addr || data.addr.length == 0) {
    return '<table border=0 cellpadding=2 cellspacing=1 width=100%>\n' +
      '<tr bgcolor=' + main.chrome2 + '><td width=1%>&nbsp;</td></tr>\n' +
      '<tr ' + main.cellBgString + '>' +
      '<td align=center width=1% nowrap>' + main.font() +
      main.i18n_ldap_no_match(
        f.attr.options[f.attr.selectedIndex].text,
	f.rule.options[f.rule.selectedIndex].text,
	f.string.value) +
      '</tr>\n' +
      '<tr bgcolor=' + main.chrome2 + '><td width=1%>&nbsp;</td></tr>\n' +
      '</table>\n';
  }
  s = '<form Accept-Charset="UTF-8" name="form">'
  s += main.tableStart
  addrs = 0;
  s += '<tr bgcolor=' + main.chrome2 + '>\n' +
  '<td align=left>' + main.font() + i18n['subscribe select'] + '</td>\n' +
  '<td align=left>' + main.font() + i18n['subscribe User ID'] + '</td>\n' +
  '<td align=left>' + main.font() + i18n['subscribe Name'] + '</td>\n' +
  '<td align=left>' + main.font() + i18n['subscribe E-Mail'] + '</td></tr>\n'
  for (var i = 0; i < list.length; i++) {
       if(list[i].uid)
             {
                  if(list[i].uid == main.cfg.uid )
                    continue
                   s += '<tr ' + main.cellBgString + '>\n' +
                  '<td align="center">' + main.font() +
                  '<input type=checkbox name="folder' + i + '">' +
                  '</td>\n' +
                  '<td align="left">' + main.font() + list[i].uid + '</td>\n' +
                  '<td align="left">' + main.font() + list[i].cn + '</td>\n' +
                  '<td align="left">' + main.font() + list[i].mail + '</td>\n' +
                  '</tr>\n'
                  addrs++
             }
  }
  if(addrs == 0)
  {
     return '<table border=0 cellpadding=2 cellspacing=1 width=100%>\n' +
      '<tr bgcolor=' + main.chrome2 + '><td width=1%>&nbsp;</td></tr>\n' +
      '<tr ' + main.cellBgString + '>' +
      '<td align=center width=1% nowrap>' + main.font() +
      main.i18n_ldap_no_match(
        f.attr.options[f.attr.selectedIndex].text,
        f.rule.options[f.rule.selectedIndex].text,
        f.string.value) +
      '</tr>\n' +
      '<tr bgcolor=' + main.chrome2 + '><td width=1%>&nbsp;</td></tr>\n' +
      '</table>\n';
   
  }
  s += '<tr bgcolor=' + main.chrome2 + '>' +
       '<td colspan="4">&nbsp;</td></tr>' 
  s +=  '</table></form>\n';
  reload(btn);
  return s;
}


function submit() {
  if (orphaned()) {
    self.close();
  } else {
    addrs = 0;
    var a = split(ldap_url,' ')
    mailaccountfilter = "(mailuserstatus=active)"
    nogroupsfilter = "(objectclass=inetmailuser)"
    mailhostfilter = "(mailhost=" + main.cfg.mailhost + ")"
    tfr =  "(&" + mailaccountfilter + nogroupsfilter + mailhostfilter + ldap_filter + ")"
    load(data, main.msgHREF + 'ldap.msc?sid=' + main.sid +
      '&start=' + ldap_from +
      '&count=' + ldap_step +
      '&filter=' + encode(tfr) +
      '&ldapurl=' + '' +
      '&version=' + a[0]);
  }
}

function apply()
{
  if (!data.addr) return
  var k =0;
  if (data.addr.length == 0)
   {
     self.close()
     return;
   }
  var f = idx.form;
  var  uidlist = new Array()
  var j = 0;
  for(i = 0; i < data.addr.length; i++)
   {
     if(data.addr[i].uid && (data.addr[i].uid != main.cfg.uid)){
      if(f[k].checked == true)
        {
               if(window.opener.defaultdomain == 1)
               	uidlist[j] =  new String(data.addr[i].uid)
               else
               	uidlist[j] =  new String(data.addr[i].uid)  + '@' + main.cfgFrame.mailDomain
               j++;

        }
      k++
    }
   }
   window.opener.AddUsersFromSearchWindow(uidlist)


}


function ok() {
  if (!data.addr) return
  var k =0;
  if (data.addr.length == 0)
   {
     self.close()
     return;
   }
  var f = idx.form;
  var  uidlist = new Array()
  var j = 0;
  for(i = 0; i < data.addr.length; i++)
   {
     if(data.addr[i].uid && (data.addr[i].uid != main.cfg.uid)){
      if(f[k].checked == true)
        {
               if(window.opener.defaultdomain == 1)
               	uidlist[j] =  new String(data.addr[i].uid)
               else
               	uidlist[j] =  new String(data.addr[i].uid)  + '@' + main.cfgFrame.mailDomain
               j++;

        }
      k++
    }
   }
   window.opener.AddUsersFromSearchWindow(uidlist)
   main.lookupWindow = null
   window.opener.lookupWindow = null
   self.close()

}

function noop() {
}

function cancel() {
  main.lookupWindow = null
  window.opener.lookupWindow = null 
  self.close();
}

function ldapCB() {
  if (data.color)
    data.color(main.chrome2);
  if (data.errno)
    alert(data.errstr);
  if (orphaned()) {
    self.close();
  } 
  
  searchResults = 1;
  addrs = 0; 
  reload(idx);
}

function timeoutCB() {
  self.close();
}

function change() {
  if (data.color)
    data.color(main.chrome2);
}

function afterFetch() {
  doSearch();
}
