/*
	INPUT
		.integer
		.numeriek 			- punten worden aangepast naar komma
		.puntjes 			- punten tussen de duizenden
		.kenteken 			- zet streepjes in het keneteken
		.postcode
		.kenteken
		.datum 				(datepicker)
		.datum2 			(losse velden doortabben)
		.no-spaces
	
	TEXTAREA
		.altt				- als je de class altt toevoegt op een textarea en je drukt alt-t in verschijnt de huidige datum / tijd in de textarea.
		
	TABLE
		.vkg-table
	
	SUBMIT / A
		.loading
		{.confirm rel="Weet u dit zeker?"}
	
	ADRES OPZOEKEN DMV AJAX
		.adres-check op postcode + op huisnummer input
		.adres-check-resultaat met daarin .adres-check-resultaat-straat en .adres-check-resultaat-plaats
		(voor meerdere op 1 pagina: gebruik rel="{uniek}" op beide .adres-check's en op de .adres-check-resultaat)
	
	POPUP
		includen:
		<link rel="stylesheet" type="text/css" href="/functies/jqueryfuncties.css" />
		<script language="JavaScript" src="/functies/jquery-ui-1.8.7.min.js" type="text/javascript"></script><!--alleen nodig voor draggable-->
		
		aanroep:
		<div class="popupWrapper [center] [width80%] [maxwidth400] [height80%] [maxheight300] [draggable] [hover] [overlay] [wit] [nomargin]">
			<div class="popupButton">tekst</div>
		    <div class="popupContent">
		        <a class="popupClose"></a>
		        <div class="popupTitle">Titel</div>
		        <div class="popupText">
					Content bla bla <br />bla
					<!--<iframe src="http://www.google.nl"></iframe>-->
		        </div>
		    </div>
		</div>
*/

$(window).unload(function(){
	$("#loadingContent").hide();
});

var popupInfoHTML = "<div class=\"popupWrapper hover draggable\">" +
					"<div class=\"popupButton\">%0</div>" +
					"<div class=\"popupContent\">" +
						"<a class=\"popupClose\"></a>" +
						"<div class=\"popupTitle\">%1</div>" +
						"<div class=\"popupText\">" +
							"%2" +
						"</div>" +
					"</div>" +
				"</div>"

var popupMessageHTML = "<div class=\"popupWrapper overlay center wit\">" +
					"<div class=\"popupContent\">" +
						"<a class=\"popupClose\"></a>" +
						"<div class=\"popupTitle\">%1</div>" +
						"<div class=\"popupText\">" +
							"%2" +
						"</div>" +
					"</div>" +
				"</div>"

