//Author				: Sonja Goosen
//Date Created			: 23-Mar-2005
//Last Changed by		: KDT
//Date Changed			: 7-Jun-2006
//Version				: 2.0.07062006
//Comments:
//Date:		Desc:															
//--------------------------------------------------------------------------------------------
//11-Apr-2006	Shuffler App reborn and combined into one Page
//07-Jun-2006	Altered the filtering to avoid duplicates

//ToDoList:
//Date:		Desc:															Status:
//--------------------------------------------------------------------------------------------
var MainBoxFilter;
/*
function MoveItemsToFromBox(fromBox, toBox, allItems)
{
	//alert("hier")
	//return;
	
	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 MoveItemsToFromBox(LeftBox, RightBox, allItems, LeftToRight) // LeftToRight boolean - true = move items from leftbox 2 rightbox
{
	//alert("hier")
	//return;
	/*if(!MainBoxFilter) 
	{ 
		MainBoxFilter = new filterlist("document." + TheForm + ".selLeftList", "document." + TheForm + ".selRightList")
	}
	*/
	
	if (LeftToRight == true) // HANDLE FROM LEFT TO RIGHT
	{
		// Check if the Mainbox object exists - If not, create both "copy" arrays
			if(!MainBoxFilter) 
				{ 
					MainBoxFilter = new filterlist(LeftBox, RightBox) // Object initiated
				}
		
		
		if (allItems) // Send all the Items to the RightBox AND UPDATE BOTH THE COPY ARRAYS
		{
			for (var i = 0; i < LeftBox.options.length; i++)
			{
				var oOption = document.createElement("OPTION");
				oOption.text = LeftBox.options[i].text;
				oOption.value = LeftBox.options[i].value;
				RightBox.options.add(oOption);
			}
			// Copy all the items in the leftOptionsCopy array into the rightOptionsCopy and set the leftOptionsCopy = new array

			for (var j = 0; j<MainBoxFilter.leftOptionsCopy.length; j++)
			{
				var optExists = false;
				var rightCounter = 0;
				
				while (rightCounter < MainBoxFilter.rightOptionsCopy.length && optExists == false) // check if the element doesn't already exists. (unlikely)
				{
					if (MainBoxFilter.leftOptionsCopy[j].text == MainBoxFilter.rightOptionsCopy[rightCounter].text) // exists
					{
						optExists == true;
					}
					rightCounter++;
				}
				
				if (optExists == false) // add the element
				{
					var lastElement = MainBoxFilter.rightOptionsCopy.length;
					MainBoxFilter.rightOptionsCopy[lastElement] = new Option();
					MainBoxFilter.rightOptionsCopy[lastElement].text = MainBoxFilter.leftOptionsCopy[j].text;
					MainBoxFilter.rightOptionsCopy[lastElement].value = MainBoxFilter.leftOptionsCopy[j].value;
				}
			}
			
			MainBoxFilter.leftOptionsCopy = null;
			MainBoxFilter.leftOptionsCopy = new Array(); // create a blank array again
			LeftBox.innerHTML = "";
			SortListBox(RightBox);
			
		}
		else // Send Individual items over to the RightBox AND UPDATE BOTH THE COPY ARRAYS
		{
			var deleteCount = 0;
			var loopLength = LeftBox.options.length
			for (var i = 0; i < loopLength; i++)
			{
				if (LeftBox.options[i - deleteCount]) {
					if (LeftBox.options[i - deleteCount].selected)
					{
						var oOption = document.createElement("OPTION");
						oOption.text = LeftBox.options[i - deleteCount].text;
						oOption.value = LeftBox.options[i - deleteCount].value;
						RightBox.options.add(oOption);
						
						// add the element to the rightOptionsCopy array
						var lastElement = MainBoxFilter.rightOptionsCopy.length;
						MainBoxFilter.rightOptionsCopy[lastElement] = new Option();
						MainBoxFilter.rightOptionsCopy[lastElement].text = oOption.text;
						MainBoxFilter.rightOptionsCopy[lastElement].value = oOption.value;
						
						// delete the element from the leftOptionsCopy array and move all the elements back one space (splice)
						for (j=0; j<MainBoxFilter.leftOptionsCopy.length; j++)
						{
							if (MainBoxFilter.leftOptionsCopy[j].value == oOption.value) // element found - delete the element
							{
								//alert(MainBoxFilter.leftOptionsCopy.length);
								MainBoxFilter.leftOptionsCopy.splice(j,1); // Delete the j index and update the array elements
								//alert(MainBoxFilter.leftOptionsCopy.length);
							}
						}
						LeftBox.options[i - deleteCount] = null;
						deleteCount++;
					}
				}
			}
			SortListBox(RightBox);
			SortListBox(LeftBox);
		}
		//alert("Leftbox Array length = " + MainBoxFilter.leftOptionsCopy.length)
		//alert("Rightbox Array length = " + MainBoxFilter.rightOptionsCopy.length)
	}
	else // HANDLE "FROM RIGHT TO LEFT"
	{
		
	if(!MainBoxFilter) 
		{ 
			MainBoxFilter = new filterlist(LeftBox, RightBox) // Object initiated
		}
				
		if (allItems) // Send all the Items to the LeftBox AND UPDATE BOTH THE COPY ARRAYS
		{
			for (var i = 0; i < RightBox.options.length; i++)
			{
				var oOption = document.createElement("OPTION");
				oOption.text = RightBox.options[i].text; 
				oOption.value = RightBox.options[i].value; 
				LeftBox.options.add(oOption);
			}

			// Copy all the items in the rightOptionsCopy array into the leftOptionsCopy and set the rightOptionsCopy = new array

			for (var j = 0; j<MainBoxFilter.rightOptionsCopy.length; j++)
			{
				var optExists = false;
				var leftCounter = 0;

				while (leftCounter < MainBoxFilter.leftOptionsCopy.length && optExists == false) // check if the element doesn't already exists. (unlikely)
				{
					if (MainBoxFilter.rightOptionsCopy[j].text == MainBoxFilter.leftOptionsCopy[leftCounter].text) // exists
					{
						optExists == true;
					}
					leftCounter++;
				}
				
				if (optExists == false) // add the element
				{
					var lastElement = MainBoxFilter.leftOptionsCopy.length;
					MainBoxFilter.leftOptionsCopy[lastElement] = new Option();
					MainBoxFilter.leftOptionsCopy[lastElement].text = MainBoxFilter.rightOptionsCopy[j].text;
					MainBoxFilter.leftOptionsCopy[lastElement].value = MainBoxFilter.rightOptionsCopy[j].value;
				}
			}
			
			MainBoxFilter.rightOptionsCopy = null;
			MainBoxFilter.rightOptionsCopy = new Array(); // create a blank array again
			
			RightBox.innerHTML = "";
			SortListBox(LeftBox);
		} 
		else // Send Individual items over to the LeftBox AND UPDATE BOTH THE COPY ARRAYS
		{
			var deleteCount = 0;
			var loopLength = RightBox.options.length
			for (var i = 0; i < loopLength; i++)
			{
				if (RightBox.options[i - deleteCount]) {
					if (RightBox.options[i - deleteCount].selected)
					{
						var oOption = document.createElement("OPTION");
						oOption.text = RightBox.options[i - deleteCount].text;
						oOption.value = RightBox.options[i - deleteCount].value;
						LeftBox.options.add(oOption);

						// add the element to the leftOptionsCopy array
						var lastElement = MainBoxFilter.leftOptionsCopy.length;
						MainBoxFilter.leftOptionsCopy[lastElement] = new Option();
						MainBoxFilter.leftOptionsCopy[lastElement].text = oOption.text;
						MainBoxFilter.leftOptionsCopy[lastElement].value = oOption.value;
						
						// delete the element from the rightOptionsCopy array and move all the elements back one space (splice)
						for (j=0; j<MainBoxFilter.rightOptionsCopy.length; j++)
						{
							if (MainBoxFilter.rightOptionsCopy[j].value == oOption.value) // element found - delete the element
							{
								//alert(MainBoxFilter.leftOptionsCopy.length);
								MainBoxFilter.rightOptionsCopy.splice(j,1); // Delete the j index and update the array elements
								//alert(MainBoxFilter.leftOptionsCopy.length);
							}
						}
						RightBox.options[i - deleteCount] = null;
						deleteCount++;
					}
				}
			}
			
			/*
			for (var i = 0; i < loopLength; i++)
			{
				if (RightBox.options[i - deleteCount]) {
					if (RightBox.options[i - deleteCount].selected)
					{
						var oOption = document.createElement("OPTION");
						oOption.text = RightBox.options[i - deleteCount].text;
						oOption.value = RightBox.options[i - deleteCount].value;
						LeftBox.options.add(oOption);
						RightBox.options[i - deleteCount] = null;
						deleteCount++;
					}
				}
			}
			*/
			SortListBox(LeftBox);
			SortListBox(RightBox);
		}
		
		//alert("Leftbox Array length = " + MainBoxFilter.leftOptionsCopy.length)
		//alert("Rightbox Array length = " + MainBoxFilter.rightOptionsCopy.length)

	}
}


