/**
 * RecordListSearchFilter
 */
function RecordListSearchFilter(id, text)
{
  this.id = id
  this.text = text;
  this.defaultValue = null;
  this.value = null;
  this.parent = null;
  
  RegisterControl.statics.getInstance().add(this);
}

RecordListSearchFilter.prototype.getId = function()
{
  return this.id;
}

RecordListSearchFilter.prototype.setDefaultValue = function(value)
{
  this.defaultValue = value;
}

RecordListSearchFilter.prototype.setValue = function(value)
{
  this.value = value;
  this.setInputValue(this.value);
}

RecordListSearchFilter.prototype.getValue = function()
{
  this.value = this.getInputValue();
  return this.value;
}

RecordListSearchFilter.prototype.attach = function(node)
{
  var td = document.createElement("td");
  td.appendChild( document.createTextNode( this.text ) );
  node.appendChild(td);
  td = document.createElement("td");
  td.appendChild( this.getInputNode() );
  node.appendChild(td);
}

RecordListSearchFilter.prototype.init = function(parent)
{
  this.parent = parent;
}

RecordListSearchFilter.prototype.getInputNode = function()
{
}

RecordListSearchFilter.prototype.setInputValue = function(value)
{
}

RecordListSearchFilter.prototype.getInputValue = function()
{
}


/**
 * RecordListSearchTextFilter
 */
subclass(RecordListSearchTextFilter, RecordListSearchFilter);
function RecordListSearchTextFilter(id, text)
{
  RecordListSearchFilter.call(this, id, text);
}

RecordListSearchTextFilter.prototype.getInputNode = function()
{
  var input = document.createElement("input");
  input.type = "text";
  input.id = this.getId();
  if(this.parent)
  {
    input.RecordListSearchId = this.parent.getId();
    input.onkeypress = RecordListSearch.statics.enterPressedEvent;
  }
  if (this.value)
    input.value = this.value;
  else if (null == this.value && this.defaultValue)
    input.value = this.defaultValue;
  return input;
}

RecordListSearchTextFilter.prototype.setInputValue = function(value)
{
  var input = document.getElementById(this.getId());
  if (input)
  {
    if (value)
      input.value = value;
    else if (null == value && this.defaultValue)
      input.value = this.defaultValue;
    else
      input.value = '';
  }
}

RecordListSearchTextFilter.prototype.getInputValue = function()
{
  var input = document.getElementById(this.getId());
  if (input && input.value && 0<input.value.length)
    return input.value;
  return null;
}


/**
 * RecordListSearchDropdownFilter
 */
subclass(RecordListSearchDropdownFilter, RecordListSearchFilter);
function RecordListSearchDropdownFilter(id, text)
{
  RecordListSearchFilter.call(this, id, text);
  this.items = new Array();
}

RecordListSearchDropdownFilter.prototype.setItems = function(items)
{
  this.items = items;
}

RecordListSearchDropdownFilter.prototype.getInputNode = function()
{
  var input = document.createElement("select");
  input.id = this.getId();
  input.options.length = this.items.length;
  var selectedIndex = 0;
  for (var i = 0; i<this.items.length; i++)
  {
    var item = this.items[i];
    if (item.key==this.value || (null==this.value && item.key==this.defaultValue))
      selectedIndex = i;
    input.options[i].text = item.value;
    input.options[i].value = item.key;
  }
  input.options.selectedIndex = selectedIndex;
  return input;
}

RecordListSearchDropdownFilter.prototype.setInputValue = function(value)
{
  var input = document.getElementById(this.getId());
  if (input)
  {
    var selectedIndex = 0;
    for (var i=input.options.length-1; 0<=i; i--)
    {
      if (value == input.options[i].value || (null == value && this.defaultValue == input.options[i].value))
      {
        selectedIndex = i;
        break;
      }
    }
    input.options.selectedIndex = selectedIndex;
  }
}

RecordListSearchDropdownFilter.prototype.getInputValue = function()
{
  var input = document.getElementById(this.getId());
  try
  {
    return input.options[input.options.selectedIndex].value;
  }
  catch (e)
  {
    return null;
  }
}


/**
 * RecordListSearchDateFilter
 */
