<?php session_start();
require_once '../Connections/courier.php';
require_once '../functions/functions.php';

$type = $_POST['type'];
$id = $_POST['calendar'];

if($type == 'new')
{
        list($startdate,$start_time) = explode("T", $_POST['startdate']);
	list($enddate,$end_time) = explode("T", $_POST['enddate']);
	$title = mysql_real_escape_string($_POST['title']);
        $max = mysql_fetch_assoc(mysql_query("SELECT MAX(parent_id)+1 as parent_id FROM scheduler", $courier));
	$insert = mysql_query("INSERT INTO scheduler(`doctor_id`, `parent_id`, `title`, `start_date`, `start_time`, `end_date`, `end_time`, `allDay`, `new_old_event`) VALUES('$id','".$max['parent_id']."','$title','$startdate','$start_time','$enddate','$end_time','false', 'new')", $courier);
	$lastid = mysql_insert_id();
	echo json_encode(array('status'=>'success','eventid'=>$lastid));
}

if ($type == 'update_resize') {
    $eventid = $_POST['eventid'];
    list($start_date, $start_time) = explode("T", $_POST['start']);
    list($end_date, $end_time) = explode("T", $_POST['end']);

    $row = mysql_fetch_assoc(mysql_query("SELECT * FROM scheduler WHERE id = '$eventid'", $courier));
    if ($row['type'] == 'appointment') {
        $check = mysql_query("SELECT * FROM scheduler WHERE start_date = '$start_date' AND (start_time <= '$end_time' AND end_time >= '$start_time') AND type='appointment' AND doctor_id = '$id' AND NOT id = '$eventid' ", $courier);
        if (mysql_num_rows($check) > 0) {
            echo json_encode(array('status' => 'overlap', 'laps' => mysql_num_rows($check)));
            exit;
        } else {
            $update = mysql_query("UPDATE scheduler SET start_time = '$start_time',end_time = '$end_time' where id='$eventid'", $courier);
        }
    } else {
        $update = mysql_query("UPDATE scheduler SET start_time = '$start_time',end_time = '$end_time' where id='$eventid'", $courier);
    }

    if ($update)
        echo json_encode(array('status' => 'success'));
    else
        echo json_encode(array('status' => 'failed'));
}

