$(document).ready(function(){
	// profiel aanpassen
	$("#profiel_aanpassen").click(function(){
		$(".aanpas_input_td").each(function(){
			var width = $(this).css("width");
			var klass = String($(this).attr("class")).replace("_td", "");
			$(this).html("<input type='text' id='" + $(this).attr("id") + "' class='" + klass + "' value='" + $(this).html() + "' style='width:" + width + "' /><span style='display:none;' class='origineel_waarde'>" + $(this).html() + "</span>");
		});
		$(this).hide();
		$("#wijzigingen_opslaan").show();
		$("#wijzigingen_annuleren").show();
		$("#zelfde_als_factuur").show();
	});
	
	$("#wijzigingen_annuleren").click(function(){
		// originele waardes terugzetten
		$(".origineel_waarde").each(function(){
			$(this).parent().html($(this).html());
		});
		$(this).hide();
		$("#wijzigingen_opslaan").hide();
		$("#zelfde_als_factuur").hide();
		$("#profiel_aanpassen").show();
	});
	
	$("#wijzigingen_opslaan").click(function(){
		$(".aanpas_input").not(".afleveradres").each(function(){
			var deze = $(this);
			$.post("/functions/profiel_aanpassen.php", { welke: $(this).attr("id"), data: $(this).val() }, function(output){
				deze.parent().html(output);
			});
		});
		if ($("input[name='zelfde_als_factuur']").attr("checked") == false){
			// afwijkend van factuuradres
			$(".aanpas_input").each(function(){
				var deze = $(this);
				$.post("/functions/profiel_aanpassen.php", { welke: $(this).attr("id"), data: $(this).val() }, function(output){
					deze.parent().html(output);
				});
			});
		} else {
			$.post("/functions/profiel_aanpassen.php", { welke: "zelfde" }, function(){
				// zelfde als factuuradres
				$(".afleveradres").not(".aanpas_input_td").each(function(){
					// id van klantelement ophalen
					var klant_id = String($(this).attr("id")).replace("_aflever", "");
					$(this).parent().html($("#" + klant_id).html());
				});
			});
		}
		$(this).hide();
		$("#wijzigingen_annuleren").hide();
		$("#zelfde_als_factuur").hide();
		$("#profiel_aanpassen").show();
	});	
	
	$("input[name='zelfde_als_factuur']").click(function(){
		if ($(this).attr("checked") == false){
			$(".afleveradres").not(".aanpas_input_td").removeAttr("disabled");
		} else {
			$(".afleveradres").not(".aanpas_input_td").attr("disabled", true);			
		}
	});
	
});


