﻿function ValidateBox() {
	//*****
	var result = "";

	//*****
	var l = parseInt($("#Lengte").val(), 10);
	if (isNaN(l))
		result += "&#0149; '" + $("#Lengte").val() + "' is geen geldige lengte.<br/>";
	else if (l < 0)
		result += "&#0149; Een negatieve of nul lengte is geen geldige lengte.<br/>";
	
	//*****
	var b = parseInt($("#Breedte").val(), 10);
	if (isNaN(b))
		result += "&#0149; '" + $("#Breedte").val() + "' is geen geldige breedte.<br/>";
	else if (b < 0)
		result += "&#0149; Een negatieve of nul breedte is geen geldige breedte.<br/>";

	//*****
	var h = parseInt($("#Hoogte").val(), 10);
	if (isNaN(h))
		result += "&#0149; '" + $("#Hoogte").val() + "' is geen geldige hoogte.<br/>";
	else if (h < 0)
		result += "&#0149; Een negatieve of nul hoogte is geen geldige hoogte.<br/>";
		
	//*****
	var a = parseInt($("#Aantal").val(), 10);
	if (isNaN(a))
		result += "&#0149; '" + $("#Aantal").val() + "' is geen geldig aantal.";
	else if (h < 0)
		result += "&#0149; Een negatief of nul aantal is geen geldige aantal.<br/>";
		
	//*****
	if (result != "") {
		result = "Niet alle zoekvelden zijn correct ingevuld:<br/><br/>" + result;
		result += "<br/>Corrigeer de invoer en probeer het opnieuw.";
		alert(result);
	}

	//*****
	if (l < b) {
		result = "Om te kunnen zoeken dient de lengte groter te zijn dan de breedte. Corrigeer uw invoer a.u.b.";
		alert(result);
	}

	//*****
	return true;
}

function QualityToString(quality) {
	switch("" + quality) {
		case "3":
			return "Enkelgolf 3mm";
		case "4":
			return "Enkelgolf 4mm";
		case "7":
			return "Dubbelgolf 7mm";
		default:
			return "-";
	}
}

function SearchBox() {
	//*****
	var l = parseInt($("#Lengte").val(), 10);
	var b = parseInt($("#Breedte").val(), 10);
	var h = parseInt($("#Hoogte").val(), 10);
	var d = $("#Dikte option:selected").val();
	var a = parseInt($("#Aantal").val(), 10);
	var da = $("#Afmeting option:selected").val();
	var ref = $("#HiddenRef").val();
	
	var url = "http://www.dozen.nu/";
	url += "dozen-" + l;
	url += "x" + b;
	url += "x" + h;
	url += "-" + QualityToString(d);
	url += "-" + a + "-stuks";
	url += ".html";
	if (ref != null && ref != "")
		url += "?ref=" + ref;
		
	url = url.replace(" ", "-");
	url = url.toLowerCase();

	window.open(url);
}

function DrawBox(size, r) {
	var l = parseInt($("#Lengte").val(), 10);
	var b = parseInt($("#Breedte").val(), 10);
	var h = parseInt($("#Hoogte").val(), 10);
	var d = parseInt($("#Dikte option:selected").val());
	if (!isNaN(l) && !isNaN(b) && !isNaN(h) && !isNaN(d)) {
		var url = "dozen-" + l;
		url += "x" + b;
		url += "x" + h;
		url += "-" + QualityToString(d);
		url += "-" + ("" + r == "" ? "bruin" : r);
		url += (size == "" ? "" : "-") + size;
		url += ".png";
		url = url.replace(" ", "-");
		url = url.toLowerCase();
		
		//***** Only update if different
		if (document.getElementById("Afbeelding").src.lastIndexOf(url) < 0)
			document.getElementById("Afbeelding").src = url;
	}
}

function UpdateBox() {
	DrawBox("klein", "");
};

$(document).ready(function() {
	//***** Events
	$("#Lengte").bind("blur", function() {
		UpdateBox();
		$(this).removeClass("highlight");
	});

	$("#Lengte").bind("focus", function() {
		$(this).removeClass("highlight");
		$(this).addClass("highlight");
	});

	$("#Breedte").bind("blur", function() {
		UpdateBox();
		$(this).removeClass("highlight");
	});

	$("#Breedte").bind("focus", function() {
		$(this).removeClass("highlight");
		$(this).addClass("highlight");
	});

	$("#Hoogte").bind("blur", function() {
		UpdateBox();
		$(this).removeClass("highlight");
	});

	$("#Hoogte").bind("focus", function() {
		$(this).removeClass("highlight");
		$(this).addClass("highlight");
	});

	$("#Dikte").bind("blur", function() {
		UpdateBox();
		$(this).removeClass("highlight");
	});

	$("#Dikte").bind("focus", function() {
		$(this).removeClass("highlight");
		$(this).addClass("highlight");
	});

	$("#Aantal").bind("blur", function() {
		$(this).removeClass("highlight");
	});

	$("#Aantal").bind("focus", function() {
		$(this).removeClass("highlight");
		$(this).addClass("highlight");
	});

	$("#Zoeken").bind("click", function() {
		if (ValidateBox())
			SearchBox();
		return false;
	});

	//*****
	$("#Lengte").focus();
	$("#Lengte").select();
	$(this).removeClass("highlight");
	$(this).addClass("highlight");
});