if($type == 'update_event')
{
	$eventid = $_POST['eventid'];
	list($patient_id, $patient_no, $patient_name) = explode("@@@",mysql_real_escape_string($_POST['patient_id_receiver']));
	$parent_id = mysql_real_escape_string($_POST['parent_id']);
	$update_all = mysql_real_escape_string($_POST['update_all']);
	$title = mysql_real_escape_string($_POST['event_title']);
        $start_date = mysql_real_escape_string($_POST['schedule_start_date']);
        $start_time = date('H:i', strtotime(mysql_real_escape_string($_POST['start_time_picker'])));
        $end_time = date('H:i', strtotime(mysql_real_escape_string($_POST['end_time_picker'])));
        $description = mysql_real_escape_string($_POST['description']);
        $repeat_event = mysql_real_escape_string($_POST['repeat_event']);
        $end_date = mysql_real_escape_string($_POST['ends_on_date']);
        $repeat_interval = mysql_real_escape_string($_POST['repeat_interval']);
        $repeat_every = mysql_real_escape_string($_POST['repeat_frequency']);
        $repeat_frequency_text = mysql_real_escape_string($_POST['repeat_frequency_text']);
        $repeat_on = $_POST['repeat_on'];
        list($event_type, $event_type2) = explode("@@@",$_POST['event_type']);
        $urgency = mysql_real_escape_string($_POST['urgency_'.$patient_id]);
        $send_sms = $_POST['send_sms'];
        $ends_on_text = mysql_real_escape_string($_POST['ends_on_text']);
        $occurrences = mysql_real_escape_string($_POST['occurrences']);
        
        
        if($update_all == 'all_ff'){
            mysql_query("UPDATE scheduler SET title='$title',start_time='$start_time',end_time='$end_time',description='$description',allDay='false' where start_date = '$start_date' AND start_time >= '$start_time' AND parent_id='$parent_id'", $courier);   
            mysql_query("UPDATE scheduler SET title='$title',start_time='$start_time',end_time='$end_time',description='$description',allDay='false' where start_date > '$start_date' AND parent_id='$parent_id'", $courier);   
          }else if($update_all == 'all_events'){
          mysql_query("UPDATE scheduler SET title='$title',start_time='$start_time',end_time='$end_time',description='$description',allDay='false' where parent_id='$parent_id'", $courier);   
        }
        
        if($event_type2 == 'appointment'){
        $check = mysql_query("SELECT * FROM scheduler WHERE start_date = '$start_date' AND (start_time <= '$end_time' AND end_time >= '$start_time') AND type='appointment' AND doctor_id = '$id' AND NOT id = '$eventid' ", $courier);
         if(mysql_num_rows($check) > 0){ ?>
        <div class="alert alert-danger alert-dismissable" style="border-radius: 0px">
                                    <button type="button" class="close" data-dismiss="alert" aria-hidden="true"></button>
                                    <span style="font-weight: normal;">This Appointment Overlaps with <b><?php echo mysql_num_rows($check); ?></b> other appointments schedules.</span> <strong style="cursor: pointer;" onclick="$('#clash_appointment_wrapper').slideToggle(function(){if($(this).is(':hidden')) {$('.form_action_buttons').show();}else{$('.form_action_buttons').hide();}});$('#main_appointment_wrapper').slideToggle();">View Details</strong>
	</div>
<div class="row" id="clash_appointment_wrapper" style="height:270px;overflow-y: scroll;display: none;">
        <?php
            while($row = mysql_fetch_assoc($check)){
                $row2 = mysql_fetch_assoc(mysql_query("SELECT picpath,insurance_no1,insurance_company1 FROM patient_information WHERE id = '".$row['patient_id']."'", $courier));
                $picpath=$row2['picpath']; 
                if(!$picpath){ 
                            if($row2['insurance_no1']){
                            $inspicpath=getpicpath($row2['insurance_company1']);
                            if($inspicpath){
                            $member_id=preg_replace("/[^0-9]/","",$row2['insurance_no1']);
                            $picpath=$inspicpath."/".ltrim($member_id,0).".jpg";
                            }else{
                            $picpath="images/profile.gif";		
                            }
                            }else{
                            $picpath="images/profile.gif";	
                            }
                }
                ?>
                <div class="col-md-12" style="height: 82px;">
                            <div class="todo-tasklist">
                                <div class="todo-tasklist-item todo-tasklist-item-border-green">
                                    <img class="todo-userpic pull-left" src="<?php echo $picpath; ?>" width="30px" height="30px">
                                    <div class="todo-tasklist-item-title">
                                         <?php echo $row['patient_name'];?> - (<?php echo $row['patient_no'];?>)
                                        
                                    </div>
                                    <div class="todo-tasklist-controls pull-left">
                                            <i class="icon-calendar"></i> <i><span class="todo-tasklist-date"><i class="fa fa-calendar"></i> <?php echo date('jS F, Y g:i a', strtotime($row['start_date'] . ' ' . $row['start_time'])) . ' - ' . date('g:i a', strtotime($row['end_time'])); ?> </span></i>
                                    </div>
                                </div>
                            </div>
                </div>
                   
          <?php  } ?>
            </div>
<?php exit;
         } 
        
        $details = mysql_fetch_assoc(mysql_query("SELECT s.* FROM scheduler s WHERE id = '$eventid'", $courier));
        $update = mysql_query("UPDATE scheduler SET title='Appointment',start_date='$start_date',start_time='$start_time',end_date='$start_date',end_time='$end_time',description='$description',allDay='false',new_old_event='old',type='appointment',urgency='$urgency',patient_id='$patient_id',patient_no='$patient_no',patient_name='$patient_name' where id='$eventid'", $courier);   
        //SEND SMS TO PARTICIPANTS OF THIS APPOINTMENT
        if ($details['status'] == 'Pending' && $send_sms == 'on') {
            $sms_permission = mysql_fetch_assoc(mysql_query("SELECT * FROM sms_details", $courier));
            $patient_info = mysql_fetch_assoc(mysql_query("SELECT othernames,tel FROM patient_information WHERE id = '$patient_id'", $courier));
            $doc_info = mysql_fetch_assoc(mysql_query("SELECT title,surname,tel,area_specialisation FROM users WHERE id = '" . $details['doctor_id'] . "'", $courier));
            
            $patient = $patient_info['othernames'];
            $patient_full = $patient_info['othernames'].' '.$patient_info['surname'];
            $doc = $doc_info['title'] . ' ' . $doc_info['surname'];
            $doc2 = $doc_info['title'] . ' ' . $doc_info['surname'].' - '.$doc_info['area_specialisation'];
            $sender = $sms_permission['sender_id'];
            
            $date = date('l, jS F Y', strtotime($start_date));
            $time = date('g:ia', strtotime($start_time));
            $time2 = date('g:ia', strtotime($end_time));

            $patient_phone = substr($patient_info['tel'], 0, 10);
            $doc_phone = substr($doc_info['tel'], 0, 10);
            
            if ($details['new_old_event'] == 'new') {
                $msg_patient = "Hello $patient, Your Appointment Schedule with $doc2 will be on $date at $time - $time2. Thank you.";
                $msg_doc = "Hello $doc, You have an Appointment Schedule with $patient_full on $date at $time - $time2. Thank you.";
            } else {
                $msg_patient = "Hello $patient, Your Appointment Schedule with $doc2 has been rescheduled to $date at $time - $time2. Thank you.";
                $msg_doc = "Hello $doc, Your Appointment Schedule with $patient_full has been rescheduled to $date at $time - $time2. Thank you.";
            }
            
            SMS_API_RETURN($patient_phone, $msg_patient, $sender);
            SMS_API_RETURN($doc_phone, $msg_doc, $sender);
        }
    }else{
        $update = mysql_query("UPDATE scheduler SET title='$title',start_date='$start_date',start_time='$start_time',end_date='$start_date',end_time='$end_time',description='$description',allDay='false',new_old_event='old' where id='$eventid'", $courier);   
        }
        
        if($repeat_event == 'on' && $event_type2 == 'normal'){
             $delete = mysql_query("DELETE FROM scheduler where id='$eventid'", $courier);
             if(empty($start_date)){$start_date = date('Y-m-d');}
             if(empty($end_date)){$end_date = date('Y-m-d', strtotime('+ 1 WEEKS', $start_date));}

            if($repeat_interval=='0'){$text = 'D';}
            if($repeat_interval=='2'){$text = 'W';}
            if($repeat_interval=='3'){$text = 'M';}
            if($repeat_interval=='4'){$text = 'Y';}
            
            if($repeat_interval == '2') {
                $week = array("Sunday" => 0, "Saturday" => 6, "Friday" => 5, "Thursday" => 4, "Wednesday" => 3, "Tuesday" => 2, "Monday" => 1);
                foreach ($repeat_on as $repeats) {$ron = $ron . $week[$repeats] . ',';}
                $ron = substr($ron, 0, -1);
                $ronS = explode(',', $ron);
                foreach ($ronS as $ronS) {$res[] = $ronS;}

               $curday= 7-date("N",strtotime($start_date));
               $max = mysql_fetch_assoc(mysql_query("SELECT MAX(parent_id)+1 as parent_id FROM scheduler", $courier));
//               if($ends_on_text == 'occurrences'){
//                 $end_date = date('Y-m-d',strtotime('+'.($repeat_every*$occurrences).' '.$repeat_frequency_text, strtotime($start_date)));
//                }
              
               foreach ($res as $key=>$val) {
                    $ncount= $res[$key]-7;
                    $nc=$ncount+$curday;
                    $cday= date('Y-m-d',strtotime('+'.$nc.' day',strtotime($start_date)));
                    $datetime = new DateTime("$cday");
                    if ( $datetime->format("w") >= 0 )
                     {
                      if(strtotime($start_date) <= strtotime($cday)){}
                      else{$cday = date('Y-m-d',strtotime('+7 day',strtotime($cday)));}

                    if (!empty($cday) && !empty($end_date)) {
                            $begin = new DateTime($cday);
                            $end = new DateTime($end_date);
//                          $end = $end->modify('+' . $repeat_every . ' ' . $repeat_frequency_text);
                            $interval = new DateInterval('P' . $repeat_every . $text);
                            $daterange = new DatePeriod($begin, $interval, $end);
                            foreach ($daterange as $date) {
                                $s = $date->format("Y-m-d");
                                mysql_query("INSERT INTO scheduler(doctor_id, parent_id, title, start_date, start_time, end_date, end_time, description, new_old_event) VALUES('$id','" . $max['parent_id'] . "','$title','$s','$start_time','$s','$end_time','$description','old')", $courier);
                            }
                        }
                    }   
               }
            } else{
            if($ends_on_text == 'occurrences'){
               $end_date = date('Y-m-d',strtotime('+'.($repeat_every*$occurrences).' '.$repeat_frequency_text, strtotime($start_date)));
              }
            if (!empty($start_date) && !empty($end_date)) {
                        $begin = new DateTime($start_date);
                        $end = new DateTime($end_date);
//                      $end = $end->modify( '+'.$repeat_every.' '.$repeat_frequency_text );
                        $interval = new DateInterval('P' . $repeat_every . $text);
                        $daterange = new DatePeriod($begin, $interval, $end);
                        $max = mysql_fetch_assoc(mysql_query("SELECT MAX(parent_id)+1 as parent_id FROM scheduler", $courier));
                        foreach ($daterange as $date) {
                            $s = $date->format("Y-m-d");
                            mysql_query("INSERT INTO scheduler(doctor_id, parent_id, title, start_date, start_time, end_date, end_time, description, new_old_event) VALUES('$id','" . $max['parent_id'] . "','$title','$s','$start_time','$s','$end_time','$description', 'old')", $courier);
                        }
                    }
                 }
               
                    }
	
//        if($update)
		echo 'success';
//	else
//		echo 'failed';
}

