-
Notifications
You must be signed in to change notification settings - Fork 0
/
javascript.js
1099 lines (972 loc) · 33.6 KB
/
javascript.js
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
/**
* Copyright (C) 2008-2014 Null Team
*
* This software is distributed under multiple licenses;
* see the COPYING file in the main directory for licensing
* information for this specific distribution.
*
* This use of this software may be subject to additional restrictions.
* See the LEGAL file in the main directory for details.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
//keep the docs always visible on page
docs_always_visible = get_cookie('docs_always_visible');
/**
* Get Internet Explorer Version
* @return the version of Internet Explorer or a -1 (indicating the use of another browser)
*/
function getInternetExplorerVersion()
{
var rv = -1; // Return value assumes failure.
if (navigator.appName == 'Microsoft Internet Explorer') {
var ua = navigator.userAgent;
var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
if (re.exec(ua) != null)
rv = parseFloat( RegExp.$1 );
}
return rv;
}
//var ie = getInternetExplorerVersion();
/**
* Show hidden element
* Function tries to set the right style.display depending on the tag type
* @param element_id String. Id of the element to be displayed
*/
function show(element_id, display)
{
if (display==null)
display = "block";
if (typeof ie == 'undefined' || ie==null)
ie = getInternetExplorerVersion();
var element = document.getElementById(element_id);
if (element == null)
return;
switch (element.tagName.toLowerCase()) {
case "tr":
case "img":
case "input":
case "select":
case "iframe":
case "font":
element.style.display = (ie > 1 && ie<=9) ? display : "table-row";
break;
case "td":
element.style.display = (ie > 1 && ie<=9) ? display : "table-cell";
break;
case "table":
element.style.display = (ie > 1 && ie<=9) ? display : "table";
break;
default:
element.style.display = display;
}
}
/**
* Hide element
* Function sets style.display to 'none'.
* @param element_id String. Id of the element to be hidden
*/
function hide(element_id)
{
if (typeof ie == 'undefined' || ie==null)
ie = getInternetExplorerVersion();
var element = document.getElementById(element_id);
if (element == null)
return;
element.style.display = "none";
}
/**
* Show/hide advanced fields in a form and change src and title associated
* to the image clicked to perform this action
* This function is used from editObject() from lib.php is used with fields marked as advanced.
* @param identifier String. In case there are multiple forms in a single page,
* all elements from a form should start with this identified
*/
function advanced(identifier)
{
var form = document.getElementById(identifier);
var elems = (form!=null) ? form.elements : [];
var elem_name;
for (var i=0;i<elems.length;i++) {
elem_name = elems[i].name;
if (identifier.length>elem_name.length && elem_name.substr(0,identifier.length)!=identifier)
continue;
var elem = document.getElementById("tr_"+elem_name);
if (elem == null || elem.style.display == null || elem.style.display == "")
continue;
show_hide("tr_"+elem_name);
}
// show objtitles that were marked as advanced and are not for objects with specific index (_$index)
// maximum 10 objtitles
for (i=1; i<10; i++) {
var elem = document.getElementById("tr_" + identifier + i + "_objtitle");
if (elem == null || elem.style.display == null || elem.style.display == "")
continue;
show_hide("tr_" + identifier + i + "_objtitle");
}
var img = document.getElementById(identifier+"xadvanced");
if (img!=null && img.tagName=="IMG") {
var imgsrc= img.src;
var imgarray = imgsrc.split("/");
if (imgarray[imgarray.length-1] == "advanced.jpg") {
imgarray[imgarray.length-1] = "basic.jpg";
img.title = "Hide advanced fields";
} else {
imgarray[imgarray.length-1] = "advanced.jpg";
img.title = "Show advanced fields";
}
img.src = imgarray.join("/");
} else
console.log("advanced() was called, but img is null and tagName='"+img.tagName+"'");
}
/**
* Check/Uncheck all checkboxes from form containing the element
* Usually used from tableOfObjects() from lib.php
* @param element. 'Select all' checkbox whose checked state dictates the state of the other checkboxes
*/
function toggle_column(element)
{
var containing_form = parent_by_tag(element, "form");
if (containing_form==null)
return;
for(var z=0; z<containing_form.length;z++) {
if (containing_form[z].type != 'checkbox')
continue;
if (containing_form[z].disabled == true)
continue;
containing_form[z].checked = element.checked;
}
}
/**
* Retrieve containing tag of certain type where element resides.
* @param element Object. Element whose containing element you need
* Note! This is not the id of the element but the element itself
* @param tagname String. Lowercase value of the tag type of the desired container
* @return first parent element with specified tagType or null if not found
*/
function parent_by_tag(element, tagname)
{
if (element==null)
return;
while(true) {
parent_element = element.parentElement;
if (parent_element==null)
return null;
if (parent_element.tagName.toLowerCase()==tagname)
return parent_element;
element = parent_element;
}
}
/*
* Show/Hide tabs
* Used from generic_tabbed_settings() from lib.php
* @param count_sections Integer. Total number of tabs
* @param part_ids String. Particle identifying specific elements to be showed/hidden.
* Defaults to ''
*/
function show_all_tabs(count_sections,part_ids)
{
if (part_ids==null)
part_ids = "";
var img = document.getElementById("img_show_tabs"+part_ids);
if (img.src.substr(-12)=="/sm_show.png")
img.src = "images/sm_hide.png";
else
img.src = "images/sm_show.png";
var i;
for (i=1; i<count_sections; i++) {
section_tab = document.getElementById("tab_"+part_ids+i);
section_fill = document.getElementById("fill_"+part_ids+i);
if (section_tab==null) {
alert("Don't have section tab for "+part_ids+i);
continue;
}
if (section_tab.style.display=="") {
section_tab.style.display = "none";
if (section_fill!=null)
section_fill.style.display = "";
} else {
section_tab.style.display = "";
if (section_fill!=null)
section_fill.style.display = "none";
}
}
}
/**
* Show a section when clicked - when tabs are used.
* Function sets classname to "section_selected basic "+custom_css+"_selected" for first section when selected
* or "section_selected "+custom_css+"_selected" in case another one is selected
* and to "section basic "+custom_css when first section is closed
* and to "section "+custom_css when another section is closed
* @param section_index Integer. Number of the section to be shown.
* @param count_sections Integer. Total number of tabs
* @param part_ids String. Particle identifying specific elements to be showed/hidden.
* Defaults to ''
* @param custom_css String. Name of custom css class
*/
function show_section(section_index,count_sections,part_ids,custom_css)
{
if (part_ids==null)
part_ids="";
if (custom_css==null)
custom_css="";
var i, section_div, section_tab;
for (i=0; i<count_sections; i++) {
section_tab = document.getElementById("tab_"+part_ids+i);
section_div = document.getElementById("sect_"+part_ids+i);
if (section_tab==null) {
console.log("Don't have section tab for "+i);
continue;
}
if (section_div==null) {
console.log("Don't have section div for "+i);
continue;
}
if (i==section_index) {
if (i==0)
section_tab.className = "section_selected basic "+custom_css+"_selected";
else
section_tab.className = "section_selected "+custom_css+"_selected";
section_div.style.display = "";
} else {
cls = section_tab.className;
if (cls.substr(0,16)=="section_selected") {
if (i==0)
section_tab.className = "section basic "+custom_css;
else
section_tab.className = "section "+custom_css;
section_div.style.display = "none";
}
}
}
}
/**
* Show/hide comment associated to a field
* @param id String. Id of the object whose comment should be shown
*/
function show_hide_comment(id)
{
show_hide("comment_"+id);
}
/**
* Show element in iframe.
* @param category_id String. Category Id of the element to be shown
* @param comment_id String. Comment Id of the element to be shown
*/
function show_docs(category_id, comment_id)
{
if (docs_always_visible == "" || docs_always_visible == "false") {
docs_always_visible = true;
set_cookie('docs_always_visible', docs_always_visible);
}
/* set reference_id with the id found in iframe html*/
var iframe = document.getElementById('iframe_param');
var reference_id = category_id+"_id";
var iframe_doc = get_iframe_doc(iframe);
if (iframe_doc.getElementById(comment_id+"_id"))
reference_id = comment_id+"_id";
console.log("Reference_id comment: " + reference_id + ", comment_id: " + comment_id);
document.getElementById("page_id").style.width="78%";
if (iframe_doc.getElementById(reference_id))
iframe_doc.getElementById(reference_id).className = "docs_focus";
if (document.last_comment_id && iframe_doc.getElementById(document.last_comment_id+"_id") &&
document.last_comment_id+"_id" != reference_id)
iframe_doc.getElementById(document.last_comment_id+"_id").className = "docs_lost_focus";
/* set the new last_reference_id */
document.last_comment_id = comment_id;
show("iframe_param");
show("docs_iframe");
show("docs_x");
if (document.getElementById("iframe_param").style.display == "none") {
document.getElementById("iframe_param").style.className = "iframe_explanation_hidden";
document.getElementById("docs_x").style.className = "docs_close_hidden";
} else {
document.getElementById("iframe_param").style.className = "iframe_explanation";
document.getElementById("docs_x").style.className = "docs_close";
}
/* find if the element exist in the iframe and scroll the contents
* of the document window to the specified horizontal and vertical position*/
var iframe_win = get_iframe_win(iframe);
var element = iframe_doc.getElementById(reference_id);
if (element)
iframe_win.scroll(0, get_offset_top(element));
/*position the scroll on top of the page*/
window.scroll(0,0);
}
/**
* Show/hide element in iframe.
* @param category_id String. Category Id of the element to be shown/hidden
* @param comment_id String. Comment Id of the element to be shown/hidden
*/
function show_hide_docs(category_id, comment_id)
{
if (docs_always_visible == "" || docs_always_visible == "false") {
docs_always_visible = true;
set_cookie('docs_always_visible', docs_always_visible);
}
/* set reference_id with the id found in iframe html*/
var iframe = document.getElementById('iframe_param');
var reference_id = category_id+"_id";
var iframe_doc = get_iframe_doc(iframe);
if (iframe_doc.getElementById(comment_id+"_id"))
reference_id = comment_id+"_id";
document.getElementById("page_id").style.width="78%";
if (iframe_doc.getElementById(reference_id))
iframe_doc.getElementById(reference_id).style.color = "red";
if (document.last_comment_id && iframe_doc.getElementById(document.last_comment_id+"_id"))
iframe_doc.getElementById(document.last_comment_id+"_id").style.color = "black";
/* show or hide the element depending on the last_reference_id value*/
if (comment_id == document.last_comment_id) {
show_hide("iframe_param");
show_hide("docs_x");
if (document.getElementById("iframe_param").style.display == "none"){
document.getElementById("page_id").style.width="100%";
}
return;
}
/* set the new last_reference_id */
document.last_comment_id = comment_id;
show("iframe_param");
show("docs_iframe");
show("docs_x");
if (document.getElementById("iframe_param").style.display == "none") {
document.getElementById("iframe_param").style.className = "iframe_explanation_hidden";
document.getElementById("docs_x").style.className = "docs_close_hidden";
} else {
document.getElementById("iframe_param").style.className = "iframe_explanation";
document.getElementById("docs_x").style.className = "docs_close";
}
/* find if the element exist in the iframe and scroll the contents
* of the document window to the specified horizontal and vertical position*/
var iframe_win = get_iframe_win(iframe);
var element = iframe_doc.getElementById(reference_id);
if (element)
iframe_win.scroll(0, get_offset_top(element));
/*position the scroll on top of the page*/
window.scroll(0,0);
}
function resize_iframe(obj)
{
obj.style.height = document.getElementById("page_id").scrollHeight-47 + 'px';
}
function resize_left_iframe(obj, elem)
{
var iframe = document.getElementById('left_iframe');
var iframe_doc = get_iframe_doc(iframe);
if (iframe_doc.getElementById(elem))
obj.style.height = iframe_doc.getElementById(elem).scrollHeight + 'px';
else
obj.style.height = "110px";
}
function closeFrame() {
document.getElementById("docs_iframe").style.display="none";
document.getElementById("page_id").style.width="100%";
docs_always_visible = false;
set_cookie('docs_always_visible', docs_always_visible);
}
function set_cookie(cname, cvalue)
{
cvalue = encodeURIComponent(cvalue);
document.cookie = cname + "=" + cvalue + "; path=/";
}
function get_cookie(cname)
{
var name = cname + "=";
var ca = document.cookie.split(';');
for (var i=0; i<ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ')
c = c.substring(1);
if (c.indexOf(name) == 0)
return decodeURIComponent(c.substring(name.length,c.length));
}
return "";
}
/**
* Returns the Document object generated by a iframe element
* @param iframe Object. The iframe document object
*/
function get_iframe_doc(iframe)
{
return iframe.contentDocument || iframe.contentWindow.document;
}
/**
* Returns the Document object generated by an iframe element
* @param iframe Object. The iframe document object
*/
function get_iframe_win(iframe)
{
return iframe.contentWindow || iframe;
}
/**
* Returns the vertical position of a given object
* @param obj.
*/
function get_offset_top(obj)
{
var ret = 0;
while (obj.offsetParent) {
ret += obj.offsetTop;
obj = obj.offsetParent;
}
return ret;
}
/**
* Show/hide element. Changes visibility of the element
* @param element_id String. Id of the element to to shown/hidden
*/
function show_hide(element_id)
{
var element = document.getElementById(element_id);
if (element==null)
return;
if (element.style.display=="none")
show(element_id);
else
hide(element_id);
}
/**
* Make HTTP request to specified url.
* encodeURI function is used on url before using @make_api_request function
* @param url String. Where to make request to
* @param cb Callback. If set, call it passing response from HTTP request as argument
* @param aync Bool. If set, specifies if the request should be handled asynchronously or not.
* Default async is true (asynchronous)
*/
function make_request(url, cb, async)
{
url = encodeURI(url);
if (typeof async === 'undefined')
async = true;
make_api_request(url, cb, async);
}
/**
* Make HTTP request to specified url.
* If callback cb is defined, call with by passing response from HTTP request as parameter
* Use @make_reques function to make sure url is encoded since this function assumes it already was encoded.
* @param url String. Where to make request to
* @param cb Callback. If set, call it passing response from HTTP request as argument
* @param aync Bool. If set, specifies if the request should be handled asynchronously or not.
* Default async is true (asynchronous)
*/
function make_api_request(url, cb, async)
{
xmlhttp = GetXmlHttpObject();
if (xmlhttp == null) {
alert("Your browser does not support XMLHTTP!");
return;
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
var response = xmlhttp.responseText;
if (response)
remove_spinner();
if (typeof cb != 'undefined' && cb !== null)
call_function(cb,response);
}
};
if (typeof async === 'undefined')
async = true;
xmlhttp.open("GET", url, async);
xmlhttp.send(null);
}
/**
* Make callback
* @param cb Callback. Name of the function to be called
* The callback can be set as object:
* cb={name:name_func, param:value}
* @param param. Parameter to be passed when calling cb
*/
function call_function(cb, param)
{
if (cb && typeof(cb) === "function") {
// execute the callback, passing parameters as necessary
cb(param);
} else if (cb && typeof(cb) === "object" && typeof(cb.name) === 'function') {
cb.name(param, cb.param);
} else
console.error("Trying to call invalid callback "+cb.toString()+", type for it is: "+typeof(cb));
}
/**
* Retrieve object used to make HTTP request.
* @returns object. Function returns new XMLHttpRequest or ActiveXObject("Microsoft.XMLHTTP") depending on browser or null if none of the two is available
*/
function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
{
/* code for IE7+, Firefox, Chrome, Opera, Safari*/
return new XMLHttpRequest();
}
if (window.ActiveXObject)
{
/* code for IE6, IE5*/
return new ActiveXObject("Microsoft.XMLHTTP");
}
return null;
}
function insert_spinner()
{
if (document.getElementById("spinner_id") != null)
show("spinner_id");
}
function remove_spinner()
{
hide("spinner_id");
}
/**
* Check if required fields were set.
* The required_fields global variable is checked
* Ex: required_fields={"username":"username", "contact_info":"Contact information"}
* "contact_info" is the actual field_name in the form while "Contact information" is what the user sees associated to that form element
* @return Bool. True when required fields are set, false otherwise
* If required_fields is undefined then function returns true and a message is logged in console
*/
function check_required_fields()
{
if (typeof(required_fields) === "undefined") {
console.log("The required fields are not defined!");
return false;
}
var err = "";
// variable required_fields is a global array
// it is usually created from method requiredFieldsJs() from Wizard class
var field_name, field_value, field, tr_field;
for (field_name in required_fields) {
tr_field = document.getElementById("tr_"+field_name);
if ((tr_field.getAttribute("trigger") == "\\\"true\\\"" || tr_field.getAttribute("trigger")=="true") && tr_field.style.display=="none")
continue;
if (document.getElementById(field_name)==null) {
console.log("The required field: "+field_name+" has not an ID defined!");
return false;
}
field_value = document.getElementById(field_name).value;
if (field_value=="")
err += "Please set "+required_fields[field_name]+"! ";
}
if (err!="") {
error(err);
return false;
}
return true;
}
/**
* Display error. Function currently used alert.
* @param error String. Error to be displayed to the user
*/
function error(error)
{
alert(error);
}
/**
* Verify if value is numeric. Used isNaN js function.
* @param val String. Value to be checked
* @return Bool
*/
function is_numeric(val)
{
return !isNaN(val);
}
/**
* Delete element
* @param id String. Id of the tag to remove
*/
function delete_element(id)
{
var obj = document.getElementById(id);
if (obj)
obj.parentNode.removeChild(obj);
}
/**
* Change object/tag id
* @param id String. Current id of the tag
* @param new_id String.
*/
function set_id_obj(id, new_id)
{
var obj = document.getElementById(id);
if (obj)
obj.id = new_id;
}
/**
* Set value of object/tag
* @param id String. Id of the tag
* @param val String. Value to be set in tag with specified id
*/
function set_val_obj(id, val)
{
var obj = document.getElementById(id);
if (obj)
obj.value = val;
}
/**
* Submit form
* @param formid String. Id of the form to submit
*/
function submit_form(formid)
{
var form_to_submit = document.getElementById(formid);
if (form_to_submit)
form_to_submit.submit();
}
/**
* Retrieve selected value from dropdown
* @param id_name String. The id of the select tag
* @return String with selected option or null if tag with it is not found or no value is selected
*/
function get_selected(id_name)
{
var selector_obj = document.getElementById(id_name);
if (selector_obj==null)
return null;
var sel = selector_obj.options[selector_obj.selectedIndex].value || selector_obj.options[selector_obj.selectedIndex].text;
return sel;
}
/**
* Sets innerHTML for specific tag
* @param id String. Id of the tag to set content into
* @param html String. The content to be set in the tag specified by id
*/
function set_html_obj(id, html)
{
var obj = document.getElementById(id);
if (obj)
obj.innerHTML = (html == null) ? "" : html;
}
/**
* Show fields after 'Add another ...' link is pressed.
* This fields must already exist and be hidden.
* When made using display_pair() you must set triggered_by => "" for the fields
* Function will show all fields ending in link_index from form
* and will hide clicked link and display the one with the next index
* @param link_index Integer. The index that unites fields to be show as part of another object
* @param link_name String. Name of the add link. Ex: add, add_contact
* @param hidden_fields Array. Contains the name of the input fields of type 'hidden'
* @param level_fields Bool. If true, the fields in the FORM are on more levels
* @param name_obj_title String. Common piece of the name of the objtitle field for this level of objects
* @param border_elems Object. {"start":"", "end":""}. Name between which we start showing elements. Used when you have more types of objects so _index is not unique
* Ex: {"end":name_border} // between start of the form and name_border
* Ex: {"start":name_border} // between name_border and end of form
* Ex: {"start":border1, "end":border2} // between border1 and border2
*/
function fields_another_obj(link_index, link_name, hidden_fields, level_fields, name_obj_title, border_elems, multiple_subtitle)
{
console.log("Entered fields_another_obj() ", arguments);
// global variable needed by wizard_advanced
current_object_index = link_index;
if (!is_numeric(link_index)) {
console.error("Called fields_another_obj with non numeric param link_index: "+link_index);
return;
}
if (name_obj_title==undefined)
name_obj_title = "objtitle";
var element_name;
// this is the link that was clicked and should be hidden
var current_link_id = link_name+(link_index-1);
if (document.getElementById(current_link_id)==null)
// maybe previous index is not with build by decresing 1 but by removing the last digit: _ or _1 are current_link_id for _2 or _12
current_link_id = link_name+link_index.substr(0,link_index.length-1);
hide(current_link_id);
// retrieve all elements from same form as the clicked link
var parentform = parent_by_tag(document.getElementById(current_link_id),"form");
if (parentform==null) {
console.error("Can't retrieve parent for for element with id " + current_link_id);
return;
}
elems = parentform.elements;
// see if there are advanced fields (check button -- it's displayed only when there are fields marked as advanced)
var show_advanced = document.getElementById("advanced");
if (show_advanced!=null) {
console.log("innerHTML advanced button:'"+show_advanced.innerHTML+"'");
show_advanced = (show_advanced.innerHTML=="Advanced") ? false : true;
} else
show_advanced = false;
console.log("Show advanced: "+show_advanced);
// show objtitle, if defined
show("tr_" + name_obj_title + link_index);
console.log("level_fields="+level_fields);
if (level_fields!=undefined && level_fields==true)
show("tr_"+name_obj_title+"_level_"+link_index);
if (multiple_subtitle!=undefined && multiple_subtitle==true) {
var id_to_show;
for (var k=0; k<10; k++) {
if (k==0)
id_to_show = "tr_"+name_obj_title+"_level_"+link_index;
else
id_to_show = "tr_"+name_obj_title+"_level"+k+"_"+link_index;
show(id_to_show);
console.log("Tried to show:'"+id_to_show+"'");
}
}
var id_tr_element, tr_element;
var have_start = true;
var have_end = false;
if (border_elems!=undefined && border_elems.start!=undefined)
have_start = false;
for (var i=0; i<elems.length; i++) {
element_name = elems[i].name;
if (element_name.substr(-2)=="[]")
element_name = element_name.substr(0,element_name.length-2);
if (!have_start && elems[i].name==border_elems.start)
have_start = true;
if (border_elems!=undefined && border_elems.end!=undefined && border_elems.end==elems[i].name)
have_end = true;
if (!have_start || have_end) {
//console.log("Skipping "+elems[i].name+": have_start="+have_start+" have_end="+have_end);
continue;
}
// we assume that elements in form have the same "id" and "name"
// the containing tr is built by concatenanting "tr_" + element_id
id_tr_element = "tr_" + element_name;
// if form fields are displayed on more levels
// split the form elements name by '_' to get their id
// and skip the elements that don't have to be displayed
// otherwise just display links ending in link_index
if (level_fields!=undefined && level_fields==true) {
var index_arr = id_tr_element.split("_");
if (index_arr[index_arr.length-1] !=link_index)
continue;
} else if (id_tr_element.substr(element_name.length+2, id_tr_element.length)!=link_index)
continue;
tr_element = document.getElementById(id_tr_element);
// this field is advanced -> display it only if user already requested to see advanced fields
if (tr_element.getAttribute("advanced")=="true" && show_advanced==false)
continue;
show(id_tr_element);
}
// this is the next link that should be displayed
show("tr_"+link_name+link_index);
//if hidden_fields was set, append it to the other already set
if (hidden_fields != undefined) {
for (var name in hidden_fields) {
var input = document.createElement("INPUT");
input.setAttribute("type", "hidden");
input.setAttribute("name", ''+hidden_fields[name]+'');
input.setAttribute("value", 'off');
tr_element_hidden = document.getElementById("tr_hidden_fields");
tr_element_hidden.appendChild(input);
}
}
// show objtitles that were marked as advanced and are not for objects with specific index (_$index)
// Ex: 1_objtitle1, 1_objtitle2, -- first number is the nr of the objtitle, last is the object index
// 2_objtitle1, 2_objtitle2
// maximum 10 objtitles
for (i=1; i<10; i++) {
var elem = document.getElementById("tr_" + i + "_objtitle" + link_index);
if (elem == null || elem.style.display == null || elem.style.display == "" || (elem.getAttribute("advanced")=="true" && show_advanced==false))
continue;
show("tr_" + i + "_objtitle" + link_index);
}
}
/**
* Builds part of a link from the fields in a form
* Values set in form field are escaped with 'encodeURIComponent' before concatenanting
* @param form_name String. Name of the form where to take the elements from
* @return String. Part of a link
* Ex: ?name=Larry&phone_number=4034222222&zipcode=50505
*/
function link_from_fields(form_name)
{
var form_obj = document.forms[form_name];
if (form_obj==null)
return null;
var qs = '';
for (e=0;e<form_obj.elements.length;e++) {
if (form_obj.elements[e].name!='') {
if (form_obj.elements[e].type=="checkbox" && form_obj.elements[e].checked!=true)
continue;
if (form_obj.elements[e].type!="select-multiple") {
qs += (qs=='')?'?':'&';
qs += form_obj.elements[e].name+'='+encodeURIComponent(form_obj.elements[e].value);
} else {
for (var i = 0; i < form_obj.elements[e].options.length; i++) {
if (form_obj.elements[e].options[i].selected) {
qs += (qs=='')?'?':'&';
qs += form_obj.elements[e].name+"="+form_obj.elements[e].options[i].value;
}
}
}
}
}
console.log("Result link_from_fields: "+qs);
return qs;
}
/**
* Removed value from array
* Note! Only the first found value is removed
* @param val. Value to be removed
* @param arr Array. Array to remove value from
*/
function removeArrayValue(val,arr)
{
if (!val)
return;
for (var i=0; i<arr.length; i++) {
if (arr[i]==val) {
arr.splice(i, 1);
break;
}
}
}
/**
* Adds div element with specified id, content and css class in another element
* Element is inserted at the beggining
* @param parent_id String. Id of the parent element in which the new element will be added
* @param element_id String. Id of the new element that will be added
* @param html String. Content to be set inside new div
* @param css_class String. CSS class(es) for the new div
*/
function add_element_with_class(parent_id, element_id, html, css_class)
{
parent_elem = document.getElementById(parent_id);
if (parent_elem==null)
return;
newDiv = document.createElement("div");
newDiv.setAttribute("id", element_id);
newDiv.innerHTML = html;
if (css_class)
newDiv.className = css_class;
parent_elem.parentNode.insertBefore(newDiv, parent_elem);
}
/**
* Adds div element with specified id, content and css class in another element
* Element is inserted at the beggining
* @param parent_id String. Id of the parent element in which the new element will be added
* @param element_id String. Id of the new element that will be added
* @param html String. Content to be set inside new div
*/
function add_element(parent_id, element_id, html)
{
add_element_with_class(parent_id, element_id, html, null);
}
/**
* Used when user selects a 'Custom' option from a dropdown
* Adds input field with id 'custom_'+id of the dropdown
* @param custom_value String. Value to set in the newly added field
* @param dropdown_id String. Id of the dropdown
*/
function custom_value_dropdown(custom_value,dropdown_id)
{
// the id of the custom field that will be added
var custom_id = "custom_"+dropdown_id;
var custom_field = document.getElementById(custom_id);
var selected = get_selected(dropdown_id);
if (selected=="Custom") {
var dropdown = document.getElementById(dropdown_id);
var parent_td = dropdown.parentNode;
if (custom_field==null) {
parent_td.appendChild(document.createElement("br"));
// custom_field wasn't added in the page => add it here
var input = document.createElement("INPUT");
input.setAttribute("type", "text");
input.setAttribute("class", "margintop");
input.setAttribute("id", ''+custom_id+'');
input.setAttribute("name", ''+custom_id+'');
if (custom_value!=null)
input.setAttribute("value", ''+custom_value+'');
parent_td.appendChild(input);
} else {
// custom_field is already in the page => display it
custom_field.style.display = "";
}
} else if (custom_field!=null && custom_field.style.display!="none")
custom_field.style.display = "none";
}
// Toggle menu build with TabbedSettings class. Show/hide (Advanced/Basic) tabs and buttons
function toggle_menu()
{
//var i = (typeof(open_tabs)==="undefined") ? 1 : open_tabs;
var i = (open_tabs==undefined) ? 1 : open_tabs;
var current_state = document.getElementById("section_"+i).style.display;
var next_state = (current_state!="none") ? "none" : "table-cell";
var toggle_content = (current_state!="none") ? "Advanced >>" : "<< Basic";
var menu_tab;