﻿// Search Control 
// (c) 2006 Trinso, Mredkj.com
// Created by: Radek Novak - novak@trinso.sk, Trinso - www.trinso.sk
// Created on: 06-09-02
// based on: http://www.mredkj.com/tutorials/tutorial_mixed2b.html
//
// ALL YOUR BASE ARE BELONG TO US
//

function addOption(theSel, theText, theValue)
{
  var newOpt    = new Option(theText, theValue);
  var selLength = theSel.length;
  theSel.options[selLength] = newOpt;
}

function deleteOption(theSel, theIndex)
{   
  var selLength = theSel.length;
  if( selLength > 0 )
  {
    theSel.options[theIndex] = null;
  }
}

// hiddenField stores pipe-separated list of selected values
function addToList(theSelFromID, theSelToID, hiddenField, doNotAddParents)
{  
  theSelFrom = document.getElementById(theSelFromID);
  theSelTo   = document.getElementById(theSelToID);
  theHidden  = document.getElementById(hiddenField);
  
  var selLength      = theSelFrom.length;
  var selectedText   = new Array();
  var selectedValues = new Array();
  var selectedCount  = 0;
  
  var i;
    
  // Find the selected Options in reverse order
  for(i=selLength-1; i>=0; i--)
  {
    // if item is not selected or is parent, skip it
    var isParent = theSelFrom.options[i].attributes["isparent"].value;
    if ( (!theSelFrom.options[i].selected) || ((isParent == "True") && (doNotAddParents == "True")) )
    {
        continue;
    }
  
    // check whether is the option already added 
    var isPresentAtDest = false;
    var j;
    for ( j = 0; j < theSelTo.length; j++ )
    {
        if ( theSelFrom.options[i].value == theSelTo.options[j].value )
        {
            isPresentAtDest = true;
            alert("You have already chosen this item");
            break;
        }
    }
    
    if ( isPresentAtDest == false )
    {
        if ( isParent == "True" )
        {
            selectedText[selectedCount]   = theSelFrom.options[i].text;
        }
        else
        {
            selectedText[selectedCount]   = removeListBoxItemPrefix(theSelFrom.options[i].text);
        }
        
        selectedValues[selectedCount] = theSelFrom.options[i].value;
        theHidden.value               = theHidden.value + theSelFrom.options[i].value + "|";
        selectedCount++;            
    }
 
  }  
    
  // Add the selected text/values in reverse order.
  // This will add the Options to the 'to' Select
  // in the same order as they were in the 'from' Select.
  for(i=selectedCount-1; i>=0; i--)
  {
    addOption(theSelTo, selectedText[i], selectedValues[i]);
  }
}

// removes selected items from the specified list
// and updates the hidden text field accordingly
function removeFromList(listID, hiddenFieldID)
{
    var list        = document.getElementById(listID);
    var hiddenField = document.getElementById(hiddenFieldID);
    var itemsCount  = list.length;     
    var ids         = hiddenField.value.split('|');    

    var i;
    var itemsToRemove
    // goes through the list in reverse order to avoid 'null pointer exception'
    for ( i = itemsCount - 1 ; i >= 0; i-- )
    {
        // remove the element from the hidden field list
        if ( list.options[i].selected )
        {
            var j;
            for ( j = 0; j < ids.length; j++ )
            {
                var k = ids[j];
                var l = list.options[i].value;
                
                if ( ids[j] == list.options[i].value )
                {
                    ids.splice(j, 1);
                    break;
                }
            }
       
           // then remove it from the listbox itself
           list.options[i] = null;   
        }
     }  
     
     var length = ids.length;
     hiddenField.value = "";
     for ( i = 0; i < length; i++ )
     {
        if ( ids[i] != "" )
        {
            hiddenField.value = hiddenField.value + ids[i] + "|";
        }
     }
}

/**
 * removes the leading ".." in listboxitem
 */
function removeListBoxItemPrefix(listboxItemText)
{
    var regEx = /../;
    return listboxItemText.replace(regEx, "");
}