sub common_min_load_params {

for my $p ($q->param) {
    my $val   = $q->param($p) ; 
       $val   =~ s/\"//g ;    
       $i{$p} = $val ; 
       # &common_min_debug("$p - $val") ;
    }
    
} #------------------------------------------------------------------------------------------

sub common_min_alert_type {

if ($error) {
    $alert = &common_min_alert('danger',"$error",'remove-sign') ;
    }
elsif ($warning) {
    $alert = &common_min_alert('warning',"$warning") ;
    }
elsif ($success) {
    $alert = &common_min_alert('success',"$success",'ok') ;
    }
    
} #------------------------------------------------------------------------------------------

sub common_min_alert {

my ($type,$msg,$glyphicon,$id) = @_ ;

unless ($id)        { $id        = 'alertmsg';         }
unless ($glyphicon) { $glyphicon = 'exclamation-sign'; }

my $alert = qq(<div class="alert alert-$type" id="$id" role="alert"><i class="glyphicon glyphicon-$glyphicon"></i> $msg</div>) ;

return ($alert) ;
    
} #------------------------------------------------------------------------------------------

sub common_min_add_form {

my ($table,$action,$label_col,$field_col,$ignore_colpart,$skip) = @_ ;

our ($lcol,$fcol) = &common_min_columns($label_col,$field_col) ;

my $form = '' ;

$form .= &common_min_forms_start($table,$skip) ;

if (%sort_field) {
	foreach my $cnt (sort {$a <=> $b} keys %sort_field) {
		$col   = $sort_field{$cnt} ;
		&common_debug("common_min_add_form [sort_field] = $col [$cnt]") ;
		$form .= &common_min_forms_loop('',$table,$ignore_colpart) ;    
		}
	}
else
	{
	foreach (sort { $col_name{$a} cmp $col_name{$b} } keys %col_name) {
		$col   = $col_name{$_} ;
		&common_debug("common_min_add_form = $col [$_]") ;
		$form .= &common_min_forms_loop('',$table,$ignore_colpart) ;
		}
	}

    
$form .= &common_min_forms_end('',$table,$action,$skip) ;
    
return ($form) ;

} #------------------------------------------------------------------------------------------

sub common_min_edit_form {

my ($iid,$table,$action,$label_col,$field_col,$ignore_colpart,$skip) = @_ ;

our ($lcol,$fcol) = &common_min_columns($label_col,$field_col) ;

my $form = '' ;

$form .= &common_min_forms_start($table,$skip) ;

if (%sort_field) {
	foreach my $cnt (sort {$a <=> $b} keys %sort_field) {
		$col   = $sort_field{$cnt} ;
		$form .= &common_min_forms_loop($iid,$table,$ignore_colpart) ;    
		}
	}
else
	{
	foreach $col (sort keys %{$db{$table}{$iid}}) {
		$form .= &common_min_forms_loop($iid,$table,$ignore_colpart) ;    
		}
	}
    
$form .= &common_min_forms_end($iid,$table,$action,$skip) ;
       
return ($form) ;
    
} #------------------------------------------------------------------------------------------

sub common_min_forms_start {

my ($table,$skip) = @_ ;

if ($skip) { return ; }

my $form = qq(<form role="form" name="form_$table" id="$table-form"  method="post"  enctype="multipart/form-data">) ;

$form .= $extra_form_fields_start ; 

return ($form) ;

} #------------------------------------------------------------------------------------------

sub common_min_forms_loop {

	my ($iid,$table,$ignore_colpart) = @_ ;

	unless ($col) { return('') ; }
	   
	if ($hidden{$col}) { return('') ; }
	if ($ignore{$col}) { return('') ; }

	my ($colpart) = split(/\_/,$col) ;
	if (($ignore_colpart) and ($ignore{$colpart})) { return('') ; }

	$ucfirst_col = ucfirst $col ;
		
	my $val = $db{$table}{$iid}{$col} ;

	# $tindex++ ;

	my $form = '' ;

	if ($checkbox{$col}) {
		if ($val) { $checked = 'CHECKED'; } else { $checked = ''; }
		$form .= &common_min_form_checkbox($col,$val,$checked) ;    
		return ($form) ;
	}
		
	if ($radio{$col}) {
		$form .= &common_min_form_radio($col,$val) ;    
		return ($form) ;
	}
		
	&common_min_forms_required ;
		
	# $req_glyph = '' ; 

	# if ($required{$col}) { 
		# if    ($required{$col} == 1){ $required{$col} = 'data-validation="required"'                                         ; }
		# elsif ($required{$col} == 2){ $required{$col} = 'data-validation="length alphanumeric" data-validation-length="6-6"' ; }
		# elsif ($required{$col} == 3){ $required{$col} = 'data-validation="alphanumeric"'                                     ; }
		# elsif ($required{$col} == 4){ $required{$col} = 'data-validation="number"'                                           ; }    
		# elsif ($required{$col} == 5){ $required{$col} = 'data-validation="number" data-validation-allowing="range[0;1]"'     ; }    
		# elsif ($required{$col} == 6){ $required{$col} = 'data-validation="email"'                                            ; }    
		# elsif ($required{$col} == 7){ $required{$col} = 'data-validation="url"'                                              ; }
		
		# $req_glyph = qq(<i class="glyphicon glyphicon-asterisk yellow"></i>) ; 
		# }
		
	if ($datetimepicker{$col}) {
		unless ($val) { $val = $now_ccyy_mm_dd . " $now_hour:$now_min:$now_sec" ; }
		$form .= &common_min_form_datetimepicker($col,$val) ;    
		return ($form) ;
	}

	if ($datepicker{$col}) {
		unless ($val) { $val = $now_ccyy_mm_dd ; }
		$form .= &common_min_form_datepicker($col,$val) ;    
		return ($form) ;
	}
		
	if ($select{$col}) {
		$form .= &common_min_form_select($col,$val) ;    
		return ($form) ;
	}
		
	if ($textarea{$col}) {
		$form .= &common_min_form_textarea($col,$val) ;    
		return ($form) ;
	}
		
	if ($label{$col}) {
		$form .= &common_min_form_label($col,$val) ;    
		return ($form) ;
	}
		
	if ($add_col{$col}) {
		$form .= &common_min_form_input_col($col,$val) ;
	} else {
		$form .= &common_min_form_input($col,$val) ;
	}

	return ($form) ; 

} #------------------------------------------------------------------------------------------

sub common_min_forms_required {

$req_glyph{$col} = '' ; 

if ($required{$col}) { 
    if    ($required{$col} == 1){ $required{$col} = 'data-validation="required"'                                         ; }
    elsif ($required{$col} == 2){ $required{$col} = 'data-validation="length alphanumeric" data-validation-length="6-6"' ; }
    elsif ($required{$col} == 3){ $required{$col} = 'data-validation="alphanumeric"'                                     ; }
    elsif ($required{$col} == 4){ $required{$col} = 'data-validation="number"'                                           ; }    
    elsif ($required{$col} == 5){ $required{$col} = 'data-validation="number" data-validation-allowing="range[0;1]"'     ; }    
    elsif ($required{$col} == 6){ $required{$col} = 'data-validation="email"'                                            ; }    
    elsif ($required{$col} == 7){ $required{$col} = 'data-validation="url"'                                              ; }
    elsif ($required{$col} == 8){ $required{$col} = 'data-validation="" data-validation-allowing="" data-validation-decimal-separator=""' ; }
    elsif ($required{$col} == 9){ $required{$col} =  'data-validation="custom" data-validation-regexp="^\d+(\.\d{1,2})?$"' ; } # New condition
	elsif ($required{$col} == 10) { $required{$col} = 'data-validation="length number" data-validation-length="10-20"'     ; }  # 0846758345 or 27846758345
	
    $req_glyph{$col} = qq(<i class="glyphicon glyphicon-asterisk yellow"></i>) ; 
    }
	
} #------------------------------------------------------------------------------------------

sub common_min_forms_end {

my ($iid,$table,$action,$skip) = @_ ;

if ($skip) { return ; }

my $form = '' ;

$form .= $extra_form_fields ;   
    
foreach (keys %hidden) { 
	$form .= qq~<input type="hidden" name="$_" value="$db{$table}{$iid}{$_}">~ ; 
}

if ($dialog) { 
	$varbtn = qq~&nbsp;<button type="button" class="btn btn-default" onclick="parent.BootstrapDialog.closeAll();">Close</button>~ ;
} else { 
	$varbtn = qq~&nbsp;<button type="button" class="btn btn-default" onclick="history.go(-1);">Back</button>~ ; 
}
# if ($savjqy) { $savbtn = qq(<button type="button" class="btn btn-primary" id="savebutt">Save</button>) ;} else { $savbtn = qq(<button type="submit" class="btn btn-primary">Save</button>) ; }

if ($savjqy) { 
	my $custom_class = ($invoice_exists && !$useropts{super}{$username} && !$useropts{it}{$username}) ? "custom" : "primary" ; 
	my $custom_tooltip = ($invoice_exists) ? qq~title data-toggle="tooltip" data-placement="top" data-original-title="Invoice Nr Exists!!!"~ : qq~~ ; 
	$savbtn = qq~&nbsp;<button type="button" class="btn btn-$custom_class" id="savebutt" $custom_tooltip>Save</button>&nbsp;~ ;
} elsif ($srchjqy) { 
	$savbtn = qq~&nbsp;<button type="button" class="btn btn-primary" id="srchbutt">Search</button>~ ;
} elsif ($srchscr) { 
	$savbtn = qq~&nbsp;<button type="submit" class="btn btn-primary">Search</button>~ ; 
} else { 
	$savbtn = qq~&nbsp;<button type="submit" id="submitbutt" class="btn btn-primary">Save</button>~ ; 
}

if ($addbkb) { 
	$bakbtn = qq~&nbsp;<button type="button" class="btn btn-default" onclick="history.go(-1);">Back</button>~; 
} else { 
	$bakbtn = qq~~ ; 
}

if ($addcpb) { 
	$cpybtn = qq~&nbsp;<button type="button" class="btn btn-success" onclick="return copyMinItem('$iid');">Copy</button>~;
	&common_min_footer('id',"$lcpage\s");
} else { 
	$cpybtn = qq~~ ; 
}

my $custom_class = ($cancel_butt & $invoice_exists && !$useropts{super}{$username} && !$useropts{it}{$username}) ? "custom" : "danger" ; 
my $custom_tooltip = ($invoice_exists) ? qq~title data-toggle="tooltip" data-placement="top" data-original-title="Invoice Nr Exists!!!"~ : qq~~ ;
$canxbtn = ($cancel_butt) ? qq~&nbsp;<button type="button" class="btn btn-$custom_class" id="cancelbutt" $custom_tooltip>Cancel</button>&nbsp;~ : '' ;

$varbtn = $custom_back_button if $custom_back_button ;

$savbtn = qq~~ if $skip_save_btn ;

$form .= qq~<input type="hidden" name="iaction" value="$action">
    <div class='row'><div class="col-md-12">&nbsp;</div></div>
    <div class="row"><div class="col-md-12">$savbtn$canxbtn$cpybtn$varbtn$bakbtn</div></div><div class="row"><div class="col-md-12">&nbsp;</div></div>
    </form>~ ;
 
return ($form) ; 
    
} #------------------------------------------------------------------------------------------

sub common_min_columns {

my ($label_col,$field_col) = @_ ;

unless ($label_col) { $lcol = 2 ; } else { $lcol = $label_col ; }
unless ($field_col) { $fcol = 4 ; } else { $fcol = $field_col ; }

return ($lcol,$fcol) ;

} #------------------------------------------------------------------------------------------

sub common_min_status {

my ($closed,$deleted) = @_ ;

my ($class,$txt) ;

if    ($deleted == 1) { $class = 'danger'  ; $txt = 'DELETED' ; $del{$iaction} = 1 ; }
elsif ($closed == 1)  { $class = 'success' ; $txt = 'CLOSED'  ; $clo{$iaction} = 1 ; }
elsif ($closed == 0)  { $class = 'warning' ; $txt = 'ACTIVE'  ; $act{$iaction} = 1 ; }
    
return ($class,$txt) ;
        
} #------------------------------------------------------------------------------------------

sub common_min_form_view {

my ($field,$val,$type,$addclass,$dont_end_row) = @_ ;

my $ucfirst_field = ucfirst $field ;
my $print_field   = &common_min_print_field($field) ; if ($preferred_title{$field}) { $print_field = $preferred_title{$field} ; }

# $vrowcnt++; if ($vrowcnt % 2 == 0) { $bgcolor = '#cecbcb' ; } else { $bgcolor = '#eeeeee' ; }

$bgcolor = '#eeeeee' ;

unless ($val) { $val = '&nbsp;' ; }

my $row = qq(<div class='row'>
        <div class='fieldname col-md-$lcol'>
            <label class='control-label' for='input$ucfirst_field'>$print_field</label>
        </div>
        <div class='col-md-$fcol' style="background-color:$bgcolor;">
            <div class='control-group'>
                <div class='controls'> 
                    <!-- <span style="font-size:90%;"><em>$val</em></span> -->
					<span style="font-size:90%;">$val</span>
                </div>
            </div>
        </div>) ;

unless ($dont_end_row) { $row .= qq(</div>) ; }

$row = qq~$row<div class='row' style="height:12.5px;"><br></div>~ if $val =~ /<br>/ ;    

return ($row) ;
    
} #------------------------------------------------------------------------------------------

sub common_min_form_view_col {

my ($val) = @_ ;

# my $ucfirst_field = ucfirst $field ;
# my $print_field   = &common_min_print_field($field) ; if ($preferred_title{$field}) { $print_field = $preferred_title{$field} ; }

# $vrowcnt++; if ($vrowcnt % 2 == 0) { $bgcolor = '#cecbcb' ; } else { $bgcolor = '#eeeeee' ; } <div class='col-md-$fcol' style="background-color:$bgcolor;">

unless ($val) { $val = '&nbsp;' ; }

my $row = qq~
        <div class='col-md-$fcol'>
            <div class='control-group'>
                <div class='controls'> 
                    <span style="font-size:90%;"><em>$val</em></span>
                </div>
            </div>
		</div>~ ;
    
return ($row) ;
    
} #------------------------------------------------------------------------------------------

sub common_min_form_input {

my ($field,$val,$type,$addclass,$dont_end_row) = @_ ;

if ($field eq 'password') { $type = 'password' ; } unless ($type) { $type = 'text' ; } if ($cust_class{$field}) { $addclass = $cust_class{$field} ; }

my $ucfirst_field = ucfirst $field ;
my $print_field   = &common_min_print_field($field) ; if ($preferred_title{$field}) { $print_field = $preferred_title{$field} ; }

if ($type eq 'hidden') { return ("<input type='$type' name='$field' id='input$ucfirst_field' value='$val'>") ; }

my $labelid = '' ; if ($label_id{$field}) { $labelid = $label_id{$field} ; }

$tindex++ ;

$fcol = $fcol{$field} if $fcol{$field} ;

my $row = '' ;
     
if ($add_finish_div{$field}) { $row .= qq~</div>~ ; }

$row .= qq~<div class='row'>
        <div class='fieldname col-md-$lcol'>
            <label class='control-label' for='input$ucfirst_field' $labelid>$print_field&nbsp;$req_glyph{$field}&nbsp;$info_glyph_tooltip{$field}</label>
        </div>
        <div class='col-md-$fcol'>
            <div class='control-group'>
                <div class='controls'> 
                    <input type='$type' name='$field' class='form-control $addclass' id='input$ucfirst_field' placeholder='$print_field' value="$val" $readonly{$field} $required{$field} tabindex="$tindex">
                </div>
            </div>
        </div>$dlg{$field}~ ;
    
if (not $dont_end_row and not $dont_end_row{$field}) { $row .= qq~</div>~ ; }
    
return ($row) ;
    
} #------------------------------------------------------------------------------------------

