summaryrefslogtreecommitdiffstats
path: root/admin/survey/script/invitations.js
blob: 71c36f6f43dcdaab9d0a1f75dee3a37b05fc2370 (plain) (blame)
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
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
function deleteRecipient_confirm(inv_rid) {
	// vprašamo ali resnično želi izbrisati respondenta
	$('#fade').fadeTo('slow', 1);
	$('#fullscreen').html('').fadeIn('slow');
	$("#fullscreen").load('ajax.php?t=invitations&a=delete_recipient_confirm', {anketa:srv_meta_anketa_id, inv_rid:inv_rid,noNavi:'true'});	
}
function inv_delete_recipient() {
	var inv_rid = $("#inv_delete_rec_confirm input#inv_rid").val();
			
	$.post('ajax.php?t=invitations&a=delete_recipient_single', {anketa:srv_meta_anketa_id, inv_rid:inv_rid,noNavi:'true'}, function(data) {
		data = jQuery.parseJSON(data);
		if (data.success == 1) {
			// uporabnik je bil zbrisan, skrijmo njevovo vrstico
			$('#tbl_recipients_list tr td input[value="'+inv_rid+'"]').closest('tr').hide('slow');
			$('#fade').fadeOut('slow');
			$('#fullscreen').fadeOut('slow').html('');
		}
	});
}
function editRecipient(inv_rid) {
	$('#fade').fadeTo('slow', 1);
	$('#fullscreen').addClass('divPopUp PopUpNarrow');
	$('#fullscreen').html('').fadeIn('slow');
	$("#fullscreen").load('ajax.php?t=invitations&a=edit_recipient', {anketa:srv_meta_anketa_id, inv_rid:inv_rid,noNavi:'true'});	
}

function inv_arch_recipients_close() {
	$('#fade').fadeOut('slow');
	$('#fullscreen').fadeOut('slow').html('');
}
function inv_arch_save_comment() {
	var aid = $("#inv_arch_id").val();
	var comment = $("#inv_arch_comment").val();
    
    $.post('ajax.php?t=invitations&a=arch_save_comment', {anketa:srv_meta_anketa_id, aid:aid, comment:comment, noNavi:'true'}, function(data) {
		// to ni vredu.window.location.reload();
		$('#fade').fadeOut('slow');
		$('#fullscreen').fadeOut('slow').html('');
	});
}
function inv_arch_recipients_send() {
	
	var send_type = $('input[name=mailto]:checked').val();
	var prefix = "";
	var checkboxes = "";

	$('input[name="mailto_status[]"]:checked').each(function(el) {
		checkboxes = checkboxes+prefix+$(this).val(); 
		prefix = ",";
	});

	$('#fullscreen').load('ajax.php?t=invitations&a=send_mail', 
			{anketa:srv_meta_anketa_id, noNavi:'true',send_type:send_type, checkboxes:checkboxes}
	);
}
function inv_change_import_type() {
	var import_type = $('input[name=inv_import_type]:checked').val();
	
	$("#inv_import").load('ajax.php?t=invitations&a=change_import_type', {anketa:srv_meta_anketa_id, import_type:import_type,noNavi:'true'}, function(){
        refreshFieldsList();
    });
}
function toggleInvCheckbox(what) {
	var id = $(what).attr("id");
    
	if ( $(what).is(":checked") ) {
		$(what).parent().addClass('inv_field_enabled');
    } 
    else {
		$(what).parent().removeClass('inv_field_enabled');
	}

	refreshFieldsList();
}

function refreshFieldsList() {
	var fields = '';
	var fields_id = '';
	var prefix = '';
	var pass_field = false;	
    
	$('ul').children('li.inv_field_enabled').each(function(idx, elm) {
		fields = fields + prefix + $(elm).find('label').html();
		fields_id = fields_id + prefix + $(elm).attr('id');
		prefix = ',';
		
		if($(elm).find('label').html() == 'PASSWORD')
			pass_field = true;
	});       
	
	$("#inv_field_list.inv_type_0, #inv_field_list.inv_type_1").html(fields);
	
	if ($("#inv_recipients_upoad_fields").length > 0) {
		$("#inv_recipients_upoad_fields").val(fields_id);
	}
	
	// Prikazemo opozorilo za dolzino passworda (20 znakov)
	if(pass_field)
		$("#inv_field_list_warning").show();
	else
		$("#inv_field_list_warning").hide();
}

function inv_save_rec_profile() {
	
	var profile_name = $("#rec_profile_name").val();
	var profile_id = $("#inv_recipients_profile_name select").find('option:selected').val();
	var profile_comment = $("#rec_profile_comment").val();
	var recipients_list = $("#inv_prof_recipients_list").val();
	var field_list = $("#inv_prof_field_list").val();
	var doAdd = ($("#inv_doAdd").val() == 1 || $("#inv_doAdd").val() == '1') ? true : false;

	$.post('ajax.php?t=invitations&a=save_rec_profile', {anketa:srv_meta_anketa_id, recipients_list:recipients_list, field_list:field_list, profile_name:profile_name, profile_comment:profile_comment, noNavi:'true', profile_id:profile_id}, function(data) {
		data = jQuery.parseJSON(data);

		if (doAdd == true) {
			inv_add_recipients(data.pid);
		} else {
			// prikažemo profil
			$("#anketa_edit").load('ajax.php?t=invitations&a=use_recipients_list', {anketa:srv_meta_anketa_id, pid:data.pid });
		}
		
	});
	$('#fade').fadeOut('slow');
	$('#fullscreen').fadeOut('slow').html('');
}

