var _handlingAjaxRequest = false;

function handleHistoryChange(newLocation, historyData)
{
    if (_handlingAjaxRequest) return;

    _handlingAjaxRequest = true;
    if (historyData == null)
    {
        var parts = window.location.href.split("#?");
        var element = null;
        if (parts[1] != null && parts[1] != "")
        {
            WebComponents.Product.AjaxProductList.GetMainProductList("?" + parts[1], GetMainProductList_callback);
            element = document.getElementById('ajaxList');
        }
        else
        {
            if (window.location.href.indexOf("?") != -1)
            {
                _handlingAjaxRequest = false;
                return;
            }
            WebComponents.Product.AjaxProductList.GetMainProductList("?p=index", GetMainProductList_callback);
            element = document.getElementById('ajaxMain');
        }
        element.innerHTML = '<div style="text-align: center;"><img alt="A Carregar..." src="images/loading.gif" width="320" height="320" /></div>';
    }
    else
    {
        if ((historyData.querystring == null || historyData.querystring == "") && window.location.href.indexOf("?") != -1)
        {
            _handlingAjaxRequest = false;
            return false;
        }

        var element = null;
        if(historyData.listType == 'rows' && document.getElementById('ajaxList') != null)
        {
            WebComponents.Product.AjaxProductList.GetRowsProductList(historyData.querystring, GetRowsProductList_callback);
            element = document.getElementById('ajaxList');
        }
        else //if(listType == 'main')
        {
            WebComponents.Product.AjaxProductList.GetMainProductList(historyData.querystring, GetMainProductList_callback);
            element = document.getElementById('ajaxMain');
        }
        element.innerHTML = '<div style="text-align: center;"><img alt="A Carregar..." src="images/loading.gif" width="320" height="320" /></div>';
    }
}

function processForm(formElement, listType)
{
    var querystring = '';
    for (var i=0; i<formElement.length; i++)
    {
	    if(querystring.length > 0)
		    querystring += '&' + formElement.elements[i].name + '=' + formElement.elements[i].value;
	    else
		    querystring = '?' + formElement.elements[i].name + '=' + formElement.elements[i].value;
    }
    /*
    var element = null;
    if(listType == 'rows')
    {
        WebComponents.Product.AjaxProductList.GetRowsProductList(querystring, GetRowsProductList_callback);
        element = document.getElementById('ajaxList');
    }
    else //if(listType == 'main')
    {
        WebComponents.Product.AjaxProductList.GetMainProductList(querystring, GetMainProductList_callback);
        element = document.getElementById('ajaxMain');
    }
    element.innerHTML = '<div style="text-align: center;"><img alt="A Carregar..." src="images/loading.gif" width="320" height="320" /></div>';
    window.location.href = "#" + querystring;
    */
    window.scroll(0, 0);
    window.dhtmlHistory.add(querystring, {querystring: querystring, listType: listType});
    handleHistoryChange(querystring, {querystring: querystring, listType: listType});
    return false;
}

function processLink(querystring, listType)
{
    /*
    var element = null;
    if(listType == 'rows')
    {
        WebComponents.Product.AjaxProductList.GetRowsProductList(querystring, GetRowsProductList_callback);
        element = document.getElementById('ajaxList');
    }
    else //if(listType == 'main')
    {
        WebComponents.Product.AjaxProductList.GetMainProductList(querystring, GetMainProductList_callback);
        element = document.getElementById('ajaxMain');
    }
    element.innerHTML = '<div style="text-align: center;"><img alt="A Carregar..." src="images/loading.gif" width="320" height="320" /></div>';
    window.location.href = "#" + querystring;
    */
    window.scroll(0, 0);
    window.dhtmlHistory.add(querystring, {querystring: querystring, listType: listType});
    handleHistoryChange(querystring, {querystring: querystring, listType: listType});
	return false;
}

function GetRowsProductList_callback(res)
{
	var element = document.getElementById('ajaxList');
	if(res.value != '')
	{
		element.innerHTML = res.value;
		setListFilters();
	}
	_handlingAjaxRequest = false;
}

function GetMainProductList_callback(res)
{
	var element = document.getElementById('ajaxMain');
	if(res.value != '')
	{
		element.innerHTML = res.value;
		setListFilters();
	}
	_handlingAjaxRequest = false;
}

//stock filter
function processFilter(filterSelectType)
{
	for(i=1; i < filterSelectType.length; i++)
	{
		if(i == filterSelectType.selectedIndex)
			showDiv(filterSelectType.options[i].value);
		else
			hideDiv(filterSelectType.options[i].value);
	}
	
	if(filterSelectType.options[filterSelectType.selectedIndex].value == 'manufFilter')
	{
		document.getElementById('orderFilterValue1').style.display = 'none';
		document.getElementById('orderFilterValue2').style.display = 'inline';
	}
	else
	{
		document.getElementById('orderFilterValue2').style.display = 'none';
		document.getElementById('orderFilterValue1').style.display = 'inline';
	}
}

function selectFilter(optionId)
{
	if (document.getElementById && document.getElementById(optionId)!=null)
	{
		document.getElementById(optionId).selected=true;
	}
}

function removeManuf()
{
	document.getElementById('filterType').remove(3);
	document.getElementById('orderFilterValue1').remove(2);
}

function setListFilters()
{
    var sToTest = window.location.href.split("#")[1];
    
    if (sToTest == null || sToTest == null) return;

    var filterType = "none";
    var stockFilterValue = "none";
    var priceFilterValue = "none";
    var manufFilterValue = "none";
    var orderFilterValue1 = "name";
    var orderFilterValue2 = "name";

    if (!(/[\?&]filterType=([^&$]*)/).test(sToTest)) return;

    if ((/[\?&]filterType=([^&$]*)/).test(sToTest)) filterType = RegExp.$1;
    if ((/[\?&]stockFilterValue=([^&$]*)/).test(sToTest)) stockFilterValue = RegExp.$1;
    if ((/[\?&]priceFilterValue=([^&$]*)/).test(sToTest)) priceFilterValue = RegExp.$1;
    if ((/[\?&]manufFilterValue=([^&$]*)/).test(sToTest)) manufFilterValue = RegExp.$1;
    if ((/[\?&]orderFilterValue1=([^&$]*)/).test(sToTest)) orderFilterValue1 = RegExp.$1;
    if ((/[\?&]orderFilterValue2=([^&$]*)/).test(sToTest)) orderFilterValue2 = RegExp.$1;
    
    document.getElementById("filterType").value = filterType;
    document.getElementById("stockFilterValue").value = stockFilterValue;
    document.getElementById("priceFilterValue").value = priceFilterValue;
    document.getElementById("manufFilterValue").value = manufFilterValue;
    document.getElementById("orderFilterValue1").value = orderFilterValue1;
    document.getElementById("orderFilterValue2").value = orderFilterValue2;
    
    processFilter(document.getElementById("filterType"));
}

//redirect to querystring passed by anchor
addListener(window, "load",
    function ()
    {
        window.dhtmlHistory.initialize();
        window.dhtmlHistory.addListener(handleHistoryChange);
        if ((/#\?/).test(window.location.href))
        {
        //    window.location.href = window.location.href.split("#")[1];
            var querystring = window.location.href.split("#")[1];
            window.handleHistoryChange(querystring, {querystring: querystring, listType: null});
        }
    });

