﻿
Type.registerNamespace("Ascent");

Ascent.FilterSelection = function(NewAttributeID, NewSelectedValues, NewDataType) {
    this.AttributeID = NewAttributeID;
    this.SelectedValues = NewSelectedValues;
    this.DataType = NewDataType;
}

Ascent.FilterSelection.prototype = {dispose: function() {}}

Ascent.FilterSelection.registerClass('Ascent.FilterSelection', null, Sys.IDisposable);

   // <summary>
   //    Compares 2 numbers and returns the lowest value
   // </summary>
	function compareNumbers(a, b) 
	{   
		var value1 = a.optText;
      var value2 = b.optText; 
      
		return value1 - value2
	}
	
   // <summary>
   //    Sorts a dropdownlist into ascending order
   // </summary>
   function sortSelect(selectToSort, ascendingOrder) 
   {
		var myOptions = [];
		var numericSort = [];
		var textSort = [];
		var numericCount = 0;
		var textCount = 0;      
		
		for (var loop=0; loop < selectToSort.options.length; loop++) 
      {
			if(selectToSort.options[loop].value == "000")
			{
				selectToSort.options[loop].text = "a " + selectToSort.options[loop].text;
			}

			if(!isNaN(selectToSort.options[loop].text))
			{
				numericSort[numericCount] = { optText:selectToSort.options[loop].text, optValue:selectToSort.options[loop].valuee };
				numericCount++;
			}
			else
			{
				textSort[textCount] = { optText:selectToSort.options[loop].text, optValue:selectToSort.options[loop].value };
				textCount++;
			}
		}
      	
		textSort.sort(sortFuncAsc);
		numericSort.sort(compareNumbers);

		myOptions = numericSort.concat(textSort)
		
      selectToSort.options.length = 0;
      
      var newOption = new Option("0", "000");
      selectToSort.options[0] = newOption;
      
      for (var loop=0; loop < myOptions.length; loop++) 
      {
			var optObj = document.createElement('option');
			optObj.text = myOptions[loop].optText;
			optObj.value = myOptions[loop].optValue;

			if(optObj.value == "000")
         {
				selectToSort.options[0].text = Right(optObj.text, optObj.text.length - 2);
			}
			else
			{
				selectToSort.options[selectToSort.options.length] = optObj;
			}
      }
	}		

   // <summary>
   //    Returns the specified left part of a string
   // </summary>
	function Left(str, n){
		if (n <= 0)
			return "";
		else if (n > String(str).length)
			return str;
		else
			return String(str).substring(0,n);
	}

   // <summary>
   //    Returns the specified right part of a string
   // </summary>
	function Right(str, n){
		if (n <= 0)
			return "";
		else if (n > String(str).length)
			return str;
		else {
			var iLen = String(str).length;
			return String(str).substring(iLen, iLen - n);
		}
	}

   // <summary>
   //    Redraws the numeric items table
   // </summary>
   // <remarks>
   // </remarks>
   // <history>
   // 	[kelly]	23/03/2007	Created
   // </history>
   function RedrawNumericTable(tblID, AttributeID, ArrayList)
   {
      var tbl = document.getElementById(tblID);
      
      var rows = tbl.rows;     
      while(rows.length)      
         tbl.deleteRow(rows.length-1); 
      
      if (ArrayList.length > 0)
      {
         var selMinVal = ArrayList[0]
         var selMaxVal = ArrayList[ArrayList.length - 1]
         
         var textNode = 'From ' + selMinVal + ' to ' + selMaxVal
         
         drawTableRow(tbl, AttributeID, textNode)
        }
   }

   // <summary>
   //    Redraws the numeric items table
   // </summary>
   function drawTableRow(tbl, AttributeID, textNode)
   {
      var iteration = tbl.rows.length;
      var row = tbl.insertRow(iteration);
		   
	   var cellRight = row.insertCell(0);
	   cellRight.align = "left";
	   cellRight.valign = "middle";

	   var checkbox = document.createElement("INPUT");
	   checkbox.type = "checkbox";
	   checkbox.id = "Itemchk" + AttributeID + iteration;
	   checkbox.onclick = function tbltest(){tblClick(this, AttributeID);}

	   cellRight.appendChild(checkbox);
		
	   var checkbo = document.getElementById("Itemchk" + AttributeID + iteration)
	   checkbo.checked = true;
		
	   var cellMiddle = row.insertCell(1);
	   var textNodes = document.createTextNode(textNode);
	   cellMiddle.width = "100%";
	   cellMiddle.appendChild(textNodes);       
   }
 
   // <summary>
   //    Redraws the display table based on what is in the array
   //    Clear all items from the table then go through array creating a new row for each item in
   //    array.
   // </summary>
   // <remarks>
   // </remarks>
   // <history>
   // 	[kelly]	23/03/2007	Created
   // </history>
   function RedrawTable(tblID, AttributeID, ArrayList)
   {
      var tbl = document.getElementById(tblID);
      
      var rows = tbl.rows;     
      while(rows.length)      
         tbl.deleteRow(rows.length-1); 

      for (var x=0; x<ArrayList.length; x++)
      {
		   drawTableRow(tbl, AttributeID, ArrayList[x])   
      }
   }
   
   // <summary>
   //    
   // </summary>
   function GetCurrentArray(value, AttributeID)
   {
		var x
      for (x in value)
      {
         if (value[x].AttributeID == AttributeID)
         {
            return value[x].SelectedValues
         }
      }
   }

   // <summary>
   //   
   // </summary>
   function GetDeserializedData()
   {
		var data = document.getElementById('ctl00_RightHandSide_ProductFilter_HiddenField1').value;
		return Sys.Serialization.JavaScriptSerializer.deserialize(data);   
   }

   // <summary>
   //   
   // </summary>  
   function addSort(item)
   {
      var value = GetDeserializedData()
      
      var x
      for (x in value)
      {
         if (value[x].Sorted == true)
         {
            value[x].Sorted = false
         }
      }
      
      var currentArray = GetCurrentArray(value, item.options[item.selectedIndex].value)

      var h
      for (h in value)
      {
        
         if (value[h].AttributeID == item.options[item.selectedIndex].value)
         {
            value[h].Sorted = true
         }
      }
		SerializeData(value)
   }
   
   // <summary>
   //    
   // </summary>
   function SerializeData(value)
   {
      document.getElementById('ctl00_RightHandSide_ProductFilter_HiddenField1').value = Sys.Serialization.JavaScriptSerializer.serialize(value);		
   }
      
   // <summary>
   //    Adds an item to the ARRAY and then adds to a table also.
   // </summary>
	function addSelectItem(item, AttributeID, DataType)
	{
		var selectname = Left(item.id, String(item.id).length - 13)

		if(item.selectedIndex == 0)
		{
			return false;
		}
		
		var value = GetDeserializedData()

		var currentArray = GetCurrentArray(value, AttributeID)
      
		Array.add(currentArray, item.options[item.selectedIndex].text)
		
		SerializeData(value)
		
		RedrawTable(selectname + 'tblFC', AttributeID, currentArray);
		
		item.options[item.selectedIndex] = null;
		item.options[0].selected;	

		if(item.length == 1)
		{
			hideSelectedItems(selectname + 'childRepeater');
		}	
	}

   // <summary>
   //    Remove item from the table and add it back to the drop-down select box.	
   // </summary>
	function tblClick(item, AttributeID) 
	{
		if (item.nodeName != "INPUT") return;
		var td = item.parentNode;

		if (td.nodeName != "TD") return;
		var tr = td.parentNode;

		var ItemText = tr.lastChild.innerHTML;
		
		var tbody = tr.parentNode;
		
		var criteria = Left(tbody.parentNode.id, String(tbody.parentNode.id).length - 5)
				
		var select1 = document.getElementById(criteria + 'childRepeater')

		if(select1 == null) //DataType = Number
		{		
			document.getElementById(criteria + 'childrepeaterNum').selectedIndex = 0;
			document.getElementById(criteria + 'childrepeaterNum2').selectedIndex = 0;		
			
		   var value = GetDeserializedData()
   		
		   var currentArray = GetCurrentArray(value, AttributeID)
         
         Array.clear(currentArray)
         
  		   SerializeData(value)
			
			RedrawNumericTable(criteria + 'tblFC', AttributeID, currentArray);
			
			selMinVal = -1
			selMaxVal = -1
		}
		
		else //DataType = Text
		{
			var currentOptions = select1.length
			select1.options[currentOptions] = new Option(ItemText, ItemText);		
		
			sortSelect(select1, true);
			
			//Read the array and remove the item ... 
		   var value = GetDeserializedData()
   		
		   var currentArray = GetCurrentArray(value, AttributeID)
   		var y
   		for (y in currentArray)
   		{
   		   if (currentArray[y] == ItemText)
   		   {
   		      Array.removeAt(currentArray, y);
   		   }
   		}
   		
		   SerializeData(value)
			
			RedrawTable(criteria + 'tblFC', AttributeID, currentArray);
									
			showSelectedItems(criteria + 'childRepeater');
		}		
	}
	
   // <summary>
   //    
   // </summary>
	function sortFuncAsc(record1, record2) 
   {
		var value1 = record1.optText.toLowerCase();
      var value2 = record2.optText.toLowerCase();     
		
		if (value1 > value2) return(1);
		if (value1 < value2) return(-1);
		return(0);
   }

   // <summary>
   //    
   // </summary>
   function removeSelectItem(selectName, selectValue)
	{
		var List = document.getElementById(selectName);
		
		for (j=0; j < List.length; j++)
		{
			if(List.options[j].value == selectValue)
			{
				List.options[j] = null;
				return;
			}
		}		
	}

   // <summary>
   //    
   // </summary>
	function showHideItems(objectID, arrowID, downsrc, upsrc) 
	{
		var theElementStyle = document.getElementById(objectID);
		var arrows = document.getElementById(arrowID);
		var textChange = document.getElementById("testShowMore");
		
		if(theElementStyle.style.display == "block")
		{
			hideSelectedItems(objectID)
			arrows.src=downsrc;
			textChange.innerHTML = 'Show more choices';
		}
		else
		{
			showSelectedItems(objectID)
			arrows.src = upsrc;
			textChange.innerHTML = 'Show less choices';
		}
	}

   // <summary>
   //    
   // </summary>
	function hideSelectedItems(selectName)
	{
		var divName = document.getElementById(selectName);
		divName.style.display = 'none';
		divName.style.visibility = 'hidden';
	}

   // <summary>
   //    
   // </summary>
	function showSelectedItems(selectName)
	{
		var divName = document.getElementById(selectName);
		divName.style.display = 'block';
		divName.style.visibility = 'visible';
	}

   // <summary>
   //    
   // </summary>
	function removeAll(item, AttributeID)
	{
		var selectName = Left(item.id, String(item.id).length - 9)	
		var select1 = document.getElementById(selectName + 'childRepeater');

		var value = GetDeserializedData()
   		
		var currentArray = GetCurrentArray(value, AttributeID)
   	
   	var y
   	for (y in currentArray)
   	{
			select1.options[select1.options.length] = new Option(currentArray[y], currentArray[y])
   	}	

   	Array.clear(currentArray)	

		SerializeData(value)
	
	   RedrawTable(selectName + 'tblFC', AttributeID, currentArray);
	   
		sortSelect(select1);
		showSelectedItems(selectName + 'childRepeater');
	}

   // <summary>
   //    
   // </summary>
	function selectAll(item, AttributeID)
	{
		var selectName = Left(item.id, String(item.id).length - 9)
		var select1 = document.getElementById(selectName + 'childRepeater');

		var value = GetDeserializedData()
		
		var currentArray = GetCurrentArray(value, AttributeID)
		
		for(var loop=1; loop < select1.options.length; loop++)
		{
			var newOption = new Option(select1.options[loop].text, select1.options[loop].value)
			
			Array.add(currentArray, select1.options[loop].text)
			var currentOptions = currentArray.length
			select1.options[loop] = null;
			loop--;
		}	
		
		RedrawTable(selectName + 'tblFC', AttributeID, currentArray);
		
		SerializeData(value)

		hideSelectedItems(selectName + 'childRepeater');
	}

   // <summary>
   //    
   // </summary>
	function removeAllNumericItem(item, AttributeID)
	{
		var selectName = Left(item.id, String(item.id).length - 16)	

		var value = GetDeserializedData()
		
		var currentArray = GetCurrentArray(value, AttributeID)
      
      Array.clear(currentArray)	
		
		SerializeData(value)

      RedrawNumericTable(selectName + 'tblFC', AttributeID, currentArray);
	}

   // <summary>
   //    
   // </summary>
	function selectAllNumericItem(item, AttributeID)
	{
		var selectName = Left(item.id, String(item.id).length - 16)	
		var select1 = document.getElementById(selectName + 'childrepeaterNum');
		document.getElementById(selectName + 'childrepeaterNum2').selectedIndex = 0;
		
      var value = GetDeserializedData()
		
		var currentArray = GetCurrentArray(value, AttributeID)
      
      Array.clear(currentArray)	

		for (j=1; j < select1.length; j++)
		{
	      Array.add(currentArray, select1.options[j].value);
   	}	
		
		SerializeData(value)

      RedrawNumericTable(selectName + 'tblFC', AttributeID, currentArray);

		select1.selectedIndex = 0;
		
		selMinVal = select1.options[1].value;
		selMaxVal = select1.options[select1.length - 1].value;
	}

   // <summary>
   //    
   // </summary>
	function addNumericItem(item, AttributeID)
	{	
		if (Right(item.id, 16) == 'childrepeaterNum') 
		{
		   var minMax = 'Min'   
		}
		else
		{
		   var minMax = 'Max'
		}

		if(minMax == "Min") 
		{
			var selectName = Left(item.id, String(item.id).length - 16)	
		}
		else
		{
			var selectName = Left(item.id, String(item.id).length - 17)	
		}
		
		var select1 = document.getElementById(selectName + 'childrepeaterNum');
		var select2 = document.getElementById(selectName + 'childrepeaterNum2');	

      var value = GetDeserializedData()
		
		var currentArray = GetCurrentArray(value, AttributeID)

      if (currentArray.length > 0)
      {
         var selMinVal = currentArray[0];
         var selMaxVal = currentArray[currentArray.length - 1];
      }
      else
      {
         var selMinVal = -1
         var selMaxVal = -1
      }
      
		if(minMax == "Min")
		{
			selMinVal = select1.options[select1.selectedIndex].value;
		}
		else
		{
			selMaxVal = select2.options[select2.selectedIndex].value;
		}

		if(selMinVal == -1)
		{
			selMinVal = select1.options[1].value;
		}

		if(selMaxVal == -1)
		{
			selMaxVal = selMinVal;
		}

		if(parseFloat(selMinVal) > parseFloat(selMaxVal)) 
		{
			alert('Please select a Max value that is greater than the Min value');
			return;
		}
		
		if(selMaxVal == 'Max' || selMinVal == 'Min')
		{
			alert('Please select a value');
			return;
		}

      Array.clear(currentArray)	

		for (j=1; j < select1.length; j++)
		{
			if(parseFloat(select1.options[j].value) >= selMinVal && parseFloat(select1.options[j].value) <= selMaxVal)
			{
				Array.add(currentArray, select1.options[j].value);
			}		
		}	
		
		SerializeData(value)

      RedrawNumericTable(selectName + 'tblFC', AttributeID, currentArray);

		//select1.selectedIndex = 0;
		//select2.selectedIndex = 0;
	}

   // <summary>
   //    
   // </summary>
   function maintainState(AttributeID, list)
   {
      alert('here');
      if (list < 10)
	   {
	      var selectname = 'ctl00_RightHandSide_ProductFilter_parentRepeater_ctl0' + list;
		}
	   else
		{
		   var selectname = 'ctl00_RightHandSide_ProductFilter_parentRepeater_ctl' + list;
	   }

		var value = GetDeserializedData()
		
		var x
      for (x in value)
      {
         if (value[x].AttributeID == AttributeID)
         {
            var currentArray = value[x]
            var currentArrayItems = value[x].SelectedValues
         }
      }	

		SerializeData(value)
		
		if (currentArray.DataType == 3)
		{
		   RedrawTable(selectname + '_tblFC', AttributeID, currentArrayItems);
		}
		else
		{
		   RedrawNumericTable(selectname + '_tblFC', AttributeID, currentArrayItems);
		}
   }
  
if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();