if($type == 'resetdate')
{
	$title = mysql_real_escape_string($_POST['title']);
	list($startdate,$start_time) = explode("T", $_POST['start']);
	list($enddate,$end_time) = explode("T", $_POST['end']);
	$eventid = $_POST['eventid'];
	$row = mysql_fetch_assoc(mysql_query("SELECT * FROM scheduler WHERE id = '$eventid'", $courier));
        if($row['type']=='appointment'){
            $check = mysql_query("SELECT * FROM scheduler WHERE start_date = '$startdate' AND (start_time <= '$end_time' AND end_time >= '$start_time') AND type='appointment' AND doctor_id = '$id' AND NOT id = '$eventid' ", $courier);
         if(mysql_num_rows($check) > 0){
           echo json_encode(array('status'=>'overlap','laps'=>mysql_num_rows($check))); 
           exit;
         }else{
        $update = mysql_query("UPDATE scheduler SET title='$title', start_date = '$startdate', start_time = '$start_time', end_date = '$enddate', end_time = '$end_time' where id='$eventid'");
         }
        }else{
        $update = mysql_query("UPDATE scheduler SET title='$title', start_date = '$startdate', start_time = '$start_time', end_date = '$enddate', end_time = '$end_time' where id='$eventid'");
        }
        if($update)
		echo json_encode(array('status'=>'success'));
	else
		echo json_encode(array('status'=>'failed'));
}