$(document).ready(function(){
	$("div.popupVKG").each(function(){
		var imghtm = "<img src=\"/images/icons/info.png\" border=\"0\"/>";
		if ($(this).hasClass("icoonmenu")){
			imghtm = "<div class=\"icon menuitem\"></div>";
		}

		$(this).html(popupInfoHTML.replace("%0", imghtm).replace("%1", $(this).attr("title")).replace("%2", $(this).html()).replace("<div class=\"popupTitle\">undefined</div>", ""));
		$(this).removeAttr("title");
	});
	
	$("input.numeriek, input.integer").keydown(function(e){
		//console.log(e.which);
		switch (e.which) {
			case 35: case 36: case 37: case 38: case 39: case 40: case 8: case 35: case 36: case 9: case 96: case 49: case 50:  case 51:  case 52:  case 53:  case 54:  case 55:  case 56:  case 57:  case 0: case 46: case 45: case 116: case 97: case 98: case 99: case 100: case 101: case 102: case 103: case 104: case 105: case 48: case 16:
				//left,right,tab,shift,etc.
				break;
			case 188: case 110: case 190: //komma, punt
				if ($(this).hasClass("integer")) {return false;}
				break;
			default:
				return false;
		}
	});

	$(".no-spaces").keydown(function(e){
		switch (e.which) {
			case 32:
				return false;
				break;
		}
	});

	$("input.numeriek").keyup(function(){
		$(this).val($(this).val().replace(/[.]/g,","));
	});
	
	$("input.puntjes").focus(function(){
		$(this).val($(this).val().replace(/[.]/g,""));
	});
	
	$("input.puntjes").blur(function(){
		if ($(this).val().length > 0){
			var decimals = false;
			if ($(this).hasClass("numeriek")) {decimals = true;}
			$(this).val(formatNumber(parseFloat($(this).val().replace(/[,]/g,".")), decimals));
		}
	});
	
	$("input.puntjes").each(function(){
		if ($(this).val().length > 0){
			var decimals = false;
			if ($(this).hasClass("numeriek")) {decimals = true;}
			if ($(this).hasClass("integer")) {$(this).val($(this).val().replace(/[.]/g,""));}
			$(this).val(formatNumber(parseFloat($(this).val().replace(/[,]/g,".")), decimals));
		}
	});
	
	$("input.adres-check").keyup(function(){haalAdresOp(true,$(this));});
	$("input.adres-check").change(function(){haalAdresOp(true,$(this));});
	
	$("input.adres-check").each(function(){
		haalAdresOp(false,$(this));
	});
	
	$("input.kenteken").keyup(function(){
		if ($(this).val().length > 5){
			var item = $(this);
			$.get("/ajax/kentekenstreepjeszetten.asp", {kenteken: $(this).val()}, function(data){
				item.val(data);
			});
		}
	});

	$("input.postcode").keyup(function(e){
		if (e.shiftKey) {return false;} // ivm shift tab
		if (valpostcode !== $(this).val()){
			valpostcode = $(this).val();
			var postcode = $(this).val();
			if (postcode.length == 6 || postcode.length == 7) {
				var reg = /^([0-9]{4})([ ])?([a-z]{2})?$/i;
				if (reg.exec(postcode)){
					var inputs = $(this).closest("form").find(":input[type=text]");
					inputs.eq( inputs.index(this)+ 1 ).focus();
				}
			}
		}
	});
	var valpostcode = $("input.postcode").val();
	$("input.postcode").css("text-transform", "uppercase");
	
	$("input.datum2").keyup(function(e){
		switch (e.which) {
			case 35: case 36: case 37: case 38: case 39: case 40: case 8: case 35: case 36: case 9: case 0: case 46: case 45: case 116: case 16:
				return false;
				break;
			case 96: case 97: case 98: case 99: case 100: case 101: case 102: case 103: case 104: case 105:
			case 48: case 49: case 50: case 51: case 52: case 53: case 54: case 55: case 56: case 57:
				var lengte = 2;
				if ($(this).attr("name").toLowerCase().indexOf("jaar") > -1) {lengte = 4;}
				if ($(this).val().length == lengte) {
					var inputs = $(this).closest("form").find(":input[type=text]");
					inputs.eq( inputs.index(this)+ 1 ).focus().select();
				}
				break;
		}
	});

	$("a.loading, input[type=submit].loading").each(function(){
		if ($("#loadingContent").length == 0){
			$("body").append("<div id=\"loadingContent\"><img src=\"/images/loading2.gif\" /></div><div id=\"loadingBackground\"></div>");
			centerPopup($("#loadingContent"));
		}
	});
	
	$("a.loading, input[type=submit].loading").click(function(){
		if ($(this).hasClass("confirm")){
			var confirmtext = $(this).attr("rel");
			if (confirmtext == "") { confirmtext = "Weet u het zeker?"; }
			if (!confirm(confirmtext)) {
				return false;
			}
		}
		$("#loadingBackground").css("opacity", "0.5").fadeIn(100);
		$("#loadingContent").show();
	});
	
	initialiseerPopups();

	$("div.popupWrapper:not(.hover) div.popupButton").live("click", function(){
		if ($(this).parent(".popupWrapper").find(">.popupStatus").val() == "0"){
			showpopup($(this).parent(".popupWrapper"));
		}
		else{
			disablePopup($(this).parents("div.popupWrapper"));
		}
	});
	
	$("div.popupWrapper.hover div.popupButton").live("mouseenter", function(){
		if ($(this).parent(".popupWrapper").find(">.popupStatus").val() == "0"){
			showpopup($(this).parent(".popupWrapper"));
		}
	});
	
	$("div.popupWrapper.hover div.popupButton, div.popupWrapper.hover div.popupContent").live("mouseleave", function(event){
		if ($(this).parent(".popupWrapper").find(">.popupVersleept").val() == "0") {
			divwrapper = $(this).parent(".popupWrapper");
			setTimeout("checkHover(divwrapper)", 100);
		}
	});
	
	$("div.popupBackground").live("click", function(){disablePopup($(this).parent(".popupWrapper"));});
	$("a.popupClose").live("click", function(){disablePopup($(this).closest(".popupWrapper"));return false;});
	
	$(document).keypress(function(e){
		if(e.keyCode == 27){
			disablePopup($(".popupWrapper"));
		}
	});
	
	$("input.puntjes").parents("form").submit(function(){ //puntjes niet submitten. waarde overnemen naar hidden veld.
		$(this).find(".puntjes").each(function(){
			$(this).parents("form").append("<input type=\"hidden\" name=\"" + $(this).attr("name") + "\" value=\"" + $(this).val().replace(/[.]/g,"") + "\" />")
			$(this).attr("name", "");
		});
	});
	
	$("div.popupButton a").attr("onclick", "return false;");
	$("div.popupWrapper iframe").attr("scrolling", "auto").attr("width", "100%").attr("height", "99%").attr("frameborder", "0").attr("marginwidth", "0").attr("marginheight", "0")
	
	$("textarea.altt").keydown(function(e){
		var elm = $(this);
		if (e.altKey) {
			if (e.which == 84) {// T
				$.get("/ajax/getdatumtijd.asp?tijd=" + new Date().getTime(), function(data){
					elm.val(elm.val() + data + "\n");
					elm.focus();
				});
				return false;
			}
		}
	});

	// Stel de datum picker in
	if ($("input.datum").length > 0){
		$.datepicker.setDefaults({ showOn: "both",
			buttonImage: "/functies/UIDdatepicker/css/ui-lightness/images/calendarIcon.jpg",
			buttonImageOnly: true,
			changeMonth: true,
			changeYear: true,
			yearRange: 'c-100:c+100',
			dateFormat: "dd-mm-yy",
			buttonText: 'Kies een datum'
		}); 
		
		$("input.datum").datepicker($.datepicker.regional['nl']);
	}
	
	$("a:not([href]), a[href='']").attr("href", "#");
	$("form ol li a.submit").click(function(){
		$(this).closest("form").submit();
		return false;
	});


	/* Stuk code om in het standaard formulier een stukje informatie te krijgen bij een input  */	
	$("ol.formlist input, ol.formlist select").focus(function(){
		$("span.uitleg:visible").fadeOut();			
		$(this).parent().parent().find("span.uitleg").fadeIn();
	});

	$("ol.formlist input, ol.formlist select").blur(function(){			
		$(this).parent().parent().find("span.uitleg").fadeOut();
	});

	$("label.radio-label").hover(function(){
		$("span.uitleg:visible").fadeOut();
		$(this).parent().parent().find("span.uitleg").fadeIn();
	}, function() {$("span.uitleg:visible").fadeOut();});
	/* ------------------------------------------------------------------------------------ */


	if ($("table.sortable").length > 0){
		$("table.sortable th.unsortable").click(function(){
			return false;
		});
		
		$.tablesorter.addParser({ 
			id: "floatNL", 
			is: function(s) { 
				return /^[0-9]?[0-9,\.]*$/.test(s); 
			}, 
			format: function(s) { 
				return $.tablesorter.formatFloat(s.replace(/\./g, '')); 
			}, 
			type: "numeric" 
		});

		$.tablesorter.addParser({
			id: 'datumNL',
			is: function(s) {
		        return false;
	    	},
			format: function(s, table, cell) {
				var dateSplit = s.split('-');
				return new Date(dateSplit[2], dateSplit[1], dateSplit[0]);
			},
	        type: 'numeric'
	    });

	    $.tablesorter.addParser({ 
			id: "percentNL", 
			is: function(s) { 
				return /^[0-9]?[0-9,\.]*$/.test(s); 
			}, 
			format: function(s) { 
				return $.tablesorter.formatFloat(s.replace(/\,/g, '.')); 
			}, 
			type: "numeric" 
		});
		
		$("table.sortable").tablesorter();
		//$("table.vkg-table").liveFilter();

		$("table.sortable").bind("sortStart",function() {}).bind("sortEnd", function(){
			$("table.vkg-table.sortable tbody tr").removeClass("odd").removeClass("even");
			$("table.vkg-table.sortable tbody tr:visible:odd").addClass("odd");
			$("table.vkg-table.sortable tbody tr:visible:even").addClass("even");
		});
		
		$("table.pager").each(function(){
			if ($(this).attr("rel") == undefined){$(this).attr("rel", "")};
			var aantal = 10;
			var divhtml= $("<div class=\"pager\">" +
								"<a class=\"prev\" onclick=\"return false\" href=\"#\">vorige " + aantal + "</a>" + 
								"<a class=\"next\" onclick=\"return false\" href=\"#\">volgende " + aantal + "</a>" +
								"<input type=\"hidden\" class=\"pagesize\" value=\"" + aantal + "\">" +
							"</div>").attr("rel", $(this).attr("rel") !== undefined ? $(this).attr("rel") : "");
			$(this).after(divhtml).tablesorterPager({container: $("div.pager"), size: aantal});
		});

				
		//================================ MUTEERBAAR ====================

		$("table.vkg-table.muteerbaar tr.invoer").hide();
		
		$("table.vkg-table.muteerbaar").each(function(){
			$(this).find("thead tr").append("<th class=\"unsortable\" width=\"50\">&nbsp;</th>")
			if ($(this).find("tbody tr").attr("data-id") == undefined){
				$(this).find("tbody tr").append("<td>&nbsp;</td>");
			}
			else{
				$(this).find("tbody tr").append("<td><a href=\"#\" onclick=\"return false;\" class=\"icon wijzigen\" alt=\"Wijzigen\"></a><a href=\"#\" onclick=\"return false;\" class=\"icon verwijderen\" alt=\"Verwijderen\"></a><a href=\"#\" onclick=\"return false;\" class=\"icon opslaan\" alt=\"Opslaan\"></a><a href=\"#\" onclick=\"return false;\" class=\"icon annuleren\" alt=\"Annuleren\"></a></td>");
				$(this).find("tbody tr").find("a.icon.opslaan, a.icon.annuleren").hide();
			}
			
			var foothtml = "<tfoot><tr>";
			$(this).append();
			for (i = 0; i < $(this).find("thead tr th").length - 1; i++) {
				foothtml += "<td>&nbsp;</td>";
			}
			foothtml += "</tr></tfoot>";
			$(this).append(foothtml);

			$(this).find("tfoot tr").append("<td><a href=\"#\" onclick=\"return false;\" class=\"icon toevoegen\" alt=\"Toevoegen\"></a><a href=\"#\" onclick=\"return false;\" class=\"icon opslaan\" alt=\"Opslaan\"></a><a href=\"#\" onclick=\"return false;\" class=\"icon annuleren\" alt=\"Annuleren\"></a></td>");
			$(this).find("tfoot tr").find("a.icon.opslaan, a.icon.annuleren").hide();
		});
		
		$("table.vkg-table.muteerbaar a.icon.wijzigen").live("click", function(){
			$(this).closest("tr").find("td:not(:last)").each(function(){
				var elmtd = $(this);
				formhtml = $(this).closest("table").find("tr.invoer td").eq($(this).index()).html();
				$(this).append(formhtml).find("input, select").each(function(){
					var divelm = elmtd.find("div[rel=" + $(this).attr("name") + "]");
					if (divelm.find("a").length > 0) {divelm = divelm.find("a")}
					var newval = $.trim(divelm.html());
					if ($(this).is("select")){
						$(this).find("option:contains('" + newval + "')").each(function(){
							if ($(this).html() == newval){newval = $(this).attr("value");}
						});
					}
					$(this).val(newval);
				});
				$(this).find("div").hide();
			});
			$(this).closest("table").find("a.icon").hide();
			$(this).closest("tr").find("a.icon.opslaan, a.icon.annuleren").show();
		});
		
		$("table.vkg-table.muteerbaar a.icon.annuleren").live("click", function(){
			$(this).closest("tr").find("input:not([type=hidden]), select").remove();
			$(this).closest("tr").find("div").show();
			$(this).closest("table tbody").find("a.icon.opslaan, a.icon.annuleren").hide();
			$(this).closest("table tbody").find("a.icon.wijzigen, a.icon.verwijderen").show();
		});
		
		$("table.vkg-table.muteerbaar a.icon.opslaan").live("click", function(){
			var elmtr = $(this).closest("tr");
			var objVariables = {"id": $(this).closest("tr").attr("data-id")};
			$(this).closest("tr").find("input, select").each(function(){
				eval("objVariables." + $(this).attr("name") + " = '" + $(this).val().replace("'", "\'") + "'");
			});
			$.post($(this).closest("tr").attr("data-ajaxupdate"), objVariables, function(){
				
				elmtr.find("td:not(:last)").each(function(){
					elmtd = $(this);
					$(this).find("input, select").each(function(){
						if ($(this).attr("name") !== undefined){
							if ($(this).is("select")){
								newval = $(this).find("option:selected").html();
							}
							else {
								newval = $(this).val();
							}
							elmtd.find("div[rel=" + $(this).attr("name") + "]").html(newval);
						}
					});
				});

				elmtr.find("input:not([type=hidden]), select").remove();
				elmtr.find("div").show();
				elmtr.closest("table tbody").find("a.icon.opslaan, a.icon.annuleren").hide();
				elmtr.closest("table tbody").find("a.icon.wijzigen, a.icon.verwijderen").show();
			});
		});
		
		$("table.vkg-table.muteerbaar a.icon.verwijderen").live("click", function(){
			if (confirm("Weet u zeker dat u deze wilt verwijderen?")){
				var objVariables = {"id": $(this).closest("tr").attr("data-id")};
				$(this).closest("tr").find("input[type=hidden]").each(function(){
					eval("objVariables." + $(this).attr("name") + " = '" + $(this).val().replace("'", "\'") + "'");
				});

				$.post($(this).closest("tr").attr("data-ajaxupdate"), objVariables, function(){
					$(this).closest("tr").remove();
				});
			}
		});
		
		//================================================================
	}

	// Uitklappen van een span met de class limit-lineheight
	var spanheightlimit = 0;
	$("table.vkg-table span.limit-lineheight").click(function(){
		if (spanheightlimit == 0) { spanheightlimit = $(this).css("line-height"); }
		if ($(this).hasClass("uitklappen")){
			$(this).css("height", "auto");
			$(this).removeClass("uitklappen");
			$(this).addClass("inklappen");
		}
		else{
			$(this).css("height", spanheightlimit);
			$(this).removeClass("inklappen");
			$(this).addClass("uitklappen");
		}
	});

	// Toolbox showen en hiden bij mouseover
	$("table.vkg-table tr").hover(function(){
		// Mouse in
		$(this).find("div.inline-toolbox").show();
	}, 
	function(){
		// Mouse out
		$(this).find("div.inline-toolbox").hide();
	});

	$("table.vkg-table tbody tr:visible:odd").addClass("odd");
	$("table.vkg-table tbody tr:visible:even").addClass("even");

	/* Standaard a.print een print actie geven. */
	$("a.print").click(function(){
		$("div.toolbox").hide();		
		window.print();
		$("div.toolbox").show();
		return false;
	});
	
	//VKG tabbladen tabs
	if ($("div.vkg-tabs div.active").length == 0){
		$("div.vkg-tabs div:first-child").addClass("active");
	}

	$("div.vkg-tabs div").each(function(){
		$("div#" + $(this).attr("rel")).hide();
		if ($(this).hasClass("active")){$("div#" + $(this).attr("rel")).show();}
	});
	
	$("div.vkg-tabs div").click(function(){
		$(this).closest("div.vkg-tabs").find("div").each(function(){
			$(this).removeClass("active");
			$("div#" + $(this).attr("rel")).hide();
		});
		$(this).addClass("active");
		$("div#" + $(this).attr("rel")).show();
	});
});