sub common_min_form_input_col {

my ($field,$val) = @_ ;

unless ($type) { $type = 'text' ; }

my $ucfirst_field = ucfirst $field ;
my $print_field   = &common_min_print_field($field) ; if ($preferred_title{$field})       { $print_field = $preferred_title{$field}       ; }
my $placeholder   = $print_field ;                    if ($preferred_placeholder{$field}) { $placeholder = $preferred_placeholder{$field} ; }
my $field_id      = "input$ucfirst_field" ;

# unless ($excl_on_change{$field}) { push @jquery_workings_trigger_fields, $field_id ; }
unless ($excl_on_change{$field}) { push @jquery_trigger_fields, $field_id ; }

if ($type eq 'hidden') { return ("<input type='$type' name='$field' id='$field_id' value='$val'>") ; }

$tindex++ ;

$fcol = $fcol{$field} if $fcol{$field} ;

my $row = qq~
        <div class='col-md-$fcol'>
            <div class='control-group'>
                <div class='controls'> 
                    <input type='$type' name='$field' class='form-control $cust_class{$field}' id='$field_id' $input_style{$field} placeholder='$placeholder' value="$val" $readonly{$field} $required{$field} tabindex="$tindex">
                </div>
            </div>
        </div>
		$dlg{$field}~ ;
    
return ($row) ;
    
} #------------------------------------------------------------------------------------------

sub common_min_form_label {

my ($field,$val) = @_ ;

$hidden{$field}   = 1 ;

my $ucfirst_field = ucfirst $field ;
my $print_field   = &common_min_print_field($field) ; if ($preferred_title{$field}) { $print_field = $preferred_title{$field} ; }
my $field_id      = "label$ucfirst_field" ;
		
my $row .= qq~<div class='row'>
        <div class='fieldname col-md-$lcol'>
            <label class='control-label' for='input$ucfirst_field' $labelid>$print_field&nbsp;</label>
        </div>
        <div class='col-md-$fcol'>
            <div class="col-md-12">$val</div>
        </div>$dlg{$field}~ ;
		    
return ($row) ;
    
} #------------------------------------------------------------------------------------------

sub common_min_form_label_col {

my ($field) = @_ ;

my $ucfirst_field = ucfirst $field ;
my $print_field   = &common_min_print_field($field) ; if ($preferred_title{$field}) { $print_field = $preferred_title{$field} ; }
my $field_id      = "label$ucfirst_field" ;

my $row = qq~<div class='fieldname col-md-$lcol'>
            <label class='control-label' for='input$ucfirst_field' label$ucfirst_field style='white-space:nowrap;'>$print_field $req_glyph{$field} $info_glyph_tooltip{$field}</label>
        </div>~;
		
unless ($excl_on_change{$field}) { push @jquery_trigger_fields, $field_id ; }
# unless ($excl_on_change{$field}) { push @jquery_workings_trigger_fields, $field_id ; }
    
return ($row) ;
    
} #------------------------------------------------------------------------------------------

sub common_min_form_textarea {

my ($field,$val,$height) = @_ ;

if ($textarea_height{$field}) { $height = $textarea_height{$field}; }

unless ($height) { $height = 100; } 

my $ucfirst_field = ucfirst $field ;
my $print_field   = &common_min_print_field($field) ; if ($preferred_title{$field}) { $print_field = $preferred_title{$field} ; }

$tindex++ ;

my $row = qq~<div class='row'>
        <div class='fieldname col-md-$lcol'>
            <label class='control-label' for='textarea$ucfirst_field'>$print_field $req_glyph{$field} $info_glyph_tooltip{$field}</label>
        </div>
        <div class='col-md-$fcol'>            
            <div class="controls">
                <textarea name="$field" style="width:100%;height:$height\px;" id='textarea$ucfirst_field' class="form-control" placeholder="$print_field" $readonly{$field} $required{$field} tabindex="$tindex">$val</textarea>
            </div>                            
        </div>
    </div>~ ;
   
return ($row) ;
    
} #------------------------------------------------------------------------------------------

sub common_min_form_textarea_col {

my ($field,$val,$height,$hide) = @_ ;

if ($textarea_height{$field}) { $height = $textarea_height{$field}; }

unless ($height) { $height = 100; } 

my $ucfirst_field = ucfirst $field ;
my $print_field   = &common_min_print_field($field) ; if ($preferred_title{$field}) { $print_field = $preferred_title{$field} ; }

$tindex++ ;

my $hide = ($hide) ? "none" : "" ;

my $row = qq~
        <div class='col-md-$fcol'>            
            <div class="controls">
                <textarea name="$field" style="width:100%;height:$height\px;display:$hide;" id='textarea$ucfirst_field' class="form-control" placeholder="$print_field" $readonly{$field} $required{$field} tabindex="$tindex">$val</textarea>
            </div>                            
        </div>~ ;
   
return ($row) ;
    
} #------------------------------------------------------------------------------------------

sub common_min_form_checkbox {

my ($field,$val,$checked,$classic) = @_ ;

my $ucfirst_field = ucfirst $field ;
my $print_field   = &common_min_print_field($field) ; if ($preferred_title{$field}) { $print_field = $preferred_title{$field} ; }
my $check_class   = ($classic) ? 'class="iphone-toggle"' : '' ;
# <input name="$field" data-no-uniform="true" type="checkbox" class="iphone-toggle" id="checkbox$ucfirst_field" $checked value="1" tabindex="$tindex">

$tindex++ ;

my $row = qq(<div class='row'>
            <div class="fieldname col-md-$lcol"><label class="control-label" for="checkbox$ucfirst_field">$print_field</label></div>
            <div class="col-md-$fcol">
                <input name="$field" data-no-uniform="true" type="checkbox" $check_class id="checkbox$ucfirst_field" $checked value="1" tabindex="$tindex">
            </div>
        </div>) ;
		
# <input name="$field" data-no-uniform="true" type="checkbox" class="iphone-toggle" id="checkbox$ucfirst_field" $checked value="1" tabindex="$tindex">
    
return ($row) ;

} #------------------------------------------------------------------------------------------

sub common_min_form_checkbox_col {

my ($field,$val,$checked,$classic) = @_ ;

my $ucfirst_field = ucfirst $field ;
my $print_field   = &common_min_print_field($field) ; if ($preferred_title{$field}) { $print_field = $preferred_title{$field} ; }
my $field_id      = "checkbox$ucfirst_field" ;
my $check_class   = ($classic) ? 'class="iphone-toggle"' : '' ;

my $label = '' ; if ($label{$field}) { $label = "<label for='$field_id' $style_field{$field}></label>" ; }

$tindex++ ;

my $row = qq(<div class="col-md-$fcol checkbox_checked" id="c_$field">
                <input name="$field" data-no-uniform="true" type="checkbox" $check_class id="$field_id" $style_field{$field} $checked value="1" tabindex="$tindex">
				$label
            </div>) ;

unless ($excl_on_change{$field}) { push @jquery_trigger_fields, $field_id ; }
# unless ($excl_on_change{$field}) { push @jquery_workings_trigger_fields, $field_id ; }
    
return ($row) ;

} #------------------------------------------------------------------------------------------

sub common_min_form_radio {

my ($field,$val,$opts) = @_ ;

my $ucfirst_field = ucfirst $field ;
my $print_field   = &common_min_print_field($field) ; if ($preferred_title{$field}) { $print_field = $preferred_title{$field} ; }

my @opts = split(/\+/,$radio_opts{$field});

foreach (@opts) {
    if ($_ eq $val) { $checked = 'CHECKED'; } else { $checked = ''; }
    $print_radios .= qq(<input type="radio" name="$field" id="radio$val" value="$val" $checked> $_) ;
    }
       
my $row = qq(<div class='row'>
                <div class="fieldname col-md-$lcol"><label class="control-label" for="radio$ucfirst_field">$print_field</label></div>
                <div class="radio col-md-$fcol">
                    <label>
                        $print_radios
                    </label>
                </div>
            </div>) ;
    
return ($row) ;

} #------------------------------------------------------------------------------------------

sub common_min_form_select {

my ($field,$val,$dont_end_row) = @_ ;

if ($multiple{$field}) { $setmultiple = 'multiple' ; } else { $setmultiple = '' ; }

my $ucfirst_field = ucfirst $field ;
my $print_field   = &common_min_print_field($field) ; if ($preferred_title{$field}) { $print_field = $preferred_title{$field} ; }

$tindex++ ;

my $row = qq~<div class="row">
                <div class="fieldname col-md-$lcol"><label class="control-label" for="select$ucfirst_field">$print_field $req_glyph{$field} $info_glyph_tooltip{$field}</label></div>
                <div class="col-md-$fcol">
                    <div class="control-group">
                        <div class="controls">
                            <select class="form-control" style="margin:2px 0 2px 0;" name="$field" id="select$ucfirst_field" $setmultiple data-placeholder="Select $print_field" data-rel="chosen" $required{$field} tabindex="$tindex">
                                <option value=""></option>
                                $opts{$field}                                       
                            </select>
                        </div>
                    </div>
                </div>
				$dlg{$field}~ ;
            
unless ($dont_end_row) { $row .= qq~</div>~ ; }

if ($allow_deselect{$field}) { 
	if ($minify_jquery) {
		push @all_select_ids,"#select$ucfirst_field" if $ucfirst_field ;
	} else {
		$trigger_jquery_raw .= qq~\$("#select$ucfirst_field").chosen({ allow_single_deselect:true,search_contains:true });~ ;
	} 
}

# if ($val) {
	# $trigger_jquery_raw .= qq~\$("#select$ucfirst_field").val($val) ; \$("#select$ucfirst_field").trigger("chosen:updated"); ~ ;
# }

return ($row) ;
    
} #------------------------------------------------------------------------------------------

sub common_min_form_select_col {

my ($field,$val) = @_ ;

my $ucfirst_field = ucfirst $field ;
my $print_field   = &common_min_print_field($field) ; if ($preferred_title{$field}) { $print_field = $preferred_title{$field} ; }

if ($multiple{$field}) { $setmultiple = 'multiple' ; } else { $setmultiple = '' ; }

$tindex++ ;

my $row = qq~<div class="col-md-$fcol">
                <div class="control-group">
                    <div class="controls">
                        <select class="form-control" style="margin:2px 0 2px 0;" name="$field" id="select$ucfirst_field" $setmultiple data-placeholder="Select $print_field" data-rel="chosen" $required{$field} tabindex="$tindex">
                            <option value=""></option>
                            $opts{$field}                                       
                        </select>
						$dlg_operator{$field}
                    </div>
                </div>
            </div>
			$dlg{$field}~ ;
			
if ($allow_deselect{$field}) { 
	if ($minify_jquery) {
		push @all_select_ids,"#select$ucfirst_field" if $ucfirst_field ;
	} else {
		$trigger_jquery_raw .= qq~\$("#select$ucfirst_field").chosen({ allow_single_deselect:true });~ ;
	} 
}

# if ($val) {
	# $trigger_jquery_raw .= qq~\$("#select$ucfirst_field").val($val) ; \$("#select$ucfirst_field").trigger("chosen:updated"); ~ ;
# }
    
return ($row) ;
    
} #------------------------------------------------------------------------------------------

sub common_min_form_datetimepicker {

my ($field,$val,$diff) = @_ ;

   $diff = 10 unless $diff ;
my $ucfirst_field = ucfirst $field ;
my $print_field   = &common_min_print_field($field) ; if ($preferred_title{$field}) { $print_field = $preferred_title{$field} ; }

$tindex++ ;

# if ($field eq 'date_from') { $val   = "$now_ccyy_mm_dd $now_hour:00:00" unless $val ; }
if ($field eq 'date_from') { $val   = "$now_ccyy_mm_dd 08:00:00" unless $val ; }
if ($field eq 'date_to')   { my $hr = sprintf("%02s", $now_hour + 2) ; $val = "$now_ccyy_mm_dd $hr:00:00" unless $val ; }

my $row = qq(<div class="row">
                <div class="fieldname col-md-$lcol"><label class="control-label" for="datetimepicker$ucfirst_field">$print_field $req_glyph{$field} $info_glyph_tooltip{$field}</label></div>
                <div class="col-md-$fcol">
                    <div class="control-group">
                        <div style="margin:2px 0 2px 0;" class="input-group controls input-append date" data-date="$val" id="datetimepicker$ucfirst_field">
                            <input style="margin:0;" data-format="yyyy-MM-dd hh:mm:ss" class="form-control" name="$field" READONLY value="$val" tabindex="$tindex">
                            <span class="input-group-addon add-on">
                              <i data-time-icon="icon-time" data-date-icon="icon-calendar">
                              </i>
                            </span>
                        </div>
                    </div>
                </div>
            </div>) ;
          
$trigger_jquery_raw .= qq(\$('#datetimepicker$ucfirst_field').datetimepicker({language: 'pt-BR'}); ) ;

my @field_name_parts = split(/\_/,$field) ;

if ($field_name_parts[-1] eq 'from' && !($ENV{'SCRIPT_NAME'} =~ /event_quotes/ && ($ENV{'QUERY_STRING'} =~ /edit/ || $ENV{'QUERY_STRING'} =~ /add/))) {
	pop @field_name_parts ; my $field_prefix = join("_",@field_name_parts) ; my $ucfirst_field_prefix = ucfirst $field_prefix ;
	if ($ENV{'QUERY_STRING'} =~ /filter/ || $ENV{'QUERY_STRING'} =~ /search/ || $ENV{'SCRIPT_NAME'} =~ /report/) {
		$trigger_jquery_raw .= qq~
		\$('#datetimepicker$ucfirst_field_prefix\_from').on('changeDate', function(e) {
			var datetimepickerDate_from = \$("input[name='$field_prefix\_from']").val();
			var datetimepickerDate_to = \$("input[name='$field_prefix\_to']").val();
			var newDatetimepickerDate_from = new Date(datetimepickerDate_from);
			var newDatetimepickerDate_to = new Date(datetimepickerDate_to);
			
			if (newDatetimepickerDate_from > newDatetimepickerDate_to) {
				newDatetimepickerDate_to = new Date(datetimepickerDate_from);
			}

			console.log("datetimepickerDate_from="+datetimepickerDate_from) ;
			newDatetimepickerDate_from.setHours(+2);
			newDatetimepickerDate_from.setMinutes(0);
			newDatetimepickerDate_from.setSeconds(0);
			newDatetimepickerDate_from.setMilliseconds(0);
			console.log("newDatetimepickerDate_from="+newDatetimepickerDate_from) ;
			\$("#datetimepicker$ucfirst_field_prefix\_from").datetimepicker("setDate", newDatetimepickerDate_from);
			
			
			console.log("datetimepickerDate_to="+newDatetimepickerDate_to) ;
			newDatetimepickerDate_to.setHours(23+2);
			newDatetimepickerDate_to.setMinutes(59);
			newDatetimepickerDate_to.setSeconds(59);
			newDatetimepickerDate_to.setMilliseconds(59);
			console.log("newDatetimepickerDate_to="+newDatetimepickerDate_to) ;
			\$("#datetimepicker$ucfirst_field_prefix\_to").datetimepicker("setDate", newDatetimepickerDate_to);
		}) ;
		\$('#datetimepicker$ucfirst_field_prefix\_to').on('changeDate', function(e) {
			var datetimepickerDate_to = \$("input[name='$field_prefix\_to']").val();
			var newDatetimepickerDate_to = new Date(datetimepickerDate_to);
			console.log("datetimepickerDate_to="+datetimepickerDate_to) ;
			newDatetimepickerDate_to.setHours(23+2);
			newDatetimepickerDate_to.setMinutes(59);
			newDatetimepickerDate_to.setSeconds(59);
			newDatetimepickerDate_to.setMilliseconds(59);
			console.log("newDatetimepickerDate_to="+newDatetimepickerDate_to) ;
			\$("#datetimepicker$ucfirst_field_prefix\_to").datetimepicker("setDate", newDatetimepickerDate_to);
		}) ;
	~;
	} else {
		$trigger_jquery_raw .= qq~
			if (\$("#datetimepicker$ucfirst_field_prefix\_to").length) {
				\$('#datetimepicker$ucfirst_field_prefix\_from').on('changeDate', function(e) {
					var datetimepickerDate_from = \$("input[name='$field_prefix\_from']").val();
					console.log("datetimepickerDate_from="+datetimepickerDate_from) ;

					if (datetimepickerDate_from) {
						// Parse datetimepickerDate_from value to a Date object
						var datetimepickerDate_to = new Date(datetimepickerDate_from);
						console.log("datetimepickerDate_to="+datetimepickerDate_to) ;

						// Add 2 hours to datetimepickerDate_from value
						// datetimepickerDate_to.setHours(datetimepickerDate_to.getHours() + $diff);
						datetimepickerDate_to.setHours(datetimepickerDate_to.getHours() + $diff);
						console.log("datetimepickerDate_to="+datetimepickerDate_to) ;
						\$("#datetimepicker$ucfirst_field_prefix\_to").datetimepicker("setDate", datetimepickerDate_to);
					}
				}) ;
			}
		~;
	}
}

return ($row) ;

# <input style="margin:0;" data-format="yyyy-MM-dd hh:mm:ss" class="form-control" name="$field" value="$val" READONLY tabindex="$tindex">
    
} #------------------------------------------------------------------------------------------

