function removeService(i) {
	$("." + i).remove();
}

function addService() {
	var a = $("table.form tbody tr:last-child td");
	var b = $("table.form tbody tr:last-child td input").attr("name");
	var c = $("table.form tbody tr:last-child").html();
	$("table.form tbody tr:last-child td a").remove();
	a.append('<a href="#" onClick = "removeService(\'tr'+b+'\'); return false;">удалить</a>')
	b = b*1 + 1;
	$("table.form tbody").append("<tr class='tr"+b+"'>" + c + "</tr>");
	$("table.form tbody tr:last-child td input").attr("name", b);
	//var a = $("table.form").children();
	//console.dir(a);
}
var serviceFormDialog;
var serviceFormDialogSuccess;
$(function() {
	serviceFormDialog = $( "#serviceForm" ).dialog({
		autoOpen: false,
		width: 640,
		modal: true
	});
	serviceFormDialogSuccess = $( "#serviceFormSuccess" ).dialog({
		autoOpen: false,
		width: 300,
		height: 100,
		modal: true
	});
	
	if ($("#scity").attr("value") == "0") {
		$("#absentCity").show();
	}
						
	$("#scity").change(function() {
		if (this.value == '0') {
			$("#absentCity").show();
		} else {
			$("#absentCity").hide();
		}
	});
	
	$('.kpReq').click(function() {
		serviceFormDialog.dialog( "open" );
		return false;
	})
});


function ServiceValidate() {
	var error = false;
	 
	var absentcity = "";
	
	$("#sunknownerror").hide();
	$("#captchaerror").hide();
	
	$("#ssuccess").hide();
	
	if ($("#scompany").attr("value") == "") {
		error = true;
	}
	
	if ($("#sperson").attr("value") == "") {
		error = true;
	}
	
	if ($("#sphone").attr("value") == "") {
		error = true;
	}
	
	if ($("#scity").attr("value") == "0") {
		if ($("#absentCity").attr("value") == "") {
			error = true;
		}
		city = $("#scity").attr("value");
		absentcity = $("#absentCity").attr("value");
	} else {
		city = $("#scity").attr("value");
	}
	
	if (error) {
		$("#serror").show();
	} else {
		$("#serror").hide();
		$.post("/blocks/sendServiceForm.json", { city: city, company: $("#scompany").attr("value"), person: $("#sperson").attr("value"), phone: $("#sphone").attr("value"), email: $("#semail").attr("value"), service: $("#sservice").attr("value"), comment: $("#scomment").attr("value"), absentCity: absentcity, recaptcha_challenge_field: $("#recaptcha_challenge_field").attr("value"), recaptcha_response_field: $("#recaptcha_response_field").attr("value")},
		 function(data) {
		   if (data["response"] == "success") {
		   	//$("#ssuccess").show();
		   	serviceFormDialog.dialog("close");
		   	serviceFormDialogSuccess.dialog('open')
		   	$("#demand input, #demand textarea").attr("value", "");
		   	$("#demand select").attr("value", "");
		   	$("#absentCity").hide();
		   } else if (data["response"] == "captcha")  {
			$("#captchaerror").show();
			Recaptcha.reload();
		   } else {   
		   	$("#sunknownerror").show();	
		   }
		 }, "json"
		);
	}
	return false;
}
