//Author				: Sonja Goosen
//Date Created			: 23-Mar-2005
//Last Changed by		: KDT
//Date Changed			: 11-Apr-2006
//Version				: 2.0.11042006
//Comments:
//Date:		Desc:															
//--------------------------------------------------------------------------------------------
//11-Apr-2006	Shuffler App reborn and combined into one Page

//ToDoList:
//Date:		Desc:															Status:
//--------------------------------------------------------------------------------------------
var LeftBoxFilter;
var RightBoxFilter;

function MoveItemsToFromBox(fromBox, toBox, allItems)
{

	if (allItems) // Send all the Items to the toBox
	{
		for (var i = 0; i < fromBox.options.length; i++)
		{
			var oOption = document.createElement("OPTION");
			oOption.text = fromBox.options[i].text; 
			oOption.value = fromBox.options[i].value; 
			toBox.options.add(oOption);
		}
		fromBox.innerHTML = "";
		SortListBox(toBox);
	}
	else // Send Individual items over to the toBox
	{
		var deleteCount = 0;
		var loopLength = fromBox.options.length
		for (var i = 0; i < loopLength; i++)
		{
			if (fromBox.options[i - deleteCount]) {
				if (fromBox.options[i - deleteCount].selected)
				{
					var oOption = document.createElement("OPTION");
					oOption.text = fromBox.options[i - deleteCount].text;
					oOption.value = fromBox.options[i - deleteCount].value;
					toBox.options.add(oOption);
					fromBox.options[i - deleteCount] = null;
					deleteCount++;
				}
			}
		}
		SortListBox(toBox);
		SortListBox(fromBox);
	}
}

function FilterListbox(listbox,searchValue)
{
	for(i=0; i<listbox.options.length; i++)
	{
		listbox.options[i].selected = false;
	}
	
	reg = new RegExp(searchValue,'i');
	for (i = 0; i < listbox.options.length; i++)
	{
		//alert(listbox.options[i].text)
		if (reg.test(listbox.options[i].text))
		{ 
			listbox.options[i].selected = true;
		}
	}
}

function MoveItemsToDB(FormName, submitToPage, MainID, proc2Save)
{
		var ShuffleCheckError = function(oXML) 
		{
			var returnVal = oXML.responseText
			regularCursor();
			if (returnVal.substr(0,5) == "ERROR") {
				alert(returnVal)
			}
			else {
				//alert(returnVal);
				alert("Item link successful");
			}
		}

		var page = submitToPage //get form action
		var dataToSend = "action=ShuffleLinks&SubmitMe=true&MainID=" + MainID + "&proc2Save=" + proc2Save + "&" + fetchFormAndListboxData(FormName)

		hiddenSubmit(page,dataToSend, ShuffleCheckError, 0, null, false)
}

function SortListBox(ListBox)
{
	var listboxItems = new Array();

	for (i = 0; i < ListBox.options.length; i++)
	{
		var listboxItemToAdd = ListBox.options[i].text + "^|^" + ListBox.options[i].value.toString();
		listboxItems[i] = listboxItemToAdd;
	}

	ListBox.innerHTML = "";
	listboxItems.sort(function (x,y){
			var a = String(x).toUpperCase();
			var b = String(y).toUpperCase();
			
			if (a >	b)
				return 1
		
			if (a < b)
				return -1
			return 0;
		});

	for (i = 0; i < listboxItems.length; i++)
	{
		var myNewItemandValue = new Array();
		myNewItemandValue = listboxItems[i].split("^|^");
		var textVal = myNewItemandValue[0,0];
		var IDVal = myNewItemandValue[1,1];
		
		var oOption = new Option(textVal,IDVal);
		ListBox.options.add(oOption);
	}
}


function filterlist(selectobj) 
{

  //==================================================
  // PARAMETERS
  //==================================================

  // HTML SELECT object
  // For example, set this to document.myform.myselect
  this.selectobj = selectobj;

  // Flags for regexp matching.
  // "i" = ignore case; "" = do not ignore case
  // You can use the set_ignore_case() method to set this
  this.flags = 'i';

  // Which parts of the select list do you want to match?
  this.match_text = true;
  this.match_value = false;

  // You can set the hook variable to a function that
  // is called whenever the select list is filtered.
  // For example:
  // myfilterlist.hook = function() { }

  // Flag for debug alerts
  // Set to true if you are having problems.
  this.show_debug = false;

  //==================================================
  // METHODS
  //==================================================

  //--------------------------------------------------
  this.init = function() 
  {
    // This method initilizes the object.
    // This method is called automatically when you create the object.
    // You should call this again if you alter the selectobj parameter.

    if (!this.selectobj) return this.debug('selectobj not defined');
    if (!this.selectobj.options) return this.debug('selectobj.options not defined');

    // Make a copy of the select list options array
    this.optionscopy = new Array();
    if (this.selectobj && this.selectobj.options) 
    {
      for (var i=0; i < this.selectobj.options.length; i++) 
      {
        // Create a new Option
        this.optionscopy[i] = new Option();

        // Set the text for the Option
        this.optionscopy[i].text = selectobj.options[i].text;

        // Set the value for the Option.
        // If the value wasn't set in the original select list,
        // then use the text.
        if (selectobj.options[i].value) 
        {
          this.optionscopy[i].value = selectobj.options[i].value;
        } 
        else 
        {
          this.optionscopy[i].value = selectobj.options[i].text;
        }

      }
    }
  }

  //--------------------------------------------------
  this.reset = function() 
  {
    // This method resets the select list to the original state.
    // It also unselects all of the options.

    this.set('');
  }


  //--------------------------------------------------
  this.set = function(pattern)
  {
    // This method removes all of the options from the select list,
    // then adds only the options that match the pattern regexp.
    // It also unselects all of the options.

    var loop=0, index=0, regexp, e;

    if (!this.selectobj) return this.debug('selectobj not defined');
    if (!this.selectobj.options) return this.debug('selectobj.options not defined');

    // Clear the select list so nothing is displayed
    this.selectobj.options.length = 0;

    // Set up the regular expression.
    // If there is an error in the regexp,
    // then return without selecting any items.
    try 
    {

      // Initialize the regexp
      regexp = new RegExp(pattern, this.flags);

    } 
    catch(e) 
    {

      // There was an error creating the regexp.

      // If the user specified a function hook,
      // call it now, then return
      if (typeof this.hook == 'function') 
      {
        this.hook();
      }

      return;
    }

    // Loop through the entire select list and
    // add the matching items to the select list
    for (loop=0; loop < this.optionscopy.length; loop++) 
    {

      // This is the option that we're currently testing
      var option = this.optionscopy[loop];

      // Check if we have a match
      if ((this.match_text && regexp.test(option.text)) ||
          (this.match_value && regexp.test(option.value))) 
      {

        // We have a match, so add this option to the select list
        // and increment the index

        this.selectobj.options[index++] =
          new Option(option.text, option.value, false);

      }
    }

    // If the user specified a function hook,
    // call it now
    if (typeof this.hook == 'function') 
    {
      this.hook();
    }

  }

  //--------------------------------------------------
  this.set_ignore_case = function(value) 
  {
    // This method sets the regexp flags.
    // If value is true, sets the flags to "i".
    // If value is false, sets the flags to "".

    if (value) 
    {
      this.flags = 'i';
    } 
    else 
    {
      this.flags = '';
    }
  }

  //--------------------------------------------------
  this.debug = function(msg) 
  {
    if (this.show_debug) 
    {
      alert('FilterList: ' + msg);
    }
  }

  //==================================================
  // Initialize the object
  //==================================================
  this.init();

}