sub common_min_form_datepicker {

my ($field,$val,$label,$dont_end_row) = @_ ;

my $ucfirst_field = ucfirst $field ;
my $print_field   = &common_min_print_field($field) ;  if ($label) { $print_field = $label ; } if ($preferred_title{$field}) { $print_field = $preferred_title{$field} ; }

if ($val eq '0000-00-00') { $val = '' ; }

$tindex++ ;
          
my $row = qq~<div class="row">
				<div class="fieldname col-md-$lcol"><label class="control-label" for="datepicker$ucfirst_field">$print_field $req_glyph{$field} $info_glyph_tooltip{$field}</label></div>
				<div class="col-md-$fcol">
					<div class="control-group">                                
						<div style="margin:2px 0 2px 0;" class="input-group controls input-append date" data-date="$val" id="datepicker$ucfirst_field">
							<input style="margin:0;" class='form-control' name="$field" value="$val" $required{$field} placeholder='ccyy-mm-dd' tabindex='$tindex' $readonly{$field}>
							<span class="input-group-addon add-on">
								<span style="cursor:pointer;" class="glyphicon glyphicon-calendar"></span>
							</span>
						</div>                   
					</div>
				</div>                        
			~ ;
			
unless ($dont_end_row) {
	$row .= qq~
			</div>
		~ ;
}
          
&common_min_form_datepicker_jquery($ucfirst_field);

return ($row) ;

# <input style="margin:0;" class='form-control' name="$field" value="$val" READONLY tabindex='$tindex'>
    
} #------------------------------------------------------------------------------------------

sub common_min_form_datepicker_col {

my ($field,$val,$end_row) = @_ ;

my $ucfirst_field = ucfirst $field ;
my $print_field   = &common_min_print_field($field) ; if ($preferred_title{$field}) { $print_field = $preferred_title{$field} ; }

$tindex++ ;

if ($val eq '0000-00-00') { $val = '' ; }
          
my $row = qq(<div class="col-md-$fcol">
					<div class="control-group">                                
						<div style="margin:2px 0 2px 0;" class="input-group controls input-append date" data-date="$val" id="datepicker$ucfirst_field">
							<input id="datepick$ucfirst_field" style="margin:0;" class='form-control' name="$field" value="$val" $required{$field} placeholder='ccyy-mm-dd' tabindex='$tindex' $readonly{$field}>
							<span class="input-group-addon add-on">
								<span style="cursor:pointer;" class="glyphicon glyphicon-calendar"></span>
							</span>
						</div>                   
					</div>
				</div>) ;
 
$row .= qq~</div>~ if $end_row ;
 
&common_min_form_datepicker_jquery($ucfirst_field);
	
return ($row) ;
    
} #------------------------------------------------------------------------------------------

sub common_min_form_datepicker_jquery {

	my ($ucfirst_field) = @_ ;

	$trigger_jquery_raw .= qq~
	\$('#datepicker$ucfirst_field').datepicker({
		format: "yyyy-mm-dd"}).on('changeDate', function (ev) {
		\$('.datepicker').hide();
		}) ; 
	~ ;

	my $lc_field         = lc $ucfirst_field ;
	my @field_name_parts = split(/\_/,$lc_field) ;

	# if ($field_name_parts[-1] eq 'from') {
		# pop @field_name_parts ; my $field_prefix = join("_",@field_name_parts) ; my $ucfirst_field_prefix = ucfirst $field_prefix ;
		# $trigger_jquery_raw .= qq~
			# if (\$("#datepicker$ucfirst_field_prefix\_to").length) {
				# // \$('#datepicker$ucfirst_field_prefix\_from').on('changeDate', function(e) {
					# // var datepickerDate_from = \$("input[name='$field_prefix\_from']").val();
					# // console.log("datepickerDate_from [$field_prefix\_from]="+datepickerDate_from) ;

					# // if (datepickerDate_from) {
						# // // Parse datepickerDate_from value to a Date object
						# // var datepickerDate_to = new Date(datepickerDate_from);
						# // console.log("datepickerDate_to="+datepickerDate_to) ;
						# // // Add 1 month to datepickerDate_from value
						# // datepickerDate_to.setMonth(datepickerDate_to.getMonth() + 1);
						# // // var formattedDate = \$("#datepicker$ucfirst_field_prefix\_to").formatDate("yyyy-mm-dd", new Date(datepickerDate_to));
						# // // console.log("datepickerDate_to [datepicker$ucfirst_field_prefix\_to]="+datepickerDate_to+" ["+formattedDate+"]") ;
						# // console.log("datepickerDate_to [datepicker$ucfirst_field_prefix\_to]="+datepickerDate_to) ;
						# // // \$("#datepicker$ucfirst_field_prefix\_to").datepicker("setDate", new Date(datepickerDate_to));
						# // // \$("#datepicker$ucfirst_field_prefix\_to").setDate(datepickerDate_to);
					# // }
				# // }) ;
				# var nowTemp = new Date();
				# var now = new Date(nowTemp.getFullYear(), nowTemp.getMonth(), nowTemp.getDate(), 0, 0, 0, 0);

				# var from = \$('#datepicker$ucfirst_field_prefix\_from').datepicker({
					# format: "yyyy-mm-dd",
					# onRender: function (date) {
						# return date.valueOf() < now.valueOf() ? 'disabled' : '';
					# }
				# }).on('changeDate', function (ev) {
					# if (ev.date.valueOf() > to.date.valueOf()) {
						# var newDate = new Date(ev.date)
						# newDate.setDate(newDate.getDate() + 30);
						# to.setValue(newDate);
					# }
					# // from.hide();
					# \$('#datepicker$ucfirst_field_prefix\_to')[0].focus();
				# }).data('datepicker');
				# var to = \$('#datepicker$ucfirst_field_prefix\_to').datepicker({
					# format: "yyyy-mm-dd",
					# onRender: function (date) {
						# return (date.valueOf() < now.valueOf()) && (date.valueOf() >= from.date.valueOf()) ? 'disabled' : '';
					# }
				# }).on('changeDate', function (ev) {
					# // to.hide();
				# }).data('datepicker');
			# }
		# ~;
	# }

} #------------------------------------------------------------------------------------------

# sub common_min_form_datepicker {

# my ($field,$val) = @_ ;

# my $ucfirst_field = ucfirst $field ;
# my $print_field   = &common_min_print_field($field) ;

# my $row = qq(<div class="row">
                # <div class="fieldname col-md-$lcol"><label class="control-label" for="datepicker$ucfirst_field">$print_field $req_glyph{$field} $info_glyph_tooltip{$field}</label></div>
                # <div class="col-md-$fcol">
                    # <div class="control-group">
                        # <div style="margin:2px 0 2px 0;" class="input-group controls input-append date" data-date="$val" id="datetimepicker$ucfirst_field">
                            # <input style="margin:0;" data-format="yyyy-MM-dd hh:mm:ss" class="form-control" name="$field" value="$val" READONLY tabindex="2">
                            # <span class="input-group-addon add-on">
                              # <i data-time-icon="icon-time" data-date-icon="icon-calendar">
                              # </i>
                            # </span>
                        # </div>
                    # </div>
                # </div>
            # </div>) ;
          
# $trigger_jquery_raw .= qq(\$('#datetimepicker$ucfirst_field').datetimepicker({language: 'pt-BR'}); ) ;

# # $trigger_jquery_raw .= qq(
# # \$('#datepicker$ucfirst_field').datepicker({
    # # format: "yyyy-mm-dd"}).on('changeDate', function (ev) {
    # # \$('.datepicker').hide();
	# # }) ; 
# # ) ;

# return ($row) ;
    
# } #------------------------------------------------------------------------------------------

sub common_min_form_timepicker_col {
	
    my ($field,$val,$diff) = @_;

    $diff = 10 unless $diff;
    my $ucfirst_field = ucfirst $field;
    my $print_field   = ($preferred_title{$field}) ? $preferred_title{$field} : &common_min_print_field($field) ;

    $tindex++;
		
    my $row = qq~
		<div class="col-md-$fcol">
			<div class="control-group">
				<div style="margin: 2px 0 2px 0;$custom_style{$field}" class="input-group controls input-append date" data-date="$val" id="timepicker$ucfirst_field">
					<input style="margin: 0;" data-format="hh:mm:ss" class="form-control" name="$field" READONLY value="$val" tabindex="$tindex">
					<span class="input-group-addon add-on">
						<i data-time-icon="icon-time" data-date-icon="icon-calendar"></i>
					</span>
				</div>
			</div>
		</div>
    ~ ;
	
    $trigger_jquery_raw .= qq(\$('#timepicker$ucfirst_field').datetimepicker({language:'pt-BR',pickDate:false});) ;

    return $row ;
	
} #------------------------------------------------------------------------------------------

# # sub common_min_form_radio_col {

	# # my ($field,$val,$opts) = @_ ;

	# # my $ucfirst_field = ucfirst $field ;
	# # my $print_field   = &common_min_print_field($field) ; if ($preferred_title{$field}) { $print_field = $preferred_title{$field} ; }

	# # my @opts = split(/\+/,$radio_opts{$field}); my $cnt = 0 ; 

	# # foreach (@opts) {	
		# # my $value = $_ ; $value = lc $value ; $value =~ s/ /\_/g ; $cnt++ ;
		# # if ($value eq $val) { $checked = 'CHECKED'; } else { $checked = ''; }
		# # $print_radios .= qq~<div class="fieldname col-md-$fcol"><input type="radio" name="$field" id="radio_$cnt" value="$value" $checked><span style="font-size:95%;">&nbsp;$_</span></div>~ ; #### WHY NOT class="radio col-md-$fcol" ?????????????
	# # }
		   
	# # my $row = qq~<div class="fieldname col-md-$lcol">
					# # <label class="control-label" for="radio$ucfirst_field">$print_field</label>
					# # <input name="selected_$field" id="selected_$field" value="" style="display:none;">
				# # </div>
				# # $print_radios~ ;
	 
	# # for (1 .. $cnt) {
		# # $trigger_jquery_raw .= qq~
			# # \$("#radio_$_").change(function () {
				# # if(\$(this).is(":checked")) {
					# # let sel_val = \$("#radio_$_").val() ;
					# # console.log("sel_val : "+sel_val) ;
					# # \$("#selected_$field").val(sel_val) ;
				# # } 
			# # }) ;
		# # ~ ;	
	# # }
	  
	# # return ($row) ;

# # } #------------------------------------------------------------------------------------------

sub common_min_form_radio_col {

	my ($field,$val,$opts,$columns,$nr_of_columns_to_display,$radio_row_id,$row_cnt,$display_horizontally) = @_ ;

	
	# my $print_field   = &common_min_print_field($field) ; if ($preferred_title{$field}) { $print_field = $preferred_title{$field} ; }

	$nr_of_columns_to_display = $columns unless $nr_of_columns_to_display ;
	
	$row_cnt = 1 unless $row_cnt ; 
	
	my %display = () ; 
	for ($nr_of_columns_to_display+1 .. 10) {
		$display{$_} = qq~style="display:none;"~ ;
	}
	$display{row} = qq~style="display:none;"~ if $nr_of_columns_to_display < 0 ; ## HIDE SECOND ROW BY DEFAULT
	
	my @default_checked = split(/\;/,$val) ; my $print_radios = qq~~ ; my @opts = split(/\+/,$radio_opts{$field}); my $cnt = 0 ; my $ucfirst_field = ucfirst $field ;
	
	$print_radios .= qq~<div class="row" id="$row_id" $display{row}>~ if $display_horizontally ;
	
	foreach my $option (@opts) {	
		
		$cnt++ ;
		
		my $row_id = ($radio_row_id) ? "$radio_row_id\_$cnt" : "" ;
		
		# my $print_field = ucfirst $option; 
		
		$print_radios .= qq~<div class="row" id="$row_id" $display{row}>~ unless $display_horizontally ;
		
		my $align = qq~align='right'~ if $display_horizontally ;
		
		$print_radios .= qq~
				<div class="fieldname col-md-$lcol" $align>
					<label class="control-label" for="radio$ucfirst_field">$option</label>
				</div>~ ;
		for (1 .. $columns) {
			my $value = $option; $value = lc $value ; $value =~ s/ /\_/g ; 
			
			my $radio_nr = ($display_horizontally) ? $display_horizontally : ($row_cnt*$columns - $columns) + $_ ; 
			
			if ($value eq $default_checked[$radio_nr - 1]) { $checked = 'CHECKED'; } else { $checked = ''; }
			
			$print_radios .= qq~<input name="selected_$radio_nr\_$field" id="selected_$radio_nr\_$field" value="" style="display:none;">~ if $cnt == 1 ;		
			$print_radios .= qq~		
				<div class="fieldname col-md-$fcol">
					<input type="radio" name="$field\_$radio_nr" id="radio_$radio_nr\_$cnt" value="$cnt" $checked $display{$_}>
				</div>
			~ ;			
		}

		$print_radios .= qq~	
			</div>
		~ unless $display_horizontally ; #### WHY NOT class="radio col-md-$fcol" ?????????????
	}
		
	$print_radios .= qq~	
		</div>
	~ if $display_horizontally ;
		
	my $row = qq~$print_radios~ ;
	 
	my @radio_ids = () ;  my $radio_to_selected = qq~~ ;
	 
	for my $option_cnt (1 .. $cnt) {
		for (1 .. $columns) {
			push @radio_ids,"#radio_$_\_$option_cnt" ;
			$radio_to_selected .= qq~"radio_$_\_$option_cnt":"selected_$_\_$option_cnt",~ ;
		}
	}
	
	chop $radio_to_selected if $radio_to_selected ;
	my $radio_ids_string = join(",",@radio_ids) ; 
	$trigger_jquery_raw .= qq~
		\$("$radio_ids_string").change(function () {
			if (\$(this).is(":checked")) {	
				const id = this.id ;
				const radio_to_selected = {$radio_to_selected} ; 
				let sel_val = \$(this).val() ;
				// console.log("sel_val : "+sel_val) ;
				\$("#"+radio_to_selected[id]).val(sel_val) ;
			} 
		}) ;
	~ ;	
	  
	return ($row) ;

} #------------------------------------------------------------------------------------------

