<?php

/* =======================================================\
  |                        Church                          |
  |--------------------------------------------------------|
  \======================================================= */

function member_list($name, $id = null, $spec_option = false, $submit_on_change = false, $show_inactive = false, $dept = false) {
    global $path_to_root,$Ajax;
    if($id == null){
        $id = get_post($name);
    }
    $sql = "SELECT per_ID, concat(per_FirstName,' ',per_LastName) as name FROM " . TB_PREF . "person_per where per_ID=".  db_escape($id);
    $res = db_query($sql);
    $row = db_fetch($res);
    
    $allocs = array();
    if (!empty($spec_option)) {
        $allocs[''] = $spec_option;
    }
    if (!empty($row['per_ID'])) {
        $allocs[$row['per_ID']] = $row['name'];
    }
    $_selector = array_selector(
            $name, $id, $allocs, array('select_submit' => $submit_on_change,'style'=>'width:100%')
    );
    $select_js = "$('select[name=\"$name\"]').select2({
        minimumInputLength: 1,
        ajax: {
          url: '$path_to_root/modules/api/members?start=0&length=1000',
          dataType: 'json',
          processResults: function (data) {
            return {
              results: data.items
            };
          }
        }
      });";
    if (in_ajax()) {
        $Ajax->addScript(true, $select_js);
    } else
        add_js_source($select_js);
    $Ajax->activate('_page_body');

    return $_selector;
}

//--------------------------------------------------------------------------

function member_list_cells($label, $name, $id = null, $all_option = false, $submit_on_change = false, $show_inactive = false, $dept = false) {
    if ($label != null)
        echo "<td>$label</td>\n";
    echo "<td>";
    echo member_list($name, $id, $all_option, $submit_on_change, $show_inactive, $dept);
    echo "</td>\n";
}

//--------------------------------------------------------------------------

function member_list_row($label, $name, $id = null, $all_option = false, $submit_on_change = false, $show_inactive = false, $dept = false) {
    echo "<tr><td class='label'>$label</td>";
    member_list_cells(null, $name, $id, $all_option, $submit_on_change, $show_inactive, $dept);
    echo "</tr>";
}

//--------------------------------------------------------------------------

function gender_radio_row1($label, $name, $gender = null) {
    echo "<tr><td class='label'>$label</td><td>";

    if ($gender == 'Female') {
        echo "<input type='radio' name=$name value='Male'>Male&nbsp;&nbsp;&nbsp;";
        echo "<input type='radio' name=$name value='Female' checked>Female&nbsp;&nbsp;&nbsp;";
        echo "<input type='radio' name=$name value='Other'>Other";
    } elseif ($gender == 'Male') {
        echo "<input type='radio' name=$name value='Male' checked>Male&nbsp;&nbsp;&nbsp;";
        echo "<input type='radio' name=$name value='Female'>Female&nbsp;&nbsp;&nbsp;";
        echo "<input type='radio' name=$name value='Other'>Other";
    } else {
        echo "<input type='radio' name=$name value='Male'>Male&nbsp;&nbsp;&nbsp;";
        echo "<input type='radio' name=$name value='Female'>Female&nbsp;&nbsp;&nbsp;";
        echo "<input type='radio' name=$name value='Other' checked>Other";
    }
    echo "</td></tr>";
}

function month_list($name, $id = null) {
    $_selector = array_selector(
            $name, $id, $allocs = array(
        '' => _("Select Month"),
        '1' => _("January"),
        '2' => _("February"),
        '3' => _("March"),
        '4' => _("April"),
        '5' => _("May"),
        '6' => _("June"),
        '7' => _("July"),
        '8' => _("August"),
        '9' => _("September"),
        '10' => _("Octomber"),
        '11' => _("November"),
        '12' => _("December"),
            ), array('select_submit' => FALSE)
    );
    return $_selector;
}

function day_list($name, $id = null) {
    $allocs[0] = 'Day';
    for ($i = 1; $i <= 31; $i++) {
        $allocs[$i] = $i;
    }
    $_selector = array_selector(
            $name, $id, $allocs, array('select_submit' => FALSE)
    );
    return $_selector;
}
