﻿// **************************************************************************************************************************
// VARIABLES FOR EXTENDED SEARCH
// **************************************************************************************************************************

// actual language id for showed data
var actualLanguageId = "4";
var firstItemText = "-- selecteer --";
var countLoadingEnabled = true;

// settings of loader icon
var concreteLoaderSuffix = "_loader";
var countLoaderId = "countLoaderElement";
var loaderImage = "/images/extended-search-count-loader.gif";
var countLoaderImage = "/images/extended-search-count-loader.gif";

// element for counting of found items
var counterElementId = "counterDIV";

// array of ID's of HTML selects
var selectsIDs = new Array (
	"FindFormDefProduct_AttributeI9",
	"FindFormDefProduct_CategoryId",
	"FindFormDefProduct_AttributeI14",
	"FindFormDefProduct_AttributeI15",
	"FindFormDefProduct_AttributeI16",
	"FindFormDefProduct_AttributeI18"
	);
	
var selectsFirstValues = new Array (
	"-- select --",
	"-- select --",
	"-- select --"
	);
	
// array of URLs for connector with data of equal HTML select
var connectors = new Array (
	"http://"+ GetUrlBase() +"/connector/connectorOrchids.aspx?file=es-01-attribute-9&languageid=[lang]",
	"http://"+ GetUrlBase() +"/connector/connectorOrchids.aspx?file=es-02-category&languageid=[lang]&attribute9value=[0]",
	"http://"+ GetUrlBase() +"/connector/connectorOrchids.aspx?file=es-04-attribute-14&languageid=[lang]&attribute9value=[0]&categoryid=[1]",
	"http://"+ GetUrlBase() +"/connector/connectorOrchids.aspx?file=es-05-attribute-15&languageid=[lang]&attribute9value=[0]&categoryid=[1]&attribute14value=[2]",
	"http://"+ GetUrlBase() +"/connector/connectorOrchids.aspx?file=es-06-attribute-16&languageid=[lang]&attribute9value=[0]&categoryid=[1]&attribute14value=[2]&attribute15value=[3]",
	"http://"+ GetUrlBase() +"/connector/connectorOrchids.aspx?file=es-07-attribute-18&languageid=[lang]&attribute9value=[0]&categoryid=[1]&attribute14value=[2]&attribute15value=[3]&attribute16value=[4]"
	);

// array of URLs for connector information about count of future found items in DB
// -- note: the last record is for final selection in all select boxes
var connectors4count = new Array (
	"http://"+ GetUrlBase() +"/connector/connectorOrchids.aspx?file=es-01-attribute-9-count",
	"http://"+ GetUrlBase() +"/connector/connectorOrchids.aspx?file=es-02-category-count&attribute9value=[0]",
	"http://"+ GetUrlBase() +"/connector/connectorOrchids.aspx?file=es-04-attribute-14-count&attribute9value=[0]&categoryid=[1]",
	"http://"+ GetUrlBase() +"/connector/connectorOrchids.aspx?file=es-05-attribute-15-count&attribute9value=[0]&categoryid=[1]&attribute14value=[2]",
	"http://"+ GetUrlBase() +"/connector/connectorOrchids.aspx?file=es-06-attribute-16-count&attribute9value=[0]&categoryid=[1]&attribute14value=[2]&attribute15value=[3]",
	"http://"+ GetUrlBase() +"/connector/connectorOrchids.aspx?file=es-07-attribute-18-count&attribute9value=[0]&categoryid=[1]&attribute14value=[2]&attribute15value=[3]&attribute16value=[4]",
	"http://"+ GetUrlBase() +"/connector/connectorOrchids.aspx?file=es-final-count&attribute9value=[0]&categoryid=[1]&attribute14value=[2]&attribute15value=[3]&attribute16value=[4]&attribute18value=[5]"
	);
	
var selectedValues = new Array (
	"-1",
	"-1",
	"-1",
	"-1",
	"-1",
	"-1"
	);

// **************************************************************************************************************************
// METHOD FOR SETTINGS OF THE BEGIN STATUS OF ALL OBJECTS
// --------------------------------------------------------------------------------------------------------------------------
// !!!!!!! IMPORTANT NOTE: the method must be executed inmediately after page is loaded
// **************************************************************************************************************************

// =========================================================================================
// Initialization of central settings
// =========================================================================================
function InitExtendedSearch()
{
	// setting default values
	for (var i = 0; i < selectsIDs.length; i++)
	{
		var obj = document.getElementById(selectsIDs[i]);
		var objLoader = document.getElementById(selectsIDs[i] + concreteLoaderSuffix);
		if (objLoader != null)
		{
			objLoader.innerHTML = "<img src=\""+ loaderImage +"\" alt=\" \" style=\"vertical-align: middle;\" />";
		}
	}
	
	// loading the first item
	ExtendedSearch(0);
}

