﻿
var Request = null;
var Textbox = null;
var SuggestionList =  null;
var SuggestionIndex = -1;
var SuggestionCount = 0;
var organizationID = 0;

function OnLoad(orgID)
{
  organizationID = orgID;
  if (document.getElementById("ctl00_ContentPlaceHolder_SEARCHCONTROL_searchKeywords") != null)
  {
    Textbox = new AutoCompleteTextbox(document.getElementById("ctl00_ContentPlaceHolder_SEARCHCONTROL_searchKeywords"));
  }
  else
  {
    Textbox = new AutoCompleteTextbox(document.getElementById("ctl00_SEARCHCONTROL_searchKeywords"));
  }
  SuggestionList = new AutoCompleteSuggestionList();
  Request = CreateXMLHttpRequest();
}

function AutoCompleteTextbox(textbox)
{
  this.Element = textbox;
  this.Initialize();
}

AutoCompleteTextbox.prototype.Initialize = function ()
{
  var act = this;
  this.Element.value = 'Enter a Symptom, Condition, Medication, Procedure, or General Topic...';
  this.Element.onclick = function (onclickEvent)
  {
    act.HandleClickEvent(GetEvent(onclickEvent));
  }
  this.Element.onkeyup = function (onkeyupEvent)
  {
    act.HandleKeyUpEvent(GetEvent(onkeyupEvent));
  }
  this.Element.onkeydown = function (onkeydownEvent)
  {
    act.HandleKeyDownEvent(GetEvent(onkeydownEvent));
  }
  this.Element.onblur = function (onblurEvent)
  {
    act.HandleBlurEvent(GetEvent(onblurEvent));
  }
  this.Element.focus();
}

AutoCompleteTextbox.prototype.HandleClickEvent = function (curEvent)
{
  if (this.Element.value == 'Enter a Symptom, Condition, Medication, Procedure, or General Topic...')
  {
    this.Element.value = '';
  }
}

AutoCompleteTextbox.prototype.HandleKeyUpEvent = function (curEvent)
{
  if (this.Element.value.length == 0) return;
  var code = curEvent.keyCode;

  // up arrow
  if (code == 38)
  {
    if ((SuggestionIndex > 0) && (SuggestionIndex <= (SuggestionCount - 1)))
    {
      SuggestionIndex--;
      SuggestionList.HighlightItem();
    }
  }
  // down arrow
  else if (code == 40)
  {
    if ((SuggestionIndex >= -1) && (SuggestionIndex < (SuggestionCount - 1)))
   {
     SuggestionIndex++;
     SuggestionList.HighlightItem();
   }
  }
  // enter key
  else if (code == 13)
  {
    SuggestionList.Hide();
  }
  // ESC key - Firefox only so far!
  else if (code == 27)
  {
    SuggestionList.Hide();
  }
  // backspace 8, delete 46
  else if ((code < 32 && code != 8 && code != 16) || (code >= 33 && code < 46) || (code >= 112 && code <= 123))
  {
    // do nothing
  }
  else
  {
    this.GetSuggestions();
  }
}

AutoCompleteTextbox.prototype.HandleKeyDownEvent = function (curEvent)
{
}

AutoCompleteTextbox.prototype.HandleBlurEvent = function (curEvent)
{
}

AutoCompleteTextbox.prototype.GetSuggestions = function ()
{
  var url = GetDomainPath("Services/AjaxService.aspx");
  Request.open("POST", url, true);
  Request.onreadystatechange = function ()
  {
    if (Request.readyState == 4)
    {
      if (Request.status == 200)
      {
        SuggestionList.Hide();
        SuggestionIndex = -1;
        SuggestionCount = 0;

        var rawResults = Request.responseText;
        var resultArray = rawResults.split("|");

        // if no results are returned, the web service returns the string "null"
        if (rawResults == "null") { return; }

        for (var loop=0; loop<resultArray.length; loop++)
        {
          var itemArray = resultArray[loop].split("$");
          var div = new AutoCompleteSuggestedItem(loop, itemArray[0], itemArray[1], itemArray[2], itemArray[3]);
          SuggestionList.Element.appendChild(div.Element);
          SuggestionCount++;
        }

        div = document.createElement("div");
        div.className = "hideSuggestionsLink";
        div.innerHTML = "<a href=\"javascript:SuggestionList.Hide();\" onMouseOver=\"return HideStatusBarText();\">Close</a>";
        SuggestionList.Element.appendChild(div);
        SuggestionList.Show();
      }
    }
  }
  Request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  Request.send("s=GetAutoCompleteSuggestions&o=" + organizationID + "&x=" + this.Element.value);
}

function AutoCompleteSuggestionList()
{
  this.Element = document.createElement("div");
  this.Element.ID = "searchSuggestions";
  this.Element.style.left = this.GetLeft() + "px";
  this.Element.style.top = this.GetTop() + "px";
  this.Hide();
  this.Initialize();
  document.body.appendChild(this.Element);
}

AutoCompleteSuggestionList.prototype.Initialize = function ()
{
  var acs = this;
  this.Element.onmouseover = function (onmouseoverEvent)
  {
    acs.HandleMouseOverEvent(GetEvent(onmouseoverEvent));
  }
  this.Element.onmouseout = function (onmouseoutEvent)
  {
    acs.HandleMouseOutEvent(GetEvent(onmouseoutEvent));
  }
}

AutoCompleteSuggestionList.prototype.HandleMouseOverEvent = function (curEvent)
{
}

AutoCompleteSuggestionList.prototype.HandleMouseOutEvent = function (curEvent)
{
}

AutoCompleteSuggestionList.prototype.GetLeft = function ()
{
  var left = 0;
  var node = Textbox.Element;
  while (node != document.body)
  {
    left += node.offsetLeft;
    node = node.offsetParent;
  }
  return left;
}

