/* Provides functions for hiding and showing document elements based on the elements of given arrays.
 * 
 * hideObjects -  Takes an array of values representing id's of document elements. All these elements
 * 				  then have their style.display value set to "none". 
 * 
 * applyFilter -  Takes two arrays of id's of document elements. These are to be filtered to find only
 * 				  values present in both arrays (via the intersection function). Document elements with
 * 				  id's present in this filtered array then have their style.display value set to "block".
 * 
 * intersection - Takes two arrays from which it generates a single array of elements which were present
 * 				  in both the passed in arrays.
 * 
 */

function hideObjects(objects) {	
	for (i=0; i<objects.length; i++) {
		var object = document.getElementById(objects[i]);
		if (object != null) {
			document.getElementById(objects[i]).style.display = "none";
		}
    }
}

function applyFilter(firstFilterValues, secondFilterValues) {
	var filteredValues = intersection(firstFilterValues, secondFilterValues);
	
	for (i=0; i<filteredValues.length; i++) {
		var valueElement = document.getElementById(filteredValues[i]);
		if (valueElement != null) {
			valueElement.style.display = "block";
			var parent = valueElement.parentNode
			parent.removeChild(valueElement);
			parent.appendChild(valueElement);
		}
	}
}

function intersection(arrayA, arrayB) {
	var intersect = [];
	
	for (i=0; i<arrayA.length; i++) {
		for (j=0; j<arrayB.length; j++) {
			if (arrayB[j] == arrayA[i]) {
				intersect.push(arrayA[i]);
				continue;
			}
		}
	}
	return intersect;
}

function contains(sourceValues, value) {
    for (var i = 0; i < sourceValues.length; i++) {
        if (sourceValues[i] == value) {
            return true;
        }
    }
    return false;
}
