function clearGeneralSearch()
{
	$("input[class='fritekst']").each(function()
		{
			this.value = "";
		}
	);
}

function go()
{
	// Function Line
	if ($("#searchProducts")[0].selectFunctionLine.selectedIndex != 0)
	{
	    $("#searchProducts")[0].functionLine.value = $("#searchProducts")[0].selectFunctionLine.options[$("#searchProducts")[0].selectFunctionLine.selectedIndex].innerHTML;
	}
	
	// Product Line
	if ($("#searchProducts")[0].selectProductLine.selectedIndex != 0)
	{
	    $("#searchProducts")[0].productLine.value = $("#searchProducts")[0].selectProductLine.options[$("#searchProducts")[0].selectProductLine.selectedIndex].innerHTML;
	}
	
	// Designer
	if ($("#searchProducts")[0].selectDesigner.selectedIndex != 0)
	{
	    $("#searchProducts")[0].designer.value = $("#searchProducts")[0].selectDesigner.options[$("#searchProducts")[0].selectDesigner.selectedIndex].innerHTML;
	}
	
	$("#searchProducts")[0].price.value = $("#searchProducts")[0].priceRange.options[$("#searchProducts")[0].priceRange.selectedIndex].value;
	//$("#searchProducts")[0].submit();
    var searchParams = "&langID=" + encodeURIComponent($("#searchProducts")[0].langID.value) + 
                        "&functionLine=" + encodeURIComponent($("#searchProducts")[0].functionLine.value) + 
                        "&productLine=" + encodeURIComponent($("#searchProducts")[0].productLine.value) + 
                        "&designer=" + encodeURIComponent($("#searchProducts")[0].designer.value) + 
                        "&price=" + $("#searchProducts")[0].price.value;
    window.location = $("#searchProducts")[0].action + searchParams;
}

function getProductLines(control)
{
	// Clear Full-Text Search
	clearGeneralSearch();
	
	// Select all controls
	$("select[name='selectFunctionLine']").each(function()
		{
			this.selectedIndex = control.selectedIndex;
		});
	
	$("select[name='selectProductLine']").each(function()
	    {
		    this.selectedIndex = 0;
	    });
		
	$("select[name='selectDesigner']").each(function()
        {
	        this.selectedIndex = 0;
        });
	
	var str = "langID=" + $("#searchProducts")[0].langID.value + "&functionLineName=" + encodeURIComponent(control.options[control.selectedIndex].value);

	$.ajax({
		type: "POST",
		url: "/WebService/productfinder.asmx/GetProductLines",
		data: str,
		success: function(data)
		{
            res = data.documentElement;
			var str = "";
			for (var i = 0; i < res.childNodes.length; i++)
				str += res.childNodes[i].nodeValue;
			
			RA = new Array();
			eval(str);
			fillProductLines(str);
			UpdateProductLine();
			getDesigners($("#selectProductLine")[0]);
		}
    });
}

function getDesigners(control)
{
	// Clear Full-Text Search
	clearGeneralSearch();
	
	$("select[name='selectProductLine']").each(function()
		{
			this.selectedIndex = control.selectedIndex;
		});
		
	$("select[name='selectDesigner']").each(function()
	    {
		    this.selectedIndex = 0;
	    });
    	
	var str = "langID=" + $("#searchProducts")[0].langID.value + "&functionLineName=" + encodeURIComponent($("#selectFunctionLine")[0].options[$("#selectFunctionLine")[0].selectedIndex].value) + "&productLineName=" + encodeURIComponent(control.options[control.selectedIndex].value);
	$.ajax({
		type: "POST",
		url: "/WebService/productfinder.asmx/GetDesigners",
		data: str,
		success: function(data)
		{
            res = data.documentElement;
			var str = "";
			for (var i = 0; i < res.childNodes.length; i++)
				str += res.childNodes[i].nodeValue;
			
			RA = new Array();
			eval(str);
			fillDesigners(str);
			UpdateDesigner();
		}
    });
}

function getPriceRanges(control)
{
    // Clear Full-Text Search
	clearGeneralSearch();
	
	var str = "langID=" + $("#searchProducts")[0].langID.value + 
	            "&functionLineName=" + encodeURIComponent($("#selectFunctionLine")[0].options[$("#selectFunctionLine")[0].selectedIndex].value) + 
	            "&productLineName=" + encodeURIComponent($("#selectProductLine")[0].options[$("#selectProductLine")[0].selectedIndex].value) + 
	            "&designerName=" + encodeURIComponent($("#selectDesigner")[0].options[$("#selectDesigner")[0].selectedIndex].value);
	$.ajax({
		type: "POST",
		url: "/WebService/productfinder.asmx/GetPriceRanges",
		data: str,
		success: function(data)
		{
            res = data.documentElement;
			var str = "";
			for (var i = 0; i < res.childNodes.length; i++)
				str += res.childNodes[i].nodeValue;
			
			RA = new Array();
			eval(str);
			fillPriceRanges(str);
			UpdatePriceRange();
		}
    });
}