AutoCompleteSuggestionList.prototype.GetTop = function ()
{
  var top = 0;
  var node = Textbox.Element;
  while (node != document.body)
  {
    top += node.offsetTop;
    node = node.offsetParent;
  }
  top += Textbox.Element.offsetHeight;
  return top;
}

AutoCompleteSuggestionList.prototype.HighlightItem = function ()
{
  for (var x=0; x<SuggestionCount; x++)
  {
    if (x == SuggestionIndex)
    {
      this.Element.childNodes[x].className = "suggestedItemOn";
      Textbox.Element.value = this.Element.childNodes[x].firstChild.innerHTML;
    }
    else
    {
      this.Element.childNodes[x].className = "suggestedItemOff";
    }
  }
}

AutoCompleteSuggestionList.prototype.Hide = function ()
{
  this.Element.innerHTML = "";
  this.Element.className = "suggestionListOff";
}

AutoCompleteSuggestionList.prototype.Show = function ()
{
  this.Element.className = "suggestionListOn";
}

function AutoCompleteSuggestedItem(id, language, originalKeyword, keyword, count)
{
  this.Language = language;
  this.OriginalKeyword = originalKeyword;
  this.Keyword = keyword;
  this.Count = count;  
  this.Element = document.createElement("div");
  this.Element.ID = "suggestedItem" + id;
  this.Element.className = "suggestedItemOff";
  this.Element.innerHTML = "<span class=\"suggestedItemKeyword\">" + this.Keyword + "</span><span class=\"suggestedItemMeta\">" + this.Count + " results</span>";
  this.Initialize();
}

AutoCompleteSuggestedItem.prototype.Initialize = function ()
{
  var item = this;
  this.Element.onmouseover = function (onmouseoverEvent)
  {
    item.HandleMouseOverEvent(GetEvent(onmouseoverEvent));
  }
  this.Element.onmouseout = function (onmouseoutEvent)
  {
    item.HandleMouseOutEvent(GetEvent(onmouseoutEvent));
  }
  this.Element.onclick = function (onclickEvent)
  {
    item.HandleClickEvent(GetEvent(onclickEvent));
  }
}

AutoCompleteSuggestedItem.prototype.HandleMouseOverEvent = function (curEvent)
{
  this.Element.className = "suggestedItemOn";
}

AutoCompleteSuggestedItem.prototype.HandleMouseOutEvent = function (curEvent)
{
  this.Element.className = "suggestedItemOff";
}

AutoCompleteSuggestedItem.prototype.HandleClickEvent = function (curEvent)
{
  Textbox.Element.value = this.OriginalKeyword;
  SuggestionList.Hide();
  parent.window.location.href = GetDomainPath("Results.aspx") + "?l=" + this.Language + "&k=" + this.OriginalKeyword;
}

function CreateXMLHttpRequest()
{
  var request = null;
  if (window.ActiveXObject)
  {
    try
    {
      request = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (ex1)
    {
      try
      {
        request = new ActiveXObject("Msxml2.XMLHTTP");
      }
      catch (ex2)
      {
        request = null;
      }
    }
  }
  else if (window.XMLHttpRequest)
  {
    try
    {
      request = new XMLHttpRequest();
    }
    catch (ex3)
    {
      request = null;
    }
  }
  return request;
}

function ClearResults()
{
  SuggestionList.Hide();
}

function HideStatusBarText()
{
  window.status = "";
  return true;
}

function GetDomainPath(path)
{
  var domain = document.domain;
  if (domain == "localhost")
  {
    domain = "http://localhost/omedixpractice/";
  }
  else
  {
    domain = "http://" + domain + "/";
  }
  domain += path;
  return domain;
}

// returns the last event.  Uses window.event for IE browsers.
function GetEvent(curEvent)
{
  if (!curEvent)
  {
    curEvent = window.event;
  }
  return curEvent;
}

// returns the type of the last event.
function GetEventType(curEvent)
{
  curEvent = GetEvent(curEvent);
  return curEvent.type;
}

// returns the target HTML element of the last event.
function GetEventTarget(curEvent)
{
  var target;
  curEvent = GetEvent(curEvent);
  if (curEvent.target)
  {
    target = curEvent.target;
  }
  else if (curEvent.srcElement)
  {
    target = curEvent.srcElement;
  }
  // special handling for Safari
  if (target.nodeType == 3)
  {
    target = target.parentNode;
  }
  return curEvent.type;
}

// returns the code of the key pressed during the last event.
function GetEventKeyCode(curEvent)
{
	var code;
	curEvent = GetEvent(curEvent);
  if (curEvent.keyCode)
  {
    code = curEvent.keyCode;
  }
  else if (curEvent.which)
  {
    code = curEvent.which;
  }
	return code;
}

// returns the string version of the key pressed during the last event.
function GetEventKeyChar(curEvent)
{
	var code;
	curEvent = GetEvent(curEvent);
  if (curEvent.keyCode)
  {
    code = curEvent.keyCode;
  }
  else if (curEvent.which)
  {
    code = curEvent.which;
  }
	return String.fromCharCode(code);
}

// returns the X and Y coordinates of the mouse pointer as an ordered pair (x,y)
function GetMouseCoordinates(curEvent)
{
  var x = 0;
  var y = 0;
  curEvent = GetEvent(curEvent);

  // works for IE 7
  if (curEvent.clientX || curEvent.clientY)
  {
    x = curEvent.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
    y = curEvent.clientY + document.body.scrollTop + document.documentElement.scrollTop;
  }
  // supposed to work for FF but doesn't - find another method
  else if (curEvent.pageX || curEvent.pageY)
  {
    x = curEvent.pageX;
    y = curEvent.pageY;
  }
  return x + "," + y;
}
