Merge pull request #2754 from solgenomics/topic/fix_homepage_add_accessions_dialog
[sgn.git] / mason / breeders_toolbox / cross_wishlist.mas
blob79cddb4e918d872f4b82ef76de0edc15119259a9
2 <style>
3 @media (min-width: 768px) {
4   .modal-xl {
5     width: 90%;
6    max-width:1200px;
7   }
9 </style>
11 <div class="modal fade" id="create_cross_wishlist_dialog" name="create_cross_wishlist_dialog" tabindex="-1" role="dialog" aria-labelledby="createCrossWishlistDialog">
12     <div class="modal-dialog modal-xl" role="document">
13         <div class="modal-content">
14             <div class="modal-header">
15                 <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
16                 <h4 class="modal-title" id="createCrossWishlistDialog">Create Cross Wishlist</h4>
17             </div>
19             <div class="modal-body">
20                 <div class="container-fluid">
21                     <div class="well">
22                         <form class="form-horizontal" id="cross_wishlist_create_select_form_id" name="cross_wishlist_create_select_form_id">
23                             <div class="form-group">
24                                 <label class="col-sm-4 control-label">Select An ODK Form on ONA: </label>
25                                 <div class="col-sm-8" >
26                                     <div id="cross_wishlist_create_select_form_id_div">LOADING...</div>
27                                 </div>
28                             </div>
29                         </form>
30                     </div>
32                     <ul class="nav nav-pills nav-justified" id="cross_wishlist_tab_select">
33                         <li id="cross_wishlist_list_tab"><a data-toggle="tab" href="#with_lists">Using Accession Lists</a></li>
34                         <li class="active" id="cross_wishlist_no_list_tab"><a data-toggle="tab" href="#not_with_lists">Using Crossing Blocks</a></li>
35                     </ul>
37                     <div class="tab-content">
39                         <div id="with_lists" class="tab-pane fade">
40                             <hr>
41                             <form class="form-horizontal" >
42                                 <div class="form-group" style="display:none" <!-- location name is coming from the trial they select --> >
43                                     <label class="col-sm-3 control-label">Location Name (Optional): </label>
44                                     <div class="col-sm-9" >
45                                         <div id="cross_wishlist_location_select_list_div">
46                                         </div>
47                                     </div>
48                                 </div>
49                                 <div class="form-group">
50                                     <label class="col-sm-3 control-label">Female's Trial Name: </label>
51                                     <div class="col-sm-9" >
52                                         <div id="cross_wishlist_female_trial_select_list_div">
53                                         </div>
54                                     </div>
55                                 </div>
56                                 <div class="form-group">
57                                     <label class="col-sm-3 control-label">Male's Trial Name: </label>
58                                     <div class="col-sm-9" >
59                                         <div id="cross_wishlist_male_trial_select_list_div">
60                                         </div>
61                                     </div>
62                                 </div>
63                                 <div class="well well-sm">
64                                     <div class="form-group">
65                                         <label class="col-sm-6 control-label">Cross plan will be added to the cross wishlist named: </label>
66                                         <div class="col-sm-6" >
67                                             <div id="cross_wishlist_trial_select_list_cross_wishlist_name_div">
68                                                 <input class="form-control" type="text" disabled value="Please Select a Trial First" />
69                                             </div>
70                                         </div>
71                                     </div>
72                                 </div>
73                                 <div class="form-group">
74                                     <label class="col-sm-3 control-label">Female Accession List: </label>
75                                     <div class="col-sm-9" id="female_accession_list_div" name="female_accession_list_div">
76                                     </div>
77                                 </div>
78                                 <div class="form-group">
79                                     <label class="col-sm-3 control-label">Male Accession List: </label>
80                                     <div class="col-sm-9" id="male_accession_list_div" name="male_accession_list_div">
81                                     </div>
82                                 </div>
83                             </form>
84                             <br/>
85                             <div id="cross_wishlist_cross_grid_div"></div>
86                         </div>
88                         <div id="not_with_lists" class="tab-pane fade in active">
89                             <hr>
91                             <form class="form-horizontal" >
92                                 <div class="form-group">
93                                     <label class="col-sm-3 control-label">Female's Trial Name: </label>
94                                     <div class="col-sm-9" >
95                                         <div id="cross_wishlist_female_trial_select_no_list_div">
96                                         </div>
97                                     </div>
98                                 </div>
99                                 <div class="form-group">
100                                     <label class="col-sm-3 control-label">Male's Trial Name: </label>
101                                     <div class="col-sm-9" >
102                                         <div id="cross_wishlist_male_trial_select_no_list_div">
103                                         </div>
104                                     </div>
105                                 </div>
106                                 <div class="well well-sm">
107                                     <div class="form-group">
108                                         <label class="col-sm-6 control-label">Cross plan will be added to the cross wishlist named: </label>
109                                         <div class="col-sm-6" >
110                                             <div id="cross_wishlist_trial_select_no_list_cross_wishlist_name_div">
111                                                 <input class="form-control" type="text" disabled value="Please Select a Trial First" />
112                                             </div>
113                                         </div>
114                                     </div>
115                                 </div>
116                                 <div class="form-group">
117                                     <label class="col-sm-3 control-label">Female Accession Name(s):</label>
118                                     <div class="col-sm-9" >
119                                         <div id="cross_wishlist_female_accession_select_div">
120                                             <p>First Select A Trial</p>
121                                         </div>
122                                     </div>
123                                 </div>
124                                 <div id ="cross_wishlist_male_parent_div">
125                                 </div>
126                             </form>
127                         </div>
129                     </div>
130                 </div>
131             </div>
133             <div class="modal-footer">
134                 <button class="btn btn-info" id="show_cross_wishlists_select_submits" name="show_cross_wishlists_select_submits">Available Cross Wishlist(s) for ODK Use</button>
135                 <button class="btn btn-primary" id="cross_wishlist_submit" name="cross_wishlist_submits">Next</button>
136                 <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
137             </div>
138         </div>
139     </div>
140 </div>
142 <div class="modal fade" id="create_cross_wishlist_select_dialog" name="create_cross_wishlist_select_dialog" tabindex="-1" role="dialog" aria-labelledby="createCrossWishlistSelectDialog">
143     <div class="modal-dialog modal-xl" role="document">
144         <div class="modal-content">
145             <div class="modal-header">
146                 <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
147                 <h4 class="modal-title" id="createCrossWishlistSelectDialog">Select Plots for Cross Wishlist</h4>
148                 <h4>Female Plots are in <span class="bg-primary">Blue</span><!-- and Male Plots are in <span class="bg-success">Green</span>.--></h4>
149             </div>
151             <div class="modal-body">
152                 <div class="container-fluid">
153                     <div id="cross_wishlist_cross_select_div"></div>
154                 </div>
155             </div>
157             <div class="modal-footer">
158                 <button class="btn btn-primary" id="cross_wishlist_select_submits" name="cross_wishlist_select_submits">Create Wishlist</button>
159                 <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
160             </div>
161         </div>
162     </div>
163 </div>
165 <div class="modal fade" id="create_cross_wishlist_response_dialog" name="create_cross_wishlist_response_dialog" tabindex="-1" role="dialog" aria-labelledby="createCrossWishlistResponseDialog">
166     <div class="modal-dialog modal-xl" role="document">
167         <div class="modal-content">
168             <div class="modal-header">
169                 <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
170                 <h4 class="modal-title" id="createCrossWishlistResponseDialog">Cross Wishlist</h4>
171             </div>
173             <div class="modal-body">
174                 <div class="container-fluid">
175                     <div id="cross_wishlist_response_div"></div>
176                 </div>
177             </div>
179             <div class="modal-footer">
180                 <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
181             </div>
182         </div>
183     </div>
184 </div>
186 <div class="modal fade" id="show_cross_wishlists_response_dialog" name="show_cross_wishlists_response_dialog" tabindex="-1" role="dialog" aria-labelledby="showCrossWishlistsResponseDialog">
187     <div class="modal-dialog modal-xl" role="document">
188         <div class="modal-content">
189             <div class="modal-header">
190                 <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
191                 <h4 class="modal-title" id="showCrossWishlistsResponseDialog">Cross Wishlists Previously Created</h4>
192             </div>
194             <div class="modal-body">
195                 <div class="container-fluid">
196                     <div id="show_cross_wishlists_response_div"></div>
197                 </div>
198             </div>
200             <div class="modal-footer">
201                 <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
202             </div>
203         </div>
204     </div>
205 </div>
207 <script>
209 var lo = new CXGN.List();
211 function draw_cross_grid(female_accessions, male_accessions){
212     console.log("draw cross grid");
213     var cross_grid_html = '<h3>Set Cross Priorities: 1 is highest and 10 is lowest</h3><h4>Female Accessions Are in First Column and Male Accessions Are in Header</h4><table class="table table-bordered"><thead><tr><th>Female Accessions</th>';
214     for(var i=0; i<male_accessions.length; i++){
215         cross_grid_html = cross_grid_html + '<th>' + male_accessions[i] + '</th>';
216     }
217     cross_grid_html = cross_grid_html + '</tr></thead><tbody>';
218     for(var i=0; i<female_accessions.length; i++){
219         cross_grid_html = cross_grid_html + '<tr><td>' + female_accessions[i] + '</td>';
220         for(var j=0; j<male_accessions.length; j++){
221             cross_grid_html = cross_grid_html + '<td><input type="text" name="cross_wishlist_cross_grid_inputs" class="form-control" data-female="'+female_accessions[i]+'" data-male="'+male_accessions[j]+'"/></td>';
222         }
223         cross_grid_html = cross_grid_html + '</tr>';
224     }
225     cross_grid_html = cross_grid_html + '</tbody></table>';
226     jQuery('#cross_wishlist_cross_grid_div').html(cross_grid_html);
229 jQuery(document).ready(function($) {
231     jQuery('#show_cross_wishlists_select_submits').click(function(){
232         jQuery('#show_cross_wishlists_response_dialog').modal('show');
234         jQuery.ajax({
235             url: '/ajax/cross/list_cross_wishlists',
236             dataType:'json',
237             beforeSend: function() {
238                 jQuery('#working_modal').modal('show');
239             },
240             success: function(response) {
241                 //console.log(response);
242                 jQuery('#working_modal').modal('hide');
243                 var html = '';
244                 if(response.success){
245                     html += "<table class='table table-bordered table-hover'><thead><tr><th>File</th><th>Created By</th><th>Date</th></tr></thead><tbody>";
246                     for(var i=0; i<response.files.length; i++){
247                         html += "<tr><td><a href='/breeders/phenotyping/download/"+response.files[i][0]+"' >"+response.files[i][1]+"</a></td><td><a href='/solpeople/personal-info.pl?sp_person_id="+response.files[i][6]+"' >"+response.files[i][7]+" "+response.files[i][8]+"</a></td><td>"+response.files[i][5]+"</td></tr>";
248                     }
249                     html += "</tbody></table>";
250                 } else {
251                     html = 'Something went wrong here.'
252                 }
253                 jQuery('#show_cross_wishlists_response_div').html(html);
254             },
255             error: function(response) {
256                 jQuery('#working_modal').modal('hide');
257                 alert("An error occurred getting cross wishlists");
258             }
259         });
261     });
263     jQuery("#create_cross_wishlist").click(function() {
264         jQuery("#create_cross_wishlist_dialog").modal("show");
265         jQuery("#female_accession_list_div").html(lo.listSelect('female_accession_list_div', ["accessions"], "Please select list with < 100 accessions", undefined, undefined ));
266         jQuery("#male_accession_list_div").html(lo.listSelect('male_accession_list_div', ["accessions"], "Please select list with < 100 accessions", undefined, undefined ));
267         var female_accessions;
268         var male_accessions
269         if (jQuery('#female_accession_list_div_list_select').val() != '' && jQuery('#male_accession_list_div_list_select').val() != '' ) {
270             female_accessions = lo.getList(jQuery('#female_accession_list_div_list_select').val());
271             male_accessions = lo.getList(jQuery('#male_accession_list_div_list_select').val());
272         }
274         jQuery.ajax ({
275             url : '/ajax/odk/get_crossing_available_forms',
276             beforeSend: function() {
277                 jQuery("#working_modal").modal("show");
278             },
279             success: function(response){
280                 console.log(response);
281                 jQuery("#working_modal").modal("hide");
283                 if (response.error){
284                     alert(response.error);
285                 }
286                 if (response.success){
287                 
288                 
289                     jQuery.ajax ({
290                         url : '/ajax/odk/get_crossing_saved_ona_forms',
291                         success: function(response_forms){
292                             console.log(response_forms);
294                             if (response_forms.error){
295                                 alert(response.error);
296                             }
297                             if (response_forms.success){
298                             
299                                 var form_dropdown_select_html = '<select class="form-control" id="cross_wishlist_availabe_odk_crossing_forms"><option value="">Not Using ONA</option>';
300                                 var allowed_forms = response_forms.odk_ona_forms;
301                                 allowed_forms.push('<% $c->config->{odk_crossing_data_test_form_name} %>');
302                                 for(var i=0; i<response.forms.length; i++){
303                                     if (jQuery.inArray(response.forms[i].id_string, allowed_forms) != -1){
304                                         form_dropdown_select_html = form_dropdown_select_html + '<option value="'+response.forms[i].id+'">'+response.forms[i].id_string+'</option>';
305                                     }
306                                 }
307                                 form_dropdown_select_html = form_dropdown_select_html + '</select>';
308                                 jQuery('#cross_wishlist_create_select_form_id_div').html(form_dropdown_select_html);
309                             
310                             }
311                         },
312                         error: function(response_forms){
313                             alert("Error retrieving saved ODK ONA forms");
314                         }
315                     });
317                 }
318             },
319             error: function(response){
320                 jQuery("#working_modal").modal("hide");
321                 alert("Error retrieving available ODK crossing forms");
322             }
323         });
325         if (jQuery('#female_accession_list_div_list_select').val() != '' && jQuery('#male_accession_list_div_list_select').val() != '' ) {
326             draw_cross_grid(female_accessions, male_accessions);
327         }
328     });
330     jQuery(document).on('change', '#female_accession_list_div_list_select', function(){
331         if (jQuery('#female_accession_list_div_list_select').val() != '' && jQuery('#male_accession_list_div_list_select').val() != '' ) {
332             var female_accessions = lo.getList(jQuery('#female_accession_list_div_list_select').val());
333             var male_accessions = lo.getList(jQuery('#male_accession_list_div_list_select').val());
334             draw_cross_grid(female_accessions, male_accessions);
335         }
336     });
337     jQuery(document).on('change', '#male_accession_list_div_list_select', function(){
338         if (jQuery('#female_accession_list_div_list_select').val() != '' && jQuery('#male_accession_list_div_list_select').val() != '' ) {
339             var female_accessions = lo.getList(jQuery('#female_accession_list_div_list_select').val());
340             var male_accessions = lo.getList(jQuery('#male_accession_list_div_list_select').val());
341             draw_cross_grid(female_accessions, male_accessions);
342         }
343     });
345     get_select_box('locations', 'cross_wishlist_location_select_list_div', { 'name' : 'cross_wishlist_location_id', 'id' : 'cross_wishlist_location_id', 'empty':1 });
346     get_select_box('trials', 'cross_wishlist_female_trial_select_no_list_div', { 'name' : 'cross_wishlist_female_trial_id', 'id' : 'cross_wishlist_female_trial_id', 'empty':1, 'multiple':0 });
347     get_select_box('trials', 'cross_wishlist_male_trial_select_no_list_div', { 'name' : 'cross_wishlist_male_trial_id', 'id' : 'cross_wishlist_male_trial_id', 'empty':1, 'multiple':0 });
349     get_select_box('trials', 'cross_wishlist_female_trial_select_list_div', { 'name' : 'cross_wishlist_list_female_trial_id', 'id' : 'cross_wishlist_list_female_trial_id', 'empty':1, 'multiple':0 });
350     get_select_box('trials', 'cross_wishlist_male_trial_select_list_div', { 'name' : 'cross_wishlist_list_male_trial_id', 'id' : 'cross_wishlist_list_male_trial_id', 'empty':1, 'multiple':0 });
352     jQuery(document).on('change', "#cross_wishlist_list_female_trial_id", function() {
353         cross_wishlist_change_trials_lists();
354     });
356     jQuery(document).on('change', "#cross_wishlist_female_trial_id", function() {
357         cross_wishlist_change_trials();
358     });
360     var female_trial_id;
361     var male_trial_id;
362     var cross_data = [];
363     var female_male_tracker = {};
364     var selected_plot_ids = [];
365     jQuery('.nav-pills a').on('shown.bs.tab', function(event){
366         cross_data = [];
367         female_male_tracker = {};
368         selected_plot_ids = [];
369         female_trial_id;
370         male_trial_id;
371     });
373     var female_trial_ids_array = [];
374     var male_trial_ids_array = [];
375     function cross_wishlist_change_trials () {
376         var female_trial_ids = jQuery('#cross_wishlist_female_trial_id').val();
377         var female_trial_ids_string = '';
378         if ( jQuery.isArray(female_trial_ids) ) {
379             female_trial_ids_array = female_trial_ids;
380             trial_ids_string = female_trial_ids.join();
381         } else {
382             female_trial_ids_array = [female_trial_ids];
383             female_trial_ids_string = female_trial_ids;
384         }
386         jQuery.ajax({
387             url: '/ajax/breeders/trial/'+female_trial_ids_array[0]+'/location',
388             success: function(response) {
389                 //console.log(response);
390                 var cross_wishlist_name;
391                 if ('<% $c->config->{odk_crossing_data_test_form_name} %>' == jQuery("#cross_wishlist_availabe_odk_crossing_forms option:selected").text()){
392                     cross_wishlist_name = 'cross_wishlist_test.csv';
393                 } else if ('<% $c->config->{odk_crossing_data_separate_wishlist_by_location} %>' == '1') {
394                     cross_wishlist_name = 'cross_wishlist_'+response.location[1]+'.csv'
395                 } else {
396                     cross_wishlist_name = 'cross_wishlist_<% $c->config->{project_name} %>.csv';
397                 }
398                 jQuery('#cross_wishlist_trial_select_no_list_cross_wishlist_name_div').html('<input type="text" class="form-control" disabled value="'+cross_wishlist_name+'" />');
399             },
400             error: function(response) {
401                 alert("An error occurred getting trial location");
402             }
403         });
405         get_select_box('stocks', 'cross_wishlist_female_accession_select_div', { 'name' : 'cross_wishlist_female_accession_ids', 'id' : 'cross_wishlist_female_accession_ids', 'size' : 7, 'stock_type_name':'accession', 'trial_id_list':female_trial_ids_array, 'names_as_select':1 });
406     };
408     function cross_wishlist_change_trials_lists () {
409         var female_trial_ids = jQuery('#cross_wishlist_list_female_trial_id').val();
410         var female_trial_ids_string = '';
411         if ( jQuery.isArray(female_trial_ids) ) {
412             female_trial_ids_array = female_trial_ids;
413             female_trial_ids_string = female_trial_ids.join();
414         } else {
415             female_trial_ids_array = [female_trial_ids];
416             female_trial_ids_string = female_trial_ids;
417         }
419         jQuery.ajax({
420             url: '/ajax/breeders/trial/'+female_trial_ids_array[0]+'/location',
421             success: function(response) {
422                 //console.log(response);
423                 var cross_wishlist_name;
424                 if ('<% $c->config->{odk_crossing_data_test_form_name} %>' == jQuery("#cross_wishlist_availabe_odk_crossing_forms option:selected").text()){
425                     cross_wishlist_name = 'cross_wishlist_test.csv';
426                 } else if ('<% $c->config->{odk_crossing_data_separate_wishlist_by_location} %>' == '1') {
427                     cross_wishlist_name = 'cross_wishlist_'+response.location[1]+'.csv'
428                 } else {
429                     cross_wishlist_name = 'cross_wishlist_<% $c->config->{project_name} %>.csv';
430                 }
431                 jQuery('#cross_wishlist_trial_select_list_cross_wishlist_name_div').html('<input type="text" class="form-control" disabled value="'+cross_wishlist_name+'" />');
432             },
433             error: function(response) {
434                 alert("An error occurred getting trial location");
435             }
436         });
437     };
439     jQuery(document).on('change', "#cross_wishlist_female_accession_ids", function() {
440         cross_wishlist_select_females();
441     });
443     function cross_wishlist_select_females () {
445         var male_trial_ids = jQuery('#cross_wishlist_male_trial_id').val();
446         var male_trial_ids_string = '';
447         if ( jQuery.isArray(male_trial_ids) ) {
448             male_trial_ids_array = male_trial_ids;
449             male_trial_ids_string = male_trial_ids.join();
450         } else {
451             male_trial_ids_array = [male_trial_ids];
452             male_trial_ids_string = male_trial_ids;
453         }
454         if (male_trial_ids.length == 0){
455             alert('Please select a male trial first!');
456             return;
457         }
459         var female_accession_ids = jQuery('#cross_wishlist_female_accession_ids').val();
460         var html = '<table class="table table-bordered" id="cross_wishlist_male_select_table"><thead><tr><th>Female Parent</th><th>Select Male Parent</th><th>Priority ( 1 : highest, 10 : lowest )</th></tr></thead><tbody>';
461         for(var i=0; i<female_accession_ids.length; i++){
462             html = html + '<tr><td>' + female_accession_ids[i] + '</td><td><div name="cross_wishlist_male_selects" id="cross_wishlist_male_select_' +i+ '"></div></td><td><span id="cross_wishlist_male_accession_id_'+ i +'_priority" data-related="'+encodeURI(female_accession_ids[i])+'" >Select Male(s)</span></td></tr>';
463         };
464         html = html + '</tbody></table>';
465         jQuery('#cross_wishlist_male_parent_div').html(html);
467         var cross_wishlist_male_parents = jQuery('#cross_wishlist_male_select_table')
468              .find("div")
469              .map(function() { return this.id; })
470              .get();
471         //console.log(cross_wishlist_male_parents);
472         for(var i=0; i<cross_wishlist_male_parents.length; i++){
473             var female_id = jQuery('#cross_wishlist_male_accession_id_'+ i +'_priority').data('related');
474             get_select_box('stocks', cross_wishlist_male_parents[i], { 'name' : 'cross_wishlist_male_accession_id_select', 'id' : 'cross_wishlist_male_accession_id_'+i, 'size' : 7, 'stock_type_name':'accession', 'trial_id_list':male_trial_ids_array, 'names_as_select':1, 'data-related':female_id });
475         };
476     };
478     jQuery(document).on('change', "select[name='cross_wishlist_male_accession_id_select']", function() {
479         var div_id = '#' + this.id;
480         var male_ids = jQuery(div_id).val();
481         if (male_ids.length > 0){
482             cross_wishlist_change_males(div_id, div_id + '_priority');
483         }
484     });
486     function cross_wishlist_change_males(div_id, select_id){
487         var female_accession_ids = jQuery('#cross_wishlist_female_accession_ids').val();
488         var male_ids = jQuery(div_id).val();
489         var female_id = jQuery(div_id).data('related');
490         var html = '<table class="table table-bordered" div="' + div_id +'_table"><thead><tr><th>Male Parent</th><th>Priority</th></tr></thead><tbody>';
491         for (var i=0; i<male_ids.length; i++){
492             html = html + '<tr><td>' + male_ids[i] + '</td><td><input class="form-control" male_id="' + male_ids[i] + '" female_id="' + female_id + '" name ="cross_wishlist_final_priority" id="cross_wishlist_male_priority_'+ i +'" value="1" placeholder="1"/></td></tr>';
493         };
494         html = html + '</tbody></table>';
495         jQuery(select_id).html(html);
496     };
499     jQuery('#cross_wishlist_submit').click(function(e){
500         e.preventDefault();
501         var active_tab = jQuery('ul#cross_wishlist_tab_select li.active').attr('id');
502         //console.log(active_tab);
503         cross_data = [];
504         female_male_tracker = {};
505         selected_plot_ids = [];
506         female_trial_id;
507         male_trial_id;
509         var male_accession_limit = 10;
510         if (active_tab == 'cross_wishlist_no_list_tab'){
511             jQuery('input[name="cross_wishlist_final_priority"]').each(function() {
512                 value = this.value;
513                 var male_id = jQuery(this).attr('male_id');
514                 var female_id = jQuery(this).attr('female_id');
515                 cross_data.push({ 'female_id':female_id, 'male_id':male_id, 'priority':value });
516                 if (female_id in female_male_tracker){
517                     female_male_tracker[female_id].push(male_id);
518                 } else {
519                     female_male_tracker[female_id] = [male_id];
520                 }
521             });
522             female_trial_id = jQuery('#cross_wishlist_female_trial_id').val();
523             male_trial_id = jQuery('#cross_wishlist_male_trial_id').val();
524         }
525         if (active_tab == 'cross_wishlist_list_tab'){
526             jQuery('input[name="cross_wishlist_cross_grid_inputs"]').each(function() {
527                 value = this.value;
528                 if (value){
529                     var male_id = jQuery(this).data('male');
530                     var female_id = jQuery(this).data('female');
531                     cross_data.push({ 'female_id':female_id, 'male_id':male_id, 'priority':value });
532                     if (female_id in female_male_tracker){
533                         female_male_tracker[female_id].push(male_id);
534                     } else {
535                         female_male_tracker[female_id] = [male_id];
536                     }
537                 }
538             });
539             female_trial_id = jQuery('#cross_wishlist_list_female_trial_id').val();
540             male_trial_id = jQuery('#cross_wishlist_list_male_trial_id').val();
541         }
542         //console.log(cross_data);
543         //console.log(trial_id);
544         //console.log(female_male_tracker);
545         if(!female_trial_id){
546             alert('You must select a female trial!');
547             return;
548         }
549         if(!male_trial_id){
550             alert('You must select a male trial! It can be the same trial as the female trial.');
551             return;
552         }
553         for (f in female_male_tracker){
554             var male_array = female_male_tracker[f];
555             if (male_array.length > male_accession_limit){
556                 alert('A maximum of 10 male accessions can be selected for each cross!');
557                 return;
558             }
559         }
560         jQuery.ajax({
561             url: '/ajax/cross/create_cross_wishlist',
562             method: 'POST',
563             data: {
564                 'crosses': JSON.stringify(cross_data),
565                 'female_trial_id': female_trial_id,
566                 'male_trial_id': male_trial_id
567             },
568             dataType:'json',
569             beforeSend: function() {
570                 jQuery('#working_modal').modal('show');
571             },
572             success: function(response) {
573                 //console.log(response);
574                 jQuery('#working_modal').modal('hide');
575                 if (response.error){
576                     alert(response.error);
577                 } else {
578                     jQuery('#cross_wishlist_cross_select_div').html(response.data);
579                     jQuery('#create_cross_wishlist_select_dialog').modal('show');
580                 }
581             },
582             error: function(response) {
583                 jQuery('#working_modal').modal('hide');
584                 alert("An error occurred submitting plots for cross wishlist");
585             }
586         });
587     });
588     
589     jQuery('#cross_wishlist_select_submits').click(function(){
590         var male_plot_limit = 10;
591         female_male_tracker = {};
592         selected_plot_ids = [];
594         jQuery('input[name="cross_wishlist_plot_select_female_input"]').each(function() {
595             if (this.checked){
596                 selected_plot_ids.push({
597                     'cross_female_accession_name' : jQuery(this).data('female_accession_name'),
598                     'female_plot_id' : jQuery(this).val(),
599                     'male_genotypes_string' : jQuery(this).data('male_genotypes_string')
600                 });
601             }
602         });
603         jQuery('input[name="cross_wishlist_plot_select_male_input"]').each(function() {
604             if (this.checked){
605                 selected_plot_ids.push({
606                     'cross_female_accession_name' : jQuery(this).data('female_accession_name'),
607                     'male_plot_id' : jQuery(this).val()
608                 });
609                 if (jQuery(this).data('female_accession_name') in female_male_tracker){
610                     female_male_tracker[jQuery(this).data('female_accession_name')].push(jQuery(this).val());
611                 } else {
612                     female_male_tracker[jQuery(this).data('female_accession_name')] = [jQuery(this).val()];
613                 }
614             }
615         });
616         //console.log(selected_plot_ids);
617         //console.log(female_male_tracker);
618         for (f in female_male_tracker){
619             var male_array = female_male_tracker[f];
620             if (male_array.length > male_plot_limit){
621                 alert('A maximum of 10 male plots can be selected for each cross!');
622                 return;
623             }
624         }
626         jQuery.ajax({
627             url: '/ajax/cross/create_cross_wishlist_submit',
628             method: 'POST',
629             data: {
630                 'crosses': JSON.stringify(cross_data),
631                 'selected_plot_ids': JSON.stringify(selected_plot_ids),
632                 'female_trial_id': female_trial_id,
633                 'male_trial_id': male_trial_id,
634                 'form_id': jQuery('#cross_wishlist_availabe_odk_crossing_forms').val(),
635                 'form_name': jQuery("#cross_wishlist_availabe_odk_crossing_forms option:selected").text()
636             },
637             dataType:'json',
638             beforeSend: function() {
639                 jQuery('#working_msg').html('Creating cross wishlist files and sending information to ONA... This can take time...');
640                 jQuery('#working_modal').modal('show');
641             },
642             success: function(response) {
643                 //console.log(response);
644                 jQuery('#working_msg').html('');
645                 jQuery('#working_modal').modal('hide');
646                 if (response.filename) {
647                     window.location.href = "/download/"+response.filename;
648                 }
649                 if (response.error) {
650                     jQuery('#cross_wishlist_response_div').html(response.error);
651                 }
652                 if (response.success){
653                     jQuery('#cross_wishlist_response_div').html(response.success);
654                 }
655                 jQuery('#create_cross_wishlist_response_dialog').modal('show');
656             },
657             error: function(response) {
658                 jQuery('#working_msg').html('');
659                 jQuery('#working_modal').modal('hide');
660                 alert("An error occurred creating cross wishlist");
661             }
662         });
663     });
667 </script>