function designerSelected(control)
{
	// Clear Full-Text Search
	clearGeneralSearch();
	
	// Select all controls
	$("select[name='selectDesigner']").each(function()
		{
			this.selectedIndex = control.selectedIndex;
		});
}

function priceSelected(control)
{
	// Clear Full-Text Search
	clearGeneralSearch();
	
	// Select all controls
	$("select[name='priceRange']").each(function()
		{
			this.selectedIndex = control.selectedIndex;
		});
}

function fillProductLines(str)
{
	$("#searchSets select[name='selectProductLine']").each(function()
		{
			this.innerHTML = "";
    
		    // Add the first element
		    var first = document.createElement("option");
		    first.value = "";
		    first.innerHTML = "alle";
			this.appendChild(first);
			
			for (var i = 0; i < RA.length; i++)
			{
				var el = document.createElement("option");
				el.value = RA[i][0];
				el.innerHTML = RA[i][1];
				this.appendChild(el);
			}
		});
}

function fillDesigners(str)
{
	$("#searchSets select[name='selectDesigner']").each(function()
		{
			this.innerHTML = "";
    
		    // Add the first element
		    var first = document.createElement("option");
		    first.value = "";
		    first.innerHTML = "alle";
			this.appendChild(first);
			
			for (var i = 0; i < RA.length; i++)
			{
				var el = document.createElement("option");
				el.value = RA[i][0];
				el.innerHTML = RA[i][1];
				this.appendChild(el);
			}
		});
}

function fillPriceRanges(str)
{
    $("#searchSets select[name='priceRange']").each(function()
	{
		this.innerHTML = "";

	    // Add the first element
	    var first = document.createElement("option");
	    first.value = "";
	    first.innerHTML = "alle";
		this.appendChild(first);
		
		for (var i = 0; i < RA.length; i++)
		{
			var el = document.createElement("option");
			el.value = RA[i][0];
			el.innerHTML = RA[i][1];
			this.appendChild(el);
		}
	});
}

function cleanCriteria(control)
{
	control.functionLine.value = "";
	control.productLine.value = "";
	control.designer.value = "";
	control.price.value = "0";
}

function getQuerystring(key, location, default_)
{
  if (default_==null) default_="";
  key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
  if (location == null)
    var qs = regex.exec(window.location.href);
  else
    var qs = regex.exec(window.location.href);
  if(qs == null)
    return default_;
  else
    return qs[1];
}

// hack for not encoded function line name
function getFunctionLineString()
{
    var src = window.location.href
    flStart = src.indexOf('functionLine') + 'functionLine'.length + 1;
    flEnd = src.indexOf('productLine') - 1;
    return src.substring(flStart, flEnd);
}

var oldFunctionLine = decodeURIComponent(getFunctionLineString()).replace('&amp;', '&');
function UpdateFunctionLine()
{
    if (oldFunctionLine != '')
    {
        var i = 0;
        $("#selectFunctionLine option").each(function()
        {
            if (this.value == oldFunctionLine)
            {
                $("select[name='selectFunctionLine']").each(function()
	            {  
	                this.selectedIndex = i > this.length ? i - this.length : i; 
	            });
	        }
            i++;
        });
        if (oldProductLine == '' && oldDesigner == '')
            getPriceRanges(null);
        oldFunctionLine = '';
    }
}

var oldProductLine = decodeURIComponent(getQuerystring('productLine')).replace('&amp;', '&');
function UpdateProductLine()
{
    if (oldProductLine != '')
    {
        var i = 0;
        $("#selectProductLine option").each(function()
        {
            if (this.value == oldProductLine)
            {
                $("select[name='selectProductLine']").each(function()
	            {  
	                this.selectedIndex = i > this.length ? i - this.length : i; 
	            });
	        }
            i++;
        });
        if (oldDesigner == '')
            getPriceRanges(null);
        oldProductLine = '';
    }
}

var oldDesigner = decodeURIComponent(getQuerystring('designer')).replace('&amp;', '&');
function UpdateDesigner()
{
    if (oldDesigner != '')
    {
        var i = 0;
        i = 0;
        $("#selectDesigner option").each(function()
        {
            if (this.value == oldDesigner)
            {
                $("select[name='selectDesigner']").each(function()
	            {  
	                this.selectedIndex = i > this.length ? i - this.length : i; 
	            });
	        }
            i++;
        });
        getPriceRanges(null);
        oldDesigner = '';
    }
}

var oldPriceRange = getQuerystring('price');
function UpdatePriceRange()
{
    if (oldPriceRange != '')
    {
        var i = 0;
        $("#priceRange option").each(function()
        {
            if (this.value == oldPriceRange)
            {
                $("select[name='priceRange']").each(function()
	            {  
	                this.selectedIndex = i > this.length ? i - this.length : i; 
	            });
	        }
            i++;
        });
        oldPriceRange = '';
    }
}

var readyExecuted = false;
$().ready(function()
{
    cleanCriteria($("#searchProducts")[0]);
    UpdateFunctionLine();
    if (!readyExecuted)
    {
        getProductLines($("#selectFunctionLine")[0]);
        readyExecuted = true;
    }
});