$.validator.addMethod("quantity", function(value, element) {
	return !this.optional(element) && !this.optional($(element).parent().prev().children("select")[0]);
}, "Wypełnij wszystkie pola!");

$().ready(function() {
	$("#orderform").validate({
		errorPlacement: function(error, element) {
			error.appendTo( element.parent().next() );
		},
		highlight: function(element, errorClass) {
			$(element).addClass(errorClass).parent().prev().children("select").addClass(errorClass);
		}
	});
	
	var template = jQuery.format($("#template").val());
	function addRow() {
		$(template(i++)).appendTo("#orderitems tbody");
		$("a.usun:not(:first)").css("display","inline");
		 $("#orderitems tbody").find("a").click(function(){
                                   
                                           $(this).parent().parent().remove();
                                           //$(this).parent().css({background:"red"});

                                     });
                        
	}
	
	var i = 1;
	// start with one row
	addRow();
	// add more rows on click
	$("#add").click(addRow);
	
	// check keyup on quantity inputs to update totals field
/*	$("#orderform").delegate("keyup", "input.quantity", function(event) {
		var totals = 0;
		$("#orderitems input.quantity").each(function() {
			totals += +this.value;
		});
		$("#totals").attr("value", totals).valid();
	});*/
	
});