// =========================================================================================
// Settings all selects as disabled
// =========================================================================================
function DisableSelects()
{
	for (var i = 0; i < selectsIDs.length; i++)
	{
		var obj = document.getElementById(selectsIDs[i]);
		if (obj != null)
		{
			obj.disabled = true;
		}
	}
}

// =========================================================================================
// Settings selects enabled from startIndex to endIndex
// =========================================================================================
function EnableSelects(startIndex, endIndex)
{
	for (var i = (startIndex != null ? startIndex : 0); i <= (endIndex != null ? endIndex : selectsIDs.length - 1); i++)
	{
		var obj = document.getElementById(selectsIDs[i]);
		if (obj != null)
		{
			obj.disabled = false;
		}
	}
}
	
// **************************************************************************************************************************
// GENERAL EXTENDED SEARCH METHODS
// **************************************************************************************************************************

// indication if we are not already searching and loading some data
var isNowSearching = false;

// =========================================================================================
// GENERAL SEARCHING METHOD
// =========================================================================================
function ExtendedSearch(itemIndex)
{
	// if we are already making some searching go out, else set that we are searching now
	if (isNowSearching) 
	{
		return;
	}
	else
	{
		isNowSearching = true;
	}
	
	// creating object of actual select
	var obj = document.getElementById(selectsIDs[itemIndex]);
	
	// if we want to return back to some value, we have to load one before that (that is when we click on the first item in select)
	if (((getSelectSelectedValue(selectsIDs[itemIndex]) == "") || (getSelectSelectedValue(selectsIDs[itemIndex]) == null)) && (obj.disabled == false))
	{
		itemIndex--;
	}
	
	// putting all search selects into settings for data loading
	LoadingStarted(itemIndex);
	
	// showing item's and count loader and start loading data
	if (obj != null)
	{
		// showing loaders
		ShowLoaders(itemIndex);
		
		// start loading data
		LoadSelectPosibilities(itemIndex);
	}
	else
	{
		isNowSearching = false;
	}
}

// =========================================================================================
// Loading all options into select
// =========================================================================================
function LoadSelectPosibilities(itemIndex)
{
    if (!ajaxLoadingData)
    {
        AjaxTransfer(ReplaceRequestValues(connectors[itemIndex]));
        LoadSelectPosibilitiesProcess(itemIndex);
    }
    else
    {
        window.setTimeout("LoadSelectPosibilities("+ itemIndex +");", 10);
    }
}

function LoadSelectPosibilitiesProcess(itemIndex)
{
    if (!ajaxResultReady)
    {
        window.setTimeout("LoadSelectPosibilitiesProcess("+ itemIndex +");", 10);
    }
    else
    {
		// filling data into select
		var items = ajaxResultsXML.getElementsByTagName("item");
		if (items.length > 0)
		{
			for (var i = 0; i < items.length; i++)
			{
				insertSelectItem(selectsIDs[itemIndex], 
					items[i].attributes.getNamedItem("name").nodeValue.toString(), 
					items[i].attributes.getNamedItem("id").nodeValue.toString());
			}
		}
		
		// setting identification for AJAX framework, that data were loaded
        ajaxResultReady = false;
        ajaxLoadingData = false;
		
		// if count loading is enabled, load count, else make operations for finished data loading
		if (countLoadingEnabled)
		{
			LoadCountBySelection(itemIndex);
		}
		else
		{
			LoadingFinished(itemIndex);
		}
    }
}

// =========================================================================================
// Getting count of records for actual choice
// =========================================================================================
function LoadCountBySelection(itemIndex)
{
    if (!ajaxLoadingData)
    {
        AjaxTransfer(ReplaceRequestValues(connectors4count[itemIndex]));
        LoadCountBySelectionProcess(itemIndex);
    }
    else
    {
        window.setTimeout("LoadCountBySelection("+ itemIndex +");", 10);
    }
}

function LoadCountBySelectionProcess(itemIndex)
{
    if (!ajaxResultReady)
    {
        window.setTimeout("LoadCountBySelectionProcess("+ itemIndex +");", 10);
    }
    else
    {
		var items = ajaxResultsXML.getElementsByTagName("item");
		if (items.length > 0)
		{
			var countLoader = document.getElementById(countLoaderId);
			if (countLoader != null)
			{
				countLoader.innerHTML = items[0].attributes.getNamedItem("count").nodeValue;
			}
		}
		
		// setting identification for AJAX framework, that data were loaded
        ajaxResultReady = false;
        ajaxLoadingData = false;
		
		// loading finish
		LoadingFinished(itemIndex);
    }
}

