var i = 2, j = 2, k = 2;
var pageHtml;

function ValidateForm(formId) {
	var isValid = true;
	var focusFirst = true;
	$.each($(formId).find(".must"), function() {
		$(this).css("background", "");
		if($(this).attr("value") == "") {
			$(this).css("background", "#cf9f9f");
			isValid = false;
			if (focusFirst) {
				$(this).focus();
				focusFirst = false;
			}
		}
	});
	return isValid;
}

$(function() {
	$(".teaor-category").parent().children(".teaor-inner").addClass("hide");
	$(".teaor-subcategory").parent().children("ul").addClass("hide");
	if ($("#adatlap_ceg_fotevekenyseg option:first").attr("disabled")) {
		$("#adatlap_ceg_fotevekenyseg").val(
			$("#adatlap_ceg_fotevekenyseg_kategoria option:selected").val()+"1"
		);
	}

	pageHtml = $("body").html();

	$(".teaor-category").click(function() {
		$(this).parent().children(".teaor-inner").slideToggle("fast");
		$(this).parent().find(".teaor-subcategory").parent().children("ul").slideUp("fast");
		$(this).children(".teaor-category-icon").toggleClass("teaor-category-icon-open");
		$(this).parent().find(".teaor-subcategory").removeClass("teaor-subcategory-open")
	});
	
	$(".teaor-subcategory").click(function() {
		$(this).parent().children("ul").slideToggle("fast");
		$(this).toggleClass("teaor-subcategory-open");
	});

	$(".adatlap-add-alapito").click(function() {
		var htmlStr = $(this).parent().find(".adatlap-alapito").html();
		htmlStr = '<div class="adatlap-wrapper" style="display: none;">'+htmlStr+'</div>';
/*		var htmlTmp = $("#alapitok").html();*/
/*		htmlStr = htmlStr.replace(/((id|name|for)="[^"]*)/g, '$1A'+i+'"');*/
		htmlStr = htmlStr.replace(/((id|name|for)="{0,1}[a-z_]*)/g, '$1A'+i);
/*		$("#alapitok").html(htmlTmp+htmlStr);*/
		$("#alapitok").append(htmlStr);
		$("#alapitok").find("input").attr("value", "");
		$("#alapitok").find(".adatlap-num").last().text(i+".");
		i = i + 1;
		$("#alapitok").find(".adatlap-num").click(function() {
			if (confirm("Biztos, hogy törölni akarja?")) {
				var $rem = $(this).text().substr(0, $(this).text().length-1);
				$(this).parent().remove();
				i = i - 1;
				$.each($("#alapitok").find(".adatlap-num"), function() {
					if ($(this).text().substr(0, $(this).text().length-1) > $rem) {
						$(this).text($(this).text().substr(0, $(this).text().length-1)-1+".");
					}
				});
			}
		})
		$("#alapitok").find(".adatlap-wrapper").last().slideDown("fast");
	});

	$(".adatlap-add-vezeto").click(function() {
		var htmlStr = $(this).parent().find(".adatlap-vezeto").html();
		htmlStr = '<div class="adatlap-wrapper" style="display: none;">'+htmlStr+'</div>';
/*		var htmlTmp = $("#vezetok").html();*/
		htmlStr = htmlStr.replace(/((id|name|for)="{0,1}[a-z_]*)/g, '$1A'+j);
/*		$("#vezetok").html(htmlTmp+htmlStr);*/
		$("#vezetok").append(htmlStr);
		$("#vezetok").find("input").attr("value", "");
		$("#vezetok").find(".adatlap-num").last().text(j+".");
		j = j + 1;
		$("#vezetok").find(".adatlap-num").click(function() {
			if (confirm("Biztos, hogy törölni akarja?")) {
				var $rem = $(this).text().substr(0, $(this).text().length-1);
				$(this).parent().remove();
				j = j - 1;
				$.each($("#vezetok").find(".adatlap-num"), function() {
					if ($(this).text().substr(0, $(this).text().length-1) > $rem) {
						$(this).text($(this).text().substr(0, $(this).text().length-1)-1+".");
					}
				});
			}
		});
		$("#vezetok").find(".adatlap-wrapper").last().slideDown("fast");
	});
	
	$(".adatlap-add-tevekenyseg").click(function() {
		var htmlStr = $(this).parent().find(".adatlap-tevekenyseg").html();
		htmlStr = '<div class="adatlap-wrapper" style="display: none;"><span class="adatlap-num">+</span><div class="adatlap-inner">'+htmlStr+'</div><div class="clear"></div></div>';
		htmlStr = htmlStr.replace(/((id|name|for)="{0,1}[a-z_]*)/g, '$1A'+k);
		$("#tevekenysegek").append(htmlStr);		
		$("#tevekenysegek").find(".adatlap-num").last().text(k+".");
		$("#tevekenysegek").find(".adatlap-wrapper").last().slideDown("fast");

		$("#tevekenysegek").find(".adatlap-ceg-fotevekenyseg").bind('change keyup', function() {
			$(this).parent().find("select").last().html(
				$.ajax({
						url: "./index.php?p=teaor&r="+$(this).attr("value"),
						async: false
					}).responseText);
		});

		k = k + 1;
		$("#tevekenysegek").find(".adatlap-num").click(function() {
			var $rem = $(this).text().substr(0, $(this).text().length-1);
			$(this).parent().remove();
			k = k - 1;
			$.each($("#tevekenysegek").find(".adatlap-num"), function() {
				if ($(this).text().substr(0, $(this).text().length-1) > $rem) {
					$(this).text($(this).text().substr(0, $(this).text().length-1)-1+".");
				}
			});
		});
	});

	$("#adatlap_ceg_fotevekenyseg_kategoria").bind('change keyup', function() {
		$("#adatlap_ceg_fotevekenyseg").html(
			$.ajax({
					url: "./index.php?p=teaor&r="+$(this).attr("value"),
					async: false
				}).responseText);
		if ($("#adatlap_ceg_fotevekenyseg option:first").attr("disabled")) {
			$("#adatlap_ceg_fotevekenyseg").val(
				$("#adatlap_ceg_fotevekenyseg_kategoria option:selected").val()+"1"
			);
		}
	});
});

