refresh matview stockprop happens in ajax controller
[sgn.git] / js / CXGN / BreedersToolbox / AddTrial.js
blobe38b2402edc18e2bc64b072c419934007c36a7a8
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     var stock_list_id;
52     var stock_list;
53     var check_stock_list_id;
54     var check_stock_list;
55     var crbd_check_stock_list_id;
56     var crbd_check_stock_list;
57     var unrep_stock_list_id;
58     var unrep_stock_list;
59     var rep_stock_list_id;
60     var rep_stock_list;
61     var seedlot_list_id;
62     var seedlot_list;
63     var accession_list_seedlot_hash = {};
64     var checks_list_seedlot_hash = {};
65     var crbd_checks_list_seedlot_hash = {};
66     var unrep_list_seedlot_hash = {};
67     var rep_list_seedlot_hash = {};
69     $(document).on('focusout', '#select_list_list_select', function() {
70         if ($('#select_list_list_select').val()) {
71             stock_list_id = $('#select_list_list_select').val();
72             stock_list = JSON.stringify(list.getList(stock_list_id));
73             verify_stock_list(stock_list);
74             if(stock_list && seedlot_list){
75                 verify_seedlot_list(stock_list, seedlot_list, 'stock_list');
76             } else {
77                 accession_list_seedlot_hash = {};
78             }
79         }
80     });
82     $(document).on('focusout', '#list_of_checks_section_list_select', function() {
83         if ($('#list_of_checks_section_list_select').val()) {
84             check_stock_list_id = $('#list_of_checks_section_list_select').val();
85             check_stock_list = JSON.stringify(list.getList(check_stock_list_id));
86             verify_stock_list(check_stock_list);
87             if(check_stock_list && seedlot_list){
88                 verify_seedlot_list(check_stock_list, seedlot_list, 'check_stock_list');
89             } else {
90                 checks_list_seedlot_hash = {};
91             }
92         }
93     });
95     $(document).on('focusout', '#crbd_list_of_checks_section_list_select', function() {
96         if ($('#crbd_list_of_checks_section_list_select').val()) {
97             crbd_check_stock_list_id = $('#crbd_list_of_checks_section_list_select').val();
98             crbd_check_stock_list = JSON.stringify(list.getList(crbd_check_stock_list_id));
99             verify_stock_list(crbd_check_stock_list);
100             if(crbd_check_stock_list && seedlot_list){
101                 verify_seedlot_list(crbd_check_stock_list, seedlot_list, 'crbd_check_stock_list');
102             } else {
103                 crbd_checks_list_seedlot_hash = {};
104             }
105         }
106     });
108     $(document).on('focusout', '#list_of_unrep_accession_list_select', function() {
109         if ($('#list_of_unrep_accession_list_select').val()) {
110             unrep_stock_list_id = $('#list_of_unrep_accession_list_select').val();
111             unrep_stock_list = JSON.stringify(list.getList(unrep_stock_list_id));
112             verify_stock_list(unrep_stock_list);
113             if(unrep_stock_list && seedlot_list){
114                 verify_seedlot_list(unrep_stock_list, seedlot_list, 'unrep_stock_list');
115             } else {
116                 unrep_list_seedlot_hash = {};
117             }
118         }
119     });
121     $(document).on('focusout', '#list_of_rep_accession_list_select', function() {
122         if ($('#list_of_rep_accession_list_select').val()) {
123             rep_stock_list_id = $('#list_of_rep_accession_list_select').val();
124             rep_stock_list = JSON.stringify(list.getList(rep_stock_list_id));
125             verify_stock_list(rep_stock_list);
126             if(rep_stock_list && seedlot_list){
127                 verify_seedlot_list(rep_stock_list, seedlot_list, 'rep_stock_list');
128             } else {
129                 rep_list_seedlot_hash = {};
130             }
131         }
132     });
134     $(document).on('focusout', '#select_seedlot_list_list_select', function() {
135         if ($('#select_seedlot_list_list_select').val() != '') {
136             seedlot_list_id = $('#select_seedlot_list_list_select').val();
137             seedlot_list = JSON.stringify(list.getList(seedlot_list_id));
138             if(stock_list && seedlot_list){
139                 verify_seedlot_list(stock_list, seedlot_list, 'stock_list');
140             } else {
141                 alert('Please make sure to select an accession list above!');
142             }
143             if(check_stock_list && seedlot_list){
144                 verify_seedlot_list(check_stock_list, seedlot_list, 'check_stock_list');
145             }
146             if(crbd_check_stock_list && seedlot_list){
147                 verify_seedlot_list(crbd_check_stock_list, seedlot_list, 'crbd_check_stock_list');
148             }
149             if(unrep_stock_list && seedlot_list){
150                 verify_seedlot_list(unrep_stock_list, seedlot_list, 'unrep_stock_list');
151             }
152             if(rep_stock_list && seedlot_list){
153                 verify_seedlot_list(rep_stock_list, seedlot_list, 'rep_stock_list');
154             }
155         } else {
156             seedlot_list = undefined;
157             seedlot_list_verified = 1;
158             if (stock_list){
159                 verify_stock_list(stock_list);
160             }
161             accession_list_seedlot_hash = {};
162             checks_list_seedlot_hash = {};
163             crbd_checks_list_seedlot_hash = {};
164             unrep_list_seedlot_hash = {};
165             rep_list_seedlot_hash = {};
166         }
167     });
169     $(document).on('click', 'button[name="convert_accessions_to_seedlots"]', function(){
170         if (!stock_list_id){
171             alert('Please first select a list of accessions above!');
172         } else {
173             var list = new CXGN.List();
174             list.seedlotSearch(stock_list_id);
175         }
176     });
178     var stock_list_verified = 0;
179     function verify_stock_list(stock_list) {
180         $.ajax({
181             type: 'POST',
182             timeout: 3000000,
183             url: '/ajax/trial/verify_stock_list',
184             beforeSend: function(){
185                 jQuery('#working_modal').modal('show');
186             },
187             dataType: "json",
188             data: {
189                 'stock_list': stock_list,
190             },
191             success: function (response) {
192                 //console.log(response);
193                 jQuery('#working_modal').modal('hide');
194                 if (response.error) {
195                     alert(response.error);
196                     stock_list_verified = 0;
197                 }
198                 if (response.success){
199                     stock_list_verified = 1;
200                 }
201             },
202             error: function () {
203                 jQuery('#working_modal').modal('hide');
204                 alert('An error occurred. sorry');
205                 stock_list_verified = 0;
206             }
207         });
208     }
210     var seedlot_list_verified = 1;
211     function verify_seedlot_list(stock_list, seedlot_list, type) {
212         $.ajax({
213             type: 'POST',
214             timeout: 3000000,
215             url: '/ajax/trial/verify_seedlot_list',
216             beforeSend: function(){
217                 jQuery('#working_modal').modal('show');
218             },
219             dataType: "json",
220             data: {
221                 'stock_list': stock_list,
222                 'seedlot_list': seedlot_list,
223             },
224             success: function (response) {
225                 console.log(response);
226                 jQuery('#working_modal').modal('hide');
227                 if (response.error) {
228                     alert(response.error);
229                     seedlot_list_verified = 0;
230                 }
231                 if (response.success){
232                     seedlot_list_verified = 1;
233                     if (type == 'stock_list'){
234                         accession_list_seedlot_hash = response.seedlot_hash;
235                     }
236                     if (type = 'check_stock_list'){
237                         checks_list_seedlot_hash = response.seedlot_hash;
238                     }
239                     if (type == 'crbd_check_stock_list'){
240                         crbd_checks_list_seedlot_hash = response.seedlot_hash;
241                     }
242                     if (type == 'unrep_stock_list'){
243                         unrep_list_seedlot_hash = response.seedlot_hash;
244                     }
245                     if (type == 'rep_stock_list'){
246                         rep_list_seedlot_hash = response.seedlot_hash;
247                     }
248                 }
249             },
250             error: function () {
251                 jQuery('#working_modal').modal('hide');
252                 alert('An error occurred. sorry');
253                 seedlot_list_verified = 0;
254             }
255         });
256     }
258     var num_plants_per_plot = 0;
259     var num_subplots_per_plot = 0;
260     function generate_experimental_design() {
261         var name = $('#new_trial_name').val();
262         var year = $('#add_project_year').val();
263         var desc = $('#add_project_description').val();
264         var trial_location = $('#add_project_location').val();
265         var block_number = $('#block_number').val();
266         //alert(block_number);
267         var row_number= $('#row_number').val();
268         var row_number_per_block=$('#row_number_per_block').val();
269         var col_number_per_block=$('#col_number_per_block').val();
270         var col_number=$('#col_number').val();
271        // alert(row_number);
272         var stock_list_id = $('#select_list_list_select').val();
273         var control_list_id = $('#list_of_checks_section_list_select').val();
274         var control_list_id_crbd = $('#crbd_list_of_checks_section_list_select').val();
276         var control_list_crbd;
277         if (control_list_id_crbd != ""){
278             control_list_crbd = JSON.stringify(list.getList(control_list_id_crbd));
279         }
280         var stock_list;
281         var stock_list_array;
282         if (stock_list_id != "") {
283             stock_list_array = list.getList(stock_list_id);
284             stock_list = JSON.stringify(stock_list_array);
285         }
286         var control_list;
287         if (control_list_id != "") {
288             control_list = JSON.stringify(list.getList(control_list_id));
289         }
291         var design_type = $('#select_design_method').val();
292         if (design_type == "") {
293             var design_type = $('#select_multi-design_method').val();
294         }
296         var rep_count = $('#rep_count').val();
297         var block_size = $('#block_size').val();
298         var max_block_size = $('#max_block_size').val();
299         var plot_prefix = $('#plot_prefix').val();
300         var start_number = $('#start_number').val();
301         var increment = $('#increment').val();
302         var fieldmap_col_number = $('#fieldMap_col_number').val();
303         var fieldmap_row_number = $('#fieldMap_row_number').val();
304         var plot_layout_format = $('#plot_layout_format').val();
305         var replicated_accession_list_id = $('#list_of_rep_accession_list_select').val();
306         var unreplicated_accession_list_id = $('#list_of_unrep_accession_list_select').val();
307         var row_in_design_number = $('#no_of_row_in_design').val();
308         var col_in_design_number = $('#no_of_col_in_design').val();
309         var no_of_rep_times = $('#no_of_rep_times').val();
310         var no_of_block_sequence = $('#no_of_block_sequence').val();
311         var no_of_sub_block_sequence = $('#no_of_sub_block_sequence').val();
312         var num_seed_per_plot = $('#num_seed_per_plot').val();
314         var seedlot_hash_combined = {};
315         seedlot_hash_combined = extend_obj(accession_list_seedlot_hash, checks_list_seedlot_hash);
316         seedlot_hash_combined = extend_obj(seedlot_hash_combined, crbd_checks_list_seedlot_hash);
317         seedlot_hash_combined = extend_obj(seedlot_hash_combined, unrep_list_seedlot_hash);
318         seedlot_hash_combined = extend_obj(seedlot_hash_combined, rep_list_seedlot_hash);
319         if (!jQuery.isEmptyObject(seedlot_hash_combined)){
320             if (num_seed_per_plot == ''){
321                 alert('Number of seeds per plot is required if you have selected a seedlot list!');
322                 return;
323             }
324         }
326         var unreplicated_accession_list;
327         if (unreplicated_accession_list_id != "") {
328             unreplicated_accession_list = JSON.stringify(list.getList(unreplicated_accession_list_id));
329         }
331         var replicated_accession_list;
332         if (replicated_accession_list_id != "") {
333             replicated_accession_list = JSON.stringify(list.getList(replicated_accession_list_id));
334         }
336         var treatments = []
337         if (design_type == 'splitplot'){
338             for(var i=1; i<5; i++){
339                 var treatment_value = $('#create_trial_with_treatment_name_input'+i).val();
340                 if(treatment_value != ''){
341                     treatments.push(treatment_value);
342                 }
343             }
344             //console.log(treatments);
345             var num_plants_per_treatment = $('#num_plants_per_treatment').val();
346             num_plants_per_plot = 0;
347             if (num_plants_per_treatment){
348                 num_plants_per_plot = num_plants_per_treatment*treatments.length;
349             }
350             num_subplots_per_plot = treatments.length;
351         }
353         var greenhouse_num_plants = [];
354         if (stock_list_id != "" && design_type == 'greenhouse') {
355             for (var i=0; i<stock_list_array.length; i++) {
356                 var value = jQuery("input#greenhouse_num_plants_input_" + i).val();
357                 if (value == '') {
358                     value = 1;
359                 }
360                 greenhouse_num_plants.push(value);
361             }
362             //console.log(greenhouse_num_plants);
363         }
365         //alert(design_type);
367         $.ajax({
368             type: 'POST',
369             timeout: 3000000,
370             url: '/ajax/trial/generate_experimental_design',
371             dataType: "json",
372             beforeSend: function() {
373                 $('#working_modal').modal("show");
374             },
375             data: {
376                 'project_name': name,
377                 'project_description': desc,
378                 'year': year,
379                 'trial_location': trial_location,
380                 'stock_list': stock_list,
381                 'control_list': control_list,
382                 'control_list_crbd': control_list_crbd,
383                 'design_type': design_type,
384                 'rep_count': rep_count,
385                 'block_number': block_number,
386                 'row_number': row_number,
387                 'row_number_per_block': row_number_per_block,
388                 'col_number_per_block': col_number_per_block,
389                 'col_number': col_number,
390                 'block_size': block_size,
391                 'max_block_size': max_block_size,
392                 'plot_prefix': plot_prefix,
393                 'start_number': start_number,
394                 'increment': increment,
395                 'greenhouse_num_plants': JSON.stringify(greenhouse_num_plants),
396                 'fieldmap_col_number': fieldmap_col_number,
397                 'fieldmap_row_number': fieldmap_row_number,
398                 'plot_layout_format': plot_layout_format,
399                 'treatments':treatments,
400                 'num_plants_per_plot':num_plants_per_plot,
401                 'row_in_design_number': row_in_design_number,
402                 'col_in_design_number': col_in_design_number,
403                 'no_of_rep_times': no_of_rep_times,
404                 'no_of_block_sequence': no_of_block_sequence,
405                 'unreplicated_accession_list': unreplicated_accession_list,
406                 'replicated_accession_list': replicated_accession_list,
407                 'no_of_sub_block_sequence': no_of_sub_block_sequence,
408                 'seedlot_hash': JSON.stringify(seedlot_hash_combined),
409                 'num_seed_per_plot': num_seed_per_plot,
410             },
411             success: function (response) {
412                 $('#working_modal').modal("hide");
413                 if (response.error) {
414                     alert(response.error);
415                 } else {
417                     $('#trial_design_information').html(response.design_info_view_html);
418                     var layout_view = JSON.parse(response.design_layout_view_html);
419                     //console.log(layout_view);
420                     var layout_html = '';
421                     for (var i=0; i<layout_view.length; i++) {
422                         //console.log(layout_view[i]);
423                         layout_html += layout_view[i] + '<br>';
424                     }
425                     $('#trial_design_view_layout_return').html(layout_html);
426                     //$('#trial_design_view_layout_return').html(response.design_layout_view_html);
428                     $('#working_modal').modal("hide");
429                     $('#trial_design_confirm').modal("show");
430                     design_json = response.design_json;
431                 }
432             },
433             error: function () {
434                 $('#working_modal').modal("hide");
435                 alert('An error occurred. sorry.');
436             }
437        });
438     }
440     //When the user submits the form, input validation happens here before proceeding to design generation
441     $(document).on('click', '#new_trial_submit', function () {
442         var name = $('#new_trial_name').val();
443         var year = $('#add_project_year').val();
444         var desc = $('textarea#add_project_description').val();
445         var method_to_use = $('.format_type:checked').val();
446         if (name == '') {
447             alert('Trial name required');
448             return;
449         }
450         if (year === '' || desc === '') {
451             alert('Year and description are required.');
452             return;
453         }
454         if (stock_list_verified == 1 && seedlot_list_verified == 1){
455             if (method_to_use == "empty") {
456                 alert('adding a project');
457                 save_project_info(name, year, desc);
458             }
459             if (method_to_use == "create_with_design_tool") {
460                 generate_experimental_design();
461             }
462         } else {
463             alert('Accession list or seedlot list is not valid!');
464             return;
465         }
466     });
468     $(document).on('change', '#select_design_method', function () {
469         //$("#add_project_dialog").dialog("option", "height","auto");
471         var design_method = $("#select_design_method").val();
472         if (design_method == "CRD") {
473             $("#trial_design_more_info").show();
474             $("#trial_multi-design_more_info").show();
475             $("#FieldMap").show();
476             $("#prephelp").hide();
477             //$("#fieldmap_options").show();
478             //$("#add_project_dialog").dialog("option", "height","auto");
479             $("#show_list_of_accession_section").show();
480             $("#show_list_of_unrep_accession").hide();
481             $("#show_list_of_rep_accession").hide();
482             $("#show_no_of_row_in_design").hide();
483             $("#show_no_of_col_in_design").hide();
484             $("#show_no_of_rep_times").hide();
485             $("#show_no_of_block_sequence").hide();
486             $("#show_no_of_sub_block_sequence").hide();
487             $("#show_list_of_checks_section").hide();
488             $("#crbd_show_list_of_checks_section").show();
489             $("#rep_count_section").show();
490             $("#block_number_section").hide();
491             $("#block_size_section").hide();
492             $("#max_block_section").hide();
493             $("#row_number_section").hide();
494             $("#row_number_per_block_section").hide();
495             $("#col_number_per_block_section").hide();
496             $("#col_number_section").hide();
497             $("#row_number_per_block_section").hide();
498             $("#other_parameter_section").hide();
499             $("#design_info").show();
500             $("#greenhouse_num_plants_per_accession_section").hide();
501             $('#greenhouse_default_num_plants_per_accession').hide();
502             $("#create_trial_with_treatment_section").hide();
503             $("#num_plants_per_plot_section").hide();
504         } else if (design_method == "RCBD") {
505             $("#trial_design_more_info").show();
506             $("#trial_multi-design_more_info").show();
507             $("#FieldMap").show();
508             $("#prephelp").hide();
509             //$("#add_project_dialog").dialog("option", "height","auto");
510             $("#show_list_of_accession_section").show();
511             $("#show_list_of_unrep_accession").hide();
512             $("#show_list_of_rep_accession").hide();
513             $("#show_no_of_row_in_design").hide();
514             $("#show_no_of_col_in_design").hide();
515             $("#show_no_of_rep_times").hide();
516             $("#show_no_of_block_sequence").hide();
517             $("#show_no_of_sub_block_sequence").hide();
518             $("#crbd_show_list_of_checks_section").show();
519             $("#show_list_of_checks_section").hide();
520             $("#rep_count_section").hide();
521             $("#block_number_section").show();
522             $("#block_size_section").hide();
523             $("#max_block_size_section").hide();
524             $("#row_number_section").hide();
525             $("#row_number_per_block_section").hide();
526             $("#col_number_per_block_section").hide();
527             $("#col_number_section").hide();
528             $("#row_number_per_block_section").hide();
529             $("#other_parameter_section").hide();
530             $("#design_info").show();
531             $("#greenhouse_num_plants_per_accession_section").hide();
532             $('#greenhouse_default_num_plants_per_accession').hide();
533             $("#create_trial_with_treatment_section").hide();
534             $("#num_plants_per_plot_section").hide();
535         } else if (design_method == "Alpha") {
536             $("#FieldMap").show();
537             $("#prephelp").hide();
538             $("#trial_design_more_info").show();
539             $("#trial_multi-design_more_info").show();
540             //$("#add_project_dialog").dialog("option", "height","auto");
541             $("#show_list_of_accession_section").show();
542             $("#show_list_of_unrep_accession").hide();
543             $("#show_list_of_rep_accession").hide();
544             $("#show_no_of_row_in_design").hide();
545             $("#show_no_of_col_in_design").hide();
546             $("#show_no_of_rep_times").hide();
547             $("#show_no_of_block_sequence").hide();
548             $("#show_no_of_sub_block_sequence").hide();
549             $("#crbd_show_list_of_checks_section").show();
550             $("#show_list_of_checks_section").hide();
551             $("#rep_count_section").show();
552             $("#block_number_section").hide();
553             $("#block_size_section").show();
554             $("#max_block_size_section").hide();
555             $("#row_number_section").hide();
556             $("#row_number_per_block_section").hide();
557             $("#col_number_per_block_section").hide();
558             $("#col_number_section").hide();
559             $("#row_number_per_block_section").hide();
560             $("#other_parameter_section").hide();
561             $("#design_info").show();
562             $("#greenhouse_num_plants_per_accession_section").hide();
563             $('#greenhouse_default_num_plants_per_accession').hide();
564             $("#create_trial_with_treatment_section").hide();
565             $("#num_plants_per_plot_section").hide();
566         } else if (design_method == "Lattice") {
567             $("#FieldMap").show();
568             $("#prephelp").hide();
569             $("#trial_design_more_info").show();
570             $("#trial_multi-design_more_info").show();
571             $("#crbd_show_list_of_checks_section").show();
572             $("#show_list_of_accession_section").show();
573             $("#show_list_of_unrep_accession").hide();
574             $("#show_list_of_rep_accession").hide();
575             $("#show_no_of_row_in_design").hide();
576             $("#show_no_of_col_in_design").hide();
577             $("#show_no_of_rep_times").hide();
578             $("#show_no_of_block_sequence").hide();
579             $("#show_no_of_sub_block_sequence").hide();
580             $("#show_list_of_checks_section").hide();
581             $("#rep_count_section").show();
582             $("#block_number_section").hide();
583             $("#block_size_section").hide();
584             $("#max_block_size_section").hide();
585             $("#row_number_section").hide();
586             $("#row_number_per_block_section").hide();
587             $("#col_number_per_block_section").hide();
588             $("#col_number_section").hide();
589             $("#row_number_per_block_section").hide();
590             $("#other_parameter_section").hide();
591             $("#design_info").show();
592             $("#greenhouse_num_plants_per_accession_section").hide();
593             $('#greenhouse_default_num_plants_per_accession').hide();
594             $("#create_trial_with_treatment_section").hide();
595             $("#num_plants_per_plot_section").hide();
596         } else if (design_method == "Augmented") {
597             $("#FieldMap").hide();
598             $("#prephelp").hide();
599             $("#trial_design_more_info").show();
600             $("#trial_multi-design_more_info").show();
601             //$("#add_project_dialog").dialog("option", "height","auto");
602             $("#show_list_of_checks_section").show();
603             $("#crbd_show_list_of_checks_section").hide();
604             $("#show_list_of_accession_section").show();
605             $("#show_list_of_unrep_accession").hide();
606             $("#show_list_of_rep_accession").hide();
607             $("#show_no_of_row_in_design").hide();
608             $("#show_no_of_col_in_design").hide();
609             $("#show_no_of_rep_times").hide();
610             $("#show_no_of_block_sequence").hide();
611             $("#show_no_of_sub_block_sequence").hide();
612             $("#rep_count_section").hide();
613             $("#block_number_section").hide();
614             $("#block_size_section").hide();
615             $("#max_block_size_section").show();
616             $("#row_number_section").hide();
617             $("#row_number_per_block_section").hide();
618             $("#col_number_per_block_section").hide();
619             $("#col_number_section").hide();
620             $("#row_number_per_block_section").hide();
621             $("#other_parameter_section").hide();
622             $("#design_info").show();
623             $("#greenhouse_num_plants_per_accession_section").hide();
624             $('#greenhouse_default_num_plants_per_accession').hide();
625             $("#create_trial_with_treatment_section").hide();
626             $("#num_plants_per_plot_section").hide();
627         } else if (design_method == "") {
628             $("#FieldMap").hide();
629             $("#prephelp").hide();
630             //$("#add_project_dialog").dialog("option", "height","auto");
631             $("#show_list_of_checks_section").hide();
632             $("#crbd_show_list_of_checks_section").hide();
633             $("#show_list_of_accession_section").hide();
634             $("#show_list_of_unrep_accession").hide();
635             $("#show_list_of_rep_accession").hide();
636             $("#show_no_of_row_in_design").hide();
637             $("#show_no_of_col_in_design").hide();
638             $("#show_no_of_rep_times").hide();
639             $("#show_no_of_block_sequence").hide();
640             $("#show_no_of_sub_block_sequence").hide();
641             $("#trial_design_more_info").hide();
642             $("#trial_multi-design_more_info").hide();
643             $("#rep_count_section").hide();
644             $("#block_number_section").hide();
645             $("#block_size_section").hide();
646             $("#max_block_size_section").hide();
647             $("#row_number_section").hide();
648             $("#row_number_per_block_section").hide();
649             $("#col_number_per_block_section").hide();
650             $("#col_number_section").hide();
651             $("#row_number_per_block_section").show();
652             $("#other_parameter_section").hide();
653             $("#other_parameter_section2").hide();
654             $("#design_info").show();
655             $("#greenhouse_num_plants_per_accession_section").hide();
656             $('#greenhouse_default_num_plants_per_accession').hide();
657             $("#create_trial_with_treatment_section").hide();
658             $("#num_plants_per_plot_section").hide();
659         }
661         else if (design_method == "MAD") {
662             $("#FieldMap").hide();
663             $("#prephelp").hide();
664             $("#trial_design_more_info").show();
665             $("#trial_multi-design_more_info").show();
666             //$("#add_project_dialog").dialog("option", "height","auto");
667             $("#show_list_of_checks_section").show();
668             $("#crbd_show_list_of_checks_section").hide();
669             $("#show_list_of_accession_section").show();
670             $("#show_list_of_unrep_accession").hide();
671             $("#show_list_of_rep_accession").hide();
672             $("#show_no_of_row_in_design").hide();
673             $("#show_no_of_col_in_design").hide();
674             $("#show_no_of_rep_times").hide();
675             $("#show_no_of_block_sequence").hide();
676             $("#show_no_of_sub_block_sequence").hide();
677             $("#rep_count_section").hide();
678             $("#row_number_section").show();
679             $("#block_number_section").hide();
680             $("#block_size_section").hide();
681             $("#row_number_per_block_section").show();
682             $("#col_number_per_block_section").show();
683             $("#col_number_section").show();
684             $("#max_block_size_section").hide();
685             $("#row_number_per_block_section").show();
686             $("#other_parameter_section").show();
687             $("#design_info").show();
689             $("#show_other_parameter_options").click(function () {
690                 if ($('#show_other_parameter_options').is(':checked')) {
691                     $("#other_parameter_options").show();
692                     //$("#add_project_dialog").dialog("option", "height","auto");
693                 }
694                 else {
695                     $("#other_parameter_options").hide();
696                     //$("#add_project_dialog").dialog("option", "height","auto");
697                 }
698             });
699             $("#greenhouse_num_plants_per_accession_section").hide();
700             $('#greenhouse_default_num_plants_per_accession').hide();
701             $("#create_trial_with_treatment_section").hide();
702             $("#num_plants_per_plot_section").hide();
703         }
705         else if (design_method == 'greenhouse') {
706             $("#FieldMap").hide();
707             $("#prephelp").hide();
708             $("#trial_design_more_info").show();
709             $("#trial_multi-design_more_info").show();
710             $("#show_list_of_checks_section").hide();
711             $("#crbd_show_list_of_checks_section").hide();
712             $("#show_list_of_accession_section").show();
713             $("#show_list_of_unrep_accession").hide();
714             $("#show_list_of_rep_accession").hide();
715             $("#show_no_of_row_in_design").hide();
716             $("#show_no_of_col_in_design").hide();
717             $("#show_no_of_rep_times").hide();
718             $("#show_no_of_block_sequence").hide();
719             $("#show_no_of_sub_block_sequence").hide();
720             $("#rep_count_section").hide();
721             $("#block_number_section").hide();
722             $("#block_size_section").hide();
723             $("#max_block_section").hide();
724             $("#row_number_section").hide();
725             $("#row_number_per_block_section").hide();
726             $("#col_number_per_block_section").hide();
727             $("#col_number_section").hide();
728             $("#row_number_per_block_section").hide();
729             $("#other_parameter_section").hide();
730             $("#design_info").hide();
731             $('#greenhouse_default_num_plants_per_accession').show();
732             $("#greenhouse_num_plants_per_accession_section").show();
733             $('#greenhouse_default_num_plants_per_accession').show();
734             $("#create_trial_with_treatment_section").hide();
735             $("#num_plants_per_plot_section").hide();
736             greenhouse_show_num_plants_section();
737         }
739         else if (design_method == 'splitplot') {
740             $("#FieldMap").show();
741             $("#prephelp").hide();
742             $("#trial_design_more_info").show();
743             $("#trial_multi-design_more_info").show();
744             $("#show_list_of_checks_section").hide();
745             $("#crbd_show_list_of_checks_section").hide();
746             $("#show_list_of_accession_section").show();
747             $("#show_list_of_unrep_accession").hide();
748             $("#show_list_of_rep_accession").hide();
749             $("#show_no_of_row_in_design").hide();
750             $("#show_no_of_col_in_design").hide();
751             $("#show_no_of_rep_times").hide();
752             $("#show_no_of_block_sequence").hide();
753             $("#show_no_of_sub_block_sequence").hide();
754             $("#rep_count_section").hide();
755             $("#block_number_section").show();
756             $("#block_size_section").hide();
757             $("#max_block_section").hide();
758             $("#row_number_section").hide();
759             $("#row_number_per_block_section").hide();
760             $("#col_number_per_block_section").hide();
761             $("#col_number_section").hide();
762             $("#row_number_per_block_section").hide();
763             $("#other_parameter_section").hide();
764             $("#design_info").show();
765             $('#greenhouse_default_num_plants_per_accession').hide();
766             $("#greenhouse_num_plants_per_accession_section").hide();
767             $('#greenhouse_default_num_plants_per_accession').hide();
768             $("#create_trial_with_treatment_section").show();
769             $("#num_plants_per_plot_section").show();
770         }
771         else if (design_method == 'p-rep') {
772             $("#FieldMap").hide();
773             $("#trial_design_more_info").show();
774             $("#trial_multi-design_more_info").show();
775             $("#prephelp").show();
776             $("#show_list_of_accession_section").hide();
777             $("#show_list_of_checks_section").hide();
778             $("#show_list_of_unrep_accession").show();
779             $("#show_list_of_rep_accession").show();
780             $("#show_no_of_row_in_design").show();
781             $("#show_no_of_col_in_design").show();
782             $("#show_no_of_rep_times").show();
783             $("#show_no_of_block_sequence").show();
784             $("#show_no_of_sub_block_sequence").show();
785             $("#crbd_show_list_of_checks_section").hide();
786             $("#rep_count_section").hide();
787             $("#block_number_section").hide();
788             $("#block_size_section").hide();
789             $("#max_block_section").hide();
790             $("#row_number_section").hide();
791             $("#row_number_per_block_section").hide();
792             $("#col_number_per_block_section").hide();
793             $("#col_number_section").hide();
794             $("#row_number_per_block_section").hide();
795             $("#other_parameter_section").hide();
796             $("#design_info").hide();
797             $('#greenhouse_default_num_plants_per_accession').hide();
798             $("#greenhouse_num_plants_per_accession_section").hide();
799             $('#greenhouse_default_num_plants_per_accession').hide();
800             $("#create_trial_with_treatment_section").hide();
801             $("#num_plants_per_plot_section").hide();
802         }
804         else {
805             alert("Unsupported design method");
806         }
807     });
809     jQuery(document).on('change', '#select_list_list_select', function() {
810         if (jQuery("#select_design_method").val() == 'greenhouse') {
811             greenhouse_show_num_plants_section();
812         }
813     });
814     jQuery(document).on('keyup', '#greenhouse_default_num_plants_per_accession_val', function() {
815         if (jQuery("#select_design_method").val() == 'greenhouse') {
816             greenhouse_show_num_plants_section();
817         }
818     });
820     jQuery(document).on('keyup', '#greenhouse_default_num_plants_per_accession_val', function() {
821         if (jQuery("#select_design_method").val() == 'greenhouse') {
822             greenhouse_show_num_plants_section();
823         }
824     });
826     $("#show_plot_naming_options").click(function () {
827         if ($('#show_plot_naming_options').is(':checked')) {
828             $("#plot_naming_options").show();
829             //$("#add_project_dialog").dialog("option", "height","auto");
830         }
831         else {
832             $("#plot_naming_options").hide();
833             //$("#add_project_dialog").dialog("option", "height","auto");
834         }
835     });
837     $("#show_field_map_options").click(function () {
838       if ($('#show_field_map_options').is(':checked')) {
839         $("#field_map_options").show();
840       }
841       else {
842         $("#field_map_options").hide();
843       }
844     });
846     function save_experimental_design(design_json) {
847         var list = new CXGN.List();
848         var name = jQuery('#new_trial_name').val();
849         var year = jQuery('#add_project_year').val();
850         var desc = jQuery('#add_project_description').val();
851         var trial_location = jQuery('#add_project_location').val();
852         var block_number = jQuery('#block_number').val();
853         var stock_list_id = jQuery('#select_list_list_select').val();
854         var control_list_id = jQuery('#list_of_checks_section_list_select').val();
855         var stock_list;
856         if (stock_list_id != "") {
857             stock_list_array = list.getList(stock_list_id);
858             stock_list = JSON.stringify(list.getList(stock_list_id));
859         }
860         var control_list;
861         if (control_list_id != "") {
862            control_list = JSON.stringify(list.getList(control_list_id));
863         }
864         var design_type = jQuery('#select_design_method').val();
865         if (design_type == "") {
866             var design_type = jQuery('#select_multi-design_method').val();
867         }
868         var greenhouse_num_plants = [];
869         if (stock_list_id != "" && design_type == 'greenhouse') {
870             for (var i=0; i<stock_list_array.length; i++) {
871                 var value = jQuery("input#greenhouse_num_plants_input_" + i).val();
872                 if (value == '') {
873                     value = 1;
874                 }
875                 greenhouse_num_plants.push(value);
876             }
877             //console.log(greenhouse_num_plants);
878         }
880         //alert(design_type);
882         var rep_count = jQuery('#rep_count').val();
883         var block_size = jQuery('#block_size').val();
884         var max_block_size = jQuery('#max_block_size').val();
885         var plot_prefix = jQuery('#plot_prefix').val();
886         var start_number = jQuery('#start_number').val();
887         var increment = jQuery('#increment').val();
888         var breeding_program_name = jQuery('#select_breeding_program').val();
889         var fieldmap_col_number = jQuery('#fieldMap_col_number').val();
890         var fieldmap_row_number = jQuery('#fieldMap_row_number').val();
891         var plot_layout_format = jQuery('#plot_layout_format').val();
892         var trial_type = jQuery('#add_project_type').val();
894         jQuery.ajax({
895            type: 'POST',
896            timeout: 3000000,
897            url: '/ajax/trial/save_experimental_design',
898            dataType: "json",
899            beforeSend: function() {
900                jQuery('#working_modal').modal("show");
901            },
902            data: {
903                 'project_name': name,
904                 'project_description': desc,
905                 //'trial_name': trial_name,
906                 'year': year,
907                 'trial_type': trial_type,
908                 'trial_location': trial_location,
909                 'stock_list': stock_list,
910                 'control_list': control_list,
911                 'design_type': design_type,
912                 'rep_count': rep_count,
913                 'block_number': block_number,
914                 'block_size': block_size,
915                 'max_block_size': max_block_size,
916                 'plot_prefix': plot_prefix,
917                 'start_number': start_number,
918                 'increment': increment,
919                 'design_json': design_json,
920                 'breeding_program_name': breeding_program_name,
921                 'greenhouse_num_plants': JSON.stringify(greenhouse_num_plants),
922                 'fieldmap_col_number': fieldmap_col_number,
923                 'fieldmap_row_number': fieldmap_row_number,
924                 'plot_layout_format': plot_layout_format,
925                 'has_plant_entries': num_plants_per_plot,
926                 'has_subplot_entries': num_subplots_per_plot,
927             },
928             success: function (response) {
929                 if (response.error) {
930                     jQuery('#working_modal').modal("hide");
931                     alert(response.error);
932                     jQuery('#trial_design_confirm').modal("hide");
933                 } else {
934                     //alert('Trial design saved');
935                     jQuery('#working_modal').modal("hide");
936                     jQuery('#trial_saved_dialog_message').modal("show");
937                 }
938             },
939             error: function () {
940                 jQuery('#trial_saving_dialog').dialog("close");
941                 alert('An error occurred saving the trial.');
942                 jQuery('#trial_design_confirm').dialog("close");
943             }
944         });
945     }
947     jQuery('#new_trial_confirm_submit').click(function () {
948             save_experimental_design(design_json);
949     });
951     $('#view_trial_layout_button').click(function () {
952         $('#trial_design_view_layout').modal("show");
953     });
955     $('#redo_trial_layout_button').click(function () {
956         generate_experimental_design();
957         $('#trial_design_view_layout').modal("show");
958     });
960     function open_project_dialog() {
961         $('#add_project_dialog').modal("show");
963         //add lists to the list select and list of checks select dropdowns.
964     document.getElementById("select_list").innerHTML = list.listSelect("select_list", [ 'accessions' ], '', 'refresh');
965     document.getElementById("select_seedlot_list").innerHTML = list.listSelect("select_seedlot_list", [ 'seedlots' ], 'none', 'refresh');
966     document.getElementById("list_of_checks_section").innerHTML = list.listSelect("list_of_checks_section", [ 'accessions' ], '', 'refresh');
968     //add lists to the list select and list of checks select dropdowns for CRBD.
969     document.getElementById("crbd_list_of_checks_section").innerHTML = list.listSelect("crbd_list_of_checks_section", [ 'accessions' ], "select optional check list", 'refresh');
970     document.getElementById("list_of_unrep_accession").innerHTML = list.listSelect("list_of_unrep_accession", [ 'accessions' ], "Required: e.g. 200", 'refresh');
971     document.getElementById("list_of_rep_accession").innerHTML = list.listSelect("list_of_rep_accession", [ 'accessions' ], "Required: e.g. 119", 'refresh');
973         //add a blank line to location select dropdown that dissappears when dropdown is opened
974         $("#add_project_location").prepend("<option value=''></option>").val('');
975         $("#add_project_location").one('mousedown', function () {
976             $("option:first", this).remove();
977         });
979         //add a blank line to list select dropdown that dissappears when dropdown is opened
980         $("#select_list_list_select").prepend("<option value=''></option>").val('');
981         $("#select_list_list_select").one('mousedown', function () {
982             $("option:first", this).remove();
983         });
985     //add a blank line to list select dropdown that dissappears when dropdown is opened
986         $("#select_seedlot_list_list_select").prepend("<option value=''></option>").val('');
987         $("#select_seedlot_list_list_select").one('mousedown', function () {
988             $("option:first", this).remove();
989         });
992         //add a blank line to list of checks select dropdown that dissappears when dropdown is opened
993         $("#list_of_checks_section_list_select").prepend("<option value=''></option>").val('');
994         $("#list_of_checks_section_list_select").one('mousedown', function () {
995             $("option:first", this).remove();
996         });
998   $("#crbd_list_of_checks_section_list_select").prepend("<option value=''></option>").val('');
999   $("#crbd_list_of_checks_section_list_select").one('mousedown', function () {
1000             $("option:first", this).remove();
1001   });
1003         //add a blank line to design method select dropdown that dissappears when dropdown is opened
1004         $("#select_design_method").prepend("<option value=''></option>").val('');
1005         $("#select_design_method").one('mousedown', function () {
1006             $("option:first", this).remove();
1007             $("#trial_design_more_info").show();
1008             //$("#add_project_dialog").dialog("option", "height","auto");
1009             //trigger design method change events in case the first one is selected after removal of the first blank select item
1010             $("#select_design_method").change();
1011         });
1013         //reset previous selections
1014         $("#select_design_method").change();
1016         var method_to_use = $('.format_type:checked').val();
1017         if (method_to_use == "empty") {
1018             $("#trial_design_info").hide();
1019             $("#trial_design_more_info").hide();
1020             $("#get_file_upload_data").hide();
1021         }
1022         if (method_to_use == "create_with_upload") {
1023             $("#get_file_upload_data").show();
1024             $("#trial_design_info").hide();
1025             $("#trial_design_more_info").hide();
1026         } else {
1027             $("#get_file_upload_data").hide();
1028         }
1029         if (method_to_use == "create_with_design_tool") {
1030             $("#trial_design_info").show();
1031         } else {
1032             $("trial_design_info").hide();
1033         }
1034     }
1036      $('#add_project_link').click(function () {
1037          get_select_box('years', 'add_project_year', {'auto_generate': 1 });
1038          get_select_box('trial_types', 'add_project_type', {'empty':1} );
1039          open_project_dialog();
1040      });
1042     jQuery('button[name="new_trial_add_treatments"]').click(function(){
1043         jQuery('#trial_design_add_treatments').modal('show');
1044     });
1046     jQuery('#new_trial_add_treatments_continue').click(function(){
1047         var treatment_name = jQuery('#new_treatment_name').val();
1048         var html = "";
1049         var design_array = JSON.parse(design_json);
1050         for (var i=0; i<design_array.length; i++){
1051             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+" [Select all <input type='checkbox' name='add_trial_treatment_select_all' />]</th></tr></thead><tbody>";
1052             var design_hash = JSON.parse(design_array[i]);
1053             //console.log(design_hash);
1054             for (var key in design_hash){
1055                 if (key != 'treatments'){
1056                     var plot_obj = design_hash[key];
1057                     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+"'  data-plant_names='"+JSON.stringify(plot_obj.plant_names)+"' data-subplot_names='"+JSON.stringify(plot_obj.subplots_names)+"' type='checkbox' name='add_trial_treatment_input'/></td></tr>";
1058                 }
1059             }
1060             html += "</tbody></table>";
1061         }
1062         html += "<br/><br/>";
1063         jQuery('#trial_design_add_treatment_select_html').html(html);
1064         jQuery('#trial_design_add_treatment_select').modal('show');
1065     });
1067     jQuery(document).on('change', 'input[name="add_trial_treatment_select_all"]', function(){
1068         if(jQuery(this).is(":checked")){
1069             jQuery('input[name="add_trial_treatment_input"]').each(function(){
1070                 jQuery(this).prop("checked", true);
1071             });
1072         } else {
1073             jQuery('input[name="add_trial_treatment_input"]').each(function(){
1074                 jQuery(this).prop("checked", false);
1075             });
1076         }
1077     });
1079     jQuery('#new_trial_add_treatments_submit').click(function(){
1080         var trial_treatments = [];
1081         jQuery('input[name="add_trial_treatment_input"]').each(function() {
1082             if (this.checked){
1083                 var plot_name = jQuery(this).data('plot_name');
1084                 var plant_names = jQuery(this).data('plant_names');
1085                 var subplot_names = jQuery(this).data('subplot_names');
1086                 var trial_index = jQuery(this).data('trial_index');
1087                 var trial_treatment = jQuery(this).data('trial_treatment');
1088                 if (trial_index in trial_treatments){
1089                     var trial = trial_treatments[trial_index];
1090                     if (trial_treatment in trial){
1091                         trial[trial_treatment].push(plot_name);
1092                     } else {
1093                         trial[trial_treatment] = [plot_name];
1094                     }
1095                     if (plant_names != 'undefined'){
1096                         for(var i=0; i<plant_names.length; i++){
1097                             trial[trial_treatment].push(plant_names[i]);
1098                         }
1099                     }
1100                     if (subplot_names != 'undefined'){
1101                         for(var i=0; i<subplot_names.length; i++){
1102                             trial[trial_treatment].push(subplot_names[i]);
1103                         }
1104                     }
1105                     trial_treatments[trial_index] = trial;
1106                 } else {
1107                     obj = {};
1108                     obj[trial_treatment] = [plot_name];
1109                     if (plant_names != 'undefined'){
1110                         for(var i=0; i<plant_names.length; i++){
1111                             obj[trial_treatment].push(plant_names[i]);
1112                         }
1113                     }
1114                     if (subplot_names != 'undefined'){
1115                         for(var i=0; i<subplot_names.length; i++){
1116                             obj[trial_treatment].push(subplot_names[i]);
1117                         }
1118                     }
1119                     trial_treatments[trial_index] = obj;
1120                 }
1121             }
1122         });
1124         var new_design_array = [];
1125         var design_array = JSON.parse(design_json);
1126         for (var i=0; i<design_array.length; i++){
1127             var design_hash = JSON.parse(design_array[i]);
1128             if ('treatments' in design_hash){
1129                 treatment_obj = design_hash['treatments'];
1130                 new_treatments = trial_treatments[i];
1131                 for (var key in new_treatments){
1132                     treatment_obj[key] = new_treatments[key];
1133                 }
1134                 design_hash['treatments'] = treatment_obj;
1135             } else {
1136                 design_hash['treatments'] = trial_treatments[i];
1137             }
1138             new_design_array[i] = JSON.stringify(design_hash);
1139         }
1140         design_json = JSON.stringify(new_design_array);
1142         var html = '';
1143         for (var i=0; i<new_design_array.length; i++){
1144             var design_hash = JSON.parse(new_design_array[i]);
1145             var treatments = design_hash['treatments'];
1146             //html += "Trial "+i+"<br/>";
1147             for (var key in treatments){
1148                 html += "Treatment: <b>"+key+"</b> Plots: ";
1149                 var plot_array = treatments[key];
1150                 html += plot_array.join(', ') + "<br/>";
1151             }
1152         }
1153         jQuery('#trial_design_confirm_treatments').html(html);
1154         jQuery('#trial_design_add_treatment_select').modal('hide');
1155         jQuery('#trial_design_add_treatments').modal('hide');
1156     });
1160 function greenhouse_show_num_plants_section(){
1161     var list = new CXGN.List();
1162     var stock_list_id = jQuery('#select_list_list_select').val();
1163     var default_num = jQuery('#greenhouse_default_num_plants_per_accession_val').val();
1164     if (stock_list_id != "") {
1165         stock_list = list.getList(stock_list_id);
1166         //console.log(stock_list);
1167         var html = '<form class="form-horizontal">';
1168         for (var i=0; i<stock_list.length; i++){
1169             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>';
1170         }
1171         html = html + '</form>';
1172         jQuery("#greenhouse_num_plants_per_accession").empty().html(html);
1173     }
1176 function extend_obj(obj, src) {
1177     for (var key in src) {
1178         if (src.hasOwnProperty(key)) obj[key] = src[key];
1179     }
1180     return obj;