<?php
define('FOLDER_NAME', 'rns/');
//define('DOC_FOLDER_NAME', 'rxhopes-doc/');

function referral_name_check($name, $id, $process_claim_no, $courier){
    $check = mysql_num_rows(mysql_query("select * from medical_history_referral_place where medical_note_id = '$id' AND referral_name = '$name' and process_claim_no = '$process_claim_no'", $courier));
    if($check > 0){
        echo 'checked=""';
    }
    
}

function get_patient_symptoms($id, $claim_no, $courier){
    $query = mysql_query("select * from claims_details_symptoms where medical_note_id = '$id' AND process_claim_no = '$claim_no'", $courier);
    $i=1;
    while ($row = mysql_fetch_assoc($query)) {
         $result .= '('.$i.') '. $row['symptoms'].',';
        $i++;
    } if(strlen($result) > 0){ ?>
<div id="symptoms_symptoms_<?php echo $id; ?>" style="display: block;">  <span style="text-decoration:underline;font-weight: bold;"> Symptoms </span> <br> <?php echo substr($result, 0, 40) ?><i onclick="$('#symptoms_<?php echo $id; ?>').show();$('#symptoms_symptoms_<?php echo $id; ?>').hide();" style="color:red;cursor:pointer;">...See More</i></div>
        <div id="symptoms_<?php echo $id; ?>" style="display: none;">  <span style="text-decoration:underline;font-weight: bold;"> Symptoms </span> <br>  <?php echo $result; ?> <i onclick="$('#symptoms_symptoms_<?php echo $id; ?>').show();$('#symptoms_<?php echo $id; ?>').hide();" style="color:red;cursor:pointer;">...See Less</i></div>
    
    <?php } }

function get_patient_diagnosis($id, $claim_no, $courier) {
    $query = mysql_query("select * from claims_details_query_diagnosis where medical_note_id = '$id' AND process_claim_no = '$claim_no'", $courier);
    $i = 1;
    while ($row = mysql_fetch_assoc($query)) {
        $result .= '(' . $i . ') ' . $row['disease_name'] . '<br/>';
        $i++;
    } if(strlen($result) > 0){ ?>
        <div id="diagnosis_diagnosis_<?php echo $id; ?>" style="display: block;"> <span style="text-decoration:underline;font-weight: bold;"> Diagnosis </span> <br> <?php echo substr($result, 0, 40) ?><i onclick="$('#diagnosis_<?php echo $id; ?>').show();$('#diagnosis_diagnosis_<?php echo $id; ?>').hide();" style="color:red;cursor:pointer;">...See More</i></div>
        <div id="diagnosis_<?php echo $id; ?>" style="display: none;"> <span style="text-decoration:underline;font-weight: bold;"> Diagnosis </span> <br> <?php echo $result; ?> <i onclick="$('#diagnosis_diagnosis_<?php echo $id; ?>').show();$('#diagnosis_<?php echo $id; ?>').hide();" style="color:red;cursor:pointer;">...See Less</i></div>
    <?php }
}

function get_patient_diagnostics($id, $claim_no, $courier) {
    $query = mysql_query("select * from claims_details where item_service='Laboratory' AND medical_note_id = '$id' AND process_claim_no = '$claim_no' ORDER BY id ASC", $courier);
    $i = 1;
    while ($row = mysql_fetch_assoc($query)) {
        $result .= '(' . $i . ') ' . $row['item'] . '<br/>';
        $i++;
    } if(strlen($result) > 0){ ?>
        <div id="diagnostics_diagnostics_<?php echo $id; ?>" style="display: block;"> <span style="text-decoration:underline;font-weight: bold;"> Diagnostic Requests </span> <br> <?php echo substr($result, 0, 40) ?><i onclick="$('#diagnostics_<?php echo $id; ?>').show();$('#diagnostics_diagnostics_<?php echo $id; ?>').hide();" style="color:red;cursor:pointer;">...See More</i></div>
        <div id="diagnostics_<?php echo $id; ?>" style="display: none;"> <span style="text-decoration:underline;font-weight: bold;"> Diagnostic Requests </span> <br> <?php echo $result; ?> <i onclick="$('#diagnostics_diagnostics_<?php echo $id; ?>').show();$('#diagnostics_<?php echo $id; ?>').hide();" style="color:red;cursor:pointer;">...See Less</i></div>
    <?php }
}

function get_patient_drug_prescription($id, $claim_no, $courier) {
    $query = mysql_query("select * from claims_details where item_service='Drugs' AND medical_note_id = '$id' AND process_claim_no = '$claim_no'", $courier);
    $i = 1;
    while ($row = mysql_fetch_assoc($query)) {
        $result .= '(' . $i . ') ' . $row['item'] . '<br/>';
        $i++;
    } if(strlen($result) > 0){ ?>
        <div id="drugs_drugs_<?php echo $id; ?>" style="display: block;"> <span style="text-decoration:underline;font-weight: bold;"> Drug Prescriptions </span> <br> <?php echo substr($result, 0, 40) ?><i onclick="$('#drugs_<?php echo $id; ?>').show();$('#drugs_drugs_<?php echo $id; ?>').hide();" style="color:red;cursor:pointer;">...See More</i></div>
        <div id="drugs_<?php echo $id; ?>" style="display: none;"> <span style="text-decoration:underline;font-weight: bold;"> Drug Prescriptions </span> <br> <?php echo $result; ?> <i onclick="$('#drugs_drugs_<?php echo $id; ?>').show();$('#drugs_<?php echo $id; ?>').hide();" style="color:red;cursor:pointer;">...See Less</i></div>
    <?php }
}