function initialiseerPopups(){
	$("div.popupButton").after("<div style=\"clear:both;height:0px;\">&nbsp;</div>");
	$("div.popupWrapper:not(:has('div.popupBackground'))").append("<input type=\"hidden\" class=\"popupStatus\" value=\"0\" /><input type=\"hidden\" class=\"popupVersleept\" value=\"0\" /><div class=\"popupBackground\"></div>");
	
	$("div.popupWrapper").each(function(){
		if ($(this).attr("class").indexOf("width", 0) == -1) {
			$(this).addClass("width400");//default waarde
		}
	});
}

function showloading(){
	$("#loadingBackground").css("opacity", "0.5").fadeIn(100);
	$("#loadingContent").show();
}

function hideloading(){
	$("#loadingBackground").css("opacity", "0.5").fadeOut(100);
	$("#loadingContent").hide();
}

function haalAdresOp(animate,elm){
	var attr = elm.attr("rel");
	if (typeof attr !== 'undefined' && attr !== false) {
		var inputpostcode = $("input[rel=" + elm.attr("rel") + "]:first");
		var inputhuisnummer = $("input[rel=" + elm.attr("rel") + "]:last");
		var outputdiv = $(".adres-check-resultaat[rel=" + elm.attr("rel") + "]");
	}
	else{
		var inputpostcode = $("input.adres-check:first");
		var inputhuisnummer = $("input.adres-check:last");
		var outputdiv = $(".adres-check-resultaat");
	}

	if (inputpostcode.length > 0 && inputhuisnummer.length > 0) {
		if (inputpostcode.val().length > 5 && inputhuisnummer.val().length > 0) {
			$.post("/standaardfuncties/jsonpostcode.asp", {postcode: inputpostcode.val(), huisnummer: inputhuisnummer.val()}, function(data){
				var obj = $.parseJSON(data);
				if (obj.Gelukt) {
					outputdiv.find(".adres-check-resultaat-straat").html(obj.Straatnaam);
					outputdiv.find(".adres-check-resultaat-plaats").html(obj.Plaatsnaam);
					if (animate){
						outputdiv.slideDown();
					}
					else{
						outputdiv.show();
					}
				}
				else{
					outputdiv.find(".adres-check-resultaat-straat").html("");
					outputdiv.find(".adres-check-resultaat-plaats").html("");
					if (animate){
						outputdiv.slideUp();
					}
					else{
						outputdiv.hide();
					}
				}
			});
		}
		else{
			outputdiv.hide();
		}
	}
	else{
		outputdiv.hide();
	}
}

