﻿$().ready( function() {
	var seans =  new Date();
	$("#seans").val(seans.getTime());
	var href="";
	var text="";
	$("div#tabs span").click( function() { 
		var prev = $("div#tabs span.active").attr("id");
		$("table#tabl_"+prev).fadeOut(200);
		$("div#tabs span.active").removeClass("active");
		var now = $(this).attr("id");
		$("table#tabl_"+now).fadeIn(200);
		$(this).addClass("active");
	});
	
	$("a.anketa").click( function() {
		if ($("#seans").val()!="") {
			var table = "<table border=1>";
			$("table tr").each( function() {
				var td = $(this).find("td")
				if (td.length==1) {
					table += "<tr><td colspan=2><b>"+td.text()+"</b></td></tr>"
				} 
				if (td.length==2) {
					var naim = $(this).find("td:first").text();
					naim = naim.replace(/"/g,'&quot;'),
					input = $(this).find("input");
					var znach = $(this).find("td:last").text();
					if (input.length==1) {
						var znach = $(this).find("input").val();
					} else {
						if (input.length==2) {
							if (input.eq(0).attr('type')=='checkbox') {
								if ($(this).find("input:checked").length==2) {
									var znach = $(this).find("input:checked:first").val()+", "+$(this).find("input:checked:last").val();
								} else {
									var znach = $(this).find("input:checked:first").val();
								}
							} else {
								naim = $(this).find("input:first").val();
								var znach = $(this).find("input:last").val()
							}
						} else {
							if ($(this).find("select option:selected").length!=0) {
								var znach = $(this).find("select option:selected").html();
							}
						}
					}
					table += "<tr><td>"+naim+"</td><td>"+znach+"</td></tr>"
				}
			});
			table += "</table>"
			json = { 'txt': table }
			$.post('anketa.php', json, function(txt){
				/*if ($("#download").attr("checked")) {
					document.location.href = txt;
				}*/
				//href = txt;
				$("#seans").val("");
				//$("a.anketa").text("Download")
				//$("a.anketa").attr("href",href);
				alert(txt);
				text = txt;
			}, 'text');
			return false;		
		} else {
			alert(text)
			return false;	
		}		
	});
	
	$("input.other").click( function() {
		if ($(this).attr("checked")) {
			$(this).parents("tr").nextAll("tr.other").fadeIn(200)
		} else {
			$(this).parents("tr").nextAll("tr.other").fadeOut(200)
		}
	});
	
	$("input.shower").click( function() {
		var sel = "tr"+$(this).attr("child");
		if ($(this).attr("checked")) {
			$(this).parents("tr").nextAll(sel).fadeIn(200)
		} else {
			$(this).parents("tr").nextAll(sel).fadeOut(200)
		}
	});
	
	$("input.other").each( function() {
		if ($(this).attr("checked")) {
			$(this).parents("tr").nextAll("tr.other").fadeIn(200)
		} else {
			$(this).parents("tr").nextAll("tr.other").fadeOut(200)
		}
	});
});