function inv_update_rec_profile() {
	
	var pid = $('#inv_recipients_profile_name').find('#rec_profile_pid').val();
	var profile_name = $('#inv_recipients_profile_name').find('#rec_profile_name').val();
	
	$("#inv_error_note").addClass('displayNone');
	
	$.post('ajax.php?t=invitations&a=update_rec_profile', {anketa:srv_meta_anketa_id, pid:pid, profile_name:profile_name, noNavi:'true'}, function(data) {
		data = jQuery.parseJSON(data);
		if (data.error == "0") {
			$('#fade').fadeOut('slow');
			$('#fullscreen').fadeOut('slow').html('');
			// osvežimo polja
			$("#anketa_edit").load('ajax.php?t=invitations&a=use_recipients_list', {anketa:srv_meta_anketa_id, pid:pid});
		} else {
			$("#inv_error_note").html(data.msg);
			$("#inv_error_note").show();
			$("#inv_error_note").removeClass('displayNone');
		}
	});
}
function inv_add_recipients(profile_id) {
	
	if (typeof profile_id === "undefined") {
		var pid = $("#inv_import_list_profiles ol li.active").attr('pid');
	} else {
		var pid = profile_id;
	}

	// vedno shranjujemo če ne druga v zacasin seznam.
	var save_profile = true;
	
	var recipients_list = $("#inv_recipients_list").val();
	var fields = [];
	$('ul').children('li.inv_field_enabled').each(function(idx, elm) {
		fields.push(elm.id);
	});                               
	if (fields.length > 0) {
		if (recipients_list.length > 0) {
			$("#anketa_edit").load('ajax.php?t=invitations&a=add_recipients', {anketa:srv_meta_anketa_id, recipients_list:recipients_list, fields:fields, pid:pid, save_profile:save_profile});
			// porihtamo še navigacijo - hardcoded
			$elm = $("ul.secondNavigation li.inv_ff_left_on");
			$elm.prev().find('a').removeClass('active');
			$elm.next().find('a').addClass('active');
			$elm.removeClass('inv_ff_left_on').addClass('inv_ff_right_on').next().next().addClass('inv_ff_left_on');
		} else {
			genericAlertPopup('srv_invitation_note1');
		}
	} else {
		genericAlertPopup('srv_invitation_note2');
	}

}
function inv_save_recipient() {
	
	var inv_rid = $("#inv_rid").val();
	var rec_email = $("#rec_email").val();
	var rec_password = $("#rec_password").val();
	var rec_firstname = $("#rec_firstname").val();
	var rec_lastname = $("#rec_lastname").val();
	var rec_salutation = $("#rec_salutation").val();
	var rec_phone = $("#rec_phone").val();
	var rec_custom = $("#rec_custom").val();
	var rec_relation = $("#rec_relation").val();

	// odstranimo morebitne predhodne napake
	$("#rec_email").removeClass('inv_input_error');
	$("#rec_password").removeClass('inv_input_error');
	
	$.post('ajax.php?t=invitations&a=save_recipient', {anketa:srv_meta_anketa_id, inv_rid:inv_rid, rec_email:rec_email, rec_password:rec_password, 
		rec_firstname:rec_firstname, rec_lastname:rec_lastname, rec_salutation:rec_salutation, rec_phone:rec_phone, rec_custom:rec_custom, rec_relation:rec_relation, noNavi:'true'}, function(data) {
        
            data = jQuery.parseJSON(data);
        
        if (data.error == "0") {
			// smo shranli lahko zapremo okno in refrešamo podatke
			$('#fade').fadeOut('slow');
			$('#fullscreen').fadeOut('slow').html('');
			
			// Refreshamo celotno stran
			location.reload();			
        } 
        else {
			// prikažemo obvestilo o napaki
			$("#inv_error_note").html(data.msg);
			$("#inv_error_note").show();
			$("#inv_error_note").removeClass('displayNone');
			if (data.error_email == '1') {
				$("#rec_email").addClass('inv_input_error');
			}
			if (data.error_password == '1') {
				$("#rec_password").addClass('inv_input_error');
			}
		}
	});
	
}
function inv_filter_recipients() {

	var inv_filter_on = $("#inv_rec_filter_on").is(":checked") ? 'true' : 'false';
	var inv_filter_value = $("#inv_rec_filter_value").val();
	var inv_filter_send = $("#inv_rec_filter_send option:selected").val();
	var inv_filter_respondet = $("#inv_rec_filter_respondet option:selected").val();
	var inv_filter_unsubscribed = $("#inv_rec_filter_unsubscribed option:selected").val();
	var inv_filter_list = $("#inv_rec_filter_list option:selected").val();
	var inv_filter_duplicates = $("#inv_rec_filter_duplicates").is(":checked") ? 'true' : 'false';

	$.post('ajax.php?t=invitations&a=set_recipient_filter', {anketa:srv_meta_anketa_id,inv_filter_on:inv_filter_on, inv_filter_value:inv_filter_value, inv_filter_send:inv_filter_send,inv_filter_respondet:inv_filter_respondet,inv_filter_unsubscribed:inv_filter_unsubscribed, inv_filter_list:inv_filter_list, inv_filter_duplicates:inv_filter_duplicates, noNavi:'true'}, function() {
		
		// Ce smo v telefonskem modulu
		if($('#advanced_module_phone').val()){
			$("#globalSettingsInner").load('ajax.php?t=invitations&a=view_recipients', {anketa:srv_meta_anketa_id});
		}
		else{
			$("#anketa_edit").load('ajax.php?t=invitations&a=view_recipients', {anketa:srv_meta_anketa_id});	
		}
	});
}

function inv_add_rec_to_db() {
	$('#fade').fadeTo('slow', 1);
	$("#anketa_edit").load('ajax.php?t=invitations&a=add_users_to_database', {anketa:srv_meta_anketa_id}, function() {
		$('#fade').fadeOut('slow');
	});
}

function recipientsProfileOnlyThisSurvey() {

	var checked = $('input[name=inv_show_list_type]:checked').val() == 1 ? true : false;

	$.post('ajax.php?t=invitations&a=only_this_survey', {anketa:srv_meta_anketa_id, checked:checked, noNavi:'true'}, function(data) {
		var pid = $("#inv_import_list_profiles ol li.active").attr('pid');
		if (pid == 'undefined') {
			pid = '-1';
		}
        
		$("#anketa_edit").load('ajax.php?t=invitations&a=use_recipients_list', {anketa:srv_meta_anketa_id, pid:pid });
	});
}

function mailToRadioChange() {
	
	var send_type = $('input[name=mailto]:checked').val();
	var prefix = "";
	var checkboxes = "";

	if ( $('#mailto4').is(":checked") ) {
		//$('#inv_send_advanced_div').slideDown();
		//disablamo ali enablamo  spodnje checkboxe
		$('#inv_send_advanced_div span').removeClass('gray');
		$('#inv_send_advanced_div span input[type=checkbox]').attr('disabled',false);

	} else {
		//$('#inv_send_advanced_div').slideUp();
		//disablamo ali enablamo  spodnje checkboxe
		$('#inv_send_advanced_div span').addClass('gray');
		$('#inv_send_advanced_div span input[type=checkbox]').attr('disabled',true);
	}
	
	$('input[name="mailto_status[]"]:checked').each(function(el) {
		checkboxes = checkboxes+prefix+$(this).val(); 
		prefix = ",";
	});

	var source_type = $('input[name=mailsource]:checked').val();
	var source_lists = "";
	var prefix = "";
	$('input[name="mailsource_lists[]"]:checked').each(function(el) {
		source_lists = source_lists+prefix+$(this).val(); 
		prefix = ",";
	});

	
	var noMailing = $('input[name=noMailing]').val();
	
	$("#inv_select_mail_to_respondents").load('ajax.php?t=invitations&a=view_send_recipients', {anketa:srv_meta_anketa_id, noNavi:'true',send_type:send_type, checkboxes:checkboxes, source_type:source_type, source_lists:source_lists, noMailing:noMailing}, function(){

		var cb = $('#tbl_recipients_send_list tr td').length;
		if (cb > 0 ) {
			$("#inv_send_mail_btn").show();
		} else {
			$("#inv_send_mail_btn").hide();
		}
		if (cb > 4999 ) {
			$("#inv_send_mail_limit").show();
		} else {
			$("#inv_send_mail_limit").hide();
		}

	});
};

function mailToSourceChange() {
	var send_type = $('input[name=mailto]:checked').val();
	var prefix = "";
	var checkboxes = "";
	$('input[name="mailto_status[]"]:checked').each(function(el) {
		checkboxes = checkboxes+prefix+$(this).val(); 
		prefix = ",";
	});
	var source_type = $('input[name=mailsource]:checked').val();
	var source_lists = "";
	var prefix = "";
	$('input[name="mailsource_lists[]"]:checked').each(function(el) {
		source_lists = source_lists+prefix+$(this).val(); 
		prefix = ",";
	});

	$("#inv_select_mail_to_source_lists").load('ajax.php?t=invitations&a=mailToSourceChange', {anketa:srv_meta_anketa_id, noNavi:'true',source_type:source_type, source_lists:source_lists}, function(){
	});

	$("#inv_select_mail_to_respondents").load('ajax.php?t=invitations&a=view_send_recipients', {anketa:srv_meta_anketa_id, noNavi:'true',send_type:send_type, checkboxes:checkboxes, source_type:source_type, source_lists:source_lists}, function(){
		var cb = $('#tbl_recipients_send_list tr td').length;
		if (cb > 0 ) {
			$("#inv_send_mail_btn").show();
		} else {
			$("#inv_send_mail_btn").hide();
		}
		if (cb > 4999 ) {
			$("#inv_send_mail_limit").show();
		} else {
			$("#inv_send_mail_limit").hide();
		}

	});
}

