summaryrefslogblamecommitdiffstats
path: root/admin/survey/modules/mod_MAZA/js/MAZA.js
blob: a229c26ce3d0d649a8fdc7920e88fbc68a00ba4b (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731


























































































































































































































































































































































































































































































































































































































































































































































                                                                                                                                                                                                             
/**
 * modul MAZA - mobilna aplikacija za anketirance
 * Uroš Podkrižnik 17.10.2017
 */

//default number of meters of radius for geofences
var maza_default_radius = 50;
//days relative to today to start date of repeater
var maza_start_day_repeater = -1;
//days relative to today to end date of repeater
var maza_end_day_repeater = 1;
//is geofencing on
var geofencing_on = false;

function maza_on_off() {
    $.post('ajax.php?t=MAZA&a=maza_on_off', {on_off: $('#advanced_module_maza').is(':checked'), anketa: srv_meta_anketa_id});
}

function doMAZAInactiveUsersExportCSV() {
    $.post('ajax.php?t=MAZA&a=InactiveUsersExportCSV', {anketa: srv_meta_anketa_id});
}

function onAlarmsFormsLoad() {
    var maza_submit_repeater = $('#maza_submit_repeater');
    var maza_submit_alarms = $('#maza_submit_alarms');
    //array of range from - to for input everywhichday in form of alarms or repeater
    var maza_everywhichday_range = [2, 6];

    //ALARM
    $('input[name="maza_title"]').bind('input', function () {
        maza_toggle_submit_alarms();
    });
    $('input[name="maza_message"]').bind('input', function () {
        maza_toggle_submit_alarms();
    });
    $('input[name="maza_alarm_intervalby"]').bind('change', function () {
        $('#maza_alarm_div_weekly').toggle(($(this).val() === 'weekly'));
        $('#maza_alarm_div_daily').toggle(($(this).val() === 'daily'));
        maza_toggle_submit_alarms();
        $.post('ajax.php?t=MAZA&a=changeRepeatBy', {anketa: srv_meta_anketa_id, maza_repeatby: $(this).val(), maza_table: "alarms"});
    });
    $('input[name="maza_alarm_timeinday[]"]').bind('change', function () {
        var times = [];
        $('input:checkbox[name="maza_alarm_timeinday[]"]:checked').each(function () {
            times.push($(this).val());
        });
        maza_toggle_submit_alarms();
        $.post('ajax.php?t=MAZA&a=changeTimeInDay', {anketa: srv_meta_anketa_id, maza_time_in_day: times, maza_table: "alarms"});
    });
    $('input[name="maza_alarm_dayinweek[]"]').bind('change', function () {
        var times = [];
        $('input:checkbox[name="maza_alarm_dayinweek[]"]:checked').each(function () {
            times.push($(this).val());
        });
        maza_toggle_submit_alarms();
        $.post('ajax.php?t=MAZA&a=changeDayInWeek', {anketa: srv_meta_anketa_id, maza_day_in_week: times, maza_table: "alarms"});
    });
    $('input[name="maza_alarm_everywhichday"]').bind('input', function () {
        if ($(this).val() >= maza_everywhichday_range[0] && $(this).val() <= maza_everywhichday_range[1])
            $.post('ajax.php?t=MAZA&a=changeEveryWhichDay', {anketa: srv_meta_anketa_id, maza_every_which_day: $(this).val(), maza_table: "alarms"});
        maza_toggle_submit_alarms();
    });

    //REPEATER
    $('input[name="maza_repeater_intervalby"]').bind('change', function () {
        $('#maza_repeater_div_weekly').toggle(($(this).val() === 'weekly'));
        $('#maza_repeater_div_daily').toggle(($(this).val() === 'daily'));
        //$.post('ajax.php?t=MAZA&a=changeRepeatBy', {anketa: srv_meta_anketa_id, maza_repeatby: $(this).val(), maza_table: "repeaters"});
        maza_toggle_submit_repeater();
    });
    $('input[name="maza_repeater_timeinday[]"]').bind('change', function () {
        /*var times = [];
        $('input:checkbox[name="maza_repeater_timeinday[]"]:checked').each(function () {
            times.push($(this).val());
        });
        if(times)
            $('#maza_repeater_div_everyday').css("border", "none");*/
        maza_toggle_submit_repeater();
        //$.post('ajax.php?t=MAZA&a=changeTimeInDay', {anketa: srv_meta_anketa_id, maza_time_in_day: times, maza_table: "repeaters"});
    });
    $('input[name="maza_repeater_dayinweek[]"]').bind('change', function () {
        /*var times = [];
        $('input:checkbox[name="maza_repeater_dayinweek[]"]:checked').each(function () {
            times.push($(this).val());
        });*/
        /*if(times)
            $('#maza_repeater_div_weekly').css("border", "none");*/
        maza_toggle_submit_repeater();
        //$.post('ajax.php?t=MAZA&a=changeDayInWeek', {anketa: srv_meta_anketa_id, maza_day_in_week: times, maza_table: "repeaters"});
    });
    $('input[name="maza_repeater_everywhichday"]').bind('input', function () {
        /*if ($(this).val() > 1 && $(this).val() < 7){
            //$('#maza_repeater_div_everywhichday').css("border", "none");
            //$.post('ajax.php?t=MAZA&a=changeEveryWhichDay', {anketa: srv_meta_anketa_id, maza_every_which_day: $(this).val(), maza_table: "repeaters"});
        }*/
        maza_toggle_submit_repeater();
    });
    $('input[name="maza_repeater_date_start"]').bind('change', function () {
        var pickedDateSplit = this.value.split(".");
        var pickedDate = new Date(pickedDateSplit[2], pickedDateSplit[1]-1, pickedDateSplit[0]);
        var pickedDateFormated = $.datepicker.formatDate('dd.mm.yy', pickedDate);
        this.value = pickedDateFormated;

        var pickedDateTimestamp = pickedDate.getTime();
        var nowDate = new Date();
        var todayDateTimestamp = (new Date(nowDate.getFullYear(), nowDate.getMonth(), nowDate.getDate())).getTime();

        //if date is not valid, clear it
        if(!pickedDateTimestamp || pickedDateTimestamp < todayDateTimestamp+(maza_start_day_repeater*86400000)){
            this.value = "";
            $("#maza_repeater_start_date_warning").hide();
        }
        maza_toggle_submit_repeater();
    });
    $('input[name="maza_repeater_date_end"]').bind('change', function () {
        var pickedDateSplit = this.value.split(".");
        var pickedDate = new Date(pickedDateSplit[2], pickedDateSplit[1]-1, pickedDateSplit[0]);
        var pickedDateFormated = $.datepicker.formatDate('dd.mm.yy', pickedDate);
        this.value = pickedDateFormated;

        var pickedDateTimestamp = pickedDate.getTime();
        var nowDate = new Date();
        var todayDateTimestamp = (new Date(nowDate.getFullYear(), nowDate.getMonth(), nowDate.getDate())).getTime();

        //if date is not valid, clear it
        if(!pickedDateTimestamp || pickedDateTimestamp < todayDateTimestamp+(maza_end_day_repeater*86400000)){
            this.value = "";
        }
        maza_toggle_submit_repeater();
    });

    /**
     * Toggle submition button for alarms form
     */
    function maza_toggle_submit_alarms() {
        if (!$('input[name="maza_message"]').val().length > 0)
            maza_submit_alarms.hide();
        else {
            if ($('input:checkbox[name="maza_alarm_timeinday[]"]:checked').length === 0)
                maza_submit_alarms.hide();
            else if ($('input[name="maza_alarm_intervalby"]:checked').val() === "everyday")
                maza_submit_alarms.show();
            else if ($('input[name="maza_alarm_intervalby"]:checked').val() === "daily") {
                var everywhichday = $('input[name="maza_alarm_everywhichday"]').val();
                if (everywhichday >= maza_everywhichday_range[0] && everywhichday <= maza_everywhichday_range[1])
                    maza_submit_alarms.show();
                else
                    maza_submit_alarms.hide();
            } else if ($('input[name="maza_alarm_intervalby"]:checked').val() === "weekly") {
                if ($('input:checkbox[name="maza_alarm_dayinweek[]"]:checked').length === 0)
                    maza_submit_alarms.hide();
                else
                    maza_submit_alarms.show();
            }
        }
    }

    /**
     * Toggle submition button for repeater form
     */
    /*function maza_check_submit_repeater() {
        if($('#maza_repeater_date_start').val().length > 0){
            if ($('input:checkbox[name="maza_repeater_timeinday"]:checked').length === 0){
                $('#maza_repeater_div_everyday').css("border", "red solid 1px");
                return false;
            }
            else if ($('input[name="maza_repeater_intervalby"]:checked').val() === "everyday")
                return true;
            else if ($('input[name="maza_repeater_intervalby"]:checked').val() === "daily") {
                var everywhichday = $('input[name="maza_repeater_everywhichday"]').val();
                if (everywhichday >= maza_everywhichday_range[0] && everywhichday <= maza_everywhichday_range[1])
                    return true;
                else{
                    $('#maza_repeater_div_daily').css("border", "red solid 1px");
                    return false;
                }
            } else if ($('input[name="maza_repeater_intervalby"]:checked').val() === "weekly") {
                if ($('input:checkbox[name="maza_repeater_dayinweek"]:checked').length === 0){
                    $('#maza_repeater_div_weekly').css("border", "red solid 1px");
                    return false;
                }
                else
                    return true;
            }
        }
    }*/
    
     /**
     * Toggle submition button for repeater form
     */
    function maza_toggle_submit_repeater() {
        var startVal = $('#maza_repeater_date_start').val();
        var endVal = $('#maza_repeater_date_end').val();
        var datesValid = true;

        if(startVal && endVal){
            var startDateSplit = startVal.split(".");
            var startDate = new Date(startDateSplit[2], startDateSplit[1]-1, startDateSplit[0]);
            var endDateSplit = endVal.split(".");
            var endDate = new Date(endDateSplit[2], endDateSplit[1]-1, endDateSplit[0]);
            datesValid = startVal < endVal;
        }
        
        if(!startVal || (startVal && !datesValid))
            maza_submit_repeater.hide();
        else if ($('input:checkbox[name="maza_repeater_timeinday[]"]:checked').length === 0)
            maza_submit_repeater.hide();
        else if ($('input[name="maza_repeater_intervalby"]:checked').val() === "everyday")
            maza_submit_repeater.show();
        else if ($('input[name="maza_repeater_intervalby"]:checked').val() === "daily") {
            var everywhichday = $('input[name="maza_repeater_everywhichday"]').val();
            if (everywhichday >= maza_everywhichday_range[0] && everywhichday <= maza_everywhichday_range[1])
                maza_submit_repeater.show();
            else
                maza_submit_repeater.hide();
        } else if ($('input[name="maza_repeater_intervalby"]:checked').val() === "weekly") {
            if ($('input:checkbox[name="maza_repeater_dayinweek[]"]:checked').length === 0)
                maza_submit_repeater.hide();
            else
                maza_submit_repeater.show();
        }
    }

    maza_toggle_submit_alarms();
    maza_toggle_submit_repeater();
    mazaChooseDate(maza_start_day_repeater, $("#maza_repeater_date_start"));
    mazaChooseDate(maza_end_day_repeater, $("#maza_repeater_date_end"));
}

/**
 * Sets datepicke on start of repeater input
 * @param {type} maza_start_day_repeater - int of days to start calendar relative to today
 * @param {type} element - element to set calendar on
 * @returns {undefined}
 */
function mazaChooseDate(maza_start_day_repeater, element) {
    element.datepicker({
        //selectOtherMonths: true,
        //changeMonth: true,
        minDate: maza_start_day_repeater,
        dateFormat: "dd.mm.yy",
        showAnim: "slideDown",
        //showOn: "button",
        //buttonImage: srv_site_url + "admin/survey/script/calendar/calendar.gif",
        //buttonImageOnly: true,
        onSelect: function (dateText, inst) {
            element.trigger('change');
            return false;
        }
    });
}

/**
 * When geofencing form is loaded
 * @param {type} ank_id - ID of survey
 * @param {type} geo_on - is geofencing on
 * @returns {undefined}
 */
function onGeofencingFormsLoad(ank_id, geo_on){
    geofencing_on = geo_on;
    //preveri, ce je google API ze includan (ce se je vedno icludal, je prislo do errorjev)
    if((typeof google === 'object' && typeof google.maps === 'object')){
        mazaMaps(ank_id);
    }
    else{
        //main/app/contollers/js/Maps/Declaration.js
        mapsAPIseNi (function(){mazaMaps(ank_id);});
    }
    
    //disable/enable notification setting based on checkbox
    $('#maza_geofence_trigger_survey').bind('change', function () {
        if(this.checked){
            $("#maza_title").prop('disabled', true);
            $("#maza_message").prop('disabled', true);
        }
        else{
            $("#maza_title").prop('disabled', false);
            $("#maza_message").prop('disabled', false);
        }
    });
}

/**
 * Toggle AR interval on AR checkbox changed
 * @param {type} checkbox - checkbox element
 * @returns {undefined}
 */
function toggleARInterval(checkbox){
        if (checkbox.checked)
                $('#maza_ar_interval_div').show();
            else
                $('#maza_ar_interval_div').hide();
}

//nastavi mapo
function mazaMaps(ank_id){
    //mapType = tip zemljevida, ki bo prikazan. Recimo za satelitsko sliko google.maps.MapTypeId.SATELLITE (možno še .ROADMAP)
    var mapType = google.maps.MapTypeId.ROADMAP;
    //centerInMap = string naslova, kaj bo zajel zemljevid. Rec. Slovenija / ali Ljubljana
    //var centerInMap = '';

    //pridobi parametre za centriranje mape in jo nastavi za kasnejso uporabo
    /*var centerInMapKoordinate = <?php echo json_encode($fokus_koordinate)?>;
    if(centerInMapKoordinate)
        centerInMapKoordinate = JSON.parse(centerInMapKoordinate);*/

    //Deklaracija potrebnih stvari za delovanje in upravljanje google maps JS API
    var mapOptions = {
            disableDoubleClickZoom: true,
            mapTypeId: mapType
    };

    //ce je v bazi naslov enak vpisanemu v nastavitvah, nastavi po parametrih
    /*if(centerInMapKoordinate.fokus === centerInMap || centerInMap === ''){
        mapOptions.center = {lat:  parseFloat(centerInMapKoordinate.center.lat), 
            lng:  parseFloat(centerInMapKoordinate.center.lng)};
        mapOptions.zoom = parseInt(centerInMapKoordinate.zoom);
    }   */
    //ce ni parametrov v bazi ali pa je nanovo kreirana spremenljivka, nastavi na Slovenijo
    //else if(!centerInMapKoordinate && centerInMap === '<?php echo $default_centerInMap; ?>'){
        mapOptions.center = {lat: 46.151241, lng: 14.995463};
        mapOptions.zoom = 7;
    //}

    //deklaracija zemljevida
    var mapdiv = document.getElementById("maza_map_geofencing");
    var map = new google.maps.Map(mapdiv, mapOptions);
    //to se kasneje uporabi za pridobitev mape z id-em spremenljivke
    mapdiv.gMap = map;
    //deklaracija mej/okvira prikaza na zemljevidu
    bounds['maza_map_geofencing'] = new google.maps.LatLngBounds();

    //deklaracija geocoderja (API)
    if(!geocoder)
        geocoder = new google.maps.Geocoder();  
    if(!infowindow)
        infowindow = new google.maps.InfoWindow();
    
    allMarkers['maza_map_geofencing'] = [];
    
    if(!geofencing_on){
        searchBox('maza_map_geofencing', function doAfterPlaceFromSearchBox(pos, address){
            //reset bounds, so we can focus only on this geofence
            bounds['maza_map_geofencing'] = new google.maps.LatLngBounds();
            //save geofence
            maza_saveGeofence(address, maza_create_basic_circle(pos, address, maza_default_radius, true));
        });

        //set click event on map
        maza_klikNaMapo();
    }
        
    //ni ok, ce se klice globalna spremenljivka srv_meta_anketa_id, ker se ne vedno pravi cas nastavi
    $.post('ajax.php?t=MAZA&a=get_all_geofences', {anketa: ank_id},
                function (data) {
                    if(data.length > 0)
                        //id - id markerja v bazi
                        map_data_fill_vnaprej_geofences(JSON.parse(data));
                });
}

/**
 * Create and fill existing geofences in map
 * @param map_data JSON Object
 */
function map_data_fill_vnaprej_geofences(map_data) {
    for (var row in map_data) {
        var row_object = map_data[row];
        var pos = {lat: row_object.lat, lng: row_object.lng};

        maza_createCircleVnaprej(row_object.address, maza_create_basic_circle(pos, row_object.address, row_object.radius, false), row_object.id, row_object.name);
    }
}


/**
 * Ustvari in vrne basic circle, na mapo spremenljivke z danimi koordinatami
 * @param {type} pos - koordinate - objekt {lat: ???, lng: ???}
 * @param {string} addressFromSearchBox - address if adding marker from searchBox, null otherwise
 *                  (used for ignoring duplicates)
 * @returns {google.maps.Circle}
 */
function maza_create_basic_circle(pos, address, radius, fromSearchBox) {
    var circle = null;
	
	if(fromSearchBox === undefined) {
		fromSearchBox = false;
	}
	
    //pridobi mapo spremenljivke
    var map = document.getElementById("maza_map_geofencing").gMap;

    if(fromSearchBox) 
        circle = address ? findMarkerFromAddress(address, "maza_map_geofencing") : null;

    if (!circle) {
        //kreiraj marker
        /*var marker = new google.maps.Marker({
            position: new google.maps.LatLng(pos.lat, pos.lng),
            map: map,
            identifier: getMarkerUniqueId(pos.lat, pos.lng, "maza_map_geofencing")//,
            //icon: {url: "img_0/marker_text_off.png"}
        });*/

        // Add circle overlay and bind to marker
        circle = new google.maps.Circle({
          center: new google.maps.LatLng(pos.lat, pos.lng),
          map: map,
          radius: parseFloat(radius), //in meters
          editable: !geofencing_on,
          fillColor: 'red'
        });
        //circle.bindTo('center', marker, 'position');
        
        //add marker in array
        allMarkers["maza_map_geofencing"].push(circle);

        return circle;
    } else {
        //trigger click on marker, to focus it and open infowindow
        //google.maps.event.trigger(circle, 'click');

        //reset bounds
        bounds['maza_map_geofencing'] = new google.maps.LatLngBounds();
        //add to bounds 
        bounds["maza_map_geofencing"].union(circle.getBounds());
        //zemljevid se prilagodi okviru
        map.fitBounds(bounds["maza_map_geofencing"]);
        return null
    }
}

/**
 * funkcija, ki kreira osnovni geofence (rdec)
 * @param {type} address - String - label o informacijah markerja
 * @param {type} marker - google maps new created circle
 * @returns {undefined}
 */
function maza_saveGeofence(address, circle) {
    if (circle != null) {
        //save new marker in DB
        $.post('ajax.php?t=MAZA&a=insert_geofence', {address: address,
            lat: circle.getCenter().lat(), lng: circle.getCenter().lng(), radius: maza_default_radius, anketa: srv_meta_anketa_id},
                function (id) {
                    //id - id markerja v bazi
                    maza_createCircleVnaprej(address, circle, id, null);
                });
    }
}

/**
 * Set what to do when click event on map
 * @returns {undefined}
 */
function maza_klikNaMapo() {
    //pridobi mapo spremenljivke
    var map = document.getElementById("maza_map_geofencing").gMap;
    
    // ko user klikne na mapo, funkcija vrne pozicijo (koordinate - position) ter kreira
    google.maps.event.addListener(map, 'click', function (event) {

        //koordinate
        var pos = {
            lat: event.latLng.lat(),
            lng: event.latLng.lng()
        };

        // naslov se pridobi, da se klice geocoding
        GeocodingF(pos, function (data) {
            //ce ne vrne null - je nasel naslov
            if (data != null) {
                //reset bounds, to set focus only on this geofence
                bounds['maza_map_geofencing'] = new google.maps.LatLngBounds();
                
                //kreira marker na lokaciji, kjer je uporabnik kliknil
                maza_saveGeofence(data.formatted_address, maza_create_basic_circle(pos, data.formatted_address, maza_default_radius, false));
            } else {
                //odpre se okno, ce je prislo do napake - null - (mozen je tudi prekratek delay med geocoding requesti)
                alert(lang['srv_resevanje_alert_location_not_found_map']);
            }
        });
        

        return pos;
    });
}             

/**
 * funkcija, ki kreira osnovni circle (rdec)
 * @param {type} address - String - label o informacijah markerja
 * @param {type} circle - google maps new created circle
 * @param {type} id - id vrednosti markerja iz tabele geofence
 * @param {type} name - interno ime geofenca
 * @returns {undefined}
 */
function maza_createCircleVnaprej(address, circle, id, name) {
    if (circle != null) {
        //pridobi mapo spremenljivke
        var map = document.getElementById("maza_map_geofencing").gMap;

        circle.address = address;
        circle.id = id;
        if(name)
            circle.name = name;
        circle.infowin = makeGeofenceInfoWindow(circle);
        setListenersGeofence(circle, map);

        //markers[markerId] = marker; // cache marker in markers object
        //dont set listeners if survey is locked
        //bindMarkerEventsVM("maza_map_geofencing", marker, map); // bind right click event to marker
        //maza_setDeleteMenu(circle, map, "maza_map_geofencing");

        //Create a div element for container.
        /*var container = document.createElement("div");
        //margin, da se pri italic ne odreze zadja crka
        container.style.cssText = 'margin-right:1px';

        //Create a label element for address.
        var address = document.createElement("label");
        address.innerHTML = '<i>' + address + '</i>';
        address.style.cssText = 'font-size:0.85em;';
        container.appendChild(address);*/

        //listener ob kliku na marker
        /*google.maps.event.addListener(marker, 'click', function () {
            //if infowindow in opened, blur focus, so saving in DB can be triggered
            blurTitleInfowindow(false);
            infowindow.setContent(container);
            infowindow.open(map, marker);
        });*/

        google.maps.event.addListener(circle,'radius_changed',function(){
            //console.log('radius_changed '+circle.getRadius());
            $.post('ajax.php?t=MAZA&a=update_geofence', {id: circle.id, radius: circle.getRadius(), anketa: srv_meta_anketa_id});
            circle.radius = circle.getRadius();
            circle.infowin = makeGeofenceInfoWindow(circle);
            infowindow.close();
        });
        google.maps.event.addListener(circle,'center_changed',function(){
            //console.log('center_changed '+circle.getCenter());
            //koordinate
            var pos = {
                lat: circle.getCenter().lat(),
                lng: circle.getCenter().lng()
            };

            // naslov se pridobi, da se klice geocoding
            GeocodingF(pos, function (data) {
                //ce ne vrne null - je nasel naslov
                if (data != null) {
                    //save changes in DB
                    $.post('ajax.php?t=MAZA&a=update_geofence', {id: circle.id, lat: circle.getCenter().lat(), lng: circle.getCenter().lng(), address: data.formatted_address, anketa: srv_meta_anketa_id});
                    circle.address = data.formatted_address;
                } else {
                    //did not found address, save empty string
                    $.post('ajax.php?t=MAZA&a=update_geofence', {id: circle.id, lat: circle.getCenter().lat(), lng: circle.getCenter().lng(), address: '', anketa: srv_meta_anketa_id});
                    circle.address = 'Unknown address';
                }
                circle.infowin = makeGeofenceInfoWindow(circle);
                infowindow.close();
            });
            
        });

        //add to bounds 
        bounds["maza_map_geofencing"].union(circle.getBounds());
        
        //zemljevid se prilagodi okviru
        map.fitBounds(bounds["maza_map_geofencing"]);
    }
}

/**
 * Creates and returns a html container for infowindow
 * @param {type} shape - googles shape - polyline or polygon
 * @returns {makeShapeInfoWindow.container|Element}
 */
function makeGeofenceInfoWindow(shape) {
    //Create a div element for container.
    var container = document.createElement("div");
    //margin, da se pri italic ne odreze zadja crka
    container.style.cssText = 'margin-right:1px';

    //Create a textarea for the input text
    var textBoxTitle = document.createElement("input");
    textBoxTitle.style.cssText = "font-weight:bold; font-size:1em !important; display: block; float: left;";
    textBoxTitle.type = "text";
    textBoxTitle.placeholder = lang['srv_maza_geofence_infowin_name'];
    if(shape.name)
        textBoxTitle.value = shape.name;
    textBoxTitle.className = "input_title";
    textBoxTitle.maxLength = "20";
    textBoxTitle.id = "map_input_overlay_id_" + shape.id;
    if(geofencing_on)
        textBoxTitle.disabled=true;
    container.appendChild(textBoxTitle);
    //ko se spremeni textarea v windowinfo, spremeni value inputa za text
    google.maps.event.addDomListener(textBoxTitle, "change", function () {
        //save title of marker in DB
        $.post('ajax.php?t=MAZA&a=update_geofence_name', {id: shape.id, name: textBoxTitle.value,
            anketa: srv_meta_anketa_id, lang_id: srv_meta_lang_id});
    });

    if(!geofencing_on){
        var deleteImg = document.createElement("span");
        deleteImg.className = "faicon delete icon-grey_dark_link";
        deleteImg.title = lang['srv_vprasanje_delete_line_map'];
        deleteImg.style.cssText = "height:1.65em; display: block; float: right; margin-left:7px; cursor: pointer;";
        container.appendChild(deleteImg);
        
        //ko se spremeni textarea v windowinfo, spremeni value inputa za text
        google.maps.event.addDomListener(deleteImg, "click", function () {
            if (confirm(lang['srv_maza_geofence_delete_confirm_map']))
                maza_delete_geofence(shape);
        });
    }
    
    container.appendChild(document.createElement("br"));
    
    //Create a label element for address.
    var address = document.createElement("label");
    address.innerHTML = '<i>' + shape.address + '</i>';
    address.style.cssText = 'font-size:0.85em; cursor: default;';
    container.appendChild(address);
    container.appendChild(document.createElement("br"));
    
    //Create a label element for radius.
    var radius = document.createElement("label");
    radius.innerHTML = lang['srv_maza_geofence_infowin_radius'] + Math.round(shape.radius) + lang['srv_maza_geofence_infowin_radius_unit'];
    radius.style.cssText = 'font-size:0.85em;cursor: default;';
    container.appendChild(radius);
    container.appendChild(document.createElement("br"));

    //ko se spremeni textarea v windowinfo, spremeni value inputa za text
    /*google.maps.event.addDomListener(textBoxTitle, "change", function () {
        //save title of SHAPE in DB
        var lineData = {anketa: srv_meta_anketa_id, spr_id: spremenljivka,
            overlay_id: shape.overlay_id, address: textBoxTitle.value};
        $.post('ajax.php?t=branching&a=edit_naslov_polyline', lineData);
    });*/

    return container;
}

/**
 * Set Liteners when editing a shape
 * @param {type} spremenljivka - id spremenljivke
 * @param {type} shape - googles shape to set listeners on
 * @param {type} map - google map
 * @param locked boolean true, if survey is locked in branching
 * @returns {undefined}
 */
function setListenersGeofence(shape, map) {
    //listener ob kliku na marker
    google.maps.event.addListener(shape, 'click', function (ev) {
        //changeFocusLine(this);

        //open infowindow
        infowindow.setContent(this.infowin);
        infowindow.setPosition(ev.latLng);
        infowindow.open(map);

        $("#map_input_overlay_id_" + shape.id).focus();
    });
}

/**
 * Sets rightclick and dblclick listeners to open delete menu
 * @param {type} shape - shape to set listeners on
 * @param {type} map  - map on which to hover menu (shape's map)
 * @param {type} spremenljivka - id of variable
 * @returns {undefined}
 */
function maza_delete_geofence(circle) {
    $.post('ajax.php?t=MAZA&a=delete_geofence', {id: circle.id, anketa: srv_meta_anketa_id},
        function (data){
            if(data = 'OK')
                circle.setMap(null);
                infowindow.close();
        });
}

/**
 * Sets rightclick and dblclick listeners to open delete menu
 * @param {type} shape - shape to set listeners on
 * @param {type} map  - map on which to hover menu (shape's map)
 * @param {type} spremenljivka - id of variable
 * @returns {undefined}
 */
function maza_setDeleteMenu(shape, map, spremenljivka) {
    google.maps.event.addListener(shape, 'rightclick', function (e) {
        deleteMenu.open_shape(map, shape, e.latLng, spremenljivka);
    });
    google.maps.event.addListener(shape, 'dblclick', function (e) {
        deleteMenu.open_shape(map, shape, e.latLng, spremenljivka);
    });
}

/**
 * Onclick function when saving repeater
 * @returns {undefined}
 */
function maza_repeater_submit_button_click(){
    var inputVal = $("#maza_repeater_date_start").val();
    var pickedDateSplit = inputVal.split(".");
    var pickedDate = new Date(pickedDateSplit[2], pickedDateSplit[1]-1, pickedDateSplit[0]);
        
    var pickedDateTimestamp = pickedDate.getTime();
    var nowDate = new Date();
    var todayDateTimestamp = (new Date(nowDate.getFullYear(), nowDate.getMonth(), nowDate.getDate())).getTime();

    //date is in starting range, show warning
    if(pickedDateTimestamp <= todayDateTimestamp){
        var result = confirm(lang['srv_maza_repeater_edit_warning_alert']);
        if(result)
            $('#maza_save_repeater_form').submit();
    }
    //date is in the future, all ok, save it
    else
        $('#maza_save_repeater_form').submit();
}

/**
 * Onclick function when finishing repeater
 * @returns {undefined}
 */
function maza_repeater_cancel_click(){
    var result = confirm(lang['srv_maza_repeater_finish_warning_alert']);
    if(result)
        $('#maza_cancel_repeater_form').submit();
}