function get_patient_status($claim_no, $courier) {
    $query = mysql_fetch_assoc(mysql_query("select consulting_room_status from process_claim_no where process_claim_no = '$claim_no'", $courier)); 
    if($query['consulting_room_status']=='admitted'){
        $status = "Patient Admitted";
    }else if($query['consulting_room_status']=='discharged'){
        $status = "Discharged Completely";  
    }else if($query['consulting_room_status']=='discharged_review'){
        $status = "Discharged To Come for Review";  
    }else if($query['consulting_room_status']=='pending_diagnostic'){
        $status = "Awaiting Diagnostic Results";  
    }else if($query['consulting_room_status']=='pending_observation'){
        $status = "Detained for Observation";  
    }else if($query['consulting_room_status']=='referred'){
        $status = "Referred To Hospital";  
    }else if($query['consulting_room_status']=='dead'){
        $status = "Pronounced Dead";  
    }else{
        $status = "Patient Status Still Pending";
    }?>
       <div style="display: block;"> <span style="text-decoration:underline;font-weight: bold;"> Patient Status </span> <br> <?php echo $status;?></div>
    <?php 
}


function get_patient_medical_note($id, $claim_no, $i, $courier){
    $query = mysql_query("select * from medical_history where id = '$id' AND process_claim_no = '$claim_no'", $courier);
    $row = mysql_fetch_assoc($query); if(strlen($row['medical_note']) > 0){ ?>
    <div id="note_note_<?php echo $id; ?>" style="display: block;"> <span style="text-decoration:underline;font-weight: bold;"> Medical Note </span> <br> <?php echo substr($row['medical_note'], 0,40)?><i onclick="$('#note_<?php echo $id;?>').show();$('#note_note_<?php echo $id;?>').hide();" style="color:red;cursor:pointer;">...See More</i></div>
    <div id="note_<?php echo $id; ?>" style="display: none;"> <span style="text-decoration:underline;font-weight: bold;"> Medical Note </span> <br> <?php echo $row['medical_note']?> <i onclick="$('#note_note_<?php echo $id;?>').show();$('#note_<?php echo $id;?>').hide();" style="color:red;cursor:pointer;">...See Less</i></div>
<?php } }


 function patient_referral_place($id, $process_claim_no, $courier) {
    $qry = mysql_query("select * from medical_history_referral_place where medical_note_id = '$id' AND process_claim_no ='$process_claim_no'", $courier);
    while ($row = mysql_fetch_assoc($qry)) {
        echo $row['referral_name'];
    }
}

function patient_sub_info($no, $courier){
    $qry = mysql_query("select picpath, insurance_no1, insurance_company1 from patient_information where card_number = '$no'", $courier);
    $row = mysql_fetch_assoc($qry);
    return array($row['picpath'], $row['insurance_no1'], $row['insurance_company1']);
}

function list_all_allergies($table, $courier) {
    $qry = mysql_query("select * from $table order by allergy_name", $courier);
    while ($row = mysql_fetch_assoc($qry)) {
        $data[] = $row;
    }
    return $data;
}

function list_all_drugs($table, $columns, $courier) {
    $qry = mysql_query("select * from $table order by $columns", $courier);
    while ($row = mysql_fetch_assoc($qry)) {
        $data[] = $row;
    }
    return $data;
}

function list_all_allergy_reactions($courier) {
    $qry = mysql_query("select * from allergy_reactions", $courier);
    while ($row = mysql_fetch_assoc($qry)) {
        $data[] = $row;
    }
    return $data;
}

function TEXT_TO_SPEECH_API($text){
    $key = "fd16d25c54c54abf8dc7a5d0516a12b9";
    $message = urlencode($text);
    $language = 'en-us';
    $r = '-3';
    $URL = "http://api.voicerss.org?key=$key&src=$message&hl=$language&r=$r";
    return $URL;
}

function SMS_API_RETURN($phone, $message, $sender_id) {
    $nosms = ceil(strlen($message) / 160);
    $key = "B8y19qHznfmmZ7QSA690Wi4cU";
    $message2 = urlencode($message);
    $to = trim($phone);
    $URL = "http://bulk.mnotify.net/smsapi?key=$key&to=$to&msg=$message2&sender_id=$sender_id";
    $result = file_get_contents($URL);
    if ($result == "1000") {
        mysql_query("INSERT INTO sms (sms_content,no_of_sms,tel,user_id) VALUES('$message','$nosms','$phone','".$_SESSION['myMM_Userid']."')");
        return true;
    }
}

function timeAgo($timestamp) {
    $datetime1 = new DateTime("now");
    $datetime2 = date_create($timestamp);
    $diff = date_diff($datetime1, $datetime2);
    $timemsg = '';
    if ($diff->y > 0) {
        $timemsg = $diff->y . ' year' . ($diff->y > 1 ? "s" : '');
    } else if ($diff->m > 0) {
        $timemsg = $diff->m . ' month' . ($diff->m > 1 ? "s" : '');
    } else if ($diff->d > 0) {
        $timemsg = $diff->d . ' day' . ($diff->d > 1 ? "s" : '');
    } else if ($diff->h > 0) {
        $timemsg = $diff->h . ' hour' . ($diff->h > 1 ? "s" : '');
    } else if ($diff->i > 0) {
        $timemsg = $diff->i . ' minute' . ($diff->i > 1 ? "s" : '');
    } else if ($diff->s > 0) {
        $timemsg = $diff->s . ' second' . ($diff->s > 1 ? "s" : '');
    }
    $timemsg = $timemsg;
    return $timemsg;
}

function searchNameById($tableName,$nameColumn,$id,$idd) {
    $query = "SELECT $nameColumn FROM $tableName WHERE $idd=$id";
    $result = mysql_query($query) or die(mysql_error());
    $array = mysql_fetch_assoc($result);
    return $array[$nameColumn];
}