subclass(RecordListSearchDateFilter, RecordListSearchFilter);
function RecordListSearchDateFilter(id, text)
{
  this.calendarTextCtrl = null;
  
  this.strDateFormat = null;
  
  RecordListSearchFilter.call(this, id, text);
}

RecordListSearchDateFilter.prototype.setDateFormat = function(strDateFormat)
{
    this.strDateFormat = strDateFormat;
}

RecordListSearchDateFilter.prototype.setUseDropDowns = function(bUseDropDowns)
{
    if(!IsNullOrUndefined(this.calendarTextCtrl))
        this.calendarTextCtrl.SetUseDropDowns(bUseDropDowns);
}

RecordListSearchDateFilter.prototype.getInputNode = function()
{
    var div = $C("div");
    div.id = this.getId();
  
    // ensure initialization
    if(this.strDateFormat == null) 
    {
        alert('RecordListSearchDateFilter Error: call setDateFormat() after constructor.');
        return;
    }
    
    var strDateFormat = this.strDateFormat;
    if(this.calendarTextCtrl == null) 
    {
        // ensure calendar control exists
        RDate.statics.setDefaultParseFormat(strDateFormat);
        var date = new RDate();
        var value = this.value;
        if(null == value && this.defaultValue)
            value = this.defaultValue;
        if (value)
            date.parse(value);
        this.calendarTextCtrl = new CalendarTextCtrl(this.id+'calendar', date.day, date.month, date.year, strDateFormat);
        this.calendarTextCtrl.setInitialValue(value);
    }
    this.calendarTextCtrl.attach(div);
    
    return div;
}

RecordListSearchDateFilter.prototype.setInputValue = function(value)
{
    if(!IsNullOrUndefined(this.calendarTextCtrl))
    {
        // this code sets the initial value displayed
        this.calendarTextCtrl.setInitialValue(value);
        
        // this code sets the textctrl in the gui if it exists
        var input = $(this.calendarTextCtrl.getIdCtrlDateTextCtrl());
        if (input)
        {
            if (value)
            {
                input.value = value;
                this.calendarTextCtrl.ParseTextCtrlValue();
            }
            else if(null == value && this.defaultValue)
            {
                input.value = this.defaultValue;
                this.calendarTextCtrl.ParseTextCtrlValue();
            }
            else
                input.value = '';
        }
    }
}

RecordListSearchDateFilter.prototype.getInputValue = function()
{
  if (this.calendarTextCtrl != null)
  {
    var input = $(this.calendarTextCtrl.getIdCtrlDateTextCtrl());
    if (input && input.value && 0<input.value.length)
      return input.value;
  }
  return null;
}




/**
 * RecordListSearchDepartmentFilter
 */
subclass(RecordListSearchDepartmentFilter, RecordListSearchFilter);
function RecordListSearchDepartmentFilter(id, text)
{
    this.departmentDropDown = null;
  
    this.idNewInstance=null;
    this.bNone=null;
    this.bAll=null;
    this.bNoChange=null;
    this.width=null;
    this.height=null;

    RecordListSearchFilter.call(this, id, text);
}

// initialize member vars for display
RecordListSearchDepartmentFilter.prototype.initialize = function(
    idNewInstance,
    bUseExternalDropDown,
    rootNodeList,
    nodeMap,
    bNone,
    textNone,
    bAll,
    textAll,
    bNoChange,
    textNoChange,
    width,
    height)
{
    this.bUseExternalDropDown = bUseExternalDropDown;
    this.rootNodeList = rootNodeList;
    this.nodeMap = nodeMap;
    
    this.idNewInstance=idNewInstance;
    this.bNone=bNone;
    this.textNone=textNone;
    this.bAll=bAll;
    this.textAll=textAll;
    this.bNoChange=bNoChange;
    this.textNoChange=textNoChange;
    this.width=width;
    this.height=height;
}

