function getQueryString(){
  var m,na,nb,o,p,q;
  m=document.location.search;
  na=0;
  if (m.charAt(0)=="?")
    na=1;
  o="";
  while (na<m.length-1){
    nb=na;
    while (nb<m.length){
      if (m.charAt(nb)=="&")
        break;
      nb++;
    }
    if (m.charAt(na)=="q"){
      na++;
      if (m.charAt(na)=="="){
        na++;
        o=m.substring(na,nb);
        break;
      }
    }
    na=nb+1;
  }
  p=0;
  while (p<o.length){
    if (o.charAt(p)=="+"){
      o=o.substring(0,p)+" "+o.substring(p+1,o.length);
      p++;
    }else{
      p++;
    }
  }
  p=0;
  while (p<o.length){
    if (o.charAt(p)!=" ")
      break;
    p++;
  }
  if (p!=0)
    o=o.substring(p,o.length);
  p=o.length-1;
  while (p>=0){
    if (o.charAt(p)!=" ")
      break;
    p--;
  }
  p++;
  if (p!=o.length)
    o=o.substring(0,p);
  p=0;
  while (p<o.length){
    while (p<o.length){
      if (o.charAt(p)==" ")
        break;
      p++;
    }
    if (p==o.length)
      break;
    p++;
    q=p;
    while (o.charAt(q)==" ")
      q++;
    if (p!=q)
      o=o.substring(0,p)+o.substring(q,o.length);
  }
  return o;
}

function doSearch(){
  var ma,mb,n,pa,pb,qa,qb,ra,rb,s;
  var specname,libtiffname,codedec,codehex,shortdescription,url;
  ma=getQueryString();
  if (ma.length>0){
    document.writeln("<br><br>");
    qa=0;
    qb=0;
    mb=ma.toLowerCase()+"|";
    s=-1;
    for (n=0;n<profiles.length;n++){
      if (n==ifdstart[s+1]){
        if (qb==1){
          document.writeln("</tbody>");
          document.writeln("</table><br><br>");
          qb=0;
        }
        s++;
      }
      pa=0;
      while (profiles[n].charAt(pa)!="|"){
        pb=pa;
        while (profiles[n].charAt(pb)!="|")
          pb++;
        pb++;
        if (mb.length==pb-pa){
          if (mb==profiles[n].substring(pa,pb).toLowerCase()){
            if (qb==0){
              document.writeln("<b>Search results in "+ifdname[s]+" IFD</b><br><br><table cellspacing=0 cellpadding=4 border=1>");
              document.writeln("<tr><th colspan=2>Code</th><th rowspan=2>Name</th><th rowspan=2>Short description</th></tr>");
              document.writeln("<tr><th>Dec</th><th>Hex</th></tr>");
              document.writeln("<tbody align=left valign=top>");
              qb=1;
            }
            ra=0;
            rb=0;
            while (profiles[n].charAt(rb)!="|")
              rb++;
            specname=profiles[n].substring(0,rb);
            rb++;
            ra=rb;
            while (profiles[n].charAt(rb)!="|")
              rb++;
            libtiffname=profiles[n].substring(ra,rb);
            rb++;
            ra=rb;
            while (profiles[n].charAt(rb)!="|")
              rb++;
            codedec=profiles[n].substring(ra,rb);
            rb++;
            ra=rb;
            while (profiles[n].charAt(rb)!="|")
              rb++;
            codehex=profiles[n].substring(ra,rb);
            rb++;
            ra=rb;
            while (profiles[n].charAt(ra)!="|"){
              while (profiles[n].charAt(rb)!="|")
                rb++;
              rb++;
              ra=rb;
            }
            ra++;
            shortdescription=profiles[n].substring(ra,profiles[n].length);
            url=specname.toLowerCase();
            ra=0;
            while (ra<url.length){
              if (url.charAt(ra)==" "){
                url=url.substring(0,ra)+url.substring(ra+1,url.length);
              }else{
                ra++;
              }
            }
            document.writeln("<tr><td>"+codedec+"</td><td>"+codehex+"</td><td nowrap><a href="+ifddir[s]+url+".html>"+specname+"</a></td><td>"+shortdescription+"</td></tr>");
            qa++;
            break;
          }
        }
        pa=pb;
      }
    }
    if (qb==1){
      document.writeln("</tbody>");
      document.writeln("</table><br><br>");
    }
    document.writeln("<b>Search query: '"+ma+"'</b><br>");
    if (qa==0)
      document.writeln("<b>No Search results.</b>");
    if (qa==1)
      document.writeln("<b>1 Search result.</b>");
    if (qa>1)
      document.writeln("<b>"+qa+" Search results.</b>");
  }
}