function mailToSourceCheckboxChange() {

	var send_type = $('input[name=mailto]:checked').val();
	var prefix = "";
    var checkboxes = "";
    
	$('input[name="mailto_status[]"]:checked').each(function(el) {
		checkboxes = checkboxes+prefix+$(this).val(); 
		prefix = ",";
    });
    
	var source_type = $('input[name=mailsource]:checked').val();
	var source_lists = "";
    var prefix = "";
    
	$('input[name="mailsource_lists[]"]:checked').each(function(el) {
		source_lists = source_lists+prefix+$(this).val(); 
		prefix = ",";
	});
	
	$("#inv_select_mail_to_respondents").load('ajax.php?t=invitations&a=view_send_recipients', {anketa:srv_meta_anketa_id, noNavi:'true',send_type:send_type, checkboxes:checkboxes, source_type:source_type, source_lists:source_lists}, function(){
		var cb = $('#tbl_recipients_send_list tr td').length;
        
        if (cb > 0 ) {
			$("#inv_send_mail_btn").show();
        } 
        else {
			$("#inv_send_mail_btn").hide();
		}
        
        if (cb > 4999 ) {
			$("#inv_send_mail_limit").show();
        } 
        else {
			$("#inv_send_mail_limit").hide();
		}
	});
}


function mailTocheCheckboxChange() {

	// izberemo rado za status
	$('#mailto4').attr('checked', true);
    
    var send_type = $('input[name=mailto]:checked').val();
	var prefix = "";
	var checkboxes = "";
    
    $('input[name="mailto_status[]"]:checked').each(function(el) {
		checkboxes = checkboxes+prefix+$(this).val(); 
		prefix = ",";
	});
    
    var source_type = $('input[name=mailsource]:checked').val();
	var source_lists = "";
	var prefix = "";
    
    $('input[name="mailsource_lists[]"]:checked').each(function(el) {
		source_lists = source_lists+prefix+$(this).val(); 
		prefix = ",";
	});
	
	$("#inv_select_mail_to_respondents").load('ajax.php?t=invitations&a=view_send_recipients', {anketa:srv_meta_anketa_id, noNavi:'true',send_type:send_type, checkboxes:checkboxes, source_type:source_type, source_lists:source_lists}, function(){
		var cb = $('#tbl_recipients_send_list tr td').length;
		if (cb > 0 ) {
			$("#inv_send_mail_btn").show();
		} else {
			$("#inv_send_mail_btn").hide();
		}
		if (cb > 4999 ) {
			$("#inv_send_mail_limit").show();
		} else {
			$("#inv_send_mail_limit").hide();
		}
	});
};
function invitations_init() {

	$(".inv_checkbox").live('change', function(event) {
		toggleInvCheckbox(this);
    });
    
	$("#inv_upload_recipients").live('click', function(event) {
		var inv_iid = $("#inv_iid").val();
		$("#inv_recipients_upload_form").submit();
	});

	$(".inv_step").mouseover(function() {
		$(this).addClass("hover");
	}).mouseout(function(){
		$(this).removeClass("hover");
	});
	
	$('#tbl_recipients_send_list tr td input').live('change', function(event) {
		// preštejemo obkljukane
		var cb = $('#tbl_recipients_send_list tr td input').filter(':checked').length;
		$("#inv_num_recipients").html(cb);
		if (cb > 0 ) {
			$("#inv_send_mail_btn").show();
		} else {
			$("#inv_send_mail_btn").hide();
		}
		
		if (cb > 4999 ) {
			$("#inv_send_mail_limit").show();
		} else {
			$("#inv_send_mail_limit").hide();
		}
		
	});
	
	// dodajanje respondentov
	$("#add_recipients").live('click', function(event) {
		inv_add_recipients();
	});
	
	// izbira obstoječega profila prejemnikov
	$("#inv_import_list_profiles ol li").live('click', function(event) {

		var pid = $(this).attr('pid');
		if (pid != 'undefined') {
			$("#anketa_edit").load('ajax.php?t=invitations&a=use_recipients_list', {anketa:srv_meta_anketa_id, pid:pid });
		}
	});
	
	$("#tbl_archive_list .as_link").live('click', function(event) {
		var target = $(event.target);
		var arch_to_view = $(target).attr('id');
		
		if($(target).hasClass('as_view')) {
			$('#fade').fadeTo('slow', 1);
			$('#fullscreen').html('').fadeIn('slow');
            archType = $(target).data('archtype');
			$("#fullscreen").load('ajax.php?t=invitations&a=view_archive_recipients', {anketa:srv_meta_anketa_id, arch_to_view:arch_to_view, archType:archType, noNavi:'true'});
		} else {
			$('#fade').fadeTo('slow', 1);
			$('#fullscreen').html('').fadeIn('slow');
			$("#fullscreen").load('ajax.php?t=invitations&a=edit_archive_comment', {anketa:srv_meta_anketa_id, arch_to_view:arch_to_view, noNavi:'true'});
		}
	});

}
function invChangeMessage(mid) {
	if (mid != 'undefined' && mid > 0) {
		if(CKEDITOR.instances['inv_message_body']) {
			delete CKEDITOR.instances['inv_message_body'];
		}

		$("#anketa_edit").load('ajax.php?t=invitations&a=make_default',
			{anketa:srv_meta_anketa_id, mid:mid },
			function(){
				if (!CKEDITOR.instances) {
					CKEDITOR.replace['inv_message_body'];
				}
		});
	}
}

function invMessageDelete() {

	var mid = $("#invitation_messages ol li.active").attr("mid");

	if (confirm(lang['srv_inv_recipients_delete_message_confirm'])) {

		if(CKEDITOR.instances['inv_message_body']) {
			delete CKEDITOR.instances['inv_message_body'];
		}

		$("#anketa_edit").load('ajax.php?t=invitations&a=delete_msg_profile', {anketa:srv_meta_anketa_id, mid:mid });

        event.stopPropagation();
	}
}	

function invShowMessageRename() {
	// prikažemo popup za preimenovanje
	var mid = $("#invitation_messages ol li.active").attr("mid");
	$('#fade').fadeTo('slow', 1);
	$('#fullscreen').addClass('divPopUp PopUpNarrow');
	$('#fullscreen').html('').fadeIn('slow');
	$("#fullscreen").load('ajax.php?t=invitations&a=show_message_rename', {anketa:srv_meta_anketa_id, noNavi:'true', mid:mid});
}	
function invMessageRename() {
	// prikažemo popup za preimenovanje
	var mid = $("#invitation_messages ol li.active").attr("mid");
	var name =  $("#inv_message_profile_name").val();
	var comment =  $("#inv_message_comment").val();
		
	$.post(
			'ajax.php?t=invitations&a=message_rename',
			{
				anketa : srv_meta_anketa_id,
				mid : mid,
				name : name,
				comment : comment,
				noNavi : 'true'
			},
			function(data) {
				data = jQuery.parseJSON(data);
				if (data.error == "0") {
					$('#fade').fadeOut('slow');
					$('#fullscreen').fadeOut('slow').html('')

				} else {
					$('#fade').fadeOut('slow');
					$('#fullscreen').fadeOut('slow').html('')

				}
			}
		);
}	

// najprej dodatno poeditiramo sporočilo
function inv_message_save_advanced(mid) {

	$('#fade').fadeTo('slow', 1);
    $('#fullscreen').addClass('divPopUp PopUpNarrow');
	$('#fullscreen').html('').fadeIn('slow');
    
	$("#fullscreen").load('ajax.php?t=invitations&a=edit_message_details', {anketa:srv_meta_anketa_id, noNavi:'true', mid:mid});
}

