sub build_workings_costings_forms_jquery {
	
	my $description_options_costing = $opts{"description_costing_event_1"} ;
	$description_options_costing =~ s/\SELECTED//g ;
	$description_options_costing =~ s/\"/\'/g ;
	
	my $description_options_workings = $opts{"description_workings_event_1"} ;
	$description_options_workings =~ s/\SELECTED//g ;
	$description_options_workings =~ s/\"/\'/g ;	
	
	our $operator_options = $opts{"operator_workings_event_1"} ;
	$operator_options =~ s/\SELECTED//g ;
	$operator_options =~ s/\"/\'/g ;
	
	my $btn_ids_string = join(",",@btn_ids) ; 

	my $excl_field_ids_string			= join(",",@excl_field_ids) ; 
	my $description_field_ids_string 	= join(",",@description_field_ids) ; 
	my $operator_field_ids_string 		= join(",",@operator_field_ids) ; 
	my $vat_field_ids_string 			= join(",",@vat_field_ids) ;
	my $qty_field_ids_string 			= join(",",@qty_field_ids) ;
	my $amount_usd_field_ids_string 	= join(",",@amount_usd_field_ids) ;
 
	$trigger_jquery_raw .= qq~
		// function update_qty_in_quote_tab_when_changing_nr_of_full_or_half_days
		function get_tab (id) {
			let tab = "" ; 
			if (id.includes("workings_event")) {
				tab = "workings_event" ;
			} else if (id.includes("costing_event")) {
				tab = "costing_event" ;
			}			
			return tab ;
		}
		
		function changed_vat (id) {
			let tab = get_tab (id) ;
			let row_nr = get_row_nr(id) ;
			calc_totals(row_nr,tab) ;
		}
		
		function changed_others (id) {
			 
			let row_nr = get_row_nr(id) ;
			let firstIndex = id.indexOf('_');
			let col = id.substring(0,firstIndex); 
			let tab = get_tab (id) ;
			if (col == "inputAmount") {
				let inp = parseFloat(\$("#"+id).val()) ;
				inp = parseFloat(inp) ;
				\$("#"+id).val(inp.toFixed(2)) ;
			} else if (\$("#selectDescription_"+tab+"_"+row_nr).val() && \$("#selectDescription_"+tab+"_"+row_nr).val() != 14 && \$("#selectOperator_"+tab+"_"+row_nr).val()) {
				// \$("#selectOperator_"+tab+"_"+row_nr).val("") ;
				// \$("#selectOperator_"+tab+"_"+row_nr).trigger("chosen:updated") ;
			} 
			// else if (col == "selectOperator" && \$("#selectDescription"+tab+"_"+row_nr).val() != 14) {
				
			// }
			
			if (col == "selectOperator") { // || (col == "selectDescription" && \$("#selectDescription_"+tab+"_"+row_nr).val(
				return ;
			}
			
			calc_totals(row_nr,tab) ;			
		}
		
		// \$("$vat_field_ids_string").change(function() {
		\$("#content").on("change","[id^='checkboxVat_']", function () {
			const id = this.id ; 
			changed_vat(id) ; 
		});

		// \$("$excl_field_ids_string,$description_field_ids_string,$qty_field_ids_string,$amount_usd_field_ids_string,$operator_field_ids_string").change(function() {					
		\$("#content").on("change","[id^='checkboxExcl_'],[id^='selectDescription_'],[id^='inputQty_'],[id^='inputAmount_usd_'],[id^='selectOperator_workings_event_']", function () {
			const id = this.id ; 

			if ((id.includes("inputQty_") || id.includes("inputAmount_usd_")) && id.includes("costing_event")) {

				let row_nr = get_row_nr(id) ;

				let f_t = (\$("input[name='added_full_day_cost_item_"+row_nr+"']").val()) ? "full" : (\$("input[name='added_half_day_cost_item_"+row_nr+"']").val()) ? "half" : "" ;
				
				if (f_t) {
				
					let col = (id.includes("inputQty_")) ? "-2" : "-1" ;
					
					let full_half_add = \$("input[name='added_"+f_t+"_day_cost_item_"+row_nr+"']").val() ;
					
					let get_e_cost_id = (id.includes("inputAmount_usd_")) ? get_row_nr(\$("input[name='added_"+f_t+"_day_cost_item_"+row_nr+"']").val()) : "" ;
	 
					if (((id.includes("inputQty_") && \$(this).val() == nr_days[f_t]) || (id.includes("inputAmount_usd_") && \$(this).val() == default_cost_item_rates[f_t+","+get_e_cost_id])) && full_half_add && full_half_add.includes(col)) {
						full_half_add = full_half_add.replace(col+"_","") ;
						\$("input[name='added_"+f_t+"_day_cost_item_"+row_nr+"']").val(full_half_add) ;
					} else if (full_half_add && !full_half_add.includes(col)) {
						\$("input[name='added_"+f_t+"_day_cost_item_"+row_nr+"']").val(col+"_"+full_half_add) ;
					}  
				}
				
			} else if (id.includes("selectDescription_")) {

				let this_id = id ;  
			
				let row_nr = get_row_nr(id) ;
				
				let tab = (this_id.includes("_costing_event_")) ? "costing_event" : (this_id.includes("_workings_event_")) ? "workings_event" : "" ;
				
				let full_day_exists = nr_days["full"] ; let half_day_exists = nr_days["half"] ; 

				let already_loaded_full_day = (\$("#"+this_id).val()) ? \$("input[name='full_"+tab+"_"+\$("#"+this_id).val()+"']").val() : "" ; 
				let already_loaded_half_day = (\$("#"+this_id).val()) ? \$("input[name='half_"+tab+"_"+\$("#"+this_id).val()+"']").val() : "" ; 
				
				already_loaded_full_day = (typeof already_loaded_full_day == 'undefined') ? "" : 1 ; 
				already_loaded_half_day = (typeof already_loaded_half_day == 'undefined') ? "" : 1 ;
				
				if (already_loaded_full_day && already_loaded_half_day) {
					return ;
				}
				
				if (already_loaded_full_day == 1) {
					full_day_exists = 0 ; 
				}
				
				if (already_loaded_half_day == 1) {
					half_day_exists = 0 ; 
				}
				
				let date_from = \$("input[name='date_from']").val() ;
				
				date_from = date_from.substring(0,10) ;
				
				let full_half_day = 0 ; 
				
				if (full_day_exists != 0) {
					if ((typeof \$("input[name='full_"+tab+"_"+\$("#"+this_id).val()+"']").val() == 'undefined' || \$("input[name='full_"+tab+"_"+\$("#"+this_id).val()+"']").val()) && \$("#"+this_id).val()) {
						\$("#content").append("<input style='display:none;' value='1' name='full_"+tab+"_"+\$("#"+this_id).val()+"'>") ;
					}
					full_half_day = 1 ;
				// } else if (half_day_exists || (already_loaded_full_day && !half_day_exists && !already_loaded_half_day)) {
				} else if (half_day_exists != 0) {
					if ((typeof \$("input[name='half_"+tab+"_"+\$("#"+this_id).val()+"']").val() == 'undefined' || !\$("input[name='half_"+tab+"_"+\$("#"+this_id).val()+"']").val()) && \$("#"+this_id).val()) {
						\$("#content").append("<input style='display:none;' value='1' name='half_"+tab+"_"+\$("#"+this_id).val()+"'>") ;
					}
					full_half_day = 2 ;
				}

				let rate_ = "" ;
				
				if (default_cost_item_rates["full,"+\$("#"+this_id).val()] && full_half_day == 1) {
					rate_ = default_cost_item_rates["full,"+\$("#"+this_id).val()] ;
				} else if (default_cost_item_rates["half,"+\$("#"+this_id).val()] && full_half_day == 2) {
					rate_ = default_cost_item_rates["half,"+\$("#"+this_id).val()] ;
				}
				if (rate_ == "") {
					\$("input[name='added_full_day_cost_item_"+row_nr+"']").remove() ; 
					\$("input[name='added_half_day_cost_item_"+row_nr+"']").remove() ; 
					return ;
				}
				full_half_day = (full_half_day == 1) ? "full" : (full_half_day == 2) ? "half" : "" ;
				
				\$("input[name='qty_"+tab+"_"+row_nr+"']").val(nr_days[full_half_day]) ;
				\$("input[name='amount_usd_"+tab+"_"+row_nr+"']").val(rate_) ;
				\$("input[name='amount_ttl_"+tab+"_"+row_nr+"']").val((rate_*nr_days[full_half_day]).toFixed(2)) ;
				if (!\$("input[name='added_"+full_half_day+"_day_cost_item_"+row_nr+"']").val()) {
					\$("#"+tab+"_"+row_nr).append("<input value='-3_"+\$(this).val()+"' name='added_"+full_half_day+"_day_cost_item_"+row_nr+"' $display_hidden_input>") ;
				} else {
					\$("input[name='added_"+full_half_day+"_day_cost_item_"+row_nr+"']").val("-3_"+\$(this).val()) ;
				}
				
				calc_totals(row_nr,tab) ;

					
			}
			
			changed_others(id) ; 
		}); 
	~ ;
		
	$trigger_jquery_raw .= qq~	
		
		\$("#content").on("click","[id^='btn_workings_event_'],[id^='btn_costing_event_']", function () {
		// \$("$btn_ids_string").click(function () {
			const id = this.id ; 
			let tab = get_tab (id) ;
			
			console.log("id 	: "+id) ; 
			console.log("tab : "+tab) ;  
			
			add_quote_expenses_row_all(id,tab) ;
		});
		
		function add_quote_expenses_row_all (id,tab) {
			
			let row_nr = get_row_nr(id) ;
			\$("#"+tab+"_"+row_nr).show();
			let row_html = "" ; 
			if (tab == "workings_event") {
				row_html = add_quote_expenses_row(row_nr,tab,"$description_options_workings","$operator_options") ; 
			} else if (tab == "costing_event") {
				row_html = add_quote_expenses_row(row_nr,tab,"$description_options_costing","") ; 
			}
			\$("#"+tab+"_"+row_nr).html(row_html);
			// \$("#$suffix\_"+row_nr).trigger("chosen:updated") ; 
			
			\$("#selectDescription_"+tab+"_"+row_nr).chosen({ allow_single_deselect:true }) ;
			
			if (tab == "workings_event") {
				\$("#selectOperator_workings_event_"+row_nr).chosen({ allow_single_deselect:true });
				\$("#inputRemarks_workings_event_"+row_nr).css("text-align","") ;
				\$("#inputSupplier_workings_event_"+row_nr).css("text-align","") ;
				\$("#inputRef_nr_workings_event_"+row_nr).css("text-align","") ;
			} 
			
			\$("#btn_"+tab+"_"+row_nr).hide();
			\$("#btn_row_"+tab+"_"+row_nr).hide();
			var v = row_nr + 1 ;
			
			// \$("#btn_$suffix\_"+v).css("padding-left","16px") ; 
			\$("#btn_"+tab+"_"+v).html("<i class='glyphicon glyphicon-plus blue'></i>") ; 
			\$("#btn_"+tab+"_"+v).show();
			\$("#btn_row_"+tab+"_"+v).show() ;
			\$("#2_"+tab+"_"+row_nr).show() ;
 		
			\$("#checkboxExcl_"+tab+"_"+row_nr).prop('checked',false) ;
			
			// \$("#"+tab+"_"+row_nr).on("change","#checkboxVat_"+tab+"_"+row_nr+"", function() {			
				// const id = this.id ; 
				// changed_vat(id) ;
			// });
			
			// \$("#"+tab+"_"+row_nr).on("change","#checkboxExcl_"+tab+"_"+row_nr+",#selectDescription_"+tab+"_"+row_nr+",#selectOperator_"+tab+"_"+row_nr+",#inputQty_"+tab+"_"+row_nr+",#inputAmount_usd_"+tab+"_"+row_nr+"", function() {
				// const id = this.id ; 
				// changed_others(id) ; 
			// });			
			
		}
		
		~ if $btn_ids_string ; 
	
		$trigger_jquery_raw .= qq~	
		
		\$("#selectCurrency").change(function () {
			
		}) ;
		
		\$("input[name='roe']").change(function () {
			
			let roe = \$(this).val() ; 
			if (roe < 0 || !roe) {
				roe = 1 ; 
			}
			\$(this).val(parseFloat(roe).toFixed(2)) ; 
			
			var sub_total = 0 ; var vat_total = 0 ;  var sub_total_quote = 0 ; var vat_total_quote = 0 ;  
			
			[sub_total,vat_total] 				= add_up_totals("workings_event",50,roe,1) ; 
			[sub_total_quote,vat_total_quote] 	= add_up_totals("costing_event",30,roe,1) ; 
			
			// // // \$("input[name='income']").val((sub_total_quote+vat_total_quote).toLocaleString('en-US', {minimumFractionDigits: 2, maximumFractionDigits: 2})) ;
			// // // \$("input[name='expenses']").val((sub_total+vat_total).toLocaleString('en-US', {minimumFractionDigits: 2, maximumFractionDigits: 2})) ;
			\$("input[name='income']").val((sub_total_quote+vat_total_quote).toFixed(2)) ;
			\$("input[name='expenses']").val((sub_total+vat_total).toFixed(2)) ;
			update_overall_totals(sub_total,vat_total,"workings_event") ; 
			update_overall_totals(sub_total_quote,vat_total_quote,"costing_event") ; 
			
		}) ;
		
		function add_up_totals (tab,nr_of_rows,roe,recalc_totals) {
			
			let sub_total = 0 ; let vat_total = 0 ;
			for (let i=1; i<=nr_of_rows; i++) {
				if (!\$("#"+tab+"_"+i).html()) { break ; }
				if (\$("#selectDescription_"+tab+"_"+i).val()) {
					if (\$("#checkboxExcl_"+tab+"_"+i).is(":checked")) { continue ; } 
					let ttl = 0 ; 
					if (recalc_totals) {
						ttl = parseFloat(\$("#inputAmount_usd_"+tab+"_"+i).val()) * parseFloat(\$("#inputQty_"+tab+"_"+i).val()) * parseFloat(roe) ;
					} else {
						ttl = parseFloat(\$("#inputAmount_ttl_"+tab+"_"+i).val().replace(/,/g, '')) 
					}
					// // // // \$("#inputAmount_ttl_"+tab+"_"+i).val(ttl.toLocaleString('en-US', {minimumFractionDigits: 2, maximumFractionDigits: 2})) ;
					\$("#inputAmount_ttl_"+tab+"_"+i).val(ttl.toFixed(2)) ;
					if (!\$("#inputAmount_ttl_"+tab+"_"+i).val() || \$("#inputAmount_ttl_"+tab+"_"+i).val() == "NaN") { 
						\$("#inputAmount_ttl_"+tab+"_"+i).val("0.00") ;
						ttl = 0 ;
					}
					sub_total += ttl ;
					if (\$("#checkboxVat_"+tab+"_"+i).is(":checked")) {
						vat_total += ttl*0.15 ;
					}	
				} else if (!\$("#checkboxExcl_"+tab+"_"+i).is(":checked")) {
					\$("#checkboxExcl_"+tab+"_"+i).prop('checked',true) ;
				}
			}
			return [sub_total,vat_total] ;
		}

		// function getNum(val) {
			
			// if (isNaN(val)) {
				// return 0;
			 // }
			// return val;
		// }
			
		function calc_totals (row_cnt,tab) {
			
			var qty  = \$("#inputQty_"+tab+"_"+row_cnt) ;	
			var usd  = \$("#inputAmount_usd_"+tab+"_"+row_cnt) ;
			var conv = \$("#inputAmount_ttl_"+tab+"_"+row_cnt) ;
			var excl = \$("#checkboxExcl_"+tab+"_"+row_cnt) ;
			var item = \$("#selectDescription_"+tab+"_"+row_cnt) ;
						
			if (!usd.val() && !conv.val() && !usd.val()) {
				// console.log("skip calc_totals_workings") ;
				if (!item.val()) {
					excl.prop("checked",true) ;
				}
				return ; 
			}

			var vat  = \$("#checkboxVat_"+tab+"_"+row_cnt) ;
		
			if (!item.val()) { 
			 
				excl.prop("checked",true) ;
				vat.prop("checked",true) ; 
				qty.val("")	; 
				usd.val("")	;
				conv.val("");
				if (tab == "workings_event") {
					var rem  = \$("#inputRemarks_"+tab+"_"+row_cnt) ;
					var sup  = \$("#inputSupplier_"+tab+"_"+row_cnt) ;	
					var ref  = \$("#inputRef_nr_"+tab+"_"+row_cnt) ;	
					var op   = \$("#selectOperator_"+tab+"_"+row_cnt) ;		
					rem.val("")	;
					sup.val("")	;
					ref.val("")	;
					op.val("")	;
					op.trigger("chosen:updated") ;
				}
			// } else if (excl.is(":checked") && item.val() && conv.val() == "0.00") {
			} else if (excl.is(":checked")) {
				excl.prop("checked",false) ;
			}
			
			if (!\$("#inputRoe").val()) { \$("#inputRoe").val(1) ; }
			
			let roe = parseFloat(\$("#inputRoe").val()) ;

			let ttl = usd.val() * qty.val() * roe ;
			
			// // // conv.val(ttl.toLocaleString('en-US', {minimumFractionDigits: 2, maximumFractionDigits: 2})) ;
			conv.val(ttl.toFixed(2)) ;
			
			let sub_total = 0 ; let vat_total = 0 ;  
			
			let nr_of_rows = 50 ; 
			if (tab == "costing_event") {
				nr_of_rows = 30 ; 
			}
			
			[sub_total,vat_total] = add_up_totals (tab,nr_of_rows,roe,0) ; 
			
			if (tab == "workings_event") {
				// // // \$("input[name='expenses']").val((sub_total+vat_total).toLocaleString('en-US', {minimumFractionDigits: 2, maximumFractionDigits: 2})) ;
				\$("input[name='expenses']").val((sub_total+vat_total).toFixed(2)) ;
				update_overall_totals(sub_total,vat_total,tab) ; 
			} else {
				// // // \$("input[name='income']").val((sub_total+vat_total).toLocaleString('en-US', {minimumFractionDigits: 2, maximumFractionDigits: 2})) ; 
				\$("input[name='income']").val((sub_total+vat_total).toFixed(2)) ; 
				update_overall_totals(sub_total,vat_total,tab) ; 
				// update_overall_totals_costings(sub_total,vat_total) ; 
			}
		}
		
		function update_overall_totals (sub_total,vat_total,tab) {
			
			let profit_loss = parseFloat(\$("input[name='income']").val().replace(/,/g, '')) - parseFloat(\$("input[name='expenses']").val().replace(/,/g, '')) ; 

			// // // \$("input[name='profit_loss']").val(profit_loss.toLocaleString('en-US', {minimumFractionDigits: 2, maximumFractionDigits: 2})) ;
			// // // \$("input[name='sub_total_"+tab+"']").val(sub_total.toLocaleString('en-US', {minimumFractionDigits: 2, maximumFractionDigits: 2})) ;
			// // // \$("input[name='vat_total_"+tab+"']").val(vat_total.toLocaleString('en-US', {minimumFractionDigits: 2, maximumFractionDigits: 2})) ;
			// // // \$("input[name='grand_total_"+tab+"']").val((sub_total+vat_total).toLocaleString('en-US', {minimumFractionDigits: 2, maximumFractionDigits: 2})) ;			
			
			\$("input[name='profit_loss']").val(profit_loss.toFixed(2)) ;
			\$("input[name='sub_total_"+tab+"']").val(sub_total.toFixed(2)) ;
			\$("input[name='vat_total_"+tab+"']").val(vat_total.toFixed(2)) ;
			\$("input[name='grand_total_"+tab+"']").val((sub_total+vat_total).toFixed(2)) ;			
		}	
		
		function add_quote_expenses_row (row_nr,tab,options1,options2) {
			
			let tindex = $tindex ; 
			tindex = parseInt(tindex) ;
			row_nr = parseInt(row_nr) ;
			tindex = tindex - 10*(1+50-row_nr) ;
			
			let row = add_checkbox(row_nr,"excl_"+tab+"_"+row_nr,"checkboxExcl_"+tab+"_"+row_nr,tindex,1) ; tindex++ ;

			// row = row+"<input name='prev_item_"+tab+"_"+row_nr+"' value='' style='display:none;'>"+add_select(row_nr,"description_"+tab+"_"+row_nr,"selectDescription_"+tab+"_"+row_nr,"Select Description "+row_nr,options1,tindex,2,90,'','') ; tindex++ ;
			row = row+add_select(row_nr,"description_"+tab+"_"+row_nr,"selectDescription_"+tab+"_"+row_nr,"Select Description "+row_nr,options1,tindex,2,90,'','') ; tindex++ ;
			if (tab == "workings_event") {
				row = row+add_select(row_nr,"operator_workings_event_"+row_nr,"selectOperator_workings_event_"+row_nr,"Select Operator "+row_nr,options2,tindex,2,90,'','') ; tindex++ ;
				row = row+add_input(row_nr,"remarks_workings_event_"+row_nr,"inputRemarks_workings_event_"+row_nr,"Remarks "+row_nr,tindex,'',1) ; tindex++ ;
				row = row+add_input(row_nr,"supplier_workings_event_"+row_nr,"inputSupplier_workings_event_"+row_nr,"Supplier "+row_nr,tindex,'',1) ; tindex++ ;
				row = row+add_input(row_nr,"ref_nr_workings_event_"+row_nr,"inputRef_nr_workings_event_"+row_nr,"Ref Nr "+row_nr,tindex,'',1) ; tindex++ ;				
			}
			row = row+add_input(row_nr,"qty_"+tab+"_"+row_nr,"inputQty_"+tab+"_"+row_nr,"Qty "+row_nr,tindex,'',1) ; tindex++ ;
			row = row+add_input(row_nr,"amount_usd_"+tab+"_"+row_nr,"inputAmount_usd_"+tab+"_"+row_nr,"Amount "+row_nr,tindex,'',1) ; tindex++ ;
			row = row+add_input(row_nr,"amount_ttl_"+tab+"_"+row_nr,"inputAmount_ttl_"+tab+"_"+row_nr,"Total Amount "+row_nr,tindex,"readonly",1) ; tindex++ ;
			row = row+add_checkbox(row_nr,"vat_"+tab+"_"+row_nr,"checkboxVat_"+tab+"_"+row_nr,tindex,1) ; tindex++ ;
			return row ; 
		}
	~ ;
	
} #------------------------------------------------------------------------------------------

