function AdviHandleidingenzoeker_loadCategories()
{
	this.wait.setStyle("display","block");
	
	while( this.categorySelect.firstChild != null )
	{
		this.categorySelect.removeChild(this.categorySelect.firstChild);
	}
	while( this.productSelect.firstChild != null )
	{
		this.productSelect.removeChild(this.productSelect.firstChild);
	}

	var request = new Request.JSON({
		url: this.jsonURL,
		onSuccess: function(jsonObject){
			if( jsonObject.result == "OK" )
			{
				// Ingeladen, afbeelden
				for( var i = 0; i < jsonObject.categories.length; i++ )
				{
					var title = jsonObject.categories[i][1];
					for( var j = 0; j < jsonObject.categories[i][2]*2; j++ )
					{
						title = String.fromCharCode(160) + title;
					}
					var option = new Option(title,jsonObject.categories[i][0]);
					try
					{
						this.instance.categorySelect.options.add(option,null);
					}
					catch(ex)
					{
						this.instance.categorySelect.options.add(option);
					}
					
				}
				
				// Selecteer categorie optie in productSelect
				var option = new Option("Selecteer eerst een categorie","0");
				option.setAttribute("disabled",true);
				try
				{
					this.instance.productSelect.options.add(option,null);
				}
				catch(ex)
				{
					this.instance.productSelect.options.add(option);
				}
				
				this.instance.wait.setStyle("display","none");
			}
			else
			{
				alert("De gegevens konden niet worden ingeladen. Probeer het aub opnieuw (onSuccess)");
			}
		},
		onFailure: function(instance){
			alert("De gegevens konden niet worden ingeladen. Probeer het aub opnieuw (onFailure)");
		},
		onException: function(headerName,value){
			alert("De gegevens konden niet worden ingeladen. Probeer het aub opnieuw (onException)");
		}
	});

	var sendArg = new Object();
	sendArg.load = "categories"
	request.instance = this;
	request.send("JSONData=" + JSON.encode(sendArg));
}

function AdviHandleidingenzoeker_loadProducts(categoryId)
{
	this.wait.setStyle("display","block");
	
	while( this.productSelect.firstChild != null )
	{
		this.productSelect.removeChild(this.productSelect.firstChild);
	}

	var request = new Request.JSON({
		url: this.jsonURL,
		onSuccess: function(jsonObject){
			if( jsonObject.result == "OK" )
			{
				// Ingeladen, afbeelden
				if( jsonObject.products.length == 0 )
				{
					var option = new Option("Geen producten gevonden","0");
					option.setAttribute("disabled",true);
					try
					{
						this.instance.productSelect.options.add(option,null);
					}
					catch(ex)
					{
						this.instance.productSelect.options.add(option);
					}
				}
				else
				{
					for( var i = 0; i < jsonObject.products.length; i++ )
					{
						var option = new Option(jsonObject.products[i][1],jsonObject.products[i][0]);
						try
						{
							this.instance.productSelect.options.add(option,null);
						}
						catch(ex)
						{
							this.instance.productSelect.options.add(option);
						}
					}
				}
				this.instance.wait.setStyle("display","none");
			}
			else
			{
				alert("De gegevens konden niet worden ingeladen. Probeer het aub opnieuw (onSuccess)");
			}
		},
		onFailure: function(instance){
			alert("De gegevens konden niet worden ingeladen. Probeer het aub opnieuw (onFailure)");
		},
		onException: function(headerName,value){
			alert("De gegevens konden niet worden ingeladen. Probeer het aub opnieuw (onException)");
		}
	});

	var sendArg = new Object();
	sendArg.load = "products"
	sendArg.category = categoryId;
	request.instance = this;
	request.send("JSONData=" + JSON.encode(sendArg));
}

