﻿$(document).ready(function() {
	getSelectedCategories();
	validateSelectedFilters();
});
// ---------------------------------------
// ---------------------------------------
// ---------------------------------------
$(function() {
	$('[id*="categoryValue"]').filter('[id*="AddButton"]').click(function(e) {
		getSelectedCategories();
	});
            
	$('[id*="categoryValue"]').filter('[id*="RemoveButton"]').click(function(e) {
		getSelectedCategories();
		
		//--- one or more categories have been deselected
		//--- remove any filter values that are no longer valid
		validateSelectedFilters(); 
	});
	
	$("select[title='IWD Category possible values']").dblclick(function(){getSelectedCategories();});
	$("select[title='IWD Category selected values']").dblclick(function(){getSelectedCategories(); validateSelectedFilters();});
});
// ---------------------------------------
// ---------------------------------------
// ---------------------------------------
function createValidFilters(strCategory){
	var objDataList;
	objDataList= $('[id*="AllowedFilterLookup"]').children().children();	//steps down to <tr>
	
	var objFilterAllowedValues = $('[id*="filterValue"]').filter('[id*="SelectCandidate"]');
	var strNewText;
	var strNewValue;
	strCategory = jQuery.trim(strCategory);
	
	//--- find the #, keep everything from the next char to the end
	strCategory = strCategory.substring(strCategory.indexOf('#')+1); 
	
	// this loop steps through each row of the SharePoint list for the valid category/filter combinations
	
	var strMatchThis;
	
	objDataList.each(function(i){
		//$(this.childNodes[1]).text() holds a category text
		//$(this.childNodes[2]).text() holds a corresponding filter text
		//$(this.childNodes[0]).text() holds a selectbox item value
		
		strMatchThis = jQuery.trim($(this.childNodes[1]).text());
		//--- find the #, keep everything from the next char to the end
		strMatchThis = strMatchThis.substring(strMatchThis.indexOf('#')+1); 
		
		if(strCategory==strMatchThis){
					
			strNewText = jQuery.trim($(this.childNodes[2]).text());
			strNewValue = jQuery.trim($(this.childNodes[0]).text());
			
			if(!objFilterAllowedValues.containsOption(strNewValue)){
				objFilterAllowedValues.addOption(strNewValue, strNewText, false);
			}
		}
	});
}

//-------------------------------------------------------------------------------
// if a previously selected category is removed,
// its corresponding filter values, if selected, must also be purged
//-------------------------------------------------------------------------------

function validateSelectedFilters(){

//---get current valid filter values (NOT text)
	$('[id*="filterValue"]').filter('[id*="SelectCandidate"]').selectOptions(/./);
	var strValidFilters = $('[id*="filterValue"]').filter('[id*="SelectCandidate"]').selectedValues();
	$('[id*="filterValue"]').filter('[id*="SelectCandidate"]').selectOptions(/~./,true);
	
//---get current selected filter values (NOT text)
	$('[id*="filterValue"]').filter('[id*="SelectResult"]').selectOptions(/./);
	var strSelectedFilters = $('[id*="filterValue"]').filter('[id*="SelectResult"]').selectedValues();
	$('[id*="filterValue"]').filter('[id*="SelectResult"]').selectOptions(/~./,true);

//---validate any selected filters against this list	
	var foundIt = false;

	if(strValidFilters.length > 0){
	//---there are valid filters available
	
		if(strSelectedFilters.length > 0){
		//---there are filters currently selected
		
			for (var i=0; i < strSelectedFilters.length; i++){
				foundIt = false;
				
				for (var j=0; j < strValidFilters.length; j++){
					if (strSelectedFilters[i] == strValidFilters[j]){
						//---this selected filter is valid
						$('[id*="filterValue"]').filter('[id*="SelectCandidate"]').removeOption(strSelectedFilters[i]);
						foundIt = true;
						break;
					}
				}
					
				if (!foundIt){
					//---this selected filter is not valid--- remove it
					$('[id*="filterValue"]').filter('[id*="SelectResult"]').removeOption(strSelectedFilters[i]);
				}
			}
		}
	}
	else $('[id*="filterValue"]').filter('[id*="SelectResult"]').removeOption(/./); //---clear selected filters
}
	
function getSelectedCategories(){
	$('[id*="categoryValue"]').filter('[id*="SelectResult"]').selectOptions(/./);
	var strCurrentCategories = $('[id*="categoryValue"]').filter('[id*="SelectResult"]').selectedTexts();
	$('[id*="categoryValue"]').filter('[id*="SelectResult"]').selectOptions(/~./,true);
				
	$('[id*="filterValue"]').filter('[id*="SelectCandidate"]').removeOption(/./);

	if(strCurrentCategories.length > 0){
		for (var i=0; i < strCurrentCategories.length; i++){
					
			$('[id*="filterValue"]').filter('[id*="SelectCandidate"]').each(function(e){
				createValidFilters(strCurrentCategories[i]);
			});
		}
	}
}