﻿//-----------------------------------------------------------------------------
// XSearch
//
// Copyright 2005-2010 - Xcential Group LLC.
//
//-----------------------------------------------------------------------------

XSearch.prototype = new XControl;
XSearch.prototype.constructor = XSearch;

//=============================================================================
// Constructor

function XSearch(
   id,
   width,
   title,
   doFunction,
   create
)
{
   id = (id == null) ? null : id;
   width = (width == null) ? null : width;
   title = (title == null) ? null : title;
   doFunction = (doFunction == null) ? null : doFunction;
   create = (create == null) ? true : false;

   if (create)
      return new XSearch(id, width, title, doFunction, false);

   //--------------------------------------------------------------------------
   // Private Interface

   //--------------------------------------------------------------------------
   // Privileged Interface

   this.valueOf = function()
   {

      return oSpec;
   }

   //--------------------------------------------------------------------------

   this.setObjectValue = function(
      id,
      width,
      title,
      doFunction
   )
   {
      id = (id == null) ? null : id;

      oSpec = XSearch.getSpecFrom(id, width, title, doFunction);

      return oSpec;
   }

   //--------------------------------------------------------------------------
   // Initialization

   var oSpec = this.setObjectValue(id, width);

   XControl.controls[oSpec.id] = this;

}

XSearch.prototype.objectClass = "XSearch";

//=============================================================================
// Static Interface

XSearch.getSpecFrom = function(
   id,
   width,
   title,
   doFunction
)
{
   id = (id == null) ? XUtils.generateId("urn:xcential-com:search:", XUtils.SCHEME_RANDOM) : id;
   width = (width == null) ? 300 : width;
   title = (title == null) ? "Search" : title;
   doFunction = (doFunction == null) ? null : doFunction;

   var spec = new Array();
   spec.id = id;
   spec.className = "XSearch";
   spec.width = width;
   spec.title = title;
   spec.doFunction = doFunction;
   spec.components = new Array();

   return spec;
}

XSearch.DROP_DOWN = "drop_down";
XSearch.TEXT_BOX  = "text_box";
XSearch.CHECK_BOX = "check_box";

//=============================================================================
// Event Handlers


//=============================================================================
// Public Interface

XSearch.prototype.setClass = function(
   className
)
{
   className = (className == null) ? "XSearch" : className;

   var spec = this.valueOf();
   spec.className = className;

   return className;
}

//-----------------------------------------------------------------------------

XSearch.prototype.addDropDown = function(
   id,
   defaultValue,
   width,
   title
)
{
   defaultValue = (defaultValue == null) ? "" : defaultValue;
   width = (width == null) ? 50 : width;
   title = (title == null) ? null : title;

   var spec = this.valueOf();

   var component = new Array();
   component.type = XSearch.DROP_DOWN;
   component.id = id;
   component.defaultValue = defaultValue;
   component.width = width;
   component.title = title;

   spec.components.push(component);

}

//-----------------------------------------------------------------------------

XSearch.prototype.addTextBox = function(
   id,
   defaultValue,
   width,
   title
)
{
   defaultValue = (defaultValue == null) ? "" : defaultValue;
   width = (width == null) ? 50 : width;
   title = (title == null) ? null : title;

   var spec = this.valueOf();

   var component = new Array();
   component.type = XSearch.TEXT_BOX;
   component.id = id;
   component.defaultValue = defaultValue;
   component.width = width;
   component.title = title;

   spec.components.push(component);

}

//-----------------------------------------------------------------------------

XSearch.prototype.toHTML = function(
   state
)
{
   state = (state == null) ? [] : state;

   var spec = this.valueOf();

   var controlHTML = "";

   controlHTML += "<span " +
      "class=\"LookupGroup InlineBlock\" " +
      "style=\"position: relative; border: solid silver 1px; width: " + spec.width + "px; height: 22px;\">";

   var left = 10;
   for (var i=0; i<spec.components.length; i++)
   {
      var component = spec.components[i];

      switch (component.type)
      {
         case XSearch.DROP_DOWN:
            controlHTML += "<span " +
               "id=\"" + spec.id + "\" " +
               "style=\"position: absolute; top: 2px; left: " + left + "px; width: " + component.width + "px;\">";
            controlHTML += "<span " +
               "id=\"" + spec.id + "." + component.id + ".value\">" + component.defaultValue + "</span>";
            controlHTML += "<img " +
               "id=\"" + spec.id + "." + component.id + ".selector\" " +
               "style=\"" +
                  "margin-left: 4px; " +
               "\" " +
               "src=\"/app/pkgs/xfw/images/Down.Blue.gif\" " +
               "title=\"" + spec.title + "\"/>";
            controlHTML += "</span>";
            left += component.width;
            break;
         case XSearch.TEXT_BOX:
            controlHTML += "<input " +
               "type=\"text\" " +
               "class=\"Input\" " +
               "id=\"" + spec.id + "." + component.id + "\" " +
               "name=\"" + spec.id + "." + component.id + "\" " +
               "style=\"" +
                  "position: absolute; " +
                  "top: 2px; " +
                  "left: " + left + "px; " +
                  "width: " + component.width + "px; " +
                  "height: 12px; " +
                  "border: solid white 0px; " +
               "\" " +
               "value=\"" + component.defaultValue + "\" " +
               "onkeydown=\"doKeyDown(event)\" " +
               "onfocus=\"XUI('" + spec.id + "." + component.id + "').clearTitle()\" " +
               "onblur=\"XUI('" + spec.id + "." + component.id + "').setTitle('" + component.defaultValue + "')\"/>"
            left += component.width;
            break;
         case XSearch.CHECK_BOX:
            break;
      }
   }

   controlHTML += "&#160;";
   controlHTML += "<img " +
      "class=\"LinkImage\" " +
      "src=\"/app/pkgs/xfw/images/SearchButton.gif\" " +
      "style=\"position: absolute; right: 1px; top: 1px;\" " +
      "title=\"" + spec.title + "\" " +
      "onClick=\"doLookup(event)\"/>";
   controlHTML += "</span>";

   return controlHTML;
}

//=============================================================================

