<?php
/**
 * MachForm
 * 
 * © 2007–2025 Appnitro Software. All rights reserved.
 * 
 * This source code is proprietary and may not be copied, modified,
 * or distributed in any form without express written permission from Appnitro Software.
 * 
 * License information: https://www.machform.com/license-agreement/
 */
	
	//generate color picker boxes
	function get_color_picker_markup(){
		$color_picker_markup =<<<EOT
												<ul class="et_color_picker">
													<li id="li_transparent"></li>
													<li style="background-color: #000000"></li>
													<li style="background-color: #262626"></li>
													<li style="background-color: #404040"></li>
													<li style="background-color: #737373"></li>
													<li style="background-color: #A3A3A3"></li>
													<li style="background-color: #D4D4D4"></li>
													<li style="background-color: #E5E5E5"></li>
													<li style="background-color: #FFFFFF"></li>
													<li style="background-color: #FEF2F2"></li>
													<li style="background-color: #FECACA"></li>
													<li style="background-color: #FCA5A5"></li>
													<li style="background-color: #F87171"></li>
													<li style="background-color: #EF4444"></li>
													<li style="background-color: #DC2626"></li>
													<li style="background-color: #B91C1C"></li>
													<li style="background-color: #7F1D1D"></li>
													<li style="background-color: #FFF7ED"></li>
													<li style="background-color: #FED7AA"></li>
													<li style="background-color: #FDBA74"></li>
													<li style="background-color: #FB923C"></li>
													<li style="background-color: #F97316"></li>
													<li style="background-color: #EA580C"></li>
													<li style="background-color: #C2410C"></li>
													<li style="background-color: #7C2D12"></li>
													<li style="background-color: #FFFBEB"></li>
													<li style="background-color: #FDE68A"></li>
													<li style="background-color: #FCD34D"></li>
													<li style="background-color: #FBBF24"></li>
													<li style="background-color: #F59E0B"></li>
													<li style="background-color: #D97706"></li>
													<li style="background-color: #B45309"></li>
													<li style="background-color: #78350F"></li>
													<li style="background-color: #FEFCE8"></li>
													<li style="background-color: #FEF08A"></li>
													<li style="background-color: #FDE047"></li>
													<li style="background-color: #FACC15"></li>
													<li style="background-color: #EAB308"></li>
													<li style="background-color: #CA8A04"></li>
													<li style="background-color: #A16207"></li>
													<li style="background-color: #713F12"></li>
													<li style="background-color: #F0FDF4"></li>
													<li style="background-color: #BBF7D0"></li>
													<li style="background-color: #86EFAC"></li>
													<li style="background-color: #4ADE80"></li>
													<li style="background-color: #22C55E"></li>
													<li style="background-color: #16A34A"></li>
													<li style="background-color: #15803D"></li>
													<li style="background-color: #14532D"></li>
													<li style="background-color: #F0FDFA"></li>
													<li style="background-color: #99F6E4"></li>
													<li style="background-color: #5EEAD4"></li>
													<li style="background-color: #2DD4BF"></li>
													<li style="background-color: #14B8A6"></li>
													<li style="background-color: #0D9488"></li>
													<li style="background-color: #0F766E"></li>
													<li style="background-color: #134E4A"></li>
													<li style="background-color: #ECFEFF"></li>
													<li style="background-color: #A5F3FC"></li>
													<li style="background-color: #67E8F9"></li>
													<li style="background-color: #22D3EE"></li>
													<li style="background-color: #06B6D4"></li>
													<li style="background-color: #0891B2"></li>
													<li style="background-color: #0E7490"></li>
													<li style="background-color: #155E75"></li>
													<li style="background-color: #EFF6FF"></li>
													<li style="background-color: #BFDBFE"></li>
													<li style="background-color: #93C5FD"></li>
													<li style="background-color: #60A5FA"></li>
													<li style="background-color: #3B82F6"></li>
													<li style="background-color: #2563EB"></li>
													<li style="background-color: #1D4ED8"></li>
													<li style="background-color: #1E3A8A"></li>
													<li style="background-color: #EEF2FF"></li>
													<li style="background-color: #C7D2FE"></li>
													<li style="background-color: #A5B4FC"></li>
													<li style="background-color: #818CF8"></li>
													<li style="background-color: #6366F1"></li>
													<li style="background-color: #4F46E5"></li>
													<li style="background-color: #4338CA"></li>
													<li style="background-color: #312E81"></li>
													<li style="background-color: #F5F3FF"></li>
													<li style="background-color: #DDD6FE"></li>
													<li style="background-color: #C4B5FD"></li>
													<li style="background-color: #A78BFA"></li>
													<li style="background-color: #8B5CF6"></li>
													<li style="background-color: #7C3AED"></li>
													<li style="background-color: #6D28D9"></li>
													<li style="background-color: #4C1D95"></li>
													<li style="background-color: #FDF4FF"></li>
													<li style="background-color: #F5D0FE"></li>
													<li style="background-color: #F0ABFC"></li>
													<li style="background-color: #E879F9"></li>
													<li style="background-color: #D946EF"></li>
													<li style="background-color: #C026D3"></li>
													<li style="background-color: #A21CAF"></li>
													<li style="background-color: #701A75"></li>
													<li style="background-color: #FFF1F2"></li>
													<li style="background-color: #FECDD3"></li>
													<li style="background-color: #FDA4AF"></li>
													<li style="background-color: #FB7185"></li>
													<li style="background-color: #F43F5E"></li>
													<li style="background-color: #E11D48"></li>
													<li style="background-color: #BE123C"></li>
													<li style="background-color: #881337"></li>
												</ul>
EOT;
	
		return $color_picker_markup;
	}
	
	//generate pattern picker boxes
	function get_pattern_picker_markup(){
		$pattern_picker_markup =<<<EOT
												<ul class="et_pattern_picker">
													<li data-pattern="grey-mild.png" style="background-image: url('images/form_resources/grey-mild.png');"></li>
													<li data-pattern="45degreee_fabric.png" style="background-image: url('images/form_resources/45degreee_fabric.png');"></li>
													<li data-pattern="batthern.gif" style="background-image: url('images/form_resources/batthern.gif');"></li>
													<li data-pattern="bgnoise_lg.jpg" style="background-image: url('images/form_resources/bgnoise_lg.jpg');"></li>
													<li data-pattern="black_linen_v2.jpg" style="background-image: url('images/form_resources/black_linen_v2.jpg');"></li>
													<li data-pattern="blu_stripes.png" style="background-image: url('images/form_resources/blu_stripes.png');"></li>
													<li data-pattern="brushed_alu_dark.jpg" style="background-image: url('images/form_resources/brushed_alu_dark.jpg');"></li>
													<li data-pattern="cloth_alike.gif" style="background-image: url('images/form_resources/cloth_alike.gif');"></li>
													<li data-pattern="darkdenim3.png" style="background-image: url('images/form_resources/darkdenim3.png');"></li>
													<li data-pattern="diagonal-noise.png" style="background-image: url('images/form_resources/diagonal-noise.png');"></li>
													<li data-pattern="egg_shell.png" style="background-image: url('images/form_resources/egg_shell.png');"></li>
													<li data-pattern="embossed_paper.png" style="background-image: url('images/form_resources/embossed_paper.png');"></li>
													<li data-pattern="escheresque.png" style="background-image: url('images/form_resources/escheresque.png');"></li>
													<li data-pattern="graphy.png" style="background-image: url('images/form_resources/graphy.png');"></li>
													<li data-pattern="grid.png" style="background-image: url('images/form_resources/grid.png');"></li>
													<li data-pattern="gridme.png" style="background-image: url('images/form_resources/gridme.png');"></li>
													<li data-pattern="knitting250px.png" style="background-image: url('images/form_resources/knitting250px.png');"></li>
													<li data-pattern="lil_fiber.png" style="background-image: url('images/form_resources/lil_fiber.png');"></li>
													<li data-pattern="linedpaper.png" style="background-image: url('images/form_resources/linedpaper.png');"></li>
													<li data-pattern="low_contrast_linen.png" style="background-image: url('images/form_resources/low_contrast_linen.png');"></li>
													<li data-pattern="moulin.png" style="background-image: url('images/form_resources/moulin.png');"></li>
													<li data-pattern="natural_paper.png" style="background-image: url('images/form_resources/natural_paper.png');"></li>
													<li data-pattern="noisy_grid.gif" style="background-image: url('images/form_resources/noisy_grid.gif');"></li>
													<li data-pattern="grey_wash_wall.png" style="background-image: url('images/form_resources/grey_wash_wall.png');"></li>
													<li data-pattern="p4.png" style="background-image: url('images/form_resources/p4.png');"></li>
													<li data-pattern="p5.png" style="background-image: url('images/form_resources/p5.png');"></li>
													<li data-pattern="p6.png" style="background-image: url('images/form_resources/p6.png');"></li>
													<li data-pattern="ps_neutral.png" style="background-image: url('images/form_resources/ps_neutral.png');"></li>
													<li data-pattern="sneaker_mesh_fabric.png" style="background-image: url('images/form_resources/sneaker_mesh_fabric.png');"></li>
													<li data-pattern="subtle_white_feathers.png" style="background-image: url('images/form_resources/subtle_white_feathers.png');"></li>
													<li data-pattern="tex2res3.png" style="background-image: url('images/form_resources/tex2res3.png');"></li>
													<li data-pattern="tex2res4.png" style="background-image: url('images/form_resources/tex2res4.png');"></li>
													<li data-pattern="tex2res5.png" style="background-image: url('images/form_resources/tex2res5.png');"></li>
													<li data-pattern="textured_paper.png" style="background-image: url('images/form_resources/textured_paper.png');"></li>
													<li data-pattern="wavegrid.png" style="background-image: url('images/form_resources/wavegrid.png');"></li>
													<li data-pattern="weave.png" style="background-image: url('images/form_resources/weave.png');"></li>
													<li data-pattern="white_brick_wall.png" style="background-image: url('images/form_resources/white_brick_wall.png');"></li>
													<li data-pattern="pattern_022.gif" style="background-image: url('images/form_resources/pattern_022.gif');"></li>
													<li data-pattern="pattern_036.gif" style="background-image: url('images/form_resources/pattern_036.gif');"></li>
													<li data-pattern="pattern_038.gif" style="background-image: url('images/form_resources/pattern_038.gif');"></li>
													<li data-pattern="pattern_044.gif" style="background-image: url('images/form_resources/pattern_044.gif');"></li>
												</ul>
EOT;
	
		return $pattern_picker_markup;
	}
	
	//generate font picker boxes with all Google Fonts from database
	function get_font_picker_markup($dbh){

		$max_fonts_displayed = 100;
		$font_picker_markup = '';
		$font_styles_data = array();
		$initial_fonts_css = array();

		// Search input
		$font_picker_markup .= '<div class="font_search_container">';
		$font_picker_markup .= '<input type="text" class="font_search_input" placeholder="Search fonts..." />';
		$font_picker_markup .= '</div>';

		$font_picker_markup .= '<ul class="et_font_picker">';

		// System fonts
		$system_fonts = array(
			array('name' => 'InterVariable', 'display' => 'Default', 'style' => "'InterVariable',sans-serif"),
			array('name' => 'Arial', 'display' => 'Arial', 'style' => "Arial,sans-serif"),
			array('name' => 'Trebuchet MS', 'display' => 'Trebuchet MS', 'style' => "'Trebuchet MS', Helvetica, sans-serif"),
			array('name' => 'Verdana', 'display' => 'Verdana', 'style' => "Verdana, sans-serif"),
			array('name' => 'Tahoma', 'display' => 'Tahoma', 'style' => "Tahoma, Geneva, sans-serif"),
			array('name' => 'Courier New', 'display' => 'Courier New', 'style' => "'Courier New', Courier, monospace"),
			array('name' => 'Palatino Linotype', 'display' => 'Palatino Linotype', 'style' => "'Palatino Linotype', 'Book Antiqua', Palatino, serif"),
			array('name' => 'Times New Roman', 'display' => 'Times New Roman', 'style' => "'Times New Roman', serif"),
			array('name' => 'Georgia', 'display' => 'Georgia', 'style' => "Georgia, serif"),
			array('name' => 'Comic Sans MS', 'display' => 'Comic Sans MS', 'style' => "'Comic Sans MS', cursive"),
			array('name' => 'Arial Black', 'display' => 'Arial Black', 'style' => "'Arial Black', Gadget, sans-serif")
		);

		foreach($system_fonts as $font){
			$font_picker_markup .= '<li class="font_system">';
			$font_picker_markup .= '<div class="font_picker_preview" style="font-family: '.$font['style'].'">'.$font['display'].'</div>';
			$font_picker_markup .= '<div class="font_picker_meta">';
			$font_picker_markup .= '<div class="font_name">'.$font['name'].'</div>';
			$font_picker_markup .= '</div>';
			$font_picker_markup .= '</li>';
		}

		// Google Fonts from database
		$query = "SELECT 
						font_id, 
						font_family, 
						font_variants, 
						font_variants_numeric
				  FROM 
				  		".MF_TABLE_PREFIX."fonts
				  ORDER BY font_id ASC";
		$params = array();
		$sth = mf_do_query($query, $params, $dbh);

		$font_count = 0;
		while($row = mf_do_fetch_result($sth)){
			$font_id 		= $row['font_id'];
			$font_family 	= htmlspecialchars($row['font_family'], ENT_QUOTES, 'UTF-8');
			$font_variants 	= $row['font_variants'];

			//display only some fonts initially to improve performance
			$visible_class = ($font_count < $max_fonts_displayed) ? ' font_visible' : '';

			if($font_count < $max_fonts_displayed){
				$initial_fonts_css[] = urlencode($row['font_family']).":".$row['font_variants'];
			}

			$font_picker_markup .= '<li data-fontid="'.$font_id.'" data-loaded="'.($font_count < $max_fonts_displayed ? 'true' : 'false').'" data-variants="'.$font_variants.'" class="font_google'.$visible_class.'">';
			$font_picker_markup .= '<div class="font_picker_preview" style="font-family: '.($font_count < $max_fonts_displayed ? "'".$row['font_family']."', " : '').'Arial;">'.$font_family.'</div>';
			$font_picker_markup .= '<div class="font_picker_meta">';
			$font_picker_markup .= '<div class="font_name">'.$font_family.'</div>';
			$font_picker_markup .= '</div>';
			$font_picker_markup .= '</li>';

			// Build font styles data for JavaScript
			$font_variants_numeric = explode(',', $row['font_variants_numeric']);
			$font_variants_pair = array();

			foreach($font_variants_numeric as $font_variant_raw){
				$font_variant_parts = explode('-', $font_variant_raw);
				$secondary_style = (($font_variant_parts[1] ?? '') == 'italic') ? ' Italic' : '';

				switch($font_variant_parts[0]){
					case '100': $primary_style = 'Ultra-Light'; break;
					case '200': $primary_style = 'Light'; break;
					case '300': $primary_style = 'Book'; break;
					case '400': $primary_style = 'Normal'; break;
					case '500': $primary_style = 'Medium'; break;
					case '600': $primary_style = 'Semi-Bold'; break;
					case '700': $primary_style = 'Bold'; break;
					case '800': $primary_style = 'Extra-Bold'; break;
					case '900': $primary_style = 'Ultra-Bold'; break;
					default: $primary_style = 'Normal'; break;
				}

				$font_variants_pair[$font_variant_raw] = $primary_style . $secondary_style;
			}

			$font_family_slug = strtolower(str_replace(' ', '', $row['font_family']));
			$font_styles_data[$font_family_slug] = $font_variants_pair;

			$font_count++;
		}

		$font_picker_markup .= '</ul>';

		return array(
			'markup' => $font_picker_markup,
			'font_styles_data' => $font_styles_data,
			'initial_fonts_css' => $initial_fonts_css
		);
	}

	//generate the CSS markup for the selected form theme
	function mf_theme_get_css_content($dbh,$theme_id){
		
		$css_content = "/** DO NOT MODIFY THIS FILE. All code here are generated by MachForm Theme Editor **/\n\n";
		$theme_properties = new stdClass();
		
		$mf_settings = mf_get_settings($dbh);

		
		$query = "SELECT
						theme_name,
						logo_type,
						ifnull(logo_custom_image,'') logo_custom_image,
						logo_custom_height,
						logo_default_image,
						wallpaper_bg_type,
						wallpaper_bg_color,
						wallpaper_bg_pattern,
						wallpaper_bg_custom,
						header_bg_type,
						header_bg_color,
						header_bg_pattern,
						header_bg_custom,
						form_bg_type,
						form_bg_color,
						form_bg_pattern,
						form_bg_custom,
						highlight_bg_type,
						highlight_bg_color,
						highlight_bg_pattern,
						highlight_bg_custom,
						guidelines_bg_type,
						guidelines_bg_color,
						guidelines_bg_pattern,
						guidelines_bg_custom,
						field_bg_type,
						field_bg_color,
						field_bg_pattern,
						field_bg_custom,
						form_title_font_type,
						form_title_font_weight,
						form_title_font_style,
						form_title_font_size,
						form_title_font_color,
						form_desc_font_type,
						form_desc_font_weight,
						form_desc_font_style,
						form_desc_font_size,
						form_desc_font_color,
						field_title_font_type,
						field_title_font_weight,
						field_title_font_style,
						field_title_font_size,
						field_title_font_color,
						guidelines_font_type,
						guidelines_font_weight,
						guidelines_font_style,
						guidelines_font_size,
						guidelines_font_color,
						section_title_font_type,
						section_title_font_weight,
						section_title_font_style,
						section_title_font_size,
						section_title_font_color,
						section_desc_font_type,
						section_desc_font_weight,
						section_desc_font_style,
						section_desc_font_size,
						section_desc_font_color,
						field_text_font_type,
						field_text_font_weight,
						field_text_font_style,
						field_text_font_size,
						field_text_font_color,
						border_form_width,
						border_form_style,
						border_form_color,
						border_guidelines_width,
						border_guidelines_style,
						border_guidelines_color,
						border_section_width,
						border_section_style,
						border_section_color,
						form_shadow_style,
						form_shadow_size,
						form_shadow_brightness,
						form_button_type,
						form_button_text,
						form_button_image,
						advanced_css
					FROM
						`".MF_TABLE_PREFIX."form_themes`
				   WHERE
				   		theme_id=? and `status`=1";
		$params = array($theme_id);
		
		$sth = mf_do_query($query,$params,$dbh);
		$row = mf_do_fetch_result($sth);
		
		$theme_properties->theme_id 		   = $theme_id;
		$theme_properties->theme_name  		   = $row['theme_name'];
		$theme_properties->logo_type 		   = $row['logo_type']; 
		$theme_properties->logo_custom_image   = $row['logo_custom_image'];
		$theme_properties->logo_custom_height  = (int) $row['logo_custom_height'];
		$theme_properties->logo_default_image  = $row['logo_default_image'];
		$theme_properties->wallpaper_bg_type 	= $row['wallpaper_bg_type'];
		$theme_properties->wallpaper_bg_color 	= $row['wallpaper_bg_color'];
		$theme_properties->wallpaper_bg_pattern = $row['wallpaper_bg_pattern'];
		$theme_properties->wallpaper_bg_custom 	= $row['wallpaper_bg_custom'];
		$theme_properties->header_bg_type 		= $row['header_bg_type'];
		$theme_properties->header_bg_color 		= $row['header_bg_color'];
		$theme_properties->header_bg_pattern 	= $row['header_bg_pattern'];;
		$theme_properties->header_bg_custom 	= $row['header_bg_custom'];
		$theme_properties->form_bg_type 		= $row['form_bg_type'];
		$theme_properties->form_bg_color 		= $row['form_bg_color'];
		$theme_properties->form_bg_pattern 		= $row['form_bg_pattern'];;
		$theme_properties->form_bg_custom 		= $row['form_bg_custom'];
		$theme_properties->highlight_bg_type 	= $row['highlight_bg_type'];
		$theme_properties->highlight_bg_color 	= $row['highlight_bg_color'];
		$theme_properties->highlight_bg_pattern = $row['highlight_bg_pattern'];
		$theme_properties->highlight_bg_custom 	= $row['highlight_bg_custom'];
		$theme_properties->guidelines_bg_type 	= $row['guidelines_bg_type'];
		$theme_properties->guidelines_bg_color 	= $row['guidelines_bg_color'];
		$theme_properties->guidelines_bg_pattern = $row['guidelines_bg_pattern'];
		$theme_properties->guidelines_bg_custom  = $row['guidelines_bg_custom'];
		$theme_properties->field_bg_type 		 = $row['field_bg_type'];
		$theme_properties->field_bg_color 		 = $row['field_bg_color'];
		$theme_properties->field_bg_pattern 	 = $row['field_bg_pattern'];
		$theme_properties->field_bg_custom  	 = $row['field_bg_custom'];
		$theme_properties->form_title_font_type    = $row['form_title_font_type'];
		$theme_properties->form_title_font_weight  = (int) $row['form_title_font_weight'];
		$theme_properties->form_title_font_style   = $row['form_title_font_style'];
		$theme_properties->form_title_font_size    = $row['form_title_font_size'];
		$theme_properties->form_title_font_color   = $row['form_title_font_color'];
		$theme_properties->form_desc_font_type    = $row['form_desc_font_type'];
		$theme_properties->form_desc_font_weight  = (int) $row['form_desc_font_weight'];
		$theme_properties->form_desc_font_style   = $row['form_desc_font_style'];
		$theme_properties->form_desc_font_size    = $row['form_desc_font_size'];
		$theme_properties->form_desc_font_color   = $row['form_desc_font_color'];
		$theme_properties->field_title_font_type    = $row['field_title_font_type'];
		$theme_properties->field_title_font_weight  = (int) $row['field_title_font_weight'];
		$theme_properties->field_title_font_style   = $row['field_title_font_style'];
		$theme_properties->field_title_font_size    = $row['field_title_font_size'];
		$theme_properties->field_title_font_color   = $row['field_title_font_color'];
		$theme_properties->guidelines_font_type    = $row['guidelines_font_type'];
		$theme_properties->guidelines_font_weight  = (int) $row['guidelines_font_weight'];
		$theme_properties->guidelines_font_style   = $row['guidelines_font_style'];
		$theme_properties->guidelines_font_size    = $row['guidelines_font_size'];
		$theme_properties->guidelines_font_color   = $row['guidelines_font_color'];
		$theme_properties->section_title_font_type    = $row['section_title_font_type'];
		$theme_properties->section_title_font_weight  = (int) $row['section_title_font_weight'];
		$theme_properties->section_title_font_style   = $row['section_title_font_style'];
		$theme_properties->section_title_font_size    = $row['section_title_font_size'];
		$theme_properties->section_title_font_color   = $row['section_title_font_color'];
		$theme_properties->section_desc_font_type    = $row['section_desc_font_type'];
		$theme_properties->section_desc_font_weight  = (int) $row['section_desc_font_weight'];
		$theme_properties->section_desc_font_style   = $row['section_desc_font_style'];
		$theme_properties->section_desc_font_size    = $row['section_desc_font_size'];
		$theme_properties->section_desc_font_color   = $row['section_desc_font_color'];
		$theme_properties->field_text_font_type    = $row['field_text_font_type'];
		$theme_properties->field_text_font_weight  = (int) $row['field_text_font_weight'];
		$theme_properties->field_text_font_style   = $row['field_text_font_style'];
		$theme_properties->field_text_font_size    = $row['field_text_font_size'];
		$theme_properties->field_text_font_color   = $row['field_text_font_color'];
		$theme_properties->border_form_width   = (int) $row['border_form_width'];
		$theme_properties->border_form_style   = $row['border_form_style'];
		$theme_properties->border_form_color   = $row['border_form_color'];
		$theme_properties->border_guidelines_width   = (int) $row['border_guidelines_width'];
		$theme_properties->border_guidelines_style   = $row['border_guidelines_style'];
		$theme_properties->border_guidelines_color   = $row['border_guidelines_color'];
		$theme_properties->border_section_width   = (int) $row['border_section_width'];
		$theme_properties->border_section_style   = $row['border_section_style'];
		$theme_properties->border_section_color   = $row['border_section_color'];
		$theme_properties->form_shadow_style	  = $row['form_shadow_style'];
		$theme_properties->form_shadow_size	  	  = $row['form_shadow_size'];
		$theme_properties->form_shadow_brightness = $row['form_shadow_brightness'];
		$theme_properties->form_button_type	  	  = $row['form_button_type'];
		$theme_properties->form_button_text	  	  = $row['form_button_text'];
		$theme_properties->form_button_image	  = $row['form_button_image'];
		$theme_properties->advanced_css	  		  = $row['advanced_css'];
		
		/** Form Logo **/
		$form_logo_style  = "#main_body h1 a";
		$form_logo_style .= "\n"."{"."\n";
		
		$form_logo_height = 40;
		
		if($theme_properties->logo_type == 'disabled'){ //logo disabled
			$form_logo_style .= "background-image: none;"."\n";
		}else if($theme_properties->logo_type == 'default'){//default logo
			$form_logo_style .= "background-image: url('{$mf_settings['base_url']}images/form_resources/{$theme_properties->logo_default_image}');"."\n";
			$form_logo_style .= "background-repeat: no-repeat;"."\n";
		}else if($theme_properties->logo_type == 'custom'){//custom logo
			$form_logo_style .= "background-image: url('{$theme_properties->logo_custom_image}');"."\n";
			$form_logo_height  = $theme_properties->logo_custom_height;
		}
		
		$form_logo_style .= "height: {$form_logo_height}px;"."\n";
		$form_logo_style .= "}"."\n\n";
		
		$css_content .= $form_logo_style;
		
		/** Wallpaper **/
		$form_wallpaper_style = "html";
		$form_wallpaper_style .= "\n"."{"."\n";
		
		if($theme_properties->wallpaper_bg_type == 'color'){
			$form_wallpaper_style .= "background-color: {$theme_properties->wallpaper_bg_color};"."\n";
			$form_wallpaper_style .= "background-image: none;"."\n";
		}else if($theme_properties->wallpaper_bg_type == 'pattern'){
			$form_wallpaper_style .= "background-image: url('{$mf_settings['base_url']}images/form_resources/{$theme_properties->wallpaper_bg_pattern}');"."\n";
			$form_wallpaper_style .= "background-repeat: repeat;"."\n";
		}else if($theme_properties->wallpaper_bg_type == 'custom'){
			$form_wallpaper_style .= "background-image: url('{$theme_properties->wallpaper_bg_custom}');"."\n";
			$form_wallpaper_style .= "background-repeat: repeat;"."\n";
		}
		
		$form_wallpaper_style .= "}"."\n\n";
		$css_content .= $form_wallpaper_style;
		
		/** Form Header **/
		$form_header_style = "#main_body h1";
		$form_header_style .= "\n"."{"."\n";
		
		if($theme_properties->header_bg_type == 'color'){
			$form_header_style .= "background-color: {$theme_properties->header_bg_color};"."\n";
			$form_header_style .= "background-image: none;"."\n";
		}else if($theme_properties->header_bg_type == 'pattern'){
			$form_header_style .= "background-image: url('{$mf_settings['base_url']}images/form_resources/{$theme_properties->header_bg_pattern}');"."\n";
			$form_header_style .= "background-repeat: repeat;"."\n";
		}else if($theme_properties->header_bg_type == 'custom'){
			$form_header_style .= "background-image: url('{$theme_properties->header_bg_custom}');"."\n";
			$form_header_style .= "background-repeat: repeat;"."\n";
		}
		
		$form_header_style .= "}"."\n\n";
		$css_content .= $form_header_style;
		
		/** Form Background **/
		$form_container_style = "#form_container";
		$form_container_style .= "\n"."{"."\n";
		
		if($theme_properties->form_bg_type == 'color'){
			$form_container_style .= "background-color: {$theme_properties->form_bg_color};"."\n";
		}else if($theme_properties->form_bg_type == 'pattern'){
			$form_container_style .= "background-image: url('{$mf_settings['base_url']}images/form_resources/{$theme_properties->form_bg_pattern}');"."\n";
			$form_container_style .= "background-repeat: repeat;"."\n";
		}else if($theme_properties->form_bg_type == 'custom'){
			$form_container_style .= "background-image: url('{$theme_properties->form_bg_custom}');"."\n";
			$form_container_style .= "background-repeat: repeat;"."\n";
		}
		
		/** Form Border **/
		if(!empty($theme_properties->border_form_width)){
			if($theme_properties->border_form_width == 1){
				if(strtolower($theme_properties->border_form_color) == '#cccccc'){
					$form_container_style .= "border-width: 0px;"."\n";
				}else{
					$form_container_style .= "border-width: 1px;"."\n";
				}
				
			}else{
				$form_container_style .= "border-width: {$theme_properties->border_form_width}px;"."\n";
			}
		}else{
			$form_container_style .= "border-width: 0px;"."\n";
			$form_container_style .= "box-shadow: none;"."\n";
		}
		
		if(!empty($theme_properties->border_form_style)){
			$form_container_style .= "border-style: {$theme_properties->border_form_style};"."\n";
		}
		
		if(!empty($theme_properties->border_form_color)){
			$form_container_style .= "border-color: {$theme_properties->border_form_color};"."\n";
		}
		
		$form_container_style .= "}"."\n\n";
		$css_content .= $form_container_style;
		
		/** Field Highlight **/
		$field_highlight_style = "#main_body form li.highlighted,#main_body .matrix tbody tr:hover td,#machform_review_table tr.alt";
		$field_highlight_style .= "\n"."{"."\n";
		
		if($theme_properties->highlight_bg_type == 'color'){
			$field_highlight_style .= "background-color: {$theme_properties->highlight_bg_color};"."\n";
		}else if($theme_properties->highlight_bg_type == 'pattern'){
			$field_highlight_style .= "background-image: url('{$mf_settings['base_url']}images/form_resources/{$theme_properties->highlight_bg_pattern}');"."\n";
			$field_highlight_style .= "background-repeat: repeat;"."\n";
		}else if($theme_properties->highlight_bg_type == 'custom'){
			$field_highlight_style .= "background-image: url('{$theme_properties->highlight_bg_custom}');"."\n";
			$field_highlight_style .= "background-repeat: repeat;"."\n";
		}
		
		$field_highlight_style .= "}"."\n\n";
		$css_content .= $field_highlight_style;
		
		/** Field Guidelines **/
		$field_guidelines_style = "#main_body form .guidelines";
		$field_guidelines_style .= "\n"."{"."\n";
		
		if($theme_properties->guidelines_bg_type == 'color'){
			$field_guidelines_style .= "background-color: {$theme_properties->guidelines_bg_color};"."\n";
		}else if($theme_properties->guidelines_bg_type == 'pattern'){
			$field_guidelines_style .= "background-image: url('{$mf_settings['base_url']}images/form_resources/{$theme_properties->guidelines_bg_pattern}');"."\n";
			$field_guidelines_style .= "background-repeat: repeat;"."\n";
		}else if($theme_properties->guidelines_bg_type == 'custom'){
			$field_guidelines_style .= "background-image: url('{$theme_properties->guidelines_bg_custom}');"."\n";
			$field_guidelines_style .= "background-repeat: repeat;"."\n";
		}
		
		//guidelines border
		if(!empty($theme_properties->border_guidelines_width)){
			$field_guidelines_style .= "border-width: {$theme_properties->border_guidelines_width}px;"."\n";
		}else{
			$field_guidelines_style .= "border-width: 0px;"."\n";
		}
		
		if(!empty($theme_properties->border_guidelines_style)){
			$field_guidelines_style .= "border-style: {$theme_properties->border_guidelines_style};"."\n";
		}
		
		if(!empty($theme_properties->border_guidelines_color)){
			$field_guidelines_style .= "border-color: {$theme_properties->border_guidelines_color};"."\n";
		}
		
		$field_guidelines_style .= "}"."\n\n";
		$css_content .= $field_guidelines_style;
		
		//guidelines font
		$field_guidelines_text_style = "#main_body form .guidelines small";
		$field_guidelines_text_style .= "\n"."{"."\n";
		
		if(!empty($theme_properties->guidelines_font_type)){
			$field_guidelines_text_style .= "font-family: '{$theme_properties->guidelines_font_type}','InterVariable',Tahoma,Arial,sans-serif;"."\n";
		}
		
		if(!empty($theme_properties->guidelines_font_weight)){
			$field_guidelines_text_style .= "font-weight: {$theme_properties->guidelines_font_weight};"."\n";
		}
		
		if(!empty($theme_properties->guidelines_font_style)){
			$field_guidelines_text_style .= "font-style: {$theme_properties->guidelines_font_style};"."\n";
		}
		
		if(!empty($theme_properties->guidelines_font_size)){
			$field_guidelines_text_style .= "font-size: {$theme_properties->guidelines_font_size};"."\n";
		}
		
		if(!empty($theme_properties->guidelines_font_color)){
			$field_guidelines_text_style .= "color: {$theme_properties->guidelines_font_color};"."\n";
		}
		
		$field_guidelines_text_style .= "}"."\n\n";
		$css_content .= $field_guidelines_text_style;
		
		
		/** Field Box **/
		$field_box_style = "#main_body input.text,#main_body input.file,#main_body textarea.textarea,#main_body select.select,#main_body input.checkbox,#main_body input.radio";
		$field_box_style .= "\n"."{"."\n";
		
		if($theme_properties->field_bg_type == 'color'){
			$field_box_style .= "background-color: {$theme_properties->field_bg_color};"."\n";
		}else if($theme_properties->field_bg_type == 'pattern'){
			$field_box_style .= "background-image: url('{$mf_settings['base_url']}images/form_resources/{$theme_properties->field_bg_pattern}');"."\n";
			$field_box_style .= "background-repeat: repeat;";
		}else if($theme_properties->field_bg_type == 'custom'){
			$field_box_style .= "background-image: url('{$theme_properties->field_bg_custom}');"."\n";
			$field_box_style .= "background-repeat: repeat;"."\n";
		}
		
		//field text values
		$font_family_array = '';
		if(!empty($theme_properties->field_text_font_type)){
			$field_box_style .= "font-family: '{$theme_properties->field_text_font_type}','InterVariable',Tahoma,Arial,sans-serif;"."\n";
			$font_family_array .= $theme_properties->field_text_font_type;
		}
		
		if(!empty($theme_properties->field_text_font_weight)){
			$field_box_style .= "font-weight: {$theme_properties->field_text_font_weight};"."\n";
		}
		
		if(!empty($theme_properties->field_text_font_style)){
			$field_box_style .= "font-style: {$theme_properties->field_text_font_style};"."\n";
		}
		
		if(!empty($theme_properties->field_text_font_size)){
			$field_box_style .= "font-size: {$theme_properties->field_text_font_size};"."\n";
		}
		
		if(!empty($theme_properties->field_text_font_color)){
			$field_box_style .= "color: {$theme_properties->field_text_font_color};"."\n";
		}
		
		$field_box_style .= "}"."\n\n";
		$css_content .= $field_box_style;

		/** Review Table, value section (right column) **/
		//this is similar as field box above, except without background
		$review_table_value_style = "#machform_review_table td.mf_review_value";
		$review_table_value_style .= "\n"."{"."\n";

		if(!empty($theme_properties->field_text_font_type)){
			$review_table_value_style .= "font-family: '{$theme_properties->field_text_font_type}','InterVariable',Tahoma,Arial,sans-serif;"."\n";
		}
		
		if(!empty($theme_properties->field_text_font_weight)){
			$review_table_value_style .= "font-weight: {$theme_properties->field_text_font_weight};"."\n";
		}
		
		if(!empty($theme_properties->field_text_font_style)){
			$review_table_value_style .= "font-style: {$theme_properties->field_text_font_style};"."\n";
		}
		
		if(!empty($theme_properties->field_text_font_size)){
			$review_table_value_style .= "font-size: {$theme_properties->field_text_font_size};"."\n";
		}
		
		//on review page, special for the value color should be the same as label color
		if(!empty($theme_properties->field_title_font_color)){
			$review_table_value_style .= "color: {$theme_properties->field_title_font_color};"."\n";
		}

		$review_table_value_style .= "}"."\n\n";
		$css_content .= $review_table_value_style;
		
		/** Form Title **/
		$form_title_style = "#main_body .form_description h2,#main_body .form_success h2";
		$form_title_style .= "\n"."{"."\n";
		
		if(!empty($theme_properties->form_title_font_type)){
			$form_title_style .= "font-family: '{$theme_properties->form_title_font_type}','InterVariable',Tahoma,Arial,sans-serif;"."\n";
		}
		
		if(!empty($theme_properties->form_title_font_weight)){
			$form_title_style .= "font-weight: {$theme_properties->form_title_font_weight};"."\n";
		}
		
		if(!empty($theme_properties->form_title_font_style)){
			$form_title_style .= "font-style: {$theme_properties->form_title_font_style};"."\n";
		}
		
		if(!empty($theme_properties->form_title_font_size)){
			$form_title_style .= "font-size: {$theme_properties->form_title_font_size};"."\n";
		}
		
		if(!empty($theme_properties->form_title_font_color)){
			$form_title_style .= "color: {$theme_properties->form_title_font_color};"."\n";
		}
		
		$form_title_style .= "}"."\n\n";
		$css_content .= $form_title_style;
		
		/** Form Description **/
		$form_desc_style = "#main_body .form_description p,#main_body form ul.payment_list_items li";
		$form_desc_style .= "\n"."{"."\n";
		
		if(!empty($theme_properties->form_desc_font_type)){
			$form_desc_style .= "font-family: '{$theme_properties->form_desc_font_type}','InterVariable',Tahoma,Arial,sans-serif;"."\n";
		}
		
		if(!empty($theme_properties->form_desc_font_weight)){
			$form_desc_style .= "font-weight: {$theme_properties->form_desc_font_weight};"."\n";
		}
		
		if(!empty($theme_properties->form_desc_font_style)){
			$form_desc_style .= "font-style: {$theme_properties->form_desc_font_style};"."\n";
		}
		
		if(!empty($theme_properties->form_desc_font_size)){
			$form_desc_style .= "font-size: {$theme_properties->form_desc_font_size};"."\n";
		}
		
		if(!empty($theme_properties->form_desc_font_color)){
			$form_desc_style .= "color: {$theme_properties->form_desc_font_color};"."\n";
		}
		
		$form_desc_style .= "}"."\n\n";
		$css_content .= $form_desc_style;

		/** Pagination Text **/
		$pagination_desc_style = "#main_body form li span.ap_tp_text";
		$pagination_desc_style .= "\n"."{"."\n";

		if(!empty($theme_properties->form_desc_font_color)){
			$pagination_desc_style .= "color: {$theme_properties->form_desc_font_color};"."\n";
		}

		$pagination_desc_style .= "}"."\n\n";
		$css_content .= $pagination_desc_style;

		
		/** Field Title **/
		$field_title_style 	   = "#main_body form li label.description,#main_body form li span.description,#main_body .matrix caption,#main_body .matrix td.first_col,#main_body form li.total_payment span,#machform_review_table td.mf_review_label";
		$field_sub_title_style = "#main_body form li span label,#main_body label.choice,#main_body .matrix th,#main_body form li span.symbol,.mf_sigpad_clear,#main_body form li div label,#main_body form li div span.label";
		
		$field_title_style .= "\n"."{"."\n";
		$field_sub_title_style .= "\n"."{"."\n";
		
		if(!empty($theme_properties->field_title_font_type)){
			$field_title_style .= "font-family: '{$theme_properties->field_title_font_type}','InterVariable',Tahoma,Arial,sans-serif;"."\n";
			$field_sub_title_style .= "font-family: '{$theme_properties->field_title_font_type}','InterVariable',Tahoma,Arial,sans-serif;"."\n";
		}
		
		if(!empty($theme_properties->field_title_font_weight)){
			$field_title_style .= "font-weight: {$theme_properties->field_title_font_weight};"."\n";
		}
		
		if(!empty($theme_properties->field_title_font_style)){
			$field_title_style .= "font-style: {$theme_properties->field_title_font_style};"."\n";
		}
		
		if(!empty($theme_properties->field_title_font_size)){
			$field_title_style .= "font-size: {$theme_properties->field_title_font_size};"."\n";
		}
		
		if(!empty($theme_properties->field_title_font_color)){
			$field_title_style .= "color: {$theme_properties->field_title_font_color};"."\n";
			$field_sub_title_style .= "color: {$theme_properties->field_title_font_color};"."\n";
		}
		
		$field_title_style .= "}"."\n\n";
		$css_content .= $field_title_style;
		
		$field_sub_title_style .= "}"."\n\n";
		$css_content .= $field_sub_title_style;
		
		/** Section Title **/
		$section_title_style = "#main_body form .section_break h3,#main_body form .media h3,#machform_review_table td .mf_section_title";
		$section_title_style .= "\n"."{"."\n";
		
		if(!empty($theme_properties->section_title_font_type)){
			$section_title_style .= "font-family: '{$theme_properties->section_title_font_type}','InterVariable',Tahoma,Arial,sans-serif;"."\n";
		}
		
		if(!empty($theme_properties->section_title_font_weight)){
			$section_title_style .= "font-weight: {$theme_properties->section_title_font_weight};"."\n";
		}
		
		if(!empty($theme_properties->section_title_font_style)){
			$section_title_style .= "font-style: {$theme_properties->section_title_font_style};"."\n";
		}
		
		if(!empty($theme_properties->section_title_font_size)){
			$section_title_style .= "font-size: {$theme_properties->section_title_font_size};"."\n";
		}
		
		if(!empty($theme_properties->section_title_font_color)){
			$section_title_style .= "color: {$theme_properties->section_title_font_color};"."\n";
		}
		
		$section_title_style .= "}"."\n\n";
		$css_content .= $section_title_style;
		
		/** Section Description **/
		$section_desc_style = "#main_body form .section_break p,#main_body form .media p,#machform_review_table td .mf_section_content";
		$section_desc_style .= "\n"."{"."\n";
		
		if(!empty($theme_properties->section_desc_font_type)){
			$section_desc_style .= "font-family: '{$theme_properties->section_desc_font_type}','InterVariable',Tahoma,Arial,sans-serif;"."\n";
		}
		
		if(!empty($theme_properties->section_desc_font_weight)){
			$section_desc_style .= "font-weight: {$theme_properties->section_desc_font_weight};"."\n";
		}
		
		if(!empty($theme_properties->section_desc_font_style)){
			$section_desc_style .= "font-style: {$theme_properties->section_desc_font_style};"."\n";
		}
		
		if(!empty($theme_properties->section_desc_font_size)){
			$section_desc_style .= "font-size: {$theme_properties->section_desc_font_size};"."\n";
		}
		
		if(!empty($theme_properties->section_desc_font_color)){
			$section_desc_style .= "color: {$theme_properties->section_desc_font_color};"."\n";
		}
		
		$section_desc_style .= "}"."\n\n";
		$css_content .= $section_desc_style;
		
		/** Section Block **/
		$section_block_style = "#main_body form li.section_break";
		$section_block_style .= "\n"."{"."\n";
		
		if(!empty($theme_properties->border_section_width)){
			$section_block_style .= "border-top-width: {$theme_properties->border_section_width}px;"."\n";
		}else{
			$section_block_style .= "border-top-width: 0px;"."\n";
		}
		
		if(!empty($theme_properties->border_section_style)){
			$section_block_style .= "border-top-style: {$theme_properties->border_section_style};"."\n";
		}
		
		if(!empty($theme_properties->border_section_color)){
			$section_block_style .= "border-top-color: {$theme_properties->border_section_color};"."\n";
		}
		
		$section_block_style .= "}"."\n\n";
		$css_content .= $section_block_style;
		
		/** Advanced CSS Code **/
		if(!empty($theme_properties->advanced_css)){
			//if the first line of the advanced CSS code contain @import statement 
			//put the Advanced CSS code block at the very first line
			if(stripos($theme_properties->advanced_css,'@import') !== false){
				$css_content  = $theme_properties->advanced_css."\n\n".$css_content;
			}else{
				$css_content .= "\n\n".'/** Advanced CSS **/'."\n\n";
				$css_content .= $theme_properties->advanced_css;
			}
		}
		
		return $css_content;
		
	}
	
	//generate the links to the fonts
	function mf_theme_get_fonts_link($dbh,$theme_id){
		
		$font_family_array = array();
		
		$query = "SELECT
						form_title_font_type,
						form_desc_font_type,
						field_title_font_type,
						guidelines_font_type,
						section_title_font_type,
						section_desc_font_type,
						field_text_font_type
					FROM
						`".MF_TABLE_PREFIX."form_themes`
				   WHERE
				   		theme_id=? and `status`=1";
		$params = array($theme_id);
		
		$sth = mf_do_query($query,$params,$dbh);
		$row = mf_do_fetch_result($sth);
		
		$font_family_array[] = $row['form_title_font_type'];
		$font_family_array[] = $row['form_desc_font_type'];
		$font_family_array[] = $row['field_title_font_type'];
		$font_family_array[] = $row['guidelines_font_type'];
		$font_family_array[] = $row['section_title_font_type'];
		$font_family_array[] = $row['section_desc_font_type'];
		$font_family_array[] = $row['field_text_font_type'];
		
		/** Build the font CSS tag **/
		if(!empty($font_family_array)){
			$font_family_joined = implode("','",$font_family_array);
			
			$query = "SELECT font_family,font_variants FROM ".MF_TABLE_PREFIX."fonts WHERE font_family IN('{$font_family_joined}')";
			$params = array();
		
			$sth = mf_do_query($query,$params,$dbh);
			$font_css_array = array();
			while($row = mf_do_fetch_result($sth)){
				$font_css_array[] = urlencode($row['font_family']).":".$row['font_variants'];
			}
			
			$ssl_suffix = mf_get_ssl_suffix();

			$font_css_markup = implode('|',$font_css_array);
			if(!empty($font_css_array)){
				$font_css_markup = "<link href='http{$ssl_suffix}://fonts.googleapis.com/css?family={$font_css_markup}' rel='stylesheet' type='text/css'>\n";
			}else{
				$font_css_markup = '';
			}
		}
		
		return $font_css_markup;
	}