if($type == 'delete_event')
{
        $eventid = $_POST['eventid'];
        $parent_id = mysql_real_escape_string($_POST['parent_id']);
	$update_all = mysql_real_escape_string($_POST['update_all']);
        $start_date = mysql_real_escape_string($_POST['schedule_start_date']);
        $start_time = date('H:i', strtotime($_POST['start_time_picker']));
        
        $delete = mysql_query("DELETE FROM scheduler where id='$eventid'", $courier);
        if($update_all == 'all_ff'){
          mysql_query("DELETE FROM scheduler where start_date >= '$start_date' AND start_time >= '$start_time' AND parent_id='$parent_id'", $courier);   
        }else if($update_all == 'all_events'){
          mysql_query("DELETE FROM scheduler where parent_id='$parent_id'", $courier);   
        }
	
        if($delete)
		echo json_encode(array('status'=>'success'));
	else
		echo json_encode(array('status'=>'failed'));
}

if($type == 'remove')
{
	$eventid = $_POST['eventid'];
	$delete = mysql_query("DELETE FROM scheduler where id='$eventid'", $courier);
	if($delete)
		echo json_encode(array('status'=>'success'));
	else
		echo json_encode(array('status'=>'failed'));
}

if($type == 'fetch')
{
	$events = array();
	$query = mysql_query( "SELECT * FROM scheduler WHERE doctor_id = '$id'", $courier);
	while($fetch = mysql_fetch_array($query,MYSQL_ASSOC))
	{
    $e = array();
    $e['id'] = $fetch['id'];
    $e['title'] = $fetch['title'];
    $e['start'] = $fetch['start_date'].' '.$fetch['start_time'];
    $e['end'] = $fetch['end_date'].' '.$fetch['end_time'];
    $allday = ($fetch['allDay'] == "true") ? true : false;
    $e['allDay'] = $allday;

    array_push($events, $e);
	}
	echo json_encode($events);
}

