<script>
    $(document).ready(function(){
      $('#<?=$controller?>').submit(function(){
        var formData = new FormData(this);
        $.ajax({
          url:this.action,
          type:this.method,
          data: formData,
          cache: false,
          contentType: false,
          processData: false,
          beforeSend: function() {
              $("#loading").fadeIn('fast');
          },
          success:function(data){
            data=JSON.parse(data);

            $('#loading').fadeOut('fast',function(){
              if(data.status=="1")
              {


                swal({
                  title: data.title,
                  text: data.message,
                  type: "success",
                  timer: 700,
                  showCancelButton: false,
                  showConfirmButton: false,
                  //showCancelButton: false,
                  confirmButtonClass: "btn-success",
                  confirmButtonText: "OK",
                  closeOnConfirm: true,
                  button: false
                },


                function(){
                  //tr.hide();
                  <?php if($parent_controller==$controller){?>
                    <?php if($parent_controller == 'registrasi'){ ?>
                    location.replace('front_office/reg_dir/registrasi');
                    <?php }else if($parent_controller == 'kunjungan'){ ?>
                    location.replace('front_office/reg_dir/kunjungan');
                    <?php }else{ ?>
                    location.reload(true);
                    <?php }?>
                    //document.location="<?=site_url('front_office/'.$controller_dir.$controller)?>";
                  <?php }else if($parent_controller=='ugd_pemeriksa' || $parent_controller == 'ugd_tmbh_pemeriksaan' || $parent_controller=='ugd_diagnosa' || $parent_controller=='ugd_obat'){ ?>
                    location.reload(true);
                    
                    $('.requireds').removeClass('has-error');
                    $('.requireds').addClass('has-success');
                    $('.requireds_error').css('display','none');
                    $('.alert').css('display','none');
                    $('.showSweetAlert').css('display','none');
                    var valu = $("#pemeriksaan_khusus").val();
                    //alert(valu);
                    if(valu == null){
                      $('.tmbh_pemeriksaans').css('display','none');
                    }else{
                      $('.tmbh_pemeriksaans').css('display','block');
                    }
                    //$(".confirm").click();
                  <?php }else{?>
                    location.reload(true);
                    document.location = '<?=$this->agent->referrer()?>';
                  <?php }?>
                }


                );
              }
              else if(data.status=="2")
              {
                $.each( data.detail_error, function( field, value ) {
                  if(value.error!='')
                  {
                    $('#form_'+field).removeClass('has-success');
                    $('#form_'+field).addClass('has-error');
                    $('#error_'+field).html(value.error).css('display','block');
                  }
                  else
                  {
                    $('#form_'+field).removeClass('has-error');
                    $('#form_'+field).addClass('has-success');
                    $('#error_'+field).css('display','none');
                  }
                });
                $('#error-text').html(data.message);
                $('#error').fadeIn('fast');
                $('html,body').animate({scrollTop: $('#portlet-content').offset().top-70},500);
              }
              else
              {
                swal({
                  title: data.title,
                  text: data.message,
                  type: "error",
                  showCancelButton: false,
                  confirmButtonClass: "btn-danger",
                  confirmButtonText: "OK",
                  closeOnConfirm: true,
                   button: false
                },
                function(){

                });
              }
            });
          }                      
        });
        return false;
      });
    });
</script>