function AdviHandleidingenzoeker_loadManuals(articleCode)
{
	this.wait.setStyle("display","block");
	
	while( this.manualResult.firstChild != null )
	{
		this.manualResult.removeChild(this.manualResult.firstChild);
	}

	var request = new Request.JSON({
		url: this.jsonURL,
		onSuccess: function(jsonObject){
			if( jsonObject.result == "OK" )
			{
				// Ingeladen, afbeelden
				if( jsonObject.manuals.length == 0 )
				{
					this.instance.manualResult.innerHTML = "Er zijn geen handleidingen gevonden.";
				}
				else
				{
					for( var i = 0; i < jsonObject.manuals.length; i++ )
					{
						var manual = new Element("a");
						manual.setAttribute("href",jsonObject.manuals[i][0]);
						manual.appendChild(document.createTextNode(jsonObject.manuals[i][1]));
						this.instance.manualResult.appendChild(manual);
					}
				}
				this.instance.wait.setStyle("display","none");
			}
			else
			{
				alert("De gegevens konden niet worden ingeladen. Probeer het aub opnieuw (onSuccess)");
			}
		},
		onFailure: function(instance){
			alert("De gegevens konden niet worden ingeladen. Probeer het aub opnieuw (onFailure)");
		},
		onException: function(headerName,value){
			alert("De gegevens konden niet worden ingeladen. Probeer het aub opnieuw (onException)");
		}
	});

	var sendArg = new Object();
	sendArg.load = "manuals"
	sendArg.articlecode = articleCode;
	request.instance = this;
	request.send("JSONData=" + JSON.encode(sendArg));
}

function AdviHandleidingenzoeker(destinationElementId,jsonURL)
{
	this.destinationElement = $(destinationElementId);
	this.jsonURL = jsonURL;
	
	this.loadCategories = AdviHandleidingenzoeker_loadCategories;
	this.loadProducts = AdviHandleidingenzoeker_loadProducts;
	this.loadManuals = AdviHandleidingenzoeker_loadManuals;
	
	this.category = new Element("div");
	this.category.addClass("at_handleidingenzoeker_header");
	this.category.appendChild(document.createTextNode("Kies categorie"));
	this.destinationElement.appendChild(this.category);
	
	this.product = new Element("div");
	this.product.addClass("at_handleidingenzoeker_header");
	this.product.appendChild(document.createTextNode("Kies product"));
	this.destinationElement.appendChild(this.product);
					
	this.manual = new Element("div");
	this.manual.addClass("at_handleidingenzoeker_header");
	this.manual.appendChild(document.createTextNode("Handleidingen"));
	this.destinationElement.appendChild(this.manual);
	
	this.categorySelect = new Element("select");
	this.categorySelect.setAttribute("size",10);
	this.categorySelect.addClass("at_handleidingenzoeker_select");
	this.categorySelect.instance = this;
	this.categorySelect.addEvent("change",function(){
		this.instance.loadProducts(this.value);
		this.instance.manualResult.innerHTML = "Selecteer een product.";
	});
	this.destinationElement.appendChild(this.categorySelect);
	
	this.productSelect = new Element("select");
	this.productSelect.setAttribute("size",10);
	this.productSelect.addClass("at_handleidingenzoeker_select");
	this.productSelect.instance = this;
	this.productSelect.addEvent("change",function(){
		this.instance.loadManuals(this.value);
	});
	this.destinationElement.appendChild(this.productSelect);
	
	this.manualSelect = new Element("div");
	this.manualSelect.addClass("at_handleidingenzoeker_manuals");
	this.destinationElement.appendChild(this.manualSelect);
	
	this.manualResult = new Element("div");
	this.manualResult.appendChild(document.createTextNode("Selecteer een categorie."));
	this.manualSelect.appendChild(this.manualResult);
	
	this.manualInfo = new Element("div");
	this.manualInfo.addClass("at_handleidingenzoeker_manuals_notfound");
	this.manualInfo.appendChild(document.createTextNode("Niet gevonden wat u zocht? Neem contact met ons op via het contactformulier hier rechts."));
	this.manualSelect.appendChild(this.manualInfo);
	
	this.wait = new Element("div");
	this.wait.id = "at_handleidingenzoeker_wait";
	this.wait.appendChild(document.createTextNode("Een ogenblik geduld aub..."));
	this.destinationElement.appendChild(this.wait);
	
	this.loadCategories();
}

var zoeker = null;

