$(document).ready(function() {

    if('#restaurant0'){
          $("#button").click(function () {
              rozm = $("input.restaurant").length;
              counter = rozm +1;
              min = rozm-1;
             // cont = '<div id="inputRestaurant'+rozm+'" class="restaurantClass"><label class="label" for="restaurant'+rozm+'">Nazwa</label><input id="restaurant'+rozm+'" class="restaurant" type="text" name="restaurant'+rozm+'" value=""/></div>';
              cont = '<div id="clear_field'+rozm+'" style="clear: both !important;"></div><div id="inputRestaurant'+rozm+'" class="restaurantClass"><label class="label" for="restaurant'+rozm+'">Nazwa</label><input id="restaurant'+rozm+'" class="restaurant" type="text" name="restaurant'+rozm+'" value=""/>'
                    + '<label class="label"  style="margin: 0 0 10px; float: left;" for="restaurant_max0">max os.:</label> '
                    + '<input id="restaurant_max'+rozm+'" class="restaurant_max" type="text" value="" name="restaurant_max'+rozm+'"/></div> ' ;

              //cont = '<div id="inputRestaurant'+rozm+'" class="restaurantClass"><label class="label" for="restaurant'+rozm+'">Nazwa</label><input id="restaurant'+rozm+'" class="restaurant" type="text" name="restaurant[]" value=""/></div>';
              if(rozm > 1){
                  $('#howmanyRest')[0].value = counter;
                  $('#inputRestaurant'+min).after(cont);
              }
              else{
                  //$(".restaurant").after(cont);
                  $('#howmanyRest')[0].value = counter;
                  $("#endfield").after(cont);
              }

        });

        $("#button2").click(function () {
              rozm2 = $("input.restaurant").length;
             var  counter2 = rozm2 +1;
              min2 = rozm2-1;
              if(rozm2 > 1){
              $('#inputRestaurant'+min2).remove();
              $('#clear_field'+min2).remove();
              }
              if((rozm2 - 1) > 0)
              $('#howmanyRest')[0].value = rozm2 - 1;
        });

     }
     if('#surround0'){
         $("#buttonSurr").click(function () {
              rozm = $("input.surround").length;
              counter = rozm +1;
              min = rozm-1;
              cont = '<div id="clear_fieldConf'+rozm+'" style="clear: both !important;"></div><div id="inputSurround'+rozm+'" class="surroundClass"><label class="label" for="surround'+rozm+'">Nazwa</label><input id="surround'+rozm+'" class="surround" type="text" name="surround'+rozm+'" value=""/>'
                      + '<label class="label" for="surround_max0">max os.:</label>'
                    + '<input id="surround_max'+rozm+'" class="surround_max" type="text" value="" name="surround_max'+rozm+'"/></div>';

              if(rozm > 1){
                   $('#howmanyConf')[0].value = counter;
                  $('#inputSurround'+min).after(cont);
              }
              else{
                  $('#howmanyConf')[0].value = counter;
                  $("#endfield2").after(cont);
              }

        });

        $("#buttonSurr2").click(function () {
              rozm2 = $("input.surround").length;
              counter2 = rozm2 +1;
              min2 = rozm2-1;
              if(rozm2>1){
                  $('#inputSurround'+min2).remove();
                  $('#clear_fieldConf'+min2).remove();
              }
              if((rozm2 - 1) > 0)
              $('#howmanyConf')[0].value = rozm2 - 1;

        });

     }

    if($('#companyId')[0]){
    if($('#companyId')[0].value != '') {
         $.ajax({
               type: "POST",
               url: "/company/dynamicForm",
               data: "id_company="+$('#companyId')[0].value,
               success: function(result){
                 myObject = eval('(' + result + ')');
                 tabl = myObject.restaurant;
                 tabConf = myObject.conference;
                 if(myObject.restaurant){

                 for(k=0; k < tabConf.length; k++){
                     rozmCont = k;
                     counterCont = rozmCont +1;
                      minCont = rozmCont-1;
                      content = '<div id="clear_field'+rozmCont+'" style="clear: both !important;"></div><div id="inputSurround'+rozmCont+'" class="surroundClass"><label class="label" for="surround'+rozmCont+'">Nazwa</label><input id="surround'+rozmCont+'" class="surround" type="text" name="surround'+rozmCont+'" value="'+myObject.conference[k].name+'"/>' +
                    '<label class="label"  style="margin: 0 0 10px; float: left;" for="surround_max'+rozmCont+'">max os.:</label> ' +
                    '<input id="surround_max'+rozmCont+'" class="surround_max" type="text" value="'+myObject.conference[k].max_persons+'" name="surround_max'+rozmCont+'"/></div> ';

                    if(rozmCont<1){
                          $('#surround0')[0].value = myObject.conference[k].name;
                          $('#surround_max0')[0].value = myObject.conference[k].max_persons;
                    }
                      else{

                          if(rozmCont > 1){
                          $('#inputSurround'+minCont).after(content);
                          $('#howmanyConf')[0].value = counterCont;
                          }
                          else{
                          $("#endfield2").after(content);
                          $('#howmanyConf')[0].value = counterCont;
                          }
                      }

                 }
                }
                if(myObject.restaurant){
                 for(k=0; k <tabl.length; k++){
                     rozmCont = k;
                     counterCont = rozmCont +1;
                      minCont = rozmCont-1;
                      content = '<div id="clear_field'+rozmCont+'" style="clear: both !important;"></div><div id="inputRestaurant'+rozmCont+'" class="restaurantClass"><label class="label" for="restaurant'+rozmCont+'">Nazwa</label><input id="restaurant'+rozmCont+'" class="restaurant" type="text" name="restaurant'+rozmCont+'" value="'+myObject.restaurant[k].name+'"/>' +
                    '<label class="label"  style="margin: 0 0 10px; float: left;" for="restaurant_max'+rozmCont+'">max os.:</label> ' +
                    '<input id="restaurant_max'+rozmCont+'" class="restaurant_max" type="text" value="'+myObject.restaurant[k].max_persons+'" name="restaurant_max'+rozmCont+'"/></div> ';

                    if(rozmCont<1){
                          $('#restaurant0')[0].value = myObject.restaurant[k].name;
                          $('#restaurant_max0')[0].value = myObject.restaurant[k].max_persons;
                    }
                      else{

                          if(rozmCont > 1){
                          $('#inputRestaurant'+minCont).after(content);
                          $('#howmanyRest')[0].value = counterCont;
                          }
                          else{
                          $("#endfield").after(content);
                          $('#howmanyRest')[0].value = counterCont;
                          }
                      }

                 }
               }
             }
         });
     }
     }


      $("div.optionsLocation div input").click(function() {
            if( $('div.optionsLocation div input:checkbox:checked').length >4){
                this.checked = false;
            }
     });

        // tylko 5 opcji przy rejestracji i edycji firmy przez usera
      $("#module-user-companyForm #register #business .options div input").click(function() {
            if( $('#module-user-companyForm #register #business .options div input:checkbox:checked').length >6){
                this.checked = false;
            }
     });

           $("#module-company-edit #edit #additionals .options div input").click(function() {
            if( $('#module-company-edit #edit #additionals .options div input:checkbox:checked').length >6){
                this.checked = false;
            }
     });


});

function validate(){

if($('fieldset#business div.optionsLocation div input:checkbox')){
    if( !($('fieldset#business')[0].style.display == 'none')){
        if($('fieldset#business div.optionsLocation div input')){
           if( $('fieldset#business div.optionsLocation div input:checkbox:checked').length == 0){
                    alert('Proszę zaznaczyć przynajmniej jeden rodziaj lokalizacji');
                    return false;
                }
        }
        else
          return true;
      }
}
      return true;
}

function validateAdminAdd(){
    if($('div.optionsLocation div input:checkbox')){
            if($('div.optionsLocation div input').length != 0){
               if( $('div.optionsLocation div input:checkbox:checked').length == 0){
                        alert('Proszę zaznaczyć przynajmniej jeden rodziaj lokalizacji');
                        return false;
                    }
            }
            else
              return true;
    }
    return true
}
