jQuery(document).on("click", ".remove-item", function(e) {
	id = $(this).data("id");
	$("#"+id).remove();
})

jQuery(document).on("click", "#add-btn", function(e){
	e.preventDefault();
	if($("#new-optionvalue").validateInput()) {
		var id = "option"+Math.round(Math.random() * 10000)+Math.round(Math.random() * 10000)+Math.round(Math.random() * 10000);
		$("#"+id).remove();
		var tr = $("<tr id=\""+id+"\"></tr>");
		var optionvalue = $("#new-optionvalue").val();
		$(tr).addElement({
			element: "td",
			content: "<input name=\"correct_option\" id=\"c-"+id+"\" value=\""+id+"\" type=\"radio\" required>"
		}).addElement({
			element: "td",
			content: optionvalue+"<input name=\"option[optionvalue]["+id+"]\" value=\""+optionvalue+"\" style=\"display: none;\"><input name=\"option[optionid][]\" value=\""+id+"\" style=\"display: none;\">"
		}).addElement({
			element: "td",
			content: "<a href=\"javaScript:void(0)\" data-id=\""+id+"\" class=\"remove-item\">Remove</a>"
		});
		$("#items-table tbody").append(tr);
		$("#new-optionvalue").val("");
		$("#add-btn").attr("disabled", true);
	}
})

jQuery(document).on("change, keyup", "#new-optionvalue, #new-optionhandle", function(e) {
	var optionvalue = $("#new-optionvalue").val();
	if(optionvalue !== "") {
		$("#add-btn").attr("disabled", false);
	} else {
		$("#add-btn").attr("disabled", true);
	}
})