<?php  

if(!function_exists('module_list')){
  function module_list($name,$selected_id=null,  $submit_on_change=false, $disabled=false){
    // $items = array('1' => _("left"), '2' => _("right"), '3' => _("center"));

      $items = array('employees' => _("Employees"));

    return array_selector($name, $selected_id, $items, 
        array( 
            'select_submit'=> $submit_on_change,
            'async' => false,
            'disabled' => $disabled ) ); // FIX?

  }
}

if(!function_exists('custom_filed_list')){
function custom_filed_list($name,$selected_id=null,  $submit_on_change=false, $disabled=false){
    // $items = array('1' => _("left"), '2' => _("right"), '3' => _("center"));

      $items = array('text' => _("Text"),'amount' => _("Amount"), 'dropdown' => _("Drop down"),'checkbox'=>_("Check box"),'radiobutton'=>_('Radio button'),'function'=>_('Function'));

    return array_selector($name, $selected_id, $items, 
        array( 
            'select_submit'=> $submit_on_change,
            'async' => false,
            'disabled' => $disabled ) ); // FIX?
}
}
if(!function_exists('field_item_list')){
function field_item_list($count ,$name,$selected_id=null,  $submit_on_change=false, $disabled=false){
    // $items = array('1' => _("left"), '2' => _("right"), '3' => _("center"));

      $items = array();
      for ($i=1; $i <=$count; $i++) { 
          $items[$i] =$i;
      }

    return array_selector($name, $selected_id, $items, 
        array( 
            'select_submit'=> $submit_on_change,
            'async' => false,
            'disabled' => $disabled ) ); // FIX?
}
}
if(!function_exists('draw_dropdown')){
function draw_dropdown($list,$name,$selected_id=null,  $submit_on_change=false, $disabled=false){
    // $items = array('1' => _("left"), '2' => _("right"), '3' => _("center"));

      $items = array();
      for ($i=1; $i <=count($list); $i++) { 
          $items[$list[$i]] =$list[$i];
      }

    return array_selector($name, $selected_id, $items, 
        array( 
            'select_submit'=> $submit_on_change,
            'async' => false,
            'disabled' => $disabled ) ); // FIX?
}
}
if(!function_exists('draw_radiobutton')){
function draw_radiobutton($pick_type,$name,$selected_id=null){
      $sql = "SELECT id, description FROM ".TB_PREF."kv_crm_picklist_values ";
      $picklist =GetDataFilter('kv_empl_picklist',array('id','description'),array('type'=>$pick_type));
      $list =array();
      $counter=1;
      foreach ($picklist as $value) {
        $list[$value['id']]=$value['description'];
      }
      $radio ='';
      foreach ($list as $key => $value) {
        
          $selected_id=null;
          if (!$selected_id && $counter ==1) {
            $selected_id =$key;
          }
          $counter++;
        $radio .= radio($value,$name,$key,$selected_id);
      }
      return $radio;
}
}
if(!function_exists('draw_position_radiobutton')){
function draw_position_radiobutton($name,$selected_id=null){
   // radio($list[1],$name,$list[1], null, true);
      $radio ='';
      $select =null;
      $list=array(1=>'left',2=>'right');
      for ($i=1; $i <=count($list); $i++) {
        if($list[$i] == $selected_id){
          $select =null;
        } 
        $radio .= radio($list[$i],$name,$list[$i], null);  
      }
      return $radio;
}
}
if(!function_exists('custom_field_section')){
function custom_field_section($module,$ref_id,$section_id){
  //for custom fields

  $field_values= array();
  if($section_id == 0 ||$section_id == 1 ){
    $section_position ='left';
  }elseif($section_id == 2){
    $section_position ='right';
  }

  $section_count =get_headers_group_count($module);

  $data =GetDataJoin('custom_fields AS fields',array(
    0=>array('join' => 'INNER', 'table_name' => 'custom_headers AS headers', 'conditions' => 'fields.header_id = headers.id')),array('fields.*','headers.*'),array('fields.module'=>$module),array(''=>'fields.header_id')
  );
  $default_header =GetAll('custom_fields',array('header_id'=>0,'module'=>$module));
  $result =$data;
  foreach ($default_header as $d_header_row) {
    $result[]=$d_header_row;
  }

  if($result){
      foreach ($result as $myrow) {
          if($myrow['header_id'] == 0){
            $myrow['position']='left';
          }
          if($myrow['position'] ==$section_position){
            $_POST[$myrow['field_name']] =null;
          }
      }
      if($ref_id){
      $field_values =get_custom_values($module,$ref_id);
      if($result){
        foreach ($result as $myrow) {
          if($myrow['header_id'] == 0){
            $myrow['position']='left';
          }
          if($myrow['position'] ==$section_position){
            $_POST[$myrow['field_name']] =$field_values[$myrow['field_name']];
          }
        }
      }
    } 

    foreach ($section_count as $section_row) {
      if($section_row['position'] ==$section_position ){
        
        if($section_id ==0){
          table_section_title(_("Custom Fields"));
        }else{
          table_section_title(_($section_row['header_label']));
        }
       
        $result2 =GetAll('custom_fields',array('module'=>$module,'header_id'=>$section_row['header_id']));
        
        foreach ($result2 as $myrow) {
          if(!isset($_POST[$myrow['field_name']]) || !$field_values){
            $_POST[$myrow['field_name']] =null;
          }
          if($myrow['field']=='text'){
            text_row(_($myrow['label']), $myrow['field_name'], $_POST[$myrow['field_name']], 32, 30);
          }else if($myrow['field']=='amount'){
            amount_row(_($myrow['label']), $myrow['field_name'], $_POST[$myrow['field_name']]);
          }elseif ($myrow['field']=='dropdown') {
            empl_picklist_row(_($myrow['label']) ,$myrow['field_name'],null, false, false,$myrow['pick_type']);
          }elseif ($myrow['field']=='checkbox') {
            check_row(_($myrow['label']),$myrow['field_name'],$_POST[$myrow['field_name']]);
          }elseif ($myrow['field']=='radiobutton') {
            label_row(_($myrow['label']),draw_radiobutton($myrow['pick_type'],$myrow['field_name'],$_POST[$myrow['field_name']]));
          }elseif ($myrow['field']=='function') {
            if(function_exists($myrow['pick_type']) && $_POST[$myrow['field_name']] ==''){
              $_POST[$myrow['field_name']] =$myrow['pick_type']();
            }
            text_row(_($myrow['label']), $myrow['field_name'], $_POST[$myrow['field_name']], 32, 30);
          }
        }
      }
    }    
  }
 //end custom fields
}

}
if(!function_exists('custom_field_update')){
function custom_field_update($module,$ref_id){

  //for update custom value 
  $result =get_custom_fields($module);
  if($result){
    $values = array();
    foreach ($result as $myrow) {
     
      if ($myrow['field']=='amount') {
        $values[$myrow['field_name']] =input_num($myrow['field_name']);
      }elseif ($myrow['field']=='checkbox') {
        $values[$myrow['field_name']] =check_value($myrow['field_name']);
      }else{
        $values[$myrow['field_name']] =$_POST[$myrow['field_name']];
      }
    }
      add_custom_values($module,$ref_id,$values);
  }
  //end update custom value
}
}
if(!function_exists('custom_field_insert')){

function custom_field_insert($module,$ref_id){

  $result =get_custom_fields($module);
    if($result){
      $values = array();
      foreach ($result as $myrow) {
          // if ($myrow['field']=='amount' && !is_numeric($_POST[$myrow['field_name']])) {
          //   display_error($myrow['label']." must be a numeric value");
          //   cancel_transaction();
          //   return false;
          // }
          if ($myrow['field']=='amount') {
            $values[$myrow['field_name']] =input_num($_POST[$myrow['field_name']]);
          }
          elseif ($myrow['field']=='checkbox') {
            $values[$myrow['field_name']] =['label'=>$myrow['label'],'value'=>check_value($myrow['field_name'])];
          }else{
            $values[$myrow['field_name']] =['label'=>$myrow['label'],'value'=>$_POST[$myrow['field_name']]];
          }
      }
      add_custom_values($module,$ref_id,base64_encode(serialize($values))); 
    }
}
}
if(!function_exists('custom_header_list')){
  function custom_header_list($name, $selected_id=null, $spec_option=false, $submit_on_change=false,$module=''){
      $sql = "SELECT * FROM ".TB_PREF."custom_headers WHERE module = ".db_escape($module);

      $ret = combo_input($name, $selected_id, $sql, 'id', 'header_label',
      array(
          'spec_option' => $spec_option === true ? _("All Users") : $spec_option,
          'spec_id' => 0,
          'select_submit'=> $submit_on_change,
          'async' => false,
      ),'' );    
      return $ret;
  } 
}
if(!function_exists('custom_header_list_cells')){
  function custom_header_list_cells($label, $name, $selected_id=null, $all_option=false, $submit_on_change=false,
    $module=''){
      if ($label != null)
          echo "<td>$label</td>\n";
      echo "<td nowrap>";
      echo custom_header_list($name, $selected_id, $all_option, $submit_on_change,$module);
      echo "</td>\n";
  }
}
?>