fixed merge master
[sgn.git] / js / CXGN / Trial.js
blob513d1f2b99a3c5fcf8ddacf605ee1a17d1863dd1
1 // Depends on CXGN.BreedersToolbox.HTMLSelect
3 function delete_phenotype_data_by_trial_id(trial_id) {
4     var yes = confirm("Are you sure you want to delete all phenotypic data associated with trial "+trial_id+" ? This action cannot be undone.");
5     if (yes) {
6         jQuery('#working').dialog("open");
7         jQuery.ajax( {
8             url: '/ajax/breeders/trial/'+trial_id+'/delete/phenotypes',
9             success: function(response) {
10                 if (response.error) {
11                     jQuery('#working').dialog("close");
12                     alert(response.error);
13                 }
14                 else {
15                     jQuery('#working').dialog("close");
16                     alert('The phenotypic data has been deleted.'); // to do: give some idea how many items were deleted.
17                     window.location.href="/breeders/trial/"+trial_id;
18                 }
19             },
20             error: function(response) {
21                 jQuery('#working').dialog("close");
22                 alert("An error occurred.");
23             }
24         });
25     }
29 function delete_layout_data_by_trial_id(trial_id) {
30     var yes = confirm("Are you sure you want to delete the layout data associated with trial "+trial_id+" ? This action cannot be undone.");
31     if (yes) {
32         jQuery('#working').dialog("open");
34         jQuery.ajax( {
35             url: '/ajax/breeders/trial/'+trial_id+'/delete/layout',
36             success: function(response) {
37                 if (response.error) {
38                     jQuery('#working').dialog("close");
39                     alert(response.error);
40                 }
41                 else {
42                     jQuery('#working').dialog("close");
43                     alert('The layout data has been deleted.'); // to do: give some idea how many items were deleted.
44                     window.location.href="/breeders/trial/"+trial_id;
45                 }
46             },
47             error: function(response) {
48                 jQuery('#working').dialog("close");
49                 alert("An error occurred.");
50             }
51         });
52     }
55 function delete_project_entry_by_trial_id(trial_id) {
56        var yes = confirm("Are you sure you want to delete the trial entry for trial "+trial_id+" ? This action cannot be undone.");
57     if (yes) {
58         jQuery('#working').dialog("open");
60         jQuery.ajax( {
61             url: '/ajax/breeders/trial/'+trial_id+'/delete/entry',
62             success: function(response) {
63                 if (response.error) {
64                     jQuery('#working').dialog("close");
65                     alert(response.error);
66                 }
67                 else {
68                     jQuery('#working').dialog("close");
69                     alert('The project entry has been deleted.'); // to do: give some idea how many items were deleted.
70                     window.location.href="/breeders/trial/"+trial_id;
71                 }
72             },
73             error: function(response) {
74                 jQuery('#working').dialog("close");
75                 alert("An error occurred.");
76             }
77         });
78     }
82 function associate_breeding_program() {
83     var program = jQuery('#breeding_program_select').val();
85     var trial_id = get_trial_id();
86     jQuery.ajax( {
87         url: '/breeders/program/associate/'+program+'/'+ trial_id,
88         async: false,
89         success: function(response) {
90             alert("Associated program with id "+program + " to trial with id "+ trial_id);
92         }
93     });
96 function load_breeding_program_info(trial_id) {
97     jQuery.ajax( {
98         url:'/breeders/programs_by_trial/'+trial_id,
99         success: function(response) {
100             if (response.error) {
101                 jQuery('#breeding_programs').html('[ An error occurred fetching the breeding program information ]');
102             }
103             else {
104                 var programs = response.projects;
105                 for (var i=0; i< programs.length; i++) {
106                     var html =  programs[0][1] + ' (' + programs[0][2] + ') ';
107                 }
108                 if (programs.length == 0) { html = "(none)"; }
109                 jQuery('#breeding_programs').html(html);
110             }
111         },
112         error: function() {
113             jQuery('#breeding_programs').html('[ An error occurred ]');
114         }
115     });
119 function open_create_fieldbook_dialog() {
120     var trialID = parseInt(jQuery('#trialIDDiv').text());
121     new jQuery.ajax({
122         type: 'POST',
123         url: '/ajax/fieldbook/create',
124         dataType: "json",
125         data: {
126             'trial_id': trialID,
127         },
128         beforeSend: function() {
129                 jQuery("#working_modal").modal("show");
130         },
131         success: function (response) {
132                 jQuery("#working_modal").modal("hide");
133             if (response.error) {
134                 alert(response.error);
135                 jQuery('#open_create_fieldbook_dialog').dialog("close");
136             } else {
137                 jQuery('#tablet_layout_download_link').attr('href',"/fieldbook");
138                 jQuery("#tablet_field_layout_saved_dialog_message").dialog("open");
139                 //alert(response.file);
140                 jQuery('#open_create_fieldbook_dialog').dialog("close");
141             }
142         },
143         error: function () {
144                 jQuery("#working_modal").modal("hide");
145             alert('An error occurred creating the field book.');
146             jQuery('#open_create_fieldbook_dialog').dialog("close");
147         }
148     });
151 function open_create_DataCollector_dialog() {
152     //jQuery('#working').dialog("open");
153     jQuery('#working_modal').modal("show");
154     var list = new CXGN.List();
155     jQuery("#trait_list_dc").html(list.listSelect("trait_list", [ 'traits' ]));
156     //jQuery('#working').dialog("close");
157     jQuery('#working_modal').modal("hide");
158     jQuery('#create_DataCollector_dialog').dialog("open");
162 function create_DataCollector() {
163     //jQuery('#working').dialog("open");
164     jQuery('#working_modal').modal("show");
165     var trialID = parseInt(jQuery('#trialIDDiv').text());
166     var list = new CXGN.List();
167     var trait_list_id = jQuery('#trait_list_list_select').val();
168     var trait_list;
169     if (! trait_list_id == "") {
170         trait_list = JSON.stringify(list.getList(trait_list_id));
171     }
172      new jQuery.ajax({
173          type: 'POST',
174          url: '/ajax/phenotype/create_DataCollector',
175          dataType: "json",
176          data: {
177              'trial_id': trialID,
178              'trait_list': trait_list,
179          },
181          success: function (response) {
182      //console.log("success "+JSON.stringify(response));
183      jQuery('#working_modal').modal("hide");
185      if (response.error) {
186        console.log("error: "+response.error);
187        alert("error: "+response.error);
188        jQuery('#open_create_DataCollector_dialog').dialog("close");
189      } else {
190        //alert("success: "+response.filename);
191        jQuery('#open_create_DataCollector_dialog').dialog("close");
192        jQuery('#working_modal').modal("hide");
193        window.location.href = "/download/"+response.filename;
194      }
195          },
196          error: function () {
197              //jQuery('#working').dialog("close");
198              jQuery('#working_modal').modal("hide");
199              alert('An error occurred creating a DataCollector file.');
200              jQuery('#open_download_DataCollector_dialog').dialog("close");
201          }
202      });
205 function open_derived_trait_dialog() {
206     jQuery('#working_modal').modal("show");
207     jQuery('#compute_derived_trait_dialog').dialog("open"); 
208     var trait = jQuery('#sel1').val();
209     jQuery("#test_xyz").html(trait);
210     jQuery('#working_modal').modal("hide");
211     
214 function compute_derived_trait() {
215     jQuery('#working_modal').modal("show");
216     var trait = jQuery('#derived_trait_select').val();
217     var trialID = parseInt(jQuery('#trialIDDiv').text());
218     if (trait === '') {
219                 alert("No trait selected");
220             }
221     
222      new jQuery.ajax({
223          type: 'POST',
224          url: '/ajax/phenotype/create_derived_trait',
225          dataType: "json",
226          data: {
227              'trial_id': trialID,
228              'trait': trait,
229          },
230                 
231          success: function (response) {
232              jQuery('#working_modal').modal("hide");
233                 
234              if (response.error) {
235                  alert("Computation stopped: "+response.error);
236                  //alert("Computation for "+trait+" stopped: "+response.error);
237                  jQuery('#open_derived_trait_dialog').dialog("close");
238                  
239              } else {
240                  jQuery('#open_derived_trait_dialog').dialog("close");
241                  jQuery('#working_modal').modal("hide");
242                  jQuery('derived_trait_saved_dialog_message');
243                  alert("Successfully derived and uploaded phenotype");
244                 // alert("Successfully derived and uploaded ' "+trait+" ' values for this trial");
245              }
246          },
247          error: function () {
248              jQuery('#working_modal').modal("hide");
249              alert('An error occurred creating trait.');
250          }
251      });
255 function trial_detail_page_setup_dialogs() {
257     jQuery('#change_breeding_program_dialog').dialog( {
258         height: 200,
259         width: 400,
260         title: 'Select Breeding Program',
261         autoOpen: false,
262         buttons: {
263             'OK': {
264                 text: "OK",
265                 id: "edit_trial_breeding_program_submit",
266                 click: function() {
267                 associate_breeding_program();
268                 jQuery('#change_breeding_program_dialog').dialog("close");
269                 var trial_id = get_trial_id();
270                 load_breeding_program_info(trial_id);
271             }
272             },
273             'Cancel': function() { jQuery('#change_breeding_program_dialog').dialog("close"); }
274         }
275     });
277     jQuery( "#tablet_field_layout_saved_dialog_message" ).dialog({
278         autoOpen: false,
279         modal: true,
280         buttons: {
281             Ok: function() {
282                 jQuery( this ).dialog( "close" );
283                 location.reload();
284             }
285         }
286     });
288     jQuery( "#data_collector_saved_dialog_message" ).dialog({
289         autoOpen: false,
290         modal: true,
291         buttons: {
292             Ok: function() {
293                 jQuery( this ).dialog( "close" );
294                 location.reload();
295             }
296         }
297     });
300 //    jQuery('#create_spreadsheet_dialog').dialog({
301 //      autoOpen: false,
302 //      modal: true,
303 //      autoResize:true,
304 //      width: 500,
305 //      position: ['top', 75],
306 //      modal: true,
307 //      buttons: {
308 //          Cancel: function() {
309 //              jQuery( this ).dialog( "close" );
310 //              return;
311 //          },
312 //          Create: {text: "Ok", id:"create_phenotyping_ok_button", click:function() {
313 //              create_spreadsheet();
314 //              //save_experimental_design(design_json);
315 //              jQuery( this ).dialog( "close" );
316 //             },
317 //          },
318 //      },
319 //    });
321 //    jQuery('#create_DataCollector_dialog').dialog({
322 //      autoOpen: false,
323 //      modal: true,
324 //      autoResize:true,
325 //      width: 500,
326 //      position: ['top', 75],
327 //      modal: true,
328 //      buttons: {
329 //          Cancel: function() {
330 //              jQuery( this ).dialog( "close" );
331 //              return;
332 //          },
333 //          Create: {text: "Create", id:"create_DataCollector_submit_button", click:function() {
334 //              create_DataCollector();
335 //              //save_experimental_design(design_json);
336 //              jQuery( this ).dialog( "close" );
337 //              }
338 //          },
339 //      },
340 //    });
342      jQuery('#compute_derived_trait_dialog').dialog({
343         autoOpen: false,
344         modal: true,
345         autoResize:true,
346         width: 500,
347         position: ['top', 75],
348         modal: true,
349         buttons: {
350             Cancel: function() {
351                 jQuery( this ).dialog( "close" );
352                 return;
353             },
354             Create: {text: "Create", id:"create_derived_trait_submit_button", click:function() {
355                 compute_derived_trait();
356                 jQuery( this ).dialog( "close" );               
357                 }
358             },
359         },
360     }); 
363     jQuery('#show_change_breeding_program_link').click(
364         function() {
365             jQuery('#change_breeding_program_dialog').dialog("open");
366             get_select_box('breeding_programs', 'change_breeding_program_select_div');
367         }
368     );
370     jQuery('#delete_phenotype_data_by_trial_id').click(
371         function() {
372             var trial_id = get_trial_id();
373             delete_phenotype_data_by_trial_id(trial_id);
374         }
375     );
377     jQuery('#delete_layout_data_by_trial_id').click(
378         function() {
379             var trial_id = get_trial_id();
380             delete_layout_data_by_trial_id(trial_id);
381         });
383     jQuery('#delete_trial_entry_by_trial_id').click(
384         function() {
385             var trial_id = get_trial_id();
386             delete_project_entry_by_trial_id(trial_id);
387         });
389     jQuery('#create_fieldbook_link').click(function () {
390         open_create_fieldbook_dialog();
391     });
394     jQuery('#view_layout_link').click(function () {
395         jQuery('#trial_design_view_layout').dialog("open");
396     });
398     jQuery('#edit_trial_description').click( function () {
399         jQuery('#edit_trial_description_dialog').dialog("open");
401     });
403     jQuery('#change_trial_year_dialog').dialog( {
404         autoOpen: false,
405         height: 200,
406         width: 300,
407         modal: true,
408         title: "Change trial year",
409         buttons: {
410             cancel: { text: "Cancel",
411                       click: function() { jQuery( this ).dialog("close"); },
412                       id: "change_trial_year_cancel_button"
413                     },
414             save:   { text: "Save",
415                       click: function() {
416                           save_trial_year();
417                           display_trial_year();
418                           jQuery('#change_trial_year_dialog').dialog("close");
420                       id: "change_trial_year_save_button"
421                     }
422         }
423     });
426     jQuery('#change_year_link').click( function() {
427         jQuery('#change_trial_year_dialog').dialog("open");
428         get_select_box('years', 'change_year_select_div', { 'name' : 'year_select'});
429     });
431     jQuery('#change_trial_location_link').click( function() {
432         jQuery('#change_trial_location_dialog').dialog("open");
433         get_select_box('locations', 'trial_location_select_div', { 'name' : 'trial_location_select' });
434     });
436     jQuery('#change_planting_date_dialog').dialog( {
437         autoOpen: false,
438         height: 200,
439         width: 300,
440         modal: true,
441         title: 'Change planting date',
442         buttons: {
443             cancel: { text: "Cancel",
444                       click: function() { jQuery( this ).dialog("close"); },
445                       id: "change_planting_date_button"
446                     },
447             save:   { text: "Save",
448                       click: function() {
449                           save_planting_date();
450                       },
451                       id: "change_planting_date_button"
452                     }
453         }
454     });
456     jQuery('#planting_date_picker').datepicker();
458     jQuery('#change_planting_date_link').click( function() {
459         jQuery('#change_planting_date_dialog').dialog("open");
460     });
462     jQuery('#change_harvest_date_dialog').dialog( {
463         autoOpen: false,
464         height: 200,
465         width: 300,
466         modal: true,
467         title: 'Change harvest date',
468         buttons: {
469             cancel: { text: "Cancel",
470                       click: function() { jQuery( this ).dialog("close"); },
471                       id: "change_harvest_date_button"
472                     },
473             save:   { text: "Save",
474                       click: function() {
475                           save_harvest_date();
476                       },
477                       id: "change_harvest_date_button"
478                     }
479         }
480     });
482     jQuery('#harvest_date_picker').datepicker();
484     jQuery('#change_harvest_date_link').click( function() {
485         jQuery('#change_harvest_date_dialog').dialog("open");
486     });
488     jQuery('#edit_trial_description_dialog').dialog( {
489         autoOpen: false,
490         height: 500,
491         width: 800,
492         modal: true,
493         title: "Change trial description",
494         buttons: {
495             cancel: { text: "Cancel",
496                       click: function() { jQuery( this ).dialog("close"); },
497                       id: "edit_description_cancel_button"
498                     },
499             save:   { text: "Save",
500                       click: function() { save_trial_description(); },
501                       id: "edit_description_save_button"
502                     }
503         }
505     });
507     jQuery('#edit_trial_type').click( function () {
508         jQuery('#edit_trial_type_dialog').dialog("open");
509         jQuery.ajax( {
510             url: '/ajax/breeders/trial/alltypes',
511             success: function(response) {
512                 if (response.error) {
513                     alert(response.error);
514                 }
515                 else {
516                     var html = "";
517                     if (response.types) {
518                         var selected = 'selected="selected"';
519                         for(var n=0; n<response.types.length; n++) {
521                             html += '<option value="'+response.types[n][1]+'" >'+response.types[n][1]+'</option>';
522                         }
523                     }
524                     else {
525                         html = '<option active="false">No trial types available</option>';
526                     }
527                 }
528                 jQuery('#trial_type_select').html(html);
529             },
530             error: function(response) {
531                 alert("An error occurred trying to retrieve trial types.");
532             }
533         });
536     });
538 //    jQuery('#trial_type_select').change( {
540  //   });
542     jQuery('#edit_trial_type_dialog').dialog( {
543         autoOpen: false,
544         height: 200,
545         width: 300,
546         modal: true,
547         title: "Change trial type",
548         buttons: {
549             cancel: { text: "Cancel",
550                       click: function() { jQuery( this ).dialog("close"); },
551                       id: "edit_type_cancel_button"
552                     },
553             save:   { text: "Save",
554                       click: function() {
555                           var type = jQuery('#trial_type_select').val();
556                           save_trial_type(type);
557                           display_trial_type(type);
558                           jQuery('#edit_trial_type_dialog').dialog("close");
560                       },
561                       id: "edit_type_save_button"
562                     }
563         }
564     });
566     jQuery('#change_trial_location_dialog').dialog( {
567         autoOpen: false,
568         height: 200,
569         width: 300,
570         model: true,
571         title: "Change trial location",
572         buttons: {
573             cancel: { text: "Cancel",
574                       click: function() { jQuery( this ).dialog("close"); },
575                       id: "change_location_cancel_button",
576                     },
577             save:   { text: "Save",
578                       id: "edit_trial_location_submit",
579                       click: function() {
580                           var new_location = jQuery('#location_select').val();
581                           save_trial_location(new_location);
582                           display_trial_location(get_trial_id());
583                           jQuery('#change_trial_location_dialog').dialog("close");
584                       }
585                     }
586         }
587     });
589     jQuery('#compute_derived_trait_link').click( function () {
590         jQuery('#compute_derived_trait_dialog').dialog("open");
591         jQuery.ajax( { 
592                 url: '/ajax/breeders/trial/trait_formula',
593                 success: function(response) { 
594                 //console.log(response);
595                 if (response.error) { 
596                     alert(response.error);
597                 }
598                 else { 
599                     var html = "";
600                     if (response.derived_traits) { 
601                         var selected = 'selected="selected"';
602                         for(var n=0; n<response.derived_traits.length; n++) { 
603                             //alert("derived trait: +derived_traits"+response.derived_traits[n]);
604                             html += '<option value="'+response.derived_traits[n]+'" title="'+response.formula[n]+'" >'+response.derived_traits[n]+' </option> ';
605                         }
606                         
607                     }
608                     else { 
609                         html = '<option active="false">No derived trait available</option>';
610                     }
611                 }
612                 jQuery('#derived_trait_select').html(html);
613             },
614             error: function(response) { 
615                 alert("An error occurred trying to retrieve derived traits.");
616             }
617         });
625 function save_trial_type(type) {
626     var trial_id = get_trial_id();
627     jQuery.ajax( {
628         url: '/ajax/breeders/trial/'+trial_id+'/type/',
629         //url: '/ajax/breeders/trial/'+trial_id+'/type/'+type,
630         data: { type:type },
631         type: 'POST',
632         //async: false, //async=false because it needs to finish before page is updated again.
633         data: { 'type' : type },
634         success: function(response) {
635             if (response.error) {
636                 alert(response.error);
637             }
638             else {
639                 alert('New trial type set successfully');
640             }
641         },
642         error: function(response) {
643             alert('An error occurred setting the trial type.');
644         }
645     });
650 function save_trial_year() {
651     var trial_id = get_trial_id();
652     var year = jQuery('#year_select').val();
653     jQuery.ajax( {
654         url: '/ajax/breeders/trial/'+trial_id+'/year/'+year,
655         type: 'POST',
656         success: function(response) {
657             if(response.error) {
658                 alert(response.error);
659             }
660             else {
661                 alert("Successfully changed year.");
662             }
663         },
664         error: function(response) {
665             alert('An error occurred.');
666         }
667     });
670 function save_harvest_date() {
671     var trial_id = get_trial_id();
672     var harvest_date = jQuery('#harvest_date_picker').val();
673     var checked_date = check_date(harvest_date);
675     if (checked_date) {
676         jQuery.ajax( {
677             url : '/ajax/breeders/trial/'+trial_id+'/harvest_date',
678             data: { 'harvest_date' : checked_date },
679             type: 'POST',
680             success: function(response){
681                 if (response.error) {
682                     alert(response.error);
683                 }
684                 else {
685                     alert("Successfully stored harvest date.");
686                     display_harvest_date();
687                     jQuery('#change_harvest_date_dialog').dialog("close");
688                 }
689             },
690             error: function(response) {
691                 alert('An error occurred.');
692             }
693         });
695     }
698 function display_harvest_date() {
699     var trial_id = get_trial_id();
700     jQuery.ajax( {
701         url : '/ajax/breeders/trial/'+trial_id+'/harvest_date',
702         type: 'GET',
703         success: function(response) { 
704             jQuery('#harvest_date').html("<a href='/calendar/personal?currentDate="+response.harvest_date+"' target=_blank>"+response.harvest_date+"</a>");
705         },
706         error: function(response) {
707             jQuery('#harvest_date').html('[ Protocol error. ]');
708         }
709     });
712 function save_planting_date() {
713     var trial_id = get_trial_id();
714     var planting_date = jQuery('#planting_date_picker').val();
715     var checked_date = check_date(planting_date);
717     if (checked_date) {
718         jQuery.ajax( {
719             url : '/ajax/breeders/trial/'+trial_id+'/planting_date',
720             data: { 'planting_date' : checked_date },
721             type: 'POST',
722             success: function(response){
723                 if (response.error) {
724                     alert(response.error);
725                 }
726                 else {
727                     alert("Successfully stored planting date.");
728                     display_planting_date();
729                     jQuery('#change_planting_date_dialog').dialog("close");
730                 }
731             },
732             error: function(response) {
733                 alert('An error test.');
734             }
735         });
737     }
741 function display_planting_date() {
742     var trial_id = get_trial_id();
743     jQuery.ajax( {
744         url : '/ajax/breeders/trial/'+trial_id+'/planting_date',
745         type: 'GET',
746         success: function(response) { 
747             jQuery('#planting_date').html("<a href='/calendar/personal?currentDate="+response.planting_date+"' target=_blank>"+response.planting_date+"</a>");
748         },
749         error: function(response) {
750             jQuery('#planting_date').html('[ Protocol error. ]');
751         }
752     });
755 function check_date(d) {
756     var regex = new RegExp("^([0-9]{2})\/([0-9]{2})\/([0-9]{4})$");
758     var match = regex.exec(d);
759     if (match === null || match[1] > 12 || match[1] < 1 || match[2] >31 || match[2] < 1 || match[3]>2030 || match[3] < 1950) {
760         alert("This is not a valid date!");
761         return 0;
762     }
763     // save as year/month/day plus time
764     return match[3]+'/'+match[1]+'/'+match[2]+" 00:00:00";
769 function display_trial_year() {
770     var trial_id = get_trial_id();
772     jQuery.ajax( {
773         url: '/ajax/breeders/trial/'+trial_id+'/year',
774         type: 'GET',
775         success: function(response) {
776             if (response.error) {
777                 jQuery('#trial_year').html('[ An error occurred fetching the trial year information. ]');
778             }
779             else {
780                 jQuery('#trial_year').html(response.year);
781             }
782         },
783         error: function(response) {
784             jQuery('#trial_year').html('[ Protocol error ]');
785         }
786     });
789 function display_trial_description(trial_id) {
790     jQuery.ajax( {
791         url: '/ajax/breeders/trial/'+trial_id+'/description',
792         success: function(response) {
793             if (response.error) {
794                 jQuery('#trial_description').html('[ An error occurred. ]');
795             }
796             else {
797                 jQuery('#trial_description').html(response.description);
798                 jQuery('#trial_description_input').html(response.description);
799             }
800         },
801         error: function(response) {
802             jQuery('#trial_description').html('An error occurred trying to display the description.');
803         }
804     });
807 function save_trial_description() {
808     var trial_id = parseInt(jQuery('#trialIDDiv').text());
809     var description = jQuery('#trial_description_input').val();
810     alert('New description = '+description);
811     jQuery.ajax( {
812         url: '/ajax/breeders/trial/'+trial_id+'/description/',
813         data: {description:description},
814         type: 'POST',
815         data: {'description' : description},
816         success: function(response) {
817             if (response.error) {
818                 alert(response.error);
819             }
820             else {
822                 alert("Successfully updated description");
823                 jQuery('#edit_trial_description_dialog').dialog("close");
824                 display_trial_description(trial_id);
825             }
826         },
827         error: function(response) {
828             alert("An error occurred updating the trial description");
829         },
830     });
834 function display_trial_location(trial_id) {
835     jQuery.ajax( {
836         url: '/ajax/breeders/trial/'+trial_id+'/location',
837         type: 'GET',
838         success: function(response) {
839             if (response.error) {
840                 jQuery('#trial_location').html('[ An error occurred fetching the location. ]');
841             }
842             else {
843                 var html = "";
844                 if (response.location[1]) {
845                     html = response.location[1];
846                 }
847                 jQuery('#trial_location').html(html);
848             }
849         },
850         error: function(response) {
851             jQuery('#trial_location').html('[ Protocol error ]');
852         }
853     });
856 function save_trial_location(location_id) {
857     var trial_id = get_trial_id();
858     jQuery.ajax( {
859         url: '/ajax/breeders/trial/'+trial_id+'/location/'+location_id,
860         //data: { 'location_id' : location_id },
861         type: 'POST',
862         success: function(response) {
863             if (response.message) { alert(response.message); }
864             if (response.error) { alert(response.error); }
865         },
866         error: function(response) {
867             alert("An error occurred.");
868         }
869     });
872 function get_trial_type(trial_id) {
874     jQuery.ajax( {
875         url: '/ajax/breeders/trial/type/'+trial_id,
876         success: function(response) {
877             if (response.error) {
878                 alert(response.error);
879             }
880             else {
881                 var type = "[type not set]";
882                 if (response.type) {
883                     type = response.type[1];
884                 }
885                 display_trial_type(type);
886                 return type;
887             }
888         },
889         error: function(response) {
890             display_trial_type('[ Protocol error. ]');
891         }
892     });
895 function display_trial_type(type) {
896     jQuery('#trial_type').html(type);
899 function trial_folder_dialog() {
900     jQuery('#set_folder_dialog').dialog("open");
905 function get_trial_id() {
906     var trial_id = parseInt(jQuery('#trialIDDiv').text());
907     return trial_id;
911 var $j = jQuery.noConflict();
913 jQuery(document).ready(function ($) {
915     $('#upload_trial_coords_link').click(function () {
916         open_upload_trial_coord_dialog();
917     });
919     jQuery("#upload_trial_coord_dialog").dialog({
920         autoOpen: false,
921         modal: true,
922         autoResize:true,
923         width: 500,
924         position: ['top', 75],
925         buttons: {
926             "Cancel": function () {
927                 jQuery('#upload_trial_coord_dialog').dialog("close");
928             },
929             "Ok": {text: "Ok", id:"upload_trial_coords_ok_button", click:function () {
930                 upload_trial_coord_file();
931                 jQuery('#upload_trial_coord_dialog').dialog("close");
932               }
933             }
934         }
935     });
938     $("#trial_coord_upload_spreadsheet_info_dialog").dialog( {
939         autoOpen: false,
940         buttons: { "OK" :  function() { $("#trial_coord_upload_spreadsheet_info_dialog").dialog("close"); },},
941         modal: true,
942         position: ['top', 75],
943         width: 900,
944         autoResize:true
945     });
947      $("#trial_coordinates_upload_spreadsheet_format_info").click( function () {
948         $("#trial_coord_upload_spreadsheet_info_dialog" ).dialog("open");
950     });
952     $("#trial_coord_upload_success_dialog_message").dialog({
953         autoOpen: false,
954         modal: true,
955         buttons: {
956             Ok: { id: "dismiss_trial_coord_upload_dialog",
957                   click: function() {
958                       //$("#upload_trial_form").dialog("close");
959                       //$( this ).dialog( "close" );
960                       location.reload();
961                   },
962                   text: "OK"
963                 }
964         }
966     });
969      $('#upload_trial_coordinates_form').iframePostForm({
970         json: true,
971         post: function () {
972             var uploadedtrialcoordFile = $("#trial_coordinates_uploaded_file").val();
973             $('#working_modal').modal("show");
974             if (uploadedtrialcoordFile === '') {
975                 $('#working_modal').modal("hide");
976                 alert("No file selected");
977             }
978         },
979         complete: function (response) {
980             $('#working_modal').modal("hide");
981             if (response.error_string) {
982                 $("#upload_trial_coord_error_display tbody").html('');
983                 $("#upload_trial_coord_error_display tbody").append(response.error_string);
986                 $(function () {
987                     $("#upload_trial_coord_error_display").dialog({
988                         modal: true,
989                         autoResize:true,
990                         width: 650,
991                         position: ['top', 250],
992                         title: "Errors in uploaded file",
993                         buttons: {
994                             Ok: function () {
995                                 $(this).dialog("close");
996                             }
997                         }
998                     });
999                 });
1000                 return;
1001             }
1002             if (response.error) {
1003                 alert(response.error);
1004                 return;
1005             }
1006             if (response.success) {
1007                 $('#trial_coord_upload_success_dialog_message').dialog("open");
1008                 //alert("File uploaded successfully");
1009             }
1010         }
1011     });
1013         function upload_trial_coord_file() {
1014         var uploadFile = $("#trial_coordinates_uploaded_file").val();
1015         $('#upload_trial_coordinates_form').attr("action", "/ajax/breeders/trial/coordsupload");
1016         if (uploadFile === '') {
1017             alert("Please select a file");
1018             return;
1019         }
1020         $("#upload_trial_coordinates_form").submit();
1021     }
1023     function open_upload_trial_coord_dialog() {
1024         $('#upload_trial_coord_dialog').dialog("open");
1026     }