function inv_message_save_forward(mid) {
	//v kolikor je CKEditor vklopljen potem, ga odstranimo pred skranjevanjem
	if(CKEDITOR.instances['inv_message_body']){
		CKEDITOR.instances['inv_message_body'].destroy();
	}

	//gremo naprej brez dodatnega editiranja
	//preverimo če so spremembe in če so shranimo v novo sporočilo
	// shranimo v mid
	var replyto = $("#inv_message_replyto").val();
	var subject = $("#inv_message_subject").val();
	var body = $("#inv_message_body").val();
	var url = $("#inv_message_url").val();

	// resetiramo morebitne prejšne napake
	$("#inv_message_replyto").css({
		'border' : 'none'
	});
	$("#inv_message_subject").css({
		'border' : 'none'
	});
	$("#inv_message_body").css({
		'border' : 'none'
	});
	
	$("#inv_error_note").addClass('displayNone');
	$.post(
		'ajax.php?t=invitations&a=message_save_forward',
		{
			anketa : srv_meta_anketa_id,
			mid : mid,
			replyto : replyto,
			subject : subject,
			body : body,
			url: url,
			noNavi : 'true',
		},
		function(data) {
			data = jQuery.parseJSON(data);
			if (data.error == "0") {
				// redirektamo na pošiljanje
				var href = 'index.php?anketa='+srv_meta_anketa_id+'&a=invitations&m=send_message';
				window.location = href;
			} else {
				if (!CKEDITOR.instances) {
					CKEDITOR.replace['inv_message_body'];
				}
				// prikažemo obvestilo o napaki
				$("#inv_error_note").html(data.msg);
				$("#inv_error_note").removeClass('displayNone');
				if (data.inv_message_replyto == '1') {
					$("#inv_¸ge_replyto").css({
						'border' : '1px solid red'
					});
				}
				if (data.inv_message_subject == '1') {
					$("#inv_message_subject").css({
						'border' : '1px solid red'
					});
				}
				if (data.inv_message_body == '1') {
					$("#inv_message_body").css({
						'border' : '1px solid red'
					});
				}
			}
		});
}

function inv_message_save_forward_noEmail(mid) {
	//v kolikor je CKEditor vklopljen potem, ga odstranimo pred skranjevanjem
	if(CKEDITOR.instances['inv_message_body']){
		CKEDITOR.instances['inv_message_body'].destroy();
	}

	//gremo naprej brez dodatnega editiranja
	//preverimo če so spremembe in če so shranimo v novo sporočilo
	// shranimo v mid
	var subject = $("#inv_message_subject").val();
	var body = $("#inv_message_body").val();
	var url = $("#inv_message_url").val();

	// resetiramo morebitne prejšne napake
	$("#inv_message_subject").css({
		'border' : 'none'
	});
	$("#inv_message_body").css({
		'border' : 'none'
	});
	
	$("#inv_error_note").addClass('displayNone');
	$.post(
		'ajax.php?t=invitations&a=message_save_forward_noEmail',
		{
			anketa : srv_meta_anketa_id,
			mid : mid,
			subject : subject,
			body : body,
			url: url,
			noNavi : 'true',
		},
		function(data) {
			data = jQuery.parseJSON(data);
			if (data.error == "0") {
				// redirektamo na pošiljanje
				var href = 'index.php?anketa='+srv_meta_anketa_id+'&a=invitations&m=send_message';
				window.location = href;
			} else {
				if (!CKEDITOR.instances) {
					CKEDITOR.replace['inv_message_body'];
				}
				// prikažemo obvestilo o napaki
				$("#inv_error_note").html(data.msg);
				$("#inv_error_note").removeClass('displayNone');
				if (data.inv_message_subject == '1') {
					$("#inv_message_subject").css({
						'border' : '1px solid red'
					});
				}
				if (data.inv_message_body == '1') {
					$("#inv_message_body").css({
						'border' : '1px solid red'
					});
				}
			}
		});
}

function inv_message_save_simple(mid) {
	//v kolikor sporočilosamo shranimo potem editorpustimo odprt
	if(CKEDITOR.instances['inv_message_body']){
		CKEDITOR.instances['inv_message_body'].destroy();
	}

	// shranimo v mid
	var replyto = $("#inv_message_replyto").val();
	var subject = $("#inv_message_subject").val();
	var body = $("#inv_message_body").val();
	var url = $("#inv_message_url").val();
	
	// resetiramo morebitne prejšne napake
	$("#inv_message_replyto").css({
		'border' : 'none'
	});
	$("#inv_message_subject").css({
		'border' : 'none'
	});
	$("#inv_message_body").css({
		'border' : 'none'
	});

	$("#inv_error_note").addClass('displayNone');
	$.post(
			'ajax.php?t=invitations&a=save_message_simple',
			{
				anketa : srv_meta_anketa_id,
				mid : mid,
				replyto : replyto,
				subject : subject,
				body : body,
				url : url,
				noNavi : 'true'
			},		
			function(data) {
				create_editor('inv_message_body', true);
				data = jQuery.parseJSON(data);

				if (data.error == 0) {
					return true;
				}
				else {
					// skrijemo okno in 
					// prikažemo obvestilo o napaki
					$("#inv_error_note").html(data.msg);
					$("#inv_error_note").removeClass('displayNone');
					if (data.inv_message_replyto == '1') {
						$("#inv_messge_replyto").css({
							'border' : '1px solid red'
						});
					}
					if (data.inv_message_subject == '1') {
						$("#inv_message_subject").css({
							'border' : '1px solid red'
						});
					}
					if (data.inv_message_body == '1') {
						$("#inv_message_body").css({
							'border' : '1px solid red'
						});
					}
					return false;
				}
			});
}

function inv_message_save_simple_noEmail(mid) {
	//v kolikor sporočilosamo shranimo potem editorpustimo odprt
	if(CKEDITOR.instances['inv_message_body']){
		CKEDITOR.instances['inv_message_body'].destroy();
	}

	// shranimo v mid
	var subject = $("#inv_message_subject").val();
	var body = $("#inv_message_body").val();
	var url = $("#inv_message_url").val();
	
	// resetiramo morebitne prejšne napake
	$("#inv_message_subject").css({
		'border' : 'none'
	});
	$("#inv_message_body").css({
		'border' : 'none'
	});

	$("#inv_error_note").addClass('displayNone');
	$.post(
			'ajax.php?t=invitations&a=save_message_simple_noEmail',
			{
				anketa : srv_meta_anketa_id,
				mid : mid,
				subject : subject,
				body : body,
				url : url,
				noNavi : 'true'
			},		
			function(data) {
				create_editor('', true)
				data = jQuery.parseJSON(data);
				if (data.error == 0) {
					return true;
				}else {
					// skrijemo okno in 
					// prikažemo obvestilo o napaki
					$("#inv_error_note").html(data.msg);
					$("#inv_error_note").removeClass('displayNone');
					if (data.inv_message_subject == '1') {
						$("#inv_message_subject").css({
							'border' : '1px solid red'
						});
					}
					if (data.inv_message_body == '1') {
						$("#inv_message_body").css({
							'border' : '1px solid red'
						});
					}
					return false;
				}
			});
}

function inv_message_save_details() {
	// najprej shranimo detajle
	var mid = $("#inv_recipients_profile_name select").val();
	var profile_comment = $("#inv_message_comment").val();
	var naslov = $("#rec_profile_name").val();

	//v kolikor je CKEditor vklopljen potem, ga odstranimo pred skranjevanjem
	if(CKEDITOR.instances['inv_message_body']){
		CKEDITOR.instances['inv_message_body'].destroy();
	}

	var subject = $("#inv_message_subject").val();
	var body = $("#inv_message_body").val();
	
	$.post(
			'ajax.php?t=invitations&a=message_save_details',
			{
				anketa : srv_meta_anketa_id,
				mid : mid,
				profile_comment : profile_comment,
				naslov : naslov,
				body : body,
				subject : subject,
				noNavi : 'true'
			},
			function(data) {
				data = jQuery.parseJSON(data);
				if (data.error == "0" && parseInt(data.mid) > 0) {
					inv_message_save_simple(parseInt(data.mid));
					/*var href = 'index.php?anketa='+srv_meta_anketa_id+'&a=invitations&m=send_message';
					window.location = href;*/
					window.location.reload()
				} else {
					// so napake
					genericAlertPopup('alert_parameter_datamsg',data.msg);
				}
			}
		);
}

function inv_new_message_list_change(what) {
	if ($(what).val() == 0) {
		$("#new_message_list_span").show();
		$("#inv_message_comment").val('');
		
	} else {
		$("#new_message_list_span").hide();
		$("#inv_message_comment").val($(what).find('option:selected').attr('comment'));
	}
}