if($type == 'fetch_single') {
   $id = mysql_real_escape_string($_POST['id']);
   $row = mysql_fetch_assoc(mysql_query( "SELECT * FROM scheduler WHERE id = '$id'", $courier)); 
   $count = mysql_num_rows(mysql_query("SELECT * FROM scheduler WHERE parent_id = '".$row['parent_id']."'", $courier));
    
   if($row['patient_id'] > 0) {
   $row2 = mysql_fetch_assoc(mysql_query("SELECT picpath,insurance_no1,insurance_company1 FROM patient_information WHERE id = '".$row['patient_id']."'", $courier));
   $picpath=$row2['picpath']; 
        if(!$picpath){ 
                    if($row2['insurance_no1']){
                    $inspicpath=getpicpath($row2['insurance_company1']);
                    if($inspicpath){
                    $member_id=preg_replace("/[^0-9]/","",$row2['insurance_no1']);
                    $picpath=$inspicpath."/".ltrim($member_id,0).".jpg";
                    }else{
                    $picpath="images/profile.gif";		
                    }
                    }else{
                    $picpath="images/profile.gif";	
                    }
        }
   }
        
    ?>
 

            <div class="form">
                <div id="clash_appointment_div"></div>
                <form name="form" class="form-horizontal" role="form" id="main_appointment_wrapper">
                    <div class="row">
                        <div class="col-md-12">
                        <input type="hidden" name="parent_id" id="parent_id" value="<?php echo $row['parent_id'];?>">
                        <?php if($row['new_old_event'] == 'new'){ ?>
                            <div class="form-body">
                                <div class="row">
                                    <div class="col-md-12">
                                        <select onchange="event_headings(this.value);" name="event_type" id="event_type" class="form-control">
                                            <option <?php if($row['type'] == 'event'){echo 'selected=""';} ?> value="Normal Event@@@normal">Normal Event</option>
                                            <option  <?php if($row['type'] == 'appointment'){echo 'selected=""';} ?> value="Appointment Booking@@@appointment">Appointment Booking</option>
                                        </select>
                                    </div>
                                </div>
                            </div>
                            <?php } ?>
                            <?php if($count > 1){ ?>
                            <div class="form-body">
                                <div class="row">
                                    <div class="col-md-4">
                                        <input checked="" title="All other events in the series will remain." id="only_this" type="radio" name="update_all" value="only_this"> <label title="All other events in the series will remain." for="only_this">Only this instance</label>
                                    </div>
                                    <div class="col-md-4">
                                        <input title="This and all the following events will be update" type="radio" name="update_all" id="all_ff" value="all_ff"> <label title="This and all the following events will be update" for="all_ff">Only Future Events </label>
                                    </div>
                                    <div class="col-md-4">
                                        <input title="All events in the series will be updated." type="radio" name="update_all" id="all_events" value="all_events"> <label title="All events in the series will be updated." for="all_events"> All Events </label>
                                    </div>
                                </div>
                            </div> 
                            <?php } ?>
                          <?php if($row['new_old_event'] == 'new' || $row['type'] == 'event'){ ?>
                            <div class="form-body hide_all" id="event_title_wrapper">
                                <div class="row">
                                    <div class="col-md-12">
                                        <input type="text" name="event_title" id="event_title" class="form-control" placeholder="Enter Title" value="<?php echo $row['title'];?>">
                                    </div>
                                </div>
                            </div>
                          <?php } ?>
                         <?php if($row['type'] == 'appointment' || $row['new_old_event'] == 'new'){ ?>
                         <div class="hide_all patient_wrapper" <?php if($row['new_old_event'] == 'new'){ ?> style="display: none;" <?php } ?>>
                             <?php if($row['new_old_event'] == 'new'){ ?>
                             <div class="form-body">
                                        <div class="row">
                                            <div class="col-md-6">
                                                <input onkeyup="process_information_2('getstep.php', 'search_patients_appointment', 'search_result_wrapper');" type="text" name="search_text" id="search_text" class="form-control" placeholder="Search Text">
                                            </div>
                                            <div class="col-md-6">
                                                <select onchange="process_information_2('getstep.php', 'search_patients_appointment', 'search_result_wrapper');" name="search_by" id="search_by" class="form-control">
                                                    <option value="">Search By</option>
                                                    <option selected="" value="patient_id">Patient ID/Old Card No.</option>
                                                    <option value="surname">Patient Surname</option>
                                                    <option value="othername">Patient Othername</option>
                                                </select>
                                            </div>
                                        </div>
                             </div>
                         <?php } ?>
                            <div class="form-body">
                          <div class="row" id="search_result_wrapper" <?php if($row['patient_id'] < 1) { ?> style="height:200px;overflow-y: scroll;display: none;" <?php } ?>>
                                    <div class="col-md-12">
                                       <?php if($row['type'] == 'appointment'){ ?>
                                        <input type="hidden" name="event_type" value="Appointment Booking@@@appointment">
                                       <?php } ?>    
                                        <div class="todo-tasklist">
                                                    <div class="todo-tasklist-item todo-tasklist-item-border-green" id="<?php echo $row['patient_id'];?>">
                                                        <i class="icon-ok todo-userpic pull-right" style="margin-top:10px;display: none;"></i>
                                                        <img class="todo-userpic pull-left" src="<?php echo $picpath;?>" width="30px" height="30px">
                                                        <div class="todo-tasklist-item-title" id="<?php echo $row['patient_id'].'@@@'.$row['patient_no'].'@@@'.$row['patient_name'];?>">
                                                            <?php echo $row['patient_name'];?> - (<?php echo $row['patient_no'];?>)
                                                        </div>
                                                        <div class="todo-tasklist-item-text"></div>
                                                        <div class="todo-tasklist-controls pull-right">
                                                                <i><span class="todo-tasklist-date"><i class="icon-calendar"></i> <?php echo date('l jS F, Y g:i a', strtotime($row['start_date'].' '.$row['start_time'])).' - '.date('g:i a', strtotime($row['end_time']));?> </span></i>
                                                                <label for="urgency_Normal_<?php echo $i;?>" class="todo-tasklist-badge badge danger badge-roundless badge-danger">Appointment Booking</label>
                                                            </div>

                                                    </div>
                                                </div>
                                   </div>
                                </div>
                             <input type="hidden" name="patient_id_receiver" id="patient_id_receiver" value="<?php echo $row['patient_id'].'@@@'.$row['patient_no'].'@@@'.$row['patient_name'];?>">
                        </div>
                            
                            </div>
                            <?php } ?>
                             <div class="form-body">
                                <div class="row">
                                    <div class="col-md-6">
                                        <input onchange="$('#schedule_end_date').val(this.value);CheckCurrentDay();" type="text" name="schedule_start_date" id="schedule_start_date" class="form-control date_time_picker" placeholder="Start Date" value="<?php echo $row['start_date'];?>">
                                    </div>
                                    <div class="col-md-6">
                                        <input type="text" name="start_time_picker" id="start_time_picker" class="form-control time_picker" placeholder="Start Time" value="<?php echo date('g:ia', strtotime($row['start_time']));?>">
                                    </div>
                                </div>
                            </div>   
                            <div class="form-body">
                                <div class="row">
                                    <div class="col-md-6">
                                        <input readonly="" onchange="$('#date_time_picker').val(this.value);" type="text" name="schedule_end_date" id="schedule_end_date" class="form-control" placeholder="End Date" value="<?php echo $row['end_date'];?>">
                                    </div>
                                    <div class="col-md-6">
                                        <input type="text" name="end_time_picker" id="end_time_picker" class="form-control time_picker" placeholder="End Time" value="<?php echo date('g:ia', strtotime($row['end_time']));?>">
                                    </div>
                                </div>
                            </div>
                            <div class="form-body">
                                <div class="row">
                                    <div class="col-md-8">
                                        <textarea name="description" id="description" class="form-control" placeholder="Description"><?php echo $row['description'];?></textarea>
                                    </div>
                        <?php if($row['new_old_event'] == 'new'){ ?>
                                    <div class="col-md-4 repeat_event_wrapper">
                                        <input onchange='if ($("#repeat_event").prop("checked") == true) { $("#repeat_event_wrapper").show();}else {$("#repeat_event_wrapper").hide();};CheckCurrentDay();' type="checkbox" name="repeat_event" id="repeat_event"> <label for="repeat_event"> Repeat Event </label>
                                    </div>
                        <?php } ?>
                                    <div class="col-md-4 hide_all patient_wrapper" <?php if($row['type'] != 'appointment'){ ?> style="display: none;" <?php } ?>>
                                        <input type="checkbox" name="send_sms" id="send_sms"> <label for="send_sms"> <i class="icon-envelope"></i> Send SMS Notification </label>
                                    </div>
                                </div>
                            </div>
                       <?php if($row['new_old_event'] == 'new'){ ?>
                         <div class="form-body repeat_event_wrapper_2" id="repeat_event_wrapper" style="display: none;">
                                <div class="row">
                                    <div class="col-md-12">
                                         <table class="table">
                                <tbody>
                                    <tr>
                                        <th>Repeats:</th>
                                        <td>
                                            <select onchange="Summary();" name="repeat_frequency" id="repeat_frequency">
                                                <?php for ($i = 1; $i <= 30; $i++) { ?>
                                                    <option value="<?php echo $i; ?>"><?php echo $i; ?></option>
                                                <?php } ?>
                                            </select>
                                            <select onchange="Repeat_Interval(this.value);Summary();if(this.value!='2'){$('.occurrences').show();}else{$('.occurrences').hide();}" name="repeat_interval" id="repeat_interval" title="Repeats">
                                                <option value="0" title="Days">Days</option>
                                                <option value="2" title="Weeks">Weeks</option>
                                                <option value="3" title="Months">Months</option>
                                                <option value="4" title="Years">Years</option>
                                            </select>
                                            <input type="hidden" name="repeat_frequency_text" id="repeat_frequency_text" value="Days">
                                        </td>
                                    </tr>
                                    <tr id="repeat_on_weekly" style="display: none;">
                                        <th>Repeat On:</th>
                                        <td>
                                            <input class="repeat_on" onchange="Summary();" value="Sunday" multiple="" type="checkbox" style="" name="repeat_on[]" title="Sunday"> S 
                                            <input class="repeat_on" onchange="Summary();" value="Monday" multiple="" type="checkbox" style="" name="repeat_on[]" title="Monday"> M
                                            <input class="repeat_on" onchange="Summary();" value="Tuesday" multiple="" type="checkbox" style="" name="repeat_on[]" title="Tuesday"> T
                                            <input class="repeat_on" onchange="Summary();" value="Wednesday" multiple="" type="checkbox" style="" name="repeat_on[]" title="Wednesday"> W
                                            <input class="repeat_on" onchange="Summary();" value="Thursday" multiple="" type="checkbox" style="" name="repeat_on[]" title="Thursday"> T
                                            <input class="repeat_on" onchange="Summary();" value="Friday" multiple="" type="checkbox" style="" name="repeat_on[]" title="Friday"> F
                                            <input class="repeat_on" onchange="Summary();" value="Saturday" multiple="" type="checkbox" style="" name="repeat_on[]" title="Saturday"> S
                                        </td>
                                    </tr>
                                    <tr>
                                        <th>Ends:</th>
                                        <td>
                                            <div class="occurrences">
                                            <input class="occurrences" type="radio" name="ends_on_text" id="occurrences" value="occurrences"> After  &nbsp;&nbsp; 
                                            <select class="occurrences" onfocus="$('#occurrences').prop('checked', true);" style="width:45px" name="occurrences" id="occurrences">
                                                <?php for ($i = 1;$i <=30;$i++) { ?>
                                                <option value="<?php echo $i;?>"><?php echo $i;?></option> 
                                                <?php }  ?>
                                            </select> Occurrences <br>
                                            </div>
                                            <input checked="" type="radio" name="ends_on_text" id="ends_date" value="On"> On &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input onfocus="$('#ends_date').prop('checked', true);" readonly="" style="box-shadow:none;border: 1px solid #e5e5e5;" type="text" name="ends_on_date" id="date_time_picker" class="date_time_picker" placeholder="Ends On">
                                        </td>
                                    </tr>
                                    <tr>
                                        <th>Summary:</th>
                                        <td style="font-weight:bold;color:blue;" id="summary_wrapper">
                                           Daily 
                                        </td>
                                    </tr>
                                </tbody>
                            </table> 
                                    </div>
                                </div>
                            </div>   
                           
                       <?php } ?>
                        </div>
                        
                    </div>
                </form>
            </div>
                        
    
<?php }

?>