our %sec_cnt = ('a_systems' => "3-Systems", 'b_software_licence' => "3-Software Licence (Monthly Fee)", 'c_game_breakdown_analytics' => "2-Game Breakdown and Analytics (Annual Fee)", 'd_installation_kits' => "7-Installation Kits", 'e_hardware_add_ons' => "7-Hardware Add Ons", 'f_additional_services' => "7-Additional Services", 'g_match_credits' => "4-Match Credits", 'h_other' => "5-Other", 'i_rental' => "2-Rental" , "j_hardware" => "1-Hardware");

sub tabs_get_default_totals {
	
	my ($id) = @_ ;
	
	# our %sec_cnt = ('a_systems' => "3-Systems", 'b_software_licence' => "3-Software Licence (Monthly Fee)", 'c_game_breakdown_analytics' => "2-Game Breakdown and Analytics (Annual Fee)", 'd_installation_kits' => "7-Installation Kits", 'e_hardware_add_ons' => "7-Hardware Add Ons", 'f_additional_services' => "7-Additional Services", 'g_match_credits' => "4-Match Credits", 'h_other' => "5-Other", 'i_rental' => "2-Rental" , "j_hardware" => "1-Hardware");
	# our %sec_cnt = ('a_other' => "5-Other");
	our %sec_cnt = ('a_systems' => "3-Systems", 'b_software_licence' => "3-Software Licence (Monthly Fee)", 'c_game_breakdown_analytics' => "2-Game Breakdown and Analytics (Annual Fee)", 'd_installation_kits' => "7-Installation Kits", 'e_hardware_add_ons' => "7-Hardware Add Ons", 'f_additional_services' => "7-Additional Services", 'g_match_credits' => "4-Match Credits", 'h_rental' => "2-Rental" , "j_hardware" => "1-Hardware");
	
	our %default_totals = () ; 

	our @ttlarr = ("one_time_fee","monthly_recurring_fee","annual_fee") ;
	our @ttlarr2 = ("subtotal","vat","total") ;
	
	my $needs_to_update_totals = ($db{$table}{$id}{total_one_time_fee} !~ /;/ && $db{quotes_min}{$id}{item_ttl_costing_other} =~ /(?<!\d)(?:[1-9]\d*)(?!\d)/) ? 1 : 0 ; 
	
	my @ttl_other = ($needs_to_update_totals) ? split(/\;/,$db{quotes_min}{$id}{item_ttl_costing_other}) : () ; 
	my @vat_other = ($needs_to_update_totals) ? split(/\;/,$db{quotes_min}{$id}{vat_costing_other}) : () ; 
	my @excl_other = ($needs_to_update_totals) ? split(/\;/,$db{quotes_min}{$id}{excl_costing_other}) : () ; 
	
	foreach my $a (@ttlarr) {
		my @tot = () ; 
		foreach my $b (@ttlarr2) {
			
			$field = "$b\_$a" ;
			
			$db{$table}{$id}{$field} = "" if $db{$table}{$id}{$field} eq "0.00" ;
			@tot = split(/\;/,$db{$table}{$id}{$field}) ;
			
			if ($needs_to_update_totals) {
				
				if ($a eq "one_time_fee") {
					if ($b eq "subtotal") {
						my $cnt = 0 ;
						foreach my $c (@ttl_other) {
							next if $excl_other[$cnt] || !$c || $c eq '0.00' ;
							$tot[0] -= $c ;
							$tot[1] += $c ;
						} 
					} elsif ($b eq "vat") {
						my $cnt = 0 ;
						foreach my $c (@ttl_other) {
							next if $excl_other[$cnt] || !$vat_other[$cnt] || !$c || $c eq '0.00' ;
							$tot[0] -= $c*0.15 ;
							$tot[1] += $c*0.15 ;
						} 
					} elsif ($b eq "total") {
						my $cnt = 0 ;
						foreach my $c (@ttl_other) {
							next if $excl_other[$cnt] || !$c || $c eq '0.00' ;
							$tot[0] -= ($vat_other[$cnt]) ? $c*1.15 : $c ;
							$tot[1] += ($vat_other[$cnt]) ? $c*1.15 : $c ;
						} 
					} 
				}
			}
			
			$tot[0] = sprintf("%0.2f",$tot[0]) ;
			$tot[1] = sprintf("%0.2f",$tot[1]) ;
			
			$default_totals{$field}{1} = ($tot[0]) ? $tot[0] : "0.00" ;
			$default_totals{$field}{2} = ($tot[1]) ? $tot[1] : "0.00" ;		

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

sub tab_build_qt_left {

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

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

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

	# &build_cost_form($id,$tab,$sec_cnt,$sec);
	
	$lcol = 2 ;
	$fcol = 5 ;
	
	$add_form_fields = '' ;
	
	# if (($db{$table}{$id}{date_from} eq '') or ($db{$table}{$id}{date_from} eq '0000-00-00')) { $db{$table}{$id}{date_from} = $now_ccyy_mm_dd  ; }
	# $add_form_fields .= &common_min_form_datepicker('date_from',$db{$table}{$id}{date_from}) ;
	# if (($db{$table}{$id}{date_to} eq '') or ($db{$table}{$id}{date_to} eq '0000-00-00')) { $db{$table}{$id}{date_to} = $now_ccyy_mm_dd  ; }
	# $add_form_fields .= &common_min_form_datepicker('date_to',$db{$table}{$id}{date_to}) ;
	
	$fcol = 9 ;
	 
	$add_form_fields .= &common_min_form_select('type',$db{$table}{$id}{type},'') ;
	
	my $checked = 'CHECKED' if $db{$table}{$id}{quote_accepted} ;
	$add_form_fields .= &common_min_form_checkbox('quote_accepted',$db{$table}{$id}{quote_accepted},$checked) ;
	
	my $checked = 'CHECKED' if $db{$table}{$id}{quote_pending} or $iaction eq 'add' ;
	$add_form_fields .= &common_min_form_checkbox('quote_pending',$db{$table}{$id}{quote_pending},$checked) ;
	
	my $checked = 'CHECKED' if $db{$table}{$id}{quote_cancelled} ; $preferred_title{quote_cancelled} = 'Quote Closed' ; 
	$add_form_fields .= &common_min_form_checkbox('quote_cancelled',$db{$table}{$id}{quote_cancelled},$checked) ;
	
	$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 .= &common_min_form_input('invoice_nr',$db{$table}{$id}{invoice_nr},'') ;
	# $add_form_fields .= &common_min_form_input('annual_invoice_2',$db{$table}{$id}{annual_invoice_2},'') ;
	# $add_form_fields .= &common_min_form_input('annual_invoice_3',$db{$table}{$id}{annual_invoice_3},'') ;
	
	$add_form_fields .= qq~<div class='row'><div class='col-md-12' style="height:42px;">&nbsp;</div></div>~ ;


$trigger_jquery_raw .= qq~

	if (!\$("#checkboxQuote_accepted").is(":checked") && !\$("#checkboxQuote_pending").is(":checked") && !\$("#checkboxQuote_cancelled").is(":checked")) {
			\$("#checkboxQuote_pending").prop("checked","true") ;
	}
	\$("#checkboxQuote_accepted,#checkboxQuote_pending,#checkboxQuote_cancelled").click( function () {
		
		if (this.id != 'checkboxQuote_accepted' && \$("#checkboxQuote_accepted").is(":checked")) {
			\$("#checkboxQuote_accepted").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 (!\$("#checkboxQuote_accepted").is(":checked") && !\$("#checkboxQuote_pending").is(":checked") && !\$("#checkboxQuote_cancelled").is(":checked")) {
			\$("#checkboxQuote_pending").prop("checked","true") ;
		}
		
	});

~ ; 
	
	&tab_content_box($sec_name,$sec_col);

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

if ($tab == 2) { # 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) ;
	$trigger_jquery           .= qq~\$("#selectCurrency_chosen").css( "width", "96%" );~ ;
	$preferred_title{roe}      = ' ROE ' ; $fcol = 1 ;
	$add_form_fields          .= &common_min_form_label_col('roe') ;
	$add_form_fields          .= &common_min_form_input_col('roe',$db{$table}{$id}{roe},1) ;
	$add_form_fields          .= qq~</div>~ ;
	my $sec = '_costing' ; my ($sec_name,$sec_cnt,$sec_col) = &tab_sec_det($sec) ;	
	$ret_pdf_link_sec .= $sec . '+' . $sec_cnt . '|' ;
	
	return if $ret ;
	
	&tab_costing_select_opts('price_list','description',"`excl`<>'1'",$sec) ;
	
	# &db_min_ro('quotes_pdf_titles', '*', "quote_id = $id", '', '') ; 
	foreach (keys %{$db{quotes_pdf_titles}}){
		next unless $db{quotes_pdf_titles}{$_}{quote_id} == $id ; 
		$old_title_to_new_title{$db{quotes_pdf_titles}{$_}{old_name}}{$db{quotes_pdf_titles}{$_}{quote_id}} = $db{quotes_pdf_titles}{$_}{new_name} ;
	}
	
	foreach (sort keys %sec_cnt) {
		$_k = substr($_,1) ;
		next if $_k eq '_hardware' ;
		my @parts = split(/\-/,$sec_cnt{$_}) ;

		$add_form_fields .= qq~<div class='row'><div class='col-md-12'><h4>$parts[1] :</h4></div></div>~ ;
		# $add_form_fields .= qq~<div class='row'><div class='col-md-3'><h4>$parts[1] :</h4></div>~ ;
		$fcol = 3 ; 
		$add_form_fields .= qq~<div class='row'><div class='col-md-1'><h5 style="margin: 5px 0px 5px 0px;color:#ccc;">PDF title : </h5></div>~ ;
		# $add_form_fields .= qq~<div class='col-md-1'><b> PDF TITLE : </b></div>~ ;
		if ($old_title_to_new_title{$parts[1]}{$id} and $old_title_to_new_title{$parts[1]}{$id} ne $parts[1]){
			$add_form_fields .=  &common_min_form_input_col($_k, $old_title_to_new_title{$parts[1]}{$id}) ;
		}
		else {
			$add_form_fields .=  &common_min_form_input_col($_k,'') ;
		}
		
		$add_form_fields .= qq~</div>~ ;  
		&tab_build_qt_col_headers("$sec$_k") ;	
		&build_cost_form($id,$tab,$parts[0],"$sec$_k") ; 
		if ($_k eq '_systems') {
			$preferred_title{'systems_included'} = '&nbsp;' ; $preferred_placeholder{'systems_included'} = 'Included' ; $fcol = 3 ;
			$add_form_fields .= &common_min_form_textarea('systems_included',$db{$table}{$id}{systems_included},'') ;
		}
		if ($_k eq '_rental') {
			$preferred_title{'rental_included'} = '&nbsp;' ; $preferred_placeholder{'rental_included'} = 'Included' ; $fcol = 3 ;
			$add_form_fields .= &common_min_form_textarea('rental_included',$db{$table}{$id}{rental_included},'') ;
		}
		if ($_k eq '_other') {
			$preferred_title{'other_included'} = '&nbsp;' ; $preferred_placeholder{'other_included'} = 'Included' ; $fcol = 3 ;
			$add_form_fields .= &common_min_form_textarea('other_included',$db{$table}{$id}{other_included},'') ;
		}
	}
	
	$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='col-md-1'><h5 style="margin: 5px 0px 5px 0px;color:#ccc;">PDF total : </h5></div>~ ;
	
	$lcol = 3 ;
	
	if ($old_title_to_new_title{"Hardware"}{$id} and $old_title_to_new_title{"Hardware"}{$id} ne "Hardware"){
		$add_form_fields .=  &common_min_form_input_col('_hardware', $old_title_to_new_title{"Hardware"}{$id}) ;
	}
	else {
		$add_form_fields .=  &common_min_form_input_col('_hardware','') ;
	}
	
	my @ttlarr_labels = ("sub_total","vat_total","grand_total") ;
	$lcol = 1 ; $fcol = 1 ;
	foreach (@ttlarr_labels) {
		$readonly{$_} = "READONLY" ;
		my $val = $db{$table}{$id}{$_} ; $val = '0.00' unless $val ;
		$type = 'hidden' ; $add_form_fields .= &common_min_form_input_col($_,$val) ; $type = '' ;
		$lcol = 1 ; $add_form_fields .= &common_min_form_label_col($_) ;   
	}
		
	$add_form_fields .= qq~</div>~ ;
		
	$lcol = 1 ; $fcol = 1 ;
	
	foreach my $a (@ttlarr) {
		
		$readonly{$a} = "READONLY" ;
		
		$add_form_fields .= qq~<div class='row'><div class="box col-md-2"></div>~ ; 
	
		$lcol = 2 ;
		$add_form_fields .= &common_min_form_label_col($a) ; 
		$lcol = 1 ;
		
		foreach my $b (@ttlarr2) {
			$field = "$b\_$a\_1" ; $readonly{$field} = "READONLY"; $input_style{$field} = qq~style="text-align:right;"~ ;
			my $val = ($default_totals{$field}{1}) ? $default_totals{$field}{1} : '0.00' ;
			$add_form_fields .= &common_min_form_input_col($field,$val) ; 			
		}
		$add_form_fields .= qq~</div>~ ;
		
	}
		
	$add_form_fields .= qq~<div class='row'><div class="box col-md-12">&nbsp;</div></div>~ ;
	
	&tab_content_box($sec_name,$sec_col) ;

	# --------------- END COSTING ----------------------------------------------------------------------------------------------------------------
	
	
		# $trigger_jquery_raw .= qq~
		
		# 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 ; 
			# 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();
			# 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") {
				# 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++ ;
			# 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 ; 
		# }
	# ~ ;
	
}

if ($tab==3) { # Costing

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

	$lcol=1; $fcol=2;

	my $sec = '_costing' ; my ($sec_name,$sec_cnt,$sec_col) = &tab_sec_det($sec) ;	
	$ret_pdf_link_sec .= $sec . '+' . $sec_cnt . '|' ;
	
	if ($ret) { return ; }
	
	%sec_cnt = ('a_other' => "5-Other");
	
	foreach (sort keys %sec_cnt) {
		$_k = substr($_,1) ;
		next if $_k eq '_hardware' ;
		my @parts = split(/\-/,$sec_cnt{$_}) ;
		$add_form_fields .= qq~<div class='row'><div class='col-md-12'><h4>$parts[1] :</h4></div></div>~ ;
		# $add_form_fields .= qq~<div class='row'><div class='col-md-3'><h4>$parts[1] :</h4></div>~ ;
		$fcol = 3 ; 
		$add_form_fields .= qq~<div class='row'><div class='col-md-1'><h5 style="margin: 5px 0px 5px 0px;color:#ccc;">PDF title : </h5></div>~ ;
		# $add_form_fields .= qq~<div class='col-md-1'><b> PDF TITLE : </b></div>~ ;
		if ($old_title_to_new_title{$parts[1]}{$id} and $old_title_to_new_title{$parts[1]}{$id} ne $parts[1]){
			$add_form_fields .=  &common_min_form_input_col($_k, $old_title_to_new_title{$parts[1]}{$id}) ;
		}
		else {
			$add_form_fields .=  &common_min_form_input_col($_k,'') ;
		}
		
		$add_form_fields .= qq~</div>~ ;  
		&tab_build_qt_col_headers("$sec$_k") ;	
		&build_cost_form($id,$tab,$parts[0],"$sec$_k") ; 
		if ($_k eq '_systems') {
			$preferred_title{'systems_included'} = '&nbsp;' ; $preferred_placeholder{'systems_included'} = 'Included' ; $fcol = 3 ;
			$add_form_fields .= &common_min_form_textarea('systems_included',$db{$table}{$id}{systems_included},'') ;
		}
		if ($_k eq '_rental') {
			$preferred_title{'rental_included'} = '&nbsp;' ; $preferred_placeholder{'rental_included'} = 'Included' ; $fcol = 3 ;
			$add_form_fields .= &common_min_form_textarea('rental_included',$db{$table}{$id}{rental_included},'') ;
		}
		if ($_k eq '_other') {
			$preferred_title{'other_included'} = '&nbsp;' ; $preferred_placeholder{'other_included'} = 'Included' ; $fcol = 3 ;
			$add_form_fields .= &common_min_form_textarea('other_included',$db{$table}{$id}{other_included},'') ;
		}
	}
	
	$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-4">&nbsp;</div>~ ;

	$add_form_fields .= qq~<div class='row'><div class='col-md-4'><h5 style="margin: 5px 0px 5px 0px;color:#ccc;">PDF total : </h5></div>~ ;
	$lcol = 3 ;
	# if ($old_title_to_new_title{"Hardware"}{$id} and $old_title_to_new_title{"Hardware"}{$id} ne "Hardware") {
		# $add_form_fields .=  &common_min_form_input_col('_hardware', $old_title_to_new_title{"Hardware"}{$id}) ;
	# } else {
		# $add_form_fields .=  &common_min_form_input_col('_hardware','') ;
	# }
	
	# my @ttlarr = ("sub_total","vat_total","grand_total") ;
	
	 # @ttlarr2 = ("subtotal","vat","total") ;
	
	$lcol = 1 ; $fcol = 1 ;
	
	print "\n Other Costing Tab " ;
	
	foreach (@ttlarr) {
		print "\n 1 ttlarr : $_ " ;
		$readonly{$_} = "READONLY" ;
		my $val = $db{$table}{$id}{$_} ; $val = '0.00' unless $val ;
		# $add_form_fields .= qq~<div class='row'><div class="box col-md-4"></div>~ ; $lcol=2;
		# $add_form_fields .= &common_min_form_label_col($_) ; $lcol = 1 ;  
		$type = 'hidden' ; $add_form_fields .= &common_min_form_input_col($_,$val) ; $type = '' ;
		# $add_form_fields .= qq~</div>~ ;
		
		$lcol = 1 ; $add_form_fields .= &common_min_form_label_col($_) ;   
		# $add_form_fields .= qq~<div class="box col-md-1"><label class='control-label'>Sub</label></div>~ ;
	}
		
	$add_form_fields .= qq~</div>~ ;
		
	# $add_form_fields .= qq~<div class='row'><div class="box col-md-12">&nbsp;</div></div>~ ;
		
	# my @ttlarr = ("one_time_fee","monthly_recurring_fee","annual_fee") ;
	$lcol = 1 ; $fcol = 1 ;
	
	foreach my $a (@ttlarr) {
 
		$readonly{$a} = "READONLY" ;
		
		# if ($_ eq 'one_time_fee') {
			
			# $add_form_fields .= qq~<div class='row'><div class='col-md-1'><h5 style="margin: 5px 0px 5px 0px;color:#ccc;">PDF total : </h5></div>~ ;
			# $lcol = 1 ;
			# if ($old_title_to_new_title{"Hardware"}{$id} and $old_title_to_new_title{"Hardware"}{$id} ne "Hardware"){
				# $add_form_fields .=  &common_min_form_input_col('_hardware', $old_title_to_new_title{"Hardware"}{$id}) ;
			# }
			# else {
				# $add_form_fields .=  &common_min_form_input_col('_hardware','') ;
			# }
		# }

		# else {
			$add_form_fields .= qq~<div class='row'><div class="box col-md-2"></div>~ ; 
		# }
	
		$lcol = 2 ;
		$add_form_fields .= &common_min_form_label_col($a) ; 
		$lcol = 1 ;
		
		foreach my $b (@ttlarr2) {
			
			$field = "$b\_$a\_2" ; $readonly{$field} = "READONLY"; $input_style{$field} = qq~style="text-align:right;"~ ;
			# my $val = $db{$table}{$id}{$field} ; $val = '0.00' unless $val ;
			my $val = $default_totals{$field}{2} ? $default_totals{$field}{2} : '0.00' ;
			$add_form_fields .= &common_min_form_input_col($field,$val) ; 			
		}
		
		# $field = "2_subtotal_$_" ; $readonly{$field} = "READONLY"; $input_style{$field} = qq~style="text-align:right;"~ ;
		# # my $val = $db{$table}{$id}{$field} ; $val = '0.00' unless $val ;
		# my $val = $db{$table}{$id}{$field} ? $db{$table}{$id}{$field} : '0.00' ;
		# $add_form_fields .= &common_min_form_input_col($field,$val) ; 
		
		# $field = "2_vat_$_" ; $readonly{$field} = "READONLY"; $input_style{$field} = qq~style="text-align:right;"~ ;
		# # my $val = $db{$table}{$id}{$field} ; $val = '0.00' unless $val ;
		# my $val = $db{$table}{$id}{$field} ? $db{$table}{$id}{$field} : '0.00' ;
		# $add_form_fields .= &common_min_form_input_col($field,$val) ; 
		
		# $field = "2_total_$_" ; $readonly{$field} = "READONLY"; $input_style{$field} = qq~style="text-align:right;"~ ;
		# # my $val = $db{$table}{$id}{$field} ; $val = '0.00' unless $val ;
		# my $val = $db{$table}{$id}{$field} ? $db{$table}{$id}{$field} : '0.00' ;
		# $add_form_fields .= &common_min_form_input_col($field,$val) ; 
		$add_form_fields .= qq~</div>~ ;
		
	}
		
	$add_form_fields .= qq~<div class='row'><div class="box col-md-12">&nbsp;</div></div>~ ;
			
	# $print_box_content_rows .= qq~<div class='row'><div class="box col-md-12"><div class="box-inner"><div class="box-content">~ ;
	# $print_box_content_rows .= $add_form_fields ; 
	# $print_box_content_rows .= &common_min_box_foot;
	
	&tab_content_box($sec_name,$sec_col) ;

	# --------------- END COSTING ----------------------------------------------------------------------------------------------------------------
	
	
	$trigger_jquery_raw .= qq~
	
		
	
	~ ;
	
}

if ($tab==4) { # Purchase Summary

	# --------------- START PURCHASE SUMMARY ----------------------------------------------------------------------------------------------------------------
	
	$add_form_fields = '' ;
	
	my $sec = '_purchase_summary' ; my ($sec_name,$sec_cnt,$sec_col) = &tab_sec_det($sec) ;
	
	$ret_pdf_link_sec .= $sec . '+' . $sec_cnt . '|' ;
	
	if ($ret) { return ; }
	
	my $style_vat = '' ; if ($no_vat{$sec}) { $style_vat = 'style="display:none;"' ; }

	$add_form_fields .= qq~<div class='row'><div class='col-md-12'>&nbsp;</div></div>~ ;
	
	my $field = "excl$sec" ;
	if ($db{$table}{$id}{$field} == 1) { $checked = 'CHECKED'; } else { $checked = ''; }
	$label{$field}    = 1 ;
	$add_form_fields .= qq~<div class='row'>~ ;$fcol=1 ;
	$add_form_fields .= &common_min_form_checkbox_col($field,$db{$table}{$id}{$field},$checked) ;
	$add_form_fields .= qq~<div class='col-md-11'><h4 style="margin:0px;">Purchase Summary</h4></div></div>~ ;
	$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'>Items</label></div>~ ;
	$add_form_fields .= qq~<div class='col-md-2' id = 'year_1_sum'><label class='control-label'>Year&nbsp;1</label></div>~ ;
	$add_form_fields .= qq~<div class='col-md-2' id = 'year_2_sum'><label class='control-label'>Year&nbsp;2</label></div>~ ;
	$add_form_fields .= qq~<div class='col-md-2' id = 'year_3_sum'><label class='control-label'>Year&nbsp;3</label></div>~ ;
	$add_form_fields .= qq~<div class='col-md-0' style = 'display:none;' id = 'year_4_sum'><label class='control-label'>Year&nbsp;4</label></div>~ ;
	$add_form_fields .= qq~<div class='col-md-0' style = 'display:none;' id = 'year_5_sum'><label class='control-label'>Year&nbsp;5</label></div>~ ;
	$add_form_fields .= qq~<div class='col-md-2'><label class='control-label'>Total</label></div>~ ;
	$add_form_fields .= qq~</div>~ ;
	
	my $suffix = $sec ; my $rcnt = 3 ;
	
	# &set_check_boxes_checked($id,$rcnt,"excl",$suffix);
	
	$default_desc{1} = 'Hardware Purchase' ;
	$default_desc{2} = 'Monthly Fees' ;
	$default_desc{3} = 'Annual Fees' ;
	
	my @prefexes = ("excl", "item") ;
	
	foreach my $prefex (@prefexes) {
		
		$col_name = "$prefex" . "$suffix" ;
		$cnt = 0 ; 
		# if($db{quotes_min}{$id}{$col_name}){
		for my $col_input (split(";", $db{quotes_min}{$id}{$col_name})){
			$cnt++ ; 
			$saved_field{$prefex}{$suffix}{$cnt} = $col_input ;  
		}	
		# }	
	}
	for (1 .. $rcnt) {
		$add_form_fields .= qq~<div class='row'>~ ;
		#----------------------------------------------------------------------------------------------------
		$field = "excl_$_$suffix" ; $fcol=1 ;
		# if ($checkit{$field} == 1) { $checked = 'CHECKED'; } else { $checked = ''; }
		# if ($db{$table}{$id}{$field} == 1)  { $checked = 'CHECKED'; } else { $checked = ''; }	
		if ($saved_field{"excl"}{$suffix}{$_} == 1)  { $checked = 'CHECKED'; } else { $checked = ''; }
		$label{$field} = 1 ;
		# $add_form_fields .= &common_min_form_checkbox_col($field,$db{$table}{$id}{$field},$checked) ;
		$add_form_fields .= &common_min_form_checkbox_col($field,$saved_field{"excl"}{$suffix}{$_},$checked) ;
		#----------------------------------------------------------------------------------------------------
		my $field = "item_$_$suffix" ; $fcol=2 ;
		$preferred_placeholder{$field} = "Item $_" ;
		# my $val = $db{$table}{$id}{$field} ; 
		my $val = $saved_field{"item"}{$suffix}{$_} ; 
		$val = $default_desc{$_} unless $val ;  
		# $readonly{$field} = 'READONLY' ;
		$add_form_fields .= &common_min_form_input_col($field,$val) ;
		#----------------------------------------------------------------------------------------------------
		for my $year (1 .. 5) {
			$field = "year_$year\_$_$suffix" ; $input_style{$field} = qq~style="text-align:right;"~; 
			
			unless ($year==1 and $_==3) { 
				$readonly{$field} = 'READONLY' ; 
			} else {
				$trigger_jquery_raw .= qq~\$("#inputYear_1_$_$suffix").change(function() {					
					calcAnnual$field() ;
					});
				~;
				$trigger_jquery_raw .= qq~function calcAnnual$field() 
					{					   
						var annual_ttl = getNum(parseFloat(\$("#inputYear_1_$_$suffix").val())) + getNum(parseFloat(\$("#inputYear_2_$_$suffix").val())) + getNum(parseFloat(\$("#inputYear_3_$_$suffix").val())) ;
						\$("#inputTotal_$_$suffix").val(annual_ttl.toFixed(2));
					}~;
			}		
			$preferred_placeholder{$field} = "Year $year Total $_" ;
			# my $val = &q_get_val($db{$table}{$id}{$field}) ;
			$excl_on_change{$field} = 1 ;
			$year_totals{"yearly_totals_$year"} += $db{$table}{$id}{$field} ;
			
			$fcol = 12 ;
			$add_form_fields .= qq~<div class='col-md-2' id = "b_year_$year\_$_"><div class='row'>~ if $year <= 3 ;
			$add_form_fields .= qq~<div class='col-md-0' style = 'display:none;' id = "b_year_$year\_$_"><div class='row'>~ if $year > 3 ;
			$add_form_fields .= &common_min_form_input_col($field,$db{$table}{$id}{$field},1) if $db{$table}{$id}{$field} ; 
			$add_form_fields .= &common_min_form_input_col($field,"0.00",1) unless $db{$table}{$id}{$field} ; 
			$add_form_fields .= qq~</div></div>~ ;
		}
		$fcol = 2 ;
		#----------------------------------------------------------------------------------------------------
		
		$field = "total_$_$suffix" ; $input_style{$field} = qq~style="text-align:right;"~; $readonly{$field} = 'READONLY' ;  # $fcol=2 ;
		$preferred_placeholder{$field} = "Total $_" ;
		# my $val = &q_get_val($db{$table}{$id}{$field}) ;
		$add_form_fields .= &common_min_form_input_col($field,$db{$table}{$id}{$field},1) ; 
		#----------------------------------------------------------------------------------------------------
		$add_form_fields .= qq~</div>~ ;
		$add_form_fields .= qq~<div class='row'><div class='col-md-12' style="margin:2px 0 0 0"></div></div>~ ;
	}
	$add_form_fields .= qq~<div class='row'><div class='col-md-3' align="right"><label class="control-label">Total</label></div>~ ;
	for my $y (1 .. 5) {
		$grand_tot += $year_totals{"yearly_totals_$y"} ;
		$field ="yearly_totals_$y" ;
		$readonly{$field} = 'READONLY' ; 
		# $year_totals{"yearly_totals_$y"} = &common_commify($year_totals{"yearly_totals_$y"}) ; 
		$input_style{$field} = qq~style="text-align:right;"~ ; 
		$fcol = 12 ;
		$add_form_fields .= qq~<div class='col-md-2' id = "total_$y"><div class='row'>~ if $y <= 3 ;
		$add_form_fields .= qq~<div class='col-md-0' id = "total_$y" style = 'display:none;'><div class='row'>~ if $y > 3 ;
		$add_form_fields .= &common_min_form_input_col($field,$year_totals{"yearly_totals_$y"},1) ;
		$add_form_fields .= qq~</div></div>~ ;		
	}
	
	$trigger_jquery_raw .= qq~
		// console.log("suffix : "+$suffix) ;console.log("1") ;
		
		if (\$("#selectPeriod").val() == 60) {
			
			for (let ii = 1; ii <= 5; ii++) {
				
				if (ii <= 3) {
					\$("#year_"+ii+"_sum").removeClass("col-md-2").addClass("col-md-1") ;
					\$("#total_"+ii+"").removeClass("col-md-2").addClass("col-md-1") 
				}
				else if (ii <= 5 && ii > 3) {
					\$("#year_"+ii+"_sum").removeClass("col-md-0").addClass("col-md-1") ;
					\$("#total_"+ii+"").removeClass("col-md-0").addClass("col-md-1") ;
					\$("#year_"+ii+"_sum").show() ; 
					\$("#total_"+ii+"").show() ;
				}
				for (let jj = 1; jj <= 3; jj++) {
				
					if (ii <= 3) {
						\$("#b_year_"+ii+"_"+jj+"").removeClass("col-md-2").addClass("col-md-1") ;
					}	
					else if (ii <= 5 && ii > 3) {
						\$("#b_year_"+ii+"_"+jj+"").removeClass("col-md-0").addClass("col-md-1") ;
						\$("#b_year_"+ii+"_"+jj+"").show() ;
					}	
				}			
			}
		}
		
		\$("#selectPeriod").on("change", function() {
			
			for (let i = 1; i <= 5; i++) {
				
				if (i <= 3 && \$(this).val() == 36) {
					\$("#year_"+i+"_sum").removeClass("col-md-1").addClass("col-md-2") ;
					\$("#total_"+i+"").removeClass("col-md-1").addClass("col-md-2") ;
				}
				else if (i <= 3 && \$(this).val() == 60) {
					\$("#year_"+i+"_sum").removeClass("col-md-2").addClass("col-md-1") ;
					\$("#total_"+i+"").removeClass("col-md-2").addClass("col-md-1") 
				}
				else if (i <= 5 && i > 3 && \$(this).val() == 36) {
					\$("#year_"+i+"_sum").removeClass("col-md-1").addClass("col-md-0") ;
					\$("#total_"+i+"").removeClass("col-md-1").addClass("col-md-0") ;
					\$("#year_"+i+"_sum").hide() ; 
					\$("#total_"+i+"").hide() ; 
				}
				else if (i <= 5 && i > 3 && \$(this).val() == 60) {
					\$("#year_"+i+"_sum").removeClass("col-md-0").addClass("col-md-1") ;
					\$("#total_"+i+"").removeClass("col-md-0").addClass("col-md-1") ;
					\$("#year_"+i+"_sum").show() ; 
					\$("#total_"+i+"").show() ;
				}
				for (let j = 1; j <= 3; j++) {
					
					if (i <= 3 && \$(this).val() == 36) {
						\$("#b_year_"+i+"_"+j+"").removeClass("col-md-1").addClass("col-md-2") ;
					}	
					else if (i <= 5 && i > 3 && \$(this).val() == 36) {
						\$("#b_year_"+i+"_"+j+"").removeClass("col-md-1").addClass("col-md-0") ;
						\$("#b_year_"+i+"_"+j+"").hide() ; 
					}

					else if (i <= 3 && \$(this).val() == 60) {
						\$("#b_year_"+i+"_"+j+"").removeClass("col-md-2").addClass("col-md-1") ;
					}	
					else if (i <= 5 && i > 3 && \$(this).val() == 60) {
						\$("#b_year_"+i+"_"+j+"").removeClass("col-md-0").addClass("col-md-1") ;
						\$("#b_year_"+i+"_"+j+"").show() ;
						// if (\$("#b_year_"+i+"_"+j+"").text().trim() === '') {
							// \$("#b_year_"+i+"_"+j+"").text("0.00") ;
						// }
					}	
				}			
			}
		});
	~ ; 
	
	$fcol = 2 ;
	$field ="yearly_total" ;
	$readonly{$field} = 'READONLY' ; 
	# $grand_tot = &common_commify($grand_tot) ; 
	$input_style{$field} = qq~style="text-align:right;"~ ; 
	$add_form_fields .= &common_min_form_input_col($field,$grand_tot,1) ; 
	$add_form_fields .= qq~</div>~ ;
	$add_form_fields .= qq~<div class='row'><div class='col-md-12'>&nbsp;</div></div>~ ;
	
	&tab_content_box($sec_name,$sec_col);

	# --------------- END PURCHASE SUMMARY ----------------------------------------------------------------------------------------------------------------
	}

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 . '|' ;
	
	if ($ret) { return ; }
	
	$add_form_fields = '' ;
	
	# &set_check_boxes_checked($id,$sec_cnt,"excl",$sec);
	
 
	&db_min_ro('quote_notes','*',"id='1'",'','') ;
	
	$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 .. 12) { &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 .. 6) { &tab_build_terms_fields('payment_term',$_,$id) ; }
	
	$add_form_fields .= qq~<div class='row'><div class='col-md-12'><h4>ROE:</h4></div></div>~ ;
	&tab_build_terms_headers;
	
	$cntnoteloop=1; for (1 .. 6) { &tab_build_terms_fields('roe',$_,$id) ; }
	
	$add_form_fields .= qq~<div class='row'><div class='col-md-12'><h4>Delivery Time:</h4></div></div>~ ;
	&tab_build_terms_headers;
	
	$cntnoteloop=1; for (1 .. 4) { &tab_build_terms_fields('delivery_time',$_,$id) ; }
	
	$add_form_fields .= qq~<div class='row'><div class='col-md-12'><h4>Terms and Conditions:</h4></div></div>~ ;
	&tab_build_terms_headers;
	
	$cntnoteloop=1; for (1 .. 1) { &tab_build_terms_fields_terms_and_conditions('terms',$_,$id) ; }
	
	$trigger_jquery .= qq~\$("#selectTerms_chosen").css( "width", "100%" );~ ;
	
	&tab_content_box($sec_name,$sec_col);

	# --------------- END NOTES ----------------------------------------------------------------------------------------------------------------
	}

if ($tab==6) { # Camera Details

	# --------------- START CAMERA DETAILS ----------------------------------------------------------------------------------------------------------------
	
	$add_form_fields = '' ;
	
	my $sec = '_camera_details' ; my ($sec_name,$sec_cnt,$sec_col) = &tab_sec_det($sec) ;
	
	$ret_pdf_link_sec .= $sec . '+' . $sec_cnt . '|' ;
	
	if ($ret) { return ; }
	
	$add_form_fields  = '' ;
	$add_form_fields .= qq~<div class='row'>~ ;
	$add_form_fields .= qq~<div class='col-md-1'><label class='control-label'>&nbsp;</label></div>~ ;
	$add_form_fields .= qq~<div class='col-md-2'><label class='control-label'>CameraNr</label></div>~ ;
	# $add_form_fields .= qq~<div class='col-md-1'><label class='control-label'>Item Code</label></div>~ ;
	$add_form_fields .= qq~<div class='col-md-1'><label class='control-label'>Description</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-2'><label class='control-label'>Serial Number</label></div>~ ;
	$add_form_fields .= qq~<div class='col-md-1'><label class='control-label'>Ref</label></div>~ ;	
	$add_form_fields .= qq~<div class='col-md-1'><label class='control-label'>Delivery Date</label></div>~ ;
	$add_form_fields .= qq~<div class='col-md-1'><label class='control-label'>Active Date</label></div>~ ;
	$add_form_fields .= qq~<div class='col-md-1'><label class='control-label'>E/W Date From</label></div>~ ;
	$add_form_fields .= qq~<div class='col-md-1'><label class='control-label'>E/W Date To</label></div>~ ;
	$add_form_fields .= qq~<div class='col-md-1'><label class='control-label'>&nbsp;</label></div>~ ;
	$add_form_fields .= qq~</div>~ ;
	
	# my $suffix = $sec ; my $rcnt = 11 ;
	# my $suffix = $sec ; my $rcnt = 20 ;
	my $suffix = $sec ; my $rcnt = $max_cams ; # set in quotes.pl
	# our %linked_vpu = () ; our %vpu_serial_nr = () ; our %vpu_camera_nr = () ; 
	&tab_camera_details_select_opts('cameras','camera_nr',"camera_nr NOT LIKE 'vpu%'",$suffix,$rcnt) ;
	
	$rcnt = $max_cams ; # reset in tab_camera_details_select_opts 
	
	# &tab_camera_details_select_opts('cameras','camera_nr',"event_system_id < 1",$suffix,$rcnt) ;
	
	my $cam_qtys = '' ;
	
	# &common_debug("cam_qtys : quote_nr=$db{$table}{$id}{quote_nr}") ;
	
	foreach (keys %{$camera_system_count{$db{$table}{$id}{quote_nr}}}) {
		# &common_debug("cam_qtys : cam=$_, qty=$camera_system_count{$db{$table}{$id}{quote_nr}}{$_}") ;
		$cam_qtys .= qq~$_ : $camera_system_count{$db{$table}{$id}{quote_nr}}{$_}<br>~ ; 
	}
		
	# our $cam_box_icon = qq~&nbsp;<a href="#" data-toggle="popover" data-placement="left" data-content="$cam_qtys" title="Camera Qty Count" data-html="true" data-trigger="focus" class="btn btn-default btn-xs"><i class="glyphicons glyphicons-hash" style="margin-top:3px;"></i></a>~ if $cam_qtys ;
	our $cam_box_icon = qq~<a href="#" data-toggle="popover" data-placement="left" data-content="$cam_qtys" title="Camera Qty Count" data-html="true" data-trigger="focus" class="btn btn-round btn-default"><i class="glyphicons glyphicons-hash" style="margin-top:-2.5px;margin-left:-9px;"></i></a>&nbsp;~ if $cam_qtys ;
	
	# 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");	
		# &build_cost_form($id,$tab,$parts[0],"$sec$_k"); 
		# }

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

	# $add_form_fields .= qq~<div class='row'>~ ;
	# if ($checkit{$exfield} == 1) { $checked = 'CHECKED'; } elsif (($excl_tick{$cnt}) and ($iaction eq 'add')) { $checked = 'CHECKED'; } else { $checked = ''; }

	# &set_check_boxes_checked($id,$rcnt,"excl",$suffix);
	
	my $cntcamloop=1;

	for (1 .. $rcnt) {

		my $q_nr = $db{$table}{$id}{quote_nr} ;
		
		
		# next if substr($sel_cam_nr{$camera_details{$q_nr}{$_}{"$_$suffix"}{id}},0,3) eq 'VPU' ; 
		my $a = "$_$suffix" ;
		# &common_debug("rcnt : $_ , q_nr : $q_nr , camera_details : $camera_details{$q_nr}{$_}{$a}{id}") ; 
		
		$cam_display_style = '' ; $cam_display_plus_btn = '' ;	
		# unless ($_==1 or $db{$table}{$id}{"camera_nr_$_$suffix"}) { 
		unless ($_==1 or $camera_details{$q_nr}{$_}{"$_$suffix"}{id}) { 
			$cam_display_style = qq~style='display:none;'~ ; 
			$trigger_jquery_raw .= qq~ \$("#btn_$_$suffix").click(function () {
					\$("#$_$suffix").toggle();
					\$("#btn_$_$suffix").hide();
					var v = $_ + 1 ;
					\$("#btn_"+v+"$suffix").show();
				});~ ; 
			if ($_>$cntcamloop) { $cam_display_butt = qq~display:none;~ ;  } else { $cam_display_butt = '' ; }
			$cam_display_plus_btn = qq~<a id="btn_$_$suffix" href="#" style="padding-left:16px;$cam_display_butt"><i class="glyphicon glyphicon-plus blue"></i></a>~;
		} else { $cntcamloop++ ; }
		

		
		$add_form_fields .= qq~<div class='row' id='$_$suffix' $cam_display_style>~ ;
		
		# my $cam_id = $db{$table}{$id}{"camera_nr_$_$suffix"} ;
		
		# $add_form_fields .= qq~<div class='row'>~ ;
		#----------------------------------------------------------------------------------------------------
		$field = "excl_$_$suffix" ; $fcol=1 ;		
		my $val = $camera_details{$q_nr}{$_}{"$_$suffix"}{quote_cam_excl} ; 
		# if ($checkit{$field} == 1) { $checked = 'CHECKED'; } elsif ($_!=1 and $iaction eq 'add') { $checked = 'CHECKED'; }  else { $checked = ''; }
		if ($val==1) { $checked = 'CHECKED'; } elsif ($iaction eq 'add') { $checked = 'CHECKED'; } elsif ($val eq '0') { $checked = ''; }  else { $checked = 'CHECKED'; }
		# &common_debug("$field : [$val] [$checked] [$_$suffix]") ;
		$label{$field} = 1 ;
		$excl_on_change{$field} = 1 ;
		$add_form_fields .= &common_min_form_checkbox_col($field,$val,$checked) ;
		#----------------------------------------------------------------------------------------------------
		my $field = "camera_nr_$_$suffix" ; $fcol=2 ;
		$allow_deselect{$field} = 1 ;
		$preferred_placeholder{$field} = "Camera Nr $_" ;
		# my $val = $db{$table}{$id}{$field} ; # if ($_==1) { $readonly{$field} = 'READONLY' ; }
		$val = $camera_details{$q_nr}{$_}{"$_$suffix"}{id} ; # if ($_==1) { $readonly{$field} = 'READONLY' ; }
		# $add_form_fields .= &common_min_form_input_col($field,$val) ;
		$excl_on_change{$field} = 1 ;
		
		$opts{$field} = $opts_on_edit{$field} if $opts_on_edit{$field} and $db{quotes}{$i{id}}{max_cams} > 50 ; #and $username eq 'rory' ; # overwrite if selected and max_cams exceeded
		
		$add_form_fields .= &common_min_form_select_col($field,$val) ;
		
		my $abrv_cam_system = substr($sel_cam_nr{$val},0,3);
		# next if $abrv_cam_system eq 'VPU'  ; 
		# &common_debug("abrv_cam_system=$abrv_cam_system, abrv_prev_cam_system=$abrv_prev_cam_system, prev_cam_nr=$prev_cam_nr, link to: c_excl_$_\_camera_details") ;				
		# if ($abrv_cam_system eq 'VPU' and ($abrv_prev_cam_system eq 'S1' or $abrv_prev_cam_system_other eq 'Prime' or $abrv_prev_cam_system_other eq 'Coach')) {
		
		# if ($abrv_cam_system eq 'VPU' and ($abrv_prev_cam_system eq 'S1' or $abrv_prev_cam_system eq 'S2' or $abrv_prev_cam_system_other eq 'Prime' or $abrv_prev_cam_system_other eq 'Coach')) {
	
		if ($linked_vpu{$val}) {
			$trigger_jquery .= qq~\$("#c_excl_$_\_camera_details").append("<i class='glyphicons glyphicons-link fright' title='Linked to VPU $vpu_camera_nr{$linked_vpu{$val}} with Serial Nr $vpu_serial_nr{$linked_vpu{$val}}' data-toggle='tooltip' data-placement='right'></i>");\$('[data-toggle="tooltip"]').tooltip({html:true}) ;~;			
		}	
	# }
		$abrv_prev_cam_system       = substr($abrv_cam_system,0,2);
		$abrv_prev_cam_system_other = substr($abrv_cam_system,0,5);
		$prev_cam_nr                = $sel_cam_nr{$val} ;
		#----------------------------------------------------------------------------------------------------
		# # my $field = "item_code_$_$suffix" ; $fcol=1 ;
		# # $preferred_placeholder{$field} = "Item Code $_" ;
		# # my $val = $db{$table}{$id}{$field} ; # if ($_==1) { $readonly{$field} = 'READONLY' ; }
		# # $add_form_fields .= &common_min_form_input_col($field,$val) ;
		#----------------------------------------------------------------------------------------------------
		my $field = "description_$_$suffix" ; $fcol=1 ;
		$preferred_placeholder{$field} = "Description $_" ;
		# my $val = $db{$table}{$id}{$field} ; # if ($_==1) { $readonly{$field} = 'READONLY' ; }
		my $val = $camera_details{$q_nr}{$_}{"$_$suffix"}{quote_cam_desc} ; # if ($_==1) { $readonly{$field} = 'READONLY' ; }
		$excl_on_change{$field} = 1 ;
		$add_form_fields .= &common_min_form_input_col($field,$val) ;
		#----------------------------------------------------------------------------------------------------
		# my $field = "qty_$_$suffix" ; $fcol=1 ;
		# $preferred_placeholder{$field} = "Qty $_" ;
		# my $val = $db{$table}{$id}{$field} ; # if ($_==1) { $readonly{$field} = 'READONLY' ; }
		# $type = 'hidden' ; $add_form_fields .= &common_min_form_input_col($field,$val) ; $type = '' ;
		#----------------------------------------------------------------------------------------------------
		my $field = "serial_nr_$_$suffix" ; $fcol=2 ;
		$preferred_placeholder{$field} = "Serial Nr $_" ;
		# my $val = $db{$table}{$id}{$field} ; # if ($_==1) { $readonly{$field} = 'READONLY' ; }
		my $val = $camera_details{$q_nr}{$_}{"$_$suffix"}{serial_nr} ; # if ($_==1) { $readonly{$field} = 'READONLY' ; }
		$excl_on_change{$field} = 1 ;
		$add_form_fields .= &common_min_form_input_col($field,$val) ;
		#----------------------------------------------------------------------------------------------------		
		my $field = "ref_$_$suffix" ; $fcol=1 ;
		$preferred_placeholder{$field} = "Ref $_" ;
		# my $val = $db{$table}{$id}{$field} ;
		my $val = $camera_details{$q_nr}{$_}{"$_$suffix"}{quote_ref}  ;
		$excl_on_change{$field} = 1 ;
		$add_form_fields .= &common_min_form_input_col($field,$val) ;
		# &common_debug("2. $field : [$q_nr] [$_] [$_$suffix] [$val]") if $val;
		#----------------------------------------------------------------------------------------------------
		my $field = "delivery_date_$_$suffix" ; $fcol=1 ;
		$preferred_placeholder{$field} = "Delivery Date $_" ;
		# my $val = $db{$table}{$id}{$field} ; # if ($_==1) { $readonly{$field} = 'READONLY' ; }
		my $val = $camera_details{$q_nr}{$_}{"$_$suffix"}{quote_delivery_date} ; # if ($_==1) { $readonly{$field} = 'READONLY' ; }
		$excl_on_change{$field} = 1 ;
		$add_form_fields .= &common_min_form_datepicker_col($field,$val) ;
		#----------------------------------------------------------------------------------------------------
		my $field = "active_date_$_$suffix" ; $fcol=1 ;
		$preferred_placeholder{$field} = "Active Date $_" ;
		# my $val = $db{$table}{$id}{$field} ; # if ($_==1) { $readonly{$field} = 'READONLY' ; }
		my $val = $camera_details{$q_nr}{$_}{"$_$suffix"}{quote_active_date} ; # if ($_==1) { $readonly{$field} = 'READONLY' ; }
		$add_form_fields .= &common_min_form_datepicker_col($field,$val) ;
		#----------------------------------------------------------------------------------------------------
		my $field = "ew_date_from_$_$suffix" ; $fcol=1 ;
		$preferred_placeholder{$field} = "E/W Date From $_" ;
		# my $val = $db{$table}{$id}{$field} ; # if ($_==1) { $readonly{$field} = 'READONLY' ; }
		my $val = $camera_details{$q_nr}{$_}{"$_$suffix"}{quote_ew_date_from} ; # if ($_==1) { $readonly{$field} = 'READONLY' ; }
		$excl_on_change{$field} = 1 ;
		$add_form_fields .= &common_min_form_datepicker_col($field,$val) ;
		#----------------------------------------------------------------------------------------------------
		my $field = "ew_date_to_$_$suffix" ; $fcol=1 ;
		$preferred_placeholder{$field} = "E/W Date To $_" ;
		# my $val = $db{$table}{$id}{$field} ; # if ($_==1) { $readonly{$field} = 'READONLY' ; }
		my $val = $camera_details{$q_nr}{$_}{"$_$suffix"}{quote_ew_date_to} ; # if ($_==1) { $readonly{$field} = 'READONLY' ; }
		$excl_on_change{$field} = 1 ;
		$add_form_fields .= &common_min_form_datepicker_col($field,$val) ;
		#----------------------------------------------------------------------------------------------------		
		# my $ew_pdf_butt = '' ; if ($username eq 'rory' or $username eq 'handre') { $ew_pdf_butt = qq~&nbsp;<a class='btn btn-default btn-xs' href="javascript:dlgMdl('$useropts{scripts}/pdf/warranty_pdf.pl?$id&$camera_details{$q_nr}{$_}{"$_$suffix"}{id}','EXTENDED WARRANTY PDF [$camera_details{$q_nr}{$_}{"$_$suffix"}{camera_nr}]','','waybill-dialog');" data-title="Extended Warranty PDF" data-toggle="tooltip" data-placement="right"><i class="glyphicon glyphicon-print"></i></a>~; }
		# my $ew_pdf_butt = '' ; if ($camera_details{$q_nr}{$_}{"$_$suffix"}{quote_ew_date_from} and $camera_details{$q_nr}{$_}{"$_$suffix"}{quote_ew_date_to} and uc substr($camera_details{$q_nr}{$_}{"$_$suffix"}{camera_nr},0,3) ne 'VPU') { $ew_pdf_butt = qq~&nbsp;<a class='btn btn-default btn-xs' href="javascript:dlgMdl('$useropts{scripts}/pdf/warranty_pdf.pl?$id&$camera_details{$q_nr}{$_}{"$_$suffix"}{id}','EXTENDED WARRANTY PDF [$camera_details{$q_nr}{$_}{"$_$suffix"}{camera_nr}]','','waybill-dialog');" data-title="Extended Warranty PDF [$camera_details{$q_nr}{$_}{"$_$suffix"}{camera_nr}]" data-toggle="tooltip" data-placement="right"><i class="glyphicon glyphicon-print"></i></a>~; }
		my $ew_pdf_butt = '' ; if ($camera_details{$q_nr}{$_}{"$_$suffix"}{quote_ew_date_from} and $camera_details{$q_nr}{$_}{"$_$suffix"}{quote_ew_date_to} and (uc substr($camera_details{$q_nr}{$_}{"$_$suffix"}{camera_nr},0,2) eq 'S1' or uc substr($camera_details{$q_nr}{$_}{"$_$suffix"}{camera_nr},0,5) eq 'SCORE')) { $ew_pdf_butt = qq~&nbsp;<a class='btn btn-default btn-xs' href="javascript:dlgMdl('$useropts{scripts}/pdf/warranty_pdf.pl?$id&$camera_details{$q_nr}{$_}{"$_$suffix"}{id}','EXTENDED WARRANTY PDF [$camera_details{$q_nr}{$_}{"$_$suffix"}{camera_nr}]','','waybill-dialog');" data-title="Extended Warranty PDF [$camera_details{$q_nr}{$_}{"$_$suffix"}{camera_nr}]" data-toggle="tooltip" data-placement="right"><i class="glyphicon glyphicon-print"></i></a>~; }
		#----------------------------------------------------------------------------------------------------
		$add_form_fields .= qq~<div class="col-md-1">$ew_pdf_butt</div>~ ;
		$add_form_fields .= qq~</div>~ ;
		$add_form_fields .= qq~<div class='row'><div class="col-md-1">$cam_display_plus_btn</div></div>~ ;
		$add_form_fields .= qq~<div class='row'><div class='col-md-12' style="margin:2px 0 0 0"></div></div>~ ;
	}
	
	$add_form_fields .= qq~<div class='row'><div class='col-md-12'>&nbsp;</div></div>~ ;
	
	&tab_content_box($sec_name,$sec_col);
	
	$cam_box_icon = '' ;

	# --------------- END CAMERA DETAILS ----------------------------------------------------------------------------------------------------------------
}
	
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 . '|' ;
	
	if ($ret) { return ; }
	
	$lcol = 2 ;
	$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_checkbox('annual_invoice',$db{$table}{$id}{annual_invoice},$checked) ;
	# if ($db{$table}{$id}{invoice_date} eq '' or $db{$table}{$id}{invoice_date} eq '0000-00-00') { $db{$table}{$id}{invoice_date} = $now_ccyy_mm_dd  ; }
	$add_form_fields .= &common_min_form_datepicker('invoice_date',$db{$table}{$id}{invoice_date}) ;
	$add_form_fields .= &common_min_form_input('annual_invoice_2',$db{$table}{$id}{annual_invoice_2},'') ;
	$add_form_fields .= &common_min_form_input('annual_invoice_3',$db{$table}{$id}{annual_invoice_3},'') ;
	# $add_form_fields .= qq~<div class='row'><div class='col-md-12' style="height:20px;">&nbsp;</div></div>~ ;
	
	&tab_content_box($sec_name,$sec_col);

	# --------------- END INVOICE DETAILS ----------------------------------------------------------------------------------------------------------
	}

# if ($tab==7) { # Extended Warranty

	# # --------------- START EXTENDED WARRANTY --------------------------------------------------------------------------------------------------------
	
	# my $sec = '_extended_warranty' ; my ($sec_name,$sec_cnt,$sec_col) = &tab_sec_det($sec) ;
	
	# $ret_pdf_link_sec .= $sec . '+' . $sec_cnt . '|' ;
	
	# if ($ret) { return ; }
	
	# $lcol = 2 ;
	# $fcol = 3 ;
	
	# $add_form_fields = '' ;
	
	# if (($db{$table}{$id}{date_from} eq '') or ($db{$table}{$id}{date_from} eq '0000-00-00')) { $db{$table}{$id}{date_from} = $now_ccyy_mm_dd  ; }
	# $add_form_fields .= &common_min_form_datepicker('date_from',$db{$table}{$id}{date_from}) ;
	# if (($db{$table}{$id}{date_to} eq '') or ($db{$table}{$id}{date_to} eq '0000-00-00')) { $db{$table}{$id}{date_to} = $now_ccyy_mm_dd  ; }
	# $add_form_fields .= &common_min_form_datepicker('date_to',$db{$table}{$id}{date_to}) ;
	
	# &tab_content_box($sec_name,$sec_col);

	# # --------------- END EXTENDED WARRANTY ----------------------------------------------------------------------------------------------------------
	# }

# if ($tab==8) { # Rental

	# # --------------- START RENTAL --------------------------------------------------------------------------------------------------------
	
	# my $sec = '_rental' ; my ($sec_name,$sec_cnt,$sec_col) = &tab_sec_det($sec) ;
	
	# $ret_pdf_link_sec .= $sec . '+' . $sec_cnt . '|' ;
	
	# if ($ret) { return ; }
	
	# $lcol = 2 ;
	# $fcol = 3 ;
	
	# $add_form_fields = '' ;
	
	# # our %sec_cnt = ('_cost' => "1-Cost");
	
	# # my $sec = '_rental' ; my ($sec_name,$sec_cnt,$sec_col) = &tab_sec_det($sec) ;	
	# # $ret_pdf_link_sec .= $sec . '+' . $sec_cnt . '|' ;
	
	# # if ($ret) { return ; }
	
	# # foreach (sort keys %sec_cnt) {
		# # 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$_");	
		# # &build_cost_form($id,$tab,$parts[0],"$sec$_"); 	
	# # }
	
	# &tab_content_box($sec_name,$sec_col);

	# # --------------- END RENTAL ----------------------------------------------------------------------------------------------------------
	# }
	
if ($tab==8) { # Document Uploads

	# --------------- START DOCUMENT UPLOADS ----------------------------------------------------------------------------------------------------------------
	
	$add_form_fields = '' ;
	
	my $sec = '_document_uploads' ; 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 @docs = ('Signed Quote','PO','Other 1','SLA','T&Cs','Other 2','Photos','Delivery Note','Other 3');
	
	$add_form_fields .= qq~<div class='row'>~ ;

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

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

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

sub tab_costing_select_opts {

my ($table,$dispfield,$where,$sec_) = @_ ;

&db_min_ro($table,'*',$where,'','') ;
my @b = (
"_costing_additional_services", 
"_costing_game_breakdown_analytics", 
"_costing_hardware_add_ons", 
"_costing_installation_kits",
"_costing_match_credits", 
"_costing_other", 
"_costing_rental", 
"_costing_software_licence", 
"_costing_systems" ) ; 

foreach my $suf (@b){
	$col_name = "item" . "$suf" ;
	$cnt = 0 ; 
	if($db{quotes_min}{$i{id}}{$col_name}){
		foreach my $col_input (split(";", $db{quotes_min}{$i{id}}{$col_name})){
			$cnt++ ; 
			$saved_field{"item"}{$suf}{$cnt} = $col_input ;  
			# &common_debug("$aa/$suffix : $cc") ; 
		}	
	}	
}

foreach my $id (keys %{$db{$table}}) {	
	foreach (sort keys %sec_cnt) {
		$_k = substr($_,1) ; $k  = substr($_,2) ;
		# &common_debug("tab_select_opts : $db{$table}{$id}{type} ne $k") ;
		unless ($db{$table}{$id}{type} eq $k) { next ; }
		my @parts = split(/\-/,$sec_cnt{$_}) ;
		for my $p_ (1 .. $parts[0]) { # item_1_costing_systems
			my $field = "$p_$sec_$_k" ; #my $ucf_field = ucfirst $field ; my $chosen_field = ucfirst $field . '_chosen' ;
			# $opts{"item_$p_$sec_$_k"} .= qq~<option value="$id">$db{$table}{$id}{$dispfield}</option>~ ; 
			my $field_2 = "$sec_$_k" ; 
			# my $saved_val = $db{quotes}{$i{id}}{"item_$field"} ;
			my $saved_val = $saved_field{"item"}{$field_2}{$p_} ;
			
			if ($saved_val) { my @valarr = split(/\:/,$saved_val) ; $saved_val = $valarr[0] ; }
			
			if ($saved_val eq $id) { $selected = 'SELECTED' ; } else { $selected = '' ; }
			
			# $opts{"item_$field"} .= qq~<option $selected value="$id:$db{$table}{$id}{freight}:$db{$table}{$id}{nett}:$db{$table}{$id}{main_price}:$db{$table}{$id}{gross_10_1_50}:$db{$table}{$id}{gross_10_51_plus}:$db{$table}{$id}{gross_20_1_50}:$db{$table}{$id}{gross_20_51_plus}:$db{$table}{$id}{gross_50}">$db{$table}{$id}{$dispfield}</option>~ ; 
			$opts{"item_$field"} .= qq~<option $selected value="$id:$db{$table}{$id}{freight}:$db{$table}{$id}{nett}:$db{$table}{$id}{main_price}:$db{$table}{$id}{source_admin}:$db{$table}{$id}{'3_perc'}:$db{$table}{$id}{'5_perc'}:$db{$table}{$id}{'6_perc'}:$db{$table}{$id}{'8_perc'}:$db{$table}{$id}{'10_perc'}:$db{$table}{$id}{'15_perc'}:$db{$table}{$id}{'20_perc'}">$db{$table}{$id}{$dispfield}</option>~ ; 
			
			# if ($field eq '3_costing_software_licence' or $field eq '2_costing_game_breakdown_analytics') {
			if ($field eq '2_costing_systems' or $field eq '3_costing_systems' or $field eq '3_costing_software_licence' or $field eq '2_costing_game_breakdown_analytics') {
				$item_type = 'input' ;
			} else { 
				$item_type = 'select' ;
			}

			unless ($got{$field}) {
				$preferred_title{"item_$field"} = &common_min_print_field("item_$p_$_k") ;
				if ($item_type eq 'select') { $trigger_jquery .= qq~\$("#selectItem_$field\_chosen").css( "width", "96%" )
~ ; } 			$got{$field} = 1 ;
				$trigger_jquery_raw .= qq~\$("#$item_type\Item_$field, #selectDiscount").change(function() {					
					calcCosting$field(1) ;
					});
				~;
				$trigger_jquery_raw .= qq~\$("#inputQty_$field, #inputUsd_amnt_$field").change(function() {					
					calcCosting$field(0) ;
					});
				~;$trigger_jquery_raw .= qq~function calcCosting$field(b) 
					{
						if (b==1) { \$("#inputUsd_amnt_$field").val(''); }
					   
						var excl = \$("#checkboxExcl_$field");
						var item = \$("#$item_type\Item_$field");
						var usd  = \$("#inputUsd_amnt_$field");
						var conv = \$("#inputCurr_amnt_$field");
						var qty  = \$("#inputQty_$field");	
						var ttl  = \$("#inputItem_ttl_$field");	
						var vat  = \$("#checkboxVat_$field");	
						
						// console.log('DISCOUNT - item: '+item.val()+', usd: '+usd.val()) ;
						
						if (!item.val()) { 
							excl.prop("checked", true);
							usd.val("");
							conv.val("");
							qty.val("");
							ttl.val("");
							vat.val("");
							// return ; 
							}
						
						calcCosting(excl,item,qty,usd,conv,vat,ttl) ;
					}~;
				
				}
			}
		}
    }
} #------------------------------------------------------------------------------------------

sub tab_camera_details_select_opts {

my ($table,$dispfield,$where,$suffix,$rcnt) = @_ ;

&db_min_ro('customers','id,name','','','') ;
foreach my $id (keys %{$db{customers}}) { $customers_name{$id} = $db{customers}{$id}{name} ; }
	
&db_min_ro('camera_systems','id,name','','','') ;
foreach my $id (keys %{$db{camera_systems}}) { $camera_system{$id} = $db{camera_systems}{$id}{name} ; }

# my $add_quote_fields = '' ; my $sql_or_where = '' ; my @sqlorwhere = () ;
# for (1 .. $rcnt) {
	# $add_quote_fields .= ",quotes.camera_nr_$_$suffix" ;
	# push @sqlorwhere, "quotes.camera_nr_$_$suffix>0" ;
# }
# $sql_or_where = join(" OR ",@sqlorwhere) ;

# &db_min_ro("$table,quotes","quotes.id,$table.id$add_quote_fields",$sql_or_where,'','') ;

# foreach my $id (keys %{$db{"$table,quotes"}}) {	
	# # $db{quotes}{$i{id}}{"camera_nr_$field"}
	# for (1 .. 10) {
		# my $field = $db{"$table,quotes"}{$id}{"camera_nr_$_$suffix"} ;
		# if ($field) {
			# # $camera_exists_on_quote{$field} = $id ; 
			# $exclude_camera_from_list{$field} = 1 ;
			# # &common_debug("exclude_camera_from_list : $field") ;
		# }
	# }
	
# }

our %linked_vpu = () ; our %vpu_serial_nr = () ; our %vpu_camera_nr = () ; my $cnt_sel=0; my %done_quote_cam_num = () ; my %done_field = () ; 
&db_min_ro($table,'id,camera_nr,serial_nr,quote_nr',"camera_nr LIKE 'vpu%'",'','') ;
foreach (keys %{$db{$table}}) {
	$vpu_serial_nr{$_} = $db{$table}{$_}{serial_nr} ;
	$vpu_camera_nr{$_} = $db{$table}{$_}{camera_nr} ;
	
	my $qnr            = $db{$table}{$_}{quote_nr} ;
	
	if ($qnr eq $db{quotes}{$i{id}}{quote_nr}) {
		$cnt_cams{$qnr}++;
		# &common_debug("$cnt_cams{$qnr}. $qnr eq $db{quotes}{$i{id}}{quote_nr} ") if $qnr == 1261 ;	
	}
	
}
&db_min_ro($table,'*',$where,'','') ;

LOOP1: foreach my $id (sort keys %{$db{$table}}) {
	
	# &common_debug("1. LOOP1 $db{$table}{$id}{quote_nr} and $db{$table}{$id}{quote_nr} ne $db{quotes}{$i{id}}{quote_nr}") if $db{$table}{$id}{quote_nr} == 1261 ;
	if ($db{$table}{$id}{quote_nr} and $db{$table}{$id}{quote_nr} ne $db{quotes}{$i{id}}{quote_nr}) { next ; }	
	
	# &common_debug("2. LOOP1 event_system_id=$db{$table}{$id}{event_system_id} ") if $db{$table}{$id}{quote_nr} == 1261 ;
	if ($db{$table}{$id}{event_system_id}) { $skip_event_linked_vpu{$db{$table}{$id}{event_linked_vpu}} = 1 ; next ; } # skip as it's being used for events
	
	# &common_debug("3. LOOP1 skip_event_linked_vpu=$skip_event_linked_vpu{$db{$table}{$id}{serial_nr}}") if $db{$table}{$id}{quote_nr} == 1261 ;
	if ($skip_event_linked_vpu{$db{$table}{$id}{serial_nr}}) { next ; } # skip as it's being used for events
	
	# &common_debug("LOOP1 rcnt=$rcnt") if $db{$table}{$id}{quote_nr} == 1261 ;
	
	my $qnr = $db{$table}{$id}{quote_nr} ;
	
	if ($qnr eq $db{quotes}{$i{id}}{quote_nr}) {
		$cnt_cams{$qnr}++;
		# &common_debug("$cnt_cams{$qnr}. $qnr eq $db{quotes}{$i{id}}{quote_nr} ") if $qnr == 1261 ;	
	}
	
	# LOOP2: for (1 .. $rcnt) {
		
		# if ($qnr eq $db{quotes}{$i{id}}{quote_nr} and $db{$table}{$id}{quote_cam_num} eq $_) { 
			# $cnt_sel++ ;
			# # &common_debug("$cnt_sel. $qnr eq $db{quotes}{$i{id}}{quote_nr} and $db{$table}{$id}{quote_cam_num} eq $_") if $qnr == 1261 ;
		# }
	# }
}

$max_cams = $cnt_cams{$db{quotes}{$i{id}}{quote_nr}} if $cnt_cams{$db{quotes}{$i{id}}{quote_nr}} > $max_cams && $db{quotes}{$i{id}}{quote_nr} ;

&common_debug("$cnt_cams{$db{quotes}{$i{id}}{quote_nr}} [max_cams = $max_cams]") ;

# print "\n cnt_sel : $cnt_sel \n" ;
LOOP1: foreach my $id (sort keys %{$db{$table}}) {
	
	# &common_debug("exclude_camera_from_list [$exclude_camera_from_list{$id}] : [$table] : $id") ;
	
	if ($db{$table}{$id}{quote_nr} and $db{$table}{$id}{quote_nr} ne $db{quotes}{$i{id}}{quote_nr}) { next ; }
	
	if ($db{$table}{$id}{event_system_id}) { $skip_event_linked_vpu{$db{$table}{$id}{event_linked_vpu}} = 1 ; next ; } # skip as it's being used for events
	if ($skip_event_linked_vpu{$db{$table}{$id}{serial_nr}}) { next ; } # skip as it's being used for events
	
	my ($camtype,$camcnt) = split(/\-/,$db{$table}{$id}{$dispfield}) ;
	$camera_system_count{$db{$table}{$id}{quote_nr}}{$camtype}++ if $db{$table}{$id}{quote_nr} and $camtype ;
	$linked_vpu{$id} = $db{$table}{$id}{linked_vpu_id} ;

	# &common_debug("[cam_qtys] quote_nr : [$db{$table}{$id}{quote_nr}] [$db{$table}{$id}{$dispfield}] [$camera_system_count{$db{$table}{$id}{quote_nr}}{$db{$table}{$id}{$dispfield}}]") if $db{$table}{$id}{quote_nr} ;
	
	my $qnr = $db{$table}{$id}{quote_nr} ;
	
	# LOOP2: for (1 .. $rcnt) {
	LOOP2: for (1 .. $max_cams) {
		my $field = "$_$suffix" ;
		
		# if ($exclude_camera_from_list{$id} and $db{quotes}{$i{id}}{"camera_nr_$_$suffix"} ne $id) { next LOOP2; ; }
		# if ($exclude_camera_from_list{$id} and $db{quotes}{$i{id}}{"camera_nr_$_$suffix"} ne $id) { next LOOP1; ; }

		# my $quote_camera_id = $db{quotes}{$i{id}}{"camera_nr_$field"} ; if ($quote_camera_id) { my @valarr = split(/\:/,$quote_camera_id) ; $quote_camera_id = $valarr[0] ; }
		
		# # my $abrv_cam_system = substr($db{$table}{$id}{camera_nr},0,3);
		# next if $seen_cnt{$_} ; 
		# $seen_cnt{$_}  = 1 ;
		
		# next unless $qnr ;
		my $selected = '' ; 

		# if ($saved_val eq $id) { $selected = 'SELECTED' ; $sel_cam_nr{$id} = $db{$table}{$id}{camera_nr}; } else { $selected = '' ; }
		# if ($db{$table}{$id}{quote_nr} eq $db{quotes}{$i{id}}{quote_nr} and $quote_camera_id eq $id) { 
		
		# if ($qnr == 1261) {
			# &common_debug("if ($qnr eq $db{quotes}{$i{id}}{quote_nr} and $_ <= $cnt_sel and not $done_quote_cam_num{$db{$table}{$id}{quote_cam_num}} and not $done_field{$field}) { ") ;
		# }
			
		# if ($qnr eq $db{quotes}{$i{id}}{quote_nr} and $_ <= $cnt_sel and not $done_quote_cam_num{$db{$table}{$id}{quote_cam_num}} and not $done_field{$field}) { 
		if ($qnr eq $db{quotes}{$i{id}}{quote_nr} and $_ <= $max_cams and not $done_quote_cam_num{$db{$table}{$id}{quote_cam_num}} and not $done_field{$field}) { 
			$done_quote_cam_num{$db{$table}{$id}{quote_cam_num}} = 1 ; $done_field{$field} = 1 ;
			$selected = 'SELECTED' ; $sel_cam_nr{$id} = $db{$table}{$id}{camera_nr}; 
			# &common_debug("1. quote_ref : [$db{$table}{$id}{quote_cam_num}] [$field] [$db{$table}{$id}{quote_ref}] [$_. max_cams=$max_cams]");
			$camera_details{$qnr}{$_}{$field}{quote_delivery_date} = $db{$table}{$id}{quote_delivery_date} ;
			$camera_details{$qnr}{$_}{$field}{quote_active_date}   = $db{$table}{$id}{quote_active_date}   ;
			$camera_details{$qnr}{$_}{$field}{quote_ew_date_from}  = $db{$table}{$id}{quote_ew_date_from}  ;
			$camera_details{$qnr}{$_}{$field}{quote_ew_date_to}    = $db{$table}{$id}{quote_ew_date_to}    ;
			$camera_details{$qnr}{$_}{$field}{quote_cam_desc}      = $db{$table}{$id}{quote_cam_desc}      ;
			$camera_details{$qnr}{$_}{$field}{quote_cam_excl}      = $db{$table}{$id}{quote_cam_excl}      ;
			$camera_details{$qnr}{$_}{$field}{quote_cam_num}       = $db{$table}{$id}{quote_cam_num}       ;
			$camera_details{$qnr}{$_}{$field}{quote_nr}            = $qnr            ; 
			$camera_details{$qnr}{$_}{$field}{quote_ref}           = $db{$table}{$id}{quote_ref}           ; 
			$camera_details{$qnr}{$_}{$field}{serial_nr}           = $db{$table}{$id}{serial_nr}           ;
			$camera_details{$qnr}{$_}{$field}{camera_nr}           = $db{$table}{$id}{camera_nr}           ;
			$camera_details{$qnr}{$_}{$field}{id}                  = $id                                   ;
		} elsif ($qnr and $db{$table}{$id}{quote_cam_num}) { # camera alread linked to a quote
			next ;
		}
	
		# print "\n $qnr eq $db{quotes}{$i{id}}{quote_nr} and  $db{$table}{$id}{quote_cam_num} and $_ <= $cnt_sel , field : $field" ; 
		# &common_debug("tab_camera_details_select_opts [$field] : [$saved_val eq $id] : $selected") ;
		
		my $display_field = $db{$table}{$id}{$dispfield} ; my ($system,$camcnt) = split(/\-/,$display_field) ; $camcnt+=0; $display_field .= " [$camcnt] [$db{$table}{$id}{serial_nr}]" ; # $display_field =~ s/-//g ;	
		
		# my $opts = qq~<option $selected value="$id:$db{$table}{$id}{camera_nr}:$db{$table}{$id}{camera_system_id}:$db{$table}{$id}{serial_nr}:$db{$table}{$id}{date_delivered}:$camera_system{$db{$table}{$id}{camera_system_id}}:$db{$table}{$id}{item_code}:$db{$table}{$id}{date_active}">$display_field</option>~ ; 
		my $opts = qq~<option $selected value="$id:$db{$table}{$id}{camera_nr}:$db{$table}{$id}{camera_system_id}:$db{$table}{$id}{serial_nr}:$db{$table}{$id}{date_delivered}:$camera_system{$db{$table}{$id}{camera_system_id}}:$db{$table}{$id}{item_code}:$db{$table}{$id}{date_active}">$display_field</option>~ ; 
		
		$opts_on_edit{"camera_nr_$field"} = $opts if $selected ; 
		
		$opts{"camera_nr_$field"} .= $opts ; 
		
		# $debugcnt++ ; &common_debug("$debugcnt. camera_nr_$field [$display_field] : [$iaction eq 'edit' and $camera_details{$q_nr}{$_}{$field}{id}] : $selected : cam camera_nr=$db{$table}{$id}{camera_nr} [max_cams=$db{quotes}{$i{id}}{max_cams}]") ; 	
		
		# # if ($username eq 'rory' and $iaction eq 'edit' and $db{quotes}{$i{id}}{quote_nr} and $db{quotes}{$i{id}}{max_cams} > 25) {
		# if ($username eq 'rory' and $iaction eq 'edit' and $camera_details{$q_nr}{$_}{$field}{id} and $db{quotes}{$i{id}}{max_cams} > 25) {
			# $opts_single{"camera_nr_$field"} = $opts if $selected ; 
			# $debugcnt++ ; &common_debug("$debugcnt. camera_nr_$field [$display_field] : [$username eq 'rory' and $iaction eq 'edit' and $camera_details{$q_nr}{$_}{$field}{id}] : $selected : cam camera_nr=$db{$table}{$id}{camera_nr} [max_cams=$db{quotes}{$i{id}}{max_cams}]") if $selected ; 	
		# } else {
			# $opts{"camera_nr_$field"} .= $opts ; 
			# $debugcnt++ ; &common_debug("NO *** $debugcnt. *** camera_nr_$field [$display_field] : [$username eq 'rory' and $iaction eq 'edit' and $camera_details{$q_nr}{$_}{$field}{id}] : $selected : cam camera_nr=$db{$table}{$id}{camera_nr} [max_cams=$db{quotes}{$i{id}}{max_cams}]") ; 	
		# }
		
		unless ($got{$field}) {			
			# $preferred_title{"item_$field"} = &common_min_print_field("item_$p_$_k") ;
			$trigger_jquery .= qq~\$("#selectCamera_nr_$field\_chosen").css( "width", "96%" );
~ ; 		$got{$field} = 1 ;

			$trigger_jquery_raw .= qq~\$("#selectCamera_nr_$field").change(function() {					
				dispCamDetails$field('$_') ;
				});
			~;
			
			$trigger_jquery_raw .= qq~function dispCamDetails$field(f) 
				{
					var excl   = \$("#checkboxExcl_$field");
					var camnr  = \$("#selectCamera_nr_$field");
					var itcode = '' ; // var itcode = \$("#inputItem_code_$field");
					var desc   = \$("#inputDescription_$field");
					var qty    = \$("#inputQty_$field");	
					var snr    = \$("#inputSerial_nr_$field");	
					var ddate  = \$("#datepickDelivery_date_$field");	
					var adate  = \$("#datepickActive_date_$field");	
					
					dispCamDetails(excl,camnr,qty,itcode,desc,snr,ddate,adate,f) ;
				}~;
		}
	}
}
	
} #------------------------------------------------------------------------------------------

sub tab_customer_select_opts {

my ($table,$dispfield,$where,$field) = @_ ;

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

foreach my $id (keys %{$db{$table}}) {	
		
		my $saved_val = $db{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%" );
			$trigger_jquery .= qq~\$("#selectQuote_to_chosen").css( "width", "100%" );
~ ; 		$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'>~ ;
$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-6'><label class='control-label'>Description</label></div>~ ;
$add_form_fields .= qq~</div>~ ;
	
} #------------------------------------------------------------------------------------

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

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

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

my $val = ($db{$table}{$id}{$inpfield} && $db{$table}{$id}{$inpfield} ne "-1") ? $db{$table}{$id}{$inpfield} : (($db{$table}{$id}{$inpfield} && $db{$table}{$id}{$inpfield} eq "-1")) ? "" : $db{quote_notes}{1}{$inpfield} ;

$note_display_style = '' ; $note_display_plus_btn = '' ;	
# unless ($cnt==1 or $db{quote_notes}{1}{$inpfield}) { 
unless ($cnt==1 || $val) { 
	$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>~ ;

# $add_form_fields .= qq~<div class='row'>~ ;
# if ($checkit{$exfield} == 1) { $checked = 'CHECKED'; } elsif (($excl_tick{$cnt}) and ($iaction eq 'add')) { $checked = 'CHECKED'; } else { $checked = ''; }

# if ($db{$table}{$id}{$exfield} == 1)  { $checked = 'CHECKED'; } else { $checked = ''; }
$sec = "_" . $sec . "s" ;
$col_name = "excl" . "$sec" ;
$counter = 0 ; 
# &db_min_ro('quotes_min', "id, excl$sec", "id = $id", '', '') ; 

foreach my $col_input (split(/\;/,$db{quotes_min}{$id}{$col_name})) {
	$counter++ ; 
	$saved_field{"excl"}{$sec}{$counter} = $col_input ;  
}		

if ($saved_field{"excl"}{$sec}{$cnt}) { $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_terms_fields_terms_and_conditions {
	
	my ($sec,$cnt,$id) = @_ ;

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

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

	if ($db{$table}{$id}{$inpfield}) { $selected{$db{$table}{$id}{$inpfield}} = 'SELECTED';}

	$note_display_style = '' ; 	
	unless ($cnt==1 or $db{$table}{$id}{$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 = '' ; }
	} else { $cntnoteloop++ ; }

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

	# $add_form_fields .= qq~<div class='row'>~ ;
	# if ($checkit{$exfield} == 1) { $checked = 'CHECKED'; } elsif (($excl_tick{$cnt}) and ($iaction eq 'add')) { $checked = 'CHECKED'; } else { $checked = ''; }
	$sec_2 = "_" . $sec  ;
	$col_name = "excl" . "$sec_2" ;
	$counter = 0 ; 
	# &db_min_ro('quotes_min', "id, excl$sec", "id = $id", '', '') ; 

	foreach my $col_input (split(";", $db{quotes_min}{$id}{$col_name})){
		$counter++ ; 
		$saved_field{"excl"}{$sec_2}{$counter} = $col_input ;  
	}		

	if ($saved_field{"excl"}{$sec_2}{$cnt}) { $checked = 'CHECKED'; } else { $checked = ''; }
	
	# 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
	$opts{$inpfield}  = "" ; 
	for (1 .. 5){ 
		$quote_notes_field = "$sec\_$_" ;		
		next unless $db{quote_notes}{1}{$quote_notes_field} ;
		$opts{$inpfield} .= qq(<option value="$_" $selected{$_}>$db{quote_notes}{1}{$quote_notes_field}</option>) ; 
	}
	$allow_deselect{$inpfield} = 1; 
	$add_form_fields .= &common_min_form_select_col($inpfield,$db{$table}{$id}{$inpfield},'') ;
	$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 . '|' ;
	
	if ($ret) { return ; }

	$lcol = 2 ;
	
	$add_form_fields = '' ;
	
	$fcol = 9 ;
	$textarea_height{notes} = 80 ;
	$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>~ ;
	$textarea_height{office_notes} = 80 ;
	$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' style="height:76px;">&nbsp;</div></div>~ ;
	
	&tab_content_box($sec_name,$sec_col);

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

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

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

	$lcol = 2 ;
	
	$add_form_fields = '' ;
	
	$fcol = 9 ;
	# $add_form_fields .= qq~<div class='row'><div class='col-md-12'>&nbsp;</div></div>~ ;
	$add_form_fields .= &common_min_form_input('po_nr',$db{$table}{$id}{po_nr},'') ;
	$add_form_fields .= &common_min_form_textarea('invoice_notes',$db{$table}{$id}{invoice_notes},'') ;
	$add_form_fields .= qq~<div class='row'><div class='col-md-12' style="height:9px;">&nbsp;</div></div>~ ;
	
	&tab_content_box($sec_name,$sec_col);

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

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

sub tab_curr_symbols {

$curr_symb{ZAR} = 'R';
$curr_symb{USD} = '$';
# $curr_symb{GBP} = '£';
$curr_symb{GBP} = 'GBP';
# $curr_symb{EUR} = '€';
$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 = '' ; if ($no_vat{$sec}) { $style_vat = 'style="display:none;"' ; }

$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>~ ;
$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&nbsp;[USD]</label></div>~ ;
# $add_form_fields .= qq~<div class='col-md-1'><label class='control-label' id='lab_curr_amnt$sec'>Price&nbsp;[ZAR]</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_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~<div class='row'><div class="box col-md-12"><div class="box-inner"><div class="box-content">~ ;                     
$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' ;
$cntxt{4}  = 'four' ;
$cntxt{5}  = 'five' ;
$cntxt{6}  = 'six' ;
$cntxt{7}  = 'seven' ;
$cntxt{8}  = 'eight' ;
# $cntxt{9}  = 'nine' ;
	
} #----------------------------------------------------------------------------------------

sub tab_names {

my ($tab) = @_ ;

my $tabname = '' ;

   if ($tab==1)    {  $tabname = 'Quote Details'             ; }
elsif ($tab==2)    {  $tabname = 'Costing'                   ; }
elsif ($tab==3)    {  $tabname = 'Costing Other'			 ; }
elsif ($tab==4)    {  $tabname = 'Purchase Summary'          ; }
elsif ($tab==5)    {  $tabname = 'Notes'                     ; }
elsif ($tab==6)    {  $tabname = 'Camera Details'            ; }
elsif ($tab==7)    {  $tabname = 'Invoice Details'           ; }
# elsif ($tab==7)    {  $tabname = 'Extended Warranty'         ; }
# elsif ($tab==8)    {  $tabname = 'Rental'                    ; }
elsif ($tab==8)    {  $tabname = 'Document Uploads'          ; }

return ($tabname) ;

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

sub tab_hash {

# &tabs_get_default_totals ;

# for (1 .. 8) {
	# my $tname = &tab_names($_) ;
	
	# our $ret_pdf_link_sec = '' ; # _sa_out+16|_cl_del+13|_sa_in+17|_des_sa+13|
	
	# &tab_build_qt_left('',$_,1) ;
	# &tab_build_qt_right('',$_,1) ;
	
	# $tabcnt{$tname} = $_    ;
	# $tabsec{$tname} = $ret_pdf_link_sec    ;
	# }

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

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 = 'Camera System Costing' ;  
	for (1 .. 20) { $vat_tick{$_} = 1 ; }	
	}

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

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

1;