function inv_del_rec_profile() {

    var pid = $("#inv_import_list_profiles ol li.active").attr("pid");
    
	if (confirm(lang['srv_inv_recipients_delete_profile_confirm'])) {

		$.post('ajax.php?t=invitations&a=delete_rec_profile', {anketa:srv_meta_anketa_id, pid:pid, noNavi:'true'}, function(data) {
            
            // osvežimo polja
            var new_pid = $("#inv_import_list_profiles ol li").first().attr('pid');
            
            // če je slučajno isti kot smo ga zbrisli izberemo nasledenjega
			if (new_pid == pid) {
				var new_pid = $("#inv_import_list_profiles ol li").first().next().attr('pid');
            }
            
			$("#anketa_edit").load('ajax.php?t=invitations&a=use_recipients_list', {anketa:srv_meta_anketa_id, pid:new_pid });
		});
	}
}

function inv_edit_rec_profile() {
	var pid = $("#inv_import_list_profiles ol li.active").attr("pid");
	
	$('#fade').fadeTo('slow', 1);
    $('#fullscreen').addClass('divPopUp PopUpNarrow');
	$('#fullscreen').html('').fadeIn('slow');
	$("#fullscreen").load('ajax.php?t=invitations&a=edit_rec_profile', {anketa:srv_meta_anketa_id, pid:pid, noNavi:'true'});
}

function inv_prepare_add_recipients() {
	genericAlertPopup('alert_deprecated');
	return false;
}

function inv_recipients_add_to_list() {
	// prejemnike shranimo v seznam pred dodajanjem v bazo
	var recipients_list = $("#inv_recipients_list").val();
	var fields = [];

	$('ul').children('li.inv_field_enabled').each(function(idx, elm) {
		fields.push(elm.id);
	});                               
		
	var doAdd = '0';
		
	if (fields.length > 0) {
		if (recipients_list.length > 0) {
			$('#fade').fadeTo('slow', 1);
			$('#fullscreen').html('').fadeIn('slow');
			$("#fullscreen").load('ajax.php?t=invitations&a=get_profile_name', {anketa:srv_meta_anketa_id, recipients_list:recipients_list, fields:fields, noNavi:'true', doAdd:doAdd});
		} else {
			genericAlertPopup('srv_invitation_note1');
		}
	} else {
		genericAlertPopup('srv_invitation_note2');
	}
}

function inv_new_recipients_list_change(what) {
	if ($(what).val() > 0) {
		$("#new_recipients_list_span_note").show();
		$("#new_recipients_list_span_note_name").html($(what).find('option:selected').text());
	} else {
		$("#new_recipients_list_span_note").hide();		
	}
	if ($(what).val() == 0) {
		
		$("#new_recipients_list_span").show();
		$("#rec_profile_comment").val('');
	} else {
		$("#new_recipients_list_span").hide();
		$("#rec_profile_comment").val($(what).find('option:selected').attr('comment'));
	}
}

function invSendPage(page,show_per_page) {
	var start = (page-1) * show_per_page;
	var end = start +show_per_page;
	$("#tbl_recipients_send_list").find('tbody tr').slice(1).addClass('displayNone').slice(start,end).removeClass('displayNone');
	$("#frm_do_send div#pagination div").removeClass('currentPage_small');
	$("#frm_do_send div#pagination div").slice(page-1,page).addClass('currentPage_small');
}

function invSendPageChangeLimit(what,cnt) {
	// osvežimo paginacijo
	$("#inv_pagination_content").load('ajax.php?t=invitations&a=changePaginationLimit', {anketa:srv_meta_anketa_id, noNavi:'true', limit:$(what).val(), cnt:cnt},
		function() { invSendPage(1,$(what).val()); }
	);
	
}
function invTogleSend(what) {
	var checked = $(what).is(":checked") ? true : false;
	 $('#tbl_recipients_send_list tr td input[type=checkbox]').attr('checked', checked );
	// preštejemo obkljukane
		var cb = $('#tbl_recipients_send_list tr td input').filter(':checked').length;
		$("#inv_num_recipients").html(cb);

		if (cb > 0 ) {
			$("#inv_send_mail_btn").show();
		} else {
			$("#inv_send_mail_btn").hide();
		}
		
		if (cb > 4999 ) {
			$("#inv_send_mail_limit").show();
		} else {
			$("#inv_send_mail_limit").hide();
		}
}

function inv_selectAll(checkbox){

    val = $(checkbox).is(':checked');

	// oznacimo vse checkboxe
	$('#tbl_recipients_list tr td input[type="checkbox"]').each(function() {
		$(this).attr("checked", val);
	});

	if(val == true){
		$("#inv_switch_on").hide();
		$("#inv_switch_off").show();
	} 
    else{
		$("#inv_switch_off").hide();
		$("#inv_switch_on").show();
	}
}

function inv_list_selectAll(val){
    
    if(val == true){
        $('.test_checkAll').each(function(){
            this.checked = true;
        })
        $("#inv_switch_on").hide();
        $("#inv_switch_off").show();
    }
    else{
        $('.test_checkAll').each(function(){
            this.checked = false;
        })
        $("#inv_switch_on").show();
        $("#inv_switch_off").hide();
    }
}

function inv_recipients_form_action(action) {

	var recipents = $('#tbl_recipients_list input[name="inv_rids[]"]:checked');
	
	if (action == 'delete') {
		// imamo akcijo briši, preverimo če je kak izbran 
		if (recipents.length) {
			if (confirm(lang['srv_inv_recipients_delete_multi'])) {
				$("#frm_inv_rec_export").attr("action", "ajax.php?t=invitations&a=delete_recipient");
				$('#frm_inv_rec_export').submit();
				
			    return false;	
			}
		} else {
			if (confirm(lang['srv_invitation_note10'])) {
				//alert('brisem vse!');
				$.post('ajax.php?t=invitations&a=delete_recipient_all', {anketa:srv_meta_anketa_id,noNavi:'true'}, function(data) {
					data = jQuery.parseJSON(data);
					if (data.success == 1) {
						$("#anketa_edit").load('ajax.php?t=invitations&a=view_recipients', {anketa:srv_meta_anketa_id});
					} else {
						genericAlertPopup('alert_parameter_dataerror',data.error);
					}
				});
			}
			
		}
	} 
	else if (action == 'export_all') {
		$("#frm_inv_rec_export").attr("action", "ajax.php?t=invitations&a=export_recipients_all");
		$("#frm_inv_rec_export").attr("target", "_blank");
		$('#frm_inv_rec_export').submit();
		
	    return false;
	}
	else if (action == 'export_qr_codes') {
		$("#frm_inv_rec_export").attr("action", "ajax.php?t=invitations&a=export_recipients_qr_codes");
		$("#frm_inv_rec_export").attr("target", "_blank");
		$('#frm_inv_rec_export').submit();
		
		return false;
	}
	else if (action == 'export') {
		// imamo akcijo izvozi
		//če je kak izbran izvozimo tistega, če ne pa izberemo vse in izvozimo vse
		if (recipents.length == 0) {
			// izberemo vse checkboxe
			
			//$('#tbl_recipients_list input[name="inv_rids[]"]').attr('checked', 'true');
			//var recipents = $('#tbl_recipients_list input[name="inv_rids[]"]:checked');
			//$('#tbl_recipients_list input[name="inv_rids[]"]').attr('checked', 'false');
			$("#frm_inv_rec_export").attr("action", "ajax.php?t=invitations&a=export_recipients_all");
			$("#frm_inv_rec_export").attr("target", "_blank");
			$('#frm_inv_rec_export').submit();
		
		} else {
			// izvozimo samo izbrane
			$("#frm_inv_rec_export").attr("action", "ajax.php?t=invitations&a=export_recipients");
			$("#frm_inv_rec_export").attr("target", "_blank");
			$('#frm_inv_rec_export').submit();
			
		}		
		
	    return false;
	} 
	else if (action == 'add') {
		// imamo akcijo dodaj respondente
		if (recipents.length) {
			//if (confirm(lang['srv_inv_recipients_delete_multi'])) {
				$("#frm_inv_rec_export").attr("action", "ajax.php?t=invitations&a=add_checked_users_to_database");
				$('#frm_inv_rec_export').submit();
				
			    return false;	
			//}
		}
	}
}