/* Taken out and replaced by filterlist 18-May-2006
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)
{
//alert(ListBox.options.length);
	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(leftSelectBox, rightSelectBox)
{

  //==================================================
  // PARAMETERS
  //==================================================

  // HTML SELECT object
  // For example, set this to document.myform.myselect
  	this.leftSelectBox = leftSelectBox;
	this.rightSelectBox = rightSelectBox;
	
  // 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.

		if (!this.leftSelectBox) return this.debug('leftSelectBox not defined');
		if (!this.rightSelectBox) return this.debug('rightSelectBox not defined');

		// Make 2 "copy arrays" of BOTH the selection lists in order to have a copy of the ORIGINAL values.
		// These arrays will be modified when the items are moved to keep them in sync with the display.
		
		// LEFT BOX
		this.leftOptionsCopy = new Array();
		if (this.leftSelectBox && this.leftSelectBox.options) 
		{
			for (var i=0; i < this.leftSelectBox.options.length; i++) 
			{
				// Create a new Option
				this.leftOptionsCopy[i] = new Option();

				// Set the text for the Option
				this.leftOptionsCopy[i].text = leftSelectBox.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 (leftSelectBox.options[i].value) 
				{
					this.leftOptionsCopy[i].value = leftSelectBox.options[i].value;
				} 
				else 
				{
					this.leftOptionsCopy[i].value = leftSelectBox.options[i].text;
				}
			}
		}

		// RIGHT BOX
		this.rightOptionsCopy = new Array();
		if (this.rightSelectBox && this.rightSelectBox.options)
		{
			for (var i=0; i < this.rightSelectBox.options.length; i++) 
			{
				// Create a new Option
				this.rightOptionsCopy[i] = new Option();

				// Set the text for the Option
				this.rightOptionsCopy[i].text = rightSelectBox.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 (rightSelectBox.options[i].value)
				{
					this.rightOptionsCopy[i].value = rightSelectBox.options[i].value;
				}
				else
				{
					this.rightOptionsCopy[i].value = rightSelectBox.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, leftSelectBox, rightSelectBox, isLeft)
  {
    // 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.
	
	if (isLeft == true) { // Handle LEFT Selection box
		
		var loop=0, index=0, regexp, e;

		if (!this.leftSelectBox) return this.debug('leftSelectBox not defined');
		if (!this.leftSelectBox.options) return this.debug('leftSelectBox.options not defined');

		// Clear the select list so nothing is displayed
		this.leftSelectBox.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 LeftBox Array and
		// add the matching items to the LeftBox selectionlist
		for (loop=0; loop < this.leftOptionsCopy.length; loop++) 
		{
			// This is the option that we're currently testing
			var option = this.leftOptionsCopy[loop];

			// Check if we have a match
			if ((this.match_text && regexp.test(option.text)) ||
				(this.match_value && regexp.test(option.value))) 
			{
				this.leftSelectBox.options[index++] = new Option(option.text, option.value, false);
			}
		}
		
		// Sort the Left box once the for-loop completed
		SortListBox(leftSelectBox);

		// If the user specified a function hook,
		// call it now
		if (typeof this.hook == 'function') 
		{
			this.hook();
		}
	}
	else { // Handle RIGHT
		
		var loop=0, index=0, regexp, e;

		if (!this.rightSelectBox) return this.debug('rightSelectBox not defined');
		if (!this.rightSelectBox.options) return this.debug('rightSelectBox.options not defined');

		// Clear the select list so nothing is displayed
		this.rightSelectBox.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 RightBox Array and
		// add the matching items to the RightBox selectionlist
		for (loop=0; loop < this.rightOptionsCopy.length; loop++) 
		{
			// This is the option that we're currently testing
			var option = this.rightOptionsCopy[loop];

			// Check if we have a match
			if ((this.match_text && regexp.test(option.text)) ||
				(this.match_value && regexp.test(option.value))) 
			{
				this.rightSelectBox.options[index++] = new Option(option.text, option.value, false);
			}
		}

		// Sort the RightBox once the for-loop completed
		SortListBox(rightSelectBox);
		
		// 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();

}

/*
function test()
{
  var ary = new Array(2,4,6,8,0);
  alert(ary);  // before
  ary.splice(2,2);
  alert(ary);  // after
}
*/

function ClearShuffleParameters()
{
	if (MainBoxFilter)
	{
		MainBoxFilter.leftOptionsCopy = null;
		MainBoxFilter.rightOptionsCopy = null;
		MainBoxFilter = null;
	}	
}