sub common_min_print_field {

my ($txt) = @_ ;

if (($testing == 1) and ($username eq 'rory')) { return($txt) ; }

if ($addid{$txt}) { $addtxt = ' ID' ; } else { $addtxt = '' ; }

$txt =~ s/\_id/ /gi ;

@strings     = split(/\_/,$txt) ; 
@strings_ucf = map(ucfirst, map(lc,@strings)); 
$txt         = join(' ', @strings_ucf);
$txt        .= $addtxt ;

return ($txt) ;

} #------------------------------------------------------------------------------------------

sub common_min_table {

my ($id,$page,$list,$sort_col,$do_not_auto_sort_table) = @_ ;

unless ($sort_col) { $sort_col = 0 ; } if ($fnsortcol) { $sort_col = $fnsortcol ; }

my $sort_order = ($do_not_auto_sort_table) ? '' : 'desc' ; if ($fnsortorder) { $sort_order = $fnsortorder; }

$print_box_content_rows .= qq~
    <table id="itv-table" class="table table-striped table-bordered bootstrap-datatable datatable responsive">    
        $print_thead
        <tbody>
            $print_tbody
        </tbody>
    </table>~ ;
    
if ($list) { $trigger_jquery .= qq~var oTable = \$('#itv-table').dataTable(); oTable.fnSort( [ [$sort_col,'$sort_order'] ] );~ ; }

&common_min_footer($id,$page);
   
} #------------------------------------------------------------------------------------------

sub common_min_footer {

my ($id,$page) = @_ ;

if ($uniq_id) { $id = $uniq_id ; }
    
$print_footer_jscript = qq(
<script>

function viewMinItem (id) 
{  
   document.view_min_form.$id.value = id ;
   \$('#view-min-form').submit();
}

function editMinItem (id,page) 
{  
   document.edit_min_form.$id.value = id ;
   if (page && typeof page != 'undefined') { \$('#edit-min-form').attr('action', 'edit-'+page); }
   \$('#edit-min-form').submit();
}

function copyMinItem (id) 
{  
   document.copy_min_form.$id.value = id ;
   \$('#copy-min-form').submit();
}

function deleteMinItem (name,id) 
{
   BootstrapDialog.confirm({
        title: 'Confirm Delete',
        message: 'Are you sure you want to delete <strong>'+name+'</strong>?',
        type: BootstrapDialog.TYPE_DANGER, // <-- Default value is BootstrapDialog.TYPE_PRIMARY <-- Default value is BootstrapDialog.TYPE_WARNING
        callback: function(result) {            
            if(result) {
               document.delete_min_form.$id.value = id ;
               \$('#delete-min-form').submit();                 
            }
        }
    });
}

</script>) ;

if ($page && $ENV{SCRIPT_NAME} =~ /\_test/) { $page .= '-test' ; }

if ($page) { $action_delete = qq(delete-$page) ; $action_edit = qq(edit-$page) ; $action_view = qq(view-$page) ; $action_copy = qq(copy-$page) ; } else { $action_delete = '' ; $action_edit = '' ; $action_view = '' ; $action_copy = '' ; }

if ($blank_action) { $action_delete = '' ; $action_edit = '' ; $action_view = '' ; $action_copy = '' ; }
    
$print_footer_forms .= qq~
<form name="delete_min_form" id="delete-min-form" action="$action_delete"    method="post">
<input type="hidden" name="iaction"      value="delete">
<input type="hidden" name="$id"          value="">
</form>

<form name="edit_min_form" id="edit-min-form" action="$action_edit"  $open_new_tab  method="post">
<input type="hidden" name="iaction"  value="edit">
<input type="hidden" name="$id"      value="">
$extra_edit_hidden_inputs
</form>

<form name="view_min_form" id="view-min-form" action="$action_view" $open_new_tab   method="post">
<input type="hidden" name="iaction"  value="view">
<input type="hidden" name="$id"      value="">
</form>

<form name="copy_min_form" id="copy-min-form" action="$action_copy"  $open_new_tab  method="post">
<input type="hidden" name="iaction"  value="copy">
<input type="hidden" name="$id"      value="">
$extra_edit_hidden_inputs
</form>
~ ;

} #------------------------------------------------------------------------------------------

# sub common_min_select_opts {

# my ($field,$table,$valfield,$fid,$required,$addfield,$where) = @_ ;

# $select{$field} = 1 ;

# if ($required) { $required{$field} = 1 ; }

# &db_min_ro($table,'*',$where,'','') ;

# foreach my $id (keys %{$db{$table}}) {
    # if ($fid == $id) { $selected = 'selected="selected"'; } else { $selected = ''; }
        
    # # &common_min_debug("common_min_select_opts - $fid == $id [$valfield] [$table] [$db{$table}{$id}{$valfield}] [[$table][$id][$valfield]]") ;
		
    # if ($db{$table}{$id}{$addfield})    { $disp_addfield = " ($db{$table}{$id}{$addfield})"  ; } else { $disp_addfield = '' ; } 
    # if ($extra{$table}{$id}{$addfield}) { $disp_addfield = " $extra{$table}{$id}{$addfield}" ; } else { $disp_addfield = '' ; } 
    # $opts{$field} .= qq(<option value="$id" $selected>$db{$table}{$id}{$valfield}$disp_addfield</option>) ; 
    # }

# } #------------------------------------------------------------------------------------------

sub common_min_select_opts {

	my ($field,$table,$dispfield,$fid,$required,$addfield,$where,$valfield,$addmore,$select,$order_dropdown) = @_ ;

	$select{$field} = 1 ;

	my $val = '' ;

	# $select = '*' unless $select;

	unless ($select) { $select = '*' ; }

	# &common_min_debug("common_min_select_opts:$field,$table,$dispfield,$fid,$required,$addfield,$where,$valfield,$addmore,$select") ;

	if ($required) { $required{$field} = 1 ; }

	# if ($dispfield){
		# my $orderby = "' $dispfield '" ;
	# } else {
		# my $orderby = ' id DESC ' ; 
	# }

	my $orderby = ($dispfield) ? "' $dispfield '" : ' id DESC' ;

	&db_min_ro($table,$select,$where,$orderby,'') ;
	
	$order_dropdown = $dispfield if $dispfield and not $order_dropdown  ;  
	
	unless ($order_dropdown) {
		foreach my $id (keys %{$db{$table}}) {
			foreach (keys %{$db{$table}{$id}}) {
				$order_dropdown  = $_ ;
				last ;
			}
		last ;
		}
	}

	foreach my $id (sort {$db{$table}{$a}{$order_dropdown} cmp $db{$table}{$b}{$order_dropdown}}keys %{$db{$table}}) {
		
		$selected = ($fid && ($fid eq $db{$table}{$id}{$valfield} || $fid == $id)) ? 'SELECTED' : '' ;
		
		# if ($table eq 'regions') { &common_debug("$selected = ($fid and ($fid eq $db{$table}{$id}{$valfield} || $fid == $id)) ? 'SELECTED' : '' ;") ;  }

		if ($db{$table}{$id}{$addfield} && $db{$table}{$id}{$dispfield} ne $db{$table}{$id}{$addfield}) { $disp_addfield = " [$db{$table}{$id}{$addfield}]" ; } else { $disp_addfield = '' ; } 
		
		my $disp_addmore = '' ;
		
		if ($display_addmore{$table}{$addmore}) {
			my $glyph = '' ;
			if ($db{$table}{$id}{$addmore} == 1) { 
				# $glyph = qq(<i class="glyphicons glyphicons-tick"></i>); 
				$disp_addmore = " - $addmore" ; 
				}		
			}
			
		if ($valfield) { $val = $db{$table}{$id}{$valfield} ; } else { $val = $id ; }
		
		# if ($table eq 'regions') { &common_debug("filter_select_opts = $filter_select_opts{$table}, filter_val = $filter_val{$table}{$val} [$val] valfield=$valfield") ;  }
		
		if ($filter_select_opts{$table} == 1) {
			next unless $filter_val{$table}{$val} == 1 ;
		}
		
		# if ($table eq 'countries') { &common_debug("$field : [$val] $db{$table}{$id}{$dispfield}$disp_addfield$disp_addmore") ;  }
		
		# &common_debug("table=$table, id=$id, val=$val, dispfield=$dispfield [$db{$table}{$id}{$dispfield}]") ;
		
		$opts{$field} .= qq~<option value="$val" $selected $selected_multi{$val}>$db{$table}{$id}{$dispfield}$disp_addfield$disp_addmore</option>~ ; 
	}

} #------------------------------------------------------------------------------------------

sub common_min_box_top {

my ($glyphicon,$title,$bgcolor,$boxicon,$chevronid) = @_ ;

# f5af19
if ($bgcolor)   { $box_header_style = qq~style="background:#$bgcolor;color:#ffffff;"~ ; } else { $box_header_style = '' ; }
if ($chevronid) { $box_chevron_id   = qq~id="$chevronid"~ ; } else { $box_chevron_id = '' ; }

my $box = qq~<div class='row'><div class="box col-md-12"><div class="box-inner"><div class="box-header well" data-original-title="" $box_header_style><h2><i class="glyphicon glyphicon-$glyphicon"></i> $title</h2><div class="box-icon">$boxicon<a href="#" class="btn btn-minimize btn-round btn-default"><i class="glyphicon glyphicon-chevron-up" $box_chevron_id></i></a></div></div><div class="box-content">~ ;

return($box) ;

} #------------------------------------------------------------------------------------------

sub common_min_box_foot {

my $box = qq(</div></div></div></div>) ;

return ($box) ;

} #------------------------------------------------------------------------------------------

sub common_min_extra_crumb {

my ($crumb_url,$crumb_txt) = @_ ;

$crumb_txt =~ s/\-/ /g; my @crumbs = split(/ /,$crumb_txt); my @crumbs_ucf = map ucfirst, @crumbs; $crumb_txt = join(' ',@crumbs_ucf) ;

$insert_crumbs .= qq(<li><a href="$useropts{'folder'}/$crumb_url">$crumb_txt</a></li>) ;

&config_set_breadcrumbs ;

} #------------------------------------------------------------------------------------------

sub common_min_dialog {
   
$dialog{'common'}{'head'} = qq(<!DOCTYPE html>
<html lang="en">
<head>    
    <meta charset="utf-8">
    <title>$useropts{title}</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="$useropts{'css'}/bootstrap-datetimepicker.min.css" rel='stylesheet'>
    <link href="$useropts{'css'}/bootstrap-itv.min.css" rel="stylesheet">    
    <link href="$useropts{'css'}/custom-itv.css" rel="stylesheet">
    <link href="$useropts{'css'}/charisma-app.css" rel="stylesheet">
    <link href="$useropts{'bower_components'}/chosen/chosen.min.css" rel='stylesheet'>
    <link href="$useropts{'bower_components'}/colorbox/example3/colorbox.css" rel='stylesheet'>
    <link href="$useropts{'bower_components'}/responsive-tables/responsive-tables.css" rel='stylesheet'>    
    <link href="$useropts{'css'}/elfinder.min.css" rel='stylesheet'>
    <link href="$useropts{'css'}/elfinder.theme.css" rel='stylesheet'>
    <link href="$useropts{'css'}/jquery.iphone.toggle.css" rel='stylesheet'>
    <script src="$useropts{'bower_components'}/jquery/jquery.min.js"></script>
    $extra_dialog_js
    <!-- The HTML5 shim, for IE6-8 support of HTML5 elements -->
    <!--[if lt IE 9]>
    <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->    
    <style>
        $useropts{'common'}{'css'}
    </style>
</head>
<body>) ;

# table>thead>tr>th, .table>thead>tr>th, table>tbody>tr>th, .table>tbody>tr>th, table>tfoot>tr>th, .table>tfoot>tr>th, table>thead>tr>td, .table>thead>tr>td, table>tbody>tr>td, .table>tbody>tr>td, table>tfoot>tr>td, .table>tfoot>tr>td { border: 1px solid black; }
# .table>caption+thead>tr:first-child>th, .table>colgroup+thead>tr:first-child>th, .table>thead:first-child>tr:first-child>th, .table>caption+thead>tr:first-child>td, .table>colgroup+thead>tr:first-child>td, .table>thead:first-child>tr:first-child>td { border-top: 1px solid black; }
   
$dialog{'common'}{'js'} = qq(<script src="$useropts{'bower_components'}/bootstrap/dist/js/bootstrap.min.js"></script>
<!-- library for cookie management -->
<script src="$useropts{'js'}/jquery.cookie.js"></script>
<!-- calender plugin -->
<script src="$useropts{'bower_components'}/moment/min/moment.min.js"></script>
<!-- data table plugin -->
<script src="$useropts{'js'}/jquery.dataTables.min.js"></script>
<!-- select or dropdown enhancer -->
<script src="$useropts{'bower_components'}/chosen/chosen.jquery.min.js"></script>
<!-- plugin for gallery image view -->
<script src="$useropts{'bower_components'}/colorbox/jquery.colorbox-min.js"></script>
<!-- notification plugin -->
<script src="$useropts{'js'}/jquery.noty.js"></script>
<!-- library for making tables responsive -->
<script src="$useropts{'bower_components'}/responsive-tables/responsive-tables.js"></script>
<!-- tour plugin -->
<script src="$useropts{'bower_components'}/bootstrap-tour/build/js/bootstrap-tour.min.js"></script>
<!-- star rating plugin -->
<script src="$useropts{'js'}/jquery.raty.min.js"></script>
<!-- for iOS style toggle switch -->
<script src="$useropts{'js'}/jquery.iphone.toggle.js"></script>
<!-- autogrowing textarea plugin -->
<script src="$useropts{'js'}/jquery.autogrow-textarea.js"></script>
<!-- multiple file upload plugin -->
<script src="$useropts{'js'}/jquery.uploadify-3.1.min.js"></script>
<!-- history.js for cross-browser state change on ajax -->
<script src="$useropts{'js'}/jquery.history.js"></script>
<!-- application script for Charisma demo -->
<script src="$useropts{'js'}/charisma.js"></script>
<!-- application script for bootstrap dialog customisation -->
<script src="$useropts{'js'}/bootstrap-datetimepicker.min.js"></script>
<script src="$useropts{'js'}/jquery.form-validator.min.js"></script>
<script src="$useropts{'js'}/bootstrap-dialog.js"></script>
) ;
   
} #------------------------------------------------------------------------------------

sub common_min_commify {

local($_) = shift;
1 while s/^(-?\d+)(\d{3})/$1,$2/;
return $_;

} #-------------------------------------------------------------------------------

sub common_min_debug {

my ($msg) = @_ ;

unless ($username eq 'rory')              { return ; }
unless (($testing == 1) or ($debug == 1)) { return ; }

print "\n" . $msg ;

} #------------------------------------------------------------------------------------------