function inv_recipients_list_action(action){
    if(action == 'delete'){
        var ids = [];
        $('.test_checkAll').each(function(){
           if(this.checked == true){
               ids.push(this.value);
           }
        });


        if(ids.length){
            if(confirm(lang['srv_inv_list_delete_multi'])){
                $.ajax({
                   type: "POST",
                    data: {anketa:srv_meta_anketa_id,noNavi:'true',ids: ids},
                    url: "ajax.php?t=invitations&a=deleteRecipientsListMulti",
                    success: function(data){
                        //console.log("Tle pride");
                        $("#anketa_edit").load('ajax.php?t=invitations&a=inv_lists', {anketa:srv_meta_anketa_id});
                    }
                });
            }
        }
    }else{
        confirm(lang['srv_inv_list_delete_multi']);
    }
}
/*
function inv_prepare_save_message() {
	// kateri mid imamo da če dodajamo k obstoječmu ga kr izberemo
	var mid = $("#invitation_messages ol li.active").attr('mid');
	
	// shrani - uredi 
	var chck1 = $("input#inv_message_save_type1").is(":checked");
	// pošlji
	//var chck2 = $("input#inv_message_send_type2").is(":checked");
	
	if (chck1) {
		// vsaj en checkbox more bit obkljukan
		$('#fade').fadeTo('slow', 1);
		$('#fullscreen').html('').fadeIn('slow');
		$("#fullscreen").load('ajax.php?t=invitations&a=prepare_save_message', {anketa:srv_meta_anketa_id, noNavi:'true', mid:mid});
	} else {
		// shranimo v trenutno izbrano sporočilo in redirektmo na pošiljanje
		// shranimoi v novo sporočilo
		edit_message_save('-1');
	}
	return false;
}
*/
/*
function edit_message_save(mid) {
	var replyto = $("#inv_message_replyto").val();
	var subject = $("#inv_message_subject").val();
	var body = $("#inv_message_body").val();
	var profile_comment = $("#inv_message_comment").val();
	var quickSave = true;
	if (!mid || mid == 'undefined') {
		mid = $("#inv_recipients_profile_name select").val();
		quickSave = false;
	}
	var naslov = $("#rec_profile_name").val();
	var old_mid = $("#invitation_messages ol li.active").attr('mid');
	// resetiramo morebitne prejšne napake
	$("#inv_message_replyto").css({
		'border' : 'none'
	});
	$("#inv_message_subject").css({
		'border' : 'none'
	});
	$("#inv_message_body").css({
		'border' : 'none'
	});

	$("#inv_error_note").addClass('displayNone');

	$.post(
			'ajax.php?t=invitations&a=save_message',
			{
				anketa : srv_meta_anketa_id,
				mid : mid,
				old_mid:old_mid,
				quickSave:quickSave,
				replyto : replyto,
				subject : subject,
				body : body,
				noNavi : 'true',
				profile_comment : profile_comment,
				naslov:naslov
			},
			function(data) {
				data = jQuery.parseJSON(data);
				if (data.error == "0") {
					// redirektamo na pošiljanje
					//var href = 'index.php?anketa='+srv_meta_anketa_id+'&a=invitations&m=send_message';
					//window.location = href;
					window.location.reload()
				} else {
					// skrijemo okno in 
					$('#fade').fadeOut('slow');
					$('#fullscreen').fadeOut('slow').html('')
					// prikažemo obvestilo o napaki
					$("#inv_error_note").html(data.msg);
					$("#inv_error_note").removeClass('displayNone');
					if (data.inv_message_replyto == '1') {
						$("#inv_message_replyto").css({
							'border' : '1px solid red'
						});
					}
					if (data.inv_message_subject == '1') {
						$("#inv_message_subject").css({
							'border' : '1px solid red'
						});
					}
					if (data.inv_message_body == '1') {
						$("#inv_message_body").css({
							'border' : '1px solid red'
						});
					}
				}
			});

	return false;
}
*/
function inv_arch_edit_details(aid) {
	if (aid > 0) {
		$('#fade').fadeTo('slow', 1);
		$('#fullscreen').html('').fadeIn('slow');
		$("#fullscreen").load('ajax.php?t=invitations&a=arch_edit_details', {anketa:srv_meta_anketa_id, aid:aid,noNavi:'true'});	
		
	}
}
function inv_arch_show_details(aid) {
	if (aid > 0) {
		$('#fade').fadeTo('slow', 1);
		$('#fullscreen').html('').fadeIn('slow');
		$("#fullscreen").load('ajax.php?t=invitations&a=arch_show_details', {anketa:srv_meta_anketa_id, aid:aid,noNavi:'true'});	
		
	}
}
function inv_arch_show_recipients(aid) {
	if (aid > 0) {
		$('#fade').fadeTo('slow', 1);
		$('#fullscreen').html('').fadeIn('slow');
		$("#fullscreen").load('ajax.php?t=invitations&a=arch_show_recipients', {anketa:srv_meta_anketa_id, aid:aid,noNavi:'true'});	
		
	}
}
function showRecipientTracking(rid) {
	if (rid > 0) {
		$('#fade').fadeTo('slow', 1);
		$('#fullscreen').html('').fadeIn('slow');
		$("#fullscreen").load('ajax.php?t=invitations&a=showRecipientTracking', {anketa:srv_meta_anketa_id, rid:rid,noNavi:'true'});	

	}
}
function changeInvRecListCheckbox() {
	var pids = "";
	var prefix = "";

	$('#inv_edit_rec_list table tr td input:checked').each(function(idx, elm) {
		pids = pids + prefix + $(elm).attr('value');
		prefix = ',';
	});     
	
	var onlyThisSurvey = $('input[name=inv_show_list_type]:checked').val();
	
	$("#inv_selected_rec_list").load('ajax.php?t=invitations&a=editRecList', {anketa:srv_meta_anketa_id,noNavi:'true',pids:pids, onlyThisSurvey:onlyThisSurvey});
	
}
/*
//urejanje prejemnikov
$("#inv_edit_rec_list table tr td").live('click', function(event) {
	var isCtrlPressed = event.ctrlKey;
	
	var target = $(event.target).closest("tr");
	var pid = $(target).attr('pid');
	
	
	// če imamo CTRL pritisnjen med klikom omogočimo muli select
	if (isCtrlPressed == true) {
		if (target.hasClass('active')) {
			target.removeClass('active');
		} else {
			target.addClass('active');
		}
	} else {
		// izbiramo vsakega posebej
		$('#inv_edit_rec_list table tr').removeClass('active');
		target.addClass('active');
	}

	var pids = "";
	var prefix = "";
	$('#inv_edit_rec_list table tr.active').each(function(idx, elm) {
		pids = pids + prefix + $(elm).attr('pid');
		prefix = ',';
	});     
	
	$("#inv_selected_rec_list").load('ajax.php?t=invitations&a=editRecList', {anketa:srv_meta_anketa_id,noNavi:'true',pids:pids});
	
});
*/
function inv_list_edit(pid) {
	$('#fade').fadeTo('slow', 1);
	$('#fullscreen').addClass('divPopUp PopUpNarrow');
	$('#fullscreen').html('').fadeIn('slow');
	$("#fullscreen").load('ajax.php?t=invitations&a=invListEdit', {anketa:srv_meta_anketa_id, pid:pid, noNavi:'true'});
}