sub event_tabs_build_qt_left {

	my ($id,$tab,$ret,$demo_page) = @_ ;
 
	if ($tab == 1) {  # Cusotmer Details

		# --------------- START CUSOTMER DETAILS --------------------------------------------------------------------------------------------------------
		
		my $title_ = ($demo_page) ? "Demo" : "Event" ; 
		
		my $sec = '_customer_details' ; 
		
		my ($sec_name,$sec_cnt,$sec_col) = &tab_sec_det($sec) ;
		
		$ret_pdf_link_sec .= $sec . '+' . $sec_cnt . '|' ;
		
		return if $ret ;
		
		$lcol = 3 ; $fcol = 1 ; $add_form_fields = '' ;

		# $add_form_fields .= qq~<div style="display:none;">~ if $is_schools_manager ; 
		$add_form_fields .= qq~<div style="display:none;">~ if $glod_user_level < 3 ; 
			
		my $checked = ($db{$table}{$id}{quote_completed}) ? 'CHECKED' : '' ; 
		$preferred_title{quote_completed} = "$title_ Completed" ;
		$add_form_fields .= &common_min_form_checkbox('quote_completed',$db{$table}{$id}{quote_completed},$checked) ;
		
		# my $checked = 'CHECKED' if $db{$table}{$id}{quote_accepted} ;
		my $checked = ($db{$table}{$id}{quote_accepted}) ? 'CHECKED' : '' ; 
		$preferred_title{quote_accepted} = "$title_ Accepted" ;
		$add_form_fields .= &common_min_form_checkbox('quote_accepted',$db{$table}{$id}{quote_accepted},$checked) ;
		
		my $checked = ($db{$table}{$id}{quote_rejected}) ? 'CHECKED' : '' ;
		$preferred_title{quote_rejected} = "$title_ Rejected" ;
		$add_form_fields .= &common_min_form_checkbox('quote_rejected',$db{$table}{$id}{quote_rejected},$checked) ;
				
		# my $checked = 'CHECKED' if $db{$table}{$id}{quote_pending} or $iaction eq 'add' ;
		my $checked = ($db{$table}{$id}{quote_pending} || $iaction eq 'add') ? 'CHECKED' : '' ;
		$preferred_title{quote_pending} = "$title_ Pending" ;
		$add_form_fields .= &common_min_form_checkbox('quote_pending',$db{$table}{$id}{quote_pending},$checked) ;
		
		# my $checked = 'CHECKED' if $db{$table}{$id}{quote_cancelled} ; 
		my $checked = ($db{$table}{$id}{quote_cancelled}) ? 'CHECKED' : '' ;
		$preferred_title{quote_cancelled} = "$title_ Cancelled" ;
		$add_form_fields .= &common_min_form_checkbox('quote_cancelled',$db{$table}{$id}{quote_cancelled},$checked) ;
			
		# $add_form_fields .= qq~</div>~ if $is_schools_manager ; 
		$add_form_fields .= qq~</div>~ if $glod_user_level < 3 ; 

		$fcol = 8 ;
		$add_form_fields .= &common_min_form_input('contact_name',$db{$table}{$id}{contact_name},'') ;
		$add_form_fields .= &common_min_form_input('address',$db{$table}{$id}{address},'') ;
		$add_form_fields .= &common_min_form_input('email',$db{$table}{$id}{email},'') ;
		$add_form_fields .= &common_min_form_input('tel',$db{$table}{$id}{tel},'') ;
		$fcol = 5 ;

		$add_form_fields .= qq~<div class='row'><div class='col-md-12' style="height:12px;">&nbsp;</div></div>~ ;
		
		&event_tabs_content_box($sec_name,$sec_col);

		$trigger_jquery_raw .= qq~

			if (!\$("#checkboxQuote_accepted").is(":checked") && !\$("#checkboxQuote_rejected").is(":checked") && !\$("#checkboxQuote_pending").is(":checked") && !\$("#checkboxQuote_cancelled").is(":checked") && !\$("#checkboxQuote_completed").is(":checked")) {
					\$("#checkboxQuote_pending").prop("checked","true") ;
			}
		
			\$("#checkboxQuote_completed,#checkboxQuote_accepted,#checkboxQuote_rejected,#checkboxQuote_pending,#checkboxQuote_cancelled").click( function () {
				
				if (this.id != 'checkboxQuote_completed' && \$("#checkboxQuote_completed").is(":checked")) {
					\$("#checkboxQuote_completed").prop("checked",false) ;
				}	
				
				if (this.id != 'checkboxQuote_accepted' && \$("#checkboxQuote_accepted").is(":checked")) {
					\$("#checkboxQuote_accepted").prop("checked",false) ;
				}
				
				if (this.id != 'checkboxQuote_rejected' && \$("#checkboxQuote_rejected").is(":checked")) {
					\$("#checkboxQuote_rejected").prop("checked",false) ;
				}	
				
				if (this.id != 'checkboxQuote_pending' && \$("#checkboxQuote_pending").is(":checked")) {
					\$("#checkboxQuote_pending").prop("checked",false) ;
				}
				
				if (this.id != 'checkboxQuote_cancelled' && \$("#checkboxQuote_cancelled").is(":checked")) {
					\$("#checkboxQuote_cancelled").prop("checked",false) ;
				}				
				
				if (this.id == 'checkboxQuote_completed' && !\$("#checkboxQuote_accepted").is(":checked")) {
					\$("#checkboxQuote_accepted").prop("checked",true) ;
				}	
				
				if (!\$("#checkboxQuote_accepted").is(":checked") && !\$("#checkboxQuote_rejected").is(":checked") && !\$("#checkboxQuote_pending").is(":checked") && !\$("#checkboxQuote_cancelled").is(":checked") && !\$("#checkboxQuote_completed").is(":checked")) {
					\$("#checkboxQuote_pending").prop("checked","true") ;
				}
				
			}) ;
			
		~ ; 

		# --------------- END CUSOTMER DETAILS ----------------------------------------------------------------------------------------------------------
	}

	if ($tab == 2) {  # Cusotmer Details

		# --------------- START CUSOTMER DETAILS --------------------------------------------------------------------------------------------------------
		
		my $sec = '_event_details' ; 
		
		my ($sec_name,$sec_cnt,$sec_col) = &tab_sec_det($sec) ;
		
		$ret_pdf_link_sec .= $sec . '+' . $sec_cnt . '|' ;
		
		return if $ret ;
 
		# my $op_change = qq~~ ; 
		
		# for (1 .. $nr_of_system_names_and_clubs) {
			# $op_change .= qq~,#selectOperator_id_calibration_$_~ ;
		# }
		# $op_change = substr($op_change,1) ;
		
		$lcol = 2 ; $fcol = 3 ;
		
		$add_form_fields = '' ;
		
		$add_form_fields .= qq~
				<div class='row'>
					<div class='col-md-12'>~ ; 

		# $fcol = 6 ;
		$preferred_title{ref} = 'Event Name' ;
		$preferred_title{date_from} = 'Event Date/Time From' ;
		$preferred_title{date_to} = 'Event Date/Time To' ;
		
		my $current_day_of_week = Day_of_Week($now_year,$now_mm,$now_dd) ; 

		my $delta_days = 6 - $current_day_of_week ;
		$delta_days = 7 unless $delta_days ;
		$delta_days = 6 if $delta_days < 0 ;
		
		my ($def_year,$def_mon,$def_day) = Add_Delta_Days($now_year,$now_mm,$now_dd,$delta_days) ; 
		$def_mon = sprintf("%02d",$def_mon) ; 
		$def_day = sprintf("%02d",$def_day) ; 

		$db{$table}{$id}{date_from} = "$def_year-$def_mon-$def_day 08:00:00" unless $db{$table}{$id}{date_from} ; 
		$db{$table}{$id}{date_to} = "$def_year-$def_mon-$def_day 17:00:00" unless $db{$table}{$id}{date_to} ; 
		
		my $hide_select_1 = '' ; my $readonly_select = '' ; my $sport_type_ids_readonly = '' ; my $organisation_ids_readonly = '' ; my $linear_id_readonly = '' ; my $readonlyform = 0 ; # my $hide_select_2 = qq~~ ;
		
		my $quote_accepted  = $db{$table}{$id}{quote_accepted} ;
		my $quote_rejected  = $db{$table}{$id}{quote_rejected} ;
		my $quote_cancelled = $db{$table}{$id}{quote_cancelled} ;
		my $quote_completed = $db{$table}{$id}{quote_completed} ;
# $db{$table}{$event_id}{quote_completed}
		# if (($quote_accepted || $quote_rejected || $quote_cancelled) && $glod_user_level < 4) {
		if ((($quote_accepted || $quote_rejected || $quote_cancelled) && $glod_user_level < 3) || ($quote_completed && $glod_user_level <= 3)) {
			
			for (1 .. 20) {
				$readonly{"time_from_$_"} = "READONLY";
				$readonly{"time_to_$_"} = "READONLY";
			}
			
			$readonlyform = 1 ; 

			$readonly{ref} = "READONLY" ; $readonly{date_from} = "READONLY" ; $readonly{date_to} = "READONLY" ; $readonly{qty} = "READONLY" ; $readonly{city_id} = "READONLY" ; 
			$add_form_fields 	.= &common_min_form_input('ref',$db{$table}{$id}{ref},'') ;
			$add_form_fields 	.= &common_min_form_input('date_from',$db{$table}{$id}{date_from}) ;	
			$add_form_fields 	.= &common_min_form_input('date_to',$db{$table}{$id}{date_to}) ;
			$hide_select_1 	= qq~<div class = "row" style ="display:none;">~ ; # $hide_select_2 = qq~</div>~ ; 
			
			$readonly{sport_type_ids_readonly} = "READONLY" ; $readonly{region_id_readonly} = "READONLY" ; $readonly{city_id_readonly} = "READONLY" ; $readonly{organisation_ids_readonly} = "READONLY" ; $readonly{linear_id_readonly} = "READONLY" ;    
			
			$preferred_title{sport_type_ids_readonly} = "Sport Type(s)" ; $preferred_title{region_id_readonly} = "Regions" ; $preferred_title{city_id_readonly} = "City" ; $preferred_title{organisation_ids_readonly} = "Venue(s)" ; $preferred_title{linear_id_readonly} = "Linear" ;
			
			foreach (split(",",$db{$table}{$id}{sport_type_ids})) {
				$sport_type_ids_readonly .= qq~$db{sport_types}{$_}{name},~ ;	
			}
			chop $sport_type_ids_readonly if $sport_type_ids_readonly ;

			foreach (split(",",$db{$table}{$id}{organisation_ids})) {
				$organisation_ids_readonly .= qq~$db{organisations}{$_}{name},~ ;	
			}
			chop $organisation_ids_readonly if $organisation_ids_readonly ;
			
			local %linear_vals = () ; $linear_vals{1} = "Yes" ; $linear_vals{2} = "No" ; $linear_vals{3} = "Delayed" ; 
			
			# my $cities_readoly = qq~~ ; 
			# foreach my $city_id (split(",",$db{$table}{$id}{city_id})) {
				# $cities_readoly .= qq~$db{cities}{$city_id}{city}, ~ ; 
			# }
			# chop $cities_readoly if $cities_readoly ; chop $cities_readoly if $cities_readoly ;
			
			my $cities_readoly = join (", ", map { "$db{cities}{$city_id}{name}" } split(/\,/,$db{$table}{$id}{city_id})) ; 
			
			# $readonly_select	.= &common_min_form_input('sport_type_ids_readonly',$db{sport_types}{$db{$table}{$id}{sport_type_ids}}{name},'') ;
			$readonly_select	.= &common_min_form_input('sport_type_ids_readonly',$sport_type_ids_readonly ,'') ;
			$readonly_select	.= &common_min_form_input('region_id_readonly',$db{regions}{$db{$table}{$id}{region_id}}{name},'') ; 
			$readonly_select	.= &common_min_form_input('city_id_readonly',$cities_readoly,'') ;
			# $readonly_select	.= &common_min_form_input('organisation_ids_readonly',$db{organisations}{$db{$table}{$id}{organisation_ids}}{name},'') ;
			$readonly_select	.= &common_min_form_input('organisation_ids_readonly',$organisation_ids_readonly,'') ;
			$readonly_select	.= &common_min_form_input('linear_id_readonly',$linear_vals{$db{$table}{$id}{linear_id}},'') ;
		} else {
			$add_form_fields .= &common_min_form_input('ref',$db{$table}{$id}{ref},'') ;
			$readonly{date_from} = "READONLY" ; $readonly{date_to} = "READONLY" ;
			$add_form_fields .= &common_min_form_datetimepicker('date_from',$db{$table}{$id}{date_from}) ;
			$add_form_fields .= &common_min_form_datetimepicker('date_to',$db{$table}{$id}{date_to}) ;
			
			$dlg{sport_type_ids}    = qq~&nbsp;&nbsp;&nbsp;<i>$db{$table}{$id}{sports_type}</i>~ ;
			$dlg{sport_type_ids}   .= qq~<div class="col-md-1"><a href="javascript:dlgMdl('$useropts{'scripts'}/dialog/add_sport_type.pl?add&selectSport_type_ids','Add Sport','','medium-dialog');" class="btn btn-primary btn-xs" style="padding:0px 2px 0px 3px;margin-top:0px;" title='Add Sport' data-toggle='tooltip' data-placement='right'><i class="glyphicon glyphicon-plus" style="padding:0;margin-top:0px;top:0px;"></i></a></div>~ ; 		
			$dlg{city_id}          .= qq~&nbsp;&nbsp;&nbsp;<i>$db{$table}{$id}{city}</i>~ ;
			$dlg{city_id} 	       .= qq~<div class="col-md-1"><a href="javascript:dlgMdl('$useropts{'scripts'}/dialog/add_city.pl?add&selectCity_id','Add City','','medium-dialog');" class="btn btn-primary btn-xs" style="padding:0px 2px 0px 3px;margin-top:0px;" title='Add City' data-toggle='tooltip' data-placement='right'><i class="glyphicon glyphicon-plus" style="padding:0;margin-top:0px;top:0px;"></i></a></div>~ ; 
			$dlg{organisation_ids} .= qq~&nbsp;&nbsp;&nbsp;<i>$db{$table}{$id}{location}</i>~ ;
			$dlg{organisation_ids} .= qq~<div class="col-md-1"><a href="javascript:dlgMdl('$useropts{'scripts'}/dialog/add_school.pl?add&selectOrganisation_ids','Add Venue','','medium-dialog');" class="btn btn-primary btn-xs" style="padding:0px 2px 0px 3px;margin-top:0px;" title='Add Venue' data-toggle='tooltip' data-placement='right'><i class="glyphicon glyphicon-plus" style="padding:0;margin-top:0px;top:0px;"></i></a></div>~ ; 
			$dlg{region_id}        .= qq~&nbsp;&nbsp;&nbsp;<i>$db{$table}{$id}{location}</i>~ ;
			$dlg{region_id}        .= qq~<div class="col-md-1"><a href="javascript:dlgMdl('$useropts{'scripts'}/dialog/add_region.pl?add&selectRegion_id','Add Region','','medium-dialog');" class="btn btn-primary btn-xs" style="padding:0px 2px 0px 3px;margin-top:0px;" title='Add Region' data-toggle='tooltip' data-placement='right'><i class="glyphicon glyphicon-plus" style="padding:0;margin-top:0px;top:0px;"></i></a></div>~ ; 
		}
		
		# $fcol = 1 ; 
		$preferred_title{qty} = 'Actual Days' ;  $dont_end_row{qty} = 1 ;  
		
		# if (($quote_accepted || $quote_rejected || $quote_cancelled) && $glod_user_level < 4) { $readonly{qty} = "READONLY" ; }
		# if (($quote_accepted || $quote_rejected || $quote_cancelled) && $glod_user_level < 3) { $readonly{qty} = "READONLY" ; }
		if ((($quote_accepted || $quote_rejected || $quote_cancelled) && $glod_user_level < 3) || ($quote_completed && $glod_user_level <= 3)) { $readonly{qty} = "READONLY" ; }
	 
		my @times_from 	= split(/\;/,";$db{$table}{$id}{times_from}") ; 
		my @times_to 	= split(/\;/,$db{$table}{$id}{times_to}) ; 
		
		my @default = () ; my $saved_time_from = qq~~ ; my $saved_time_to = qq~~ ;
		
		my $year 	= ($db{$table}{$id}{date_from}) ? substr($db{$table}{$id}{date_from},0,4) : $now_year ; 
		my $month 	= ($db{$table}{$id}{date_from}) ? substr($db{$table}{$id}{date_from},5,2) : $now_mm ; 
		my $day 	= ($db{$table}{$id}{date_from}) ? substr($db{$table}{$id}{date_from},8,2) : $now_dd ; 
		
		$fcol = 1 ;
		
		$radio_opts{event_length} = "Full Day+Half Day" ;
		
		# my $cnt_radio_opts = 0 ;
		
		# foreach (split(/\+/,$radio_opts{event_length})) {
			# $cnt_radio_opts++ ;
		# }
		
		my @timepicker_ids = () ; my @day_ids = () ; 
		
		my %default_days_selected = () ; our %default_active_event_dates = () ; 
		
		# our $final_day_active = 0 ; local $cnt_days = 0 ; 
		
		# foreach (@defaut_days_active) {
			# $cnt_days++ ; 
			# $final_day_active = $cnt_days if $_ ; 
		# }
		
		my ($year1,$month1,$day1) = split(/\-/,substr($db{$table}{$id}{date_from},0,10));
		my ($year2,$month2,$day2) = split(/\-/,substr($db{$table}{$id}{date_to},0,10));

		our $final_day = Delta_Days($year1,$month1,$day1,$year2,$month2,$day2) + 1 ;
		
		our @defaut_days_active = split(/\;/,$db{$table}{$id}{days_active}) ; 
		
		my $event_without_saved_defaults = 0 ; 

		if (!$db{$table}{$id}{days_active}) { 
			$event_without_saved_defaults = 1 ; 
			# my $def_qty = ($db{$table}{$id}{qty}) ? $db{$table}{$id}{qty} : 1 ;
			
			for (1 .. $final_day) {
			# for (1 .. $def_qty) {
				$db{$table}{$id}{days_active} .= "1;" ;
			} 
			# $trigger_jquery .= qq~\$("#selectDay_1_chosen").css("width","100%" );~ if $def_qty== 1 ;
		}
		my @default_event_length = split(/\;/,$db{$table}{$id}{event_length}) ;  my %calc_default_event_length = () ; 
				
		my @days_active =  split(/\;/,$db{$table}{$id}{days_active}) ;

		my $cnt = 0 ; 
		# my $final_day = 0 ; 
		my $no_event_days_cnt = 0 ; 

		my $hide_rows_1 = (substr($db{$table}{$id}{date_from},0,10) eq substr($db{$table}{$id}{date_to},0,10)) ? "style='display:none;'" : "" ;
		my $hide_rows_2 = ($final_day <= 10) ? "style='display:none;'" : "" ;
		
		my %time_label_header_row = () ; 
		
		$time_label_header_row{1} = qq~<div class='row' id="days_drop_downs_1" $hide_rows_1><div class='fieldname col-md-2'><label class="control-label">Individual Dates</label></div>~ ;
		$time_label_header_row{2} = qq~<div class='row' id="days_drop_downs_2" $hide_rows_2><div class='fieldname col-md-2'><label class="control-label">Individual Dates (After 10 Days)</label></div>~ ;
		
		# foreach (@days_active) {
			# $cnt++ ; 
			# $final_day = $cnt if $_ ;
		# }
		
		# $final_day = 1 unless $final_day ;
		# $final_day = $db{$table}{$id}{qty} if $db{$table}{$id}{qty} && !$days_active[$db{$table}{$id}{qty}-1] ;
		
		my $def_nr_of_full_days = 0 ; my $def_nr_of_half_days = 0 ; 
		
		for (1 .. 20) {
			
			my $cnt_row = ($_ <= 10) ? 1 : 2 ; 
			
			$default_days_selected{blank}{$_} = "SELECTED" unless $days_active[$_ - 1] ;
			$default_days_selected{1}{$_} = "SELECTED" if $days_active[$_ - 1] ;
			
			push @timepicker_ids,"#timepickerTime_from_$_" ;
			push @timepicker_ids,"#timepickerTime_to_$_" ;
			push @day_ids,"#day_$_" ;
			
			my ($new_year,$new_month,$new_day) = ($_ > 1) ? Add_Delta_Days($year,$month,$day,$_-1) : ($year,$month,$day) ;
			$new_day = sprintf("%02s",$new_day) ;
			$new_month = sprintf("%02s",$new_month) ;
			
			# $default_active_event_dates{$_} = "$new_year-$new_month-$new_day" if $days_active[$_ - 1] ;
			$default_active_event_dates{$_} = "$new_year-$new_month-$new_day" if $_ <= $final_day ;
			
			my $field = "time_from_$_" ;
			$default[$_ - 1] = $times_from[$_ - 1] ;
			$default[$_ - 1] = substr($db{$table}{$id}{date_from},11) if $db{$table}{$id}{date_from} && (!$times_from[$_ - 1] || $_ == 1) ;
			my $default1 = $default[$_ - 1] ;
			
			$custom_style{$field} = ((!$event_without_saved_defaults && !$defaut_days_active[$_ - 1]) || $_ == 1 || ($_ > $final_day)) ? qq~display:none;~ : ''  ;
			# $days_time_row .= ($glod_user_level > 2) ? &common_min_form_timepicker_col($field,$default[$_ - 1],'',1) : ($readonly{qty} ne "READONLY" || ($final_day >= $_ && !$custom_style{$field})) ? qq~<div class='col-md-$fcol'><div class="controls"><input type="text" name="$field" class="form-control " readonly value="$default[$_ - 1]" style="$custom_style{$field}"></div></div>~ : qq~<div class='col-md-$fcol'></div>~ ;
			$days_time_row{$cnt_row} .= ($readonly{qty} ne "READONLY") ? &common_min_form_timepicker_col($field,$default[$_ - 1],'',1) : ($readonly{qty} eq "READONLY" && ($final_day >= $_ && !$custom_style{$field})) ? qq~<div class='col-md-$fcol'><div class="controls"><input type="text" name="$field" class="form-control " readonly value="$default[$_ - 1]" style="$custom_style{$field}"></div></div>~ : qq~<div class='col-md-$fcol'></div>~ ;
			
			$trigger_jquery_raw .= qq~\$("#timepickerTime_from_1").css("display","none");~ ;
			
			# $default_time_from .= qq~"$_":"$default[$_ - 1]",~ ; 
			$saved_time_from .= qq~"$_":"$times_from[$_ - 1]",~ if $times_from[$_ - 1] ; 
			# $final_day = $_ if $times_from[$_ - 1] ; 
			$field = "time_to_$_" ;
			
			
			$custom_style{$field} = ((!$event_without_saved_defaults && !$defaut_days_active[$_ - 1]) || $_ >= $final_day) ? qq~display:none;~ : ''  ;
			$default[$_ - 1] = $times_to[$_ - 1] ;
			$default[$_ - 1] = substr($db{$table}{$id}{date_to},11) if $db{$table}{$id}{date_to} && (!$times_to[$_ - 1] || $_ == 6) ;
			
			# $days_time_row_2 .= ($readonly{qty} ne "READONLY") ? &common_min_form_timepicker_col($field,$default[$_ - 1],'',1) : ($final_day >= $_ && !$custom_style{$field}) ? &common_min_form_input_col($field,$default[$_ - 1],'',0) : qq~<div class='col-md-$fcol'></div>~ ;
						
			# $days_time_row_2 .= ($glod_user_level > 2) ? &common_min_form_timepicker_col($field,$default[$_ - 1],'',1) : ($readonly{qty} ne "READONLY" || ($final_day >= $_ && !$custom_style{$field})) ? qq~<div class='col-md-$fcol'><div class="controls"><input type="text" name="$field" class="form-control " readonly value="$default[$_ - 1]" style="$custom_style{$field}"></div></div>~ : qq~<div class='col-md-$fcol'></div>~ ;
			$days_time_row_2{$cnt_row} .= ($readonly{qty} ne "READONLY") ? &common_min_form_timepicker_col($field,$default[$_ - 1],'',1) : ($readonly{qty} eq "READONLY" && ($final_day >= $_ && !$custom_style{$field})) ? qq~<div class='col-md-$fcol'><div class="controls"><input type="text" name="$field" class="form-control " readonly value="$default[$_ - 1]" style="$custom_style{$field}"></div></div>~ : qq~<div class='col-md-$fcol'></div>~ ;
			
			# $trigger_jquery_raw .= qq~\$("#timepickerTime_from_$_").css("display","none");~ if $db{$table}{$id}{qty} < $_ ;

			# if (!$db{$table}{$id}{days_active} && ($db{$table}{$id}{qty} != 1 || !$db{$table}{$id}{event_length}) && $db{$table}{$id}{qty} >= $_) {
			# if (!$db{$table}{$id}{days_active} || ($days_active[$_ - 1] && !$default_event_length[$_ - 1])) {
			
			# $trigger_jquery .= qq~
				# \$("#radio_1_1").prop('checked',true) ;
			# ~ if $_ == 1 ;
			
			if ($default_event_length[$_ - 1] eq '1' && $days_active[$_ - 1]) {
				$def_nr_of_full_days++ ;
			} elsif ($default_event_length[$_ - 1] eq '2' && $days_active[$_ - 1]) {
				$def_nr_of_half_days++ ;
			}
			
			if (!$db{$table}{$id}{event_length}) {
				
				my ($h1,$m1,$s1) = ($_ == 1 || !$times_from[$_ - 1]) ? split(/\:/,substr($db{$table}{$id}{date_from},11)) : split(/\:/,$times_from[$_ - 1]) ;
				my ($h2,$m2,$s2) = split(/\:/,substr($db{$table}{$id}{date_to},11)) ;
				my $time_diff = 3600*($h2-$h1) + 60*($m2-$m1) + ($s2-$s1) ; 
 
				if ($time_diff >= 18000) {
					$calc_default_event_length{$_} = 1 ;
					$trigger_jquery .= qq~
						\$("#radio_$_\_1").prop('checked',true) ;
					~ if $readonly{qty} ne "READONLY" ; 		
					# $def_nr_of_full_days++ if $default_event_length[$_ - 1] ; 			
				} elsif ($time_diff >= 0 && $time_diff < 18000) {
					$calc_default_event_length{$_} = 2 ;
					$trigger_jquery .= qq~
						\$("#radio_$_\_2").prop('checked',true) ;
					~ if $readonly{qty} ne "READONLY" ; 	
					# $def_nr_of_half_days++ if $default_event_length[$_ - 1] ; 		
				}
				
				$trigger_jquery .= qq~
					\$("#day_$_").val("1") ; 
					\$("#day_$_").trigger("chosen:updated") ; 
				~ if $_ <= $final_day && $readonly{qty} ne "READONLY" ;
				
			} elsif ($_ <= $final_day) {
				
				# $days_active[$_ - 1
				
				# my ($h1,$m1,$s1) = split(/\:/,$times_from[$_ - 1]) ;
				# my ($h2,$m2,$s2) = split(/\:/,$times_to[$_ - 1]) ;
				# my $time_diff = 3600*($h2-$h1) + 3600*($m2-$m1) + ($s2-$s1) ; 
				if ($default_event_length[$_ - 1]) {
					if ($default_event_length[$_-1] eq "1") {
						$trigger_jquery .= qq~
							// \$("input[name='selected_$_\_event_length']").val("1") ; 
							\$("#radio_$_\_1").prop('checked',true) ;
						~ ; 
						# $def_nr_of_full_days++ ; 								
					} elsif ($default_event_length[$_-1] eq "2") {
						$trigger_jquery .= qq~
							// \$("input[name='selected_$_\_event_length']").val("2") ; 
							\$("#radio_$_\_2").prop('checked',true) ;
						~ ;  
						# $def_nr_of_half_days++ ; 		
					}					
				} else {
					my ($h1,$m1,$s1) = split(/\:/,substr($db{$table}{$id}{date_from},11)) ;
					my ($h2,$m2,$s2) = split(/\:/,substr($db{$table}{$id}{date_to},11)) ;
					my $time_diff = 3600*($h2-$h1) + 60*($m2-$m1) + ($s2-$s1) ; 

					if ($time_diff >= 18000) {
						$calc_default_event_length{$_} = 1 ;
						$trigger_jquery .= qq~
							\$("#radio_$_\_1").prop('checked',true) ;
						~ if $readonly{qty} ne "READONLY" ; 		
						# $def_nr_of_full_days++ ;
					} elsif ($time_diff >= 0 && $time_diff < 18000) {
						$calc_default_event_length{$_} = 2 ;
						$trigger_jquery .= qq~
							\$("#radio_$_\_2").prop('checked',true) ;
						~ if $readonly{qty} ne "READONLY" ; 	
						# $def_nr_of_half_days++ ;
					}					
				}
			}
			
			if ((!$days_active[$_ - 1] && $_ > $final_day) || ($db{$table}{$id}{days_active} == ";;;;;") || (!$days_active[$_ - 1] && $_ <= $final_day)) {
				$trigger_jquery .= qq~\$("#radio_$_\_1").css("display","none") ; ~ ;
				$trigger_jquery .= qq~\$("#radio_$_\_2").css("display","none") ; ~ ;
			}
	
			# $default_time_to .= qq~"$_":"$default[$_ - 1]",~ ; 
			$saved_time_to .= qq~"$_":"$times_to[$_ - 1]",~ if $times_to[$_ - 1] ; 
			
			$field = "day_$_" ;
			$custom_style{$field} = ($final_day < $_ || $final_day == 1) ? qq~display:none;~ : ''  ;
			
			if ($readonly{qty} ne "READONLY") {
			# if ($glod_user_level > 2) {
				
				$trigger_jquery .= qq~\$("#day_$_\_chosen").css("display","none");~ if $_ > $final_day || $final_day == 1 ;
				
				# $time_label_header_row .= qq~</div><div class='row' id="days_drop_downs_2" $hide_rows><div class='fieldname col-md-2'><label class="control-label">Individual Dates</label></div>~ ;

				$time_label_header_row{$cnt_row} .= qq~
					<div class='fieldname col-md-1' align="center">
						<select class="form-control" style="margin:2px 0 2px 0;$custom_style{$field}" name="day_$_" id="day_$_" data-rel="chosen" tabindex="$_">
							<option value="" $default_days_selected{blank}{$_}>No Events</option>
							<option value="1" $default_days_selected{1}{$_}>$new_year-$new_month-$new_day</option>
						</select>
					</div>
				~ ;
				
				# $trigger_jquery .= qq~ console.log("Day $_ : ;$default_days_selected{blank}{$_};$default_days_selected{1}{$_}; "+\$("#day_$_").val()) ; ~ ; 
					
			} else {
				$time_label_header_row{$cnt_row} .= (!$default_days_selected{1}{$_}) ? qq~
					<div class='fieldname col-md-1' align="left" style="$custom_style{$field}"><label class="control-label">&nbsp;&nbsp;No Events</label></div>
				~ : qq~
					<div class='fieldname col-md-1' align="left" style="$custom_style{$field}">
						<input type="text" style="display:none;" name="day_$_" value="1"><label class="control-label">&nbsp;&nbsp;$new_year-$new_month-$new_day</label></div>
				~ ;									
			}
		}
		
		$def_nr_of_full_days = 1 if !$def_nr_of_full_days && !$def_nr_of_half_days ; 
		
		$trigger_jquery_raw .= qq~\$("#timepickerTime_to_$final_day").css("display","none");~ if $final_day ;
		
		my $day_ids_string = join(",",@day_ids) ;
		
		# chop $default_time_from if $default_time_from ; chop $default_time_to if $default_time_to ; 
		
		$trigger_jquery_raw .= qq~
			
			// let [nr_days["full"],nr_days["half"]] = count_nr_of_full_day_and_half_days() ;
			
			let nr_days = {} ; let prev_nr_days = {} ;
			
			nr_days["full"] = "$def_nr_of_full_days" ; 
			nr_days["half"] = "$def_nr_of_half_days" ; 
			
			nr_days["full"] = parseInt(nr_days["full"]) ;
			nr_days["half"] = parseInt(nr_days["half"]) ;
			
			prev_nr_days["full"] = nr_days["full"] ; prev_nr_days["half"] = nr_days["half"] ; 
			const default_cost_item_rates = {$default_cost_item_rates_javascript} ;
			// const default_full_day_cost_item_rates = {$default_full_day_cost_item_rates_javascript} ;
			// const default_half_day_cost_item_rates = {$default_half_day_cost_item_rates_javascript} ;
 
			let ij = 0 ; let orig_selected_system_names = {} ;  
			// const get_event_cost_id = {"63":"24","286":"25","285":"10"} ; // TESTING
			const get_event_cost_id = {$get_event_cost_id_from_system_id_javascript} ; // TESTING
			let cnt_systems_per_cost_item = {} ;
			
			let changed_radio_cnt = 0 ; let system_with_cost_item_rates_selected = 0 ; 
			
			let saved_time_from = {$saved_time_from} ; 
			let saved_time_to = {$saved_time_to} ;
			
			// while (true) { 
				// ij++ ;
				// if (!\$("#system_name_row_"+ij).html()) {
					// break ;
				// }
				// orig_selected_system_names[ij] = (\$("#selectSystem_name_"+ij).val()) ? \$("#selectSystem_name_"+ij).val() : "" ;
				// if (default_cost_item_rates[get_event_cost_id[\$("#selectSystem_name_"+ij).val()]]) {
					// system_with_cost_item_rates_selected = 1 ;
				// }
				
			// }

			cnt_selected_systems_per_cost_item() ;
			
			\$("$day_ids_string").change(function (e) {
				
				// console.log("changed day") ; 
				update_qty_in_quote_tab_when_changing_nr_of_full_or_half_days() ;
				
				const id = this.id ; 
				let col_nr = get_row_nr(id) ; 
				// const secondLastIndex = beforeLastUnderscore.lastIndexOf('_');
				// const changedInput = beforeLastUnderscore.substring(secondLastIndex + 1);
				
				let nr_days = \$("input[name='qty']").val() ; 
				
				let nr_of_active_days = calc_nr_of_active_days() ;
				
				let datesBetween ; 
				
				let date_counter = 0 ; let dates_map = {} ; 
				
				// if ((\$\(this).val() && (nr_of_active_days == 7 || nr_of_active_days == 11)) || (!\$\(this).val() && (nr_of_active_days == 6 || nr_of_active_days == 10))) {
					datesBetween = getDatesBetween(\$("input[name='date_from']").val(),\$("input[name='date_to']").val(),0) ;
					for (const date of datesBetween) {
						date_counter++ ; 
						dates_map[date_counter] = date ;
						// console.log("date_counter : "+date_counter+" , dates_map[date_counter] : "+dates_map[date_counter]) ; 
					}					
				// }
				
				let all_added_dropdowns = "#1" ; 
				let all_added_dropdowns_chosen = "#1" ; 

				// let select_chosen_ids_1 = '#3' ;
				// let select_chosen_ids_2 = '#4' ;

								// let def_val = 0 ; 
								
								// if (\$("#day_"+b).val()) {
									
									// def_val = \$("#selectCustom_operator_"+a+"_"+b).val() ;
									// add_operator_dropdown("day_"+b+"_name_"+a+"_extra_1","day_"+a+"_"+b+"_extra_1","custom_operator_"+a+"_"+b,"selectCustom_operator_"+a+"_"+b,dates_map[b],a,0) ; 
									// select_chosen_ids_1 = select_chosen_ids_1+",#selectCustom_operator_"+a+"_"+b ;
									// select_chosen_ids_2 = select_chosen_ids_2+",#selectCustom_operator_"+a+"_"+b+"_chosen" ;	
									// names_to_display = names_to_display + ",#day_"+b+"_name_"+a+"_extra_1" ;
								// }
							
								// delete_operator_dropdown ("day_"+b+"_name","day_"+a+"_"+b) ;	
								// delete_operator_dropdown ("day_"+b+"_name_"+a+"_extra_1","day_"+a+"_"+b+"_extra_2") ;	
								
								// if (!\$("#selectCustom_operator_"+a+"_"+b).val() && def_val) {
									// \$("#selectCustom_operator_"+a+"_"+b).val(def_val) ;
									// \$("#selectCustom_operator_"+a+"_"+b).trigger("chosen:updated") ;									
								// }
							// } 	

				let def_val = 0 ; 
				
				let hide_this_1 = '' ; 
				let hide_this_2 = '' ; 
				
				if (!\$\(this).val()) {
				
					\$("#timepickerTime_from_"+col_nr+"").css("display","none") ;
					\$("#timepickerTime_to_"+col_nr+"").css("display","none") ;
					\$("#radio_"+col_nr+"_1").css("display","none") ;
					\$("#radio_"+col_nr+"_2").css("display","none") ;
					nr_days=nr_days-1;
					if (nr_of_active_days < 6) {	
						\$("#day_"+col_nr+"_name").css("display","none") ;
						\$("#day_"+col_nr+"_name").removeClass('col-md-1').addClass('col-md-0') ;
					}
					for (let q=1;q<=15;q++) {
						if (\$("input[name='clicked_button_"+q+"']").val()) {
							
							if (nr_of_active_days <= 6 && q > 1 && !\$("#extra_heading_"+q).is(":hidden")) {
								\$("#extra_heading_"+q).hide() ; 
								console.log("Hide Heading") ; 
							} else if (nr_of_active_days > 6 && q > 1 && \$("#extra_heading_"+q).length && \$("#extra_heading_"+q).is(":hidden") && \$("input[name='clicked_button_"+q+"']").val()) {
								\$("#extra_heading_"+q).show() ;
								console.log("Display Heading") ; 
							} else if (nr_of_active_days > 6 && q > 1 && !\$("#extra_heading_"+q).length && \$("input[name='clicked_button_"+q+"']").val()) {
								console.log("Add Heading") ; 
								\$("#system_name_row_"+q).before("<div class='row' id='extra_heading_"+q+"'><div class='col-md-2'></div><div class='col-md-4'><div class='row'><div class='col-md-4'><label class='control-label'>&nbsp;&nbsp;System&nbsp;Name</label></div><div class='col-md-4'><label class='control-label'>&nbsp;&nbsp;Club&nbsp;Name</label></div><div class='col-md-4'><label class='control-label'>&nbsp;&nbsp;Operator&nbsp;Name</label></div></div></div></div>") ;	
								
							}
							
							if (nr_of_active_days < 6) {	
								\$("#day_"+q+"_"+col_nr).css("display","none") ;
								\$("#day_"+q+"_"+col_nr).removeClass('col-md-1').addClass('col-md-0') ;
							} else if (nr_of_active_days == 6) {
								
								if (q > 1 && !\$("#extra_heading_"+q).is(":hidden")) {
									\$("#extra_heading_"+q).hide() ; 
								}
								
								for (let col=1;col<=20;col++) {
									 if (\$("#day_"+col).val() && !\$("#day_"+q+"_"+col).html()) {
										add_operator_dropdown ("day_"+col+"_name","day_"+q+"_"+col,"custom_operator_"+q+"_"+col,"selectCustom_operator_"+q+"_"+col,dates_map[col],q,0) ;
										all_added_dropdowns = all_added_dropdowns + ",#selectCustom_operator_"+q+"_"+col ;
										all_added_dropdowns_chosen = all_added_dropdowns_chosen + ",selectCustom_operator_"+q+"_"+col+"_chosen" ;
									 } else if (!\$("#day_"+col).val() && dates_map[col] && !\$("#day_"+q+"_"+col).html()) {
										add_operator_dropdown ("day_"+col+"_name","day_"+q+"_"+col,"custom_operator_"+q+"_"+col,"selectCustom_operator_"+q+"_"+col,dates_map[col],q,1) ;
										all_added_dropdowns = all_added_dropdowns + ",#selectCustom_operator_"+q+"_"+col ;
										all_added_dropdowns_chosen = all_added_dropdowns_chosen + ",selectCustom_operator_"+q+"_"+col+"_chosen" ;
									 }  else if (\$("#day_"+col).val() && \$("#day_"+q+"_"+col).html() && \$("#day_"+q+"_"+col).is(":hidden")) {
										display_non_empty_operator_dropdown("day_"+col+"_name","day_"+q+"_"+col) ; 
									 }
									 delete_operator_dropdown ("day_"+col+"_name_"+q+"_extra_1","day_"+q+"_"+col+"_extra_1") ;
									// console.log("hide_operator_dropdown : ) ; 
								}
								\$("#system_name_row_"+q+"_extra_1").hide() ; 
								\$("#day_names_"+q+"_extra_1").hide() ; 
							} else if (nr_of_active_days > 6 && nr_of_active_days < 10) {
								// hide_non_empty_operator_dropdown("day_"+col_nr+"_name","day_"+q+"_"+col_nr) ; 							
								hide_non_empty_operator_dropdown("day_"+col_nr+"_name_"+q+"_extra_1","day_"+q+"_"+col_nr+"_extra_1") ; 							
							} else if (nr_of_active_days == 10) {
								for (let col=1;col<=20;col++) {
									if (\$("#day_"+col).val() && dates_map[col]) {
										add_operator_dropdown ("day_"+col+"_name_"+q+"_extra_1","day_"+q+"_"+col+"_extra_1","custom_operator_"+q+"_"+col,"selectCustom_operator_"+q+"_"+col,dates_map[col],q,0) ;
										all_added_dropdowns = all_added_dropdowns + ",#selectCustom_operator_"+q+"_"+col ;
										all_added_dropdowns_chosen = all_added_dropdowns_chosen + ",#selectCustom_operator_"+q+"_"+col+"_chosen" ;
									} else if (!\$("#day_"+col).val() && dates_map[col]) {
										add_operator_dropdown ("day_"+col+"_name_"+q+"_extra_1","day_"+q+"_"+col+"_extra_1","custom_operator_"+q+"_"+col,"selectCustom_operator_"+q+"_"+col,dates_map[col],q,1) ;
										all_added_dropdowns = all_added_dropdowns + ",#selectCustom_operator_"+q+"_"+col ;
										all_added_dropdowns_chosen = all_added_dropdowns_chosen + ",#selectCustom_operator_"+q+"_"+col+"_chosen" ;
									} else if (\$("#day_"+col).val() && \$("#day_"+q+"_"+col+"_extra_1").html() && \$("#day_"+q+"_"+col+"_extra_1").is(":hidden")) {
										display_non_empty_operator_dropdown("day_"+col+"_name_"+q+"_extra_1","day_"+q+"_"+col+"_extra_1") ; 
									}
									delete_operator_dropdown ("day_"+col+"_name_"+q+"_extra_2","day_"+q+"_"+col+"_extra_2") ;
								}		
								hide_non_empty_operator_dropdown("day_"+col_nr+"_name_"+q+"_extra_1","day_"+q+"_"+col_nr+"_extra_1") ; 	
								\$("#system_name_row_"+q+"_extra_1").show() ; 
								\$("#day_names_"+q+"_extra_1").show() ; 								
								\$("#system_name_row_"+q+"_extra_2").hide() ; 
								\$("#day_names_"+q+"_extra_2").hide() ; 
							} else if (nr_of_active_days > 10) {

								let nr_dates_used = 0 ; 
								
								console.log("de select : #nr_of_active_days : "+nr_of_active_days) ; 
								
								for (let col=1;col<=20;col++) {
									
									if (nr_dates_used == 10 && !dates_map[col]) {
										break ;
									}
									if (\$("#day_"+col).val() && dates_map[col] && nr_dates_used < 10) {
										nr_dates_used++ ;
										if (\$("#day_"+q+"_"+col+"_extra_1").is(":hidden")) {
											def_val = \$("#selectCustom_operator_"+q+"_"+col).val() ;
											add_operator_dropdown ("day_"+col+"_name_"+q+"_extra_1","day_"+q+"_"+col+"_extra_1","custom_operator_"+q+"_"+col,"selectCustom_operator_"+q+"_"+col,dates_map[col],q,0) ;
											all_added_dropdowns = all_added_dropdowns + ",#selectCustom_operator_"+q+"_"+col ;
											all_added_dropdowns_chosen = all_added_dropdowns_chosen + ",#selectCustom_operator_"+q+"_"+col+"_chosen" ;	
											delete_operator_dropdown("day_"+col+"_name_"+q+"_extra_2","day_"+q+"_"+col+"_extra_2",0) ;
											if (!\$("#selectCustom_operator_"+q+"_"+col).val() && def_val) {
												\$("#selectCustom_operator_"+q+"_"+col).val(def_val) ;
												\$("#selectCustom_operator_"+q+"_"+col).trigger("chosen:updated") ;									
											}	
										}
									} else if (!\$("#day_"+col).val() && dates_map[col]) {
										add_operator_dropdown ("day_"+col+"_name_"+q+"_extra_1","day_"+q+"_"+col+"_extra_1","custom_operator_"+q+"_"+col,"selectCustom_operator_"+q+"_"+col,dates_map[col],q,1) ;
										if (!\$("#day_"+q+"_"+col+"_extra_1").is(":hidden")) {											
											hide_this_1 = "day_"+col+"_name_"+q+"_extra_1" ;
											hide_this_2 = "day_"+q+"_"+col+"_extra_1" ;
										} 
										if (!\$("#day_"+q+"_"+col+"_extra_2").is(":hidden") && col > 10) {											
											hide_this_1 = "day_"+col+"_name_"+q+"_extra_2" ;
											hide_this_2 = "day_"+q+"_"+col+"_extra_2" ;
										}
									}									
								}
							}
						}
						if (hide_this_1) {
							console.log("hide this : "+hide_this_1+" , "+hide_this_2) ; 
							hide_non_empty_operator_dropdown (hide_this_1,hide_this_2) ;
						}
					}
				} else {
					if (col_nr != "1") {
						\$("#timepickerTime_from_"+col_nr).css("display","") ;
					} 
					\$("#timepickerTime_to_"+col_nr).css("display","") ;						
					\$("#radio_"+col_nr+"_1").css("display","") ;
					\$("#radio_"+col_nr+"_2").css("display","") ;		
					nr_days=parseInt(nr_days)+1;

					for (let q=1;q<=15;q++) {
						if (\$("input[name='clicked_button_"+q+"']").val()) {
							\$("#day_"+q+"_"+col_nr).css("display","") ;
							if (\$("#selectCustom_operator_"+q+"_"+col_nr+"_chosen").css("width") !== "100%"){
								\$("#selectCustom_operator_"+q+"_"+col_nr+"_chosen").css("width","100%") ;								
							}						
						}
					}
					
					if (nr_of_active_days <= 6 && \$("#clicked_any_button").text() && \$("#day_"+col_nr+"_name").css("display") === "none") {	
						\$("#day_"+col_nr+"_name").show() ;
						\$("#day_"+col_nr+"_name").removeClass('col-md-0').addClass('col-md-1') ;
					}
					for (let q=1;q<=15;q++) {
						
						if (\$("input[name='clicked_button_"+q+"']").val()) {
	
							if (nr_of_active_days <= 6 && q > 1 && !\$("#extra_heading_"+q).is(":hidden")) {
								\$("#extra_heading_"+q).hide() ; 
								console.log("Hide Heading") ; 
							} else if (nr_of_active_days > 6 && q > 1 && \$("#extra_heading_"+q).length && \$("#extra_heading_"+q).is(":hidden") && \$("input[name='clicked_button_"+q+"']").val()) {
								\$("#extra_heading_"+q).show() ;
								console.log("Display Heading") ; 
							} else if (nr_of_active_days > 6 && q > 1 && !\$("#extra_heading_"+q).length && \$("input[name='clicked_button_"+q+"']").val()) {
								console.log("Add Heading") ; 
								\$("#system_name_row_"+q).before("<div class='row' id='extra_heading_"+q+"'><div class='col-md-2'></div><div class='col-md-4'><div class='row'><div class='col-md-4'><label class='control-label'>&nbsp;&nbsp;System&nbsp;Name</label></div><div class='col-md-4'><label class='control-label'>&nbsp;&nbsp;Club&nbsp;Name</label></div><div class='col-md-4'><label class='control-label'>&nbsp;&nbsp;Operator&nbsp;Name</label></div></div></div></div>") ;	
								
							}
							
							if (q > 1 && \$("#extra_heading_"+q).is(":hidden")) {
								\$("#extra_heading_"+q).show() ; 
							}
							
							if (nr_of_active_days <= 6) {	
								\$("#day_"+q+"_"+col_nr).show() ;
								\$("#day_"+q+"_"+col_nr).removeClass('col-md-0').addClass('col-md-1') ;
								console.log("#day_"+q+"_"+col_nr) ; 
							} else if (nr_of_active_days == 7) {
								for (let col=1;col<=20;col++) {
									def_val = \$("#selectCustom_operator_"+q+"_"+col).val() ;	
									if (\$("#day_"+col).val()) {
										add_operator_dropdown ("day_"+col+"_name_"+q+"_extra_1","day_"+q+"_"+col+"_extra_1","custom_operator_"+q+"_"+col,"selectCustom_operator_"+q+"_"+col,dates_map[col],q,0) ;
										all_added_dropdowns = all_added_dropdowns + ",#selectCustom_operator_"+q+"_"+col ;
									    all_added_dropdowns_chosen = all_added_dropdowns_chosen + ",#selectCustom_operator_"+q+"_"+col+"_chosen" ;
									 } else if (!\$("#day_"+col).val() && dates_map[col]) {
										add_operator_dropdown ("day_"+col+"_name_"+q+"_extra_1","day_"+q+"_"+col+"_extra_1","custom_operator_"+q+"_"+col,"selectCustom_operator_"+q+"_"+col,dates_map[col],q,1) ;
										all_added_dropdowns = all_added_dropdowns + ",#selectCustom_operator_"+q+"_"+col ;
										all_added_dropdowns_chosen = all_added_dropdowns_chosen + ",#selectCustom_operator_"+q+"_"+col+"_chosen" ;
									} else if (\$("#day_"+col).val() && \$("#day_"+q+"_"+col).html() && \$("#day_"+q+"_"+col).is(":hidden")) {
										display_non_empty_operator_dropdown("day_"+col+"_name_"+q+"_extra_1","day_"+q+"_"+col+"_extra_1") ; 
									}
									delete_operator_dropdown ("day_"+col+"_name","day_"+q+"_"+col) ;
									if (!\$("#selectCustom_operator_"+q+"_"+col).val() && def_val) {
										\$("#selectCustom_operator_"+q+"_"+col).val(def_val) ;
										\$("#selectCustom_operator_"+q+"_"+col).trigger("chosen:updated") ;									
									}									 
								}
								\$("#day_"+col_nr+"_name").hide() ;
								\$("#system_name_row_"+q+"_extra_1").show() ; 
								\$("#day_names_"+q+"_extra_1").show() ; 
							} else if (nr_of_active_days > 7 && nr_of_active_days <= 10) {
								display_non_empty_operator_dropdown("day_"+col_nr+"_name_"+q+"_extra_1","day_"+q+"_"+col_nr+"_extra_1") ; 
								hide_non_empty_operator_dropdown("day_"+col_nr+"_name","day_"+q+"_"+col_nr)	;					
							} else if (nr_of_active_days >= 11) {
								let date_used = {} ; let date_used_hidden = {} ; let nr_dates_used = 0 ; 
								for (let col=1;col<=20;col++) {
									
									if (nr_dates_used == 10 && !dates_map[col]) {
										break ;
									}
									if (\$("#day_"+col).val() && dates_map[col] && nr_dates_used < 10) {
										date_used[col] = 1 ;
										nr_dates_used++ ;				
										add_operator_dropdown ("day_"+col+"_name_"+q+"_extra_1","day_"+q+"_"+col+"_extra_1","custom_operator_"+q+"_"+col,"selectCustom_operator_"+q+"_"+col,dates_map[col],q,0) ;
										all_added_dropdowns = all_added_dropdowns + ",#selectCustom_operator_"+q+"_"+col ;
										all_added_dropdowns_chosen = all_added_dropdowns_chosen + ",#selectCustom_operator_"+q+"_"+col+"_chosen" ;
									} else if (!\$("#day_"+col).val() && dates_map[col]) {
										date_used_hidden[col] = 1 ;
										add_operator_dropdown ("day_"+col+"_name_"+q+"_extra_1","day_"+q+"_"+col+"_extra_1","custom_operator_"+q+"_"+col,"selectCustom_operator_"+q+"_"+col,dates_map[col],q,1) ;
									} 
								}
								
								for (let col=11;col<=20;col++) {
									// if (col > 10) {
										if (\$("#day_"+col).val() && !date_used[col]) {
											def_val = \$("#selectCustom_operator_"+q+"_"+col).val() ;					
											add_operator_dropdown ("day_"+col+"_name_"+q+"_extra_2","day_"+q+"_"+col+"_extra_2","custom_operator_"+q+"_"+col,"selectCustom_operator_"+q+"_"+col,dates_map[col],q) ;
											all_added_dropdowns = all_added_dropdowns + ",#selectCustom_operator_"+q+"_"+col ;
											all_added_dropdowns_chosen = all_added_dropdowns_chosen + ",#selectCustom_operator_"+q+"_"+col+"_chosen" ;		
											delete_operator_dropdown("day_"+col+"_name_"+q+"_extra_1","day_"+q+"_"+col+"_extra_1",0) ;
											if (!\$("#selectCustom_operator_"+q+"_"+col).val() && def_val) {
												\$("#selectCustom_operator_"+q+"_"+col).val(def_val) ;
												\$("#selectCustom_operator_"+q+"_"+col).trigger("chosen:updated") ;									
											}												
										} else {
											delete_operator_dropdown ("day_"+col+"_name_"+q+"_extra_2","day_"+q+"_"+col+"_extra_2",0) ;
										}
								}								
								\$("#system_name_row_"+q+"_extra_2").show() ; 
								\$("#day_names_"+q+"_extra_2").show() ; 								
								\$("#system_name_row_"+q+"_extra_1").show() ; 
								\$("#day_names_"+q+"_extra_1").show() ; 
							}
						}
					} 
				}
				
				// hide_non_empty_operator_dropdown (hide_this_1,hide_this_2) ;
				
				\$(all_added_dropdowns).chosen({allow_single_deselect:true}) ;
				\$(all_added_dropdowns_chosen).css("width","100%") ;
				
				// \$(select_chosen_ids_1).chosen({allow_single_deselect:true}) ;
				// \$(select_chosen_ids_2).css("width","100%") ;	
				\$("input[name='qty']").val(nr_days) ; 
			}) ;
		~ if $readonly{qty} ne "READONLY" ;
					
		my $timepicker_ids_string = join(",",@timepicker_ids) ;
		
		# $trigger_jquery_raw .= qq~ \$("$timepicker_ids_string").datetimepicker({language:'pt-BR',pickDate:false});~ ;
 
		if ($readonly{qty} ne "READONLY") {
			
			$trigger_jquery_raw .= qq~	
				
				function set_full_half_day (beforeUnderscore,afterUnderscore) {
					
					let start_time = \$("input[name='time_from_"+afterUnderscore+"']").val() ; 
					let end_time = \$("input[name='time_to_"+afterUnderscore+"']").val() ; 
					 
					let seconds1 = timeStringToSeconds(start_time) ;
					let seconds2 = timeStringToSeconds(end_time) ;
					
					let diffSeconds = seconds2 - seconds1 ;
					
					// let nr_days = \$("#inputQty").val() ; 
					
					let changed_a_full_half_day_toggle = 0 ;  
					if (diffSeconds >= 18000) {
						if (\$("#radio_"+afterUnderscore+"_2").is(":checked")) {
							nr_days["half"]-- ;
						}
						if (!\$("#radio_"+afterUnderscore+"_1").is(":checked")) {
							\$("#radio_"+afterUnderscore+"_1").prop('checked',true) ;
							changed_a_full_half_day_toggle = 1 ; 
							nr_days["full"]++ ;
						}
					} else if (diffSeconds >= 0 && diffSeconds < 18000) {
						if (\$("#radio_"+afterUnderscore+"_1").is(":checked")) {
							nr_days["full"]-- ;
						}
						if (!\$("#radio_"+afterUnderscore+"_2").is(":checked")) {
							\$("#radio_"+afterUnderscore+"_2").prop('checked',true) ;
							changed_a_full_half_day_toggle = 1 ; 
							nr_days["half"]++ ;
						}
					} else {
						// nr_days = parseInt(nr_days) ;
						// nr_days-- ; 
						// \$("#inputQty").val(nr_days) ; 
						
						if (\$("#radio_"+afterUnderscore+"_1").is(":checked")) {
							\$("#radio_"+afterUnderscore+"_1").prop('checked',false) ;
							changed_a_full_half_day_toggle = 1 ; 
							nr_days["full"]-- ;
						}
						if (\$("#radio_"+afterUnderscore+"_2").is(":checked")) {
							\$("#radio_"+afterUnderscore+"_2").prop('checked',false) ;
							changed_a_full_half_day_toggle = 1 ; 
							nr_days["half"]-- ;
						}
						if (beforeUnderscore == 'timepickerTime_from') {
							\$("input[name='time_from_"+afterUnderscore+"']").val(end_time) ;  
						} else if (beforeUnderscore == 'timepickerTime_to') {
							\$("input[name='time_to_"+afterUnderscore+"']").val(start_time) ; 
						}	 
					}
					if (changed_a_full_half_day_toggle == 1) {
						changed_radio_cnt++ ; 
					}
					
				}
				
				\$("$timepicker_ids_string").datetimepicker({language:'pt-BR',pickDate:false});
				
				// \$("#timepickerTime_from_1").on('changeDate', function() {
					// let dateString1 = \$("input[name='time_from").val() ; 		
				// }) ;
				
				\$("$timepicker_ids_string").on('changeDate', function() {

					const id = this.id ; 
					const lastIndex = id.lastIndexOf('_');
					const beforeUnderscore = id.substring(0,lastIndex);
					let   afterUnderscore = id.substring(lastIndex+1);
					afterUnderscore = parseInt(afterUnderscore) ;
					// const from_to_toggle = {"timepickerTime_from":"time_to","timepickerTime_to":"time_from"} ;
					// console.log("timepicker change id : "+id+" , beforeUnderscore : "+beforeUnderscore+" , afterUnderscore : "+afterUnderscore) ; 
					// let date_to = \$("input[name='date_to']").val() ;
					// let datetimeValue = \$("#datetimepickerDate_to").data("date");
					// // // if (beforeUnderscore == 'from' && afterUnderscore == 1) {
						
						// // // let dateString = \$("input[name='time_from").val() ; 
						// // // let timePart = dateString.split(' ')[1]; 
						// // // \$("input[name='time_from_1']").val(timePart) ;  
						// // // return ; 
					// // // }
					// // // if (beforeUnderscore == 'to') { 
						// // // let dateString1 = \$("input[name='time_from").val() ; 		
						// // // let dateString2 = \$("input[name='time_to").val() ; 
						// // // const count_dates = getDatesBetween(dateFromInput,dateToInput,1) ;
						// // // if (afterUnderscore == count_dates) {
							// // // let timePart = dateString.split(' ')[1]; 
							// // // \$("input[name='time_to_"+count_dates+"']").val(timePart) ; 
							// // // return ; 							
						// // // } 
					// // // }
					
					if (id.includes("timepickerTime_from_")) {   
						saved_time_from[afterUnderscore] = \$("input[name='time_from_"+afterUnderscore+"']").val() ;
					} else if (id.includes("timepickerTime_to_")) { 
						saved_time_to[afterUnderscore] = \$("input[name='time_to_"+afterUnderscore+"']").val() ; 
					}
					
					// console.log("input[name='time_from_"+afterUnderscore+"']") ; 
					// console.log("input[name='time_to_"+afterUnderscore+"']") ; 
					
					changed_radio_cnt = 0 ; 
					
					set_full_half_day(beforeUnderscore,afterUnderscore) ; 
					
					if (changed_radio_cnt) {
						update_qty_in_quote_tab_when_changing_nr_of_full_or_half_days() ;
					}
					
					// let start_time = \$("input[name='time_from_"+afterUnderscore+"']").val() ; 
					// let end_time = \$("input[name='time_to_"+afterUnderscore+"']").val() ; 
					// let seconds1 = timeStringToSeconds(start_time) ;
					// let seconds2 = timeStringToSeconds(end_time) ;
					
					// let diffSeconds = seconds2 - seconds1 ;
					// if (diffSeconds >= 18000) {
						// if (!\$("#radio_"+afterUnderscore+"_1").is(":checked")) {
							// \$("#radio_"+afterUnderscore+"_1").prop('checked',true) ;
						// }
					// } else if (diffSeconds >= 0 && diffSeconds < 18000) {
						// if (!\$("#radio_"+afterUnderscore+"_2").is(":checked")) {
							// \$("#radio_"+afterUnderscore+"_2").prop('checked',true) ;
						// }
					// } else {
						// if (\$("#radio_"+afterUnderscore+"_1").is(":checked")) {
							// \$("#radio_"+afterUnderscore+"_1").prop('checked',false) ;
						// }
						// if (\$("#radio_"+afterUnderscore+"_2").is(":checked")) {
							// \$("#radio_"+afterUnderscore+"_2").prop('checked',false) ;
						// }
						// if (beforeUnderscore == 'timepickerTime_from') {
							// \$("input[name='time_from_"+afterUnderscore+"']").val(end_time) ;  
						// } else {
							// \$("input[name='time_to_"+afterUnderscore+"']").val(start_time) ; 
						// }	
							
					// }
					// console.log("start_time : "+start_time+" , end_time : "+end_time+" , seconds1 : "+seconds1+" , seconds2 : "+seconds2+" , diffSeconds : "+diffSeconds) ;
				}) ;
			~ ;
		} 
		
		chop $default_time_from  if $default_time_from ; chop $default_time_to  if $default_time_to ;
		
		$fcol = 3 ; $lcol = 2 ; my $expenses_html_for_javascript = qq~~ ; my $vat_default_for_hidden_expenses = qq~~ ; my $cnt_hidden_rows = 0 ; my $changed_opid_as_level_3 = 0 ;
	
		# my $default_event_length_1 = qq~~ ; my $default_event_length_2 = qq~~ ; my $e_cnt = 0 ; 
		# foreach (split(/\;/,$db{$table}{$id}{event_length})) {
			# $e_cnt++ ;
			# $default_event_length_1 .= qq~$_;~ if $e_cnt <= 10 ; 
			# $default_event_length_2 .= qq~$_;~ if $e_cnt > 10 ; 
		# } 
		# chop $default_event_length_1 if $default_event_length_1 ;
		# chop $default_event_length_2 if $default_event_length_2 ;
		
		my %event_length_rows = () ; 
		
		if ($readonly{qty} eq "READONLY") {
			
			$readonly{nr_of_systems} = "READONLY" ;
			
			$event_length_rows{1} .= qq~
			<div class='row' $hide_rows>~ ;
			
			$event_length_rows{1} .= qq~
				<div class='fieldname col-md-$lcol'>
					<label class='control-label' for='inputFull_half_day'>
						Event Length
					</label>
				</div> 
			~ ;
			
			# $event_length_rows{2} = $event_length_rows{1} ; 
			
			if ($db{$table}{$id}{event_length}) {
				 
				# my  
				my @words = split /\;/, $db{$table}{$id}{event_length} ;
				my $output = join ' ', map { ucfirst lc $_ } @words;
				# style="top:4px;left:-4px;" 
				
				# my $time_label_header_row = qq~<div class='row' id="days_drop_downs" $hide_rows><div class='fieldname col-md-2'><label class="control-label">Individual Dates</label></div>~ ;
# <input type="text" name="event_length_$cnt" value="2" style="display:none;"><input type="text" name="event_length_$cnt" value="1" style="display:none;">
				my $cnt = 0 ; 
				foreach (@words) {
					$cnt++ ;
					
					$event_length_rows{2} = qq~<div class='row'><div class='fieldname col-md-$lcol'><label class='control-label' for='inputFull_half_day'>Event Length</label></div>~ if $cnt >= 11 && $_ && !$event_length_rows{2} ; 
					
					my $el_row_cnt = ($cnt <= 10) ? 1 : 2 ;
					
					$event_length_rows{$el_row_cnt} .= ($_ eq '1') ? qq~
				<div class='fieldname col-md-1' align="left">
					<label class='control-label'>&nbsp;&nbsp;Full Day</label>
				</div>
						~ : ($_ eq '2') ? qq~
				<div class='fieldname col-md-1' align="left"> 
					<label class='control-label'>&nbsp;&nbsp;Half Day</label>
				</div>~ : qq~
				<div class='fieldname col-md-1'>
				</div>~ ; 
				
				}
			} else {
				for (1 .. $final_day) { # <input type="text" name="event_length_$cnt" value="1" style="display:none;"><input type="text" name="event_length_$cnt" value="2" style="display:none;">
					
					my $el_row_cnt = ($cnt <= 10) ? 1 : 2 ;
					$event_length_rows{$el_row_cnt} .= ($calc_default_event_length{$_} eq '1') ? qq~
				<div class='fieldname col-md-1' align="left">
					<label class='control-label'>&nbsp;&nbsp;Full Day</label>
				</div>
						~ : ($calc_default_event_length{$_} eq '2') ? qq~
				<div class='fieldname col-md-1'> 
					<label class='control-label'>Half Day</label>
				</div>
						~ : qq~
				<div class='fieldname col-md-1'>
				</div>
						~ ; 
				} 
			} 
			
			# $add_form_fields	.= &common_min_form_label('event_length',$output) ;
			# $add_form_fields    .= qq~</div>~ ;
			$event_length_rows{1}    .= qq~
			</div>~ if $event_length_rows{1} ;			
					
			$event_length_rows{2}    .= qq~
			</div>~ if $event_length_rows{2} ;
			# $add_form_fields .= &common_min_form_input('qty',$db{$table}{$id}{qty},'',1) ;
			# $add_form_fields .= qq~</div>~ ;
			# $add_form_fields    .= qq~</div>~ ;
		
		} else {
			
			$fcol = 1 ;
			# $add_form_fields	.= qq~<div class='row'>~ ;
			# $add_form_fields	.= &common_min_form_radio_col('event_length',$db{$table}{$id}{event_length},'',10,$final_day) ;
			
			$event_length_rows{1} .= &common_min_form_radio_col('event_length',$db{$table}{$id}{event_length},'',10,$final_day,"",1) ;
			my $final_day_second = $final_day - 10 ; 
			$final_day_second = -1 if $final_day_second <= 0 ; 
			$event_length_rows{2} .= &common_min_form_radio_col('event_length',$db{$table}{$id}{event_length},'',10,$final_day_second,"radio_row_id_2",2) ;
			
			# my $sec = &common_min_form_radio_col('event_length',$default_event_length_2,'',10,$final_day_second) ;
			
			# $add_form_fields    .= qq~</div>~ ;
			$fcol = 3 ;
			# $add_form_fields .= &common_min_form_input('qty',$db{$table}{$id}{qty},'',1) ;
			# $add_form_fields .= qq~</div>~ ;			
			if ($glod_user_level < 4) {
				
				$changed_opid_as_level_3 = 1 ; 
				
				my @excl_def 		= split(/\;/,$db{event_quotes_min}{$id}{excl_workings_event}) ;
				my @saved_des 		= split(/\;/,$db{event_quotes_min}{$id}{description_workings_event}) ;
				my @saved_op 		= split(/\;/,$db{event_quotes_min}{$id}{operator_workings_event}) ;
				my @remarks_def 	= split(/\;/,$db{event_quotes_min}{$id}{remarks_workings_event}) ;
				my @supp_def 		= split(/\;/,$db{event_quotes_min}{$id}{supplier_workings_event}) ;
				my @ref_nr_def 		= split(/\;/,$db{event_quotes_min}{$id}{ref_nr_workings_event}) ;
				my @qty_old 		= split(/\;/,$db{event_quotes_min}{$id}{qty_workings_event});
				my @amount_usd_old 	= split(/\;/,$db{event_quotes_min}{$id}{amount_usd_workings_event});
				my @amount_old 		= split(/\;/,$db{event_quotes_min}{$id}{amount_workings_event});			
				my @vat_default 	= split(/\;/,$db{event_quotes_min}{$id}{vat_workings_event}) ;
				
				my %expenses_html = () ; 

				&db_min_ro('users','id',"user_type LIKE 'casual%' AND block <> 1 AND inactive <> 1",'','') ;
				
				foreach (keys %{$db{users}}) {
					$opts{users} .= qq~<option value='$_'>$_</option>~ ; 
				}
				$opts{description} = qq~<option value='14'>14</option>~ ; 
				
				$add_form_fields .= qq~<div style="display:none;">~ ; 
				$db{$table}{$id}{roe} = 1 unless $db{$table}{$id}{roe} ;
				
				my $custom_set_checked = ($db{$table}{$id}{custom_set}) ? "CHECKED" : "" ;
				
				$add_form_fields .= qq~
					<select name="currency" id="selectCurrency"><option value=""></option>$opts{currency}</select>
					<input type="text" name="roe" id="inputRoe" value="$db{$table}{$id}{roe}">
					<input type="checkbox" name="custom_set" id="checkboxCustom_set" value="1" $custom_set_checked>
					<input type="text" name="sub_total_workings_event" id="inputSub_total_workings_event" value="$db{$table}{$id}{total_amount_workings_event}">
					<input type="text" name="vat_total_workings_event" id="inputVat_total_workings_event" value="$db{$table}{$id}{total_vat_amount_workings_event}">
					<input type="text" name="grand_total_workings_event" id="inputGrand_total_workings_event" value="$db{$table}{$id}{total_grand_amount_workings_event}">
				~ ;
				
				for (1 .. 50) { 
					if ($qty_old[50-$_] || $amount_usd_old[50-$_] || $amount_old[50-$_] || $saved_des[50-$_] || $saved_op[50-$_]) {
						$cnt_hidden_rows = 51-$_ ; 
						last ;
					}
				}
				
				my @ttlarr = ("sub_total_workings_event","vat_total_workings_event","grand_total_workings_event") ;
				
				if ($cnt_hidden_rows) { 
					
					# &db_min_ro('event_cost_items','id','','','') ;
					&db_min_ro('event_cost_items','id',"excl_from_expenses < 1",'','') ;
					
					for my $row_cnt (1 .. $cnt_hidden_rows) {
						
						foreach (keys %{$db{event_cost_items}}) {
							my $sel = ($saved_des[$row_cnt-1] and $saved_des[$row_cnt-1] eq $_) ? "SELECTED" : "" ;
							$opts{"description_$row_cnt"} .= qq~<option value="$_" $sel>$_</option>~ ; 
						}
						foreach (keys %{$db{users}}) {
							my $sel = ($saved_op[$row_cnt-1] and $saved_op[$row_cnt-1] eq $_) ? "SELECTED" : "" ;
							$opts{"users_$row_cnt"} .= qq~<option value="$_" $sel>$_</option>~ ; 
						}	
						
						$vat_default_for_hidden_expenses .= qq~$row_cnt:"$vat_default[$row_cnt-1],"~ ;
						 
						$add_form_fields .= qq~<div id="hidden_expenses_$row_cnt">~ ; 
						my $excl_checked = ($excl_def[$row_cnt-1]) ? "CHECKED" : "" ;
						my $vat_checked = ($vat_default[$row_cnt-1]) ? "CHECKED" : "" ;
						
						$add_form_fields .= qq~<input type="checkbox" name="excl_workings_event_$row_cnt" id="checkboxExcl_workings_event_$row_cnt" value="1" $excl_checked>~ ; 
						$add_form_fields .= qq~<select name="description_workings_event_$row_cnt" id="selectDescription_workings_event_$row_cnt"><option value=""></option>$opts{"description_$row_cnt"}</select>~ ; 
						$add_form_fields .= qq~<select name="operator_workings_event_$row_cnt" id="selectOperator_workings_event_$row_cnt"><option value=""></option>$opts{"users_$row_cnt"}</select>~ ; 			
						$add_form_fields .= qq~<input type="text" name="supplier_workings_event_$row_cnt" id="inputSupplier_workings_event_$row_cnt" value="$supp_def[$row_cnt-1]">~ ; 
						$add_form_fields .= qq~<input type="text" name="remarks_workings_event_$row_cnt" id="inputRemarks_workings_event_$row_cnt" value="$remarks_def[$row_cnt-1]">~ ; 
						$add_form_fields .= qq~<input type="text" name="ref_nr_workings_event_$row_cnt" id="inputRef_nr_workings_event_$row_cnt" value="$ref_nr_def[$row_cnt-1]">~ ; 
						$add_form_fields .= qq~<input type="text" name="qty_workings_event_$row_cnt" id="inputQty_workings_event_$row_cnt" value="$qty_old[$row_cnt-1]">~ ; 
						$add_form_fields .= qq~<input type="text" name="amount_usd_workings_event_$row_cnt" id="inputAmount_usd_workings_event_$row_cnt" value="$amount_usd_old[$row_cnt-1]">~ ; 
						$add_form_fields .= qq~<input type="text" name="amount_ttl_workings_event_$row_cnt" id="inputAmount_ttl_workings_event_$row_cnt" value="$amount_old[$row_cnt-1]">~ ;
						$add_form_fields .= qq~<input type="checkbox" name="vat_workings_event_$row_cnt" id="checkboxVat_workings_event_$row_cnt" value="1" $vat_checked>~ ;
						
						$add_form_fields .= qq~</div>~ ; 
						
						last if $cnt_hidden_rows == $row_cnt ; 
					} 
					
					$add_form_fields .= qq~<div id="hidden_expenses_totals">~ ; 
					foreach (@ttlarr) {
						my $inp_name = "input" . ucfirst $_ ; 
						$add_form_fields .= qq~<input type='text' name='$_' id='$inp_name' value='$db{$table}{$id}{$_}'>~ ;
					}
					$add_form_fields .= qq~</div>~ ;
					# chop $vat_default_for_hidden_expenses if $vat_default_for_hidden_expenses ;
				} 
				
				for ($cnt_hidden_rows+1 .. 50) { 
					# next if $cnt_hidden_rows && $_ <= $cnt_hidden_rows ;
					$add_form_fields .= qq~<div id="hidden_expenses_$_"></div>~ ; 
					# $expenses_html{$_} = qq~$_:"<input type='checkbox' name='excl_$_\_workings_event' id='checkboxExcl_$_\_workings_event' value='1'><select name='description_$_\_workings_event' id='selectDescription_$_\_workings_event'><option value=''></option>$opts{description}</select><select name='operator_$_\_workings_event' id='selectOperator_$_\_workings_event'><option value=''></option>$opts{users}</select><input type='text' name='qty_$_\_workings_event' id='inputQty_$_\_workings_event' value=''><input type='text' name='amount_usd_$_\_workings_event' id='inputAmount_usd_$_\_workings_event' value=''><input type='text' name='amount_$_\_workings_event' id='inputAmount_$_\_workings_event' value=''><input type='checkbox' name='vat_$_\_workings_event' id='checkboxVat_$_\_workings_event' value='1'>"~ ; 
				}
				
				$add_form_fields .= qq~<div id="hidden_expenses_totals"></div>~ ; 
				$add_form_fields .= qq~</div>~ ; 
				 
				foreach (@ttlarr) {
					my $inp_name = "input" . ucfirst $_ ; 
					$expenses_html{1000} .= qq~<input type='text' name='$_' id='$inp_name' value=''>~ ;
				}
				$expenses_html{1000} = qq~1000:"$expenses_html{1000}"~ ; 
				
				foreach (sort keys %expenses_html) {
					$expenses_html_for_javascript .= qq~$expenses_html{$_},~ ;
				}
				chop $expenses_html_for_javascript if $expenses_html_for_javascript ;

			} 
		}
		
		$add_form_fields .= qq~$time_label_header_row{1}</div>~ ;	
		$add_form_fields .= qq~<div class="row" id="start_times_row_1" $hide_rows_1><div class='fieldname col-md-$lcol'><label class='control-label'>Start Time</label></div>$days_time_row{1}</div>~ ; 
		$add_form_fields .= qq~<div class="row" id="end_times_row_1" $hide_rows_1><div class='fieldname col-md-$lcol'><label class='control-label'>End Time</label></div>$days_time_row_2{1}</div>~ ; 		
		$add_form_fields .= qq~$event_length_rows{1}~ ; 		
		
		$add_form_fields .= qq~$time_label_header_row{2}</div>~ ;	
		$add_form_fields .= qq~<div class="row" id="start_times_row_2" $hide_rows_2><div class='fieldname col-md-$lcol'><label class='control-label'>Start Time</label></div>$days_time_row{2}</div>~ ; 
		$add_form_fields .= qq~<div class="row" id="end_times_row_2" $hide_rows_2><div class='fieldname col-md-$lcol'><label class='control-label'>End Time</label></div>$days_time_row_2{2}</div>~ ; 
		$add_form_fields .= qq~$event_length_rows{2}~ ; 

		$add_form_fields .= &common_min_form_input('qty',$db{$table}{$id}{qty},'',1) ;
		$add_form_fields .= qq~</div>~ ;
		
		$db{$table}{$id}{nr_of_systems} = 0 unless $db{$table}{$id}{nr_of_systems} ; 
		
		$preferred_title{nr_of_systems} = "Number of Systems" ;
		
		# if ($glod_user_level > 3) {
		# if ($glod_user_level > 2) {

		if ($glod_user_level > 3 || (!$quote_completed && $glod_user_level == 3)) {
			$preferred_title{type} = "Type of System" ;
			$add_form_fields .= &common_min_form_select('type',$db{$table}{$id}{type},'') ;
			$trigger_jquery .= qq~\$("#selectType\_chosen").css( "width", "100%" );~ ;
			$add_form_fields .= &common_min_form_input('nr_of_systems',$db{$table}{$id}{nr_of_systems},'') ;
			$preferred_title{type_details_id} = "Type Details" ;
			$add_form_fields .= &common_min_form_select('type_details_id',$db{$table}{$id}{type_details_id},'') ;
			$trigger_jquery .= qq~\$("#selectType_details_id\_chosen").css("width","100%") ;~ ;
		# } elsif ($glod_user_level < 4 && ($quote_accepted || $quote_rejected || $quote_cancelled)) {
		} 
		# elsif ($glod_user_level < 3 && ($quote_accepted || $quote_rejected || $quote_cancelled)) {
		elsif ((($quote_accepted || $quote_rejected || $quote_cancelled) && $glod_user_level < 3) || ($quote_completed && $glod_user_level <= 3)) {
			&db_min_ro('event_types','id,name',"`id`='$db{$table}{$id}{type}'",'','') if $db{$table}{$id}{type} ;
			$readonly{type_of_system} = "READONLY" ; $readonly{system_details} = "READONLY" ; $readonly{system_name} = "READONLY" ; $readonly{club_name} = "READONLY" ;
			$preferred_title{type_of_system} = "Type of System" ;
			$add_form_fields .= &common_min_form_input('type_of_system',$db{event_types}{$db{$table}{$id}{type}}{name},'') ;
			$add_form_fields .= &common_min_form_input('nr_of_systems',$db{$table}{$id}{nr_of_systems},'') ;
			
			&db_min_ro('event_type_details','id,name',"`id`='$db{$table}{$id}{type_details_id}'",'','') unless $db{$table}{$id}{type_details_id} =~ /;/ ;
			&db_min_ro('event_type_details','id,name',"",'','') if $db{$table}{$id}{type_details_id} =~ /;/ ;
			my $input_val = qq~~ ;
			foreach my $type_id (split(",",$db{$table}{$id}{type_details_id})) {
				next unless $db{event_type_details}{$type_id}{name} ;
				$input_val .= qq~$db{event_type_details}{$type_id}{name}, ~ ;		
			}
			chop $input_val if $input_val ; chop $input_val if $input_val ; 
			$add_form_fields .= &common_min_form_input('system_details',$input_val,'') ;			
		} else {
			$preferred_title{type} = "Type of System" ;
			$add_form_fields .= &common_min_form_select('type',$db{$table}{$id}{type},'') ;
			$trigger_jquery .= qq~\$("#selectType\_chosen").css("width","100%" );~ ;
			$add_form_fields .= &common_min_form_input('nr_of_systems',$db{$table}{$id}{nr_of_systems},'') ;
		}
		
		our $remaining_op_ids = qq~~ ;
		
		&build_system_name_form($db{$table}{$id}{event_system_id_multiple},$db{$table}{$id}{club_ids},$quote_accepted,$quote_rejected,$quote_cancelled,$db{$table}{$id}{daily_operator_ids},$db{$table}{$id}{days_active},$db{$table}{$id}{quote_completed}) ;
		
		my $hide_club_name1 = qq~~ ; my $hide_club_name2 = qq~~ ; my $required_fields_full = 0 ; 
		
		$required_fields_full = 1 if $db{$table}{$id}{ref} and $db{$table}{$id}{type} and $db{$table}{$id}{event_system_id}
		and $db{$table}{$id}{qty} and $db{$table}{$id}{region_id} and $db{$table}{$id}{city_id} ;
		
		$add_form_fields .= qq~<div class="row"><div class="col-md-12"></div></div>~ ;
		
		$fcol = 3 ;
		
		# # if ($glod_user_level > 3) {
		# if ($glod_user_level > 2) {
			# $dlg{operator_ids} .= qq~<div class="col-md-1"><a href="javascript:dlgMdl('$useropts{'scripts'}/dialog/add_operator.pl?add&selectOperator_ids','Add Operator','','medium-dialog');" class="btn btn-primary btn-xs" style="padding:1px 2px 1px 3px;margin-top:2px;" title='Add Operator' data-toggle='tooltip' data-placement='right'><i class="glyphicon glyphicon-plus" style="padding:0;margin-top:2px;top:-2px;"></i></a></div>~ ; 
			# $preferred_title{operator_ids} = "Operator(s)" ;
			# $add_form_fields .= &common_min_form_select('operator_ids',$remaining_op_ids,'') ;
			# $trigger_jquery .= qq~\$("#selectOperator_ids\_chosen").css( "width", "100%" );~ ;
		# # } elsif ($glod_user_level < 4 && ($quote_accepted || $quote_rejected || $quote_cancelled)) {		
		# } 
		
		# if ($glod_user_level < 3 && ($quote_accepted || $quote_rejected || $quote_cancelled)) {		
		if ((($quote_accepted || $quote_rejected || $quote_cancelled) && $glod_user_level < 3) || ($quote_completed && $glod_user_level <= 3)) {		
			my @op_names = map { $db{users}{$_}{name} } @multi_select_op_ids;
			$op_names = join(", ",@op_names) ;
			$preferred_title{operators} = "Operator(s)" ; $readonly{operators} = "READONLY" ;
			$add_form_fields .= &common_min_form_input('operators',$op_names,'') ;
		} else {
			
			if ($glod_user_level > 2) {
				$dlg{operator_ids} .= qq~<div class="col-md-1"><a href="javascript:dlgMdl('$useropts{'scripts'}/dialog/add_operator.pl?add&selectOperator_ids','Add Operator','','medium-dialog');" class="btn btn-primary btn-xs" style="padding:0px 2px 0px 3px;margin-top:0px;" title='Add Operator' data-toggle='tooltip' data-placement='right'><i class="glyphicon glyphicon-plus" style="padding:0;margin-top:0px;top:0px;"></i></a></div>~ ; 
				$preferred_title{operator_ids} = "Operator(s)" ;
				$trigger_jquery .= qq~\$("#selectOperator_ids\_chosen").css( "width", "100%" );~ ;			
			}
			
			$add_form_fields .= qq~<div style="display:none;">~ if $glod_user_level < 3 ; 
			$add_form_fields .= &common_min_form_select('operator_ids',$remaining_op_ids,'') ;
			$add_form_fields .= qq~</div>~ if $glod_user_level < 3 ; 
		# } elsif ($glod_user_level < 4 && ($quote_accepted || $quote_rejected || $quote_cancelled)) {	
		}
			
		my @casuals = () ;
		
		foreach (keys %{$db{events}}) { 
			$qnr = $db{events}{$_}{event_quote_nr} ; 
			$uid = $db{events}{$_}{user_id} ; 	
			next unless $db{events}{$_}{category} eq 'available' ; 
			push @casuals, "$casual_name{$uid} [$casual_type{$uid}]" ;
		}

		$add_form_fields .= &common_min_form_input('casual_worker','A','hidden')   ; 
		$add_form_fields .= &common_min_form_input('casual_worker_2','B','hidden') ;
		
		my $nr_of_casuals = 0 + $db{$table}{$id}{nr_of_casuals} + $db{$table}{$id}{nr_of_casuals_2} ;
		
		$add_form_fields .= &common_min_form_input('nr_of_casuals',$nr_of_casuals,'hidden') ;
		$add_form_fields .= &common_min_form_input('nr_of_casuals_2',0,'hidden') ;     
		
		$fcol = 3 ;
		# $add_form_fields   .= &common_min_form_input('sports_type',$db{$table}{$id}{sports_type},'') ;
		
		if ($hide_select_1) {	
			$add_form_fields       .= qq~$readonly_select~ ;		
		}
		
		$preferred_title{sport_type_ids} = "Sport Type(s)" ; $preferred_title{organisation_ids} = "Venue(s)" ;
		$add_form_fields	       .= qq~$hide_select_1~ ;
		$add_form_fields	       .= &common_min_form_select('sport_type_ids',$db{$table}{$id}{sport_type_ids},'') ;
		# $add_form_fields         .= &common_min_form_input('ligr',$db{$table}{$id}{ligr},'') ;
		# if ($db{$table}{$id}{ligr} eq 'Yes')  { $checked = 'CHECKED'; } else { $checked = ''; }
		# $add_form_fields         .= &common_min_form_checkbox('ligr',$db{$table}{$id}{ligr},$checked) ;
		# $add_form_fields         .= &common_min_form_input('Location',$db{$table}{$id}{location},'') ;
		$add_form_fields	       .= &common_min_form_select('region_id',$db{$table}{$id}{region_id},'') ;

		$add_form_fields	       .= &common_min_form_select('city_id',$db{$table}{$id}{city_id},'') ;
		$add_form_fields	       .= &common_min_form_select('organisation_ids',$db{$table}{$id}{organisation_ids},'') ;
		
		$allow_deselect{linear_id}  = 1 ; 
		my %linear_def              = () ; $linear_def{$db{$table}{$id}{linear_id}} = 'SELECTED' ;
		$opts{linear_id}           .= qq~<option value="1" $linear_def{1}>Yes</option><option value="2" $linear_def{2}>No</option><option value="3" $linear_def{3}>Delayed</option>~ ;
		$add_form_fields	       .= &common_min_form_select('linear_id',$db{$table}{$id}{linear_id},'') ;
		
		# $supplier_type_ids
		
		$preferred_title{supplier_type_ids} = "Supplier Type(s)" ;
		$add_form_fields	       .= qq~<input style="display:none;" value="" name="suppliertypeids" id="supplier-type-ids">~ . &common_min_form_select('supplier_type_ids',$db{$table}{$id}{supplier_type_ids},'') if $db{$table}{$id}{sss_quote_nr} ;
		
		if ($hide_select_1) {
			$add_form_fields       .= qq~</div>~ ;		
		}
		
		# $opts{event_length} = qq~~ ; 
		# $radio_opts{event_length} = "Full Day+Half Day" ;
		# $fcol = 1 ;
		# $add_form_fields	.= qq~<div class='row'>~ ;
		# $add_form_fields	.= &common_min_form_radio_col('event_length',$db{$table}{$id}{event_length},'') ;
		# $add_form_fields    .= qq~</div>~ ;
		# $fcol = 6 ;
		# $add_form_fields	.= qq~$hide_select_2~ ;
	
		# $ignore{club} = 1 ;
		# $add_form_fields        .= &common_min_form_input('club','','') ;
		# $add_form_fields      .= &common_min_form_input('City',$db{$table}{$id}{city},'') ;
		
		$add_form_fields .= qq~</div></div>~ ; 
		# $add_form_fields .= qq~<div class='row'><div class='col-md-4'>~ ; 
		# $add_form_fields .= qq~</div></div>~ ; 
		
		# $operator_options = qq~<option value='1'>Hello</option>~ ; 
		
		$trigger_jquery_raw .= qq~
		
			// // // \$("input[name='event_length']").change( function () {
				
				// // // let radio_sel = \$(this).val() ; 
				
				// // // for (let i = 1; i<=50; i++) {
					
					// // // let usd = \$("#inputAmount_usd_workings_event_"+i) ;
					// // // let item = \$("#selectDescription_workings_event_"+i) ;
					
					// // // let amnt = 0 ; 
					// // // if (\$('input[name="event_length"][value="full_day"]').is(":checked") && usd.val() == '500.00' && item.val() == 14) {
						// // // amnt = 750 ;  
					// // // } else if (\$('input[name="event_length"][value="half_day"]').is(":checked") && usd.val() == '750.00' && item.val() == 14) {
						// // // amnt = 500 ; 
					// // // } else {
						// // // continue ;
					// // // }
					
					// // // let qty = \$("#inputQty_workings_event_"+i) ;	
					// // // let conv = \$("#inputAmount_ttl_workings_event_"+i) ;
					
					// // // usd.val(amnt.toFixed(2)) ;
					// // // let total_amnt = qty.val()*amnt ;
					// // // conv.val(total_amnt.toFixed(2)) ;
				// // // }
				// // // if ($changed_opid_as_level_3) { 
					// // // calc_hidden_totals() ; 
				// // // } else {
					// // // calcAllWorkings() ;
				// // // } 
			// // // }) ;
		
			\$("#selectRegion_id").change(function() {
				
				var vid = \$("#selectCity_id")  ;
				
				var selected_city = \$("#selectCity_id").chosen().val() ;
				selected_city = String(selected_city);
				let splittedNumbers = selected_city.split(',') ;
				
				vid.empty(); // remove old options
				vid.append("<option value='0'></option>");
				vid.trigger("chosen:updated");
				var url = "$useropts{scripts}/get/get_cities_from_region.pl?region_id=" + \$(this).val() ;
				
				\$.get(url, function(json) {
					\$.each(json, function(key, data) {   		
						if (splittedNumbers.includes(data.id)) {
							vid.append("<option value='"+data.id+"' selected>"+data.city_name+"</option>");
						} else {
							vid.append("<option value='"+data.id+"'>"+data.city_name+"</option>");
						}	
						vid.trigger("chosen:updated");
					});
				},
				'json');
				
				var vid2 = \$("#selectOrganisation_ids")  ;
				
				var selected_venues = \$("#selectOrganisation_ids").chosen().val() ;
				selected_venues = String(selected_venues);
				let splittedNumbers2 = selected_venues.split(',') ;
				
				vid2.empty(); // remove old options
				vid2.append("<option value='0'></option>");
				vid2.trigger("chosen:updated");
				url = "$useropts{scripts}/get/get_venues_from_region.pl?region_id=" + \$(this).val() ;
				
				\$.get(url, function(json) {
					\$.each(json, function(key, data) {   		
						if (splittedNumbers2.includes(data.id)) {
							vid2.append("<option value='"+data.id+"' selected>"+data.venue_name+"</option>");
						} else {
							vid2.append("<option value='"+data.id+"'>"+data.venue_name+"</option>");
						}
						vid2.trigger("chosen:updated");
					});
				},
				'json');
				
			});
			
 
			let prev_date_part_from = \$("input[name='date_from']").val() ; 
			prev_date_part_from = prev_date_part_from.split(' ')[0] ;
			let prev_date_part_to = \$("input[name='date_to']").val() ; 
			prev_date_part_to = prev_date_part_to.split(' ')[0] ;
			
			\$('#datetimepickerDate_from,#datetimepickerDate_to').on('changeDate', function(e) {
				let from_to = get_row_nr(this.id) ;
				// saved_time_from["1"] = $("input[name='date_from']").val() ; 
				if (this.id == "datetimepickerDate_from") {
					let date_new = \$("input[name='date_from']").val() ; 
					let time_part = date_new.split(' ')[1]; 
					\$("input[name='time_from_1']").val(time_part) ;  
				}
				calc_date_diff_in_days(this.id) ; 	
				// \$("#day_4_name_1_extra_1").css("display","none") ;		
			}) ;
			
			// function javascript_sprintf_sort_off (num) {
				// if (num < 10) {
					// num = "0"+num.toString() ;
				// }
				// return num ; 
			// }



			// // function move_system_operators_drop_downs_between_short_to_long () {
				
				// // let cnt_active_days = 0 ; let cnt_dates_between = 0 ; 
				// // for (let col=1; col<=20; col++) {
					// // if (\$("#day_"+col).val()) {
						// // cnt_active_days++ ; cnt_dates_between = col ; 
					// // }
				// // }
				// // if ((cnt_active_days <= 6 && \$("#day_1_1").html()) || (cnt_active_days <= 20 && cnt_active_days > 6 && \$("#day_1_1_1").html())) {
					// // return ; 
				// // }
				
				// // if (cnt_active_days && &cnt_active_days <= 6 && !\$("#day_1_1").html() && \$("#day_1_1_extra_1").html()) {
					// // for (let row=1; row<=15; col++) {
						// // if (!\$("input[name='clicked_button_"+row+"']").val()) { continue ; }
						
						// // \$("#system_name_row_"+row+"_extra_1").css("display","none") ; 
						// // \$("#system_name_row_"+row+"_extra_2").css("display","none") ; 
						// // \$("day_names_extra_1").css("display","none") ; 
						// // \$("day_names_extra_2").css("display","none") ; 
						
						// // for (let col=1; col<=20; col++) {
							// // if (\$("#day_"+col).val()) {
								// // \$("#day_"+row+"_"+col).append(\$("#day_"+row+"_"+col+"_extra").contents());
								// // \$("#day_"+row+"_"+col).css("display","") ; 
							// // }
						// // }
					// // }
					 
					 // // // \$("#2").append(\$("#1").contents());
				// // } else if (cnt_active_days > 6 && \$("#day_1_1").html() && !\$("#day_1_1_1").html()) {
					// // for (let row=1; row<=15; col++) {
						// // if (!\$("input[name='clicked_button_"+row+"']").val()) { continue ; }
						
						// // if (\$("#system_name_row_"+row+"_1").is(":hidden")) {
							// // (\$("#system_name_row_"+row+"_1").css("display","") ;
						// // }
						// // if (\$("#system_name_row_"+row+"_2").is(":hidden") && cnt_active_days > 10) {
							// // (\$("#system_name_row_"+row+"_2").css("display","") ;
						// // }
						// // let active_day_counter = 0 ; 
						// // for (let col=1; col<=20; col++) {
							// // if (\$("#day_"+col).val()) {
								// // active_day_counter++ ;
								// // \$("#day_"+row+"_"+active_day_counter+"_extra").append(\$("#day_"+row+"_"+col+"").contents());
							// // }
						// // }
					// // }					
				// // }
				
			// // }

			function calc_date_diff_in_days(from_to) {
				
				// console.log("from_to : "+from_to) ;
				
				let dateFromInput		= \$("input[name='date_from']").val();
				// const dateToInput   	= \$("#datetimepickerDate_to").data("date");
				let dateToInput   		= \$("input[name='date_to']").val() ;
				
				let date_part_from = dateFromInput.split(' ')[0]; 
				let date_part_to = dateToInput.split(' ')[0]; 

				let time_part_from 	= dateFromInput.split(' ')[1]; 
				let time_part_to 	= dateToInput.split(' ')[1];
				
				if (prev_date_part_from == date_part_from && date_part_to == prev_date_part_to) {
					
					changed_radio_cnt = 0 ; 
					
					const datesBetween = getDatesBetween(dateFromInput,dateToInput,0) ; let date_counter = 0 ; 
					
					for (const date of datesBetween) {
						
						date_counter++ ; 
						 
						if (date_counter > 20) { date_counter = 20 ; break ; }
						
						if (((!saved_time_to[date_counter] || datesBetween.length == date_counter) && from_to.includes("Date_to")) || (date_counter > 1 && !saved_time_from[date_counter] && from_to.includes("Date_from"))) {
							
							if (from_to.includes("Date_from")) {
								\$("input[name='time_from_"+date_counter+"']").val(time_part_from) ;  		
							} else {
								\$("input[name='time_to_"+date_counter+"']").val(time_part_to) ;  	
							}
							
							set_full_half_day('',date_counter) ; 	
							if ((nr_days["full"] && !prev_nr_days["full"]) || (nr_days["half"] && !prev_nr_days["half"]) || (!nr_days["full"] && prev_nr_days["full"]) || (!nr_days["half"] && prev_nr_days["half"])) {
								update_qty_in_quote_tab_when_changing_nr_of_full_or_half_days() ;
								changed_radio_cnt = 0 ;  
							}
							prev_nr_days["full"] = nr_days["full"] ;
							prev_nr_days["half"] = nr_days["half"] ;
							
						}
					}
					
					// if (saved_time_to[date_counter] && from_to.includes("Date_to")) {
						// \$("input[name='time_to_"+date_counter+"']").val(time_part_to) ;  		
					// }	
					
					if (from_to.includes("Date_to")) {
						saved_time_to[date_counter] = time_part_to ; 
					} else {
						saved_time_from["1"] = time_part_from ; 
					}
					
					// if (from_to.includes("Date_from")) {
						// \$("input[name='time_from_1']").val(time_part_from) ; 
						// set_full_half_day('',1) ;
					// } else {
						// let diffInDays 	= 1 + (new Date(date_part_to) - new Date(date_part_from)) / (1000 * 60 * 60 * 24) ; 
						// \$("input[name='time_to_"+diffInDays+"']").val(time_part_to) ; 
						// set_full_half_day('',diffInDays) ;
					// }
					
					if (changed_radio_cnt) {
						update_qty_in_quote_tab_when_changing_nr_of_full_or_half_days() ;
					}		
					return ; 
				}
				
				prev_date_part_from = date_part_from ;
				prev_date_part_to = date_part_to ;
				
				const date1	= new Date(dateFromInput) ;
				const date2	= new Date(dateToInput) ;
				
				let hide_date_names = "#1" ;
				
				let glod_user_level = parseInt("$glod_user_level") ;
 
				let y1 = date1.getFullYear() ; let m1 = date1.getMonth() ; let d1 = date1.getDate() ; let y2 = date2.getFullYear() ; let m2 = date2.getMonth() ; let d2 = date2.getDate() ;
 
				m1 = m1 + 1 ;
				if (m1 < 10) {
					m1 = "0"+m1.toString() ;
				}
				m2 = m2 + 1 ; 
				if (m2 < 10) {
					m2 = "0"+m2.toString() ;
				}
 
				if ((y1 == y2 && m1 == m2 && d1 == d2) || date1 > date2) {
					
					if (date1 > date2 && from_to == "datetimepickerDate_from") {
						\$("#datetimepickerDate_to").datetimepicker("setDate",date_part_from+" "+time_part_to) ;				
						\$("#day_1_name label.control-label").text(date_part_from) ;
					} else if (date1 > date2 && from_to == "datetimepickerDate_to") {
						\$("#datetimepickerDate_from").datetimepicker("setDate",date_part_to+" "+time_part_from) ;				
						\$("#day_1_name label.control-label").text(date_part_to) ;
					}
					
					\$("input[name='time_from_1']").val(time_part_from) ; 
					\$("input[name='time_to_1']").val(time_part_to) ; 
					
					changed_radio_cnt = 0 ; 
					
					set_full_half_day('',1) ; 
					
					\$("#radio_1_1").css("display","");
					\$("#radio_1_2").css("display","");	
					
					\$("#radio_row_id_2_1").hide() ;  
					\$("#radio_row_id_2_2").hide() ;  
					
					if (!\$("#days_drop_downs_1").is(":hidden")) { 
						\$("#start_times_row_1").hide() ;  
						\$("#end_times_row_1").hide() ;  
						\$("#days_drop_downs_1").hide() ; 
					}
					
					if (!\$("#days_drop_downs_2").is(":hidden")) {
						\$("#start_times_row_2").hide() ;  
						\$("#end_times_row_2").hide() ;  
						\$("#days_drop_downs_2").hide() ;  
					}
					
					let reduced_nr_of_days = 0 ; 
					
					for (let i=2; i<=20; i++) {	
						\$("#day_"+i+"_chosen").css("display","none") ;
						
						if (\$("#day_"+i).val()) {
							\$("#day_"+i).val("") ;
							\$("#day_"+i).trigger("chosen:updated") ; 
							\$("#radio_"+i+"_1").css("display","none") ;
							\$("#radio_"+i+"_2").css("display","none") ;
							reduced_nr_of_days = 1 ; 
						}
					}
					
					if (changed_radio_cnt || reduced_nr_of_days) {
						update_qty_in_quote_tab_when_changing_nr_of_full_or_half_days() ;
					}
					
					if (!\$("#day_1").val()) {
						\$("#day_1").val("1") ;
						\$("#day_1").trigger("chosen:updated") ; 
					}
					\$("#inputQty").val("1") ;
					 
					if (\$("#clicked_any_button").text()) {
						
						// if (!\$("#days_drop_downs_1").is(":hidden")) {
							// \$("#days_drop_downs_1").css("display","none") ; 
							// \$("#start_times_row_1").css("display","none") ; 
							// \$("#end_times_row_1").css("display","none") ;					
						// }

						// if (!\$("#days_drop_downs_2").is(":hidden")) {
							// \$("#days_drop_downs_2").css("display","none") ; 
							// \$("#start_times_row_2").css("display","none") ; 
							// \$("#end_times_row_2").css("display","none") ;
						// }
						// if (\$("#day_1_1").is(":hidden")) {
							// \$("#day_1_1").removeClass('col-md-0').addClass('col-md-1') ;		
							// \$("#day_1_1").css("display","") ; 						
						// }

						// if (!\$("#day_1_1").html()) {
							// let select_1_1_raw =  add_select_raw (1,"custom_operator_1_1","selectCustom_operator_1_1","Select an Option","$operator_options",-1,100) ;
							// \$("#day_1_1").html(select_1_1_raw) ;
						// }
						
						add_operator_dropdown("day_1_name","day_1_1","custom_operator_1_1","selectCustom_operator_1_1",y1+"-"+m1+"-"+d1,1,0) ; 
						
						for (let a=1;a<=15;a++) {
							if (!\$("input[name='clicked_button_"+a+"']").val()) { continue ; }
							
							if (a > 1 && !\$("#extra_heading_"+a).is(":hidden")) {
								\$("#extra_heading_"+a).hide() ; 
							} 
							
							if (!\$("#day_names_"+a+"_extra_1").is(":hidden")) {
								\$("#day_names_"+a+"_extra_1").css("display","none") ;
								\$("#system_name_row_"+a+"_extra_1").css("display","none") ;
							}
							if (!\$("#day_names_"+a+"_extra_2").is(":hidden")) {
								\$("#day_names_"+a+"_extra_2").css("display","none") ;
								\$("#system_name_row_"+a+"_extra_2").css("display","none") ;
							}
							
							// \$("#day_"+row+"_"+active_day_counter+"_extra").append(\$("#day_"+row+"_"+col+"").contents());
							
							\$("#day_1_name").css("display","") ;
							\$("#day_"+a+"_1").css("display","") ;
							
							for (let b=2;b<=20;b++) {
								if (!\$("#day_"+b+"_name").is(":hidden")) {
									\$("#day_"+b+"_name").css("display","none") ;
								}
								if (!\$("#day_"+a+"_"+b).is(":hidden")) {
									\$("#day_"+a+"_"+b).css("display","none") ;
								}
							}
						}
					}
					
					return ; 
				} 
				
				// if (\$("#clicked_any_button").text()) {
					
					// for (let a=1;a<=15;a++) {
						
						// if (!\$("input[name='clicked_button_"+a+"']").val()) { continue ; }
						
						// for (let b=1;b<=date_counter;b++) {
							// if (!\$("#day_"+b+"_name").is(":hidden")) {
								// \$("#day_"+a+"_"+b).css("display","") ;
								// \$("#selectCustom_operator_"+a+"_"+b+"_chosen").css("width","100%") ;
							// } else {
								// \$("#day_"+a+"_"+b).css("display","none") ;
							// }
						// } 
					// }
				// }
			
				
				if (\$("#days_drop_downs_1").is(":hidden")) {
					// if (glod_user_level > 3) {
					\$("#days_drop_downs_1").css("display","") ; 
					\$("#start_times_row_1").css("display","") ; 
					\$("#end_times_row_1").css("display","") ; 					
				}
 
				// if (date1 > date2 ) {
					
					// \$("#inputQty").val("0") ; 
					// for (let k=1;k<=6;k++) {	
						// \$("#day_"+k).val('') ; 
						// \$("#day_"+k).trigger("chosen:updated") ; 					
						// \$("input[name='time_from_"+k+"']").val('') ; 
						// \$("input[name='time_to_"+k+"']").val('') ; 
						// \$("#timepickerTime_from_"+k).css("display","none") ;
						// \$("#timepickerTime_to_"+k).css("display","none") ;
						// \$("#radio_"+k+"_1").css("display","none") ;
						// \$("#radio_"+k+"_2").css("display","none") ;
						// \$("#day_"+k+"_chosen").css("display","none") ; 
					// }
					
					// return ;
					
					
					// let date_part_from = dateFromInput.split(' ')[0]; 
					// \$("#datetimepickerDate_to").datetimepicker("setDate",date_part_from+" "+time_part_to) ;
					
					// \$("input[name='time_from_1']").val(time_part_from) ; 
					// \$("input[name='time_to_1']").val(time_part_to) ; 
					
					// let time_1 = timeStringToSeconds(time_part_from) ; 
					// let time_2 = timeStringToSeconds(time_part_to) ; 
				 
					// if (time_2 < time_1 || !\$("#day_1").val()) {
						// \$("#inputQty").val("0") ; 
					// } else {
						// \$("#inputQty").val("1") ; 
						// let time_diff_ = time_2 - time_1 ;
						// if (time_diff_ >= 18000) {
							// \$("#radio_1_1").prop('checked',true) ;
						// } else {
							// \$("#radio_1_2").prop('checked',true) ;
						// }
					// }
					
					// \$("#days_drop_downs").css("display","none") ; 
					// \$("#start_times_row").css("display","none") ; 
					// \$("#end_times_row").css("display","none") ; 	
					
					// for (let k=2;k<=6;k++) {	
						// if (\$("#day_"+k).val()) {
							// \$("#day_"+k).val('') ; 
							// \$("#day_"+k).trigger("chosen:updated") ; 					
							// \$("#radio_"+k+"_1").css("display","none") ;
							// \$("#radio_"+k+"_2").css("display","none") ;
						// }
					// }
					
					// return ;
				// }
				
				\$("input[name='time_from_1']").val(time_part_from) ;

				const datesBetween = getDatesBetween(dateFromInput,dateToInput,0) ;
				let date_counter = 0 ; let dates_count = 0 ; let count_days = 0 ; 
				
				let count_days_after = 0 ; let nr_of_active_days = 0 ; 
				
				// for (const date of datesBetween) {
					// date_counter++ ; dates_count++ ;
					// if (\$("#day_"+date_counter+"_chosen").is(":hidden")) {
						
						// \$("#radio_"+date_counter+"_1").css("display","");
						// \$("#radio_"+date_counter+"_2").css("display","");	
						// \$("#day_"+date_counter).val("1") ;
						// \$("#day_"+date_counter).trigger("chosen:updated") ;
						// \$("#day_"+date_counter+"_chosen").css("display","");
						// \$("#day_"+date_counter+"_chosen").css("width","100%");
						// \$("#day_"+date_counter).val('1') ; 
						// \$("#day_"+date_counter).trigger("chosen:updated") ; 
						// if (date_counter > 1) {
							// console.log("1 . display timepickerTime_from_"+date_counter) ;
							// \$("#timepickerTime_from_"+date_counter).css("display","");
						// }  
					// } 
					// if (\$("#day_"+date_counter).val()) { 
						// nr_of_active_days++ ;	
					// } 
				// }
				
				// date_counter = 0 ;
				
				let dates_map = {} ; 
				
				changed_radio_cnt = 0 ; let changed_nr_of_days = 0 ; 
				
				for (const date of datesBetween) {
					
					date_counter++ ; 
					
					dates_map[date_counter] = date ;
					
					// console.log("1 . date_counter : "+dates_map[date_counter]) ; 
					
					// if (date_counter > 6) { date_counter = 6 ; count_days_after++ ; continue ; }
					if (date_counter > 20) { date_counter = 20 ; break ; }
					if (date_counter > 1 && !saved_time_from[date_counter]) {
						\$("input[name='time_from_"+date_counter+"']").val(time_part_from) ;  		
					}
					if (!saved_time_to[date_counter]) {
						\$("input[name='time_to_"+date_counter+"']").val(time_part_to) ;  		
					}	
					
					set_full_half_day('',date_counter) ; 
 
					if (\$("#day_"+date_counter+"_chosen").is(":hidden")) {
						
						\$("#radio_"+date_counter+"_1").css("display","");
						\$("#radio_"+date_counter+"_2").css("display","");	
						\$("#day_"+date_counter).val("1") ;
						\$("#day_"+date_counter).trigger("chosen:updated") ;
						\$("#day_"+date_counter+"_chosen").css("display","");
						\$("#day_"+date_counter+"_chosen").css("width","100%");
						\$("#day_"+date_counter).val('1') ; 
						\$("#day_"+date_counter).trigger("chosen:updated") ; 
						if (date_counter > 1) {
							// console.log("1 . display timepickerTime_from_"+date_counter) ;
							\$("#timepickerTime_from_"+date_counter).css("display","");
						}  
						changed_nr_of_days = 1 ; 
					} 					
					
					if (\$("#day_"+date_counter).val()) {
						\$("#timepickerTime_to_"+date_counter).css("display","") ;
						nr_of_active_days++ ;	
					}
					
					\$("#day_"+date_counter+" option[value='1']").text(date);
					\$("#day_"+date_counter+" option[value='1']").trigger("chosen:updated") ;
	
					if (\$("#day_"+date_counter).val()) {
						count_days++ ;
					}
				}
				
				if (\$("#days_drop_downs_2").is(":hidden") && date_counter > 10) {
					// if (glod_user_level > 3) {
					\$("#days_drop_downs_2").css("display","") ; 
					\$("#start_times_row_2").css("display","") ; 
					\$("#end_times_row_2").css("display","") ; 					
					\$("#radio_row_id_2_1").css("display","") ; 					
					\$("#radio_row_id_2_2").css("display","") ; 					
				} else if (!\$("#days_drop_downs_2").is(":hidden") && date_counter <= 10) {
					\$("#days_drop_downs_2").css("display","none") ; 
					\$("#start_times_row_2").css("display","none") ; 
					\$("#end_times_row_2").css("display","none") ; 						
					\$("#radio_row_id_2_1").css("display","none") ; 						
					\$("#radio_row_id_2_2").css("display","none") ; 						
				}
				
				if (count_days_after) {
					
					date2.setDate(date2.getDate() - count_days_after);
					\$("#datetimepickerDate_to").datetimepicker("setDate",date2) ;
					
				}
				
				let select_raw ;
				
				// console.log("nr_of_active_days : "+nr_of_active_days) ;
				
				let select_chosen_ids_1 = '#1' ; 
				let select_chosen_ids_2 = '#2' ; 				
				let names_to_display = '#3' ; 
				
				let def_val = 0 ; 
				
				if (\$("#clicked_any_button").text()) {
					
					if (nr_of_active_days <= 6) {
						for (let b=1;b<=date_counter;b++) {	
							
							if (from_to == "datetimepickerDate_from") {
								\$("#day_"+b+"_name label.control-label").text('') ;
							}

							// if (!\$("#day_"+date_counter+"_name label.control-label").text()) {
							if (\$("#day_"+b+"_name").is(":hidden")) {
								\$("#day_"+b+"_name label.control-label").text(dates_map[b]) ;
								\$("#day_"+b+"_name").css("display","") ; 
								\$("#day_"+b+"_name").removeClass('col-md-0').addClass('col-md-1') ;		
							} 				
							
							if (!\$("#day_"+b).val() || !\$("#clicked_any_button").text()) {
								\$("#day_"+b+"_name").css("display","none") ; 
							}
						}
					}
					
					for (let a=1;a<=15;a++) {
						
						if (!\$("input[name='clicked_button_"+a+"']").val()) { 
							let prev_row_nr = a - 1 ; 
							if (!\$("extra_heading_"+a).html() && \$("input[name='clicked_button_"+a+"']").val() && prev_row_nr && nr_of_active_days > 6) {
								
								console.log("hello world Add Heading") ; 
								
								\$("#system_name_row_"+a).before("<div class='row' id='extra_heading_"+a+"'><div class='col-md-2'></div><div class='col-md-4'><div class='row'><div class='col-md-4'><label class='control-label'>&nbsp;&nbsp;System&nbsp;Name</label></div><div class='col-md-4'><label class='control-label'>&nbsp;&nbsp;Club&nbsp;Name</label></div><div class='col-md-4'><label class='control-label'>&nbsp;&nbsp;Operator&nbsp;Name</label></div></div></div></div>") ;							
							} else if (\$("extra_heading_"+a).is(":hidden") && \$("input[name='clicked_button_"+a+"']").val() && prev_row_nr && nr_of_active_days > 6) {
								\$("#system_name_row_"+a).show() ;
							} else if (!\$("extra_heading_"+a).is(":hidden") && \$("input[name='clicked_button_"+a+"']").val() && prev_row_nr && nr_of_active_days <= 6) {
								\$("#extra_heading_"+a).hide() ;
							}
							continue ; 
						}
						
						// let prev_row = a - 1 ;
						
						if (nr_of_active_days <= 6 && a > 1 && !\$("#extra_heading_"+a).is(":hidden")) {
							\$("#extra_heading_"+a).hide() ; 
							console.log("Hide Heading") ; 
						} else if (nr_of_active_days > 6 && a > 1 && \$("#extra_heading_"+a).length && \$("#extra_heading_"+a).is(":hidden") && \$("input[name='clicked_button_"+a+"']").val()) {
							\$("#extra_heading_"+a).show() ;
							console.log("Display Heading") ; 
						} else if (nr_of_active_days > 6 && a > 1 && !\$("#extra_heading_"+a).length && \$("input[name='clicked_button_"+a+"']").val()) {
							console.log("123 Add Heading") ; 
							\$("#system_name_row_"+a).before("<div class='row' id='extra_heading_"+a+"'><div class='col-md-2'></div><div class='col-md-4'><div class='row'><div class='col-md-4'><label class='control-label'>&nbsp;&nbsp;System&nbsp;Name</label></div><div class='col-md-4'><label class='control-label'>&nbsp;&nbsp;Club&nbsp;Name</label></div><div class='col-md-4'><label class='control-label'>&nbsp;&nbsp;Operator&nbsp;Name</label></div></div></div></div>") ;	
							
						}
						
						if (nr_of_active_days <= 6) {
							
							
							if (!\$("#day_names_"+a+"_extra_1").is(":hidden")) {
								\$("#day_names_"+a+"_extra_1").css("display","none") ; 
								\$("#system_name_row_"+a+"_extra_1").css("display","none") ; 
							}
							if (!\$("#day_names_"+a+"_extra_2").is(":hidden")) {
								\$("#day_names_"+a+"_extra_2").css("display","none") ; 
								\$("#system_name_row_"+a+"_extra_2").css("display","none") ; 
							}
							for (let b=1;b<=date_counter;b++) {
								// if (!\$("#day_"+b+"_name").is(":hidden")) {
								
								def_val = \$("#selectCustom_operator_"+a+"_"+b).val() ;
								
								if (!\$("#day_"+a+"_"+b).html() && \$("#day_"+b).val()) {
									\$("#day_"+a+"_"+b).show() ;
									\$("#day_"+a+"_"+b).removeClass('col-md-0').addClass('col-md-1') ;		
									if (!\$("#day_"+a+"_"+b).html()) {
										let select_raw =  add_select_raw (a,"custom_operator_"+a+"_"+b,"selectCustom_operator_"+a+"_"+b,"Select an Option","$operator_options",-1,100) ;
										\$("#day_"+a+"_"+b).html(select_raw) ;
										\$("#day_"+a+"_"+b).trigger("chosen:updated") ;
										\$("#selectCustom_operator_"+a+"_"+b).chosen({allow_single_deselect:true}) ;
										\$("#selectCustom_operator_"+a+"_"+b+"_chosen").css("width","100%") ;
									}
									
									// \$("#selectCustom_operator_"+a+"_"+b+"_chosen").css("width","100%") ;
								} 
								
								if (!\$("#selectCustom_operator_"+a+"_"+b).val() && def_val) {
									\$("#selectCustom_operator_"+a+"_"+b).val(def_val) ;
									\$("#selectCustom_operator_"+a+"_"+b).trigger("chosen:updated") ;									
								}
								
								// else {
									// \$("#day_"+a+"_"+b).css("display","none") ;
								// }
							} 							
						} else if (nr_of_active_days > 6 && nr_of_active_days <= 10) {

							// console.log("EXTRA TAB 1 nr_of_active_days : "+nr_of_active_days) ; 
							
							if (\$("#day_names_"+a+"_extra_1").is(":hidden")) {
								\$("#day_names_"+a+"_extra_1").show() ; 
							}
							\$("#system_name_row_"+a+"_extra_1").show() ; 
							if (!\$("#day_names_"+a+"_extra_2").is(":hidden")) {
								\$("#day_names_"+a+"_extra_2").hide() ;
							}
							\$("#system_name_row_"+a+"_extra_2").hide() ; 
							
							for (let b=1;b<=date_counter;b++) {
							// for (let b=1;b<=20;b++) {
								
								// if (\$("#day_"+b).val()) {
									
									def_val = \$("#selectCustom_operator_"+a+"_"+b).val() ;
									add_operator_dropdown("day_"+b+"_name_"+a+"_extra_1","day_"+a+"_"+b+"_extra_1","custom_operator_"+a+"_"+b,"selectCustom_operator_"+a+"_"+b,dates_map[b],a,0) ; 
									select_chosen_ids_1 = select_chosen_ids_1+",#selectCustom_operator_"+a+"_"+b ;
									select_chosen_ids_2 = select_chosen_ids_2+",#selectCustom_operator_"+a+"_"+b+"_chosen" ;	
									names_to_display = names_to_display + ",#day_"+b+"_name_"+a+"_extra_1" ;
									\$("#day_"+b+"_name_"+a+"_extra_1 label.control-label").text(dates_map[b]) ;
								// }
								console.log("#day_"+b+"_name_"+a+"_extra_1") ; 
								if (!\$("#day_"+b).val()) {
									
									hide_non_empty_operator_dropdown("day_"+b+"_name_"+a+"_extra_1","day_"+a+"_"+b+"_extra_1") ;
									// \$("#day_"+b+"_name_"+a+"_extra_1").css("display","none") ;	
									hide_date_names	= hide_date_names + ",#day_"+b+"_name_"+a+"_extra_1" ;
									// \$("#day_4_name_1_extra_1").css("display","none") ;										
								}
								
								delete_operator_dropdown ("day_"+b+"_name","day_"+a+"_"+b) ;	
								delete_operator_dropdown ("day_"+b+"_name_"+a+"_extra_2","day_"+a+"_"+b+"_extra_2") ;	
								
								if (!\$("#selectCustom_operator_"+a+"_"+b).val() && def_val) {
									\$("#selectCustom_operator_"+a+"_"+b).val(def_val) ;
									\$("#selectCustom_operator_"+a+"_"+b).trigger("chosen:updated") ;									
								}
							} 	
						} else if (nr_of_active_days > 10) {
							
							if (\$("#day_names_"+a+"_extra_1").is(":hidden")) {
								\$("#day_names_"+a+"_extra_1").show() ; 
							}
							if (\$("#day_names_"+a+"_extra_2").is(":hidden")) {
								\$("#day_names_"+a+"_extra_2").show() ; 
							}
							\$("#system_name_row_"+a+"_extra_1").show() ; 
							\$("#system_name_row_"+a+"_extra_2").show() ; 
							
							let used_dates = 0 ; 
							
							for (let b=1;b<=date_counter;b++) {
								
								// if (used_dates <= 10) {
								
								let def_val = 0 ; 
								def_val = \$("#selectCustom_operator_"+a+"_"+b).val() ;
								
								if (used_dates < 10 && \$("#day_"+b).val()) {
									
									used_dates++ ;
									add_operator_dropdown("day_"+b+"_name_"+a+"_extra_1","day_"+a+"_"+b+"_extra_1","custom_operator_"+a+"_"+b,"selectCustom_operator_"+a+"_"+b,dates_map[b],a,0) ; 
									select_chosen_ids_1 = select_chosen_ids_1+",#selectCustom_operator_"+a+"_"+b ;
									select_chosen_ids_2 = select_chosen_ids_2+",#selectCustom_operator_"+a+"_"+b+"_chosen" ;
									delete_operator_dropdown ("day_"+b+"_name_"+a+"_extra_2","day_"+a+"_"+b+"_extra_2") ;	
									\$("#day_"+b+"_name_"+a+"_extra_2 label.control-label").text(dates_map[b]) ;		
									
								} else if (!\$("#day_"+b).val() && dates_map[b]) {
									add_operator_dropdown("day_"+b+"_name_"+a+"_extra_1","day_"+a+"_"+b+"_extra_1","custom_operator_"+a+"_"+b,"selectCustom_operator_"+a+"_"+b,dates_map[b],a,1) ; 
									select_chosen_ids_1 = select_chosen_ids_1+",#selectCustom_operator_"+a+"_"+b ;
									select_chosen_ids_2 = select_chosen_ids_2+",#selectCustom_operator_"+a+"_"+b+"_chosen" ;
									hide_non_empty_operator_dropdown("day_"+b+"_name_"+a+"_extra_2","day_"+a+"_"+b+"_extra_2") ; 
								} else if (\$("#day_"+b).val() && used_dates == 10) {
									add_operator_dropdown("day_"+b+"_name_"+a+"_extra_2","day_"+a+"_"+b+"_extra_2","custom_operator_"+a+"_"+b,"selectCustom_operator_"+a+"_"+b,dates_map[b],a,0) ; 
									delete_operator_dropdown ("day_"+b+"_name_"+a+"_extra_1","day_"+a+"_"+b+"_extra_1") ;	
									select_chosen_ids_1 = select_chosen_ids_1+",#selectCustom_operator_"+a+"_"+b ;
									select_chosen_ids_2 = select_chosen_ids_2+",#selectCustom_operator_"+a+"_"+b+"_chosen" ;
								}
								
								delete_operator_dropdown ("day_"+b+"_name","day_"+a+"_"+b) ;	
								
								if (!\$("#selectCustom_operator_"+a+"_"+b).val() && def_val) {
									\$("#selectCustom_operator_"+a+"_"+b).val(def_val) ;
									\$("#selectCustom_operator_"+a+"_"+b).trigger("chosen:updated") ;									
								}
								
									// if (\$("#day_"+a+"_"+b+"_extra_2").html()) {
										// \$("#day_"+a+"_"+b+"_extra_2").html('') ;
										// \$("#day_"+a+"_"+b+"_extra_2").removeClass('col-md-1').addClass('col-md-0') ;
										// \$("#day_"+a+"_"+b+"_extra_2").css("display","none") ;
									// }
									
								// } 
								
								// else {
									
									// if (\$("#day_"+b).val()) {
										
										// add_operator_dropdown("day_"+b+"_name_"+a+"_extra_2","day_"+a+"_"+b+"_extra_1","custom_operator_"+a+"_"+b,"selectCustom_operator_"+a+"_"+b,dates_map[b],a,0) ; 
																		
									// } else if (!\$("#day_"+b).val() && dates_map[b]) {
										// hide_non_empty_operator_dropdown("day_"+b+"_name_"+a+"_extra_2","day_"+a+"_"+b+"_extra_2") ; 
									// }
									
								// }
							} 	
						}
					}
					
				}

				\$("#inputQty").val(count_days) ; 
				
				\$("#timepickerTime_to_"+date_counter).css("display","none");
				
				// console.log("date_counter : "+date_counter) ; 
				
				if (date_counter <= 20) {
					for (let j=date_counter+1;j<=20;j++) {	
						for (let k=1; k<=15; k++) {
							if (\$("input[name='clicked_button_"+k+"']").val()) {
								if ( \$("#day_"+k+"_"+j).html()) {
									\$("#day_"+k+"_"+j).html('') ;
									\$("#day_"+k+"_"+j).removeClass('col-md-1').addClass('col-md-0') ;
									\$("#day_"+k+"_"+j).hide() ;									
								}
								// delete_operator_dropdown("day_"+j+"_name_"+k,"day_"+k+"_"+j) ; 			
								delete_operator_dropdown("day_"+j+"_name_"+k+"_extra_1","day_"+k+"_"+j+"_extra_1") ; 			
								delete_operator_dropdown("day_"+j+"_name_"+k+"_extra_2","day_"+k+"_"+j+"_extra_2") ; 
							}
						}
						if (\$("#day_"+j).val()) {
							\$("#day_"+j).val("").trigger("chosen:updated") ; changed_nr_of_days = 1 ; 
						} 
						if (!\$("#day_"+j+"_chosen").is(":hidden")) {
							\$("#day_"+j+"_name label.control-label").text('') ;
							\$("#day_"+j+"_name").css("display","none") ; 
							\$("#radio_"+j+"_1").css("display","none") ;
							\$("#radio_"+j+"_2").css("display","none") ;
							\$("#day_"+j+"_chosen").css("display","none") ;			
							// console.log("col : "+j) ; 
						} 
						if (!\$("#timepickerTime_from_"+j).is(":hidden")) {
							\$("#timepickerTime_from_"+j).css("display","none") ; 
						}
						if (!\$("#timepickerTime_to_"+j).is(":hidden")) {
							\$("#timepickerTime_to_"+j).css("display","none") ;
						}
						
					}					
				}
				
				
				if (changed_radio_cnt || changed_nr_of_days) {
					update_qty_in_quote_tab_when_changing_nr_of_full_or_half_days() ;
				}
				
				
				\$(names_to_display).removeClass('col-md-0').addClass('col-md-1');	 
				\$(names_to_display).show() ;				
				\$(select_chosen_ids_1).chosen({allow_single_deselect:true}) ;
				\$(select_chosen_ids_2).css("width","100%") ;	
				 
				// // // for (let k=1;k<=50;k++) {
					// // // if (\$("#selectDescription_workings_event_"+k).val() == 14) {
						// // // \$("#inputQty_workings_event"+k).val(0) ;
						// // // let ttl_amnt = 0 ;
						// // // \$("#inputAmount_workings_event"+k).val(ttl_amnt.toFixed(2)) ;
					// // // }
				// // // }
				// // // calcAllWorkings() ; 

				// // // for (let k=1;k<=50;k++) {
					// // // if (\$("#selectDescription_workings_event"+k).val() == 14) {
						// // // let inp_qty = differenceDays ; 
						// // // \$("#inputQty_workings_event"+k).val(inp_qty) ;
						// // // let amnt = \$("#inputAmount_usd_workings_event"+k).val() ;
						// // // let ttl_amnt = amnt*inp_qty ;
						// // // \$("#inputAmount_workings_event"+k).val(ttl_amnt.toFixed(2)) ;
					// // // }
				// // // }
				// // // if ($changed_opid_as_level_3) { 
					// // // calc_hidden_totals() ; 
				// // // } else {
					// // // calcAllWorkings() ;
				// // // } 
				
				// \$("#day_4_name_1_extra_1").css("display","none") ; 
				
				if (hide_date_names != "#1") {
					\$(hide_date_names).css("display","none") ; 
				}
				
			}	
			
			
			
			
			function timeStringToSeconds(timeString) {
 
				const [hours, minutes, seconds] = timeString.split(':').map(Number);
				return hours * 3600 + minutes * 60 + seconds;
			}

			function getDatesBetween(startDateStr,endDateStr,count_dates) {
				if (!startDateStr || !endDateStr) {
					return ; 
				}
				const dates = [];
				let currentDate = new Date(startDateStr);
				let endDate = new Date(endDateStr) ;
				// currentDate.setHours(currentDate.getHours() + 2);
				// endDate.setHours(endDate.getHours() + 2);				
				currentDate.setHours(+2);
				currentDate.setMinutes(0);
				currentDate.setSeconds(0);		
				endDate.setHours(+2);
				endDate.setMinutes(59);
				endDate.setSeconds(59);							
			
				// console.log("2 . currentDate : "+currentDate.toISOString().split('T')[0]) ; 
				while (currentDate < endDate) {
					// console.log("3 . currentDate : "+currentDate.toISOString().split('T')[0]) ; 
					dates.push(currentDate.toISOString().split('T')[0]);
					currentDate.setDate(currentDate.getDate() + 1);
					if (count_dates) { count_dates++ ; }
				} 
				if (count_dates) {
					count_dates-- ;
					return count_dates;
				} else {
					return dates;
				}
			}
		~ ;
		
		&event_tabs_content_box($sec_name,$sec_col);

		# --------------- END CUSOTMER DETAILS ----------------------------------------------------------------------------------------------------------
	}

	if ($tab == 3) { # Event Specifics

	# --------------- START DOCUMENT UPLOADS ----------------------------------------------------------------------------------------------------------------
	
	$add_form_fields = '' ;
	
	my $sec = '_event_specifics' ; my ($sec_name,$sec_cnt,$sec_col) = &tab_sec_det($sec) ;
	
	$ret_pdf_link_sec .= $sec . '+' . $sec_cnt . '|' ;
	
	if ($ret) { return ; }
	
	$add_form_fields = '' ;
	
	my %selected_1 = () ; my %selected_2 = () ; 
	
	&db_min_ro('event_quotes_categories','*','','','') ; 
	
	$selected_1{$db{$table}{$id}{category_id_1}} = 'SELECTED' ;
	$selected_2{$db{$table}{$id}{category_id_2}} = 'SELECTED' ;
	my $none_id = 0 ; 
	
	foreach (sort {$db{event_quotes_categories}{$a}{category} cmp $db{event_quotes_categories}{$b}{category}} keys %{$db{event_quotes_categories}}) {	

		if ($db{event_quotes_categories}{$_}{category} eq 'None') {
			$none_id = $_ ;
			next ;	
		}
		$opts{category_id_2} .= qq~<option value="$_" $selected_2{$_}>$db{event_quotes_categories}{$_}{category}</option>~ ; 
		$opts{category_id_1} .= qq~<option value="$_" $selected_1{$_}>$db{event_quotes_categories}{$_}{category}</option>~ ;	
	}
	$opts{category_id_2} .= qq~<option value="$none_id" $selected_2{$none_id}>None</option>~ ; 
	$opts{category_id_1} .= qq~<option value="$none_id" $selected_1{$none_id}>None</option>~ ; 
	
	$add_form_fields .= qq~<div class='row'>~ ;
	$add_form_fields .= qq~<div class="col-md-5">~ ;
	
	$fcol = 8 ; $lcol = 3 ;
	
	my @cat_details = split('\|;\|',$db{event_quotes_min}{$id}{category_details}) ; 
	
	$preferred_title{category_1_details} = "Category 1 Sub Categories" ;
	$preferred_title{category_2_details} = "Category 2 Sub Categories" ;
	
	# $add_form_fields .= &common_min_form_input('poc_name',$db{$table}{$id}{poc_name},'') ; 
	# $add_form_fields .= &common_min_form_input('poc_contact_nr',$db{$table}{$id}{poc_contact_nr},'') ; 
		
	# $db{$table}{$id}{poc_id} = 1 ;
		
	&db_min_ro('event_quotes_poc','id,name,contact_nr',"",'','') ; 
	
	my %seen;
	my @unique_keys = grep {
		my $name = $db{event_quotes_poc}{$_}{name};
		!$seen{$name}++
	} sort {
		$db{event_quotes_poc}{$a}{name} cmp $db{event_quotes_poc}{$b}{name}
	} keys %{$db{event_quotes_poc}};

	$opts{poc_name} = qq~<option ~ . join(
		"</option><option ",
		map {
			my $name = $db{event_quotes_poc}{$_}{name} ;
			qq~value="$name">$name~
		} @unique_keys
	) . qq~</option>~ ;
	
	my $len = scalar @unique_keys ; 

	$opts{poc_name} =~ s/value="$db{event_quotes_poc}{$db{$table}{$id}{poc_id}}{name}"/value="$db{event_quotes_poc}{$db{$table}{$id}{poc_id}}{name}" SELECTED/g  if $db{$table}{$id}{poc_id} ;
	
	my @filtered = grep {
		$db{event_quotes_poc}{$_}{name} eq $db{event_quotes_poc}{$db{$table}{$id}{poc_id}}{name}
	} keys %{$db{event_quotes_poc}};

	$opts{poc_contact_nr} = qq~<option ~ . join(
		"</option><option ",
		map {
			# return () if $db{event_quotes_poc}{$_}{name} ne $db{event_quotes_poc}{$db{$table}{$id}{poc_id}}{name} ;
			my $disp_nr = $db{event_quotes_poc}{$_}{contact_nr} ;
			if (length($disp_nr) == 11) {
				$disp_nr = substr($disp_nr,0,2) . ' ' .  substr($disp_nr,2,2) . ' ' .  substr($disp_nr,4,3) . ' ' .  substr($disp_nr,7,4) ;
				$disp_nr = '+' . $disp_nr if $disp_nr =~ /^27/ ;
			}
			qq~value='$db{event_quotes_poc}{$_}{contact_nr}'>$disp_nr~
		} @filtered 
	) . qq~</option>~ ;

	$opts{poc_contact_nr} =~ s/value='$db{event_quotes_poc}{$db{$table}{$id}{poc_id}}{contact_nr}'/value='$db{event_quotes_poc}{$db{$table}{$id}{poc_id}}{contact_nr}' SELECTED/g if $db{event_quotes_poc}{$db{$table}{$id}{poc_id}}{contact_nr} ;
	
	$dlg{poc_name} .= qq~<div class="col-md-1"><a href="javascript:dlgMdl('$useropts{'scripts'}/dialog/add_event_poc.pl?add','Add POC','','medium-dialog');" class="btn btn-primary btn-xs" style="padding:0px 2px 0px 3px;margin-top:0px;" title='Add POC' data-toggle='tooltip' data-placement='right'><i class="glyphicon glyphicon-plus" style="padding:0;margin-top:0px;top:0px;"></i></a></div>~ ; 
	
	my $add_poc_contact_nr_style = (!$db{$table}{$id}{poc_id} || ($db{event_quotes_poc}{$db{$table}{$id}{poc_id}}{name} && !$db{event_quotes_poc}{$db{$table}{$id}{poc_id}}{contact_nr})) ? "style='display:none;'" : "" ;
	
	$dlg{poc_contact_nr} .= qq~<div class="col-md-1" id='add_poc_contact_nr' $add_poc_contact_nr_style><a href="javascript:dlgMdl('$useropts{'scripts'}/dialog/add_event_poc.pl?add_number_only','Add POC Number','','medium-dialog');" class="btn btn-primary btn-xs" style="padding:0px 2px 0px 3px;margin-top:0px;" title='Add POC Number' data-toggle='tooltip' data-placement='right'><i class="glyphicon glyphicon-plus" style="padding:0;margin-top:0px;top:0px;"></i></a></div>~ unless $db{event_quotes_poc}{$db{$table}{$id}{poc_id}}{contact_nr} ; 
		
	$add_form_fields .= &common_min_form_select('poc_name',$db{$table}{$id}{poc_name}) ; 
	$add_form_fields .= &common_min_form_select('poc_contact_nr',$db{$table}{$id}{poc_contact_nr}) ; 
	$add_form_fields .= qq~<input type='hidden' name="poc_id" value="$db{$table}{$id}{poc_id}">~ ; 

	$trigger_jquery .= qq~
		\$("#selectPoc_name_chosen,#selectPoc_contact_nr_chosen").css("width","100%") ; 
	~ ; 
	
	$trigger_jquery_raw .= qq~
		\$("#selectPoc_name").change( async function () {
			var vid = \$("#selectPoc_contact_nr") ;
			vid.empty() ;
			vid.append("<option value=''></option>").trigger("chosen:updated") ;
			let sel_val = \$(this).val()  ;
			var url = "$useropts{scripts}/get/get_poc_contact_nr.pl?poc_name="+sel_val ; 
			console.log("url : "+url) ; 
			let nrs_cnt = 0 ; let def_val = "" ; let disp_val = "" ; let poc_id = 0 ;
			await \$.get(url, function(json) {
				\$.each(json, function(key, data) {
					disp_val = data.contact_nr ;
					disp_val = disp_val.toString() ;
					if (disp_val.length === 11 && disp_val.match(/^27/)) {
						disp_val = `+27 \${disp_val.slice(2, 4)} \${disp_val.slice(4, 7)} \${disp_val.slice(7)}`
					}
					vid.append("<option value='"+data.contact_nr+"'>"+disp_val+"</option>").trigger("chosen:updated") ;
					if (data.contact_nr) {
						nrs_cnt++ ;
					}
					def_val = data.contact_nr ;
					poc_id = data.id ;
				}) ;
			},'json') ;
			if (nrs_cnt == 1) {
				vid.val(def_val).trigger("chosen:updated") ;
				\$("input[name='poc_id']").val(poc_id) ; 
			} else {
				\$("input[name='poc_id']").val("") ; 
			}
			if (\$(this).val() && nrs_cnt == 0 && \$("#add_poc_contact_nr").is(":hidden")) {
				\$("#add_poc_contact_nr").show() ;
			} else if (!\$("#add_poc_contact_nr").is(":hidden")) {
				\$("#add_poc_contact_nr").hide() ;
			}
			
		}) ;
	~ ;
					
	$add_form_fields .= &common_min_form_select('category_id_1',$db{$table}{$id}{category_id_1},'') ; 
	$add_form_fields .= &common_min_form_textarea('category_1_details',$cat_details[0],'') ;
	$add_form_fields .= &common_min_form_select('category_id_2',$db{$table}{$id}{category_id_2},'') ; 	
	$add_form_fields .= &common_min_form_textarea('category_2_details',$cat_details[1],'') ;
	
	$trigger_jquery .= qq~\$("#selectCategory_id_1\_chosen").css( "width", "100%" );~ ;
	$trigger_jquery .= qq~\$("#selectCategory_id_2\_chosen").css( "width", "100%" );~ ;
	
	$preferred_title{format_of_title} = "Format of TITLE to be followed when scheduling events" ;
	
	$add_form_fields .= qq~</div>~ ;	
		$add_form_fields .= qq~<div class="col-md-6">~ ;
			$add_form_fields .= &common_min_form_input('recipient_name',$db{$table}{$id}{recipient_name},'') ; 
			$add_form_fields .= &common_min_form_input('recipient_nr',$db{$table}{$id}{recipient_nr},'') ; 
			$add_form_fields .= &common_min_form_textarea('additional_notes',$db{$table}{$id}{additional_notes},'') ;
			$add_form_fields .= &common_min_form_textarea('format_of_title',$db{$table}{$id}{format_of_title},'') ;
		$add_form_fields .= qq~</div>~ ;
	$add_form_fields .= qq~</div>~ ;	
	
	my @docs = ('1','2','3') ;

	$add_form_fields .= qq~
		<div class='row'><div class="col-md-12">&nbsp;</div></div>
		<div class='row'>
	~ ;

	foreach (@docs) {
		my $doc = lc $_ ; $doc =~ s/ /\_/ ; $doc =~ s/&/\_/ ;
		$working_uploads = &common_get_field_uploads($id,"upload_$doc",'id') ;
		if ($working_uploads) {
			$add_form_fields .= qq~
			<div class="col-md-4" id="workings">
				$working_uploads
			</div>~; 
		} else {
			$add_form_fields .= qq~
			<div class="col-md-4">
				<div class="form-group">
					<label for="imageInputFile">Attach Fixture $_</label>
					<input type="file" id="imageInputFile" name='iattachupload_$doc' tabindex='$tindex'>
					<p class="help-block">Upload file from PC.</p>
				</div>
			</div>
			~;		
		}
	}
	
	$add_form_fields .= qq~
		</div>
	~ ;	
	
	&event_tabs_content_box($sec_name,$sec_col);

	# --------------- END DOCUMENT UPLOADS ----------------------------------------------------------------------------------------------------------------
	}

	if ($tab == 4) { # Costing

		# --------------- START COSTING ----------------------------------------------------------------------------------------------------------------
		
		$add_form_fields = '' ;

		$lcol = 1; 
		$fcol = 2;
		$preferred_title{currency} = 'Currency' ; 
		$add_form_fields          .= &common_min_form_select('currency',$db{$table}{$id}{currency},1) ; # don't end row
		$trigger_jquery           .= qq~\$("#selectCurrency_chosen").css( "width", "96%" );~ ;
		$preferred_title{roe}      = ' ROE ' ; $fcol=1;  # $fcol=2 ;
		$add_form_fields          .= &common_min_form_label_col('roe') ;
		$db{$table}{$id}{roe} = 1 if not $db{$table}{$id}{roe} ; 
		$add_form_fields          .= &common_min_form_input_col('roe',$db{$table}{$id}{roe},1) ;
		# $preferred_title{roe}      = ' Discount ' ;
		# $add_form_fields          .= &common_min_form_label_col('discount') ;
		# $add_form_fields          .= &common_min_form_select_col('discount',$db{$table}{$id}{discount}) ;
		$trigger_jquery           .= qq~\$("#selectDiscount_chosen").css( "width", "96%" );~ ;
		$add_form_fields          .= qq~</div>~ ;

		our %sec_cnt = ('a_event' => "30-List Event Costings");
		
		my $sec = 'costing' ; 
		my ($sec_name, $sec_cnt, $sec_col) = &tab_sec_det($sec) ;	
		$ret_pdf_link_sec .= $sec . '+' . $sec_cnt . '|' ;
		
		return if $ret;
		
		&event_tabs_build_sec($sec,$tab) ;
		
		foreach (sort keys %sec_cnt) {
			$_k = substr($_, 1) ;
			my @parts = split(/\-/, $sec_cnt{$_}) ;
			$add_form_fields .= qq~
				<div class='row'>
					<div class='col-md-12'>
						<h4>
							$parts[1] :
						</h4>
					</div>
				</div>
			~ ;
			&tab_build_qt_col_headers("$sec$_k");	
			# # # &db_min_ro('event_quotes_min','id,excl_costing_event,item_costing_event,qty_costing_event,usd_amnt_costing_event,item_ttl_costing_event,vat_costing_event,curr_amnt_costing_event',"id=$id",'','') if $id ;
			&event_tabs_build_costs_forms($id,$tab,$parts[0],"$sec$_k"); 			
		}
		
		$add_form_fields .= qq~
			<div class='row'>
				<div class="box col-md-12">
					&nbsp;
				</div>
			</div>~ ;
		$add_form_fields .= qq~
			<div class='row'>
				<div class="box col-md-3">
					&nbsp;
				</div>
			~ ;
		
		my @ttlarr = ("sub_total","vat_total","grand_total") ;
		$lcol = 1 ;
		$fcol = 1 ;
		foreach (@ttlarr) {
			$lcol = 1 ;  $add_form_fields .= &common_min_form_label_col($_) ;   
		}
			
		$add_form_fields .= qq~</div>~ ;
		$add_form_fields .= qq~<div class='row'><div class="box col-md-1"></div>~ ; 
		$lcol = 2 ;
		$add_form_fields .= &common_min_form_label_col('Total') ; 
		$lcol = 1 ; $fcol = 1 ;
		
		foreach (@ttlarr) {
			$readonly{"$_\_costing_event"} = "READONLY" ; 
			$input_style{"$_\_costing_event"} = qq~style="text-align:right;"~ ;
			my $val = $db{$table}{$id}{$_} ? $db{$table}{$id}{$_} : '0.00' ;
			$add_form_fields .= &common_min_form_input_col("$_\_costing_event",$val) ; 
		}
		
		$add_form_fields .= qq~</div>~ ;

		$add_form_fields .= qq~
			<div class='row'>
				<div class="box col-md-12">
					&nbsp;
				</div>
			</div>
		~ ;
		
		&event_tabs_content_box($sec_name,$sec_col);
	 
		my $display_hidden_input = ($username eq 'handre') ? "" : "style='display:none;'" ; 
	 
		$trigger_jquery_raw .= qq~
			
			function count_nr_of_full_day_and_half_days () {
				
				let full_day_exists = 0 ; let half_day_exists = 0 ; 
				 
				console.log("count_nr_of_full_day_and_half_days") ; 
				
				for (let i = 1; i<=20; i++) {
					if (\$("#radio_"+i+"_1").is(":checked") && \$("#day_"+i).val()) {
						full_day_exists++ ;
					} else if (\$("#radio_"+i+"_2").is(":checked") && \$("#day_"+i).val()) {
						half_day_exists++ ;
					}
				}
				
				console.log("full_day_exists : "+full_day_exists) ; 
				
				return [full_day_exists,half_day_exists] ;
				
			}
			
			function cnt_selected_systems_per_cost_item () {
				
				let i = 0 ;
				cnt_systems_per_cost_item = {} ;
				while (true) { 
					i++ ;
					
					let system_id = \$("#selectSystem_name_"+i).val() ;
					
					if (!cnt_systems_per_cost_item[get_event_cost_id[system_id]]) {
						cnt_systems_per_cost_item[get_event_cost_id[system_id]] = 0 ;
					}
					if (!\$("#system_name_row_"+i).html()) {
						i-- ;
						break ;
					}
					if (system_id && get_event_cost_id[system_id]) {
						cnt_systems_per_cost_item[get_event_cost_id[system_id]]++ ;
					}
				}				
			}
		
			function auto_deselect_cost_items_in_quote_tab (row_nr) {
 
				let orig_system_id = orig_selected_system_names[row_nr] ; 
				let orig_cost_item = get_event_cost_id[orig_system_id] ; // Check whether get_event_cost_id exists 
				
				if (!orig_system_id || (orig_system_id && !orig_cost_item)) {
					return ;
				}
				let i = 0 ;
				// Chech if the corresponding cost item exists for a diffent selected system 
				 
				while (true) {
					i++ ;
					if (!\$("#system_name_row_"+i).html()) {
						break ;
					}					
					if (i == row_nr) {
						continue ;
					}
					if (orig_cost_item == get_event_cost_id[\$("#selectSystem_name_"+i).val()]) {
						return ;
					}
				}
				
				i = 0 ; let j = 0 ; let select_new_values = {} ; let input_new_values = {} ; let checkbox_new_values = {} ;

				while (true) {
					
					i++ ; j++ ;
					if (!\$("#costing_event_"+i).html()) {
						i-- ; j-- ;
						break ;
					}
					
					let hidden_input = (\$("input[name='added_full_day_cost_item_"+i+"']").val()) ? \$("input[name='added_full_day_cost_item_"+i+"']").val() : (\$("input[name='added_half_day_cost_item_"+i+"']").val()) ? \$("input[name='added_full_day_cost_item_"+i+"']").val() : "" ;
					
					if (hidden_input.includes("-1") || hidden_input.includes("-2")) {
						continue ;
					}
					
					if (orig_cost_item == \$("#selectDescription_costing_event_"+i).val()) {
	
						j-- ; 
						\$("#selectDescription_costing_event_"+i).val("").trigger("chosen:updated") ;
						\$("#inputQty_costing_event_"+i).val("") ; 
						\$("#inputAmount_usd_costing_event_"+i).val("") ; 

						// calc_totals(i,"costing_event") ;
					} else {
						select_new_values["#selectDescription_costing_event_"+j] = \$("#selectDescription_costing_event_"+i).val() ;
						input_new_values["input[name='qty_costing_event_"+j+"']"] = \$("input[name='qty_costing_event_"+i+"']").val() ;
						input_new_values["input[name='amount_usd_costing_event_"+j+"']"] = \$("input[name='amount_usd_costing_event_"+i+"']").val() ; ;
						input_new_values["input[name='amount_ttl_costing_event_"+j+"']"] = \$("input[name='amount_ttl_costing_event_"+i+"']").val() ; ;
						checkbox_new_values["#checkboxVat_costing_event_"+j] = (\$("#checkboxVat_costing_event_"+i).is(":checked")) ? 1 : 0 ;							
					}
					
					if (i != j) {
						\$("input[name='added_full_day_cost_item_"+i+"']").remove() ;
						\$("input[name='added_half_day_cost_item_"+i+"']").remove() ;
					}
					
				}
				
				for (let key in select_new_values) {
					console.log("select_new_values : "+key) ; 
					if (select_new_values[key] != \$(key).val()) {
						\$(key).val(select_new_values[key]).trigger("chosen:updated") ;
					}
				}
				
				for (let key in input_new_values) {
					if (typeof \$(key).val() == 'undefined' && input_new_values[key]) {
						let display = (hidden_input_name[key].includes("_day_cost_item_")) ? "" : "" ;
						\$(hidden_input_to_html_row[key]).append("<input value='"+input_new_values[key]+"' name='"+hidden_input_name[key]+"' "+display+">") ; 
					}
					
					if (input_new_values[key] != \$(key).val()) { 
						\$(key).val(input_new_values[key]) ;
					}
				}
				
				for (let key in checkbox_new_values) {
					if (\$(key).is(":checked") && !checkbox_new_values[key]) {
						\$(key).prop("checked",false) ;
					} else if (!\$(key).is(":checked") && checkbox_new_values[key]) {
						\$(key).prop("checked",true) ;
					}
				}
				
				if (j < i) {
					for (let k=j+1;k<=i+1;k++) {
						if ((j == 0 && k > 1) || j > 0) {
							\$("#costing_event_"+k).html("") ;
						} 
						if ((k == j+1 && j > 0) || (j == 0 && k == 2)) {
							console.log("SHOW : #btn_row_costing_event_"+k) ; 
							\$("#btn_row_costing_event_"+k).show() ;
							\$("#btn_costing_event_"+k).show() ;
						} else if (j > 0 || (j == 0 && k > 2)) {
							console.log("HIDE : #btn_row_costing_event_"+k) ; 
							\$("#btn_row_costing_event_"+k).hide() ;
							\$("#btn_costing_event_"+k).hide() ;
						}
					}
				}
				
			}
			
			function auto_select_cost_items_in_quote_tab (id,row_number) {
				
				cnt_selected_systems_per_cost_item() ;

				let system_id = \$("#"+id).val() ;
				
				let row_nr = (row_number) ? row_number : get_row_nr(id) ;
				
				if (orig_selected_system_names[row_nr] && get_event_cost_id[orig_selected_system_names[row_nr]] != get_event_cost_id[system_id]) {
					auto_deselect_cost_items_in_quote_tab(row_nr) ; 
				}
				
				let event_cost_id = get_event_cost_id[system_id] ;

				let i = 0 ;
 
				if (!event_cost_id) {
					
					while (true) {
						i++ ; 
						if (!\$("#costing_event_"+i).html()) { 
							break ; 
						}
						if (\$("#selectDescription_costing_event_"+i).val() && \$("#selectDescription_costing_event_"+i).val() == get_event_cost_id[orig_selected_system_names[row_nr]] && i != row_nr) {
							event_cost_id = get_event_cost_id[orig_selected_system_names[row_nr]] ;
						} 
					}
					 
					if (!event_cost_id) {
						orig_selected_system_names[row_nr] = \$("#selectSystem_name_"+row_nr).val() ;
						return ;
					}
					
				} else if (event_cost_id && get_event_cost_id[orig_selected_system_names[row_nr]] && get_event_cost_id[orig_selected_system_names[row_nr]] != event_cost_id) {
					
					while (true) {
						i++ ; 
						if (!\$("#costing_event_"+i).html()) { 
							break ; 
						}
						if (\$("#selectDescription_costing_event_"+i).val() && \$("#selectDescription_costing_event_"+i).val() == get_event_cost_id[orig_selected_system_names[row_nr]]) {
							
							let hidden_input_val = (\$("input[name='added_full_day_cost_item_"+i+"']").val()) ? \$("input[name='added_full_day_cost_item_"+i+"']").val() : (\$("input[name='added_half_day_cost_item_"+i+"']").val()) ? \$("input[name='added_half_day_cost_item_"+i+"']").val() : "" ;
							if (hidden_input_val && !hidden_input_val.includes("-2")) {
								let hidden_input_fh = (\$("input[name='added_full_day_cost_item_"+i+"']").val()) ? "full" : (\$("input[name='added_half_day_cost_item_"+i+"']").val()) ? "half" : "" ;
								if (hidden_input_fh) {
									\$("#inputQty_costing_event_"+i).val(nr_days[hidden_input_fh]*cnt_systems_per_cost_item[get_event_cost_id[orig_selected_system_names[row_nr]]]) ; 
									calc_totals(i,"costing_event") ;
								}								
							}

						}
					}
				}
				
				orig_selected_system_names[row_nr] = \$("#selectSystem_name_"+row_nr).val() ;

				let default_full_day_cost_item_rate = default_cost_item_rates["full,"+event_cost_id] ;
				let default_half_day_cost_item_rate = default_cost_item_rates["half,"+event_cost_id] ;

				if (!default_full_day_cost_item_rate && !default_half_day_cost_item_rate) {
					return ;
				}
				
				if (!nr_days["full"] && !nr_days["half"]) {
					return ;
				}

				if (nr_days["full"] && !default_full_day_cost_item_rate && !nr_days["half"] && default_half_day_cost_item_rate) {
					return ;
				}
				
				if (!nr_days["full"] && default_full_day_cost_item_rate && nr_days["half"] && !default_half_day_cost_item_rate) {
					return ;
				}

				let last_used_row = 0 ; i = 0 ; let cnt_systems = cnt_systems_per_cost_item[event_cost_id] ;

				if (\$("#costing_event_1").html() && !\$("#costing_event_2").html() && !\$("#selectDescription_costing_event_1").val()) {
					last_used_row = 0 ;
				} else {
					
					let nr_of_blank_rows_needed = (nr_days["full"] && nr_days["half"]) ? 2 : 1 ;
					
					while (true) {
						i++ ;
						let j = 1 + i ;
						if (\$("#selectDescription_costing_event_"+i).val() && \$("#selectDescription_costing_event_"+i).val() != event_cost_id) {
							last_used_row = i ;
							console.log(" 5.1 last_used_row : "+last_used_row) ; 
						} else if (\$("#costing_event_"+i).html() && \$("#selectDescription_costing_event_"+i).val() && \$("#selectDescription_costing_event_"+i).val() == event_cost_id) {
							last_used_row = i - 1 ;
							break ;
						} else if ((!\$("#selectDescription_costing_event_"+i).val() && nr_of_blank_rows_needed == 1) || (nr_of_blank_rows_needed == 2 && !\$("#selectDescription_costing_event_"+i).val() && !\$("#selectDescription_costing_event_"+j).val())) {
							i-- ;
							break ;
						} 
					}					
				} 
				
				let full_half_loop = (nr_days["full"] && nr_days["half"]) ? ["full","half"] : (nr_days["full"]) ? ["full"] : (nr_days["half"]) ? ["half"] : [] ;
				
				let = f_t_h = {"full":"half","half":"full"} ; 
				
				i = 0 ; let j = 0 ; last_used_row++ ; let need_to_shift_data_below_down = 0 ; let select_new_values = {} ; let input_new_values = {} ; let checkbox_new_values = {} ; let hidden_input_to_html_row = {} ;let hidden_input_name = {} ; let add_html_row  = {} ; let already_moved_this_row = {}  ; let break_loop  = 0 ; let cals_totals_row = {} ; 
				
				while (true) {
					
					i++ ; j++ ;
					
					if (last_used_row > i) {
						continue ;
					}
					
					let k = last_used_row ;

					if (j > i && \$("#selectDescription_costing_event_"+i).val() && !already_moved_this_row[i]) {

						need_to_shift_data_below_down = 1 ;
						select_new_values["#selectDescription_costing_event_"+j] 			= \$("#selectDescription_costing_event_"+i).val() ;
						input_new_values["input[name='qty_costing_event_"+j+"']"] 			= \$("input[name='qty_costing_event_"+i+"']").val() ;
						input_new_values["input[name='amount_usd_costing_event_"+j+"']"] 	= \$("input[name='amount_usd_costing_event_"+i+"']").val() ;
						input_new_values["input[name='amount_ttl_costing_event_"+j+"']"] 	= \$("input[name='amount_ttl_costing_event_"+i+"']").val() ;
						checkbox_new_values["#checkboxVat_costing_event_"+j]	 			= (\$("#checkboxVat_costing_event_"+i).is(":checked")) ? 1 : 0 ;
						let hidden_input_value = (\$("input[name='added_full_day_cost_item_"+i+"']").val()) ? "full" : (\$("input[name='added_half_day_cost_item_"+i+"']").val()) ? "half" : "" ;
						if (hidden_input_value && hidden_input_value != "") {
							input_new_values["input[name='added_"+hidden_input_value+"_day_cost_item_"+j+"']"] = \$("input[name='added_"+hidden_input_value+"_day_cost_item_"+i+"']").val() ; 
							hidden_input_to_html_row["input[name='added_"+hidden_input_value+"_day_cost_item_"+j+"']"] = "#costing_event_"+j ;							
							hidden_input_name["input[name='added_"+hidden_input_value+"_day_cost_item_"+j+"']"] = "added_"+hidden_input_value+"_day_cost_item_"+j ;		
							\$("input[name='added_"+hidden_input_value+"_day_cost_item_"+i+"']").remove() ;								
						}
						
						if (!\$("#costing_event_"+j).html()) {
							add_html_row[j] = 1 ;
						}
						cals_totals_row[j] = 1 ;
						
					}
					
					if (i == last_used_row) {
						
						for (let f_h in full_half_loop) {
							
							let default_rate = (full_half_loop[f_h] == "full") ? default_full_day_cost_item_rate : default_half_day_cost_item_rate ;

							if (nr_days[full_half_loop[f_h]] && default_rate) {
								
								i++ ; j++ ;
								if (f_h == 0 && full_half_loop[1] && \$("#selectDescription_costing_event_"+i).val() && \$("#selectDescription_costing_event_"+i).val() != event_cost_id) {
									
									j++ ;
									already_moved_this_row[i] = 1 ;  
									need_to_shift_data_below_down = 1 ;
									select_new_values["#selectDescription_costing_event_"+j] 			= \$("#selectDescription_costing_event_"+i).val() ;
									input_new_values["input[name='qty_costing_event_"+j+"']"] 			= \$("input[name='qty_costing_event_"+i+"']").val() ;
									input_new_values["input[name='amount_usd_costing_event_"+j+"']"] 	= \$("input[name='amount_usd_costing_event_"+i+"']").val() ;
									input_new_values["input[name='amount_ttl_costing_event_"+j+"']"] 	= \$("input[name='amount_ttl_costing_event_"+i+"']").val() ;
									checkbox_new_values["#checkboxVat_costing_event_"+j]	 			= (\$("#checkboxVat_costing_event_"+i).is(":checked")) ? 1 : 0 ;
									let hidden_input_value = (\$("input[name='added_full_day_cost_item_"+i+"']").val()) ? "full" : (\$("input[name='added_half_day_cost_item_"+i+"']").val()) ? "half" : "" ;
									if (hidden_input_value && hidden_input_value != "") {
										input_new_values["input[name='added_"+hidden_input_value+"_day_cost_item_"+j+"']"] = \$("input[name='added_"+hidden_input_value+"_day_cost_item_"+i+"']").val() ; 
										hidden_input_to_html_row["input[name='added_"+hidden_input_value+"_day_cost_item_"+j+"']"] = "#costing_event_"+j ;							
										hidden_input_name["input[name='added_"+hidden_input_value+"_day_cost_item_"+j+"']"] = "added_"+hidden_input_value+"_day_cost_item_"+j ;		
										\$("input[name='added_"+hidden_input_value+"_day_cost_item_"+i+"']").remove() ;								
									}
									cals_totals_row[j] = 1 ;
								}
								i-- ; j-- ;
								
								if (!\$("#costing_event_"+k).html()) {
									add_quote_expenses_row_all("costing_event_"+k,"costing_event") ;
									\$("#selectDescription_costing_event_"+k+"_chosen").css("width","90%") ;
									\$("#costing_event_"+k).append("<input value='"+event_cost_id+"' name='added_"+full_half_loop[f_h]+"_day_cost_item_"+k+"'>") ;
									break_loop = 1 ;
								} else if (\$("#costing_event_"+k).html() && typeof \$("input[name='added_"+full_half_loop[f_h]+"_day_cost_item_"+k+"']").val() == 'undefined') {
									\$("#costing_event_"+k).append("<input value='"+event_cost_id+"' name='added_"+full_half_loop[f_h]+"_day_cost_item_"+k+"'>") ;
									\$("input[name='added_"+f_t_h[full_half_loop[f_h]]+"_day_cost_item_"+k+"']").remove() ; 
								} else {
									\$("input[name='added_"+full_half_loop[f_h]+"_day_cost_item_"+k+"']").val(event_cost_id) ;
								}
								
								\$("#selectDescription_costing_event_"+k).val(event_cost_id).trigger("chosen:updated") ; 
								\$("#inputQty_costing_event_"+k).val(nr_days[full_half_loop[f_h]]*cnt_systems) ; 
								\$("#inputAmount_usd_costing_event_"+k).val(default_rate) ; 
								calc_totals(k,"costing_event") ;
								
								k++ ; 
								
							}				
						}
					}
					
					if (!\$("#costing_event_"+i).html() || break_loop) {
						i-- ; j-- ;
						break ; 
					} 
					
				}
				
				if (need_to_shift_data_below_down) {
					
					for (let row_nr in add_html_row) {
						add_quote_expenses_row_all("costing_event_"+row_nr,"costing_event") ;
						\$("#selectDescription_costing_event_"+row_nr+"_chosen").css("width","90%") ;
					}
					
					for (let key in select_new_values) {
						if (select_new_values[key] != \$(key).val()) {
							\$(key).val(select_new_values[key]).trigger("chosen:updated") ;
						}
					}
					
					for (let key in input_new_values) {
						if (typeof \$(key).val() == 'undefined' && input_new_values[key]) {
							let display = (hidden_input_name[key].includes("_day_cost_item_")) ? "" : "" ;
							\$(hidden_input_to_html_row[key]).append("<input value='"+input_new_values[key]+"' name='"+hidden_input_name[key]+"' "+display+">") ;
						}
						if (input_new_values[key] != \$(key).val()) {
							\$(key).val(input_new_values[key]) ;
						}
					}
					
					for (let key in checkbox_new_values) {
						if (\$(key).is(":checked") && !checkbox_new_values[key]) {
							\$(key).prop("checked",false) ;
						} else if (!\$(key).is(":checked") && checkbox_new_values[key]) {
							\$(key).prop("checked",true) ;
						}
					}
					
					for (let key in cals_totals_row) {
						calc_totals(key,"costing_event") ;
					}
					
					if (j > i) {
						for (let k=i+1;k<=j;k++) {
							\$("#costing_event_"+k).show() ;
							\$("#btn_row_costing_event_"+k).hide() ;
							\$("#btn_costing_event_"+k).hide() ;
						}
						let k = j+1 ;
						\$("#btn_row_costing_event_"+k).show() ;
						\$("#btn_costing_event_"+k).show() ;
					}
					
					// let [sub_total,vat_total] = add_up_totals ("costing_event",j,\$("input[name='roe']").val(),0) ;  
					// \$("input[name='income']").val((sub_total+vat_total).toFixed(2)) ; 
					// update_overall_totals(sub_total,vat_total,"costing_event") ; 
					
				} 
			}
			
			\$("#content").on("change","[id^='radio_']", function () {
				
				// [nr_days["full"],nr_days["half"]] = count_nr_of_full_day_and_half_days() ;
				update_qty_in_quote_tab_when_changing_nr_of_full_or_half_days () ;
				
			}) ; 
			
			function update_qty_in_quote_tab_when_changing_nr_of_full_or_half_days () {

				[nr_days["full"],nr_days["half"]] = count_nr_of_full_day_and_half_days() ;
				
				let opp_add = {"full":"half","half":"full"} ; 
				
				let i = 0 ; let j = 0 ; let select_new_values = {} ; let input_new_values = {} ; let checkbox_new_values = {} ;
				
				if (!prev_nr_days["full"] && !prev_nr_days["half"] && (nr_days["full"] || nr_days["half"])) {
					// return ;
				}
				
				let add = 
					(!prev_nr_days["full"] && !prev_nr_days["half"] && nr_days["full"]) ? "blank_full" : 
					(!prev_nr_days["half"] && !prev_nr_days["full"] && nr_days["half"]) ? "blank_half" : 
					(!prev_nr_days["full"] && nr_days["full"]) ? "full" : 
					(!prev_nr_days["half"] && nr_days["half"]) ? "half" :  
					"" ;
					
				let remove = 
					(prev_nr_days["full"] && !nr_days["full"]) ? "full" : 
					(prev_nr_days["half"] && !nr_days["half"]) ? "half" : 
					"" ;
					
				let change = 
					(prev_nr_days["full"] != nr_days["full"] && prev_nr_days["half"] != nr_days["half"] && prev_nr_days["full"] && nr_days["full"] && prev_nr_days["half"] && nr_days["half"]) ? "both" :
					(prev_nr_days["full"] && nr_days["full"] && prev_nr_days["full"] != nr_days["full"]) ? "full" : 
					(prev_nr_days["half"] != nr_days["half"] && prev_nr_days["half"] && nr_days["half"]) ? "half" : 
					"" ; 

				let hidden_input_to_html_row = {} ; let hidden_input_name = {} ; let add_html_row = {} ; let remove_input = {} ; let cals_totals_row = {} ;

				while (true) {
					
					i++ ;  j++ ;
					
					if (!\$("#costing_event_"+i).html()) {
						i-- ;  j-- ;
						break ;
					} 
					
					let added_full_cost_item = \$("input[name='added_full_day_cost_item_"+i+"']").val() ; 
					let added_half_cost_item = \$("input[name='added_half_day_cost_item_"+i+"']").val() ; 
					
					let event_cost_id = (added_full_cost_item) ? added_full_cost_item : (added_half_cost_item) ? added_half_cost_item : "" ;
					
					let event_cost_id_extract = (event_cost_id.includes("_")) ? get_row_nr(event_cost_id) : event_cost_id ;
					
					let added = (added_full_cost_item) ? "full" : (added_half_cost_item) ? "half" : "" ;
					
					if ((added && add && added == add) || (add == "blank_full" && add == "full") || (add == "blank_half" && add == "half")) {
						continue ;
					}
					
					let do_not_remove_this_row = (event_cost_id.includes("-1") || event_cost_id.includes("-2")) ? 1 : 0 ; 
					
					if (event_cost_id && event_cost_id != "") {

						let change_row = (change && (change == added || change == "both")) ? added : 0 ; 
						
						let rate = (event_cost_id.includes("-1")) ? \$("input[name='amount_usd_costing_event_"+i+"']").val() : default_cost_item_rates[added+","+event_cost_id_extract] ;
						// let rate = \$("input[name='amount_usd_costing_event_"+i+"']").val() ;
						let qty = (event_cost_id.includes("-2")) ? \$("input[name='qty_costing_event_"+i+"']").val() : (!cnt_systems_per_cost_item[event_cost_id_extract]) ? nr_days[added] : nr_days[added]*cnt_systems_per_cost_item[event_cost_id_extract] ;

						if (!qty) { qty = 0 ; }
						
						if (change_row) {  
							input_new_values["input[name='qty_costing_event_"+j+"']"] = qty ;
							input_new_values["input[name='amount_usd_costing_event_"+j+"']"] = rate ;
							// input_new_values["input[name='amount_ttl_costing_event_"+j+"']"] = (qty*rate).toFixed(2) ;
							if (i != j) {
								select_new_values["#selectDescription_costing_event_"+j] = \$("#selectDescription_costing_event_"+i).val() ;
								checkbox_new_values["#checkboxVat_costing_event_"+j] = (\$("#checkboxVat_costing_event_"+i).is(":checked")) ? 1 : 0 ; 
								input_new_values["input[name='added_"+change_row+"_day_cost_item_"+j+"']"] = event_cost_id ; 
								hidden_input_to_html_row["input[name='added_"+change_row+"_day_cost_item_"+j+"']"] = "#costing_event_"+j ;							
								hidden_input_name["input[name='added_"+change_row+"_day_cost_item_"+j+"']"] = "added_"+change_row+"_day_cost_item_"+j ;		
								\$("input[name='added_"+change_row+"_day_cost_item_"+i+"']").remove() ;	
							}
							cals_totals_row[j] = 1 ;
							if (!add || event_cost_id.includes("-3")) {
								continue ;
							}
						}
						
						if (add == "full") {
							\$("input[name='added_"+added+"_day_cost_item_"+i+"']").remove() ;		
							j++ ;
						}

						if (i != j && ((add && add != added && added != remove) || (!do_not_remove_this_row && remove && remove != added))) {
							
							select_new_values["#selectDescription_costing_event_"+j] = \$("#selectDescription_costing_event_"+i).val() ;
							input_new_values["input[name='qty_costing_event_"+j+"']"] = qty ;
							input_new_values["input[name='amount_usd_costing_event_"+j+"']"] = rate ;
							// input_new_values["input[name='amount_ttl_costing_event_"+j+"']"] = (qty*rate).toFixed(2) ;
							checkbox_new_values["#checkboxVat_costing_event_"+j] = (\$("#checkboxVat_costing_event_"+i).is(":checked")) ? 1 : 0 ;
							if (!\$("#costing_event_"+j).html()) {
								add_html_row[j] = 1 ;
								console.log("add_html_row : "+j) ; 
								input_new_values["input[name='added_"+added+"_day_cost_item_"+j+"']"] = event_cost_id ;
								hidden_input_to_html_row["input[name='added_"+added+"_day_cost_item_"+j+"']"] = "#costing_event_"+j ;
								hidden_input_name["input[name='added_"+added+"_day_cost_item_"+j+"']"] = "added_"+added+"_day_cost_item_"+j ;
							} 
							cals_totals_row[j] = 1 ;
							
						}

						if (add == "half") {
							if (i != j) {
								\$("input[name='added_full_day_cost_item_"+i+"']").remove() ;		
								input_new_values["input[name='added_"+added+"_day_cost_item_"+j+"']"] = event_cost_id ;
								hidden_input_to_html_row["input[name='added_"+added+"_day_cost_item_"+j+"']"] = "#costing_event_"+j ;
								hidden_input_name["input[name='added_"+added+"_day_cost_item_"+j+"']"] = "added_"+added+"_day_cost_item_"+j ;
							}
							
							j++ ;
						} 
						
						if (remove && remove == added && !add) {
							if (do_not_remove_this_row) {
								continue ;
							}
							j-- ;
							\$("#selectDescription_costing_event_"+i).val("").trigger("chosen:updated") ;
							\$("input[name='qty_costing_event_"+i+"']").val("") ;
							\$("input[name='amount_usd_costing_event_"+i+"']").val("") ;
							\$("input[name='amount_ttl_costing_event_"+i+"']").val("") ;
							\$("input[name='added_"+remove+"_day_cost_item_"+i+"']").remove() ;
							continue ;
						} else if (!do_not_remove_this_row && remove && remove == added && add) {
							if (add == "half") {
								j-- ;
							}
							\$("input[name='added_"+remove+"_day_cost_item_"+i+"']").remove() ;
						}
						
						let jj = (add == "full") ? j - 1 : (add == "half") ? j : j ;
						
						if (add) {
							if (!\$("#costing_event_"+jj).html()) {
								add_html_row[jj] = 1 ;
								// console.log("add : "+add) ; 
							}
							if (!do_not_remove_this_row && remove == "half") {
								j-- ;
							}
							
							rate = default_cost_item_rates[add+","+event_cost_id_extract] ;
							qty = (!cnt_systems_per_cost_item[event_cost_id_extract]) ? nr_days[add] : nr_days[add]*cnt_systems_per_cost_item[event_cost_id_extract] ;
							
							// select_new_values["#selectDescription_costing_event_"+jj] = \$("#selectDescription_costing_event_"+i).val() ;
							select_new_values["#selectDescription_costing_event_"+jj] = event_cost_id_extract ;
							input_new_values["input[name='qty_costing_event_"+jj+"']"] = qty ;
							input_new_values["input[name='amount_usd_costing_event_"+jj+"']"] = rate ;
							// input_new_values["input[name='amount_ttl_costing_event_"+jj+"']"] = (rate*qty).toFixed(2) ;
							input_new_values["input[name='added_"+add+"_day_cost_item_"+jj+"']"] = (event_cost_id.includes("-3")) ? "-3_"+event_cost_id_extract : event_cost_id_extract ; 
							hidden_input_to_html_row["input[name='added_"+add+"_day_cost_item_"+jj+"']"] = "#costing_event_"+jj ;							
							hidden_input_name["input[name='added_"+add+"_day_cost_item_"+jj+"']"] = "added_"+add+"_day_cost_item_"+jj ;	
							if (add != added && \$("input[name='added_"+added+"_day_cost_item_"+jj+"']").val()) {
								// \$("input[name='added_"+added+"_day_cost_item_"+jj+"']").remove() ;
								remove_input["input[name='added_"+added+"_day_cost_item_"+jj+"']"] = 1 ;
							}
							cals_totals_row[jj] = 1 ;
						} 
					} else if (!event_cost_id && i != j) {
						select_new_values["#selectDescription_costing_event_"+j] 			= \$("#selectDescription_costing_event_"+i).val() ;
						input_new_values["input[name='qty_costing_event_"+j+"']"] 			= \$("input[name='qty_costing_event_"+i+"']").val() ;
						input_new_values["input[name='amount_usd_costing_event_"+j+"']"] 	= \$("input[name='amount_usd_costing_event_"+i+"']").val() ;
						input_new_values["input[name='amount_ttl_costing_event_"+j+"']"] 	= \$("input[name='amount_ttl_costing_event_"+i+"']").val() ;
						checkbox_new_values["#checkboxVat_costing_event_"+j]	 			= (\$("#checkboxVat_costing_event_"+i).is(":checked")) ? 1 : 0 ;
						if (!\$("#costing_event_"+j).html()) {
							add_html_row[j] = 1 ; 
						}
						// cals_totals_row[j] = 1 ;
						
					} else if (!event_cost_id && (add == "blank_full" || add == "blank_half")) {

						let k = j + 1 ;
						
						if (\$("#selectDescription_costing_event_"+j).val() && !\$("#costing_event_"+k).html()) {
							j++ ;
						}

						if (!\$("#selectDescription_costing_event_"+j).val()) {
							
							let ij = 0 ; let add_type = (add == "blank_full") ? "full" : "half" ; 
							
							while (true) {
								
								ij++ ;
								if (!\$("#system_name_row_"+ij).html()) {
									break ;
								}
								
								if (default_cost_item_rates[add_type+","+get_event_cost_id[\$("#selectSystem_name_"+ij).val()]]) {
									
									if (!\$("#costing_event_"+j).html()) {
										add_html_row[j] = 1 ;   
									} else {
										\$("#checkboxExcl_costing_event_"+j).prop("checked",false) ; 
									}
									
									select_new_values["#selectDescription_costing_event_"+j] 							= get_event_cost_id[\$("#selectSystem_name_"+ij).val()] ;
									input_new_values["input[name='qty_costing_event_"+j+"']"] 							= nr_days[add_type] ;
									input_new_values["input[name='amount_usd_costing_event_"+j+"']"] 					= default_cost_item_rates[add_type+","+get_event_cost_id[\$("#selectSystem_name_"+ij).val()]] ;
									// input_new_values["input[name='amount_ttl_costing_event_"+j+"']"] 					= (nr_days[add_type]*default_cost_item_rates[add_type+","+get_event_cost_id[\$("#selectSystem_name_"+ij).val()]]).toFixed(2)  ;
									checkbox_new_values["#checkboxVat_costing_event_"+j]	 							= 1 ;				
									input_new_values["input[name='added_"+add_type+"_day_cost_item_"+j+"']"] 			= get_event_cost_id[\$("#selectSystem_name_"+ij).val()] ; ;
									hidden_input_to_html_row["input[name='added_"+add_type+"_day_cost_item_"+j+"']"] 	= "#costing_event_"+j ;
									hidden_input_name["input[name='added_"+add_type+"_day_cost_item_"+j+"']"] 			= "added_"+add_type+"_day_cost_item_"+j ;
									cals_totals_row[j] = 1 ;
									j++ ;
								}
							}
							j-- ;
							break ;
							
						}
					} else if (event_cost_id.includes("-3")) {

						let rate = (event_cost_id.includes("-1")) ? \$("input[name='amount_usd_costing_event_"+i+"']").val() : default_cost_item_rates[added+","+event_cost_id_extract] ;
						// let rate = \$("input[name='amount_usd_costing_event_"+i+"']").val() ;
						let qty = (event_cost_id.includes("-2")) ? \$("input[name='qty_costing_event_"+i+"']").val() : (!cnt_systems_per_cost_item[event_cost_id_extract]) ? nr_days[added] : nr_days[added]*cnt_systems_per_cost_item[event_cost_id_extract] ;
						
						if (!qty) { qty = 0 ; }
						
						input_new_values["input[name='qty_costing_event_"+j+"']"] = qty ;
						input_new_values["input[name='amount_usd_costing_event_"+j+"']"] = rate ;
						// input_new_values["input[name='amount_ttl_costing_event_"+j+"']"] = (qty*rate).toFixed(2) ;
						cals_totals_row[j] = 1 ;
						
					}
				}

				for (let key in remove_input) {
					\$(key).remove() ;
				}
				
				for (let row_nr in add_html_row) {
					add_quote_expenses_row_all("costing_event_"+row_nr,"costing_event") ; 
					\$("#selectDescription_costing_event_"+row_nr+"_chosen").css("width","90%") ;
				}
				
				for (let key in select_new_values) {
					if (select_new_values[key] != \$(key).val()) {
						\$(key).val(select_new_values[key]).trigger("chosen:updated") ;
					}
				}
				
				for (let key in input_new_values) {
					if (typeof \$(key).val() == 'undefined' && input_new_values[key]) {
						let display = (hidden_input_name[key].includes("_day_cost_item_")) ? "" : "" ;
						\$(hidden_input_to_html_row[key]).append("<input value='"+input_new_values[key]+"' name='"+hidden_input_name[key]+"' "+display+">") ; 
					}
					if (input_new_values[key] != \$(key).val()) { 
						\$(key).val(input_new_values[key]) ;
					}
				}
				
				for (let key in checkbox_new_values) {
					if (\$(key).is(":checked") && !checkbox_new_values[key]) {
						\$(key).prop("checked",false) ;
					} else if (!\$(key).is(":checked") && checkbox_new_values[key]) {
						\$(key).prop("checked",true) ;
					}
				}
				
				for (let key in cals_totals_row) {
					calc_totals(key,"costing_event") ;
				}
				
				if (remove && !j) {
					j++ ;
				}

				if (j < i) {
					for (let k=j+1;k<=i+1;k++) {
						\$("#costing_event_"+k).html("") ;
						if (k == j+1) {
							\$("#btn_row_costing_event_"+k).show() ;
							\$("#btn_costing_event_"+k).show() ;
						} else {
							\$("#btn_row_costing_event_"+k).hide() ;
							\$("#btn_costing_event_"+k).hide() ;
						}
					}
				} else if (j > i) {
					for (let k=i+1;k<=j;k++) {
						\$("#costing_event_"+k).show() ;
						\$("#btn_row_costing_event_"+k).hide() ;
						\$("#btn_costing_event_"+k).hide() ;
					}	
					let k = j+1 ; 
					\$("#btn_row_costing_event_"+k).show() ;
					\$("#btn_costing_event_"+k).show() ;
				}
				
				let [sub_total,vat_total] = add_up_totals ("costing_event",j,\$("input[name='roe']").val(),0) ;  
				\$("input[name='income']").val((sub_total+vat_total).toFixed(2)) ; 
				update_overall_totals(sub_total,vat_total,"costing_event") ; 

				prev_nr_days["full"] = nr_days["full"] ;
				prev_nr_days["half"] = nr_days["half"] ;
				
			}
			
			\$("#content").on("change","[id^='selectSystem_name_']", function () {
				
				let corresponding_cost_item_exists = ((default_cost_item_rates["full,"+get_event_cost_id[\$(this).val()]] && nr_days["full"]) || (default_cost_item_rates["half,"+get_event_cost_id[\$(this).val()]] && nr_days["half"])) ? 1 : 0 ;
				
				if (corresponding_cost_item_exists) {
					system_with_cost_item_rates_selected = 1 ;
				} else if (system_with_cost_item_rates_selected && (!\$(this).val() || (\$(this).val() && !corresponding_cost_item_exists))) {
					system_with_cost_item_rates_selected = 0 ;
					let ij = 0 ;
					while (true) { 
						ij++ ;
						if (!\$("#system_name_row_"+ij).html()) {
							break ;
						}
						if (!\$("#selectSystem_name_"+ij).val()) {
							continue ;
						}
						if ((default_cost_item_rates["full,"+get_event_cost_id[\$("#selectSystem_name_"+ij).val()]] && nr_days["full"]) || (default_cost_item_rates["half,"+get_event_cost_id[\$("#selectSystem_name_"+ij).val()]] && nr_days["half"])) {
							system_with_cost_item_rates_selected = 1 ;
						}
					}
				}
				
				auto_select_cost_items_in_quote_tab(this.id) ;
				
			}) ;
			
			// \$("#content").on("change","[id^='selectDescription_']", async function () {
				
				// let this_id = this.id ;  
				
				// let row_nr = get_row_nr(this_id) ; 
				
				// let tab = (this_id.includes("_costing_event_")) ? "costing_event" : (this_id.includes("_workings_event_")) ? "workings_event" : "" ;
				
				// // if (!\$("input[name='added_half_day_cost_item_"+row_nr+"']").val() && !\$("input[name='added_half_day_cost_item_"+row_nr+"']")val() && ((nr_days["full"] && default_cost_item_rates["full,"+\$("#"+this_id).val()]) || (nr_days["full"] && default_cost_item_rates["full,"+\$("#"+this_id).val()]))) {
					
					// // if (nr_days["full"] && default_cost_item_rates["full,"+\$("#"+this_id).val()]) {
						
						// // "input[name='qty_costing_event_"+j+"']"
						
					// // }
					
					
					// // if (nr_days["full"] && default_cost_item_rates["full,"+\$("#"+this_id).val()] && nr_days["half"] && default_cost_item_rates["half,"+\$("#"+this_id).val()]) {
						
						// // let j = row_nr + 1 ;
						// // select_new_values["#selectDescription_costing_event_"+j] 							= \$(this).val() ;
						// // input_new_values["input[name='qty_costing_event_"+j+"']"] 							= nr_days["half"] ;
						// // input_new_values["input[name='amount_usd_costing_event_"+j+"']"] 					= default_cost_item_rates["half,"+\$(this).val()] ;
						// // input_new_values["input[name='amount_ttl_costing_event_"+j+"']"] 					= (nr_days["half"]*default_cost_item_rates["half,"+\$(this).val()]).toFixed(2)  ;
						// // checkbox_new_values["#checkboxVat_costing_event_"+j]	 							= 1 ;				
						// // input_new_values["input[name='added_half_day_cost_item_"+j+"']"] 					= \$(this).val() ;
						// // hidden_input_to_html_row["input[name='added_half_day_cost_item_"+j+"']"] 			= "#costing_half"+add_type+"_day_cost_item_"+j ;
						// // \$("input[name='added_full_day_cost_item_"+j+"']").remove() ;
						
						// // // let ij = j ; 
						
						// // // while (true) {
							// // // select_new_values["#selectDescription_costing_event_"+j] 							= \$("#selectDescription_costing_event_"+j).val() ;
							// // // input_new_values["input[name='qty_costing_event_"+j+"']"] 							= \$("input[name='qty_costing_event_"+j+"']").val() ;
							// // // input_new_values["input[name='amount_usd_costing_event_"+j+"']"] 					= \$("input[name='amount_usd_costing_event_"+j+"']") ;
							// // // input_new_values["input[name='amount_ttl_costing_event_"+j+"']"] 					= (nr_days["half"]*default_cost_item_rates["half,"+\$(this).val()]).toFixed(2)  ;
							// // // checkbox_new_values["#checkboxVat_costing_event_"+j]	 							= 1 ;				
							// // // input_new_values["input[name='added_half_day_cost_item_"+j+"']"] 					= \$(this).val() ;
							// // // hidden_input_to_html_row["input[name='added_half_day_cost_item_"+j+"']"] 			= "#costing_half"+add_type+"_day_cost_item_"+j ;
							// // // \$("input[name='added_full_day_cost_item_"+j+"']").remove() ;							
						// // // }
						
					// // } 
					
				// // }
				
				
				
				// if (\$("input[name='amount_usd_"+tab+"_"+row_nr+"']").val()) {
					// // return ;
				// }
				
				// let full_day_exists = nr_days["full"] ; let half_day_exists = nr_days["half"] ; 
				
				// // [full_day_exists,half_day_exists] = count_nr_of_full_day_and_half_days() ; 
				
				// let already_loaded_full_day = (\$("#"+this_id).val()) ? \$("input[name='full_"+tab+"_"+\$("#"+this_id).val()+"']").val() : "" ; 
				// let already_loaded_half_day = (\$("#"+this_id).val()) ? \$("input[name='half_"+tab+"_"+\$("#"+this_id).val()+"']").val() : "" ; 
				
				// already_loaded_full_day = (typeof already_loaded_full_day == 'undefined') ? "" : 1 ; 
				// already_loaded_half_day = (typeof already_loaded_half_day == 'undefined') ? "" : 1 ;
				
				// if (already_loaded_full_day && already_loaded_half_day) {
					// return ;
				// }
				
				// if (already_loaded_full_day == 1) {
					// full_day_exists = 0 ; 
				// }
				
				// if (already_loaded_half_day == 1) {
					// half_day_exists = 0 ; 
				// }
				
				// let date_from = \$("input[name='date_from']").val() ;
				
				// date_from = date_from.substring(0,10) ;
				
				// let full_half_day = 0 ; 
				
				// // if (full_day_exists || (!already_loaded_full_day && already_loaded_half_day && !full_day_exists)) {
				// if (full_day_exists != 0) {
					// if ((typeof \$("input[name='full_"+tab+"_"+\$("#"+this_id).val()+"']").val() == 'undefined' || \$("input[name='full_"+tab+"_"+\$("#"+this_id).val()+"']").val()) && \$("#"+this_id).val()) {
						// \$("#content").append("<input style='display:none;' value='1' name='full_"+tab+"_"+\$("#"+this_id).val()+"'>") ;
					// }
					// full_half_day = 1 ;
				// // } else if (half_day_exists || (already_loaded_full_day && !half_day_exists && !already_loaded_half_day)) {
				// } else if (half_day_exists != 0) {
					// if ((typeof \$("input[name='half_"+tab+"_"+\$("#"+this_id).val()+"']").val() == 'undefined' || !\$("input[name='half_"+tab+"_"+\$("#"+this_id).val()+"']").val()) && \$("#"+this_id).val()) {
						// \$("#content").append("<input style='display:none;' value='1' name='half_"+tab+"_"+\$("#"+this_id).val()+"'>") ;
					// }
					// full_half_day = 2 ;
				// }

				// let rate_ = "" ;
				
				// if (default_cost_item_rates["full,"+\$("#"+this_id).val()] && full_half_day == 1) {
					// rate_ = default_cost_item_rates["full,"+\$("#"+this_id).val()] ;
				// } else if (default_cost_item_rates["half,"+\$("#"+this_id).val()] && full_half_day == 2) {
					// rate_ = default_cost_item_rates["half,"+\$("#"+this_id).val()] ;
				// } else if (full_half_day) {
					// let rate_url = "$useropts{scripts}/get/get_event_cost_item_rates.pl?"+\$("#"+this_id).val()+"&"+date_from+"&"+full_half_day+"&"+this_id ;
					// await \$.get(rate_url, function(json) {
						// \$.each(json, function(key, data) {
							// rate_ = data.rate ;
						// });
					// },'json') ;				
				// }
				
				// if (rate_ == "") {
					// return ;
				// }
				
				// // let tab = "" ;
				
				// // if (this_id.includes("_costing_event_")) {
					// // tab = "costing_event" ;
				// // } else if (this_id.includes("_workings_event_")) {
					// // tab = "workings_event" ;
				// // }
				
				// full_half_day = (full_half_day == 1) ? "full" : (full_half_day == 2) ? "half" : "" ;
				
				// \$("input[name='qty_"+tab+"_"+row_nr+"']").val(nr_days[full_half_day]) ;
				// \$("input[name='amount_usd_"+tab+"_"+row_nr+"']").val(rate_) ;
				// \$("input[name='amount_ttl_"+tab+"_"+row_nr+"']").val((rate_*nr_days[full_half_day]).toFixed(2)) ;
				// \$("#"+tab+"_"+row_nr).append("<input value='-3_"+\$(this).val()+"' name='added_"+full_half_day+"_day_cost_item_"+row_nr+"'>") ;
				// calc_totals(row_nr,tab) ;
				
				// // amount_usd_costing_event_1
				// // amount_usd_workings_event_1
				
			// }) ;
			
			
			// \$("#content").on("change","[id^='selectSystem_name_']", function () {
				 
			// }) ; 
			
		~ ;

		# --------------- END COSTING ----------------------------------------------------------------------------------------------------------------
	}

	if ($tab == 5) { # Notes

		# --------------- START NOTES ----------------------------------------------------------------------------------------------------------------

		# # my $sec = '_notes' ; 
		
		# # my ($sec_name,$sec_cnt,$sec_col) = &tab_sec_det($sec) ;
		
		# # $ret_pdf_link_sec .= $sec . '+' . $sec_cnt . '|' ;
		
		# # return if $ret ;
		
		# # $add_form_fields = '' ;
		
		# # &db_min_ro('quote_event_notes','*','','','') ;
		
		# # $add_form_fields .= qq~
			# # <div class='row'>
				# # <div class='col-md-12'>
					# # <h4>
						# # Notes:
					# # </h4>
				# # </div>
			# # </div>~ ;
			
		# # &tab_build_terms_headers ;
			
		# # $cntnoteloop = 1 ; 
		# # for (1 .. 5) { 
			# # &tab_build_terms_fields('note',$_,$id) ; 
		# # }
		
		 # # $add_form_fields .= qq~
			# # <div class='row'>
				# # <div class='col-md-12'>
					# # <h4>
						# # Payment Terms:
					# # </h4>
				# # </div>
			# # </div>~ ;
		# # &tab_build_terms_headers ;
		
		# # $cntnoteloop = 1 ; 
		# # for (1 .. 5) { 
			# # &tab_build_terms_fields('payment_term',$_,$id) ; 
		# # }
		# # &event_tabs_content_box($sec_name,$sec_col) ;

		# --------------- END NOTES ----------------------------------------------------------------------------------------------------------------
	}
	
	if ($tab == 6) { # Workings

		# --------------- START Working ----------------------------------------------------------------------------------------------------------------
	
	$add_form_fields = '' ;

	$lcol = 1 ; 
	$fcol = 2 ;

	my @docs = ('Slip') ;
	
	$add_form_fields .= qq~
		<div class='row'>
	~ ;

	foreach (@docs) {
		my $doc = lc $_ ; $doc =~ s/ /\_/ ; $doc =~ s/&/\_/ ;
		$event_uploads = &common_get_field_uploads($id,"$doc",'id') ;
		if ($event_uploads) {
			$add_form_fields .= qq~
			<div class="col-md-3" id="costings">
				$event_uploads
			</div>~; 
		} else {
			$add_form_fields .= qq~
			<div class="col-md-3">
				<div class="form-group">
					<label for="imageInputFile">Attach Document $_</label>
					<input type="file" id="imageInputFile" name='iattach$doc' tabindex='$tindex'>
					<p class="help-block">Upload file from PC.</p>
				</div>
			</div>
			~;		
		}
	}
	
	$event_uploads = '' ; 
	# $add_form_fields .= &common_min_form_label_col("Custom Set") ; 
	
	my $custom_set_checked = ($db{$table}{$id}{custom_set}) ? "CHECKED" : "" ;
	
	$add_form_fields .= qq~
			<div class="col-md-3">
				<label>Custom Set Operator Amounts : </label> 
				<input type="checkbox" name="custom_set" value="1" $custom_set_checked>
			</div>
	~ ; #  class="control-label"
	# $add_form_fields .= &common_min_form_checkbox_col('custom_set','') ; 
	
	$add_form_fields .= qq~
		</div>
	~ ;

		our %sec_cnt = ('a_event' => "50-List Expenses") ;
		
		my $sec = 'workings' ; 
		my ($sec_name, $sec_cnt_workings, $sec_col) = &tab_sec_det($sec) ;	
		$ret_pdf_link_sec .= $sec . '+' . $sec_cnt_workings . '|' ;
		
		return if $ret ;
		
		&event_tabs_build_sec($sec,$tab) ;
		
		foreach (sort keys %sec_cnt) {
			$_k = substr($_, 1) ;
			my @parts = split(/\-/, $sec_cnt{$_}) ;
			$add_form_fields .= qq~
				<div class='row'>
					<div class='col-md-12'>
						<h4>
							$parts[1] :
						</h4>
					</div>
				</div>
			~ if $parts[1] ne 'Operators';
			
			$add_form_fields .= qq~
				<div class='row'>
					<div class='col-md-12'>
						<div class='col-md-1'>
							<h4>
								$parts[1] :
							</h4>
						</div>
						$icon_casuals 
					</div>
				</div>
			~ if $parts[1] eq 'Operators' ;
			
			&tab_build_qt_col_headers_workings("$sec$_k") ;	
			# # # &db_min_ro('event_quotes_min', 'id, remarks_workings_event, supplier_workings_event, ref_nr_workings_event, qty_workings_event, amount_usd_workings_event, amount_workings_event, vat_workings_event, description_workings_event', "id = $id", '', '') if $id ;
			&event_tabs_build_costs_forms($id,$tab,$parts[0],"$sec$_k") ; 			
		}
		
		$add_form_fields .= qq~
			<div class='row'>
				<div class="box col-md-12">
					&nbsp;
				</div>
			</div>~ ;
		$add_form_fields .= qq~
			<div class='row'>
				<div class="box col-md-8">
					&nbsp;
				</div>
			~ ;
		
		my @ttlarr = ("sub_total", "vat_total", "grand_total") ;
		$lcol = 1;
		$fcol = 1;
		foreach (@ttlarr) {
			$lcol = 1; 
			$add_form_fields .= &common_min_form_label_col($_) ;   
		}
			
		$add_form_fields .= qq~</div>~ ;
		
		$trigger_jquery_raw .= qq~
			\$("#textareaAdditional_notes").change(function () {
				if (\$(this).val()) {
					\$('label[for="additional_notes_expenses"]').text('Additional Notes : ') ;
					\$("#textareaAdditional_notes_expenses_input").html(\$(this).val()) ; 
				} else if (!\$(this).val() && \$('label[for="additional_notes_expenses"]').text()) {
					\$('label[for="additional_notes_expenses"]').text('') ;
					\$("#textareaAdditional_notes_expenses_input").html('') ;					
				}
			}) ;
		~ ; 
		
		my $add1 = ($db{$table}{$id}{additional_notes}) ? "Additional Notes : " : "" ;
		my $add2 = ($db{$table}{$id}{additional_notes}) ? "$db{$table}{$id}{additional_notes}" : "" ;
		
		$add_form_fields .= qq~<div class='row'><div class="box col-md-1"><label for='additional_notes_expenses'>$add1</label></div><div class="box col-md-4"><p id='textareaAdditional_notes_expenses_input' class="help-block">$add2</p></div><div class="box col-md-1"></div>~ ; 
		$lcol = 2;
		$add_form_fields .= &common_min_form_label_col('Total') ; 
		$lcol = 1;

		my @ttlarr = ("total_amount_workings_event","total_vat_amount_workings_event","total_grand_amount_workings_event");
		$lcol = 1 ; $fcol = 1 ;
		
		my %renamed_totals = ("total_amount_workings_event" => "sub_total_workings_event", "total_vat_amount_workings_event" => "vat_total_workings_event" , "total_grand_amount_workings_event" => "grand_total_workings_event") ; 
		
		foreach (@ttlarr) {			
			$readonly{$renamed_totals{$_}} = "READONLY"; 
			$input_style{$renamed_totals{$_}} = qq~style="text-align:right;"~ ;
			my $val = $db{$table}{$id}{$_} ? $db{$table}{$id}{$_} : '0.00' ;
			$add_form_fields .= &common_min_form_input_col($renamed_totals{$_},$val) ; 			
		}
		
		$add_form_fields .= qq~</div>~ ;
			
		$add_form_fields .= qq~
			<div class='row'>
				<div class="box col-md-12">
					&nbsp;
				</div>
			</div>~ ;
		
		&event_tabs_content_box($sec_name,$sec_col) ;

		# --------------- END WORKING ----------------------------------------------------------------------------------------------------------------
	}

	if ($tab == 7) { # Invoice Details

		# --------------- START INVOICE DETAILS --------------------------------------------------------------------------------------------------------
		
		my $sec = '_invoice_details' ; my ($sec_name,$sec_cnt,$sec_col) = &tab_sec_det($sec) ;
		
		$ret_pdf_link_sec .= $sec . '+' . $sec_cnt . '|' ;
		
		return if $ret ; 
		
		$lcol = 3 ;
		$fcol = 5 ;
		
		$add_form_fields = '' ;
		
		$add_form_fields .= &common_min_form_input('invoice_nr',$db{$table}{$id}{invoice_nr},'') ;
		my $checked = 'CHECKED' if $db{$table}{$id}{annual_invoice} ;
		$add_form_fields .= &common_min_form_input('po_nr',$db{$table}{$id}{po_nr},'') ;
		$add_form_fields .= &common_min_form_datepicker('invoice_date',$db{$table}{$id}{invoice_date}) ;
		
		&event_tabs_content_box($sec_name,$sec_col);
		
		# --------------- END INVOICE DETAILS ----------------------------------------------------------------------------------------------------------
	}

} #------------------------------------------------------------------------------------
	
