trial treatments
[sgn.git] / js / CXGN / BreedersToolbox / AddTrial.js
blob0f42e8155945e4b9c4832ac99b94b61e8b05ecdf
1 /*jslint browser: true, devel: true */
3 /**
5 =head1 AddTrial.js
7 Dialogs for adding trials
10 =head1 AUTHOR
12 Jeremy D. Edwards <jde22@cornell.edu>
14 =cut
19 var $j = jQuery.noConflict();
21 jQuery(document).ready(function ($) {
23     var list = new CXGN.List();
25     var design_json;
27     function save_project_info(name, year, desc) {
28         alert('data = ' + name + ' ' + year + ' ' + desc);
29         $.ajax({
30             type: 'GET',
31             async: false,
32             url: '/ajax/breeders/project/insert',
33             data: {
34                 'project_name': name,
35                 'project_description': desc,
36                 'year': year
37             },
38             success: function (response) {
39                 if (response.error) {
40                     alert(response.error);
41                 } else {
42                     alert('The trial information was saved.');
43                 }
44             },
45             error: function () {
46                 alert('An error occurred. sorry');
47             }
48         });
49     }
51     $(document).on('focusout', '#select_list_list_select', function() {
52         if ($('#select_list_list_select').val()) {
53             var stock_list_id = $('#select_list_list_select').val();
54             var stock_list = JSON.stringify(list.getList(stock_list_id));
55             verify_stock_list(stock_list);
56         }
57     });
59     $(document).on('focusout', '#list_of_checks_section_list_select', function() {
60         if ($('#list_of_checks_section_list_select').val()) {
61             var stock_list_id = $('#list_of_checks_section_list_select').val();
62             var stock_list = JSON.stringify(list.getList(stock_list_id));
63             verify_stock_list(stock_list);
64         }
65     });
67     $(document).on('focusout', '#crbd_list_of_checks_section_list_select', function() {
68         if ($('#crbd_list_of_checks_section_list_select').val()) {
69             var stock_list_id = $('#crbd_list_of_checks_section_list_select').val();
70             var stock_list = JSON.stringify(list.getList(stock_list_id));
71             verify_stock_list(stock_list);
72         }
73     });
74     
75     $(document).on('focusout', '#list_of_unrep_accession_list_select', function() {
76         if ($('#list_of_unrep_accession_list_select').val()) {
77             var stock_list_id = $('#list_of_unrep_accession_list_select').val();
78             var stock_list = JSON.stringify(list.getList(stock_list_id));
79             verify_stock_list(stock_list);
80         }
81     });
82     
83     $(document).on('focusout', '#list_of_rep_accession_list_select', function() {
84         if ($('#list_of_rep_accession_list_select').val()) {
85             var stock_list_id = $('#list_of_rep_accession_list_select').val();
86             var stock_list = JSON.stringify(list.getList(stock_list_id));
87             verify_stock_list(stock_list);
88         }
89     });
91     var stock_list_verified = 0;
92     function verify_stock_list(stock_list) {
93         $.ajax({
94             type: 'POST',
95             timeout: 3000000,
96             url: '/ajax/trial/verify_stock_list',
97             beforeSend: function(){
98                 jQuery('#working_modal').modal('show');
99             },
100             dataType: "json",
101             data: {
102                 'stock_list': stock_list,
103             },
104             success: function (response) {
105                 //console.log(response);
106                 jQuery('#working_modal').modal('hide');
107                 if (response.error) {
108                     alert(response.error);
109                     stock_list_verified = 0;
110                 }
111                 if (response.success){
112                     stock_list_verified = 1;
113                 }
114             },
115             error: function () {
116                 jQuery('#working_modal').modal('hide');
117                 alert('An error occurred. sorry');
118                 stock_list_verified = 0;
119             }
120        });
121     }
123     var num_plants_per_plot = 0;
124     var num_subplots_per_plot = 0;
125     function generate_experimental_design() {
126         var name = $('#new_trial_name').val();
127         var year = $('#add_project_year').val();
128         var desc = $('#add_project_description').val();
129         var trial_location = $('#add_project_location').val();
130         var block_number = $('#block_number').val();
131         //alert(block_number);
132         var row_number= $('#row_number').val();
133         var row_number_per_block=$('#row_number_per_block').val();
134         var col_number_per_block=$('#col_number_per_block').val();
135         var col_number=$('#col_number').val();
136        // alert(row_number);
137         var stock_list_id = $('#select_list_list_select').val();
138         var control_list_id = $('#list_of_checks_section_list_select').val();
139         var control_list_id_crbd = $('#crbd_list_of_checks_section_list_select').val();
141         var control_list_crbd;
142         if (control_list_id_crbd != ""){
143             control_list_crbd = JSON.stringify(list.getList(control_list_id_crbd));
144         }
145         var stock_list;
146         if (stock_list_id != "") {
147             stock_list_array = list.getList(stock_list_id);
148             stock_list = JSON.stringify(list.getList(stock_list_id));
149         }
150         var control_list;
151         if (control_list_id != "") {
152             control_list = JSON.stringify(list.getList(control_list_id));
153         }
155         var design_type = $('#select_design_method').val();
156         if (design_type == "") {
157             var design_type = $('#select_multi-design_method').val();
158         }
160         var rep_count = $('#rep_count').val();
161         var block_size = $('#block_size').val();
162         var max_block_size = $('#max_block_size').val();
163         var plot_prefix = $('#plot_prefix').val();
164         var start_number = $('#start_number').val();
165         var increment = $('#increment').val();
166         var fieldmap_col_number = $('#fieldMap_col_number').val();
167         var fieldmap_row_number = $('#fieldMap_row_number').val();
168         var plot_layout_format = $('#plot_layout_format').val();
169         var replicated_accession_list_id = $('#list_of_rep_accession_list_select').val();
170         var unreplicated_accession_list_id = $('#list_of_unrep_accession_list_select').val();
171         var row_in_design_number = $('#no_of_row_in_design').val();
172         var col_in_design_number = $('#no_of_col_in_design').val();
173         var no_of_rep_times = $('#no_of_rep_times').val();
174         var no_of_block_sequence = $('#no_of_block_sequence').val();
175         var no_of_sub_block_sequence = $('#no_of_sub_block_sequence').val();
176         
177         var unreplicated_accession_list;
178         if (unreplicated_accession_list_id != "") {
179             unreplicated_accession_list = JSON.stringify(list.getList(unreplicated_accession_list_id));
180         }
181         
182         var replicated_accession_list;
183         if (replicated_accession_list_id != "") {
184             replicated_accession_list = JSON.stringify(list.getList(replicated_accession_list_id));
185         }
187         var treatments = []
188         if (design_type == 'splitplot'){
189             for(var i=1; i<5; i++){
190                 var treatment_value = $('#create_trial_with_treatment_name_input'+i).val();
191                 if(treatment_value != ''){
192                     treatments.push(treatment_value);
193                 }
194             }
195             //console.log(treatments);
196             var num_plants_per_treatment = $('#num_plants_per_treatment').val();
197             num_plants_per_plot = 0;
198             if (num_plants_per_treatment){
199                 num_plants_per_plot = num_plants_per_treatment*treatments.length;
200             }
201             num_subplots_per_plot = treatments.length;
202         }
204         var greenhouse_num_plants = [];
205         if (stock_list_id != "" && design_type == 'greenhouse') {
206             for (var i=0; i<stock_list_array.length; i++) {
207                 var value = jQuery("input#greenhouse_num_plants_input_" + i).val();
208                 if (value == '') {
209                     value = 1;
210                 }
211                 greenhouse_num_plants.push(value);
212             }
213             //console.log(greenhouse_num_plants);
214         }
216         //alert(design_type);
218         $.ajax({
219             type: 'POST',
220             timeout: 3000000,
221             url: '/ajax/trial/generate_experimental_design',
222             dataType: "json",
223             beforeSend: function() {
224                 $('#working_modal').modal("show");
225             },
226             data: {
227                 'project_name': name,
228                 'project_description': desc,
229                 'year': year,
230                 'trial_location': trial_location,
231                 'stock_list': stock_list,
232                 'control_list': control_list,
233                 'control_list_crbd': control_list_crbd,
234                 'design_type': design_type,
235                 'rep_count': rep_count,
236                 'block_number': block_number,
237                 'row_number': row_number,
238                 'row_number_per_block': row_number_per_block,
239                 'col_number_per_block': col_number_per_block,
240                 'col_number': col_number,
241                 'block_size': block_size,
242                 'max_block_size': max_block_size,
243                 'plot_prefix': plot_prefix,
244                 'start_number': start_number,
245                 'increment': increment,
246                 'greenhouse_num_plants': JSON.stringify(greenhouse_num_plants),
247                 'fieldmap_col_number': fieldmap_col_number,
248                 'fieldmap_row_number': fieldmap_row_number,
249                 'plot_layout_format': plot_layout_format,
250                 'treatments':treatments,
251                 'num_plants_per_plot':num_plants_per_plot,
252                 'row_in_design_number': row_in_design_number,
253                 'col_in_design_number': col_in_design_number,
254                 'no_of_rep_times': no_of_rep_times,
255                 'no_of_block_sequence': no_of_block_sequence,
256                 'unreplicated_accession_list': unreplicated_accession_list,
257                 'replicated_accession_list': replicated_accession_list,
258                 'no_of_sub_block_sequence': no_of_sub_block_sequence,
259             },
260             success: function (response) {
261                 $('#working_modal').modal("hide");
262                 if (response.error) {
263                     alert(response.error);
264                 } else {
266                     $('#trial_design_information').html(response.design_info_view_html);
267                     var layout_view = JSON.parse(response.design_layout_view_html);
268                     //console.log(layout_view);
269                     var layout_html = '';
270                     for (var i=0; i<layout_view.length; i++) {
271                         //console.log(layout_view[i]);
272                         layout_html += layout_view[i] + '<br>';
273                     }
274                     $('#trial_design_view_layout_return').html(layout_html);
275                     //$('#trial_design_view_layout_return').html(response.design_layout_view_html);
277                     $('#working_modal').modal("hide");
278                     $('#trial_design_confirm').modal("show");
279                     design_json = response.design_json;
280                 }
281             },
282             error: function () {
283                 $('#working_modal').modal("hide");
284                 alert('An error occurred. sorry.');
285             }
286        });
287     }
289     //When the user submits the form, input validation happens here before proceeding to design generation
290     $(document).on('click', '#new_trial_submit', function () {
291         var name = $('#new_trial_name').val();
292         var year = $('#add_project_year').val();
293         var desc = $('textarea#add_project_description').val();
294         var method_to_use = $('.format_type:checked').val();
295         if (name == '') {
296             alert('Trial name required');
297             return;
298         }
299         if (year === '' || desc === '') {
300             alert('Year and description are required.');
301             return;
302         }
303         if (stock_list_verified == 1){
304             if (method_to_use == "empty") {
305                 alert('adding a project');
306                 save_project_info(name, year, desc);
307             }
308             if (method_to_use == "create_with_design_tool") {
309                 generate_experimental_design();
310             }
311         } else {
312             alert('Accession list is not valid!');
313             return;
314         }
315     });
317     $(document).on('change', '#select_design_method', function () {
318         //$("#add_project_dialog").dialog("option", "height","auto");
320         var design_method = $("#select_design_method").val();
321         if (design_method == "CRD") {
322             $("#trial_design_more_info").show();
323             $("#trial_multi-design_more_info").show();
324             $("#FieldMap").show();
325             $("#prephelp").hide();
326             //$("#fieldmap_options").show();
327             //$("#add_project_dialog").dialog("option", "height","auto");
328             $("#show_list_of_accession_section").show();
329             $("#show_list_of_unrep_accession").hide();
330             $("#show_list_of_rep_accession").hide();
331             $("#show_no_of_row_in_design").hide();
332             $("#show_no_of_col_in_design").hide();
333             $("#show_no_of_rep_times").hide();
334             $("#show_no_of_block_sequence").hide();
335             $("#show_no_of_sub_block_sequence").hide();
336             $("#show_list_of_checks_section").hide();
337             $("#crbd_show_list_of_checks_section").show();
338             $("#rep_count_section").show();
339             $("#block_number_section").hide();
340             $("#block_size_section").hide();
341             $("#max_block_section").hide();
342             $("#row_number_section").hide();
343             $("#row_number_per_block_section").hide();
344             $("#col_number_per_block_section").hide();
345             $("#col_number_section").hide();
346             $("#row_number_per_block_section").hide();
347             $("#other_parameter_section").hide();
348             $("#design_info").show();
349             $("#greenhouse_num_plants_per_accession_section").hide();
350             $('#greenhouse_default_num_plants_per_accession').hide();
351             $("#create_trial_with_treatment_section").hide();
352             $("#num_plants_per_plot_section").hide();
353         } else if (design_method == "RCBD") {
354             $("#trial_design_more_info").show();
355             $("#trial_multi-design_more_info").show();
356             $("#FieldMap").show();
357             $("#prephelp").hide();
358             //$("#add_project_dialog").dialog("option", "height","auto");
359             $("#show_list_of_accession_section").show();
360             $("#show_list_of_unrep_accession").hide();
361             $("#show_list_of_rep_accession").hide();
362             $("#show_no_of_row_in_design").hide();
363             $("#show_no_of_col_in_design").hide();
364             $("#show_no_of_rep_times").hide();
365             $("#show_no_of_block_sequence").hide();
366             $("#show_no_of_sub_block_sequence").hide();
367             $("#crbd_show_list_of_checks_section").show();
368             $("#show_list_of_checks_section").hide();
369             $("#rep_count_section").hide();
370             $("#block_number_section").show();
371             $("#block_size_section").hide();
372             $("#max_block_size_section").hide();
373             $("#row_number_section").hide();
374             $("#row_number_per_block_section").hide();
375             $("#col_number_per_block_section").hide();
376             $("#col_number_section").hide();
377             $("#row_number_per_block_section").hide();
378             $("#other_parameter_section").hide();
379             $("#design_info").show();
380             $("#greenhouse_num_plants_per_accession_section").hide();
381             $('#greenhouse_default_num_plants_per_accession').hide();
382             $("#create_trial_with_treatment_section").hide();
383             $("#num_plants_per_plot_section").hide();
384         } else if (design_method == "Alpha") {
385             $("#FieldMap").show();
386             $("#prephelp").hide();
387             $("#trial_design_more_info").show();
388             $("#trial_multi-design_more_info").show();
389             //$("#add_project_dialog").dialog("option", "height","auto");
390             $("#show_list_of_accession_section").show();
391             $("#show_list_of_unrep_accession").hide();
392             $("#show_list_of_rep_accession").hide();
393             $("#show_no_of_row_in_design").hide();
394             $("#show_no_of_col_in_design").hide();
395             $("#show_no_of_rep_times").hide();
396             $("#show_no_of_block_sequence").hide();
397             $("#show_no_of_sub_block_sequence").hide();
398             $("#crbd_show_list_of_checks_section").show();
399             $("#show_list_of_checks_section").hide();
400             $("#rep_count_section").show();
401             $("#block_number_section").hide();
402             $("#block_size_section").show();
403             $("#max_block_size_section").hide();
404             $("#row_number_section").hide();
405             $("#row_number_per_block_section").hide();
406             $("#col_number_per_block_section").hide();
407             $("#col_number_section").hide();
408             $("#row_number_per_block_section").hide();
409             $("#other_parameter_section").hide();
410             $("#design_info").show();
411             $("#greenhouse_num_plants_per_accession_section").hide();
412             $('#greenhouse_default_num_plants_per_accession').hide();
413             $("#create_trial_with_treatment_section").hide();
414             $("#num_plants_per_plot_section").hide();
415         } else if (design_method == "Lattice") {
416             $("#FieldMap").show();
417             $("#prephelp").hide();
418             $("#trial_design_more_info").show();
419             $("#trial_multi-design_more_info").show();
420             $("#crbd_show_list_of_checks_section").show();
421             $("#show_list_of_accession_section").show();
422             $("#show_list_of_unrep_accession").hide();
423             $("#show_list_of_rep_accession").hide();
424             $("#show_no_of_row_in_design").hide();
425             $("#show_no_of_col_in_design").hide();
426             $("#show_no_of_rep_times").hide();
427             $("#show_no_of_block_sequence").hide();
428             $("#show_no_of_sub_block_sequence").hide();
429             $("#show_list_of_checks_section").hide();
430             $("#rep_count_section").show();
431             $("#block_number_section").hide();
432             $("#block_size_section").hide();
433             $("#max_block_size_section").hide();
434             $("#row_number_section").hide();
435             $("#row_number_per_block_section").hide();
436             $("#col_number_per_block_section").hide();
437             $("#col_number_section").hide();
438             $("#row_number_per_block_section").hide();
439             $("#other_parameter_section").hide();
440             $("#design_info").show();
441             $("#greenhouse_num_plants_per_accession_section").hide();
442             $('#greenhouse_default_num_plants_per_accession').hide();
443             $("#create_trial_with_treatment_section").hide();
444             $("#num_plants_per_plot_section").hide();
445         } else if (design_method == "Augmented") {
446             $("#FieldMap").hide();
447             $("#prephelp").hide();
448             $("#trial_design_more_info").show();
449             $("#trial_multi-design_more_info").show();
450             //$("#add_project_dialog").dialog("option", "height","auto");
451             $("#show_list_of_checks_section").show();
452             $("#crbd_show_list_of_checks_section").hide();
453             $("#show_list_of_accession_section").show();
454             $("#show_list_of_unrep_accession").hide();
455             $("#show_list_of_rep_accession").hide();
456             $("#show_no_of_row_in_design").hide();
457             $("#show_no_of_col_in_design").hide();
458             $("#show_no_of_rep_times").hide();
459             $("#show_no_of_block_sequence").hide();
460             $("#show_no_of_sub_block_sequence").hide();
461             $("#rep_count_section").hide();
462             $("#block_number_section").hide();
463             $("#block_size_section").hide();
464             $("#max_block_size_section").show();
465             $("#row_number_section").hide();
466             $("#row_number_per_block_section").hide();
467             $("#col_number_per_block_section").hide();
468             $("#col_number_section").hide();
469             $("#row_number_per_block_section").hide();
470             $("#other_parameter_section").hide();
471             $("#design_info").show();
472             $("#greenhouse_num_plants_per_accession_section").hide();
473             $('#greenhouse_default_num_plants_per_accession').hide();
474             $("#create_trial_with_treatment_section").hide();
475             $("#num_plants_per_plot_section").hide();
476         } else if (design_method == "") {
477             $("#FieldMap").hide();
478             $("#prephelp").hide();
479             //$("#add_project_dialog").dialog("option", "height","auto");
480             $("#show_list_of_checks_section").hide();
481             $("#crbd_show_list_of_checks_section").hide();
482             $("#show_list_of_accession_section").hide();
483             $("#show_list_of_unrep_accession").hide();
484             $("#show_list_of_rep_accession").hide();
485             $("#show_no_of_row_in_design").hide();
486             $("#show_no_of_col_in_design").hide();
487             $("#show_no_of_rep_times").hide();
488             $("#show_no_of_block_sequence").hide();
489             $("#show_no_of_sub_block_sequence").hide();
490             $("#trial_design_more_info").hide();
491             $("#trial_multi-design_more_info").hide();
492             $("#rep_count_section").hide();
493             $("#block_number_section").hide();
494             $("#block_size_section").hide();
495             $("#max_block_size_section").hide();
496             $("#row_number_section").hide();
497             $("#row_number_per_block_section").hide();
498             $("#col_number_per_block_section").hide();
499             $("#col_number_section").hide();
500             $("#row_number_per_block_section").show();
501             $("#other_parameter_section").hide();
502             $("#other_parameter_section2").hide();
503             $("#design_info").show();
504             $("#greenhouse_num_plants_per_accession_section").hide();
505             $('#greenhouse_default_num_plants_per_accession').hide();
506             $("#create_trial_with_treatment_section").hide();
507             $("#num_plants_per_plot_section").hide();
508         }
510         else if (design_method == "MAD") {
511             $("#FieldMap").hide();
512             $("#prephelp").hide();
513             $("#trial_design_more_info").show();
514             $("#trial_multi-design_more_info").show();
515             //$("#add_project_dialog").dialog("option", "height","auto");
516             $("#show_list_of_checks_section").show();
517             $("#crbd_show_list_of_checks_section").hide();
518             $("#show_list_of_accession_section").show();
519             $("#show_list_of_unrep_accession").hide();
520             $("#show_list_of_rep_accession").hide();
521             $("#show_no_of_row_in_design").hide();
522             $("#show_no_of_col_in_design").hide();
523             $("#show_no_of_rep_times").hide();
524             $("#show_no_of_block_sequence").hide();
525             $("#show_no_of_sub_block_sequence").hide();
526             $("#rep_count_section").hide();
527             $("#row_number_section").show();
528             $("#block_number_section").hide();
529             $("#block_size_section").hide();
530             $("#row_number_per_block_section").show();
531             $("#col_number_per_block_section").show();
532             $("#col_number_section").show();
533             $("#max_block_size_section").hide();
534             $("#row_number_per_block_section").show();
535             $("#other_parameter_section").show();
536             $("#design_info").show();
538             $("#show_other_parameter_options").click(function () {
539                 if ($('#show_other_parameter_options').is(':checked')) {
540                     $("#other_parameter_options").show();
541                     //$("#add_project_dialog").dialog("option", "height","auto");
542                 }
543                 else {
544                     $("#other_parameter_options").hide();
545                     //$("#add_project_dialog").dialog("option", "height","auto");
546                 }
547             });
548             $("#greenhouse_num_plants_per_accession_section").hide();
549             $('#greenhouse_default_num_plants_per_accession').hide();
550             $("#create_trial_with_treatment_section").hide();
551             $("#num_plants_per_plot_section").hide();
552         }
554         else if (design_method == 'greenhouse') {
555             $("#FieldMap").hide();
556             $("#prephelp").hide();
557             $("#trial_design_more_info").show();
558             $("#trial_multi-design_more_info").show();
559             $("#show_list_of_checks_section").hide();
560             $("#crbd_show_list_of_checks_section").hide();
561             $("#show_list_of_accession_section").show();
562             $("#show_list_of_unrep_accession").hide();
563             $("#show_list_of_rep_accession").hide();
564             $("#show_no_of_row_in_design").hide();
565             $("#show_no_of_col_in_design").hide();
566             $("#show_no_of_rep_times").hide();
567             $("#show_no_of_block_sequence").hide();
568             $("#show_no_of_sub_block_sequence").hide();
569             $("#rep_count_section").hide();
570             $("#block_number_section").hide();
571             $("#block_size_section").hide();
572             $("#max_block_section").hide();
573             $("#row_number_section").hide();
574             $("#row_number_per_block_section").hide();
575             $("#col_number_per_block_section").hide();
576             $("#col_number_section").hide();
577             $("#row_number_per_block_section").hide();
578             $("#other_parameter_section").hide();
579             $("#design_info").hide();
580             $('#greenhouse_default_num_plants_per_accession').show();
581             $("#greenhouse_num_plants_per_accession_section").show();
582             $('#greenhouse_default_num_plants_per_accession').show();
583             $("#create_trial_with_treatment_section").hide();
584             $("#num_plants_per_plot_section").hide();
585             greenhouse_show_num_plants_section();
586         }
587         
588         else if (design_method == 'splitplot') {
589             $("#FieldMap").show();
590             $("#prephelp").hide();
591             $("#trial_design_more_info").show();
592             $("#trial_multi-design_more_info").show();
593             $("#show_list_of_checks_section").hide();
594             $("#crbd_show_list_of_checks_section").hide();
595             $("#show_list_of_accession_section").show();
596             $("#show_list_of_unrep_accession").hide();
597             $("#show_list_of_rep_accession").hide();
598             $("#show_no_of_row_in_design").hide();
599             $("#show_no_of_col_in_design").hide();
600             $("#show_no_of_rep_times").hide();
601             $("#show_no_of_block_sequence").hide();
602             $("#show_no_of_sub_block_sequence").hide();
603             $("#rep_count_section").hide();
604             $("#block_number_section").show();
605             $("#block_size_section").hide();
606             $("#max_block_section").hide();
607             $("#row_number_section").hide();
608             $("#row_number_per_block_section").hide();
609             $("#col_number_per_block_section").hide();
610             $("#col_number_section").hide();
611             $("#row_number_per_block_section").hide();
612             $("#other_parameter_section").hide();
613             $("#design_info").show();
614             $('#greenhouse_default_num_plants_per_accession').hide();
615             $("#greenhouse_num_plants_per_accession_section").hide();
616             $('#greenhouse_default_num_plants_per_accession').hide();
617             $("#create_trial_with_treatment_section").show();
618             $("#num_plants_per_plot_section").show();
619         }
620         else if (design_method == 'p-rep') {
621             $("#FieldMap").hide();
622             $("#trial_design_more_info").show();
623             $("#trial_multi-design_more_info").show();
624             $("#prephelp").show();
625             $("#show_list_of_accession_section").hide();
626             $("#show_list_of_checks_section").hide();
627             $("#show_list_of_unrep_accession").show();
628             $("#show_list_of_rep_accession").show();
629             $("#show_no_of_row_in_design").show();
630             $("#show_no_of_col_in_design").show();
631             $("#show_no_of_rep_times").show();
632             $("#show_no_of_block_sequence").show();
633             $("#show_no_of_sub_block_sequence").show();
634             $("#crbd_show_list_of_checks_section").hide();
635             $("#rep_count_section").hide();
636             $("#block_number_section").hide();
637             $("#block_size_section").hide();
638             $("#max_block_section").hide();
639             $("#row_number_section").hide();
640             $("#row_number_per_block_section").hide();
641             $("#col_number_per_block_section").hide();
642             $("#col_number_section").hide();
643             $("#row_number_per_block_section").hide();
644             $("#other_parameter_section").hide();
645             $("#design_info").hide();
646             $('#greenhouse_default_num_plants_per_accession').hide();
647             $("#greenhouse_num_plants_per_accession_section").hide();
648             $('#greenhouse_default_num_plants_per_accession').hide();
649             $("#create_trial_with_treatment_section").hide();
650             $("#num_plants_per_plot_section").hide();
651         }
653         else {
654             alert("Unsupported design method");
655         }
656     });
658     jQuery(document).on('change', '#select_list_list_select', function() {
659         if (jQuery("#select_design_method").val() == 'greenhouse') {
660             greenhouse_show_num_plants_section();
661         }
662     });
663     jQuery(document).on('keyup', '#greenhouse_default_num_plants_per_accession_val', function() {
664         if (jQuery("#select_design_method").val() == 'greenhouse') {
665             greenhouse_show_num_plants_section();
666         }
667     });
669     jQuery(document).on('keyup', '#greenhouse_default_num_plants_per_accession_val', function() {
670         if (jQuery("#select_design_method").val() == 'greenhouse') {
671             greenhouse_show_num_plants_section();
672         }
673     });
675     $("#show_plot_naming_options").click(function () {
676         if ($('#show_plot_naming_options').is(':checked')) {
677             $("#plot_naming_options").show();
678             //$("#add_project_dialog").dialog("option", "height","auto");
679         }
680         else {
681             $("#plot_naming_options").hide();
682             //$("#add_project_dialog").dialog("option", "height","auto");
683         }
684     });
686     $("#show_field_map_options").click(function () {
687       if ($('#show_field_map_options').is(':checked')) {
688         $("#field_map_options").show();
689       }
690       else {
691         $("#field_map_options").hide();
692       }
693     });
695     function save_experimental_design(design_json) {
696         var list = new CXGN.List();
697         var name = jQuery('#new_trial_name').val();
698         var year = jQuery('#add_project_year').val();
699         var desc = jQuery('#add_project_description').val();
700         var trial_location = jQuery('#add_project_location').val();
701         var block_number = jQuery('#block_number').val();
702         var stock_list_id = jQuery('#select_list_list_select').val();
703         var control_list_id = jQuery('#list_of_checks_section_list_select').val();
704         var stock_list;
705         if (stock_list_id != "") {
706             stock_list_array = list.getList(stock_list_id);
707             stock_list = JSON.stringify(list.getList(stock_list_id));
708         }
709         var control_list;
710         if (control_list_id != "") {
711            control_list = JSON.stringify(list.getList(control_list_id));
712         }
713         var design_type = jQuery('#select_design_method').val();
714         if (design_type == "") {
715             var design_type = jQuery('#select_multi-design_method').val();
716         }
717         var greenhouse_num_plants = [];
718         if (stock_list_id != "" && design_type == 'greenhouse') {
719             for (var i=0; i<stock_list_array.length; i++) {
720                 var value = jQuery("input#greenhouse_num_plants_input_" + i).val();
721                 if (value == '') {
722                     value = 1;
723                 }
724                 greenhouse_num_plants.push(value);
725             }
726             //console.log(greenhouse_num_plants);
727         }
729         //alert(design_type);
731         var rep_count = jQuery('#rep_count').val();
732         var block_size = jQuery('#block_size').val();
733         var max_block_size = jQuery('#max_block_size').val();
734         var plot_prefix = jQuery('#plot_prefix').val();
735         var start_number = jQuery('#start_number').val();
736         var increment = jQuery('#increment').val();
737         var breeding_program_name = jQuery('#select_breeding_program').val();
738         var fieldmap_col_number = jQuery('#fieldMap_col_number').val();
739         var fieldmap_row_number = jQuery('#fieldMap_row_number').val();
740         var plot_layout_format = jQuery('#plot_layout_format').val();
741         var trial_type = jQuery('#add_project_type').val();
743         jQuery.ajax({
744            type: 'POST',
745            timeout: 3000000,
746            url: '/ajax/trial/save_experimental_design',
747            dataType: "json",
748            beforeSend: function() {
749                jQuery('#working_modal').modal("show");
750            },
751            data: {
752                 'project_name': name,
753                 'project_description': desc,
754                 //'trial_name': trial_name,
755                 'year': year,
756                 'trial_type': trial_type,
757                 'trial_location': trial_location,
758                 'stock_list': stock_list,
759                 'control_list': control_list,
760                 'design_type': design_type,
761                 'rep_count': rep_count,
762                 'block_number': block_number,
763                 'block_size': block_size,
764                 'max_block_size': max_block_size,
765                 'plot_prefix': plot_prefix,
766                 'start_number': start_number,
767                 'increment': increment,
768                 'design_json': design_json,
769                 'breeding_program_name': breeding_program_name,
770                 'greenhouse_num_plants': JSON.stringify(greenhouse_num_plants),
771                 'fieldmap_col_number': fieldmap_col_number,
772                 'fieldmap_row_number': fieldmap_row_number,
773                 'plot_layout_format': plot_layout_format,
774                 'has_plant_entries': num_plants_per_plot,
775                 'has_subplot_entries': num_subplots_per_plot,
776             },
777             success: function (response) {
778                 if (response.error) {
779                     jQuery('#working_modal').modal("hide");
780                     alert(response.error);
781                     jQuery('#trial_design_confirm').modal("hide");
782                 } else {
783                     //alert('Trial design saved');
784                     jQuery('#working_modal').modal("hide");
785                     jQuery('#trial_saved_dialog_message').modal("show");
786                 }
787             },
788             error: function () {
789                 jQuery('#trial_saving_dialog').dialog("close");
790                 alert('An error occurred saving the trial.');
791                 jQuery('#trial_design_confirm').dialog("close");
792             }
793         });
794     }
796     jQuery('#new_trial_confirm_submit').click(function () {
797             save_experimental_design(design_json);
798     });
800     $('#view_trial_layout_button').click(function () {
801         $('#trial_design_view_layout').modal("show");
802     });
804     function open_project_dialog() {
805         $('#add_project_dialog').modal("show");
807         //removes any old list selects before adding current ones.
808         //his is important so that lists that are added and will appear without page refresh
809         $("#select_list_list_select").remove();
810         $("#list_of_checks_section_list_select").remove();
812     $("#select_list_list_select").remove();
813         $("#crbd_list_of_checks_section_list_select").remove();
814     $("#list_of_unrep_accession_list_select").remove();
815     $("#list_of_rep_accession_list_select").remove();
817         //add lists to the list select and list of checks select dropdowns.
818         $("#select_list").append(list.listSelect("select_list", [ 'accessions' ], '', 'refresh'));
819         $("#list_of_checks_section").append(list.listSelect("list_of_checks_section", [ 'accessions' ], '', 'refresh'));
821   //add lists to the list select and list of checks select dropdowns for CRBD.
822         $("#crbd_list_of_checks_section").append(list.listSelect("crbd_list_of_checks_section", [ 'accessions' ], "select optional check list", 'refresh'));
823     $("#list_of_unrep_accession").append(list.listSelect("list_of_unrep_accession", [ 'accessions' ], "Required: e.g. 200", 'refresh'));
824     $("#list_of_rep_accession").append(list.listSelect("list_of_rep_accession", [ 'accessions' ], "Required: e.g. 119", 'refresh'));
826         //add a blank line to location select dropdown that dissappears when dropdown is opened
827         $("#add_project_location").prepend("<option value=''></option>").val('');
828         $("#add_project_location").one('mousedown', function () {
829             $("option:first", this).remove();
830         });
832         //add a blank line to list select dropdown that dissappears when dropdown is opened
833         $("#select_list_list_select").prepend("<option value=''></option>").val('');
834         $("#select_list_list_select").one('mousedown', function () {
835             $("option:first", this).remove();
836         });
838         //add a blank line to list of checks select dropdown that dissappears when dropdown is opened
839         $("#list_of_checks_section_list_select").prepend("<option value=''></option>").val('');
840         $("#list_of_checks_section_list_select").one('mousedown', function () {
841             $("option:first", this).remove();
842         });
844   $("#crbd_list_of_checks_section_list_select").prepend("<option value=''></option>").val('');
845   $("#crbd_list_of_checks_section_list_select").one('mousedown', function () {
846             $("option:first", this).remove();
847   });
849         //add a blank line to design method select dropdown that dissappears when dropdown is opened
850         $("#select_design_method").prepend("<option value=''></option>").val('');
851         $("#select_design_method").one('mousedown', function () {
852             $("option:first", this).remove();
853             $("#trial_design_more_info").show();
854             //$("#add_project_dialog").dialog("option", "height","auto");
855             //trigger design method change events in case the first one is selected after removal of the first blank select item
856             $("#select_design_method").change();
857         });
859         //reset previous selections
860         $("#select_design_method").change();
862         var method_to_use = $('.format_type:checked').val();
863         if (method_to_use == "empty") {
864             $("#trial_design_info").hide();
865             $("#trial_design_more_info").hide();
866             $("#get_file_upload_data").hide();
867         }
868         if (method_to_use == "create_with_upload") {
869             $("#get_file_upload_data").show();
870             $("#trial_design_info").hide();
871             $("#trial_design_more_info").hide();
872         } else {
873             $("#get_file_upload_data").hide();
874         }
875         if (method_to_use == "create_with_design_tool") {
876             $("#trial_design_info").show();
877         } else {
878             $("trial_design_info").hide();
879         }
880     }
882      $('#add_project_link').click(function () {
883          get_select_box('years', 'add_project_year', {'auto_generate': 1 });
884          get_select_box('trial_types', 'add_project_type', {'empty':1} );
885          open_project_dialog();
886      });
888     jQuery('button[name="new_trial_add_treatments"]').click(function(){
889         jQuery('#trial_design_add_treatments').modal('show');
890     });
892     jQuery('#new_trial_add_treatments_continue').click(function(){
893         var treatment_name = jQuery('#new_treatment_name').val();
894         var html = "";
895         var design_array = JSON.parse(design_json);
896         for (var i=0; i<design_array.length; i++){
897             html += "<table class='table table-hover'><thead><tr><th>plot_name</th><th>accession</th><th>plot_number</th><th>block_number</th><th>rep_number</th><th>is_a_control</th><th>row_number</th><th>col_number</th><th class='table-success'>"+treatment_name+"</th></tr></thead><tbody>";
898             var design_hash = JSON.parse(design_array[i]);
899             for (var key in design_hash){
900                 if (key != 'treatments'){
901                     var plot_obj = design_hash[key];
902                     html += "<tr><td>"+plot_obj.plot_name+"</td><td>"+plot_obj.stock_name+"</td><td>"+plot_obj.plot_number+"</td><td>"+plot_obj.block_number+"</td><td>"+plot_obj.rep_number+"</td><td>"+plot_obj.is_a_control+"</td><td>"+plot_obj.row_number+"</td><td>"+plot_obj.col_number+"</td><td><input data-plot_name='"+plot_obj.plot_name+"' data-trial_index='"+i+"' data-trial_treatment='"+treatment_name+"' type='checkbox' name='add_trial_treatment_input'/></td></tr>";
903                 }
904             }
905             html += "</tbody></table>";
906         }
907         html += "<br/><br/>";
908         jQuery('#trial_design_add_treatment_select_html').html(html);
909         jQuery('#trial_design_add_treatment_select').modal('show');
910     });
912     jQuery('#new_trial_add_treatments_submit').click(function(){
913         var trial_treatments = [];
914         jQuery('input[name="add_trial_treatment_input"]').each(function() {
915             if (this.checked){
916                 var plot_name = jQuery(this).data('plot_name');
917                 var trial_index = jQuery(this).data('trial_index');
918                 var trial_treatment = jQuery(this).data('trial_treatment');
919                 if (trial_index in trial_treatments){
920                     var trial = trial_treatments[trial_index];
921                     if (trial_treatment in trial){
922                         trial[trial_treatment].push(plot_name);
923                     } else {
924                         trial[trial_treatment] = [plot_name];
925                     }
926                     trial_treatments[trial_index] = trial;
927                 } else {
928                     obj = {};
929                     obj[trial_treatment] = [plot_name];
930                     trial_treatments[trial_index] = obj;
931                 }
932             }
933         });
935         var new_design_array = [];
936         var design_array = JSON.parse(design_json);
937         for (var i=0; i<design_array.length; i++){
938             var design_hash = JSON.parse(design_array[i]);
939             if ('treatments' in design_hash){
940                 treatment_obj = design_hash['treatments'];
941                 new_treatments = trial_treatments[i];
942                 for (var key in new_treatments){
943                     treatment_obj[key] = new_treatments[key];
944                 }
945                 design_hash['treatments'] = treatment_obj;
946             } else {
947                 design_hash['treatments'] = trial_treatments[i];
948             }
949             new_design_array[i] = JSON.stringify(design_hash);
950         }
951         design_json = JSON.stringify(new_design_array);
953         var html = '';
954         for (var i=0; i<new_design_array.length; i++){
955             var design_hash = JSON.parse(new_design_array[i]);
956             var treatments = design_hash['treatments'];
957             //html += "Trial "+i+"<br/>";
958             for (var key in treatments){
959                 html += "Treatment: <b>"+key+"</b> Plots: ";
960                 var plot_array = treatments[key];
961                 html += plot_array.join(', ') + "<br/>";
962             }
963         }
964         jQuery('#trial_design_confirm_treatments').html(html);
965         jQuery('#trial_design_add_treatment_select').modal('hide');
966         jQuery('#trial_design_add_treatments').modal('hide');
967     });
971 function greenhouse_show_num_plants_section(){
972     var list = new CXGN.List();
973     var stock_list_id = jQuery('#select_list_list_select').val();
974     var default_num = jQuery('#greenhouse_default_num_plants_per_accession_val').val();
975     if (stock_list_id != "") {
976         stock_list = list.getList(stock_list_id);
977         //console.log(stock_list);
978         var html = '<form class="form-horizontal">';
979         for (var i=0; i<stock_list.length; i++){
980             html = html + '<div class="form-group"><label class="col-sm-9 control-label">' + stock_list[i] + ': </label><div class="col-sm-3"><input class="form-control" id="greenhouse_num_plants_input_' + i + '" type="text" placeholder="'+default_num+'" value="'+default_num+'" /></div></div>';
981         }
982         html = html + '</form>';
983         jQuery("#greenhouse_num_plants_per_accession").empty().html(html);
984     }