function inv_list_get_name(saveNew) {
	// prejemnike shranimo v seznam pred dodajanjem v bazo
	var recipients_list = $("#inv_recipients_list").val();
	var fields = [];

	$('div#inv_field_container ul').children('li.inv_field_enabled').each(function(idx, elm) {
		fields.push(elm.id);
	});                               
	// kateri pid imamo da če dodajamo k obstoječmu ga kr izberemo
	//var pid = $("#inv_import_list_profiles ol li.active").attr('pid');
	var pid = "";
	var prefix = "";
	$('#inv_edit_rec_list table tr td input:checked').each(function(idx, elm) {
		pid = pid + prefix + $(elm).attr('value');
		prefix = ',';
	});  
	
	if (fields.length > 0) {
		if (recipients_list.length > 0) {
			$('#fade').fadeTo('slow', 1);
			$('#fullscreen').html('').fadeIn('slow');
			$("#fullscreen").load('ajax.php?t=invitations&a=list_get_name', {anketa:srv_meta_anketa_id, recipients_list:recipients_list, fields:fields, noNavi:'true', pid:pid, saveNew:saveNew});
		} else {
			genericAlertPopup('srv_invitation_note1');
		}
	} else {
		genericAlertPopup('srv_invitation_note2');
	}

}
function changeInvRecListEdit() {
	var checked = $("#inv_show_list_edit").is(":checked");
	$.post('ajax.php?t=invitations&a=changeInvRecListEdit', {anketa:srv_meta_anketa_id, checked:checked, noNavi:'true'}, function() {
		changeInvRecListCheckbox();
	});
}
function inv_list_save() {
	
	var profile_id = $("#profile_id").val();
	var profile_name = $("#inv_recipients_profile_name").find("#rec_profile_name").val();
	var profile_comment = $("#inv_recipients_profile_name").find("#rec_profile_comment").val();
	var recipients_list = $("#inv_prof_recipients_list").val();
	var field_list = $("#inv_prof_field_list").val();
	var saveNew = ($("#saveNew").val() == 'true') ? 'true': 'false';
	genericAlertPopup('alert_parameter_profilename',profile_name);
	$("#anketa_edit").load('ajax.php?t=invitations&a=inv_list_save', {anketa:srv_meta_anketa_id, recipients_list:recipients_list, field_list:field_list, profile_id:profile_id, profile_name:profile_name, profile_comment:profile_comment, saveNew:saveNew});
	$('#fade').fadeOut('slow');
	$('#fullscreen').fadeOut('slow').html('');
}
function inv_list_edit_save() {
	var form_serialize = $("#inv_list_edit_form").serializeArray();
	form_serialize[form_serialize.length] = {name:'anketa', value:srv_meta_anketa_id}
	form_serialize[form_serialize.length] = {name:'noNavi', value:'true'}
	
	$("#anketa_edit").load('ajax.php?t=invitations&a=invListEditSave', form_serialize);
	$('#fade').fadeOut('slow');
	$('#fullscreen').fadeOut('slow').html('');
}
function inv_list_save_old(profile_id) {
	
	var recipients_list = $("#inv_recipients_list").val();
	var fields = [];
	$('ul').children('li.inv_field_enabled').each(function(idx, elm) {
		fields.push(elm.id);
	});       
	var rec_profile_name = $("#rec_profile_name").val();
	var rec_profile_comment = $("#rec_profile_comment").val();
	
	$("#anketa_edit").load('ajax.php?t=invitations&a=invListSaveOld', {anketa:srv_meta_anketa_id, recipients_list:recipients_list, field_list:fields, profile_id:profile_id, rec_profile_name:rec_profile_name, rec_profile_comment:rec_profile_comment});
	$('#fade').fadeOut('slow');
	$('#fullscreen').fadeOut('slow').html('');
}

function deleteRecipientsList_confirm(id) {
	if (confirm(lang['srv_inv_recipients_delete_list_confirm'])) {
		$("#anketa_edit").load('ajax.php?t=invitations&a=deleteRecipientsList', {anketa:srv_meta_anketa_id, id:id});
	}
	
}
function inv_listAccess(show_hide) {
	if (show_hide == 'true') {
		$("#invListAccessShow1").toggle();
		$("#invListAccessShow2").toggle();
		$("div[name=listAccess]").each(function(){
			$(this).removeClass('displayNone');
		});
	} else {
		$("#invListAccessShow1").toggle();
		$("#invListAccessShow2").toggle();
		$("div[name=listAccess] label input:not(:checked)").each(function(){
			$(this).parent().parent().addClass('displayNone');
		});
	}
}

function showInvitationListsNames() {
	var onlyThisSurvey = $('input[name=inv_show_list_type]:checked').val();
	var pids = "";
	var prefix = "";
	$('#inv_edit_rec_list table tr td input:checked').each(function(idx, elm) {
		pids = pids + prefix + $(elm).attr('value');
		prefix = ',';
	});    
	
	
	$("#inv_edit_rec_list").load('ajax.php?t=invitations&a=showInvitationListsNames', {anketa:srv_meta_anketa_id,noNavi:'true',onlyThisSurvey:onlyThisSurvey, pids:pids},
			function() {
		// pids pogledamo na novo, ker se lahko vmes kaj spremeni 
		var pids = "";
		var prefix = "";
		$('#inv_edit_rec_list table tr td input:checked').each(function(idx, elm) {
			pids = pids + prefix + $(elm).attr('value');
			prefix = ',';
		});     
		
		$("#inv_selected_rec_list").load('ajax.php?t=invitations&a=editRecList', {anketa:srv_meta_anketa_id,noNavi:'true',pids:pids, onlyThisSurvey:onlyThisSurvey});
				//$('#inv_selected_rec_list div').nextAll().remove();
			}
	);
}

function inv_upload_list() {
	// vprašamo ali resnično želi izbrisati respondenta
	$('#fade').fadeTo('slow', 1);
	$('#fullscreen').html('').fadeIn('slow');
	$("#fullscreen").load('ajax.php?t=invitations&a=upload_list', {anketa:srv_meta_anketa_id, noNavi:'true'});	
}

function inv_upload_list_check() {
	// preverimo polja
	
	// preverimo datoteko
	var file = $('#invListFile').val();
	
	if (file != '') {
		// preverimo končnico
		var extension = file.split('.').pop().toLowerCase();
		//var extension = file.substr( (file.lastIndexOf('.') +1) );
		if (extension == 'txt' || extension == 'csv')  {
			$('#inv_recipients_upload_form').submit();
		} else {
			genericAlertPopup('alert_incorrect_filetype');
		}
	} else {
		genericAlertPopup('alert_choose_file');
	}
}


//prikaze editor za ne-spremenljivko (za karkoli druzga pac)
function create_inv_editor (id, focus) {
	id='inv_message_body';

	if (!editor_init) {
		CKEDITOR.replace(id, {
            fullPage: true,
            allowedContent: true
        });

		editor_init = true;
    }
    else{
		CKEDITOR.replace(id, {
            fullPage: true,
            allowedContent: true
        });
    }
}

function mailSourceMesageChange(what) {
	var mid = $(what).val();
	if (mid != 'undefined' && mid > 0) {
		$("#inv_select_mail_preview").load('ajax.php?t=invitations&a=make_default_from_preview', {anketa:srv_meta_anketa_id, mid:mid,noNavi:'true'});
	}
}

function inv_set_sort_field(field,type) {
	$.post('ajax.php?t=invitations&a=setSortField', {anketa:srv_meta_anketa_id,noNavi:'true', field:field,type:type}, function() {
		$("#anketa_edit").load('ajax.php?t=invitations&a=view_recipients', {anketa:srv_meta_anketa_id});	
	});
}