// =========================================================================================
// Method for putting all selects into settings for started loading
// =========================================================================================
function LoadingStarted(itemIndex)
{
	// disable of all selects
	DisableSelects();

	// hiding all items with higher index in arrays and setting them to theirs default values
	for (var i = itemIndex; i < selectsIDs.length; i++)
	{
		var obj = document.getElementById(selectsIDs[i]);
		if (obj != null)
		{
			clearSelect(selectsIDs[i]);
			insertSelectItem(selectsIDs[i], selectsFirstValues[i], "");
			//obj.disabled = true;
		}
		
		var objLoader = document.getElementById(selectsIDs[i] + concreteLoaderSuffix);
		if (objLoader != null)
			objLoader.style.visibility = "hidden";
	}
	
	// showing wanted loaders
	ShowLoaders(itemIndex);
}

// =========================================================================================
// Method for putting all selects into settings for finished
// =========================================================================================
function LoadingFinished(itemIndex)
{
	// making all select until loaded choice enabled
	EnableSelects(0, itemIndex);

	// hiding all loaders assigned to selects
	HideLoaders();
	
	// searching indicator settings to status, that actual searching is finished
	isNowSearching = false;
}

// =========================================================================================
// Method for showing loaders by index of select item in arrays
// =========================================================================================
function ShowLoaders(itemIndex)
{
	// showing item's loader
	var objLoader = document.getElementById(selectsIDs[itemIndex] + concreteLoaderSuffix);
	if (objLoader != null)
		objLoader.style.visibility = "visible";
	
	// showing count loader	
	if (countLoadingEnabled)
	{
		var countLoader = document.getElementById(countLoaderId);
		if (countLoader != null)
			countLoader.innerHTML = "<img src=\""+ countLoaderImage +"\" alt=\" \" />";
	}
}

// =========================================================================================
// Method for hiding all possible loaders
// =========================================================================================
function HideLoaders()
{
	// hiding loaders assigned to selects
	for (var i = 0; i < selectsIDs.length; i++)
	{	
		var objLoader = document.getElementById(selectsIDs[i] + concreteLoaderSuffix);
		if (objLoader != null)
			objLoader.style.visibility = "hidden";
	}
}

// =========================================================================================
// Method for replacement of values inside all selects
// =========================================================================================
function ReplaceRequestValues(requestURL)
{
    var result = (requestURL == null ? "" : requestURL);
	for (var i = 0; i < selectsIDs.length; i++)
	{	
		var obj = document.getElementById(selectsIDs[i]);
		if (obj != null)
		{
			var selectedValue = getSelectSelectedValue(selectsIDs[i]);
			result = result.replace("["+ i +"]", (selectedValue != null ? selectedValue : "-1"));
		}
	}
	
	result = result.replace("[lang]", actualLanguageId);
	
	return result;
}


















// **************************************************************************************************************************
// Work with SELECT element OPTIONS
// **************************************************************************************************************************

// removing all items from select
function clearSelect(selectId)
{
    var selectToClear = document.getElementById(selectId);
	if (selectToClear == null) { return -1; }
	
    while (selectToClear.options.length > 0) {
        removeSelectItem(selectId, selectToClear.options.length - 1);
    }
}

// getting value of selected item in select
function getSelectSelectedValue(selectId)
{
    var result = null;
    var selectObj = document.getElementById(selectId);
	if (selectObj == null) { return -1; }
	
    for (var i = selectObj.length - 1; i >= 0; i--) {
        if (selectObj.options[i].selected) {
            result = selectObj.options[i].value;
        }
    }
    return result;
}

// getting text of selected item in select
function getSelectSelectedText(selectId)
{
    var result = null;
    var selectObj = document.getElementById(selectId);
	if (selectObj == null) { return -1; }
	
    for (var i = selectObj.length - 1; i >= 0; i--) {
        if (selectObj.options[i].selected) {
            result = selectObj.options[i].text;
        }
    }
    return result;
}

// removing item in select
function removeSelectItem(selectId, index)
{
    var sel = document.getElementById(selectId);
	if (sel == null) { return -1; }
	
    sel.remove(index);
}

// inserting item into select
function insertSelectItem(selectId, newText, newValue)
{
    var newOption = document.createElement('option');
    newOption.text = newText;
    newOption.value = newValue;
    var sel = document.getElementById(selectId);
	if (sel == null) { return -1; }
	  
    try {
        sel.add(newOption, null); // standards compliant; doesn't work in IE
    } catch(ex) {
        sel.add(newOption);       // IE only
    }
}