var isIE, isMozilla, isSafari, isOpera = false;
var np = 0;
var autocomplete_status = 'idle';
var re = '';
var autocompleteXML = '';
var staff = undefined;
var boxSize = '';

function getXMLHTTPObject(){
    var objhttp=(window.XMLHttpRequest)?new XMLHttpRequest():new ActiveXObject('Microsoft.XMLHTTP');
    if(!objhttp){return};
    if (navigator.userAgent.indexOf('MSIE') != -1)
        isIE = true;
    else if (navigator.userAgent.indexOf('Safari') != -1)
        isSafari = true;
    else if (navigator.userAgent.indexOf('Opera') != -1)
        isOpera = true;
    else
        isMozilla = true;
    return objhttp;
};

function sendACRequest(url,data,method,header,calltype){
    objhttp=getXMLHTTPObject();
    if ( !isIE ) {
       objhttp.onreadystatechange = processReqChange(objhttp,calltype);
    }
    if(!url){url='/cms/staffsearch?xml=autocomplete'};
    if(!data){data=''};
    if(!method){method='get'};
    if(!header){header='Content-Type:text/html; charset=iso-8859-1'};
    objhttp.open(method,url,false);
    objhttp.setRequestHeader(header.split(':')[0],header.split(':')[1]);
    objhttp.send(null);
    return objhttp;
};

function processReqChange(req,calltype) {
    if (req.readyState == 4) {

    }
};

function autoSuggest(e,searchObj,sFlds) {
   if ( autocomplete_status == 'idle' ) {
      init_data();
   }
   e=(window.event) ? event : e;

   var getKey = (e.keyCode) ? e.keyCode: e.charCode;

   if (!(getKey >= 65 && getKey <= 90) && !(getKey >= 97 && getKey <= 122) && !(getKey == 8 || getKey == 32)) return false;
   var a = document.getElementById("autosuggest");
   a.style.display = "block";
   a.style.zIndex = 100;
   a.innerHTML = "";
/*<img src='/cms/photo/icons/indicator.gif' alt='Searching' title='Searching'/> Searching";*/
   if ( !boxSize ) {/* store orig size */
       var computedStyle='';
       if (isIE) {computedStyle=a.currentStyle;}
       else {computedStyle=document.defaultView.getComputedStyle(a,'');}
       boxSize=computedStyle.height;
       if( boxSize == '0px' ) { boxSize = '100px'}
   }
   var c=new Array();
   var s=searchObj ? searchObj.value : undefined;
   if (s.length<2) {a.style.display="none";return false;}
   for(var b=0; b<staff.length; b++) {
      /*var j = staff[b].getElementsByTagName('ln')[0].firstChild.nodeValue.toLowerCase();
      var k = staff[b].getElementsByTagName('fn')[0].firstChild.nodeValue.toLowerCase();*/ 
      var x = staff[b].getElementsByTagName('nm')[0].firstChild.nodeValue.toLowerCase();
      /*if (j.indexOf(s.toLowerCase()) > -1 || k.indexOf(s.toLowerCase()) > -1 ) {*/
      if (x.indexOf(s.toLowerCase()) > -1 ) {
         c.push(staff[b]);
      }
   }
   var p = autocompleteXML.getElementsByTagName("profile_url")[0].firstChild.nodeValue;
   if (c.length > 0) { a.innerHTML = "";a.style.height=boxSize;} else { a.innerHTML = 'No results found'; a.style.overflowY='hidden';a.style.height='20px';};
   var matches=0;
   for (var d=0;d<c.length;d++) {
      var f = new RegExp("(" + s + ")", 'gi');
      var g = c[d].getElementsByTagName('fn')[0].firstChild.nodeValue+' '+c[d].getElementsByTagName('ln')[0].firstChild.nodeValue;
      var l = c[d].getElementsByTagName('un')[0].firstChild.nodeValue
      a.innerHTML += '<a href="' + p + l + '">' + g + "</a><br/>";
      matches++;
   }
   if ( matches > 3 ) {a.style.overflowY='scroll';}else {a.style.overflowY='hidden'}; 
   if ( matches > 1 ) {a.style.height=boxSize}
   if ( matches == 1 ) {a.style.height='20px'}
}

function init_data() {
    autocomplete_status = 'pending';
    var searchRequestURL = '/cms/staffsearch?xml=autocomplete';
    myRequest = sendACRequest(searchRequestURL,'','get','');
    autocompleteXML = '';
    if ( myRequest.responseXML) {
       /*document.getElementById("entries").innerHTML='';*/
       autocompleteXML = myRequest.responseXML;
       staff = autocompleteXML.getElementsByTagName("staff");
       /*for ( i=0; i<staff.length; i++ ) {
           var name = staff[i].getElementsByTagName('nm')[0].firstChild.nodeValue;
           var firstname = staff[i].getElementsByTagName('fn')[0].firstChild.nodeValue;
           var lastname = staff[i].getElementsByTagName('ln')[0].firstChild.nodeValue; 
           document.getElementById("entries").innerHTML += '<a>' + lastname + "</an>";
       }*/
       autocomplete_status = 'ready';
    }
}