sub event_tabs_build_sec {
	
	my ($sec_,$tab) = @_ ;
	
	# # # &db_min_ro('event_quotes_min', 'id,description_workings_event,operator_workings_event', "id = $i{id}", '', '') if $i{id};
	
	my @saved_des = () ; my @saved_op = () ; 

	# my %casual_name_dropdown_hash = () ;
	
	if ($sec_ eq "workings") { 
		@saved_des = split(";", $db{event_quotes_min}{$i{id}}{description_workings_event}) ;
		@saved_op = split(";", $db{event_quotes_min}{$i{id}}{operator_workings_event}) ;
		&db_min_ro('users','*',"user_type LIKE 'casual%' AND block <> 1 AND inactive <> 1",'','') if $iaction eq 'add' ;
		foreach my $uid (sort {$db{users}{$a}{name} cmp $db{users}{$b}{name}} keys %{$db{users}}) { 	
			# $casual_name_dropdown_hash{$uid} = $db{users}{$uid}{name} ;
			$opts{operator_field} .= qq~<option value="$uid">$db{users}{$uid}{name}</option>~ ; 
		}
	} else {
		@saved_des = split(";", $db{event_quotes_min}{$i{id}}{item_costing_event}) ;
	}
	
	unless ($opts{description_field}) {
		$opts{description_field} = qq~~ ;
		my $other_id = 0 ;
		foreach (sort {$db{event_cost_items}{$a}{name} cmp $db{event_cost_items}{$b}{name}} keys %event_list_name_array_expenses) {
			next if $db{event_cost_items}{$_}{excl_from_expenses}  ;
			if (lc $db{event_cost_items}{$_}{name} eq "other") {
				$other_id = $_ ;
				next ; 
			}
			$opts{description_field} .= qq~<option value="$_">$event_list_name_array_expenses{$_}</option>~ ; 
		}
		$opts{description_field} .= qq~<option value="$other_id">Other</option>~ ; 
	
	} 
	
	my @description_chosen_field_ids = () ; my @operator_chosen_field_ids = () ; my @desriptions_that_are_operators = () ;
	
	foreach (sort keys %sec_cnt) {
		
		$_k = substr($_,1) ; $k = substr($_,2) ;
		
		my @parts = split(/\-/,$sec_cnt{$_}) ;

		for my $p_ (1 .. $parts[0]) { # item_1_costing_systems
			
			my $field = "$sec_$_k\_$p_" ;
			my $saved_val = $saved_des[$p_ - 1] ;
			next if $p_ > 1 && !$saved_val ;
			my $saved_val_2 = $saved_op[$p_ - 1] ;
			
			if ($_k eq '_event') {
				$opts{"description_$field"} = $opts{description_field} ; 
				$trigger_jquery_raw .= qq~\$("#selectDescription_$field").val("$saved_val") ; \$("#selectDescription_$field").trigger("chosen:updated") ; ~ if $saved_val and $saved_val ne "14" ; 
				push @desriptions_that_are_operators,"#selectDescription_$field" if $saved_val eq "14" ; 
				
				if ($sec_ eq "workings") { 
					$opts{"operator_$field"} = $opts{operator_field} ; 
					$trigger_jquery_raw .= qq~\$("#selectOperator_$field").val("$saved_val_2") ; \$("#selectOperator_$field").trigger("chosen:updated") ; ~ if $saved_val_2 ;		 
				}
			}
			
			$item_type = 'input' ;	

			unless ($got{$field}) {
				
				$got{$field} = 1 ;
				push @excl_field_ids,"#checkboxExcl_$field" ;
				push @description_field_ids,"#selectDescription_$field" ;
				push @operator_field_ids,"#selectOperator_$field" ;
				push @description_chosen_field_ids,"#selectDescription_$field\_chosen" ;
				push @operator_chosen_field_ids,"#selectOperator_$field\_chosen" ;
				push @vat_field_ids,"#checkboxVat_$field" ;
				push @qty_field_ids,"#inputQty_$field" ;
				push @amount_usd_field_ids,"#inputAmount_usd_$field" ;
				
			}
		}
	}
	
	my $desriptions_that_are_operators_string = join (",",@desriptions_that_are_operators) ;

	$trigger_jquery_raw .= qq~\$("$desriptions_that_are_operators_string").val("14") ; \$("$desriptions_that_are_operators_string").trigger("chosen:updated") ; ~ if $desriptions_that_are_operators_string ; 	
	
	# if ($sec_ eq "demo_expenses") {
		
	$trigger_jquery .= qq~
		\$("#$cntxt{$tab} [id^='select']").filter( function() {
			return this.id.endsWith("_chosen") ;
		}).css("width","90%");
	~ ; 	 
	
	# $trigger_jquery .= qq~\$("#$cntxt{$tab} [id^='select']").css("width","90%");~ ;
		# return ;
	# }
	
	# my $description_chosen_field_ids_string 	= join(",",@description_chosen_field_ids) ; 
	
	# $trigger_jquery .= qq~\$("$description_chosen_field_ids_string").css("width","90%");~ ;
	
	# if ($sec_ eq "workings") {
		# my $operator_chosen_field_ids_string	 	= join(",",@operator_chosen_field_ids) ;
		# $trigger_jquery .= qq~\$("$operator_chosen_field_ids_string").css("width","90%");~ ;		
	# }

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

sub event_tabs_customer_select_opts {

	my ($table,$dispfield,$where,$field) = @_ ;
	
	$where .= " AND " if $where ;
	$where .= "events='1'" ;
	
	&db_min_ro($table,'*',$where,'','') ;

	foreach my $id (sort { $db{$table}{$a}{name} cmp $db{$table}{$b}{name} } keys %{$db{$table}}) {	
			
		my $saved_val = $db{event_quotes}{$i{id}}{$field} ;
		
		if ($saved_val) { my @valarr = split(/\:/,$saved_val) ; $saved_val = $valarr[0] ; }
		
		if ($saved_val eq $id) { $selected = 'SELECTED' ; } else { $selected = '' ; }
		
		$opts{$field} .= qq~<option $selected value="$id:$db{$table}{$id}{contact_name}:$db{$table}{$id}{contact_email}:$db{$table}{$id}{phone}:$db{$table}{$id}{physical_address}">$db{$table}{$id}{$dispfield}</option>~ ; 
		unless ($got{$field}) {
			$trigger_jquery .= qq~\$("#selectQuote_to_chosen").css( "width", "96%" );~ ; 		
			$got{$field} = 1 ;
			$trigger_jquery_raw .= qq~
				\$("#selectQuote_to").change(function() {					
					dispCustomer() ;
				});
			~;
			
			$trigger_jquery_raw .= qq~
				function dispCustomer() {
					var custval = \$("#selectQuote_to").val();
					var arrP    = custval.split(":");
					\$("#inputContact_name").val(arrP[1]);
					\$("#inputAddress").val(arrP[4]);
					\$("#inputEmail").val(arrP[2]);
					\$("#inputTel").val(arrP[3]);
				}
			~;
		}
	}
} #------------------------------------------------------------------------------------------

sub tab_build_terms_headers {

	$add_form_fields .= qq~	
		<div class='row'>
			<div class='col-md-1'>
				<label class='control-label'>
					&nbsp;&nbsp;&nbsp;&nbsp;Excl.
				</label>
			</div>
			<div class='col-md-6'>
				<label class='control-label'>
					Description
				</label>
			</div>
		</div>	
	~ ;
} #------------------------------------------------------------------------------------

sub tab_build_terms_fields {
		
	my ($sec,$cnt,$id) = @_ ;

	my $exfield = "excl_$cnt\_$sec\s" ;
	my $inpfield = "$sec\_$cnt" ;	

	unless ($db{quote_event_notes}{1}{$inpfield}) { 
		$checkit{$exfield} = 1 ; 
	}

	if ($db{$table}{$id}{$inpfield}) { 
		$val = $db{$table}{$id}{$inpfield} ; 
	} else { 
		$val = $db{quote_event_notes}{1}{$inpfield} ; 
	}

	$note_display_style = '' ; $note_display_plus_btn = '' ;	
	unless ($cnt==1 or $db{quote_event_notes}{1}{$inpfield}) { 
		$note_display_style = qq~style='display:none;'~ ; 
		$trigger_jquery_raw .= qq~ \$("#btn_$cnt\_$sec").click(function () {
				\$("#$cnt\_$sec").toggle();
				\$("#btn_$cnt\_$sec").hide();
				var v = $cnt + 1 ;
				\$("#btn_"+v+"\_$sec").show();
			});~ ; 
		if ($cnt>$cntnoteloop) { 
			$note_display_butt = qq~display:none;~ ;  
		} else { 
			$note_display_butt = '' ; 
		}
		$note_display_plus_btn = qq~<a id="btn_$cnt\_$sec" href="#" style="padding-left:16px;$note_display_butt"><i class="glyphicon glyphicon-plus blue"></i></a>~;
	} else { 
		$cntnoteloop++ ; 
	}

	$add_form_fields .= qq~<div class='row' id='$cnt\_$sec' $note_display_style>~ ;

	if ($db{$table}{$id}{$exfield} == 1)  { $checked = 'CHECKED'; } else { $checked = ''; }

	$label{$exfield} = 1 ;
	$fcol=1 ;
	$add_form_fields .= &common_min_form_checkbox_col($exfield,$checkit{$exfield},$checked) ;
	$fcol=6 ;
	$input_style{$inpfield} = qq~style='margin-bottom:4px;margin-top:-2px;'~ ; # add spaces between boxes
	$add_form_fields .= &common_min_form_input_col($inpfield,$val,'') ;
	$add_form_fields .= qq~</div>~ ;
	$add_form_fields .= qq~
		<div class='row'>
			<div class="col-md-1">
				$note_display_plus_btn
			</div>
		</div>
	~ ;
	
} #------------------------------------------------------------------------------------

sub tab_build_qt_right {

	my ($id,$tab,$ret) = @_ ;

	if ($tab==1) {  # Cusotmer Details

		# --------------- START CUSOTMER DETAILS --------------------------------------------------------------------------------------------------------
		
		my $sec = '_office_details' ; 
		my ($sec_name,$sec_cnt,$sec_col) = &tab_sec_det($sec) ;
		
		$ret_pdf_link_sec .= $sec . '+' . $sec_cnt . '|' ;
		
		return if $ret ;

		$lcol = 3 ;
		
		$add_form_fields = '' ;
		
		$fcol = 8 ;
		# $textarea_height{notes} = ($is_schools_manager) ? 102 : 93  ;
		# $textarea_height{notes} = ($glod_user_level < 4) ? 102 : 93  ;
		$textarea_height{notes} = ($glod_user_level < 3) ? 102 : 93  ;
		$add_form_fields .= &common_min_form_textarea('notes',$db{$table}{$id}{notes},'') ;
		$add_form_fields .= qq~<div class='row'><div class='col-md-12'>&nbsp;</div></div>~ ;
		
		# unless ($is_schools_manager) {
		if ($glod_user_level > 3) {
			$textarea_height{office_notes} = 93 ;
			$add_form_fields .= &common_min_form_textarea('office_notes',$db{$table}{$id}{office_notes},'') ;
			$add_form_fields .= qq~<div class='row'><div class='col-md-12'>&nbsp;</div></div>~ ;	
		}
		
		&event_tabs_content_box($sec_name,$sec_col);

		# --------------- END CUSOTMER DETAILS ----------------------------------------------------------------------------------------------------------
	}
	
	if ($tab==7) {  # Cusotmer Details

		# --------------- START CUSOTMER DETAILS --------------------------------------------------------------------------------------------------------
		

		my $sec = '_profit_loss' ; my ($sec_name,$sec_cnt,$sec_col) = &tab_sec_det($sec) ;
		
		$ret_pdf_link_sec .= $sec . '+' . $sec_cnt . '|' ;
		
		return if $ret ; 
		
		$lcol = 3 ; $fcol = 5 ;
		
		my $expenses = $db{$table}{$id}{total_grand_amount_workings_event} ? sprintf("%0.2f",$db{$table}{$id}{total_grand_amount_workings_event}) : '0.00' ;
		# my $income = $db{$table}{$id}{grand_total} ? $db{$table}{$id}{grand_total} : '0.00' ;
		my $income = $db{$table}{$id}{sub_total} ? sprintf("%0.2f",$db{$table}{$id}{sub_total}) : '0.00' ;
		# my $profit_or_loss = $income - $expenses ;
		my $profit_or_loss = sprintf("%0.2f",($income - $expenses)) ;
		
		$add_form_fields = '' ;
		
		$lcol = 3;
		$fcol = 3;
		
		$readonly{income}      = "READONLY"; 
		$readonly{expenses}    = "READONLY"; 
		$readonly{profit_loss} = "READONLY"; 
		
		$add_form_fields .= &common_min_form_input('income',$income,'') ;
		$add_form_fields .= &common_min_form_input('expenses',$expenses,'') ;
		$add_form_fields .= &common_min_form_input('profit_loss',$profit_or_loss,'') ;
		
		&event_tabs_content_box($sec_name,$sec_col);
		
		# my $sec = '_overall_revenue' ; 
		# my ($sec_name,$sec_cnt,$sec_col) = &tab_sec_det($sec) ;
		
		# my @ttlarr = ("total_grand_amount_workings_event", "grand_total") ; 
		
		# foreach (@ttlarr) {
			# $readonly{$_} = "READONLY"; 
			# $input_style{$_} = qq~style="text-align:right;"~;
			# my $val = $db{$table}{$id}{$_} ? $db{$table}{$id}{$_} : '0.00' ;
			# $add_form_fields .= &common_min_form_input_col($_,$val) ; 
		# }
		
		# &event_tabs_content_box($sec_name,$sec_col) ;

		# --------------- END CUSOTMER DETAILS ----------------------------------------------------------------------------------------------------------

	}

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

sub tab_curr_symbols {

	$curr_symb{ZAR} = 'R';
	$curr_symb{USD} = '$';
	$curr_symb{GBP} = 'GBP';
	$curr_symb{EUR} = 'EUR';

	our $js_curr_arr = '' ;

	foreach (keys %curr_symb) {
		$js_curr_arr .= qq(currArr['$_'] = "$curr_symb{$_}";) ;
	}

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

sub tab_build_qt_col_headers {

	my ($sec) = @_ ;

	my $style_vat = '' ; 
	
	$style_vat = 'style="display:none;"' if $no_vat{$sec} ; 
		
	$add_form_fields .= qq~<div class='row'>~ ;
	$add_form_fields .= qq~<div class='col-md-1'><label class='control-label'>&nbsp;&nbsp;&nbsp;&nbsp;Excl.</label></div>~ ;
	$add_form_fields .= qq~<div class='col-md-2'><label class='control-label'>Item</label></div>~ if $sec eq 'costing_event';
	$add_form_fields .= qq~<div class='col-md-2'><label class='control-label'>Casual User</label></div>~ if $sec eq 'costing_casual_users';
	$add_form_fields .= qq~<div class='col-md-1'><label class='control-label'>Qty</label></div>~ ;
	$add_form_fields .= qq~<div class='col-md-1'><label class='control-label' id='lab_usd_amnt$sec'>Price</label></div>~ ;
	# $add_form_fields .= qq~<div class='col-md-1'><label class='control-label' id='lab_curr_amnt$sec'>Price</label></div>~ ;
	$add_form_fields .= qq~<div class='col-md-1'><label class='control-label'>Total</label></div>~ ;	
	$add_form_fields .= qq~<div class='col-md-1'><label class='control-label' $style_vat>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VAT&nbsp;[15\%]</label></div>~ ;
	$add_form_fields .= qq~</div>~ ;
	
	%cost_desc = () ; 
	%def_qty = () ;
	%def_zar = () ; 
	%min_zar = () ; 
	%vat_tick = () ; 
	%excl_tick = () ;

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

sub tab_build_qt_col_headers_workings {

	my ($sec) = @_ ;

	my $style_vat = '' ; 
	
	$style_vat = 'style="display:none;"' if $no_vat{$sec} ; 
	
	$add_form_fields .= qq~<div class='row'>~ ;
	$add_form_fields .= qq~<div class='col-md-1'><label class='control-label'>&nbsp;&nbsp;&nbsp;&nbsp;Excl.</label></div>~ ;
	$add_form_fields .= qq~<div class='col-md-2'><label class='control-label'>Description</label></div>~ if $sec ne 'workings_casual_users';
	$add_form_fields .= qq~<div class='col-md-2'><label class='control-label'>Operators</label></div>~ if $sec ne 'workings_casual_users';
	# $add_form_fields .= qq~<div class='col-md-2'><label class='control-label'>Operators</label></div>~ if $sec eq '_workings_casual_users';
	$add_form_fields .= qq~<div class='col-md-1'><label class='control-label'>Remarks</label></div>~ if $sec ne 'workings_casual_users';
	# $add_form_fields .= qq~<div class='col-md-2'><label class='control-label'>Remarks</label></div>~ if $sec eq '_workings_casual_users';
	$add_form_fields .= qq~<div class='col-md-1'><label class='control-label'>Supplier</label></div>~ if $sec ne 'workings_casual_users';
	$add_form_fields .= qq~<div class='col-md-1'><label class='control-label'>Ref Nr</label></div>~ ;
	# $add_form_fields .= qq~<div class='col-md-1'><label class='control-label'>Date</label></div>~ ;
	$add_form_fields .= qq~<div class='col-md-1'><label class='control-label'>Qty</label></div>~ ;
	$add_form_fields .= qq~<div class='col-md-1'><label class='control-label' id='lab_usd_amnt$sec'>Amount</label></div>~ ;
	$add_form_fields .= qq~<div class='col-md-1'><label class='control-label' id='lab_curr_amnt$sec'>Total</label></div>~ ;
	$add_form_fields .= qq~<div class='col-md-1'><label class='control-label' $style_vat>Incl. VAT&nbsp;[15\%]</label></div>~ ;
	$add_form_fields .= qq~</div>~ ;		

	%cost_desc = () ; 
	%def_qty = () ;
	%def_zar = () ; 
	%min_zar = () ; 
	%vat_tick = () ; 
	%excl_tick = () ;

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

sub event_tabs_content_box {

	my ($sec_name,$sec_col) = @_ ;

	$fcol=9 ;
	$print_box_content_rows .= &common_min_box_top('indent-left',$sec_name,$sec_col,$cam_box_icon);            
	$print_box_content_rows .= qq~$add_form_fields~;                       
	$print_box_content_rows .= &common_min_box_foot;

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

sub tab_cnt {

	our %cntxt = () ;
		
	$cntxt{1}  = 'one' ; 
	$cntxt{2}  = 'two' ;
	$cntxt{3}  = 'three' ;	
	# return if $is_schools_manager ;
	return if $glod_user_level < 4 ;
	$cntxt{4}  = 'four' ;
	# $cntxt{5}  = 'five' ;
	$cntxt{6}  = 'six' ;
	$cntxt{7}  = 'seven' ;
		
} #----------------------------------------------------------------------------------------

sub tab_names {

	my ($tab) = @_ ;

	my $tabname = '' ;

	   if ($tab==1) { $tabname = 'Customer Details'	; }
	elsif ($tab==2) { $tabname = 'Event Details'	; }
	elsif ($tab==3) { $tabname = 'Event Specifics'	; }
	elsif ($tab==4) { $tabname = 'Quote'			; }
	# elsif ($tab==5) { $tabname = 'Notes'			; }
	elsif ($tab==6) { $tabname = 'Expenses'			; }
	elsif ($tab==7) { $tabname = 'Invoice Details'	; }

	return ($tabname) ;

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

sub tab_hash {
	
	# &db_min_ro('users',"id","user_type like '%casual%'",'','') ; 
	# &db_min_ro('events',"id,event_quote_nr,user_id,category",'','','') ;
	# &db_min_ro('event_cost_items','*','','name asc','') ;
	
	our @excl_field_ids = () ; our @description_field_ids = () ; our @operator_field_ids = () ; our @vat_field_ids = () ; our @qty_field_ids = () ; our @amount_usd_field_ids = () ; our @excl_field_ids = () ; our @btn_ids = () ;
	
	&tab_load_vars;

	for (1 .. 7) {
		
		next if $_ == 5 ;
		
		my $tname = &tab_names($_) ;
		our $ret_pdf_link_sec = '' ; # _sa_out+16|_cl_del+13|_sa_in+17|_des_sa+13|

		&event_tabs_build_qt_left('',$_,1) ;
		&tab_build_qt_right('',$_,1) ;
		
		$tabcnt{$tname} = $_ ;
		$tabsec{$tname} = $ret_pdf_link_sec ;
	}
		
	# &build_workings_costings_forms_jquery ; 	
		
} #------------------------------------------------------------------------------------

sub tab_load_vars {
	
	# our %casuals               = (); 
	our %available_casuals     = () ; 
	our %not_available_casuals = () ; 
	
	# unless (defined %casuals) {
	unless (scalar keys %casuals > 0) {
		&db_min_ro('users',"id","user_type like '%casual%'",'','') ; 
		
		foreach my $user_id (keys %{$db{users}}){
			$casuals{$user_id} = 1 ; 
		}
	}
	
	&db_min_ro('events',"id,event_quote_nr,user_id,category",'','','') ; 
	
	foreach my $event_id (keys %{$db{events}}){
		next unless $casuals{$db{events}{$event_id}{user_id}} ; 
		if ($db{events}{$event_id}{event_quote_nr} == $id and $db{events}{$event_id}{category} eq 'available'){
			$available_casuals{$db{events}{$event_id}{user_id}} = 1 ;
		} elsif ($db{events}{$event_id}{event_quote_nr} == $id and $db{events}{$event_id}{category} eq 'unavailable'){
			$not_available_casuals{$db{events}{$event_id}{user_id}} = 1 ;
		}
	}
	
	our %dlg_operator = () ; 
	
	for (1 .. 50) {
		next unless $db{$table}{$id}{"operator_workings_event_$_"} ; 
		if ($available_casuals{$db{$table}{$id}{"operator_workings_event_$_"}}) {
			$dlg_operator{"operator_workings_event_$_"} = qq~<div style = "margin-left: 10px;" class="label label-success" title data-toggle="tooltip" data-original-title="Available" class="fright"><i class="glyphicon glyphicon-ok"></i></div>~ ;
		} elsif ($not_available_casuals{$db{$table}{$id}{"operator_workings_event_$_"}}) {
			$dlg_operator{"operator_workings_event_$_"} = qq~<span style = "margin-left: 10px;" class="label label-danger"  title data-toggle="tooltip" data-original-title="Not Available" class="fright"><i class="glyphicon glyphicon-remove"></i> </span>~ ;
		} else {
			$dlg_operator{"operator_workings_event_$_"} = qq~<span style = "margin-left: 10px;" class="label label-warning"  title data-toggle="tooltip" data-original-title="No Response" class="fright"><i class="glyphicon glyphicon-question-sign"></i></span>~ ;
		}
	}
	
	# &db_min_ro('event_cost_items','*','','name asc','') ;
	&db_min_ro('event_cost_items','*',"excl_from_expenses < 1",'name asc','') ;
	
	our %event_list_name_array_quote = () ;
	our %event_list_name_array_expenses = () ;
	my @spl = () ; 
	
	foreach my $iid (keys %{$db{event_cost_items}}) { 	
		@spl = split(" ", $db{event_cost_items}{$iid}{name});
		for (@spl) {$_ = ucfirst $_ ; }
		$db{event_cost_items}{$iid}{name} = join (" ", @spl) ; 
		$event_list_name_array_quote{$iid} = $db{event_cost_items}{$iid}{name} ;
		$event_list_name_array_expenses{$iid} = $db{event_cost_items}{$iid}{name} ; 
	}
	
} #------------------------------------------------------------------------------------

sub tab_sec_det {

	my ($sec) = @_ ;

	my ($sec_nam,$sec_cnt,$sec_col) ;

	$sec_col = '71919b' ; # '85acb8' ;
	$sec_cnt = 20 ; 
	$sec_nam = substr($sec,1) ; 
	@array   = split(/\_/,$sec_nam); 
	my @sec_nam = map ucfirst, @array;
	$sec_nam = join(" ", @sec_nam) ;

	if ($sec eq 'costing') {
		$sec_nam = 'Event Costings' ;  
		for (1 .. 30) { $vat_tick{$_} = 1 ; }	
	}
	
	if ($sec eq 'workings') {
		$sec_nam = 'Event Expenses' ;  
		for (1 .. 50) { $vat_tick{$_} = 1 ; }	
	}

	return ($sec_nam,$sec_cnt,$sec_col) ;

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

sub event_tabs_build_boxes_top {
	
	my ($id) = @_ ; 
	
	our ($lcol,$fcol) = &common_min_columns(3,6) ;
		
	$add_form_fields = '' ;

	$fcol=8 ;

	$add_form_fields            .= qq~<div class='col-md-3'>~ ;
	$preferred_title{quote_nr}   = "Nr" ; 
	$add_form_fields            .= &common_min_form_input('quote_nr',$db{$table}{$id}{quote_nr},'') ;
	$add_form_fields            .= qq~</div>~ ;
	$add_form_fields            .= qq~<div class='col-md-3'>~ ;
	
	if (($db{$table}{$id}{quote_date} eq '') or ($db{$table}{$id}{quote_date} eq '0000-00-00')) { 
			$db{$table}{$id}{quote_date} = $now_ccyy_mm_dd  ; 
	}
	$preferred_title{quote_date} = "Date" ;
	$add_form_fields            .= &common_min_form_datepicker('quote_date',$db{$table}{$id}{quote_date}) ;
	my $plus_n_days_ccyy_mm_dd   = &common_add_delta_days(14) ;	
	
	$add_form_fields .= qq~</div>~ ;
	$add_form_fields .= qq~<div class='col-md-3'>~ ;
		$preferred_title{quote_to} = "Client" ;
		$add_form_fields .= &common_min_form_select('quote_to',$db{$table}{$id}{quote_to},'') ;
	$add_form_fields .= qq~</div>~ ;
	$add_form_fields .= qq~<div class='col-md-3'>~ ;
	$dlg{quote_to} = qq~ 
		<div class="col-md-1">
			<a href="javascript:dlgMdl('$useropts{'scripts'}/dialog/add_customer.pl?add&selectQuote_to','Add Customer','','max-dialog');" class="btn btn-primary btn-xs" style="padding:1px 2px 1px 3px;margin-top:0px;" title='Add Customer' data-toggle='tooltip' data-placement='right'>
				<i class="glyphicon glyphicon-plus" style="padding:0;margin-top:0px;">
				</i>
			</a>
		</div>~ ;
	$fcol = 7;
	$add_form_fields .= &common_min_form_select('country_id',$db{$table}{$id}{country_id},'') ;	
	$add_form_fields .= qq~</div>~ ;
	$fcol = 8;
	$print_box_content_rows .= &common_min_box_top('qrcode',"Quote",'BABEC2');           
	$print_box_content_rows .= qq~
								<div class="row">
									<div class="col-md-12">
										$add_form_fields
									</div>
								</div>~;  
	$print_box_content_rows .= qq~
								<div class='row'>
									<div class='col-md-12' style='height:6px;'>
										&nbsp;
									</div>
								</div>~ ;							
	$print_box_content_rows .= &common_min_box_foot;
	
} #------------------------------------------------------------------------------------

sub event_tabs_build_costs_forms {

	my ($id,$tab,$rcnt,$suffix) = @_ ;
	
	$tab_sections{$tab} .= "$suffix+$rcnt|" ;

	$jq_work_cal = qq~
		var vat_totalW$suffix = 0 ; 
		var total_totalW$suffix = 0 ; 
		var subtotalW$suffix = 0 ; 
		var vat_amountW = 0 ; 
	~ ;
	$jq_fs  = '' ;

	%cnt_specific_jquery = () ;
		
	&set_check_boxes_checked($id,$rcnt,"excl",$suffix);
	&set_check_boxes_checked($id,$rcnt,"vat",$suffix);

	my $cntloop = 1 ;
	my $field = "" ;
	my $val = 0 ;
	
	my $table_ = "event_quotes_min" ; 
	
	my @excl_old = split(";", $db{$table_}{$id}{"excl_$suffix"}) ; 
	my @qty_old = split(";", $db{$table_}{$id}{"qty_$suffix"}) ;
	my @vat_old = split(";", $db{$table_}{$id}{"vat_$suffix"}) ;
	my @remarks_old		= () ; 
	my @supplier_old 	= () ; 
	my @ref_nr_old 		= () ; 
	my @amount_usd_old 	= () ; 
	my @amount_ttl_old 	= () ; 
	my @des_old 		= () ; 
	my @ops_old 		= () ; 
	my @curr_amnt_cost 	= () ; 

	if ($suffix eq "workings_event") {
		@remarks_old	= split(";",$db{$table_}{$id}{remarks_workings_event}) ;
		@supplier_old 	= split(";",$db{$table_}{$id}{supplier_workings_event}) ;
		@ref_nr_old		= split(";",$db{$table_}{$id}{ref_nr_workings_event}) ;
		@amount_usd_old	= split(";",$db{$table_}{$id}{amount_usd_workings_event});
		@amount_ttl_old	= split(";",$db{$table_}{$id}{amount_workings_event});		
		@des_old 		= split(";",$db{$table_}{$id}{description_workings_event}) ;
		@ops_old 		= split(";",$db{$table_}{$id}{operator_workings_event}) ;
	} else {
		@amount_usd_old	= split(";",$db{$table_}{$id}{usd_amnt_costing_event});
		@amount_ttl_old	= split(";",$db{$table_}{$id}{item_ttl_costing_event}); 
		@des_old      	= split(";",$db{$table_}{$id}{item_costing_event});
		@curr_amnt_cost	= split(";",$db{$table_}{$id}{curr_amnt_costing_event});
	}
	
	for (1 .. $rcnt) {

		$display_style = '' ; 
		$display_plus_btn = '' ;
		my $style_display_butt = "" ;
		unless ($_ == 1 or $des_old[$_ - 1]) { 
			$display_style = qq~style='display:none;'~ ; 
			push @btn_ids,"#btn_$suffix\_$_" ;
			if ( $_>$cntloop ) { 
				$display_butt = qq~display:none;~ ;  
				$style_display_butt = qq~style="display:none;"~ ;
			} else { 
				$display_butt = '' ; 
			}
			$display_plus_btn = ($_>$cntloop) ? qq~<a id="btn_$suffix\_$_" href="#" style="padding-left:16px"></a>~ : qq~<a id="btn_$suffix\_$_" href="#" style="padding-left:16px;$display_butt"><i class="glyphicon glyphicon-plus blue"></i></a>~;		
		} else { 
			$cntloop++ ; 
		}
		
		$add_form_fields .= qq~<div class='row' id='$suffix\_$_' $display_style>~ ;
		
		#----------------------------------------------------------------------------------------------------
		
		if ($des_old[$_-1] || $_ == 1) {
			
			push @all_select_ids,"#selectDescription_$suffix\_$_" ;
			push @all_select_ids,"#selectOperator_$suffix\_$_" if $suffix eq "workings_event" ; 	
			
			$field = "excl_$suffix\_$_" ; 
			$fcol = 1;
			# if ($checkit{$field} == 1) { 
			if ($excl_old[$_ - 1]){
				$checked = 'CHECKED'; 
			} elsif (($excl_tick{$_}) and ($iaction eq 'add')) { 
				$checked = 'CHECKED'; 
			} else { 
				$checked = ''; 
			}
			$label{$field} = 1;
			$add_form_fields .= &common_min_form_checkbox_col($field,$checkit{$field},$checked) ;

			#----------------------------------------------------------------------------------------------------

			$field = "description_$suffix\_$_" ;
			$fcol = 2;
			# $allow_deselect{$field} = 1;
			$preferred_title{$field} = "Description $_" ; #if $suffix ne '_workings_casual_users';	
			
			$opts{$field} = $opts{description_field} unless $opts{$field} ;
			
			if ($des_old[$_-1] && $opts{$field} !~ /SELECTED/ && $opts{$field} !~ /selected/) {
				$opts{$field} =~ s/\"/\'/g ;
				$opts{$field} =~ s/value='$des_old[$_-1]'/value='$des_old[$_-1]' SELECTED/g ;
			}
			
			$add_form_fields .= &common_min_form_select_col($field,'') ; 

			#----------------------------------------------------------------------------------------------------

			if ($suffix eq "workings_event") {
				
				$field = "operator_$suffix\_$_" ;
				$fcol = 2;
				# $allow_deselect{$field} = 1;
				$preferred_title{$field} = "Operator $_" ;	
				
				$opts{$field} = $opts{operators_field} unless $opts{$field} ;

				if ($ops_old[$_-1] && $opts{$field} !~ /SELECTED/ && $opts{$field} !~ /selected/) {
					$opts{$field} =~ s/\"/\'/g ;
					$opts{$field} =~ s/value='$ops_old[$_-1]'/value='$ops_old[$_-1]' SELECTED/g ;
				} 
				
				$add_form_fields .= &common_min_form_select_col($field,'') ; 

				#----------------------------------------------------------------------------------------------------
				
				$field = "remarks_$suffix\_$_" ;
				$fcol = 1;
				# $allow_deselect{$field} = 1;
				$preferred_placeholder{$field} = "Remarks $_" ;	
				$add_form_fields .= &common_min_form_input_col($field, $remarks_old[$_ - 1]) ;

				#----------------------------------------------------------------------------------------------------

				$field = "supplier_$suffix\_$_" ;
				$fcol = 1;
				# $allow_deselect{$field} = 1;
				$preferred_placeholder{$field} = "Supplier $_" ;
				$add_form_fields .= &common_min_form_input_col($field,$supplier_old[$_ - 1]) if $suffix ne '_workings_casual_users'; 	

				#----------------------------------------------------------------------------------------------------

				$field = "ref_nr_$suffix\_$_" ;
				$fcol = 1;
				# $allow_deselect{$field} = 1;
				$preferred_placeholder{$field} = "Ref Nr $_" ;	
				$add_form_fields .= &common_min_form_input_col($field,$ref_nr_old[$_ - 1]) ;

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

			$field = "qty_$suffix\_$_" ; 
			# $field .= '_costing_event';
			$fcol = 1; 
			$input_style{$field} = qq(style="text-align:right;");
			$preferred_placeholder{$field} = "Qty $_" ;
			$val = $qty_old[$_ - 1] ; 
			unless ($val) { 
				$val = $def_qty{$_} ; 
			} 
			$val = &q_get_val($val) ;
			$add_form_fields .= &common_min_form_input_col($field,$val) ;
			
			#----------------------------------------------------------------------------------------------------
			
			$field = "amount_usd_$suffix\_$_" ; 
			$fcol = 1; 
			$input_style{$field} = qq(style="text-align:right;");
			$val = $amount_usd_old[$_ - 1] ; 
			unless ($val) { 
				$val = $def_usd{$_} ;
			} 
			$val = &q_get_val($val) ;
			$preferred_placeholder{$field} = "Amount $_" ;
			# $excl_on_change{$field} = 1 ;
			$val = '0.00' unless $val ;
			$add_form_fields .= &common_min_form_input_col($field,$val,1) ;
			
			$add_form_fields .= qq~<input style='display:none;' value='1' name='half_$suffix\_$des_old[$_-1]'>~ if $des_old[$_-1] && $amount_usd_old[$_ - 1] eq $default_cost_item_rates{$des_old[$_-1]}{half} ;
			$add_form_fields .= qq~<input style='display:none;' value='1' name='full_$suffix\_$des_old[$_-1]'>~ if $des_old[$_-1] && $amount_usd_old[$_ - 1] eq $default_cost_item_rates{$des_old[$_-1]}{full} ;
			#----------------------------------------------------------------------------------------------------
			
			# if ($suffix eq "costing_event") {
				
				# $field = "curr_amnt_$suffix\_$_" ; 
				# # $field .= '_costing_event';
				# $input_style{$field} = qq(style="text-align:right;"); 
				# $readonly{$field} = 'READONLY' ; # $fcol=2 ;
				# $preferred_placeholder{$field} = "ZAR Amnt $_" ;
				# # $val = &q_get_val($db{$table}{$id}{$field}) ;
				# $val = &q_get_val($curr_amnt_cost[$_ -1]) ;
				# our $type = 'hidden' ;
				# $add_form_fields .= &common_min_form_input_col($field,$val,1) ; $type = '' ;
				# $type = '' ;				
				
			# }
			
			# #----------------------------------------------------------------------------------------------------
			
			$field = "amount_ttl_$suffix\_$_" ; 
			$input_style{$field} = qq(style="text-align:right;");
			$val = $amount_ttl_old[$_ - 1] ;
			$val = &q_get_val($val) ;
			# $val = &common_commify($val) ;
			# $val = sprintf("%0.2f",$val) ; 
			$preferred_placeholder{$field} = "Total Amount $_" ;
			# $excl_on_change{$field} = 1 ;
			$readonly{$field} = 'READONLY' ;
			$val = '0.00' unless $val ;
			$add_form_fields .= &common_min_form_input_col($field,$val,1) ;
			
			#----------------------------------------------------------------------------------------------------
			
			$field = "vat_$suffix\_$_" ; 
			$fcol=1 ;
			# if ($checkit{$field} == 1) { 
			if($vat_old[$_ - 1]){
				$checked = 'CHECKED'; 
			} elsif ($iaction eq 'add') { 
				$checked = 'CHECKED'; 
			} else { 
				$checked = ''; 
			}
			$label{$field} = 1 ;
			if ($no_vat{$suffix}) { 
				$style_field{$field} = 'style="display:none;"' ; 
			}
			# $excl_on_change{$field} = 1 ;
			$add_form_fields .= &common_min_form_checkbox_col($field,$checkit{$field},$checked) ;
			
			#----------------------------------------------------------------------------------------------------
		
		} else {	
			$tindex+=10 if $suffix eq "workings_event"  ; 
			$tindex+=7 if $suffix eq "costing_event" ; 
		}
		
		my $display_hidden_input = ($username eq 'handre') ? "" : "style='display:none;'" ; 
		
		my $f_h_val = substr($quote_tab_default_full_half_items[$_-1],2) ;
		$add_form_fields .= (substr($quote_tab_default_full_half_items[$_-1],0,2) eq '1:') ? qq~<input name="added_full_day_cost_item_$_" value="$f_h_val" $display_hidden_input>~ : (substr($quote_tab_default_full_half_items[$_-1],0,2) eq '2:') ? qq~<input name="added_half_day_cost_item_$_" value="$f_h_val" $display_hidden_input>~ : qq~~ ;
		
		$add_form_fields .= qq~</div>~ ;

		$add_form_fields .= qq~
			<div class='row' id="btn_row_$suffix\_$_" $style_display_butt >
				<div class="col-md-1">
					$display_plus_btn
				</div>
			</div>
		~ ;
		
		my $style_disp_row = ($des_old[$_-1]) ? "" : "style='display:none;'" ;
		$add_form_fields .= qq~
			<div class='row' $style_disp_row id = "2_$suffix\_$_">
				<div class='col-md-12' style="margin:2px 0 0 0">
				</div>
			</div>
		~ ;
		
	}

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

1;
