﻿    // AJAXAutoComplete for ASP.NET 2.0 - v1.0, 2006-04-20
    // Wayne Hunt - http://blog.ourcurrentfuture.com

// ** function getObj(name) **
// parameters: name = id of the object to get
// performs: sets the variable to the DOM object associated with the ID
// returns: object (x)
function getObj(strName)
{
  if (document.getElementById)
  {
    return document.getElementById(strName);
  }
  else if (document.all)
  {
    return document.all[strName];
  }
  else if (document.layers)
  {
    return document.layers[strName];
  } 
  else 
  {
    return null;
  }
 }


// ** function fill AutoComplete **
// parameters:  result - pipe-delimited list of matching entries
// performs: Clears list, then fills matching values
// returns: none
function fillAutoComplete(strResult) 
{
    var divResults = getObj('AutoCompleteResults');
    //divResults.style.display = "none";
    strFields = strResult.split('|');  // split the results into a string[]
    var strOptionString = "";
    if((strFields.length-2) >=1 && strFields[0] != "")
    {
        
        var strOptionID;
        for (var i = 0; i <  strFields.length-2; i++)
        {
                var searchvalue=strFields[i];
                searchvalue= searchvalue.replace("<b>","");
                searchvalue=searchvalue.replace("</b>","");
                searchvalue=searchvalue.replace("<font color='Red'>","");
                searchvalue=searchvalue.replace("</font>","");
                searchvalue= searchvalue.replace("<b>","");
                searchvalue=searchvalue.replace("</b>","");
                searchvalue=searchvalue.replace("<font color='Red'>","");
                searchvalue=searchvalue.replace("</font>","");
                strOptionString += "<a href=\"http://emart.manoramaonline.com/search.aspx?catname=books&srchkey=Title&srchVal=" + searchvalue + "\"><div class=\"listOption\" onmouseover=\"this.className='listOptionHighlighted'\" onmouseout=\"this.className='listOption'\">" + strFields[i] + "</div></a>\n"; // add Results to result group
        }

        var txtQuery = getObj('ctl00_searchVal');
        var len=strFields[strFields.length-2].split('=')[1];
        //alert(getObj('ctl00_AutoSuggestResult'));
        var result=getObj('ctl00_AutoSuggestResult');
        if((txtQuery.value.length==parseInt(len) || txtQuery.value.length==parseInt(len-1) || txtQuery.value.length==parseInt(len-2))  && strFields.length>2)
        {
             divResults.style.display = "block";
             result.value=strOptionString;
             //divResults.innerHTML = strOptionString;
        }
        if(parseInt(len)!=2)
        {
        if(result.value!='')
                divResults.innerHTML = strOptionString;
        else
                divResults.style.display = "none";
        }
//        else
//       {
//        divResults.style.display = "none";
//        }
        if(txtQuery.value.length==0)
        {
         divResults.style.display = "none";
        }
    }
    else 
    {
        divResults.style.display = "none";
    }
}

// ** function Choose **
// parameters:  strSelectedTerm - selected value
// performs: Fills Querybox with selected value, and submits if AutoPostBack is enabled
function Choose(strSelectedTerm)
{
  var txtQuery = getObj(strQueryBoxName);
  txtQuery.value = strSelectedTerm;

  var divResults = getObj(strResultDivName);
  divResults.style.display = "none";

  if(blnAutoPostBack)
  {
    //var submitButton = getObj(strSubmitButtonName);
    //submitButton.click();
  }

}
// ** function onError(message) **
// parameters:  message - error message to be appended
// performs: pops up a Javascript alert when there's an error
// returns: none
function onError(strMessage) 
{
    alert("Error:\n" + strMessage);
}


// ** Hiding the result result div **//
document.onclick=check; 
function check(e)
{ 
    var target = (e && e.target) || (event && event.srcElement); 
    document.getElementById('AutoCompleteResults').style.display = "none";
}