sub common_min_action {

if ($iaction eq 'add'){
    &common_min_add_screen;
    &common_min_screen1;
    }

if ($iaction eq 'list' or $iaction eq 'detailed' or $iaction eq 'summary' or $iaction eq 'accepted' or $iaction eq 'pending' or $iaction eq 'closed' or $iaction eq 'all' or $iaction eq 'open' or $iaction eq 'active') {
    &list_screen ;    
    &common_min_screen3;
    } 

if ($iaction eq 'display'){
    &list_screen ;    
    &common_min_screen3;
    } 

if ($iaction eq 'view'){
    &common_min_load_params ;
    &common_min_view_screen ;    
    &common_min_screen1;
    } 

if ($iaction eq 'edit'){
    &common_min_load_params ;
    &common_min_edit_screen ;    
    &common_min_screen1;
    }

if ($iaction eq 'save'){
    &common_min_load_params ;
    &validate ;
    &insert ;
    &common_min_screen2;
    }
    
if ($iaction eq 'update'){    
    &common_min_load_params ;
    &update ;
    &common_min_screen2;
    }

if ($iaction eq 'delete'){    
    &common_min_load_params ;
    &common_min_delete ;
    &common_min_screen2;    
    }
	
if ($iaction eq 'copy'){    
    &common_min_load_params ;
    &duplicate ; ### Will cause error if using use File::Copy;, do not use sub copy { ..  } ...
    &common_min_edit_screen;
    &common_min_screen1;
    }
    
if ($iaction eq 'search'){
    $srchscr = 1 ;
    &report_screen ;
    }
	
if ($iaction eq 'filter'){
    $srchscr = 1 ;
    &filter_screen ;
    }
	
if ($iaction eq 'report'){
	&common_min_load_params ;
	&report_ifields;
	&list_screen ;    
    &common_min_screen3;
	}

} #------------------------------------------------------------------------------------------

sub common_min_add_screen {

&add_screen;

%col_name = () ;

my $dbid = 'id' ; if ($uniq_id) { $dbid = $uniq_id ; }

&db_min_ro($table,'*',"$dbid > 0 AND $dbid < 10",'','') ;

$page_title  = 'Add' ;

&add_db_fields ;

unless ($double_box_layout) { $print_box_content_rows = &common_min_add_form($table,'save') ; }

} #------------------------------------------------------------------------------------------

sub common_min_error_screen {

	$page_title  = ($iaction eq 'save') ? 'Add' : "Edit $i{id}" ;
	
	foreach (keys %i) { $db{$table}{''}{$_} = $i{$_} ; } # reset other fields after &db_common_ro($table,'*','','id DESC',1) ; ## ALSO CHECK THAT THIS INCLUDES ALL THE FIELD IN ON SCREEN!!!

	&add_screen; # must occur before %col_name set to blank otherwise will include all previous columns

	%col_name = () ;

	&add_db_fields ;

	unless ($double_box_layout) { $print_box_content_rows = &common_min_add_form($table,'save') ; }

} #------------------------------------------------------------------------------------------

sub common_min_edit_screen {

my $dbid = 'id' ; if ($uniq_id) { $dbid = $uniq_id ; }

unless ($i{$dbid}) { $warning = uc "NO $dbid" ; return ; }

&db_min_ro($table,'*',"`$dbid`='$i{$dbid}'",'','') ;

$page_title  = 'Edit' ;
$page_title .= " : $i{$dbid}" ;

&edit_db_fields ;

&edit_screen;

unless ($double_box_layout) { $print_box_content_rows = &common_min_edit_form($i{$dbid},$table,'update') ; }

} #------------------------------------------------------------------------------------------

sub common_min_view_screen {

my $dbid = 'id' ; if ($uniq_id) { $dbid = $uniq_id ; }

unless ($i{$dbid}) { $warning = uc "NO $dbid" ; return ; }

&db_min_ro($table,'*',"`$dbid`='$i{$dbid}'",'','') ;

$page_title  = 'View' ;
$page_title .= " : $i{$dbid}" ;

&view_db_fields ;

&view_screen($i{$dbid});

} #------------------------------------------------------------------------------------------

sub common_min_delete {

my $dbid = 'id' ; if ($uniq_id) { $dbid = $uniq_id ; }

unless ($i{$dbid}) { $error = uc "NO $dbid" ; return ; }

&db_min_delete($table,"$dbid='$i{$dbid}'") ;

} #------------------------------------------------------------------------------------------

sub common_min_search_screen {

&thead;

&common_min_alert_type ;
   
require _blank ;

exit ;

} #------------------------------------------------------------------------------------------

sub common_min_thead {

$print_thead  = qq(<THEAD><TR>) ;

foreach (@sql_col_display) { 
    my $coltitle = $_ ; $coltitle =~ s/\_id//g ; $coltitle =~ s/\_/ /g ; $coltitle = uc $coltitle ; $coltitle = $replace_blank_table_top_right if $coltitle eq '' and $replace_blank_table_top_right ;
	# $custom_column_styles{$_} = qq~style="min-width:120px;"~ unless $custom_column_styles{$_} ;
	$print_thead .= qq(<TH class="dt-center" $custom_column_styles{$_}>$coltitle</TH>);
}
	
if ($add_th_row) { $print_thead .= $add_th_row ; } # used in agent_commission_report.pl and cruiseline_commission_report.pl
	
$print_thead .= qq(</TR></THEAD>) ;
    
} #-------------------------------------------------------------------------------

sub common_min_split_sql_date_time {

my ($datetime) = @_ ;

my ($date,$time) = split(/ /,$datetime) ;

my $dy = substr($date,0,4) ;
my $dm = substr($date,5,2) ;
my $dd = substr($date,8,2) ;

my ($th,$tm,$ts) = split(/:/,$time) ;

return($dy,$dm,$dd,$th,$tm,$ts) ;

} #-------------------------------------------------------------------------------

sub common_min_convert_sql_date_time {

my ($datetime) = @_ ;

my ($date,$time) = split(/ /,$datetime) ;

my $dy = substr($date,0,4) ;
my $dm = substr($date,5,2) ;
my $dd = substr($date,8,2) ;

my ($th,$tm,$ts) = split(/:/,$time) ;

my $return = "$dy$dm$dd" ; if ($time) { $return .= "$th$tm$ts" ; }

return($return) ;

} #-------------------------------------------------------------------------------

sub common_min_restriction_super {

unless ($useropts{super}{lc $username}){ print "This page is restricted!" ; exit ; }

} #-------------------------------------------------------------------------------

sub common_min_add_extras {
	
my $page = "$lcpage\s" ; if ($s{no}) { $page = $lcpage ; }

$box_icon = qq(<div class="box-icon"><a class="btn btn-default btn-round" href="/add-$page" style="padding-top:1px;"><i class="glyphicon glyphicon-plus icon-white"></i></a></div>) ;

$useropts{'common'}{'css'} .= qq(.box-icon a i { top:1px; } .iPhoneCheckContainer { margin:2px 0 2px 0; } .iPhoneCheckContainer label { font-size: 14px; line-height: 14px; padding-top:7px; } .iPhoneCheckLabelOff { padding-right:5px; }  ) ;

# $extra_css .= qq~<link href="$useropts{'css'}/custom-itv.css" rel="stylesheet">~ ;

} #------------------------------------------------------------------------------------------

sub common_min_add_box_icon {

my $page = "$lcpage\s" ; if ($s{no}) { $page = $lcpage ; }

$box_icon = qq(<div class="box-icon"><a class="btn btn-default btn-round" href="/add-$page" style="padding-top:1px;"><i class="glyphicon glyphicon-plus icon-white"></i></a></div>) ;

} #------------------------------------------------------------------------------------------

sub common_min_screen1 {

&thead;

&common_min_alert_type ;

my $page = "$lcpage\s" ; if ($s{no}) { $page = $lcpage ; }
my $ucfpage = "$ucfirstpage\s" ; if ($s{no}) { $ucfpage = $ucfirstpage ; }

unless ($redirtype) { $redirtype = 'list' ; } my $pname = ucfirst $redirtype ;
    
&common_min_extra_crumb("$redirtype-$page","$pname $ucfpage") ;
    
# &common_min_extra_crumb("list-$page","List $ucfpage") ;
   
if ($cust_cols or $double_box_layout) {
	require _blank_3 ;
	}
else
	{
	require _blank ;
	}

exit ;

} #------------------------------------------------------------------------------------------

sub common_min_screen2 {

my $page = "$lcpage\s" ; if ($s{no}) { $page = $lcpage ; }

unless ($redirsave) { $redirsave = 'list' ; }
# unless ($redirsave) { $redirsave = 'edit' ; }

if ($i{id} and $iaction ne 'delete') {
	&common_min_edit_screen ;    
    &common_min_screen1;
	# $input_hidden_id = qq~<input type="hidden" name="id" value="$i{id}">~ ; 
}
	
# # if ($i{id} && $redirsave eq 'edit') {
# if ($redirsave eq 'edit') {
	# $input_hidden_id = qq~<input type="hidden" name="id" value="$i{id}">~ ; 
# }

# my $doc_ready_jquery = qq~\$(document).ready(function(){ \$('#$redirsave-form').submit(); })~ unless $username eq 'rory' ;

my $form_name = $redirsave . 'form' ;
	
&common_min_alert_type ;

print <<ENDOFTEXT;
<!DOCTYPE html>
<html lang="en">
<head>    
    <meta charset="utf-8">
    <title>$useropts{title}</title>
    <script src="$useropts{'bower_components'}/jquery/jquery.min.js"></script>   
</head>
<body>
<script>
	\$(document).ready(function(){ \$('#$redirsave-form').submit(); })
</script>
</body>

<form role="form" name="$form_name" id="$redirsave-form" method="post" action="$redirsave-$page">
<input type="hidden" name="iaction"          value="$redirsave">
<input type="hidden" name="isaved"           value="$success">
$input_hidden_id
</form>

</html>
ENDOFTEXT
#

exit ;

} #------------------------------------------------------------------------------------------

sub common_min_screen3 {

&thead;

if ($isaved) { $alert = &common_min_alert('success',"$ucpage $isaved!",'ok') ; } else { &common_min_alert_type ; }

my $page = "$lcpage\s" ; if ($s{no}) { $page = $lcpage ; }

&common_min_table('id',$page,'list') ;

# &common_min_extra_crumb("manage-$lcpage\s","Manage $ucfirstpage\s") ;

require _blank ;

exit ;

} #------------------------------------------------------------------------------------------

sub common_min_opts {

my ($whichone,$field,$deselect) = @_ ;

if ($whichone eq 'leave_type') {
	$required{$field} = 1 ;
	$select{$field} = 1 ;
	$opts{$field} = qq(<option value="annual" $selected{$field}{'annual'}>Annual</option>
						 <option value="compassionate" $selected{$field}{'compassionate'}>Compassionate</option>
						 <option value="maternity" $selected{$field}{'maternity'}>Maternity</option>
						 <option value="sick" $selected{$field}{'sick'}>Sick</option>
						 ) ;
	}

my $ucfirst_field = ucfirst $field ;
	
if ($deselect){
	$trigger_jquery_raw .= qq(\$("#select$ucfirst_field").chosen({ allow_single_deselect:true });) ;
	}
	
} #------------------------------------------------------------------------------------------

sub common_min_dialog_load_screen {

&dialog_common ;

print <<ENDOFTEXT;
$dialog{'common'}{'head'}
    
<div id="content" class="col-lg-12 col-sm-12">
    <!-- content starts -->  
    
    $alert
    
    <div class="row">
		<div class="box col-md-12">
			<div class="box-inner">
				<div class="box-content">
					$print_box_content_rows
				</div>
			</div>
		</div>
	</div><!--/row-->    
   
</div><!--/fluid-row-->
    
</div><!--/.fluid-container-->

<!-- external javascript -->

<script src="$useropts{'bower_components'}/chosen/chosen.jquery.min.js"></script>
<script src="$useropts{'bower_components'}/bootstrap/dist/js/bootstrap.min.js"></script>
<script src="$useropts{'js'}/jquery.form-validator.min.js"></script>

<script>
\$.validate();

$trigger_jquery_raw

</script>

</body>
</html>
ENDOFTEXT
#

exit;

} #------------------------------------------------------------------------------------------

sub common_min_dialog_save_screen {

print <<ENDOFTEXT;
<!DOCTYPE html>
<html lang="en">
<head>    
    <meta charset="utf-8">
    <title>ITV Admin</title>
    <script src="$useropts{'bower_components'}/jquery/jquery.min.js"></script>   
</head>
<body>
<script>

\$(function () {
	$trigger_jquery_raw
});

</script>
</body>
</html>
ENDOFTEXT
#

exit ;

} #------------------------------------------------------------------------------------------