function checkHover(popupdiv){
	if (popupdiv.find(">.popupButton").css("z-index") != "99" && popupdiv.find(">.popupContent").css("z-index") != "99"){
		disablePopup(popupdiv);
	}
}

function disablePopup(popupdiv){
	popupdiv.find(">.popupBackground").fadeOut("slow");
	popupdiv.find(">.popupContent").fadeOut("slow", function(){
		$(this).css("top", "auto").css("left", "auto");
	});
	popupdiv.find(">.popupStatus").val("0");
	popupdiv.find(">.popupVersleept").val("0");
}

function centerPopup(contentdiv){
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = contentdiv.height();
	var popupWidth = contentdiv.width();
	contentdiv.css({
		"position": "fixed",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
}

function showpopup(popupdiv){
	//loadPopup(popupdiv);
	//if (!popupdiv.hasClass("hover")){
		$.each($(".popupStatus[value=1]"), function(){
			if (!$(this).parents(".popupWrapper").hasClass("overlay")){
				disablePopup($(this).parents(".popupWrapper"));
			}
		});
	//}
	
	if (popupdiv.hasClass("draggable")) {
		popupdiv.find(">.popupContent").draggable({
  			scroll: false,
			cancel: ".popupText,.popupClose",
			stop: function() {//niet buiten het scherm eindigen
			    if(popupdiv.find(">.popupContent").css("top").substr(0,1) == "-"){popupdiv.find(">.popupContent").css("top", "0");}
			    if(popupdiv.find(">.popupContent").css("left").substr(0,1) == "-"){popupdiv.find(">.popupContent").css("left", "0");}
				popupdiv.find(">.popupVersleept").val("1");
			}
		});
		popupdiv.find(">.popupContent").find(">.popupTitle").css("cursor", "move");
	}
	
	$.each(popupdiv.attr("class").split(/\s+/), function(count, classname){
		if (classname.indexOf("maxwidth", 0) > -1) {
			var maxwidth = parseInt(classname.substr(8, classname.length - 8));
			if (parseInt(popupdiv.find(">.popupContent").width() - 4) > maxwidth ) {
				popupdiv.find(">.popupContent").css("width", parseInt(maxwidth - 4) + "px");
			}
		}
		else if (classname.indexOf("width", 0) > -1) {
			if (classname.substr(classname.length - 1, 1) == "%") {
				popupdiv.find(">.popupContent").css("width", classname.substr(5, classname.length - 5));
			}
			else {
				popupdiv.find(">.popupContent").css("width", parseInt(classname.substr(5, classname.length - 5) - 4) + "px");
			}
		}
		
		if (classname.indexOf("maxheight", 0) > -1) {
			var maxheight = classname.substr(9, classname.length);
			if (parseInt(popupdiv.find(">.popupContent").height() - 4) > maxheight ) {
				popupdiv.find(">.popupContent").css("height", parseInt(maxheight - 4) + "px");
				popupdiv.find(">.popupContent").find(">.popupText").css("height", parseInt(maxheight - 36) + "px").css("overflow","auto");
			}
		}
		else if (classname.indexOf("height", 0) > -1) {
			if (classname.substr(classname.length - 1, 1) == "%") {
				var height = parseInt(classname.substr(6, classname.length));
				var screenheight = $(window).height();
				var heightpixel = Math.round(screenheight * height / 100);
				
				popupdiv.find(">.popupContent").css("height", parseInt(heightpixel - 4) + "px");
				popupdiv.find(">.popupContent").find(">.popupText").css("height", parseInt(heightpixel - 36) + "px").css("overflow","auto");
			}
			else {
				var height = classname.substr(6, classname.length);
				popupdiv.find(">.popupContent").css("height", parseInt(height - 4) + "px");
				popupdiv.find(">.popupContent").find(">.popupText").css("height", parseInt(height - 36) + "px").css("overflow","auto");
			}
		}
	});
	
	popupdiv.find(">.popupContent").width(popupdiv.find(">.popupContent").width() + "px"); //IE fix voor de header background. vraag me niet waarom maar het werkt.
	
	if (popupdiv.hasClass("center")) {
		centerPopup(popupdiv.find(">.popupContent"));
	}
	else {
		var windowWidth = document.documentElement.clientWidth;
		var windowHeight = document.documentElement.clientHeight;
		
		//popup valt rechts buiten de pagina
		if (parseInt(popupdiv.offset().left + popupdiv.find(">.popupContent").width() + 4) > parseInt(windowWidth)) {
			//popupdiv.find(">.popupContent").offset({left:0});
			//popupdiv.find(">.popupContent").offset(popupdiv.find(">.popupContent").offset());
			popupdiv.find(">.popupContent").css("left", parseInt(windowWidth - popupdiv.find(">.popupContent").width() - 4) + "px");
		}
		
		//popup valt aan onderkant buiten de pagina
//console.log(popupdiv.offset().top);
//console.log(popupdiv.find(">.popupContent").height());
//console.log(windowHeight);
//		if (parseInt(popupdiv.offset().top + popupdiv.find(">.popupContent").height() + 4) > parseInt(windowHeight)) {
//console.log(popupdiv.find(">.popupContent").offset());
//			popupdiv.find(">.popupContent").offset({top: parseInt(windowHeight - popupdiv.find(">.popupContent").height() - popupdiv.offset().top)})
//console.log(popupdiv.find(">.popupContent").offset());
//		}
	}
	
	if(popupdiv.find(">.popupStatus").val() == "0"){
		if (popupdiv.hasClass("overlay")){
			popupdiv.find(">.popupBackground").css({
				"opacity": "0.3"
			});
			popupdiv.find(">.popupBackground").fadeIn("fast");
		}
		popupdiv.find(">.popupContent").fadeIn("fast");
		popupdiv.find(">.popupStatus").val("1");
	}
	//divwrapper = popupdiv;
	//setTimeout("checkHover(divwrapper)", 500);
}

function ValidatiefoutenTonen(sText){
	if (sText){
		var elms = sText.split(";");
		for (i = 0; i < elms.length; i++) {
			if ($("#" + elms[i]).length > 0)
			{
				$("#" + elms[i]).closest("li").addClass("error");
			} else
			{
				$("input[name="+elms[i]+"],select[name="+elms[i]+"],textarea[name="+elms[i]+"]").closest("li").addClass("error");
			}
		}
	}
}
		
function trim(value){
	value = value.replace(/^\s+/,''); 
	value = value.replace(/\s+$/,'');
	return value;
}

function IsNumeric(sText){
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
}

function formatNumber (theNumber, decimals){
	var theDecimalDigits = Math.round((theNumber*100)-(Math.floor(theNumber)*100));
	theDecimalDigits= ""+ (theDecimalDigits + "0").substring(0,2);
	theNumber = ""+Math.floor(theNumber);
	var theOutput = "";
	for (x=0; x<theNumber.length; x++) {
		theOutput += theNumber.substring(x,x+1);
		if (isThousands(theNumber.length-x-1) && (theNumber.length-x-1 !=0)) {
			theOutput += ".";
		};
	};
	
	if (decimals) {theOutput += "," + theDecimalDigits;}
	return theOutput;
};

function isThousands(position) {
	if (Math.floor(position / 3) * 3 == position) return true;
	return false;
};

function CheckRelatie(relnr){
	$.get("/ajax/CheckRelatie.asp", {relnr: relnr}, function(data){
		if (data == "1"){
			$("input[type=submit], a.loading").attr("disabled", true).removeAttr("onclick").removeAttr("href").removeClass("loading");
			$("div#loadingContent").remove();
			$("div#loadingBackground").remove();
			
			$("body").append(popupMessageHTML.replace("%1", "Aanvraag niet mogelijk").replace("%2", "Er zijn nog openstaande posten op deze relatie. Deze dienen eerst te worden voldaan voordat er een wijziging/aanvraag kan worden gedaan.") + "<div id=\"popupBackground\"></div>");
			
			 initialiseerPopups();
			showpopup($(".popupWrapper:last"));
		}
	});
}

function l(val){
	if (window.console){console.log(val)}
}