function inv_addSysVarsMapping() {
	var form_serialize = $("#inv_ValidateSysVarsMappingFrm").serializeArray();
	form_serialize[form_serialize.length] = {name:'anketa', value:srv_meta_anketa_id};
	form_serialize[form_serialize.length] = {name:'noNavi', value:'true'};
	
	$("#inv_ValidateSysVarsMappingDiv").load('ajax.php?t=invitations&a=addSysVarsMapping', form_serialize);
	
}
function inv_ValidateSysVarsMapping() {
	var form_serialize = $("#inv_ValidateSysVarsMappingFrm").serializeArray();
	form_serialize[form_serialize.length] = {name:'anketa', value:srv_meta_anketa_id};
	form_serialize[form_serialize.length] = {name:'noNavi', value:'true'};
	
	$("#inv_ValidateSysVarsMappingDiv").load('ajax.php?t=invitations&a=validateSysVarsMapping', form_serialize);

}

function invSysVarMapChange(what) {
	var value = $(what).val()+"";
	var name = $(what).attr('name')+"";
	/*	
	if (value != "") {
		// uporabnik je izbral polje, v vseh ostalih selectih je potrebno disejblat to polje

		$('#inv_ValidateSysVarsMappingFrm select > option').each(function(el) {
			if ( $(this).parent().attr('name') != name && value == $(this).val()+"" ) {
				$(this).attr("disabled","disabled");
			}
		});
	} else 
	*/
	{
		//polovimo izbrane vrednosti ostale enejlamo
		var values = [];
		$('#inv_ValidateSysVarsMappingFrm select').each(function(el){
			if ($(this).val()+"" != "") {
				values.push($(this).val()+"");
			}
		});
		// zlopamo skozi vse opcije in najprej odstranimo disabled 
		$('#inv_ValidateSysVarsMappingFrm select > option').each(function(el) {
			$(this).removeAttr("disabled","disabled");
		});
		// zlopamo skozi vse opcije in najprej dodamo disabled 
		$('#inv_ValidateSysVarsMappingFrm select > option').each(function(el) {
			if ( $(this).parent().attr('name') != name && ($.inArray($(this).val()+"", values) !== -1)) {
				$(this).attr("disabled","disabled");
			}
		});
	}
}

function invRenameRecipientsChange() {
	var checked = $("#inv_recipients_rename_profile").is(":checked");
	if (checked  == true) {
		$("#div_inv_recipients_rename_list_type").show();
		$('#rec_profile_name').focus();
	} else {
		$("#div_inv_recipients_rename_list_type").hide();
	}
}

function invRecipientsForward() {
	// ali dodajamo respondente v anketo
	var doAdd = $("#inv_recipients_add").is(":checked") ? true : false;
	
	// vedno shranjujemo če ne druga v zacasin seznam.
	var doSave = true;
	
	var profile_name = $("#rec_profile_name").val();
	// ce ne shranjujemo uporabimo id novega profila
	var profile_id = $("#inv_import_list_profiles ol li.active").attr('pid');
	
	if (doSave == true)
	{ // ce shranjujemo uporabimo id novega profila
		profile_id = $("#sel_inv_list_type").find('option:selected').val();
	} 
	var profile_comment = $("#rec_profile_comment").val();
	var recipients_list = $("#inv_recipients_list").val();
        var recipientsDelimiter = $('input[name=recipientsDelimiter]:checked').val();
        
        var fields = [];
	$('ul').children('li.inv_field_enabled').each(function(idx, elm) 
	{
		fields.push(elm.id);
	});      
	
	if (fields.length > 0)
	{
		if (recipients_list.length > 0)
		{
			// shranimo seznam ali dodamo respondente ali oboje
            $("#anketa_edit").load('ajax.php?t=invitations&a=recipientsAddForward', {anketa:srv_meta_anketa_id, doAdd:doAdd, doSave:doSave, recipients_list:recipients_list, fields:fields, profile_name:profile_name, profile_comment:profile_comment, profile_id:profile_id, recipientsDelimiter: recipientsDelimiter});
			/*$.post('ajax.php?t=invitations&a=recipientsAddForward', {anketa:srv_meta_anketa_id, doAdd:doAdd, doSave:doSave, recipients_list:recipients_list, fields:fields, profile_name:profile_name, profile_comment:profile_comment, profile_id:profile_id, recipientsDelimiter: recipientsDelimiter});
			window.location = 'index.php?anketa='+srv_meta_anketa_id+'&a=invitations&m=view_recipients';*/
        }
		else
		{
			genericAlertPopup('srv_invitation_note1');
		}
	} 
	else 
	{
		genericAlertPopup('srv_invitation_note2');
	}
}

function showInvitationAdvancedConditions(cid) {
	if (typeof cid === 'undefined')
	{
		if ($("a.faicon.if_add").data('cid').length)
		{
			cid = $("a.faicon.if_add").data('cid');
		}
		else 
		{
			cid = 0;
		}
	}
	
	
	$('#fade').fadeTo('slow', 1);
	$('#fullscreen').html('').fadeIn('slow');
	$("#fullscreen").load('ajax.php?t=invitations&a=showAdvancedConditions', {anketa:srv_meta_anketa_id,noNavi:'true', cid:cid});
}

function invitationSetCondition(cid)
{
	var cid = $("#divConditionProfiles #condition_profile").find(".active").data('cid');
	$('#fullscreen').fadeOut('slow').html('');
	$('#fade').fadeOut('slow');

	$("#anketa_edit").load('ajax.php?t=invitations&a=setAdvancedCondition', {anketa:srv_meta_anketa_id, cid:cid});
}

function noEmailingToggle(value){
	
	$.post('ajax.php?t=invitations&a=set_noEmailing', {anketa:srv_meta_anketa_id, value:value}, function(data) {
		/*if(value == '1'){
			$('#inv_messages_holder').hide();
			$('#inv_messages_holder_noEmailing').show();
		}
		else{
			$('#inv_messages_holder_noEmailing').hide(); 
			$('#inv_messages_holder').show();
		}*/
		window.location.reload();
	});
}
function noEmailingType(value){
	
	$.post('ajax.php?t=invitations&a=set_noEmailing_type', {anketa:srv_meta_anketa_id, value:value});
}


// AAI - popup pri vklopu ARNES smtp streznika pri vabilih
function smtpAAIPopupShow(){
	
    $('#fade').fadeTo('slow', 1);
	$('#general_popup').html('').addClass('PopUpNarrow').fadeIn('slow');
	$("#general_popup").load('ajax.php?t=invitations&a=showAAISmtpPopup', {anketa: srv_meta_anketa_id, noNavi:'true'});
}
function smtpAAIPopupClose(){
	
    // Ni sprejel - vrnemo radio
    $('input[name=SMTPMailMode][value=2]').prop('checked', true);

    popupClose();
}
function smtpAAISet(){
	
    // Shranimo formo
    $("form[name='settingsanketa_"+srv_meta_anketa_id+"']").submit();
}
function smtpAAIAccept(){
	
    if($('#aai_smtp_checkbox').is(':checked'))
        $('#aai_smtp_button').show();
    else
        $('#aai_smtp_button').hide();
}


// SQUALO
function squaloSwitch(){

    if($('#squalo_mode').prop('checked')){
        $('#send_mail_mode0, #send_mail_mode1, #send_mail_mode2, .mail_mode_switch, #send_mail_mode_test').addClass('displayNone');
        $('#success_save').hide();
    }
    else{
        $('.squalo_settings').addClass('displayNone');
        $('#send_mail_mode2, .mail_mode_switch, #send_mail_mode_test').removeClass('displayNone');
        $('#success_save').hide();
    }
}

// Sortiranje box-ov pri dodajanju respondentov
function initInvitationsConnectedSortable(){

    // Na mobitelu ne inicializiramo sorrtiranja, ker potem gumbi niso klikabilni (na nekaterih androidih ne deluje ok)
    if($(window).width() < 850)
        return false;

    $('ul.connectedSortable').sortable({
        update: function(){ 
            refreshFieldsList(); 
        }, 
        forcePlaceholderSize:'true', 
        tolerance:'pointer', 
        placeholder:'inv_field_placeholder', 
        cancel:'#inv_field_relation'
    });
}