sub common_min_table_validate_date_inputs_jquery {
	
	my $date_value_ids_str = join(",",@date_value_ids) ;
	
	return unless $date_value_ids_str ;

	$trigger_jquery_raw .= qq~
		
		function isLeapYear(year) {
		  return (year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0);
		}
		
		function isValidDate(dateString) {
			
			let count_comp = 0 ; let found_non_int = 0 ;
			
			for (let key in dateString.split('-').map(Number)) {
				count_comp++ ; 
				if (dateString.split('-').map(Number)[key] != parseInt(dateString.split('-').map(Number)[key])) {
					found_non_int = 1 ; 
				}
			}
			
			if (count_comp != 3 || found_non_int) {
				return false
			} else {
				return true ;
			}

		}
			
		\$("$date_value_ids_str").change( function () {
			
			// console.log("date_value_ids_str : $date_value_ids_str") ;
			
			const dateInput = \$(this).val() ;
			
			if (!dateInput) {
				return ; 
			}
			
			let result = "" ;
	
			if (!isValidDate(dateInput)) {
				result = "Invalid Date Format!!!";
			}

			let [inputYear,inputMonth,inputDay] = dateInput.split("-").map(Number);

			if (inputYear > 2100 || inputYear < 2000) {
				if (result) {
					result = result + "<br>";
				}
				result = result + "Invalid Year!!!";
			}
			
			if (inputMonth < 1 || inputMonth > 12) {
				if (result) {
					result = result + "<br>";
				}
				result = result + "Invalid Month!!!";
			}

			const daysInMonth = [31, (isLeapYear(inputYear) ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
			
			if (inputDay < 1 || inputDay > daysInMonth[inputMonth - 1]) {
				if (result) {
					result = result + "<br>";
				}
				result = result + "Invalid Day!!!";
			}	
			
			if (result) {
				
				noty({text:result,layout:"center",type:"error",timeout:3000}) ;
				
			} else {
				
				let correct_day_or_month_format = 0 ;
				
				if (inputMonth > 0 && inputMonth < 10) {
					inputMonth = inputMonth.toString() ;
					inputMonth = inputMonth.padStart(2,'0');
					 correct_day_or_month_format = 1 ;
				}
				if (inputDay > 0 && inputDay < 10) {
					inputDay = inputDay.toString() ;
					inputDay = inputDay.padStart(2,'0');
					 correct_day_or_month_format = 1 ;
				}
				if (correct_day_or_month_format) {
					\$("#"+this.id).val(inputYear+"-"+inputMonth+"-"+inputDay) ; 				
				}				
			} 
			
		}) ; 
	
	~ ; 
} #------------------------------------------------------------------------------------------

sub common_min_table_update_checkbox_col_default {
	
	my $default_javascript_by_id_str = join ",", @default_javascript_by_id ; 

	if ($default_javascript_by_id_str) {
		$trigger_jquery_raw .= qq~
			let all_def_vals = {$default_javascript_by_id_str} ;
		~ ;
	}
	
} #------------------------------------------------------------------------------------------

sub common_min_table_update_checkbox_col_jquery {
	
	my ($nr_of_characters_in_id_to_determine_row,$content_id,$update_fields_short_1,$update_fields_short_2) = @_ ;
	
	$content_id = "content" unless $content_id ;
	
	$nr_of_characters_in_id_to_determine_row = 1 unless $nr_of_characters_in_id_to_determine_row ;
	
	my $jquery_on_change_1 = "" ;
	my $jquery_on_change_2 = "" ;
	
	if ($update_fields_short_1 || $update_fields_short_2) {
		
		## NEW WAY
		
		$jquery_on_change_1 = qq~\$("#$content_id").on("change","$update_fields_short_1", function (event) {~ if $update_fields_short_1 ; # EXAMPLE: [id^='selectFixture_check_'],[id^='selectStatus_']
		$jquery_on_change_2 = qq~\$("#$content_id").on("changeDate","$update_fields_short_2", function (event) {~ if $update_fields_short_2 ;
		
	} else {
		
		## OLD WAY 
		
		my @all_update_field_ids_1 = () ; my @all_update_field_ids_2 = () ; 
		
		foreach my $fields (@all_update_field_ids) {
			foreach (split(/\,/,$fields)) {
				if ($_ =~ /#datepicker/ || $_ =~ /#datetimepicker/ || $_ =~ /#timepicker/) {
					push @all_update_field_ids_2,$_ ;
				} else {
					push @all_update_field_ids_1,$_ ;
				}
			}
		}
		
		# my $all_update_field_ids_str = join "," , @all_update_field_ids ;
		my $all_update_field_ids_str_1 = join "," , @all_update_field_ids_1 ;
		my $all_update_field_ids_str_2 = join "," , @all_update_field_ids_2 ;	
		
		$jquery_on_change_1 = qq~\$("$all_update_field_ids_str_1").change( function (event) {~   if $all_update_field_ids_str_1	 ;
		$jquery_on_change_2 = qq~\$("$all_update_field_ids_str_2").on('changeDate',function() {~ if $all_update_field_ids_str_2  ;
		
	}
	
	chop $multi_select_ids if $multi_select_ids ; 
	
	if ($jquery_on_change_1 || $jquery_on_change_2) {
		
		$trigger_jquery_raw .= qq~ 
		
			function get_row_nr (id,nr_of_characters_in_id_to_determine_row) {
				
				// console.log("get_row_nr : "+id) ; 
				// console.log("nr_of_characters_in_id_to_determine_row : "+nr_of_characters_in_id_to_determine_row) ; 
				
				let lastIndex = id.lastIndexOf('_');
				lastIndex = parseInt(lastIndex) ;
				let row_nr_1 = id.substring(1+lastIndex); 
				
				if (!nr_of_characters_in_id_to_determine_row || nr_of_characters_in_id_to_determine_row == 1) {
					return row_nr_1 ; 
				}
				
				let id_2 = id.substring(0,lastIndex); 
				lastIndex = id_2.lastIndexOf('_');
				lastIndex = parseInt(lastIndex) ;
				let row_nr_2 = id_2.substring(1+lastIndex); 
				
				if (nr_of_characters_in_id_to_determine_row == 2) {
					return row_nr_2+"_"+row_nr_1 ; 
				}
				
				let id_3 = id_2.substring(0,lastIndex); 
				lastIndex = id_3.lastIndexOf('_');
				lastIndex = parseInt(lastIndex) ;
				let row_nr_3 = id_3.substring(1+lastIndex); 	
			
				if (nr_of_characters_in_id_to_determine_row == 3) {
					// console.log("return : "+row_nr_3+"_"+row_nr_2+"_"+row_nr_1) ; 
					return row_nr_3+"_"+row_nr_2+"_"+row_nr_1 ; 
				}
				
			}	
			
			function check_whether_to_update (changed_id) {

				let row_nr = get_row_nr(changed_id) ;
				
				$trigger_jquery_raw_default_alt

				let nr_of_characters_in_id_to_determine_row = "$nr_of_characters_in_id_to_determine_row" ;
				
				if (row_nr == "row") {
					row_nr = "add_row" ; 
				} else if (nr_of_characters_in_id_to_determine_row == "2") {
					// Nothing Yet
					row_nr = get_row_nr(changed_id,2) ; 
				} else if (nr_of_characters_in_id_to_determine_row == "3") {
					row_nr = get_row_nr(changed_id,3) ; 
				}
				
				let def_vals = all_def_vals[row_nr] ;
				
				
				console.log("changed_id val : "+\$("#"+changed_id).val()) ; 
				
				let tick_update = 0 ; 
				
				let multi_select_ids = {$multi_select_ids} ; 
				
				for (const id in def_vals) {
					
					console.log("def_vals : "+\$("#"+id).val()) ;  
					
					if ((\$("#"+id).attr('type') == 'radio' && (\$("#"+id).is(":checked") !== (def_vals[id] === "true"))) || (id.includes('input[name') && def_vals[id] != \$(id).val())) {
						tick_update = 1 ; 
					} else if (id.includes('datepicker') || id.includes('timepicker')) {
						console.log("1 tick_update : "+tick_update) ; 
						console.log("11 def_vals : "+def_vals[id]) ; 
						console.log("22 def_vals : "+\$("#"+id).data("date")) ; 
						if (def_vals[id] != \$("#"+id).data("date")) {
							tick_update = 1 ; 
						}
						console.log("2 tick_update : "+tick_update) ; 
					} else if (\$("#"+id).attr('type') == 'checkbox') {
						if ((\$("#"+id).is(":checked") !== (def_vals[id] === "true")) || (!\$("#"+id).is(":checked") !== (def_vals[id] === "false"))) {
							tick_update = 1 ; 
						}
					} else if (!multi_select_ids[id] && !id.includes('input[name')  && \$("#"+id).attr('type') != 'radio') {
						
						let sel_val = \$("#"+id).val() ;
						if (sel_val == null) {
							sel_val = '' ;
						}
						if (sel_val != def_vals[id]) {
							tick_update = 1 ; 
						}
						
					} else if (multi_select_ids[id]) {
						
						let select_multi = \$("#"+id).chosen().val() ;
						if (select_multi == null) { select_multi = "" ; }
						if (select_multi != def_vals[id]) {
							tick_update = 1 ; 
						}
					}
					
					
					
				}
				
				if (tick_update && !\$("#checkboxUpdate_"+row_nr).is(":checked")) {
					\$("#checkboxUpdate_"+row_nr).prop("checked",true) ; 
				} else if (!tick_update && \$("#checkboxUpdate_"+row_nr).is(":checked")) {
					\$("#checkboxUpdate_"+row_nr).prop("checked",false) ; 
				}				
			}
		~ ;
		
		# $trigger_jquery_raw .= ($ids_short) ? qq~
			# \$("#$content_id").on("change","$update_fields_short_1", function (event) { // EXAMPLE: [id^='selectFixture_check_'],[id^='selectStatus_']
				# event.preventDefault(); 
				# check_whether_to_update(this.id); 
			# });
		# ~ : qq~ 	
			# // \$("$all_update_field_ids_str_1").change( function (event) {
			# $jquery_on_change_1	
				# event.preventDefault();
				# $trigger_jquery_raw_on_change_1
				# check_whether_to_update(this.id) ; 
			# }) ;

		# ~ ;	
		
		$trigger_jquery_raw .= qq~
			$jquery_on_change_1
				event.preventDefault(); 
				check_whether_to_update(this.id); 
			});
		~ if $jquery_on_change_1 ;

		$trigger_jquery_raw .= qq~ 
			$jquery_on_change_2
				$trigger_jquery_raw_on_change_2
				check_whether_to_update(this.id) ;
			}) ; 
		
		~ if $jquery_on_change_2 && !$no_dates_in_table ;
		# unless $no_dates_in_table ;
	} 
	
} #------------------------------------------------------------------------------------------

sub common_min_table_update_checkbox_col {
	
	my ($nr_of_characters_in_id_to_determine_row,$content_id,$ids_short_1,$ids_short_2) = @_ ;
	
	&common_min_table_update_checkbox_col_default ;
	
	&common_min_table_update_checkbox_col_jquery($nr_of_characters_in_id_to_determine_row,$content_id,$ids_short_1,$ids_short_2) ;
	
} #------------------------------------------------------------------------------------------

sub common_min_table_checkbox {
	
	my ($field_id,$default_val,$only_display_val,$table_row,$table_col,$background_color,$default_val_is_saved) = @_ ;
	
	if (!$only_display_val) {
		my $ucfirstfield = ($ucfirstfield{$field_id}) ? $ucfirstfield{$field_id} : "checkbox" . ucfirst $field_id ;
		$val = qq~<input name="$field_id" data-no-uniform="true" type="checkbox" id="$ucfirstfield" value="1" tabindex="$tindex">~ ;
	} else {
		$val = qq~<i class="glyphicon glyphicon-remove"></i>~ ;
	}
	$val_min = '' ;
	return $val ; 
	
} #------------------------------------------------------------------------------------------

sub common_min_table_textarea {
	
	my ($field_id,$default_val,$only_display_val,$table_row,$table_col,$background_color,$default_val_is_saved,$col_name,$table_id,$hide_textarea) = @_ ;
	
	$val = '' ; $val_min = 'MUSTBEBLANK' ;
	
	$table_id = "itv-table" unless $table_id ; 
	
	if (!$only_display_val && $table_col) {
		$found_editable = 1 ; 
		# my $print_field = ($preferred_placeholder{$field_id}) ? $preferred_placeholder{$field_id} : &common_min_print_field($field_id) ; 
		my $print_field = ($preferred_placeholder{$field_id}) ? $preferred_placeholder{$field_id} : ($preferred_title{$field_id}) ? $preferred_title{$field_id} : &common_min_print_field($field_id) ; 
		
		$tindex++ ;

		$val_min = $default_val if $default_val ;
		
		my $ucfirstfield = ($ucfirstfield{$field_id}) ? $ucfirstfield{$field_id} : "textarea" . ucfirst $field_id ;
		
		# my $style = ($default_val && $default_val =~ /\n/) ? qq~style="height:100%;"~ : qq~~ ;
		
		# $default_val =~ s/\\n/\n/g ;
		
		$hide_textarea = ($hide_textarea) ? qq~display:none;~ : qq~~ ;
		
		$val = qq~
			<div class="control-group">
				<div class='controls'> 
					<textarea type='text' class='form-control' name='$field_id' id='$ucfirstfield' placeholder='$print_field' value="$defualt" tabindex="$tindex" $readonly{$field} style="width:100%;$hide_textarea">$default_val</textarea>
				</div>
			</div>
		~ ;	
	
		# $default_val =~ s/\n/\\n/g ;
		$default_val =~ s/[\n\r]+/\\n/g ;  
		
		if ($default_val && $default_val_is_saved) {
			$background_color = "#424949" unless $background_color ;
			$table_with_default_ids{$background_color} .= "#$table_id tr:eq($table_row) td:nth-last-child($table_col)," ;
			# $trigger_jquery .= qq~ \$("#$ucfirstfield").css("width","100%") ;~ ;	
			$default_javascript .= qq~"$ucfirstfield":"$default_val",~ ; 
		} elsif ($default_val && !$default_val_is_saved) {
			$default_javascript .= qq~"$ucfirstfield":"$default_val",~ ; 
		} else {
			$default_javascript .= qq~"$ucfirstfield":"",~ ; 
		}
		
		# box-sizing:border-box;overflow:hidden;		
		# box-sizing:border-box;overflow: hidden;
		# oninput="adjustHeight(this)" row="1"
		
		# $trigger_jquery .= qq~
			# window.onload = function() {
				# var textarea = document.getElementById('$ucfirstfield');
				# adjustHeight(textarea,'$ucfirstfield');
			# };
		# ~ ; 
		
		# $trigger_jquery .= qq~ 
			# \$("#$ucfirstfield").css("width","100%") ;
		# ~ ;	
		
		#$trigger_jquery_raw .= qq~ 
		#	\$('#$ucfirstfield').on('input', function() {
		#		// this.style.height = 'auto' ;
        #        // this.style.height = (this.clientHeight) + 'px'; 
        #    }) ;
        #    \$('#$ucfirstfield').trigger('input');
		# ~ ;

	} elsif ($default_val) {
		$val = $default_val ; 
		$val_min = $val ;
		$val =~ s/\n/\<br>/g ; 	
	} 
	
	$val_min =~ s/\\n/ /g ; 
	
	return $val ;
	
} #------------------------------------------------------------------------------------------

sub common_min_table_input {
	
	my ($field_id,$default_val,$only_display_val,$table_row,$table_col,$background_color,$default_val_is_saved,$col_name,$table_id,$hide_input_field) = @_ ;
	
	$val = '' ; $val_min = 'MUSTBEBLANK' ;
	
	if (!$only_display_val) { 
		$found_editable = 1 ;  
		# my $print_field = ($preferred_placeholder{$field_id}) ? $preferred_placeholder{$field_id} : &common_min_print_field($field_id) ; 
		my $print_field = ($preferred_placeholder{$field_id}) ? $preferred_placeholder{$field_id} : ($preferred_title{$field_id}) ? $preferred_title{$field_id} : &common_min_print_field($field_id) ; 
		$tindex++ ; 
		
		my $ucfirstfield = ($ucfirstfield{$field_id}) ? $ucfirstfield{$field_id} : "input" . ucfirst $field_id ;
		
		$hide_input_field = qq~display:none;~ if $hide_input_field ;
		
		$val = qq~<div class="control-group"><div class='controls'><input type='text' class='form-control' name='$field_id' id='$ucfirstfield' placeholder='$print_field' value="$default_val" $readonly{$field_id} tabindex="$tindex" style="width:100%;$hide_input_field">
		</div></div>~ ;	
						
		# $trigger_jquery .= qq~ \$("#$ucfirstfield").css("width","100%") ;~ ;	
		if ($default_val && $default_val_is_saved) {
			$background_color = "#424949" unless $background_color ;
			$table_with_default_ids{$background_color} .= "#itv-table tr:eq($table_row) td:nth-last-child($table_col)," ;			
			$default_javascript .= qq~"$ucfirstfield":"$default_val",~ ; 
			$val_min = $default_val  ; 				
		} else {
			$val_min = ''  ; 		
			$default_javascript .= qq~"$ucfirstfield":"",~ ; 			
		}
	} else {
		$val = $default_val ;
		$val_min = $val ;
	}	
	
	return $val ;
	
} #------------------------------------------------------------------------------------------

sub common_min_table_select {
	
	my ($field_id,$default_val,$only_display_val,$table_row,$table_col,$background_color,$default_val_is_saved,$col_name,$table_id,$tooltip_1_text) = @_ ;

	if ($multiple{$field_id}) { $setmultiple = 'multiple' ; } else { $setmultiple = '' ; }
	
	$table_id = "itv-table" unless $table_id ; 
	
	$val = '' ; 
	# $val_min = 'MUSTBEBLANK' ;
	
	# if (!$only_display_val && $table_col) {
	if (!$only_display_val) {
		
		$found_editable = 1 ; 
		
		my $ucfirstfield = ($ucfirstfield{$field_id}) ? $ucfirstfield{$field_id} : "select" . ucfirst $field_id ;
		
		my $print_field = ($preferred_placeholder{$field_id}) ? $preferred_placeholder{$field_id} : ($preferred_title{$field_id}) ? "Select $preferred_title{$field_id}" : "Select " . &common_min_print_field($field_id) ; 

		push @all_select_ids,"#$ucfirstfield" if $ucfirstfield ;
		if ($multiple{$field_id}) {
			push @all_multi_select_ids,"#$ucfirstfield" if $ucfirstfield ;
			$multi_select_ids .= qq~"$ucfirstfield":"1",~ if $ucfirstfield ;
			push @dropdown_multi_chosen_ids,"#$ucfirstfield\_chosen" if $ucfirstfield ;
			
		}

		# $table_select_rows{$table_row} .= qq~#itv-table tr:eq($table_row) td:nth-last-child($table_col),~  ;
		
		# $trigger_jquery_raw .= qq~
			# \$("#$ucfirstfield").chosen({ allow_single_deselect:true }) ; \$("#itv-table tr:eq($table_row) td:nth-last-child($table_col)").css("z-index","$table_row") ; 
		# ~ ;		
		
		# my $tooltip_1 = qq~~ ;
		# if ($tooltip_1_text) {
			# $tooltip_1 = qq~title data-toggle='tooltip' data-placement='top' data-original-title='$tooltip_1_text'~ ;
		# }
			
		if ($tooltip_1_text) {
			$add_tooltip{$tooltip_1_text} .= "#$ucfirstfield\_chosen," ;
		}
 
		if (!$col_name) {
			
			$val = qq~
				<select class="form-control" name="$field_id" id="$ucfirstfield" $setmultiple data-placeholder="$print_field" data-rel="chosen" $tooltip_1>
					<option value=""></option>
					$opts{$field_id}                                      
				</select>
			~ ;
			
		} else {
			$val = qq~
				<select class="form-control" name="$field_id" id="$ucfirstfield" $setmultiple data-placeholder="$print_field" data-rel="chosen" $tooltip_1>                                    
				</select>
			~ ;	
			$opts{$col_name} = $opts{$field_id} unless $opts{$col_name} ;
			$col_name_select_ids{$col_name} .= "#$ucfirstfield," ;
			
		}

		if ($default_val) {
			unless ($val_min) {
				$val_min = $default_val ;
				$val_min =~ s/\_/ \[/g ; 
				$val_min .= qq~]~ if $val_min =~ /\[/;				
			}
			
			push @selects_with_default_ids,"#$ucfirstfield" if $ucfirstfield ;
			
			# foreach (split(/\,/,$default_val)) {
				$selects_by_def_val{$default_val} .= "#$ucfirstfield," if $ucfirstfield ;
			# }
			
			
			# $trigger_jquery_raw .= qq~
				# \$("#$ucfirstfield").val("$default_val") ;
			# ~ ;	
			
			# if ($default_val_is_saved) {
			if ($default_val_is_saved && $table_col) {
				$background_color = "#424949" unless $background_color ;
				$table_with_default_ids{$background_color} .= "#$table_id tr:eq($table_row) td:nth-last-child($table_col)," ;
				$default_javascript .= qq~"$ucfirstfield":"$default_val",~ ; 
			} elsif ($default_val_is_saved && !$table_col) {
				$default_javascript .= qq~"$ucfirstfield":"$default_val",~ ;
			} else {
				$default_javascript .= qq~"$ucfirstfield":"",~ ; 
			}
							
		} else {
			$default_javascript .= qq~"$ucfirstfield":"",~ ;
		}
		if ($ucfirstfield) { push @dropdown_chosen_ids,"#$ucfirstfield\_chosen" unless $multiple{$field_id} ; }

	} elsif ($default_val) {
		$val = $default_val ;
		$val_min = $default_val ;
		$val_min =~ s/\_/ \[/g ; 
		$val_min .= qq~]~ if $val_min =~ /\[/;		
		
		# $val =~ s/\_/\[/g ; 
		$val =~ s/\_//g ; 
		# $val .= qq~]~ ;
		# $val_min = $val unless $val_min ;
		$table_with_default_ids{$background_color} .= "#$table_id tr:eq($table_row) td:nth-last-child($table_col)," if $background_color && $background_color ne "#424949" ;
	} 	
	
	return $val ;
	
} #------------------------------------------------------------------------------------------

sub common_min_table_textarea_jquery {
	
	# $trigger_jquery .= qq~
		# function adjustHeight(textarea,id) {
			
			# textarea.style.height = 'auto'; // Reset height to auto to get the scroll height
			# // textarea.style.height = textarea.scrollHeight + 'px'; // Set the height to the scroll height
			
			# \$("#"+id).css("height",textarea.scrollHeight+"px") ; 
			
			# console.log("scrollHeight : "+textarea.scrollHeight) ; 
			# console.log("textarea : "+textarea) ; 
			# console.log("id : "+id) ; 
			
		# }
	# ~ ;
	
} #------------------------------------------------------------------------------------------

sub common_min_table_select_jquery {
	
	my ($columns_with_selects,$table_id) = @_ ; 
	
	&common_min_table_select_fixed_width_jquery($columns_with_selects) ; 
	
	&common_min_table_select_prevent_dropdown_overlap_jquery($table_id) ;
	
	&common_min_table_select_default_values_jquery ;
	
	&common_min_table_multi_select_jquery ;
	
} #------------------------------------------------------------------------------------------

sub common_min_table_select_fixed_width_jquery {
	
	my ($columns_with_selects) = @_ ; 
	
	return unless $columns_with_selects ;	
	
	$trigger_jquery_raw .= qq~
		\$("$columns_with_selects").css("position","relative") ;
		\$("$columns_with_selects").css("text-align","center") ;
	~ ;
	
	my $dropdown_chosen_ids_str = join(',',@dropdown_chosen_ids) ; 		
	my $dropdown_multi_chosen_ids_str = join(',',@dropdown_multi_chosen_ids) ; 		

	$trigger_jquery .= qq~ 
		\$("$dropdown_multi_chosen_ids_str").css("width","100%") ;~ if $dropdown_multi_chosen_ids_str ;		
	
	return unless $dropdown_chosen_ids_str ;
	
	$trigger_jquery .= qq~ 
		\$("$dropdown_chosen_ids_str").css("position","absolute") ;~ ;	
	$trigger_jquery .= qq~ 
		\$("$dropdown_chosen_ids_str").css("width","90%") ;~ ;	
	$trigger_jquery .= qq~ 
		\$("$dropdown_chosen_ids_str").css("left","5%") ;~ ;	
	$trigger_jquery .= qq~ 
		\$("$dropdown_chosen_ids_str").css("top","50%") ;~ ;	
	$trigger_jquery .= qq~ 
		\$("$dropdown_chosen_ids_str").css("transform","translateY(-50%)") ;~ ;		
	
	foreach (keys %add_tooltip) {
		chop $add_tooltip{$_} ;

		$trigger_jquery .= qq~ 
			\$("$add_tooltip{$_}").attr({
				title : '',
				'data-original-title': '$_',
				'data-toggle': 'tooltip',
				'data-placement': 'top'
			});
		~ ;
	}
	
} #------------------------------------------------------------------------------------------

sub common_min_table_select_prevent_dropdown_overlap_jquery_1 {
	
	my ($table_id) = @_ ; 

	$table_id = "$useropts{table_id}" unless $table_id ;
	
	$trigger_jquery .= qq~
	
		function prevent_drop_down_overlap (loading_page) {
			
			const table_ = \$('#$table_id').DataTable()._('tr') ;
			// var table_ =\$("#$table_id").find('tr') ;
			let row_nr = 1 ; 
			let z_index = 1 ; 
			
			for (let i = table_.length; i >= 1 ; i--) { 
				
				if (loading_page) {
					$hide_other_column_for_logistics_report
					$hide_columns_by_default
				}
				
				if (!\$("#$table_id tr:eq("+i+") td:nth-last-child(1)").find('input').length && !\$("#$table_id tr:eq("+i+") td:nth-last-child(2)").find('input').length) {
					continue ; 
				}
				
				let columns = table_[row_nr - 1];
				
				for (let col_nr = 1; col_nr <= columns.length ; col_nr++) {
					
					if (!\$("#$table_id tr:eq("+i+") td:nth-last-child("+col_nr+")").find('select').length) {
						continue ; 
					}
					\$("#$table_id tr:eq("+i+") td:nth-last-child("+col_nr+")").css("z-index",z_index) ;
					z_index++ ;
				}
				row_nr++ ;
			}	
			// table_ = "" ; 
		}

		\$('#$table_id th').on('click', function() {
			 prevent_drop_down_overlap(0) ; 
		});
		
		// \$("#select_temp_calibrators_2_length").css("display","none");
		// \$("#select_temp_calibrators_2_filter").css("display","none");
		// \$("#select_temp_calibrators_2_info").css("display","none");
		// \$("#select_temp_calibrators_2_paginate").css("display","none");
		
	~ ; 
	
} #------------------------------------------------------------------------------------------

sub common_min_table_select_prevent_dropdown_overlap_jquery_2 {
	
	$trigger_jquery .= qq~
		prevent_drop_down_overlap(1) ; 
	~ ;
	
} #------------------------------------------------------------------------------------------

sub common_min_table_select_prevent_dropdown_overlap_jquery {
	
	my ($table_id) = @_ ; 
	
	&common_min_table_select_prevent_dropdown_overlap_jquery_1($table_id) ; 
	&common_min_table_select_prevent_dropdown_overlap_jquery_2 ; 
	
} #------------------------------------------------------------------------------------------

sub common_min_table_select_default_values_jquery {
		
	foreach my $col_name (keys %col_name_select_ids) {
		chop $col_name_select_ids{$col_name} if $col_name_select_ids{$col_name} ;
		$opts{$col_name} =~ s/\"/\'/g ; 
		$opts{$col_name} =~ s/\' >/\'>/g ; 
		$opts{$col_name} =~ s/\'  >/\'>/g ; 
		$trigger_jquery_raw .= qq~
			\$("$col_name_select_ids{$col_name}").html("<option value=''></option>$opts{$col_name}").trigger("chosen:updated") ;
		~ ;
	}
	
	my $all_select_ids_str = join(",",@all_select_ids) ; 
	
	$trigger_jquery_raw .= qq~\$("$all_select_ids_str").chosen({ allow_single_deselect:true }) ;~ ;
	
	# $trigger_jquery_raw .= qq~
		# \$("$all_select_ids_str").on("change", function () {	
			# console.log("FOCUS!!!!") ; 
			# event.preventDefault() ;	
		# }) ;
	# ~ ;
	
	# $trigger_jquery_raw .= qq~
		# // \$("$all_select_ids_str").change( function () {
			# // console.log("preventDefault") ; 
			# // event.preventDefault() ;
			# // let scrollContainer = \$('#itv-table_wrapper') ;
			# // let scrollPosition = scrollContainer.scrollLeft() ;
			# // console.log("scrollPosition : "+scrollPosition) ; 
			# // scrollContainer.scrollLeft(scrollPosition) ;
			
		# // }) ;
	# ~ ;

	foreach (keys %table_with_default_ids) {
		chop $table_with_default_ids{$_} if $table_with_default_ids{$_} ;
		$trigger_jquery_raw .= qq~
			\$("$table_with_default_ids{$_}").css("background-color","$_") ; 
		~ ;
	}
	
	foreach my $def_val (keys %selects_by_def_val) {
		chop $selects_by_def_val{$def_val} if $selects_by_def_val{$def_val} ;
		my $def_val2 = $def_val ;
		if ($def_val2 =~ /,/) {
			$def_val2 =~ s/\,/\','/g ;			
			$def_val2 = qq~['$def_val2']~ ; 
			$trigger_jquery_raw .= qq~
				\$("$selects_by_def_val{$def_val}").val($def_val2).trigger("chosen:updated") ;
			~ ;	
		} else {
			
			# foreach (split(/\,/,$selects_by_def_val{$def_val})) {
				# $trigger_jquery_raw .= qq~
					# \$("$_").val("$def_val2").trigger("chosen:updated")  ;
				# ~ ;		
			# }
			$trigger_jquery_raw .= qq~
				\$("$selects_by_def_val{$def_val}").val("$def_val2").trigger("chosen:updated")  ;
			~ ;	
		}
			
	}
	
	# my $selects_with_default_ids_str = join(',',@selects_with_default_ids) ; 		
	# $trigger_jquery_raw .= qq~ 
		# \$("$selects_with_default_ids_str").trigger("chosen:updated") ; 
	# ~ ;	
	
} #------------------------------------------------------------------------------------------

sub common_min_table_multi_select_jquery {
	
	# my $all_multi_select_ids_str = join(",",@all_multi_select_ids) ; 
	
	# foreach (@all_multi_select_ids) {
		# $trigger_jquery_raw .= qq~set_row_height("$_") ; ~ ;
	# }
	
	# $trigger_jquery_raw .= qq~

		# function set_row_height (multi_id) {
			
			# let row_nr = get_row_nr(multi_id) ; 
			# let numOptions = \$(multi_id).find('option:selected').length;
			# var optionHeight = 25;
			# var padding = 40 ; 
			# let numOptions2 = Math.floor(numOptions/3) ;
			
			# var newHeight = numOptions * optionHeight + padding;
			# console.log("set_row_height : "+multi_id) ; 
			
			# if (numOptions == '3' || numOptions2 == '0') {
				# newHeight = 0 ;
			# }
			# \$("#"+row_nr).height(newHeight);			
		# }

		# \$("$all_multi_select_ids_str").change( function() {
			 # set_row_height("#"+this.id) ;
		# });
	
	# ~ ; 
	
} #------------------------------------------------------------------------------------------

sub common_min_table_datepicker {

	my ($field_id,$default_val,$only_display_val,$table_row,$table_col,$background_color,$default_val_is_saved,$col_name,$table_id,$icon_color,$tooltip_text,$tooltip_1_text,$tooltip_2_text) = @_ ;
	
	if ($default_val eq '0000-00-00') { $default_val = '' ; }

	if (!$only_display_val) { 
		
		my $ucfirstfield = ($ucfirstfield{$field_id}) ? $ucfirstfield{$field_id} : "datepicker" . ucfirst $field_id ;
		
		$tindex++ ;
		
		my $tooltip_1 = qq~~ ; my $tooltip_2 = qq~~ ;
		
		if ($tooltip_1_text) {
			$tooltip_1 = qq~title data-toggle='tooltip' data-placement='top' data-original-title='$tooltip_1_text'~ ;
		}
		
		if ($default_val && $add_datepicker_tooltip{$field_id}) {
			$tooltip_2 = qq~title data-toggle='tooltip' data-placement='top' data-original-title='$default_val'~ ;
		} elsif ($tooltip_2_text) {
			$tooltip_2 = qq~title data-toggle='tooltip' data-placement='top' data-original-title='$tooltip_2_text'~ ;
		}
	
		if (!$default_val) {  $default_val = "$now_year-$now_mm-$now_dd" ; } ; 
		
		$icon_color = (!$icon_color) ? 'black' : $icon_color ;
		
		$val = qq~                            
			<div style="margin:2px 0 2px 0;" class="input-group controls input-append date" data-date="$default_val" id="$ucfirstfield" $tooltip_1>
				<input style="margin:0;" class='form-control' name="$field_id" value="$default_val" placeholder='ccyy-mm-dd' tabindex='$tindex' $readonly{$field_id} READONLY >
				<span class="input-group-addon add-on">
					<span style="cursor:pointer;color:$icon_color;" class="glyphicon glyphicon-calendar" $tooltip_2 ></span>
				</span>
			</div>
		~ ;
						
		if ($default_val_is_saved) {
			$background_color = "#424949" unless $background_color ;
			$table_with_default_ids{$background_color} .= "#itv-table tr:eq($table_row) td:nth-last-child($table_col)," if $table_row && $table_col ;			
			$default_javascript .= qq~"$ucfirstfield":"$default_val",~ if $ucfirstfield ;
			$val_min = $default_val ;
		} else {
			$default_javascript .= qq~"$ucfirstfield":"",~ if $ucfirstfield ;
			$val_min = '' ;			
		}  	
		push @all_datepicker_ids,"#$ucfirstfield" if $ucfirstfield ;
		push @all_datepicker_names,"input[name='$field_id']" if $field_id ;
		
	} else {
		$val = $default_val ; 
		$val_min = $val ;
	}

# &common_min_form_datepicker_jquery($ucfirst_field);
	
	return ($val) ;
    
} #------------------------------------------------------------------------------------------

sub common_min_table_datepicker_jquery {
	
	our $all_datepicker_ids_str = join(",",@all_datepicker_ids) ; 
	
	$trigger_jquery_raw .= qq~
		\$('$all_datepicker_ids_str').datepicker({format: "yyyy-mm-dd"}).on('changeDate', function (ev) {
			\$('.datepicker').hide();
			\$("input[name='"+this.id.substring(10).toLowerCase()+"']").val(\$("#"+this.id).data("date")) ; 
		}) ; 
	~ ;

} #------------------------------------------------------------------------------------------

sub common_min_table_timepicker {
       
	my ($field_id,$default_val,$only_display_val,$table_row,$table_col,$background_color,$default_val_is_saved,$col_name,$table_id,$icon_color,$tooltip_1_text,$tooltip_2_text) = @_ ;

    $tindex++;
	$val = '' ; $val_min = '' ;
	
	if (!$only_display_val) { 
	
		my $ucfirstfield = ($ucfirstfield{$field_id}) ? $ucfirstfield{$field_id} : "timepicker" . ucfirst $field_id ;
		
		if (!$default_val) {  $default_val = "$now_hour:$now_min:$now_sec" ; } ; 
		
		my $tooltip_1 = qq~~ ; my $tooltip_2 = qq~~ ;
		
		if ($tooltip_1_text) {
			$tooltip_1 = qq~title data-toggle='tooltip' data-placement='top' data-original-title='$tooltip_1_text'~ ;
		}
		
		if ($default_val && $add_timepicker_tooltip{$field_id}) {
			$tooltip_2 = qq~title data-toggle='tooltip' data-placement='top' data-original-title='$default_val'~ ;
		} elsif ($tooltip_2_text) {
			$tooltip_2 = qq~title data-toggle='tooltip' data-placement='top' data-original-title='$tooltip_2_text'~ ;
		}
		
		$icon_color = (!$icon_color) ? 'black' : $icon_color ;

		# <span class="input-group-addon add-on">
				# <span style="cursor:pointer;" class="glyphicon glyphicon-calendar"></span>
		# </span>
	
		# <i data-time-icon="icon-time" data-date-icon="icon-calendar" $tooltip ></i>
			
		$val = qq~
			<div style="margin: 2px 0 2px 0;$custom_style{$field_id}" class="input-group controls input-append date" data-date="$default_val" id="$ucfirstfield" $tooltip_1>
				<input style="margin: 0;" data-format="hh:mm:ss" class="form-control" name="$field_id" READONLY value="$default_val" tabindex="$tindex">
				<span class="input-group-addon add-on">
					<span style="cursor:pointer;color:$icon_color;" class="glyphicon glyphicon-time" $tooltip_2 ></span>				
				</span>
			</div>
		~ ;
		
		if ($default_val && $default_val_is_saved) {
			$background_color = "#424949" unless $background_color ;
			$table_with_default_ids{$background_color} .= "#itv-table tr:eq($table_row) td:nth-last-child($table_col)," if $table_row && $table_col ;			
			$default_javascript .= qq~"$ucfirstfield":"$default_val",~ if $ucfirstfield && $default_val ;	
			$val_min = $default_val ;
		} else {
			$default_javascript .= qq~"$ucfirstfield":"$default_val",~ if $ucfirstfield ;
			$val_min = '' ;			
		}  
		push @all_timepicker_ids,"#$ucfirstfield" if $ucfirstfield ;
		push @all_timepicker_names,"input[name='$field_id']" if $field_id ;		
		# $trigger_jquery_raw .= qq(\$('#timepicker$ucfirst_field').datetimepicker({language:'pt-BR',pickDate:false});) ;
	} else {
		$val     = $default_val ; 
		$val_min = $val ;
	}
    return $val ;
	
} #------------------------------------------------------------------------------------------

sub common_min_table_timepicker_jquery {
	
	my ($ids_short) = @_ ; 
	
	if ($ids_short) {
		$trigger_jquery_raw .= qq~\$("$ids_short").datetimepicker({language:'pt-BR',pickDate:false});~ ;
		return ; 
	}
	
	our $all_timepicker_ids = join(",",@all_timepicker_ids) ;
	
	$trigger_jquery_raw .= qq~\$('$all_timepicker_ids').datetimepicker({language:'pt-BR',pickDate:false});~ ;
	
} #------------------------------------------------------------------------------------------

sub common_min_get_event_quote_button {
	
	my ($event_id,$table,$page_url_part) = @_ ; 
	
	$table = "event_quotes" unless $table ; 

	return unless $event_id ;
	
	my $class = 'info' ; my $style = '' ; my $does_not_exist = 0 ;				
	if ($db{$table}{$event_id}{demo_id}) {
		$style  = 'style="background-color:rgb(255,64,255);border-color:rgb(255,64,255);"'; # pink
		$tt_txt = 'Demo' ;
		if ($db{$table}{$event_id}{quote_completed}) {
			$tt_txt = 'Completed ' . $tt_txt ;
		} elsif ($db{$table}{$event_id}{quote_accepted}) {
			$tt_txt = 'Accepted ' . $tt_txt ;
		} elsif ($db{$table}{$event_id}{quote_rejected}) {
			$tt_txt = 'Rejected ' . $tt_txt ;
		} elsif ($db{$table}{$event_id}{quote_cancelled}) {
			$tt_txt = 'Cancelled ' . $tt_txt ;
		} elsif ($db{$table}{$event_id}{quote_created}) {
			$tt_txt = 'Pending, Created by School Manager ' . $tt_txt ;
		} elsif ($db{$table}{$event_id}{quote_pending}) {
			$tt_txt = 'Pending ' . $tt_txt ;
		}  
	} elsif ($analytics_events{$event_id}) {
		$style  = 'style="background-color:rgb(255,129,0);border-color:rgb(255,129,0);"'; # pink
		$tt_txt = 'Analytics Event' ;	
		if ($db{$table}{$event_id}{quote_completed}) {
			$tt_txt = 'Completed ' . $tt_txt ;
		} elsif ($db{$table}{$event_id}{quote_accepted}) {
			$tt_txt = 'Accepted ' . $tt_txt ;
		} elsif ($db{$table}{$event_id}{quote_rejected}) {
			$tt_txt = 'Rejected ' . $tt_txt ;
		} elsif ($db{$table}{$event_id}{quote_cancelled}) {
			$tt_txt = 'Cancelled ' . $tt_txt ;
		} elsif ($db{$table}{$event_id}{quote_created}) {
			$tt_txt = 'Pending, Created by School Manager ' . $tt_txt ;
		} elsif ($db{$table}{$event_id}{quote_pending}) {
			$tt_txt = 'Pending ' . $tt_txt ;
		}  		
	} elsif ($db{$table}{$event_id}{quote_completed} || $db{$table}{$event_id}{event_completed} || $quote_completed) {
		$class  = 'success' ; # green
		$tt_txt = 'Completed' ;
	} elsif ($db{$table}{$event_id}{quote_accepted} || $db{$table}{$event_id}{event_completed} || $quote_accepted) {
		$class  = 'info' ; # blue
		$tt_txt = 'Accepted' ;
		my @fixed_system_overall_statuses = split(/\;/,$db{$table}{$event_id}{fixed_system_overall_status}) ;
		my @event_system_ids              = split(/\;/,$db{$table}{$event_id}{event_system_id_multiple}) ;
		my $event_sys_cnt                 = 0 ; my $event_sys_test_status = 0 ; my $got_some_fixed = 0 ; my $fixed_status_color = '164473' ; # Dark Blue
		foreach my $event_id (@event_system_ids) {			
			if ($db{event_systems}{$event_id}{system_type} eq 'fixed') {
				$tt_txt = 'Accepted and tests show All Clear for the Fixed Systems' if $tt_txt eq 'Accepted' ;
				$got_some_fixed = 1 ;
				if ($fixed_system_overall_statuses[$event_sys_cnt] == 3) { # Not Clear
					$fixed_status_color = '5d09d3' ; # Dark Purple
					$tt_txt = 'Accepted but Not Clear on some Fixed Systems' ;   
				} elsif ($fixed_system_overall_statuses[$event_sys_cnt] == 2) { # Sound Issue
					$fixed_status_color = '5d092c' ;
					$tt_txt = 'Accepted but Sound Issue found on some Fixed Systems' ;
				}
			}
			$event_sys_cnt++ ;
		}
		if ($got_some_fixed) { $style  = "style='background-color:#$fixed_status_color;border-color:#$fixed_status_color'";  }
	} elsif ($db{$table}{$event_id}{quote_rejected} || $db{$table}{$event_id}{event_rejected} || $quote_rejected) {
		$style  = 'style="background-color:#ca0ad3;border-color:#ca0ad3"'; # purple
		$tt_txt = 'Rejected' ;
	} elsif ($db{$table}{$event_id}{quote_cancelled} || $db{$table}{$event_id}{event_cancelled} || $quote_cancelled) {
		$class  = 'danger' ; # red
		$tt_txt = 'Cancelled' ;
	} elsif ($db{$table}{$event_id}{quote_created} || $db{$table}{$event_id}{event_created} || $quote_created) { # create by schools_manager
		$style  = 'style="background-color:#f6e305;border-color:#f6e305"'; # yellow
		$tt_txt = 'Pending, Created by School Manager' ;
	} elsif ($db{$table}{$event_id}{quote_pending} || $db{$table}{$event_id}{event_pending} || $quote_pending) {
		$class  = 'warning' ; # orange
		$tt_txt = ($db{$table}{$event_id}{sssadmin_quote_nr}) ? 'Pending [Created by SSS platform]' : 'Pending' ;
	} elsif (!$db{$table}{$event_id}{id}) {
		$style  = 'style="background-color:black;border-color:black"'; 
		$tt_txt = 'Does not Exist!!!' ;
		$does_not_exist = 1 ; 
	}

	my $tt = qq~data-toggle="tooltip" data-placement="right" data-title="$tt_txt"~;
	# ($glod_user_level < 3 && !$is_operator)
	
	my $glod_user_level_eff = ($glod_user_level == 2 && $is_operator) ? 3 : $glod_user_level ;
	
	my $href = ($page_url_part) ? qq~javascript:editMinItem('$event_id','$page_url_part');~ : qq~javascript:editMinItem('$event_id');~ ;
		
	my $event_quote_btn = ($glod_user_level_eff < 3 || $db{$table}{$event_id}{quote_cancelled} || $does_not_exist) ? qq~<a class="btn btn-$class btn-xs" href="javascript:void(0);" $style $tt>$event_id</a>~ : qq~<a class="btn btn-$class btn-xs" href="$href" $style $tt>$event_id</a>~ ;
	
	 # || ($db{$table}{$event_id}{quote_completed} && $glod_user_level <= 3)
	
	return ($event_quote_btn) ;
	
} #------------------------------------------------------------------------------------------

sub common_min_get_quote_button {
	
	my ($quote_id,$table) = @_ ; 
	
	$table = "quotes" unless $table ; 

	return unless $quote_id ;
	
	my $class = 'info' ; my $style = '' ; my $tt_txt = '' ;

	if ($db{$table}{$quote_id}{quote_cancelled}) {
		$class = 'danger' ;
		$tt_txt = 'Cancelled' ;
	} elsif ($db{$table}{$quote_id}{quote_accepted}) {
		$class = 'success' ; 
		$tt_txt = 'Accepted' ;
	} elsif ($quote_expiry and $quote_expiry<"$now_year$now_mm$now_dd") { ### $quote_expiry must be defined
		$class = 'danger' ;
		$tt_txt = 'Expired' ;
	} elsif ($db{$table}{$quote_id}{quote_pending}) {
		$class = 'warning' ; 
		$tt_txt = 'Pending' ;
	} 
	
	my $tt = qq~data-toggle="tooltip" data-placement="right" data-title="$tt_txt"~;

	my $quote_btn = qq~<a class="btn btn-$class btn-xs" href="javascript:editMinItem('$quote_id');" $style $tt>$db{$table}{$quote_id}{quote_nr}</a>~ ; 
	
	return ($quote_btn) ;
	
} #------------------------------------------------------------------------------------------

# sub common_min_date_as_string {
	
	# my ($date) = @_ ; 

	# my ($yr,$mon,$day) = split("-",$date) ; ## our @months = ("","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec") ; 

	# return ("$day-$months[$mon]-$yr") ;
	
# } #------------------------------------------------------------------------------------------

sub common_min_date_as_string {
	
	my ($date) = @_ ; 

	return if $date !~ /(\d+)/g ;

	my ($yr,$mon,$day) = split("-",substr($date,0,10)) ; ## our @months = ("","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec") ; 

	if (substr($date,11,5)) {
		return ("$day-$months[$mon]-$yr \@ " . substr($date,11,5)) ;
	} else {
		return ("$day-$months[$mon]-$yr") ;
	}
	
} #------------------------------------------------------------------------------------------

sub greater_date_time {
	
	local ($date_time_1,$date_time_2) = @_ ;
	
	return Time::Piece->strptime($date_time_1,'%Y-%m-%d %H:%M:%S') <= Time::Piece->strptime($date_time_2,'%Y-%m-%d %H:%M:%S') ;	
		
} #------------------------------------------------------------------------------------------

sub common_min_greater_date_time {
	
	local ($date_time_1,$date_time_2) = @_ ;
	
	return Time::Piece->strptime($date_time_1,'%Y-%m-%d %H:%M:%S') <= Time::Piece->strptime($date_time_2,'%Y-%m-%d %H:%M:%S') ;	
		
} #------------------------------------------------------------------------------------------

sub common_min_greater_date {
	
	local ($date_1,$date_2) = @_ ;
	
	return Time::Piece->strptime($date_1,'%Y-%m-%d') <= Time::Piece->strptime($date_2,'%Y-%m-%d') ;	
		
} #------------------------------------------------------------------------------------------

sub common_min_calc_time_diff {
	
	local ($date_time_1,$date_time_2) = @_ ;
	
	return "blank_date" if !$date_time_1 || !$date_time_2 ; 
	
	return (Time::Piece->strptime($date_time_2,'%Y-%m-%d %H:%M:%S') - Time::Piece->strptime($date_time_1,'%Y-%m-%d %H:%M:%S')) ;	
		
} #------------------------------------------------------------------------------------------

sub common_min_calc_day_diff {
	
	local ($date_1,$date_2) = @_ ;
	
	return 1 + abs(Time::Piece->strptime(substr($date_2,0,10),'%Y-%m-%d') - Time::Piece->strptime(substr($date_1,0,10),'%Y-%m-%d'))->days ;	
		
} #------------------------------------------------------------------------------------------

sub common_min_delete_files {
	
	my ($dir,$file_name) = @_  ;
	
	return if !$dir || ($dir && not -d $dir) || ($file_name && not -f "$dir/$file_name") ;
	
	my @files = () ; my $dh ;
	if ($file_name) {
		push @files,$file_name ;
	} else {
		opendir($dh, $dir) or die "Cannot open directory: $!";
		@files = grep { !/^\.\.?$/ } readdir($dh);			
	}
	foreach my $file (@files) {
		my $file_path = "$dir/$file" ;
		if (-f $file_path) {
			unlink ("$file_path") or print "\n unable to unlink - $file_path : $!";
		}
	}
	if (!$file_name) {
		closedir($dh);
	}
	
} #------------------------------------------------------------------------------------------

sub common_min_copy_files {
	
	# # # # use File::Copy; Make Sure THis is called ;
	
	my ($dir_from,$dir_to,$file_name) = @_ ;
	
	return if !$dir_from || !$dir_to || not -d $dir_from || ($file_name && not -f "$dir_from/$file_name");
	
	mkdir $dir_to if not -d $dir_to; 
	
	my @files = () ; my $dh ;
	
	if ($file_name) {
		push @files,$file_name ;
	} else {
		opendir($dh,$dir_from) or die "Can't open $dir_from: $!";
		my $file_counter = 0 ; 
		@files = readdir($dh);
	}

	foreach my $file (@files) {
		next if $file =~ /^\.\.?$/ ;
		my $src_file = "$dir_from/$file";
		my $dst_file = "$dir_to/$file";
		if (-f $src_file) {
			copy("$src_file","$dst_file") or die "File cannot be copied." ;
		}
	}
	
	if (!$file_name) {
		closedir($dh);
	}
	
} #------------------------------------------------------------------------------------------

1;
