<?php session_start();
        ##CHANGE LOG - CLS STANDARD
		if (isset($_SESSION['logged']) && $_SESSION['logged'] == 'yes') {
		include ('../scripts/classes.php'); //Retrieve functions
		if ($_SESSION['lang']=="EN"){$lang="EN"; include ('../lang/en.php');}elseif ($_SESSION['lang']=="GR"){$lang="GR";include ('../lang/gr.php');}else {$lang="EN";include ('../lang/en.php');}
		$user = $_SESSION['user'];	//webnail QUERY TO get user
        $user_branch = $_SESSION['branch'];
		$array = getGlobalSettings(); foreach ($array as $settings){extract($settings);}
		$page_title = $lbl_page_title_customers; //Assign Page Title
		$mode=$lbl_mode_manage_customers; //Assign Mode
?>	
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo $page_title ?></title>
<link rel="shortcut icon" href="../img/favicon.png" />
<link href="../cms1.css" rel="stylesheet" type="text/css" /> 
<link rel="stylesheet" href="../scripts/jkit/css/jkit.css" />
<script type="text/javascript" src="../scripts/jquery.js"></script>
<script>
    function PassParam(the_id, last_name, first_name){
    var elem = document.getElementById("syycustom_3");
    document.getElementById("this_div").innerHTML = last_name + " " + first_name;
    elem.value = the_id;
    $('#jkit-lightbox-content').empty();
	// hide lightbox and shadow <div/>'s
	$('#jkit-lightbox-bg').remove();
	$('#jkit-lightbox-nav').remove();
    $('#jkit-lightbox-title').remove();
	// remove contents of lightbox in case a video or other content is actively playing
    $('#jkit-lightbox-content').remove(); 
    }
</script>
<script>
function cus_onSubmitMyCheck(){ 
    var selection = document.getElementById("ryycustom_2").value;
    var hidden = document.getElementById("syycustom_3").value;
    var reference = '<?php echo _REFERENCE; ?>';
        if (selection != reference){
        document.getElementById("syycustom_3").value = "";
        return true;
        }else if (selection == reference && hidden == ""){
        alert ('<?php echo _WHEN_CUSTOMER_BY_REF_TEXT ?>');
        return false;
        }else if (selection == reference && hidden != ""){
        return true;
        }    
    }
</script>
</head>
<body>
<script>top.document.title=self.document.title;</script>
	<div>
		<fieldset>
		<legend><?php echo $mode ?></legend>
			<div class="align-left">
			<?php
    ##WI SOL        
    if (isset($_POST['ryycustom_2']) and $_POST['ryycustom_2']== _REFERENCE){
        $ref_cust_vis = true;
        $res_cust_link = array("inner_html"=>"<div style=\"padding-left:32%\" ><span id='this_div' style='padding-left:0px; padding-right: 10px; color: #8db63a'></span><a href='customers_pick.php' data-jkit=\"[lightbox]\" title=\""._REFERENCE."\">"._LINK_CUSTOMER."</a></div>");
    }else{
        $ref_cust_vis = false;
        $res_cust_link = array();
        }
    
    ################################################################################
    ## +---------------------------------------------------------------------------+
    ## | 1. Creating & Calling:                                                    | 
    ## +---------------------------------------------------------------------------+
    ##  *** define a relative (virtual) path to datagrid.class.php file
    ##  *** (relatively to the current file)
    ##  *** RELATIVE PATH ONLY ***     
      define ("DATAGRID_DIR", "../datagrid/");                     /* Ex.: "datagrid/" */ 
      define ("PEAR_DIR", "../datagrid/pear/");                    /* Ex.: "datagrid/pear/" */
    
      require_once(DATAGRID_DIR.'datagrid.class.php');
      require_once(PEAR_DIR.'PEAR.php');
      require_once(PEAR_DIR.'DB.php');
    
    ##
    ##  *** creating variables that we need for database connection 

      // includes database connection parameters
      include_once('../datagrid/config.inc.php');
    
      ob_start();
      $db_conn = DB::factory('mysql');  /* don't forget to change on appropriate db type */
      $result_conn = $db_conn->connect(DB::parseDSN('mysql://'.$DB_USER.':'.$DB_PASS.'@'.$DB_HOST.'/'.$DB_NAME));
      if(DB::isError($result_conn)){ die($result_conn->getDebugInfo()); }
    ##  *** put a primary key on the first place 
      $sql = "SELECT
			cls_customers.id,
			barcode_id,
			group_id,
			erp_code,
			cls_customer_groups.name,
			first_name,
			last_name,
			birth_date,
			company,
			b_address,
			b_address_2,
			b_city,
			b_zipcode,
			b_country,
			b_state,
			phone,
			phone2,
			fax,
			comments,
			industry,
			last_transaction_date AS last_trans_date,
			DATE_FORMAT(cls_customers.date_created, '%d-%m-%Y') AS date_created, 
			DATE_FORMAT(cls_customers.last_transaction_date, '%d-%m-%Y (%H:%i)') AS last_transaction_date,
			is_active,
			credits,
			score,
			turnover,
			redeem_amount,
            email,
            facebook,
            image,
            custom_1,
            custom_2,
            custom_3,
            BRA.branch_name as branch,
            branch_id,
            CONCAT('<img src=\"../img/transaction.gif\" />') AS new_transaction
            FROM cls_customers 
			LEFT OUTER JOIN cls_customer_groups ON cls_customers.group_id = cls_customer_groups.id 
            LEFT OUTER JOIN cls_branches BRA ON cls_customers.branch_id = BRA.id 
			";
            //DATE_FORMAT(a.date_created, '%d-%m-%Y %H:%i') as date_created,

    ##  *** set needed options and create a new class instance 
      $debug_mode = false;        /* display SQL statements while processing */    
      $messaging = true;          /* display system messages on a screen */ 
      $unique_prefix = "cus_";    /* prevent overlays - must be started with a letter */
      $dgrid = new DataGrid($debug_mode, $messaging, $unique_prefix, DATAGRID_DIR);
	  //webnail get current mode
	  $mode = isset($_REQUEST[$unique_prefix.'mode']) ? $_REQUEST[$unique_prefix.'mode'] : ""; 
      $rid = (isset($_REQUEST[$unique_prefix.'rid'])) ? $_REQUEST[$unique_prefix.'rid'] : '';
	   $the_record = $dgrid->GetCurrentId();
      
    ##  *** set encoding and collation (default: utf8/utf8_unicode_ci)
     
      $dg_encoding = "utf8";
      $dg_collation = "utf8_unicode_ci";
      $dgrid->SetEncoding($dg_encoding, $dg_collation);
    ##  *** set data source with needed options
      $default_order_field = "last_trans_date";
      $default_order_type = "DESC";
      $dgrid->DataSource($db_conn, $sql, $default_order_field, $default_order_type);
    ## +---------------------------------------------------------------------------+
    ## | 2. General Settings:                                                      | 
    ## +---------------------------------------------------------------------------+
    ## +-- PostBack Submission Method ---------------------------------------------+
    ##  *** defines postback submission method for DataGrid: AJAX, POST or GET(default)
    $postback_method = "ajax";
    $dgrid->SetPostBackMethod($postback_method);
    ##  *** allow multirow operations
        $multirow_option = false;
        $dgrid->AllowMultirowOperations($multirow_option);
        $multirow_operations = array(
            "edit"  => array("view"=>false),
            "delete"  => array("view"=>false),
            "details" => array("view"=>false), 
        );
        $dgrid->SetMultirowOperations($multirow_operations);
		//webnail
		if ($_SESSION['lang']=="EN"){$dg_language = "en";}elseif ($_SESSION['lang']=="GR"){$dg_language = "gk";}else{$dg_language = "en";}
		$dgrid->SetInterfaceLang($dg_language);  
         ##  *** set modes for operations ("type" => "link|button|image")
    ##  *** "view" - view mode | "edit" - add/edit/details modes
    ##  *** "byFieldValue"=>"fieldName" - make the field to be a link to edit mode page
    $modes = array(
         "add"	  =>array("view"=>true, "edit"=>false, "type"=>"button", "show_add_button"=>"outside"),
         "edit"	  =>array("view"=>true, "edit"=>true,  "type"=>"image"),
         "cancel"  =>array("view"=>true, "edit"=>true,  "type"=>"button"),
         "details" =>array("view"=>true, "edit"=>true, "type"=>"image"),
         "delete"  =>array("view"=>false, "edit"=>false,  "type"=>"image"),

    );
    $dgrid->SetModes($modes);   
    ##  *** set CSS class for datagrid
    ##  *** "default|blue|gray|green|pink|empty|x-blue|x-gray|x-green" or your own css style
     $css_class = "default";
     $dgrid->SetCssClass($css_class);
    ##  *** set DataGrid caption
    $dg_caption = "";
      $dgrid->SetCaption($dg_caption);
    
    ## +---------------------------------------------------------------------------+
    ## | 3. Printing & Exporting Settings:                                         | 
    ## +---------------------------------------------------------------------------+
    ##  *** set printing option: true(default) or false 
      $printing_option = true;
      $dgrid->AllowPrinting($printing_option);
	  $exporting_option = true;
		$exporting_directory = "../exports/";               
		$export_all = false;
		$dgrid->AllowExporting($exporting_option, $exporting_directory, $export_all);
		$exporting_types = array("excel"=>"true", "pdf"=>"false", "xml"=>"false");
		$dgrid->AllowExportingTypes($exporting_types);
    
    ## +---------------------------------------------------------------------------+
    ## | 4. Sorting & Paging Settings:                                             | 
    ## +---------------------------------------------------------------------------+
    ##  *** set paging option: true(default) or false 
      $paging_option = true;
      $rows_numeration = false;
      $numeration_sign = "#";       
      $dgrid->AllowPaging($paging_option, $rows_numeration, $numeration_sign);
    ##  *** set paging settings
      $bottom_paging = array("results"=>true, "results_align"=>"left", "pages"=>true, "pages_align"=>"center", "page_size"=>true, "page_size_align"=>"right");
      $top_paging = array();
      $pages_array = array("25"=>"25", "50"=>"50", "100"=>"100", "200"=>"200");
      $default_page_size = 25;
      $dgrid->SetPagingSettings($bottom_paging, $top_paging, $pages_array, $default_page_size);
    
    ## +---------------------------------------------------------------------------+
    ## | 5. Filter Settings:                                                       | 
    ## +---------------------------------------------------------------------------+
    ##  *** set filtering option: true or false(default)
      $filtering_option = true;
      $show_search_type = true;
      $dgrid->AllowFiltering($filtering_option, $show_search_type);
      $fill_from_array = array("1"=>$lbl_yes, "0"=>$lbl_no );
      $about_us_array = array(""=>_SELECT_VALUE, _INTERNET_SEARCH =>_INTERNET_SEARCH, _SAW_THE_SIGN=>_SAW_THE_SIGN, _REFERENCE=>_REFERENCE, _SOCIAL_MEDIA=>_SOCIAL_MEDIA, _DO_NOT_REMEMBER=>_DO_NOT_REMEMBER); #WI SOL 1.4.6
   
        ##GET BRANCHES
        $sql_17 = 'SELECT id, branch_name FROM cls_branches ORDER BY id ASC';					
        $result_17 = mysqli_query($mysqli, $sql_17) or die("Query failed : " . mysqli_error($mysqli));	
        
        while( $row = mysqli_fetch_assoc($result_17)){
        $fill_from_branches[$row['id']] = $row['branch_name'];
        }    
   
    ##  *** set additional filtering settings
    ##  *** tips: use "," (comma) if you want to make search by some words, for ex.: hello, bye, hi
      $filtering_fields = array(
      _OPENED_BY_BRANCH =>array("type"=>"dropdownlist", "table"=>"cls_customers", "field"=>"branch_id", "filter_condition"=>"", "source"=>$fill_from_branches, "show_operator"=>"false", "default_operator"=>"=", "case_sensitive"=>"false","comparison_type"=>"string", "width"=>"", "on_js_event"=>""),
      $lbl_grid_last_name=>array("type"=>"textbox", "table"=>"cls_customers", "field"=>"last_name", "filter_condition"=>"", "show_operator"=>"false", "default_operator"=>"like", "case_sensitive"=>"false","comparison_type"=>"string", "width"=>"", "on_js_event"=>""),	
      $lbl_grid_first_name=>array("type"=>"textbox", "table"=>"cls_customers", "field"=>"first_name", "filter_condition"=>"", "show_operator"=>"false", "default_operator"=>"like", "case_sensitive"=>"false","comparison_type"=>"string", "width"=>"", "on_js_event"=>""),  
      $lbl_grid_barcode=>array("type"=>"textbox", "table"=>"cls_customers", "field"=>"barcode_id", "filter_condition"=>"", "show_operator"=>"false", "default_operator"=>"like", "case_sensitive"=>"false","comparison_type"=>"string", "width"=>"", "on_js_event"=>""),
      $lbl_grid_cat_group=>array("type"=>"dropdownlist", "table"=>"cls_customer_groups", "field"=>"name", "show_operator"=>"false", "case_sensitive"=>"false","comparison_type"=>"string", "width"=>"", "on_js_event"=>""),           
	 );
      $dgrid->SetFieldsFiltering($filtering_fields);
      $dgrid->SetDefaultFiltering(array("table"=>"cls_customers", "field_name"=>"branch_id", "field_value"=>$user_branch));
    
    ## +---------------------------------------------------------------------------+
    ## | 6. View Mode Settings:                                                    | 
    ## +---------------------------------------------------------------------------+
    ##  *** set view mode table properties
      $vm_table_properties = array("width"=>"100%");
      $dgrid->SetViewModeTableProperties($vm_table_properties);  
    ##  *** set columns in view mode
    ##  *** Ex.: "on_js_event"=>"onclick='alert(\"Yes!!!\");'"
    ##  ***      "barchart" : number format in SELECT SQL must be equal with number format in max_value
      $vm_colimns = array(
        "id" => array("header"=>$lbl_grid_code, "type"=>"label", "align"=>"left", "width"=>"20px", "wrap"=>"nowrap", "text_length"=>"-1", "tooltip"=>"true", "tooltip_type"=>"floating", "case"=>"normal", "summarize"=>"false", "sort_type"=>"string", "sort_by"=>""), 	  
        "barcode_id" => array("header"=>$lbl_grid_barcode, "type"=>"label", "align"=>"left", "width"=>"20px", "wrap"=>"nowrap", "text_length"=>"-1", "tooltip"=>"true", "tooltip_type"=>"floating", "case"=>"normal", "summarize"=>"false", "sort_type"=>"string", "sort_by"=>"", "visible"=>"true", "on_js_event"=>"",  "field_data"=>"", "rel"=>"", "title"=>""),
        "last_name"     => array("header"=>$lbl_grid_last_name, "type"=>"label", "target"=>"_self", "align"=>"left", "width"=>"", "wrap"=>"nowrap", "text_length"=>"-1", "tooltip"=>"true", "tooltip_type"=>"floating", "case"=>"normal", "summarize"=>"false", "sort_type"=>"string", "sort_by"=>"", "visible"=>"true", "on_js_event"=>""),
		"first_name" => array("header"=>$lbl_grid_first_name, "type"=>"label",      "align"=>"left", "wrap"=>"nowrap", "text_length"=>"50", "tooltip"=>"true", "tooltip_type"=>"simple", "case"=>"normal", "summarize"=>"false", "sort_type"=>"string", "sort_by"=>"", "visible"=>"true", "on_js_event"=>""),		
		"name" => array("header"=>$lbl_grid_cat_group, "type"=>"label",      "align"=>"left", "wrap"=>"nowrap", "text_length"=>"50", "tooltip"=>"true", "tooltip_type"=>"simple", "case"=>"normal", "sort_type"=>"string", "sort_by"=>"", "visible"=>"true", "on_js_event"=>""),		
		"credits" => array("header"=>$lbl_grid_customer_credits, "type"=>"label",      "align"=>"right", "wrap"=>"nowrap", "text_length"=>"50", "tooltip"=>"true", "tooltip_type"=>"simple", "case"=>"normal", "sort_type"=>"string", "sort_by"=>"", "visible"=>"true", "on_js_event"=>""),
		"score" => array("header"=>$lbl_grid_score, "type"=>"label",      "align"=>"right", "wrap"=>"nowrap", "text_length"=>"50", "tooltip"=>"true", "tooltip_type"=>"simple", "case"=>"normal", "sort_type"=>"string", "sort_by"=>"", "visible"=>"true", "on_js_event"=>""),
		"turnover" => array("header"=>$lbl_grid_turnover, "type"=>"money", "align"=>"right", "wrap"=>"nowrap", "text_length"=>"50", "tooltip"=>"true", "tooltip_type"=>"simple", "case"=>"normal", "sort_type"=>"string", "sort_by"=>"", "visible"=>"true", "on_js_event"=>"","sign"=>$currency, "sign_place"=>"before", "decimal_places"=>"2", "dec_separator"=>",", "thousands_separator"=>"."),
		"redeem_amount" => array("header"=>$lbl_grid_redeem_amount, "type"=>"money", "align"=>"right", "wrap"=>"nowrap", "text_length"=>"50", "tooltip"=>"true", "tooltip_type"=>"simple", "case"=>"normal", "sort_type"=>"string", "sort_by"=>"", "visible"=>"true", "on_js_event"=>"","sign"=>$currency, "sign_place"=>"before", "decimal_places"=>"2", "dec_separator"=>",", "thousands_separator"=>"."),		
		//"date_created" => array("header"=>$lbl_grid_created_on, "type"=>"label", "align"=>"left", "wrap"=>"nowrap", "tooltip"=>"true", "tooltip_type"=>"simple", "case"=>"normal", "summarize"=>"false", "sort_type"=>"string", "sort_by"=>"", "visible"=>"true", "on_js_event"=>""),	
		"last_transaction_date" => array("header"=>$lbl_last_transaction_date, "type"=>"label", "align"=>"left", "wrap"=>"nowrap", "tooltip"=>"true", "tooltip_type"=>"simple", "case"=>"normal", "summarize"=>"false", "sort_type"=>"string", "sort_by"=>"", "visible"=>"true", "on_js_event"=>""),			
		"branch" => array("header"=>_BRANCH, "type"=>"label",      "align"=>"left", "wrap"=>"nowrap", "text_length"=>"50", "tooltip"=>"true", "tooltip_type"=>"simple", "case"=>"normal", "sort_type"=>"string", "sort_by"=>"", "visible"=>"true", "on_js_event"=>""),
        "is_active"=>array("header"=>$lbl_grid_user_active, "type"=>"checkbox",  "align"=>"center", "sort_type"=>"string", "sort_by"=>"", "visible"=>"true", "on_js_event"=>"", "true_value"=>1, "false_value"=>0),	      				
        "new_transaction"=>array("header"=>_INSERT_TRANSACTION, "type"=>"link",       "align"=>"center", "text_length"=>"-1", "tooltip"=>"false", "tooltip_type"=>"floating", "summarize"=>"false", "visible"=>"true", "on_js_event"=>"", "field_key"=>"barcode_id", "field_key_1"=>"field_name_1", "field_data"=>"field_name_2" , "rel"=>"", "title"=>_INSERT_TRANSACTION, "target"=>"_self", "href"=>"transaction.php?barcode={0}&page=cust"),      
      );
      $dgrid->SetColumnsInViewMode($vm_colimns);
    
    
    ## +---------------------------------------------------------------------------+
    ## | 7. Add/Edit/Details Mode Settings:                                        | 
    ## +---------------------------------------------------------------------------+
    ##  *** set add/edit mode table properties
	$em_columns= array();
      $em_table_properties = array("width"=>"100%");
      $dgrid->SetEditModeTableProperties($em_table_properties);
    ##  *** set details mode table properties
      $dm_table_properties = array("width"=>"100%");
      $dgrid->SetDetailsModeTableProperties($dm_table_properties);
    ##  ***  set settings for add/edit/details modes
      $table_name  = "cls_customers";
      $primary_key = "id";	
      $condition   = "";
      $dgrid->SetTableEdit($table_name, $primary_key, $condition);      
      
   	if ($mode=="add"){	
    $em_columns = array(	
            "barcode_id"=>array("header"=>$lbl_grid_barcode, "type"=>"textbox", "autocomplete"=>"off", "req_type"=>"ry", "width"=>($barcode_len * 10)."px", "title"=>"", "readonly"=>"false", "maxlength"=>$barcode_len, "default"=>"", "unique"=>"true", "visible"=>"true", "on_js_event"=>""),		
            "last_name"=>array("header"=>$lbl_grid_last_name, "type"=>"textbox", "autocomplete"=>"off", "req_type"=>"ry", "width"=>"300px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>""),
			"first_name"=>array("header"=>$lbl_grid_first_name, "type"=>"textbox", "autocomplete"=>"off", "req_type"=>"ry", "width"=>"300px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>""),
            "birth_date"=>array("header"=>$lbl_grid_birth_date, "type"=>"date",  "autocomplete"=>"off", "default"=>"",  "req_type"=>"rt", "width"=>"210px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "unique"=>"false", "unique_condition"=>"", "visible"=>"true",  "calendar_type"=>"dropdownlist",),
            //"delimiter_1"=>array("inner_html"=>"<div style=\"padding-left:32%\"><a href=\"#\" onclick='daysOld(\"cus_frmEditRowrtybirth_date\");'>"._CALCULATE_AGE."</a></div>"),            		
			"company"=>array("header"=>$lbl_grid_company, "type"=>"textbox", "autocomplete"=>"off", "req_type"=>"", "width"=>"300px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>""),
			"industry"=>array("header"=>$lbl_grid_industry, "type"=>"textbox", "autocomplete"=>"off", "req_type"=>"ry", "width"=>"300px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>""),			
			"b_address"=>array("header"=>$lbl_grid_address, "type"=>"textbox", "autocomplete"=>"off", "req_type"=>"ry", "width"=>"300px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>""),
			"b_address_2"=>array("header"=>$lbl_grid_address." (2)", "type"=>"textbox", "req_type"=>"", "width"=>"300px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>""),
            //"b_address_2"=>array("header"=>_MUNICIPALITY, "type"=>"textbox", "autocomplete"=>"off", "req_type"=>"ry", "width"=>"300px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>""),
			"b_city"=>array("header"=>$lbl_grid_city, "type"=>"textbox", "autocomplete"=>"off", "req_type"=>"ry", "width"=>"300px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>""),
			"b_zipcode"=>array("header"=>$lbl_grid_zipcode, "type"=>"textbox", "autocomplete"=>"off", "req_type"=>"ry", "width"=>"300px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>""),
            "b_state"=>array("header"=>$lbl_grid_state, "type"=>"foreign_key", "autocomplete"=>"off", "req_type"=>"ry", "width"=>"300px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1",  "visible"=>"true"),		
			"phone"=>array("header"=>_MOBILE_PHONE, "type"=>"textbox", "autocomplete"=>"off", "req_type"=>"ry", "width"=>"300px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>""),	
			"phone2"=>array("header"=>$lbl_grid_phone." (2)", "type"=>"textbox", "autocomplete"=>"off", "req_type"=>"", "width"=>"300px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>""),	
			"fax"=>array("header"=>$lbl_grid_fax, "type"=>"textbox", "autocomplete"=>"off", "req_type"=>"", "width"=>"300px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>""),						
            "email"    =>array("header"=>_EMAIL, "type"=>"textbox",  "autocomplete"=>"off", "req_type"=>"se", "width"=>"300px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "visible"=>"true", "on_js_event"=>"", "for_field"=>""),
            "facebook" =>array("header"=>_FACEBOOK_ACCOUNT, "type"=>"link",   "autocomplete"=>"off", "req_type"=>"su", "width"=>"300px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>""),
            "group_id"=>array("header"=>$lbl_grid_cat_group, "type"=>"foreign_key", "autocomplete"=>"off", "req_type"=>"ry", "width"=>"300px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1",  "visible"=>"true"),			
			"date_created"=>array("header"=>$lbl_grid_created_on, "type"=>"hidden",  "width"=>"100px", "title"=>"", "readonly"=>"true", "maxlength"=>"-1", "default"=>date("Y-m-d H:i:s"), ),
			"branch_id"=>array("header"=>_BRANCH, "type"=>"hidden",  "width"=>"100px", "title"=>"", "readonly"=>"true", "maxlength"=>"-1", "default"=>$user_branch, ),
            "is_active"   =>array("header"=>$lbl_grid_active_male, "source"=>$fill_from_array, "table"=>"cls_customers", "field"=>"is_active", "type"=>"enum",  "width"=>"210px", "title"=>"", "readonly"=>"false",  "unique"=>"false", "unique_condition"=>"", "visible"=>"true"),
            "erp_code"=>array("header"=>$lbl_grid_erp_code, "type"=>"textbox", "autocomplete"=>"off", "req_type"=>"sy", "width"=>"300px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"true", "visible"=>"true", "on_js_event"=>""),				
			"comments"=>array("header"=>$lbl_grid_comments, "type"=>"textarea", "autocomplete"=>"off", "req_type"=>"", "width"=>"400px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>""),
            "image"  =>array("header"=>_IMAGE, "type"=>"image", "req_type"=>"st", "width"=>"300px", "title"=>_IMAGE, "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"", "target_path"=>"img/customers/", "max_file_size"=>"30K", "image_width"=>"120px", "image_height"=>"120px", "resize_image"=>"false", "resize_width"=>"120", "resize_height"=>"120", "magnify"=>"true", "magnify_type"=>"magnifier", "magnify_power"=>"2", "file_name"=>rand_string(20), "host"=>"local", "allow_downloading"=>"true", "allowed_extensions"=>"jpg"),				
            //"custom_1"   =>array("header"=>_PHOTOTYPE, "type"=>"enum", "req_type"=>"ry", "source"=>$phototype_array, "table"=>"cls_customers", "field"=>"custom_1",  "width"=>"210px", "title"=>"", "readonly"=>"false", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true"),
            "custom_2"   =>array("header"=>_HOW_YOU_LEARNED_ABOUT_US, "type"=>"enum", "req_type"=>"ry", "source"=>$about_us_array, "table"=>"cls_customers", "field"=>"custom_2",  "width"=>"210px", "title"=>"", "readonly"=>"false", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"onchange='formAction(\"\", \"\", \"".$dgrid->uniquePrefix."\", \"".$dgrid->HTTP_URL."\", \"".$_SERVER['QUERY_STRING']."\", \"ajax\", \"".$_REQUEST[$dgrid->uniquePrefix.'mode']."\")'"),
            "custom_3"=>array("header"=>_REFERENCE_CUSTOMER, "type"=>"hidden", "autocomplete"=>"off", "req_type"=>"sy", "width"=>"50px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false"),
            "delimiter_2"=>$res_cust_link,
      );														
	  }elseif ($mode=="details"){
	   			//Reference Match Query
			$ref_sql = "SELECT 
                        CUS.custom_3, 
                        (SELECT CONCAT(last_name, ' ', first_name) from cls_customers WHERE id = CUS.custom_3) as cust_ref                   
                        FROM 
                        cls_customers CUS
                        WHERE id=".$the_record."" ;
			$ref_result = mysqli_query($mysqli, $ref_sql) or die("Query failed : " . mysqli_error($mysqli));
			$num_rows = mysqli_num_rows($ref_result);
			//echo $num_rows;
			while ($row = mysqli_fetch_array($ref_result, MYSQLI_ASSOC)) {
            $ref_customer = $row['custom_3']; 
            $ref_customer_name = $row['cust_ref']; 
                if (($ref_customer != '' and !isset($_POST['ryycustom_2'])) OR ($ref_customer != '' and isset($_POST['ryycustom_2']) and $_POST['ryycustom_2']== _REFERENCE )) {
                    $ref_cust_vis = true;
                    $res_cust_link = array("inner_html"=>"<div style=\"padding-left:32%\"><span id='this_div' style='padding-left:0px; padding-right: 10px; color: #8db63a'>$ref_customer_name</span></div>");
                }
            }
	  $em_columns = array(
            "image"  =>array("header"=>_IMAGE, "type"=>"image", "req_type"=>"st", "width"=>"300px", "title"=>_IMAGE, "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"", "target_path"=>"img/customers/", "max_file_size"=>"30K", "image_width"=>"120px", "image_height"=>"120px", "resize_image"=>"false", "resize_width"=>"120", "resize_height"=>"120", "magnify"=>"true", "magnify_type"=>"magnifier", "magnify_power"=>"2",  "host"=>"local", "allow_downloading"=>"false", "allowed_extensions"=>"jpg"),
            "id"   =>array("header"=>$lbl_grid_code, "type"=>"label"),			
            "last_name"=>array("header"=>$lbl_grid_last_name, "type"=>"label"),	
            "first_name" =>array("header"=>$lbl_grid_first_name, "type"=>"label"),
            "barcode_id"=>array("header"=>$lbl_grid_barcode, "type"=>"label"), 
			"company"=>array("header"=>$lbl_grid_company, "type"=>"label"),
			"industry"=>array("header"=>$lbl_grid_industry, "type"=>"label"),
			"group_id"=>array("header"=>$lbl_grid_cat_group, "type"=>"foreign_key"), 
			"birth_date" =>array("header"=>$lbl_grid_birth_date, "type"=>"datedmy", "value"=>date("d-m-Y")),	
			"date_created" =>array("header"=>$lbl_grid_created_on, "type"=>"datetimedmy", "value"=>date("d-m-Y H:i:s")),
            "branch_id"=>array("header"=>_BRANCH, "type"=>"foreign_key",),	
			"last_transaction_date" =>array("header"=>$lbl_last_transaction_date, "type"=>"datetimedmy", "value"=>date("d-m-Y H:i:s")),			
			"credits"=>array("header"=>$lbl_grid_customer_credits, "type"=>"label"),
			"score"=>array("header"=>$lbl_grid_score, "type"=>"label"),
			"turnover" => array("header"=>$lbl_grid_turnover, "type"=>"money", "sign"=>$currency, "sign_place"=>"before", "decimal_places"=>"2", "dec_separator"=>",", "thousands_separator"=>"."),
			"redeem_amount" => array("header"=>$lbl_grid_redeem_amount, "type"=>"money", "sign"=>$currency, "sign_place"=>"before", "decimal_places"=>"2", "dec_separator"=>",", "thousands_separator"=>"."),			
			"b_address"=>array("header"=>$lbl_grid_address, "type"=>"label"),
			"b_address_2"=>array("header"=>$lbl_grid_address."(2)", "type"=>"label"),
            //"b_address_2"=>array("header"=>_MUNICIPALITY, "type"=>"label"),
            "b_city"=>array("header"=>$lbl_grid_city, "type"=>"label"),
			"b_zipcode"=>array("header"=>$lbl_grid_zipcode, "type"=>"label"),
			"b_state"=>array("header"=>$lbl_grid_state, "type"=>"label"),
			"phone"=>array("header"=>_MOBILE_PHONE, "type"=>"label"),
			"phone2"=>array("header"=>$lbl_grid_phone."(2)", "type"=>"label"),
			"fax"=>array("header"=>$lbl_grid_fax, "type"=>"label"),
            "email" =>array("header"=>_EMAIL,  "type"=>"link",   "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"", "field_key"=>"email", "field_key_1"=>"0", "field_data"=>"email", "rel"=>"", "title"=>"", "target"=>"_self",  "href"=>"mailto:{0}"),
			"facebook" =>array("header"=>_FACEBOOK_ACCOUNT,  "type"=>"link", "visible"=>"true",  "field_key"=>"facebook", "field_key_1"=>"0", "field_data"=>"facebook", "title"=>_FACEBOOK_ACCOUNT, "target"=>"_new", "href"=>"{0}"),
            "is_active" =>array("header"=>$lbl_grid_user_active,  "type"=>"checkbox"),
            "erp_code"=>array("header"=>$lbl_grid_erp_code, "type"=>"label"),
			"comments"=>array("header"=>$lbl_grid_comments, "type"=>"label"),
            //"custom_1"=>array("header"=>_PHOTOTYPE, "type"=>"label"),
            "custom_2"   =>array("header"=>_HOW_YOU_LEARNED_ABOUT_US, "type"=>"label"),
            "custom_3"=>array("header"=>_REFERENCE_CUSTOMER, "type"=>"hidden", "autocomplete"=>"off", "req_type"=>"sy", "width"=>"50px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false"),
            "delimiter_2"=>$res_cust_link,
      );	   
	  }elseif ($mode=="edit"){
			//Reference Match Query
			$ref_sql = "SELECT 
                        CUS.custom_3, 
                        CUS.custom_2,
                        (SELECT CONCAT(last_name, ' ', first_name) FROM cls_customers WHERE id = CUS.custom_3) AS cust_ref                   
                        FROM 
                        cls_customers CUS
                        WHERE id=".$the_record."" ;
			$ref_result = mysqli_query($mysqli, $ref_sql) or die("Query failed : " . mysqli_error($mysqli));
			$num_rows = mysqli_num_rows($ref_result);
			//echo $num_rows;
			while ($row = mysqli_fetch_array($ref_result, MYSQLI_ASSOC)) {
            $ref_customer = $row['custom_3']; 
            $ref_way = $row['custom_2']; 
            $ref_customer_name = $row['cust_ref']; 
                if (
                        ($ref_way == _REFERENCE && !isset($_POST['ryycustom_2'])) || 
                        ($ref_way == _REFERENCE && isset($_POST['ryycustom_2']) && $_POST['ryycustom_2']==_REFERENCE)
                    ) {
                    $ref_cust_vis = true;
                    $res_cust_link = array("inner_html"=>"<div style=\"padding-left:32%\" >
                    <span id='this_div' style='padding-left:0px; padding-right: 10px; color: #8db63a'>$ref_customer_name</span>
                    <a href='customers_pick.php' data-jkit=\"[lightbox]\" title=\""._REFERENCE."\">"._LINK_CUSTOMER."</a>
                    </div>");
                }
            }
	  $em_columns = array(
            "id"   =>array("header"=>$lbl_grid_code, "type"=>"label", "width"=>"30px", "title"=>"", "readonly"=>"true", "visible"=>"true"),			
            "last_name"=>array("header"=>$lbl_grid_last_name, "type"=>"textbox", "req_type"=>"ry", "width"=>"300px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>""),
			"first_name"=>array("header"=>$lbl_grid_first_name, "type"=>"textbox", "req_type"=>"ry", "width"=>"300px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>""),
            "barcode_id"=>array("header"=>$lbl_grid_barcode, "type"=>"textbox", "req_type"=>"ry", "width"=>"300px", "title"=>"", "readonly"=>"false", "maxlength"=>$barcode_len, "default"=>"", "unique"=>"true", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>""),	
            "birth_date"=>array("header"=>$lbl_grid_birth_date, "type"=>"date",  "default"=>"",  "req_type"=>"rt", "width"=>"210px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"", "calendar_type"=>"dropdownlist",),		
            //"delimiter_1"=>array("inner_html"=>"<div style=\"padding-left:32%\"><a href=\"#\" onclick='daysOld(\"cus_frmEditRowrtybirth_date\");'>"._CALCULATE_AGE."</a></div>"), 			
            "company"=>array("header"=>$lbl_grid_company, "type"=>"textbox", "req_type"=>"", "width"=>"300px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>""),
			"industry"=>array("header"=>$lbl_grid_industry, "type"=>"textbox", "req_type"=>"ry", "width"=>"300px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>""),			
			"b_address"=>array("header"=>$lbl_grid_address, "type"=>"textbox", "req_type"=>"ry", "width"=>"300px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>""),
			"b_address_2"=>array("header"=>$lbl_grid_address." (2)", "type"=>"textbox", "req_type"=>"", "width"=>"300px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>""),
            //"b_address_2"=>array("header"=>_MUNICIPALITY, "type"=>"textbox", "req_type"=>"ry", "width"=>"300px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>""),
			"b_city"=>array("header"=>$lbl_grid_city, "type"=>"textbox", "req_type"=>"ry", "width"=>"300px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>""),
			"b_zipcode"=>array("header"=>$lbl_grid_zipcode, "type"=>"textbox", "req_type"=>"ry", "width"=>"300px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>""),
			"b_state"=>array("header"=>$lbl_grid_state, "type"=>"textbox", "req_type"=>"ry", "width"=>"300px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>""),	
			"phone"=>array("header"=>_MOBILE_PHONE, "type"=>"textbox", "req_type"=>"ry", "width"=>"300px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>""),	
			"phone2"=>array("header"=>$lbl_grid_phone." (2)", "type"=>"textbox", "req_type"=>"", "width"=>"300px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>""),	
			"fax"=>array("header"=>$lbl_grid_fax, "type"=>"textbox", "req_type"=>"", "width"=>"300px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>""),						
            "email"=>array("header"=>_EMAIL, "type"=>"textbox",   "req_type"=>"se", "width"=>"300px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "visible"=>"true", "on_js_event"=>"", "for_field"=>""),            
            "facebook" =>array("header"=>_FACEBOOK_ACCOUNT, "type"=>"link",   "req_type"=>"su", "width"=>"300px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>""),
            "group_id"=>array("header"=>$lbl_grid_cat_group, "type"=>"foreign_key", "req_type"=>"ry", "width"=>"300px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1",  "visible"=>"true"),	
			"date_created"=>array("header"=>$lbl_grid_created_on, "type"=>"datetimeymd",  "width"=>"300px", "title"=>"", "readonly"=>"true", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>""),
			"last_transaction_date"=>array("header"=>$lbl_last_transaction_date, "type"=>"datetimeymd",  "width"=>"300px", "title"=>"", "readonly"=>"true", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>""),
			"credits"=>array("header"=>$lbl_grid_customer_credits, "type"=>"label", "req_type"=>"", "width"=>"150px", "title"=>"", "readonly"=>"true", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>""),
			"score"=>array("header"=>$lbl_grid_score, "type"=>"label", "req_type"=>"", "width"=>"150px", "title"=>"", "readonly"=>"true", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>""),
			"turnover"=>array("header"=>$lbl_grid_turnover, "type"=>"label", "req_type"=>"", "width"=>"150px", "title"=>"", "readonly"=>"true", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"","sign"=>$currency, "sign_place"=>"before", "decimal_places"=>"2", "dec_separator"=>",", "thousands_separator"=>"."),
			"redeem_amount"=>array("header"=>$lbl_grid_redeem_amount, "type"=>"label", "req_type"=>"", "width"=>"150px", "title"=>"", "readonly"=>"true", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"","sign"=>$currency, "sign_place"=>"before", "decimal_places"=>"2", "dec_separator"=>",", "thousands_separator"=>"."),			
			"branch_id" => array("header"=>_OPENED_BY_BRANCH, "type"=>"foreign_key",   "readonly"=>"true",   "align"=>"left", "wrap"=>"nowrap", "text_length"=>"50", "tooltip"=>"true", "tooltip_type"=>"simple", "case"=>"normal", "sort_type"=>"string", "sort_by"=>"", "visible"=>"true", "on_js_event"=>""),
            "is_active"   =>array("header"=>$lbl_grid_active_male, "source"=>$fill_from_array, "table"=>"cls_customers", "field"=>"is_active", "type"=>"enum",  "width"=>"210px", "title"=>"", "readonly"=>"false", "default"=>"1", "unique"=>"false", "unique_condition"=>"", "visible"=>"true"),
            "erp_code"=>array("header"=>$lbl_grid_erp_code, "type"=>"textbox", "req_type"=>"sy", "width"=>"300px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"true", "visible"=>"true", "on_js_event"=>""),				
			"comments"=>array("header"=>$lbl_grid_comments, "type"=>"textarea", "req_type"=>"", "width"=>"400px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>""),				            
            "image"  =>array("header"=>_IMAGE, "type"=>"image", "req_type"=>"st", "width"=>"300px", "title"=>_IMAGE, "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"", "target_path"=>"img/customers/", "max_file_size"=>"30K", "image_width"=>"120px", "image_height"=>"120px", "resize_image"=>"false", "resize_width"=>"120", "resize_height"=>"120", "magnify"=>"true", "magnify_type"=>"magnifier", "magnify_power"=>"2", "file_name"=>rand_string(20), "host"=>"local", "allow_downloading"=>"true", "allowed_extensions"=>"jpg"),	
            //"custom_1"   =>array("header"=>_PHOTOTYPE, "type"=>"enum", "req_type"=>"ry", "source"=>$phototype_array, "table"=>"cls_customers", "field"=>"custom_1",  "width"=>"210px", "title"=>"", "readonly"=>"false", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true"),
            "custom_2"   =>array("header"=>_HOW_YOU_LEARNED_ABOUT_US, "type"=>"enum",  "req_type"=>"ry", "source"=>$about_us_array, "table"=>"cls_customers", "field"=>"custom_2",  "width"=>"210px", "title"=>"", "readonly"=>"false", "default"=>"", "unique"=>"false", "unique_condition"=>"", "visible"=>"true", "on_js_event"=>"onchange='formAction(\"\", \"\", \"".$dgrid->uniquePrefix."\", \"".$dgrid->HTTP_URL."\", \"".$_SERVER['QUERY_STRING']."\", \"ajax\", \"".$_REQUEST[$dgrid->uniquePrefix.'mode']."\");NullifyField();'"),
            "custom_3"=>array("header"=>_REFERENCE_CUSTOMER, "type"=>"hidden", "autocomplete"=>"off", "req_type"=>"sy", "width"=>"50px", "title"=>"", "readonly"=>"false", "maxlength"=>"-1", "default"=>"", "unique"=>"false",),
            "delimiter_2"=>$res_cust_link,      
      );	
	  }
   ##update mode
        if(($mode == "update") && ($rid != "-1")){
         if (isset($_POST['syycustom_3']) and $_POST['syycustom_3'] != ''  ){
            // Get column names from the needed table
            $referer = $_POST['syycustom_3'];
            $row_before_array = array();
            $dSet = $dgrid->ExecuteSQL("SELECT custom_3 FROM cls_customers WHERE id =".$rid);
            $row = $dSet->fetchRow();
            foreach($row as $key => $val){
             if ($val != $referer){ //insert credits
               $match = "SELECT id, barcode_id, credits, group_id FROM cls_customers WHERE id='".$referer."'"; 
               $result = mysqli_query($mysqli, $match) or die("Query failed : " . mysqli_error($mysqli));
       		   $cus = mysqli_fetch_array($result, MYSQLI_ASSOC);
                if (mysqli_num_rows($result) != 0){ 
                    $cust_to_update = $referer;
                    $cust_credits = $cus['credits'];
                    $barcode_id = $cus['barcode_id'];
                    $cust_category_id = $cus['group_id'];
                    $random_string = rand_string(20);
                    $credits_to_add = $custom_1;
                    $dSet = $dgrid->ExecuteSQL("INSERT INTO cls_transactions 
					                           (id, customer_id, barcode_id, cust_category_id, transaction_type,  category, amount, credits, created_on, created_by, transaction_id, trans_total_amount, multiplier, category_id, additional_info, branch_id) 
                                                VALUES (NULL , '$referer', '$barcode_id', '$cust_category_id', 'Σύσταση', 'Σύσταση', '0',  '$credits_to_add', NOW(), '$user', '$random_string', '0', '0', '99', 'Από Σύσταση πελάτη με κωδικό $rid', '$user_branch')");
                    
                    $dSet = $dgrid->ExecuteSQL("UPDATE cls_customers set credits = credits +'$credits_to_add',
				                                score = score + '$credits_to_add' WHERE id='$referer'") or die("Failed to update cust balance ". mysqli_error($mysqli));
				    //update transactions log (user's last transaction)
				    $dSet = $dgrid->ExecuteSQL("UPDATE users set params='$random_string' WHERE username='$user'") or die("Failed to update transaction log ". mysqli_error($mysqli));
                }
             } 
            }
         }
        }   
        ##add mode    
        if(($mode == "update") && ($rid == "-1")){
         if (isset($_POST['syycustom_3']) and $_POST['syycustom_3'] != ''  ){
            // Get column names from the needed table
           $referer = $_POST['syycustom_3'];
           $match = "SELECT id, barcode_id, credits, group_id FROM cls_customers WHERE id='".$referer."'"; 
           $result = mysqli_query($mysqli, $match) or die("Query failed : " . mysqli_error($mysqli));
   		   $cus = mysqli_fetch_array($result, MYSQLI_ASSOC);
                if (mysqli_num_rows($result) != 0){ 
                    $cust_to_update = $referer;
                    $cust_credits = $cus['credits'];
                    $barcode_id = $cus['barcode_id'];
                    $cust_category_id = $cus['group_id'];
                    $random_string = rand_string(20);
                    $credits_to_add = $custom_1;
                    $next_record = $dgrid->GetNextId();
                    $dSet = $dgrid->ExecuteSQL("INSERT INTO cls_transactions 
					                           (id, customer_id, barcode_id, cust_category_id, transaction_type, category, amount, credits, created_on, created_by, transaction_id, trans_total_amount, multiplier, category_id, additional_info, branch_id) 
                                                VALUES (NULL, '$referer', '$barcode_id', '$cust_category_id', 'Σύσταση', 'Σύσταση', '0',  '$credits_to_add', NOW(), '$user', '$random_string', '0', '0', '99', 'Από Σύσταση πελάτη με κωδικό $next_record', '$user_branch')");
                    
                    $dSet = $dgrid->ExecuteSQL("UPDATE cls_customers set credits = credits +'$credits_to_add',
				                                score = score + '$credits_to_add' WHERE id='$referer'") or die("Failed to update cust balance ". mysqli_error($mysqli));
				    //update transactions log (user's last transaction)
				    $dSet = $dgrid->ExecuteSQL("UPDATE users set params='$random_string' WHERE username='$user'") or die("Failed to update transaction log ". mysqli_error($mysqli));
                }
              
            }
            
        }
 
      $dgrid->SetColumnsInEditMode($em_columns);
    ##  *** set auto-generated columns in edit mode
      $dgrid->SetAutoColumnsInEditMode(false);
      $foreign_keys = array(
        "group_id"=>array("table"=>"cls_customer_groups", "field_key"=>"id", "field_name"=>"name", "view_type"=>"dropdownlist", "order_by_field"=>"id", "order_type"=>"ASC", "on_js_event"=>""),	
        "b_state"=>array("table"=>"core_regions", "field_key"=>"name", "field_name"=>"name", "view_type"=>"dropdownlist", "order_by_field"=>"priority_order", "order_type"=>"ASC", "on_js_event"=>""),						
      ); 
      $dgrid->SetForeignKeysEdit($foreign_keys);
     
    ## +---------------------------------------------------------------------------+
    ## | 8. Bind the DataGrid:                                                     | 
    ## +---------------------------------------------------------------------------+
    ##  *** bind the DataGrid and draw it on the screen
      $dgrid->initFilteringState = "opened";
	  $dgrid->firstFieldFocusAllowed = true;
	  $dgrid->Bind();        
      ob_end_flush();
?>

			</div>
		</fieldset>
	</div>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script src="../scripts/jkit/jquery.easing.1.3.js"></script>
    <script src="../scripts/jkit/jquery.jkit.1.2.16.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
    $('body').jKit({ 
    'plugins': {
    },
    'replacements': {
    'encode': specialEncodeCommand
    }
    });
    });
    function specialEncodeCommand(that, type, options){
    var s = this.settings;
    var $that = $(that);
    this.executeCommand(that, type, options);
    if (options.format == 'uppercase'){
    $that.html($that.html().toUpperCase());
    }
    }
    </script>    
</body>
</html>
<?php
} else {
    header('Location: login.php');
}
?>
