﻿/*
	"FACT-Finder Integration" operation:
	Compose URL for FACT-Finder "guided search" functionality
	N.B. Manage "OR" composition of different filter options for the same filter attribute
	N.B. Manage "AND" composition of conditions for different filter attributes
	@author Triboo
*/
function startSearch(anchorObject)
{
	var CategoryUUIDLevel1Value = document.getElementById("FF_ASN_CategoryUUIDLevel1").value;

	var pageSize = document.getElementById("FF_ASN_PageSize").value;

	var query = document.getElementById("FF_ASN_Query").value;

	var conditionsMap = new Object();

	var entryIDs = document.getElementsByName("FF_ASN_entryID");
	for (var i = 0; i < entryIDs.length; i++)
	{
		var entryID = entryIDs[i].value;

		var numberOfOptions = document.getElementById("FF_ASN_counter_" + entryID).value;

		var conditionName = document.getElementById("FF_ASN_entry_" + entryID).value;

		var extendedConditionValue = "";
		for (var j = 1; j <= numberOfOptions; j++)
		{
			var input = document.getElementById("FF_ASN_input_" + entryID + "_" + j);
			if (input.checked == true)
			{
				var optionValue = document.getElementById("FF_ASN_option_" + entryID + "_" + j).value;
				if (extendedConditionValue == "")
				{
					extendedConditionValue += optionValue;
				}
				else
				{
					extendedConditionValue += "_or_" + optionValue;
				}
			}
		}

		if (extendedConditionValue != "")
		{
			conditionsMap[conditionName] = extendedConditionValue;
		}
	}

	var extendedConditionsString = "";
	for (prop in conditionsMap)
	{
		extendedConditionsString += "&";
		extendedConditionsString += prop;
		extendedConditionsString += "=";
		extendedConditionsString += conditionsMap[prop];
	}

	if (extendedConditionsString != "")
	{
		var encodedExtendedConditionsString = encodeURIComponent(extendedConditionsString);

		var searchURL = query + encodedExtendedConditionsString;

		if (CategoryUUIDLevel1Value != "")
		{
			searchURL += "&CategoryUUIDLevel1=" + CategoryUUIDLevel1Value;
		}

		if (pageSize != "")
		{
			searchURL += "&PageSize=" + pageSize;
		}

		anchorObject.href = searchURL;
	}
	else
	{
		alert("Seleziona almeno una opzione di filtro");
	}
}

/*
	"FACT-Finder Integration" operation:
	Manage "checked" property of checkbox to select/deselect all product comparison checkboxes
	@author Triboo
*/
function manageCompareCheckbox()
{
	var compareCheckbox = document.getElementById("confronta");

	var productCheckboxes = document.getElementsByName("ProductRefID");

	var isChecked = false;
	for (var i = 0; i < productCheckboxes.length; i++)
	{
		if (productCheckboxes[i].checked == true)
		{
			isChecked = true;
			break;
		}
	}

	if (isChecked == true)
	{
		compareCheckbox.checked = true;
	}
	else
	{
		compareCheckbox.checked = false;
	}
}

