
  function EditInsertAppointment(action, event, ui, diary_key, region_key, diarydate, trial)
  {
                    var stopcache = new Date().getTime();

                    if (trial == 'T')
                    {
                      switch (action){
                        case 'new'   : var appointmenttitle = 'New Trial Date'; break;
                        case 'edit'  : var appointmenttitle = 'Edit Trial Date'; break;
                        case 'delete': var appointmenttitle = 'Delete Trial Date'; break;
                      };
                    }
                    else
                    if (trial == 'F')
                    {
                      switch (action){
                        case 'new'   : var appointmenttitle = 'New Appointment'; break;
                        case 'edit'  : var appointmenttitle = 'Edit Appointment'; break;
                        case 'delete': var appointmenttitle = 'Delete Appointment'; break;
                      };
                    };

                    $('#appointment').load("appointment.php?action=" + action+ "&id1="+ diary_key + "&id2=" + region_key + "&id3=" + diarydate + "&id4=" + trial + "&id5="+ stopcache , function (){

                    $( "#appointment" ).dialog({

                       height    : 540,
                       width     : 540,
                       modal     : true,
                       stack     : true,
                       resizable : false,

                       title : appointmenttitle,

                       open: function(event, ui) {

                               $(event.target).dialog('widget').css({ position: 'fixed', top:20 });

                               $(".ui-widget-overlay").css({
                                 position: 'fixed',
                                 top: '0'
                               });
                             },
                       close : function(event, ui) {
                                  $('#appointment').html('');
                                  $(this).dialog('destroy');
                               },
                       buttons: {
                         "Save": function(event) {

                           var account      = $('#edtAccount_Key').val();
                           var temppatient  = $('#edtTempPatient').val();
                           var practitioner = $('#cobxPractitioner').val();
                           var tdate        = $('#trialdateTF').val();
                           
                           if ((account == '') && (temppatient == ''))
                           {
                             alert('A Patient is required to be selected !');
                           }
                           else
                           if ((tdate == 'F') && (practitioner == -1))
                           {
                            alert('A Practitioner is required to be selected !'); 
                           } 
                           else
                           {


                               $("body").css("cursor", "progress");

                               $.ajax({
                                   url     : 'appointmentsave.php?stopcache='+ new Date().getTime(),
                                   type    : 'POST',
                                   cache   : false,
                                   context : $(this),
                                   data    : {'appointmentstatus'  : $('#appointmentstatus').val(),
                                              'diary_key'          : $('#diary_key').val(),
                                              'region'             : $('#cobxApptRegion').val(),
                                              'edtAccount_Key'     : $('#edtAccount_Key').val(),
                                              'Patient_Key'        : $('#Patient_Key').val(),
                                              'practitioner'       : $('#cobxPractitioner').val(),
                                              'cobxDiaryStatus'    : $('#cobxDiaryStatus').val(),
                                              'mmoDiaryNotes'      : $('#mmoDiaryNotes').val(),
                                              'diarydate'          : $('#hideApptDate').val(),
                                              'diaryfromtime'      : $('#cobxApptTime').val(),
                                              'edtTempPatient'     : $('#edtTempPatient').val(),
                                              'edtTempPatientCell' : $('#edtTempPatientCell').val(),
                                              'cobxDriver'         : $('#cobxDriver').val(),
                                              'cobxCourtHouse'     : $('#cobxCourtHouse').val(),
                                              'trialdate'          : $('#trialdateTF').val()},
                                   success : function(resp){

                                               if (resp != ''){
                                                 alert(resp);
                                               };

                                               var pract     = $('#Practitioner option:selected').val();
                                               var driver    = $('#Driver option:selected').val();
                                               var diarydate = $('#hidediarydate').val();
                                               var region    = $('#Region').val();

                                               $("#div1").load("diary.php?Practitioner="+pract+"&SearchDay="+diarydate+"&Driver="+driver+"&Region="+region);

                                               $( this ).dialog( "close" );},

                                   error   : function(resp){
                                               alert('Error' + resp);
                                             }
                               });

                               $("body").css("cursor", "default");
                           };
                         },
                         Cancel: function() {
                           $( this ).dialog( "close" );
                         }
                         }
                      });
                 });
  }