RecordListSearchDepartmentFilter.prototype.getInputNode = function()
{
    var div = $C("div");
    div.id = this.getId();
  
    // ensure initialization
    if(this.idNewInstance == null) 
    {
        alert('RecordListSearchDepartmentFilter Error: call initialize() after constructor.');
        return;
    }
    
    if(this.departmentDropDown == null) 
    {
        // ensure control exists
        // build node data
        var initText = this.initialText;
        var value = this.value;
        if(null == value && this.defaultValue)
            value = this.defaultValue;
        var bValueFound = false;

        for(var i in this.nodeMap)
        {
            var item = this.nodeMap[i];
            if(item.id == value)
            {
                bValueFound = true;
                initText = item.getText();
            }
        }

        var dropDown = new DropDown(this.idNewInstance, this.rootNodeList, this.nodeMap);      
        dropDown.setUseExternalDropDown(this.bUseExternalDropDown);
        
        dropDown.treeSetOptionNoneText(this.textNone);
        dropDown.treeSetOptionAllText(this.textAll);
        dropDown.treeSetOptionNoChangeText(this.textNoChange);
        
        // options
        if(this.bNone == true)
        {
            var text = dropDown.textNone;
            dropDown.treeSetOptionNoneText(text);
            dropDown.addNoneOptionToNodeMap();
            if('None' == value)
            {
                bValueFound = true;
                initText = text;
            }
        }
        if(this.bAll == true)
        {
            var text = dropDown.textAll;
            dropDown.treeSetOptionAllText(text);
            dropDown.addAllOptionToNodeMap();
            if('All' == value || null == value || '' == value)
            {
                bValueFound = true;
                value = 'All';
                initText = text;
            }
        }
        if(this.bNoChange == true)
        {
            var text = dropDown.textNoChange;
            dropDown.treeSetOptionNoChangeText(text);
            dropDown.addNoChangeOptionToNodeMap();
            if('NoChange' == value)
            {
                bValueFound = true;
                initText = text;
            }
        }

        if(bValueFound)
        {
            dropDown.selectedValue = value;
            dropDown.selectedText = initText;
        }
        
        if(this.width != null && this.height != null)
        {
            dropDown.setSize(this.width,this.height);
        }

        dropDown.attach(div);

        this.departmentDropDown = dropDown;
    }
    
    return div;
}

// assume value is id
RecordListSearchDepartmentFilter.prototype.setInputValue = function(value)
{
    if(!IsNullOrUndefined(this.departmentDropDown))
    {
        if(value)
            this.departmentDropDown.setSelectedItem(value, null);
        else if (null == value && this.defaultValue)
            this.departmentDropDown.setSelectedItem(this.defaultValue, null);
        else
            this.departmentDropDown.setSelectedItem(this.departmentDropDown.idAll, null);
    }
}

// return id
RecordListSearchDepartmentFilter.prototype.getInputValue = function()
{
  var retval = null;
  if (this.departmentDropDown != null)
  {
    if( (this.departmentDropDown.getSelectedItemId() == this.departmentDropDown.idAll) ||
        (this.departmentDropDown.getSelectedItemId() == this.departmentDropDown.idNone) ||
        (this.departmentDropDown.getSelectedItemId() == this.departmentDropDown.idNoChange) )
    {
        // retval = null;
        // rkrk consider id == NoChange to return a specific value
    }
    else retval = this.departmentDropDown.getSelectedItemId();
  }
  return retval;
}


/**
 * RecordListSearchNumberFilter
 */
subclass(RecordListSearchNumberFilter, RecordListSearchFilter);
function RecordListSearchNumberFilter(id, number)
{
  RecordListSearchFilter.call(this, id, number);
}

RecordListSearchNumberFilter.prototype.getInputNode = function()
{
  var input = document.createElement("input");
  input.type = "Text";
  input.id = this.getId();
  input.onblur = RecordListSearchNumberFilter.statics.onblur;
  if (this.value)
    input.value = this.value;
  else if (null == this.value && this.defaultValue)
    input.value = this.defaultValue;
  return input;
}

RecordListSearchNumberFilter.prototype.setInputValue = function(value)
{
  var input = document.getElementById(this.getId());
  if (input)
  {
    if (value)
      input.value = value;
    else if(null == value && this.defaultValue)
      input.value = this.defaultValue;
    else
      input.value = '';
  }
}

RecordListSearchNumberFilter.prototype.getInputValue = function()
{
  var input = document.getElementById(this.getId());
  if (input && input.value && 0<input.value.length)
    return input.value;
  return null;
}

RecordListSearchNumberFilter.statics = new Object();
RecordListSearchNumberFilter.statics.onblur = function()
{
    var v = parseFloat(this.value);
    if (isNaN(v) || !isFinite(v))
    {
        if(this.value != 0)
            this.value = '';
    }    
}