﻿
function addFormField()
    {
 	   var id = document.getElementById("id").value;
 	   $("#addPerson").append("<table id='remove" + id + "'><tr class='lichy' id='row" + id + "'><td rowspan='2' class='cislo'>"+ id +".</td><td><label for='first_name" + id + "'>Jméno</label><br /><input id='first_name" + id + "' class='required letterswithbasicpunc' type='text' size='20' name='first_name" + id + "' value=''></td>" +
 	   					"<td><label for='last_name" + id + "'>Příjmení</label><br /><input id='last_name" + id + "' class='required letterswithbasicpunc' type='text' size='20' name='last_name" + id + "' value=''></td>" +
 	   					"<td><label for='email" + id + "'>E-mail</label><br /><input id='email" + id + "' class='required email' type='text' size='20' name='email" + id + "' value=''></td>" +
 	   					"<tr class='sudy' id='row1" + id + "'><td><label for='phone" + id + "'>Telefon</label><br /><input id='phone" + id + "' class='required phonenumber' type='text' size='20' name='phone" + id + "' value=''><a href='#' onClick='removeFormField(\"#remove" + id + "\"); return false;'><img src='/css/images/delete.png'/></a></td>" +
 	   					"<td colspan='2'><label for='adress" + id + "'>Adresa</label><br /><input id='adress" + id + "' class='required' minlength='5' type='text' size='20' name='address" + id + "' value=''></td></tr></table>");
 	   
 	   
 		id = (id - 1) + 2;
 		document.getElementById("id").value = id;
    }
    
    function removeFormField(id) 
    {
 	   $(id).remove();
    }    
    
   function spocitej()
    {

        var soucet=0;


        $('#techform input[type="checkbox"]:checked').each
        (

            function()
            {
                soucet += parseInt(this.value);
            }
        );

        $('#techform input[type="radio"]:checked').each
        (

            function()
            {
                soucet += parseInt(this.value);
            }
        );

        $("div#vysledek").html(soucet);

        return(soucet);
       }


   
    function ajax()
    {
        var first_name=" ";
        var last_name=" ";
        var email=" ";
        var mail=" ";
        var podminka="Vyplňte správně formulář!";
        var hodnoty=new Array();
        var balicek=$('input[type="radio"]:checked').val();
        var koment=" ";
        var vystav=0;
        
        
        $.param($('input[type="text"]:filled').each(function(){hodnoty+=(this.value+"\n")}));

        koment=$("#comment").val();

        if($('input[type="checkbox"]:checked').length)
        {
          vystav=$('input[type="checkbox"]:checked').val();
        }
       

        if(!$("#techform").valid())
        {
            alert(podminka);
        }

        if($("#techform").valid())
        {
            if(spocitej() > 0)
           {
                //jQuery.post( url, [data], [callback], [type] )
                $.post("ajax.php", {
              							"hodnoty": hodnoty,
                						"balicek": balicek,
                                                                "koment": koment,
                                                                "vystav": vystav
                                   },
                function(data)
                {


                    $("div#vysledek1").html(
                    						data.mail
                                           );

                },

                "json");
                
                $(".meetingform").hide("blind", { direction: "vertical" }, 1000);
                
            }
            if(spocitej() == 0)
            {
                $("div#vysledek1").html(podminka);
            }
            
        }
        
    }

    
$(document).ready
(

    function()
    {
        spocitej();
        $('input[type="checkbox"]').click(spocitej);
        $('input[type="radio"]').click(spocitej);
        $('input[type="button"]').click(ajax);
        $(".addButton").click(function(){addFormField()});


        $("#techform").validate
        (
            {
            	success: "valid",

                
          	  errorPlacement: function(error, element) {
           	     error.appendTo( element.parent("td").children("label") );
           	   },
           	debug:true
            }              	 
        );
	$("input").bind('propertychange',function(e) { e.preventDefault(); }); 



    }



) ;