﻿function InitializeDialogs() {
	$("#controle").dialog({
		autoOpen: false,
		closeOnEscape: true,
		draggable: false,
		height: 360,
		modal: true,
		resizable: false,
		width: 480
	}); //***** $(dialogname).dialog

	$("#tellafriend").dialog({
		autoOpen: false,
		closeOnEscape: true,
		draggable: false,
		height: 620,
		modal: true,
		resizable: false,
		width: 760
	}); //***** $(dialogname).dialog
	
	$("#overonderlevering").dialog({
		autoOpen: false,
		closeOnEscape: true,
		draggable: false,
		height: 560,
		modal: true,
		resizable: false,
		width: 665
	}); //***** $(dialogname).dialog		
};

function TellAFriend() {
	$("#tellafriend").dialog("option", "buttons", {
		"Sluiten": function() {
			$("#tellafriend").dialog("close");
		}
	});

	if ($("#taf").attr("src") != "Tell-a-friend.aspx")
		$("#taf").attr("src", "Tell-a-friend.aspx");
	
	$("#tellafriend").dialog("open");
}

function OverOnderlevering() {
	$("#overonderlevering").dialog("option", "buttons", {
		"OK": function() {
			$("#overonderlevering").dialog("close");
		}
	});

	if ($("#ool").attr("src") != "OverOnderlevering.aspx")
		$("#ool").attr("src", "OverOnderlevering.aspx");

	$("#overonderlevering").dialog("open");
}

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.";
		$("#problemen").html(result);

		$("#controle").dialog("option", "title", "Informatie");
		$("#controle").dialog("option", "width", 450);
		$("#controle").dialog("option", "height", 330);

		$("#controle").dialog("option", "open", function() { 
        $(this).parents('.ui-dialog-buttonpane button:eq(0)').focus();
    });
		
		$("#controle").dialog("option", "buttons", {
			"OK": function() {
				$("#controle").dialog("close");
			}
		});

		$("#controle").dialog("open");

		return false;
	}

	//*****
	//***** PEOS:20110121:N.a.v. CH(Mail):20101215 uitgeschakeld.
	/*if (l < b) {
		$("#problemen").html("Om te kunnen zoeken dient de lengte groter te zijn dan de breedte.<br /><br />Wilt u de lengte en breedte omdraaien?");

		$("#controle").dialog("option", "title", "Vraag");
		$("#controle").dialog("option", "width", 510);
		$("#controle").dialog("option", "height", 225);

		$("#controle").dialog("option", "open", function() { 
        $(this).parents('.ui-dialog-buttonpane button:eq(1)').focus();
    });

		$("#controle").dialog("option", "buttons", {
			"Nee": function() {
				$("#controle").dialog("close");
			},
			"Ja": function() {
				$("#Lengte").val(b);
				$("#Breedte").val(l);
				$("#controle").dialog("close");
				$("#Zoeken").click();
			}
		});

		$("#controle").dialog("open");

		return false;
	}*/

	//*****
	return true;
}

function CopyToClipboard(text) {
	if (window.clipboardData && clipboardData.setData) {
		clipboardData.setData("Text", text);
	}
}

function ISOHelper() {
	$("#isohelper").dialog("option", "buttons", {
		"OK": function() {
			$("#isohelper").dialog("close");
		}
	});

	$("#isohelper").dialog("open");
}

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 r = $("#Kleur option:selected").val();

	var url = "dozen-" + l;
	url += "x" + b;
	url += "x" + h;
	url += "-" + QualityToString(d);
	url += "-" + r;
	url += "-" + a + "-stuks";
	url += ".html";
	url = url.replace(" ", "-");
	url = url.toLowerCase();

	window.location = 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());
	//***** 20110628:Wit;
	var r = $("#Kleur 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);
		//***** 20110628:Wit;
		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 WindowOpen(url) {
	window.open(url, '', 'width=880,height=740,scrollbars=no,toolbar=no,location=no');
}

/*function WindowOpen(url, width, height) {
	window.open(url, '', 'width=' + width + ',height=' + height + ',scrollbars=no,toolbar=no,location=no');
}*/

/*function WindowOpenScrollbar(url) {
	window.open(url, '', 'width=880,height=740,scrollbars=yes,toolbar=no,location=no');
}*/

function WindowOpenScrollbar(url, width, height) {
	window.open(url, '', 'width=' + width + ',height=' + height + ',scrollbars=yes,toolbar=no,location=no,resizable=1');
}

$(document).ready(function () {
	//*****
	InitializeDialogs();

	//***** Alleen initialiseren indien gewenst.
	if (typeof window.InitializeCustomDialogs == 'function') {
		InitializeCustomDialogs();
	}

	//***** 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");
	});

	$("#Afmeting").bind("blur", function () {
		$(this).removeClass("highlight");
	});

	$("#Afmeting").bind("focus", function () {
		$(this).removeClass("highlight");
		$(this).addClass("highlight");
	});

	$("#Dikte").bind("blur", function () {
		//***** 20110628:4 mm => altijd bruin;
		if ($("#Dikte option:selected").val() == 4) {
			$("#Kleur").val("bruin");
			$("#Kleur").attr("disabled", "disabled");
		}
		else
			$("#Kleur").attr("disabled", "");

		//*****
		UpdateBox();
		$(this).removeClass("highlight");
	});

	$("#Dikte").bind("focus", function () {
		$(this).removeClass("highlight");
		$(this).addClass("highlight");
	});

	$("#Kleur").bind("blur", function () {
		//***** 20110628:wit => nooit 4 mm
		if ($("#Kleur option:selected").val() == "wit") {
			$("#Dikte option[value='4']").remove();
		} else {
			if ($("#Dikte option[value='4']").val() == undefined)
				$('<option value="4">' + QualityToString("4") + '</option>').insertAfter("#Dikte option[value='3']");
		}
		UpdateBox();
		$(this).removeClass("highlight");
	});

	$("#Kleur").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");

	//***** 20110628:4 mm => altijd bruin;
	if ($("#Dikte option:selected").val() == 4) {
		$("#Kleur").val("bruin");
		$("#Kleur").attr("disabled", "disabled");
	}
	else
		$("#Kleur").attr("disabled", "");

	//***** 20110628:wit => nooit 4 mm
	if ($("#Kleur option:selected").val() == "wit") {
		$("#Dikte option[value='4']").remove();
	} else {
		if ($("#Dikte option[value='4']").val() == undefined)
			$('<option value="4">' + QualityToString("4") + '</option>').insertAfter("#Dikte option[value='3']");
	}
});

function PrintBookmarkLink() {
	//***** FF
	if (window.sidebar) {
		document.write('<a href="javascript:CreateBookmarkLink()");">Bladwijzer maken voor deze pagina</a>');
	//***** IE
	} else if (document.all) {
		document.write('<a href="javascript:CreateBookmarkLink()");">Toevoegen aan favorieten</a>');
	}
}

function CreateBookmarkLink() {
	//***** FF
	if (window.sidebar) {
		window.sidebar.addPanel(document.title, location.href, "");
		//***** IE
	} else if (document.all) {
		window.external.AddFavorite(location.href, document.title);
	}
}

