<?php
/**
 * Joomla! component Creative Contact Form
 *
 * @version $Id: 2012-04-05 14:30:25 svn $
 * @author creative-solutions.net
 * @package Creative Contact Form
 * @subpackage com_creativecontactform
 * @license GNU/GPL
 *
 */

// no direct access
defined('_JEXEC') or die('Restircted access');

class CreativecontactformHelper
{

	//function to add scripts/styles
	private function add_scripts() {

		$version = '3.0.0';

		$document = JFactory::getDocument();

		$types_array = $this->types_array;
		$form_id = $this->form_id;

		$cssFile = JURI::base(true).'/components/com_creativecontactform/assets/css/main.css?version='.$version;
		$document->addStyleSheet($cssFile, 'text/css', null, array());

		$cssFile = JURI::base(true).'/components/com_creativecontactform/assets/css/creativecss-ui.css';
		$document->addStyleSheet($cssFile, 'text/css', null, array());

		$cssFile = JURI::base(true).'/components/com_creativecontactform/assets/css/creative-scroll.css';
		$document->addStyleSheet($cssFile, 'text/css', null, array());

		$cssFile = JURI::base(true).'/components/com_creativecontactform/generate.css.php?id_form='.$form_id.'&module_id=0';
		$document->addStyleSheet($cssFile, 'text/css', null, array());

		$cssFile = JURI::base(true).'/components/com_creativecontactform/assets/css/creative-tooltip.css';
		$document->addStyleSheet($cssFile, 'text/css', null, array());

		if(in_array('file-upload',$types_array)) {
			$cssFile = JURI::base(true).'/components/com_creativecontactform/assets/css/creative-upload.css';
			$document->addStyleSheet($cssFile, 'text/css', null, array());
		}

		if(in_array('datepicker',$types_array)) {
			$cssFile = JURI::base(true).'/components/com_creativecontactform/assets/css/creative-datepicker.css';
			$document->addStyleSheet($cssFile, 'text/css', null, array());
		}

		$jsFile = JURI::base(true).'/components/com_creativecontactform/assets/js/creativelib.js';
		$document->addScript($jsFile);

		$jsFile = JURI::base(true).'/components/com_creativecontactform/assets/js/creativelib-ui.js';
		$document->addScript($jsFile);

		$jsFile = JURI::base(true).'/components/com_creativecontactform/assets/js/creative-mousewheel.js';
		$document->addScript($jsFile);

		$jsFile = JURI::base(true).'/components/com_creativecontactform/assets/js/creative-scroll.js';
		$document->addScript($jsFile);

		if(in_array('file-upload',$types_array)) {
			$jsFile = JURI::base(true).'/components/com_creativecontactform/assets/js/jquery.iframe-transport.js';
			$document->addScript($jsFile);
			
			$jsFile = JURI::base(true).'/components/com_creativecontactform/assets/js/jquery.fileupload.js';
			$document->addScript($jsFile);
			
			$jsFile = JURI::base(true).'/components/com_creativecontactform/assets/js/jquery.fileupload-process.js';
			$document->addScript($jsFile);
			
			$jsFile = JURI::base(true).'/components/com_creativecontactform/assets/js/jquery.fileupload-validate.js';
			$document->addScript($jsFile);
		}

		$jsFile = JURI::base(true).'/components/com_creativecontactform/assets/js/creativecontactform.js?version='.$version;
		$document->addScript($jsFile);

		if(in_array('google-recaptcha',$types_array)) {
			$jsFile = "https://www.google.com/recaptcha/api.js?onload=ccf_recaptcha_onload&render=explicit";
			$document->addScript($jsFile);
		}	

	}

	private function getBrowser() { 
	    $u_agent = $_SERVER['HTTP_USER_AGENT']; 
	    $bname = 'Unknown';
	    $platform = 'Unknown';
	    $version= "";

	    //First get the platform?
	    if (preg_match('/linux/i', $u_agent)) {
	        $platform = 'linux';
	    }
	    elseif (preg_match('/macintosh|mac os x/i', $u_agent)) {
	        $platform = 'mac';
	    }
	    elseif (preg_match('/windows|win32/i', $u_agent)) {
	        $platform = 'windows';
	    }
	    
	    // Next get the name of the useragent yes seperately and for good reason
	    if(preg_match('/MSIE/i',$u_agent) && !preg_match('/Opera/i',$u_agent)) 
	    { 
	        $bname = 'Internet Explorer'; 
	        $ub = "MSIE"; 
	    } 
	    elseif(preg_match('/Firefox/i',$u_agent)) 
	    { 
	        $bname = 'Mozilla Firefox'; 
	        $ub = "Firefox"; 
	    } 
	    elseif(preg_match('/Chrome/i',$u_agent)) 
	    { 
	        $bname = 'Google Chrome'; 
	        $ub = "Chrome"; 
	    } 
	    elseif(preg_match('/Safari/i',$u_agent)) 
	    { 
	        $bname = 'Apple Safari'; 
	        $ub = "Safari"; 
	    } 
	    elseif(preg_match('/Opera/i',$u_agent)) 
	    { 
	        $bname = 'Opera'; 
	        $ub = "Opera"; 
	    } 
	    elseif(preg_match('/Netscape/i',$u_agent)) 
	    { 
	        $bname = 'Netscape'; 
	        $ub = "Netscape"; 
	    } 
	    
	    // finally get the correct version number
	    $known = array('Version', $ub, 'other');
	    $pattern = '#(?<browser>' . join('|', $known) .
	    ')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';
	    if (!preg_match_all($pattern, $u_agent, $matches)) {
	        // we have no matching number just continue
	    }
	    
	    // see how many we have
	    $i = count($matches['browser']);
	    if ($i != 1) {
	        //we will have two since we are not using 'other' argument yet
	        //see if version is before or after the name
	        if (strripos($u_agent,"Version") < strripos($u_agent,$ub)){
	            $version= $matches['version'][0];
	        }
	        else {
	            $version= $matches['version'][1];
	        }
	    }
	    else {
	        $version= $matches['version'][0];
	    }
	    
	    // check if we have a number
	    if ($version==null || $version=="") {$version="?";}
	    
	    return array(
	        'userAgent' => $u_agent,
	        'name'      => $bname,
	        'version'   => $version,
	        'platform'  => $platform,
	        'pattern'    => $pattern
	    );
	}

	private function getOS() { 
	    $user_agent = $_SERVER['HTTP_USER_AGENT'];
	    $os_platform    =   "Unknown OS Platform";
	    $os_array       =   array(
	                            '/windows nt 6.3/i'     =>  'Windows 8.1',
	                            '/windows nt 6.2/i'     =>  'Windows 8',
	                            '/windows nt 6.1/i'     =>  'Windows 7',
	                            '/windows nt 6.0/i'     =>  'Windows Vista',
	                            '/windows nt 5.2/i'     =>  'Windows Server 2003/XP x64',
	                            '/windows nt 5.1/i'     =>  'Windows XP',
	                            '/windows xp/i'         =>  'Windows XP',
	                            '/windows nt 5.0/i'     =>  'Windows 2000',
	                            '/windows me/i'         =>  'Windows ME',
	                            '/win98/i'              =>  'Windows 98',
	                            '/win95/i'              =>  'Windows 95',
	                            '/win16/i'              =>  'Windows 3.11',
	                            '/macintosh|mac os x/i' =>  'Mac OS X',
	                            '/mac_powerpc/i'        =>  'Mac OS 9',
	                            '/linux/i'              =>  'Linux',
	                            '/ubuntu/i'             =>  'Ubuntu',
	                            '/iphone/i'             =>  'iPhone',
	                            '/ipod/i'               =>  'iPod',
	                            '/ipad/i'               =>  'iPad',
	                            '/android/i'            =>  'Android',
	                            '/blackberry/i'         =>  'BlackBerry',
	                            '/webos/i'              =>  'Mobile'
	                        );
	    foreach ($os_array as $regex => $value) { 
	        if (preg_match($regex, $user_agent)) {
	            $os_platform    =   $value;
	        }
	    }   
	    return $os_platform;
	}
	
	private function get_data() {
		$db = JFactory::getDBO();

		//get field types array/////////////////////////////////////////////////////////////////////////////////////////////////
		$query = "
					SELECT
					sp.id,
					st.name as type
					FROM
					`#__creative_fields` sp
					JOIN `#__creative_field_types` st ON st.id = sp.id_type
					WHERE sp.published = '1'
					AND sp.id_form = '".$this->form_id."'
					ORDER BY sp.ordering,sp.id
				";
		$db->setQuery($query);
		$types_array_data = $db->loadAssocList();
		$types_array_index = 1;
		$types_array = array();
		if(is_array($types_array_data)) {
			foreach($types_array_data as $type) {
				$types_array[$types_array_index] = strtolower(str_replace(' ','-',str_replace('-','',$type['type'])));
				$types_array_index ++;
			}
		}
		$this->types_array = $types_array;

		// set field index
		if(!isset($this->field_index))
			$this->field_index = 1;

		//get form data/////////////////////////////////////////////////////////////////////////////////////////////////
		$query = "
					SELECT
					sp.`id_template`,
					sp.name,
					sp.top_text,
					sp.pre_text,
					sp.thank_you_text,
					sp.send_text,
					sp.send_new_text,
					sp.close_alert_text,
					sp.form_width,
					sp.redirect,
					sp.redirect_itemid,
					sp.redirect_url,
					sp.redirect_delay,
					sp.shake_count,
					sp.shake_distanse,
					sp.send_copy_enable,
					sp.send_copy_text,
					sp.shake_duration,
					sp.custom_css,
					st.styles
					FROM
					`#__creative_forms` sp
					LEFT JOIN `#__contact_templates` st ON st.id = sp.id_template
					WHERE sp.published = '1'
					AND sp.id = '".$this->form_id."'";
		$db->setQuery($query);
		$this->form_data = $db->loadAssoc();

		//sp.id_template

		//get fields data/////////////////////////////////////////////////////////////////////////////////////////////////
		$query = "
					SELECT
					sp.id,
					sp.name,
					sp.tooltip_text,
					sp.required,
					sp.ordering_field,
					sp.select_default_text,
					sp.show_parent_label,
					sp.select_no_match_text,
					sp.width,
					sp.field_margin_top,
					sp.select_show_scroll_after,
					sp.select_show_search_after,
					sp.upload_button_text,
					sp.upload_minfilesize,
					sp.upload_maxfilesize,
					sp.upload_acceptfiletypes,
					sp.upload_minfilesize_message,
					sp.upload_maxfilesize_message,
					sp.upload_acceptfiletypes_message,
					sp.captcha_wrong_message,

					sp.datepicker_date_format,
					sp.datepicker_animation,
					sp.datepicker_style,
					sp.datepicker_icon_style,
					sp.datepicker_show_icon,
					sp.datepicker_input_readonly,
					sp.datepicker_number_months,
					sp.datepicker_mindate,
					sp.datepicker_maxdate,
					sp.datepicker_changemonths,
					sp.datepicker_changeyears,
					sp.column_type,
					sp.custom_html,
					sp.google_maps,
					sp.heading,
					sp.recaptcha_site_key,
					sp.recaptcha_wrong_message,
					sp.recaptcha_theme,
					sp.recaptcha_type,
					sp.contact_data,
					sp.contact_data_width,
					sp.creative_popup,
					sp.creative_popup_embed,

					st.name as type
				FROM
					`#__creative_fields` sp
				JOIN `#__creative_field_types` st ON st.id = sp.id_type
				WHERE sp.published = '1'
				AND sp.id_form = '".$this->form_id."'
				ORDER BY sp.ordering,sp.id
		";
		$db->setQuery($query);
		$this->field_data = $db->loadAssocList();

		//get fields data/////////////////////////////////////////////////////////////////////////////////////////////////
		$REMOTE_ADDR = null;
		if(isset($_SERVER['REMOTE_ADDR'])) {
			$REMOTE_ADDR = $_SERVER['REMOTE_ADDR'];
		}
		elseif(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
			$REMOTE_ADDR = $_SERVER['HTTP_X_FORWARDED_FOR'];
		}
		elseif(isset($_SERVER['HTTP_CLIENT_IP'])) {
			$REMOTE_ADDR = $_SERVER['HTTP_CLIENT_IP'];
		}
		elseif(isset($_SERVER['HTTP_VIA'])) {
			$REMOTE_ADDR = $_SERVER['HTTP_VIA'];
		}
		else { $REMOTE_ADDR = 'Unknown';
		}
		$this->remote_addr = $REMOTE_ADDR;
	}

	private function get_rule_from_row($key,$row) {
		preg_match('/'.$key.'~([^\|]+)\|/',$row,$m);
		if(isset($m[1]))
			return $m[1];
		else
			return '';
	}	
	private function get_last_rule_from_row($key,$row) {
		preg_match('/'.$key.'~([^\|]+)$/',$row,$m);
		if(isset($m[1]))
			return $m[1];
		else
			return '';
	}	

	private function process_sections($matched_sections) {
		$sections_width = isset($this->sections_width) ? $this->sections_width : 120;
		$section_type = $matched_sections[1];
		$section_label = $matched_sections[2];
		$section_content = $matched_sections[3];

		$section_id = $this->section_id;

		$contact_data = '<div class="ccf_content_element ccf_content_element_'.$section_id.'">';
			$contact_data .= '<div class="ccf_content_element_content_wrapper">';
				$contact_data .= '<div style="margin-left: '.$sections_width.'px;" class="ccf_section_margin_control_'.$section_id.'">';
					$contact_data .= '<span>';
						$contact_data .= $section_content;
					$contact_data .= '</span>';
				$contact_data .= '</div>';
			$contact_data .= '</div>';
			$contact_data .= '<div class="ccf_content_element_icon_wrapper ccf_section_width_control_'.$section_id.'" style="width: '.$sections_width.'px;" >';
				$contact_data .= '<span class="ccf_content_icon ccf_content_icon_'.$section_type.'"></span>';
				$contact_data .= '<span class="ccf_content_element_label">'.$section_label.'</span>';
			$contact_data .= '</div>';
		$contact_data .= '</div>';

		$this->section_id ++;

		return $contact_data;
	}

	private function process_txt($txt,$process_types_array) {
		if(in_array('sections',$process_types_array)) {
			// preg_match_all('/{section type=\"([a-zA-Z]*)\" label=\"(.*?)\"}(.*?){\/section}/s',$sections,$matched_sections);
			$txt = preg_replace_callback('/{section icon=\"([a-zA-Z]*)\" label=\"(.*?)\"}(.*?){\/section}/s', array($this, 'process_sections'), $txt);

		}
		if(in_array('num',$process_types_array)) {
			$txt = preg_replace('/{num}(.*?){\/num}/s','<span class="ccf_content_styling">$1</span>', $txt);
		}
		if(in_array('popup',$process_types_array)) {
			$txt = preg_replace('/{creative_popup id="(.*?)" size="(\d+)X(\d+)"}(.*?){\/creative_popup}/s','<span class="ccf_popup_link" popup_id="$1" w="$2" h="$3">$4</span>', $txt);
		}
		if(in_array('heading',$process_types_array)) {
			$txt = preg_replace('/{heading}(.*?){\/heading}/s','<div class="creativecontactform_heading '.$this->heading_text_font_effect.'"><div class="creativecontactform_heading_inner">$1</div></div>', $txt);
		}

		return $txt;
	} 

	private function print_fields_array_html($field_data) {

		$db = JFactory::getDBO();
		//get data/////////////////////////////////////////////////////////////////////////////////////////////////////////
		$this->get_data();

		//add scripts/////////////////////////////////////////////////////////////////////////////////////////////////////
		if($this->type != 'plugin')
			$this->add_scripts();
		
		//Get variables////////////////////////////////////////////////////////////////////////////////////////////////////
		$module_id = $this->module_id;
		$form_data = $this->form_data;
		$types_array = $this->types_array;
		$form_id = $this->form_id;
		$templateid = $form_data['id_template'];
		$styles_row = $form_data['styles'];

		$tooltip_style = $this->get_rule_from_row(505,$styles_row);
		$tooltip_style = $tooltip_style == '' ? 'white' : $tooltip_style;

		$ccf_datepicker_style = $this->get_rule_from_row(562,$styles_row);
		$ccf_datepicker_icon_style = $this->get_rule_from_row(563,$styles_row);
		$ccf_global_icons_style = $this->get_rule_from_row(589,$styles_row);


		$top_text_font_effect = $this->get_rule_from_row(510,$styles_row);
		$heading_text_font_effect = $this->get_rule_from_row(530,$styles_row);
		$pre_text_font_effect = $this->get_rule_from_row(511,$styles_row);
		$label_text_font_effect = $this->get_rule_from_row(512,$styles_row);
		$label_hover_text_font_effect = $this->get_rule_from_row(513,$styles_row);
		$label_error_text_font_effect = $this->get_rule_from_row(514,$styles_row);
		$send_font_effect = $this->get_rule_from_row(515,$styles_row);
		$send_hover_font_effect = $this->get_rule_from_row(516,$styles_row);

		$this->heading_text_font_effect = $heading_text_font_effect;


		$focus_anim_enabled = $label_text_font_effect == $label_hover_text_font_effect ? 0 : 1;
		$error_anim_enabled = $label_text_font_effect == $label_error_text_font_effect ? 0 : 1;

		$user = JFactory::getUser();

		$is_textarea_exist = false;
		foreach($field_data as $field) {
			$field_index = $this->field_index;

			$field_width = $field['width'] != '' ? 'style="width: '.$field['width'].' !important"' : '';
			$field_width_select = $field['width'] != '' ? $field['width'] : '';
			$field_margin = $field['field_margin_top'] != '' ? 'style="margin: '.$field['field_margin_top'].' !important"' : '';
			
			$field_name = stripslashes($field['name']);
			$field_name = $this->process_txt($field_name,array('num','popup'));
			
			$field_tooltip_text = stripslashes($field['tooltip_text']);
			$field_type = strtolower(str_replace(' ','-',str_replace('-','',$field['type'])));
			$element_id = $field_type.'_'.$module_id.'_'.$field['id'];
			$required_classname = $field['required'] == 1 ? 'creativecontactform_required' : '';
			$required_symbol = $field['required'] == 1 ? ' <span class="creativecontactform_field_required">*</span>' : '';
			$predefined_value = $field_type == 'name' ? $user->name : ($field_type == 'email' ? $user->email : '');
			
			if($field_type == 'text-area') 
				$is_textarea_exist = true;
			//input html
			$input_type_text_arrays = array('text-input','name','address','email','phone','number','url');
			if(in_array($field_type,$input_type_text_arrays)) {
				$input_html = '<div class="creativecontactform_input_element '.$required_classname.'"><div class="creative_input_dummy_wrapper">';
				if($field_tooltip_text != '')
					$input_html .= '<span class="the-tooltip top right '.$tooltip_style.'"><span class="tooltip_inner creative_tooltip_ivisible">'.$field_tooltip_text.'</span></span>';
				$input_html .= '<input class="creative_'.$field_type.' '.$required_classname.' creative_input_reset" pre_value="'.str_replace('"','',$predefined_value).'" value="'.str_replace('"','',$predefined_value).'" type="text" id="'.$element_id.'" name="creativecontactform_fields['.$field_index.'][0]"></div></div>';
			}
			elseif($field_type == 'contact-data') {
				$sections_width = (int) $field["contact_data_width"];
				$this->sections_width = $sections_width;

				$sections_html = stripslashes($field["contact_data"]);
				$processed_data = $this->process_txt($sections_html,array('sections','num','heading'));

				$input_html = $processed_data;
			}
			elseif($field_type == 'custom-html') {
				$custom_html = stripslashes($field['custom_html']);
				$processed_data = $this->process_txt($custom_html,array('sections','num','heading'));

				$input_html = $processed_data;
			}		
			elseif($field_type == 'heading') {
				$heading = stripslashes($field['heading']);
				$heading = $this->process_txt($heading,array('numpopup'));

				$input_html = '<div class="creativecontactform_heading '.$heading_text_font_effect.'"><div class="creativecontactform_heading_inner">'.$heading.'</div></div>';
			}
			elseif($field_type == 'google-maps') {
				$google_maps = stripslashes($field['google_maps']);
				$google_maps = $this->process_txt($google_maps,array('popup'));

				$input_html = $google_maps;
			}
			elseif($field_type == 'text-area') {
				$input_html = '<div class="creativecontactform_input_element creative_textarea_wrapper '.$required_classname.'"><div class="creative_textarea_dummy_wrapper">';
				if($field_tooltip_text != '')
					$input_html .= '<span class="the-tooltip top right '.$tooltip_style.'"><span class="tooltip_inner creative_tooltip_ivisible">'.$field_tooltip_text.'</span></span>';
				$input_html .= '<textarea class="creative_textarea creative_'.$field_type.' '.$required_classname.' creative_textarea_reset" value="'.$predefined_value.'" cols="30" rows="15" id="'.$element_id.'" name="creativecontactform_fields['.$field_index.'][0]"></textarea></div></div>';
			}
			elseif($field_type == 'select' || $field_type == 'multiple-select' || $field_type == 'radio' || $field_type == 'checkbox') {
				//get child options
				$query = "
							SELECT
 							spo.name,
 							spo.id,
 							spo.value,
 							spo.selected
							FROM
								`#__creative_form_options` spo
							WHERE spo.id_parent = '".$field['id']."'
							AND spo.showrow = '1'
							ORDER BY ";
							if($field['ordering_field'] == 0)
								$query .= "spo.ordering";
							else
								$query .= "spo.name";
				$db->setQuery($query);
				$childs_array = $db->loadAssocList();
				if (sizeof($childs_array) > 0)
				{
					$childs_length = sizeof($childs_array);
					if($field_type == 'select' || $field_type == 'multiple-select') {
						$selected_count = 0;
						foreach ($childs_array as $key => $value)
						{
							if($value['selected'] == 1) {
								$selected_count= 1;
								break;
							}
						}
						$def_selection = $selected_count == 0 ? 'selected="selected"' : '';
						
						$show_search = $childs_length >= $field["select_show_search_after"] ? 'show' : 'hide';
						$scroll_after = (int)$field["select_show_scroll_after"] > 3 ? (int)$field["select_show_scroll_after"] : 3;
						
						$multile_info = $field_type == 'multiple-select' ? 'multiple="multiple"' : '';
						$multile_info_val = $field_type == 'multiple-select' ? '[]' : '';
						$input_html = '<select show_search="'.$show_search.'" scroll_after="'.$scroll_after.'" special_width="'.$field_width_select.'" select_no_match_text="'.stripslashes(str_replace('"','',$field["select_no_match_text"])).'" class="will_be_creative_select '.$required_classname.'" '.$multile_info.' name="creativecontactform_fields['.$field_index.'][0]'.$multile_info_val.'">';
						$input_html .= '<option '.$def_selection.' class="def_value" value="creative_empty">'.$field["select_default_text"].'</option>';
						$selected = '';
						$pre_val='';
						$seted_value = false;
						foreach ($childs_array as $key => $value)
						{
							if(!$seted_value && $field_type == 'select' && $value['selected'] == '1') {
								$selected = 'selected="selected"';
								$pre_val = 'pre_val="selected"';
								$seted_value = true;
							}
							elseif($field_type == 'multiple-select'  &&  $value['selected'] == '1') {
								$selected = 'selected="selected"';
								$pre_val = 'pre_val="selected"';
							}
							else {
								$selected = '';
								$pre_val = '';
							}
							
							$input_html .= '<option id="'.$module_id.'_'.$field["id"].'_'.$value["id"].'" value="'.stripslashes(str_replace('"','',$value["value"])).'" '.$selected.' '.$pre_val.'>'.stripslashes($value["name"]).'</option>';
						}
						$input_html .= '</select>';
					}
					elseif($field_type == 'radio' || $field_type == 'checkbox') {

						$input_html = '';
						$colors_array = array("black","blue","red","litegreen","yellow","liteblue","green","crimson","litecrimson");
						$selected = '';
						$pre_val='';
						$seted_value = false;
						foreach ($childs_array as $key => $value)
						{
							if($field_type == 'radio' && !$seted_value && $value['selected'] == '1') {
								$selected = 'checked="checked"';
								$pre_val = 'pre_val="checked"';
								$seted_value = true;
							}
							elseif($field_type == 'checkbox'  &&  $value['selected'] == '1') {
								$selected = 'checked="checked"';
							$pre_val = 'pre_val="checked"';	 											
							}
							else {
								$selected = '';
							$pre_val = '';	 											
							}
							
							$data_color_index = $key % 8;

							$current_label = stripslashes($value["name"]);
							$current_label = $this->process_txt($current_label,array('num','popup'));
							
							$label_class = $field['show_parent_label'] == 0 ? 'without_parent_label' : '';
							$req_symbol = ($field['show_parent_label'] == 0 && $key == 0) ? $required_symbol : '';
							$input_html .= '<div class="creative_checkbox_wrapper centered"><div class="answer_name"><label uniq_index="'.$module_id.'_'.$field["id"].'_'.$value["id"].'" class="twoglux_label '.$label_class.'"><span class="creative_checkbox_label_wrapper">'.$current_label.' '.$req_symbol.'</span></label></div>';
							$input_html .= '<div class="answer_input">';
							
							if($field_type == 'radio')
								$input_html .= '<input '.$selected.' '.$pre_val.' id="'.$module_id.'_'.$field["id"].'_'.$value["id"].'" type="radio" class="creative_ch_r_element creativeform_twoglux_styled elem_'.$module_id.'_'.$field["id"].'" value="'.stripslashes(str_replace('"','',$value["value"])).'" uniq_index="elem_'.$module_id.'_'.$field["id"].'" name="remove_this_partcreativecontactform_fields['.$field_index.'][0]" data-color="'.$colors_array[$data_color_index].'" />';
							else
								$input_html .= '<input '.$selected.' '.$pre_val.' id="'.$module_id.'_'.$field["id"].'_'.$value["id"].'" type="checkbox" class="creative_ch_r_element creativeform_twoglux_styled" value="'.stripslashes(str_replace('"','',$value["value"])).'" name="creativecontactform_fields['.$field_index.'][0][]" data-color="'.$colors_array[$data_color_index].'" />';
							
							$input_html .= '</div></div><div class="creative_clear"></div>';
						}
					}
				}
				else {
					$input_html = 'There are no options to be shown.';
				}
			}
			elseif($field_type == 'captcha') {
				$input_html = '<img id="creative_captcha_'.$module_id.'_'.$field["id"].'" class="creative_captcha" src="components/com_creativecontactform/captcha.php?fid='.$field["id"].'&r='.rand(100000,999999).'" /><div fid="'.$field["id"].'" holder="creative_captcha_'.$module_id.'_'.$field["id"].'" class="reload_creative_captcha icon_'.$ccf_global_icons_style.'"></div><div class="creative_clear"></div>';
				$input_html .= '<div style="width: 200px !important;" class="creativecontactform_input_element creativecontactform_required creative_captcha_input_wrapper"><div class="creative_input_dummy_wrapper"><input class="creative_'.$field_type.' creativecontactform_required creative_input_reset" value="" type="text" id="'.$element_id.'" name="creativecontactform_captcha['.$field["id"].']"></div></div><div class="creative_captcha_info" style="display:none">'.stripslashes($field["captcha_wrong_message"]).'</div>';
			}

			$hidden_field_types = array('file-upload','captcha','custom-html','heading','google-maps','google-recaptcha','contact-data','social-links','creative-popup');
			if(!in_array($field_type,$hidden_field_types)) {
				$input_html .= '<input type="hidden" name="creativecontactform_fields['.$field_index.'][1]" value="'.stripslashes(str_replace('"','',$field_name)).'" />';
				$input_html .= '<input type="hidden" name="creativecontactform_fields['.$field_index.'][2]" value="'.$field_type.'" />';
			}
			
			//start printing html
			$radio_checkbox_class = ($field_type == 'radio' || $field_type == 'checkbox' || $field_type == 'file-upload') ? 'creative_'.$field_type : '';
			$radio_checkbox_req_class = ($field_type == 'radio' || $field_type == 'checkbox'  || $field_type == 'file-upload') ? $required_classname : '';

			if($field_type == 'radio' || $field_type == 'checkbox' || $field_type == 'file-upload' || $field_type == 'captcha') {
				$box_inner_class = $is_textarea_exist ? 'creativecontactform_field_box_textarea_inner' : 'creativecontactform_field_box_inner';
			}
			else {
				$box_inner_class = $field_type == 'text-area' ? 'creativecontactform_field_box_textarea_inner' : 'creativecontactform_field_box_inner';

			}

			//start printing boxes
			$wrapper_id = $field_type == 'google-recaptcha' ? $element_id : '';
			$field_box_style = $field_type == 'creative-popup' ? 'style="display: none"' : '';
				echo '<div id="'.$wrapper_id.'" '.$field_margin.' class="creativecontactform_field_box creative_hidden_animation_block_state1 creative_timing_'.$field_index.' creative_timing_'.$field_type.' '.$radio_checkbox_class.' '.$radio_checkbox_req_class.'"><div '.$field_width.' class="'.$box_inner_class.'">';
					$show_label = ($field['show_parent_label'] == 0 || $field_type == 'heading') ? 'style="display:none !important"' : '';
					echo '<label normal_effect_class="'.$label_text_font_effect.'" hover_effect_class="'.$label_hover_text_font_effect.'" error_effect_class="'.$label_error_text_font_effect.'" class="creativecontactform_field_name '.$label_text_font_effect.'" for="'.$element_id.'" '.$show_label.'><span class="creative_label_txt_wrapper">'.$field_name;
					if($field_type == 'captcha')
						echo ' <span class="creativecontactform_field_required">*</span></label>';
					else	
						echo $required_symbol.'</span></label>';
					echo $input_html;
				echo '</div></div>';
			// echo '</div>';
			
			$this->field_index ++;
		}
	}
	
	public function render_html()
	{
		$db = JFactory::getDBO();
		$document = JFactory::getDocument();

		$browser_data = $this -> getBrowser();
		$browser = $browser_data["name"] . ' ' . $browser_data["version"];
		$op_s = $this -> getOs();

		//get data/////////////////////////////////////////////////////////////////////////////////////////////////////////
		$this->get_data();

		//add scripts/////////////////////////////////////////////////////////////////////////////////////////////////////
		if($this->type != 'plugin')
			$this->add_scripts();
		
		//Get variables////////////////////////////////////////////////////////////////////////////////////////////////////
		$module_id = $this->module_id;
		$form_data = $this->form_data;
		$field_data = $this->field_data;
		$types_array = $this->types_array;
		$form_id = $this->form_id;
		$templateid = $form_data['id_template'];
		$styles_row = $form_data['styles'];

		$this->section_id = 0;

		$tooltip_style = $this->get_rule_from_row(505,$styles_row);
		$tooltip_style = $tooltip_style == '' ? 'white' : $tooltip_style;

		$scrollbar_popup_style = $this->get_rule_from_row(629,$styles_row);
		$scrollbar_popup_style = $scrollbar_popup_style == '' ? 'inset-2-dark' : $scrollbar_popup_style;
		$scrollbar_content_style = $this->get_rule_from_row(630,$styles_row);
		$scrollbar_content_style = $scrollbar_content_style == '' ? 'inset-2-dark' : $scrollbar_content_style;
		$custom_js = $this->get_last_rule_from_row(628,$styles_row);

		$ccf_global_icons_style = $this->get_rule_from_row(589,$styles_row);
		$ccf_sections_icons_style = $this->get_rule_from_row(552,$styles_row);

		$top_text_font_effect = $this->get_rule_from_row(510,$styles_row);
		$heading_text_font_effect = $this->get_rule_from_row(530,$styles_row);
		$pre_text_font_effect = $this->get_rule_from_row(511,$styles_row);
		$label_text_font_effect = $this->get_rule_from_row(512,$styles_row);
		$label_hover_text_font_effect = $this->get_rule_from_row(513,$styles_row);
		$label_error_text_font_effect = $this->get_rule_from_row(514,$styles_row);
		$send_font_effect = $this->get_rule_from_row(515,$styles_row);
		$send_hover_font_effect = $this->get_rule_from_row(516,$styles_row);

		$focus_anim_enabled = $label_text_font_effect == $label_hover_text_font_effect ? 0 : 1;
		$error_anim_enabled = $label_text_font_effect == $label_error_text_font_effect ? 0 : 1;

		//get reqired google fonts
		$ccf_fonts_indexes_array = array(506,507,508,509,131,112,202,152,529);
		$ccf_google_requested_fonts = array();
		foreach($ccf_fonts_indexes_array as $key) {
			$ccf_googlefont = 'ccf-googlewebfont-';
			preg_match('/'.$key.'~([^\|]+)\|/',$styles_row,$m);

			if(is_array($m) && sizeof($m) > 0) {
				$ccf_font_rule = $m[1];
				if (strpos($ccf_font_rule,$ccf_googlefont) !== false) {
					$ccf_font_rule = str_replace($ccf_googlefont, '', $ccf_font_rule);
					$ccf_font_rule = str_replace(' ', '+', $ccf_font_rule);
					$ccf_google_requested_fonts[] = $ccf_font_rule;
				}
			}
		}
		if(sizeof($ccf_google_requested_fonts) > 0) {
			$ccf_google_requested_fonts = implode('|',$ccf_google_requested_fonts);
			$ccf_google_font_link = 'http://fonts.googleapis.com/css?family='.$ccf_google_requested_fonts;
			$ccf_google_link = '<link rel="stylesheet" type="text/css" href="'.$ccf_google_font_link.'">';
		}
		else
			$ccf_google_link = '';

		$REMOTE_ADDR = $this->remote_addr;
		$user = JFactory::getUser();

		$toptxt = $form_data['top_text'];
		$pretxt = stripcslashes($form_data['pre_text']);
		$pretxt = $this->process_txt($pretxt,array('num'));

		$form_width = $form_data['form_width'];
		$custom_css = $form_data['custom_css'];
		$redirect_enable =  $form_data['redirect'];
		$redirect = '';
		if ($redirect_enable) {
			$redirectItemId = (int) $form_data['redirect_itemid'] == 0 ? 1 : (int) $form_data['redirect_itemid'];
			$redirectUrl = $form_data['redirect_url'];
			if ($redirectUrl != '') {
				$redirect = JRoute::_($redirectUrl, false);
			} else {
				$redirect = JRoute::_('index.php?Itemid='.$redirectItemId);
			}
		}
		$redirect_delay = (int) $form_data['redirect_delay'];
		$thank_you_text = htmlspecialchars($form_data['thank_you_text'],ENT_QUOTES);
		$send_text = htmlspecialchars($form_data['send_text'],ENT_QUOTES);
		$send_new_text = htmlspecialchars($form_data['send_new_text'],ENT_QUOTES);
		$close_alert_text = htmlspecialchars($form_data['close_alert_text'],ENT_QUOTES);

		//validation options
		$shake_count = (int) $form_data['shake_count'];
		$shake_distanse = (int) $form_data['shake_distanse'];
		$shake_duration = (int) $form_data['shake_duration'];

		//send copy options
		$send_copy_enable= (int) $form_data['send_copy_enable'];
		$send_copy_text=htmlspecialchars($form_data['send_copy_text'],ENT_QUOTES);

		//strat rendering html///////////////////////////////////////////////////////////////////////////////////////////////
		ob_start();
		if(sizeof($field_data) > 0) {
			?>
			<?php echo $ccf_google_link;?>
			<div class="creativecontactform_wrapper creative_wrapper_animation_state_1 creative_form_module creative_form_<?php echo $form_id;?> ccf_icon_<?php echo $ccf_global_icons_style;?> ccf_sections_template_<?php echo $ccf_sections_icons_style;?>" <?php if($form_width != '') { echo 'style="width: '.$form_width.' !important"'; }?> focus_anim_enabled="<?php echo $focus_anim_enabled;?>" error_anim_enabled="<?php echo $error_anim_enabled;?>" scrollbar_popup_style="<?php echo $scrollbar_popup_style;?>"  scrollbar_content_style="<?php echo $scrollbar_content_style;?>">
				<div class="creativecontactform_wrapper_inner">
				<div class="creativecontactform_loading_wrapper"><table style="border: none;width: 100%;height: 100%"><tr><td align="center" valign="middle"><img src="<?php echo JURI::base(true).'/components/com_creativecontactform/assets/images/ajax-loader.gif';?>" /></td></tr></table></div>
	 			
	 			<div class="creativecontactform_header creative_header_animation_state_1">
		 			<div class="creativecontactform_title <?php echo $top_text_font_effect;?>"><?php echo $toptxt;?></div>
		 			<?php if($pretxt != '') {?><div class="creativecontactform_pre_text <?php echo $pre_text_font_effect;?>"><?php echo $pretxt;?></div><?php }?>
	 			</div>
 				<form class="creativecontactform_form">
		 			<div class="creativecontactform_body creative_body_animation_state_1">
					 		<?php 
			 				if(sizeof($field_data) > 0) {

		 						// split data
		 						$fields_final_array = array();
		 						$k = 0;
		 						$separate_col_0 = false;
		 						$separate_col_12 = false;
		 						foreach($field_data as $field) {
		 							$column_type = $field['column_type'];
		 							if($column_type != 0) {
		 								if($separate_col_12) {
		 									$k ++;
		 									$separate_col_12 = false;
		 								}

		 								$fields_final_array[$k][$column_type][] = $field;
		 								$separate_col_0 = true;
		 							}
		 							else {
		 								if($separate_col_0) {
		 									$k ++;
		 									$separate_col_0 = false;
		 								}

		 								$fields_final_array[$k][$column_type][] = $field;
	 									$separate_col_12 = true;

		 							}
		 						}
		 						foreach($fields_final_array as $k => $field_columns_array) {
		 							// echo '<div style="clear: both">'.$k.'</div>';
		 							//print left columns
		 							if(isset($field_columns_array['1']) || isset($field_columns_array['2'])) {
			 							echo '<div class="creative_field_box_wrapper creative_field_box_wrapper_1 creative_field_box_animation_state_1"><div class="creative_field_box_wrapper_1_inner">';
			 								if(isset($field_columns_array['1'])) {
			 									$this->print_fields_array_html($field_columns_array['1']);
			 								}
			 							echo '</div></div>';
			 						}
			 						if(isset($field_columns_array['2'])) {
			 							//print right column
			 							echo '<div class="creative_field_box_wrapper creative_field_box_wrapper_2 creative_field_box_animation_state_1"><div class="creative_field_box_wrapper_2_inner">';
			 								if(isset($field_columns_array['2'])) {
			 									$this->print_fields_array_html($field_columns_array['2']);
			 								}
			 							echo '</div></div>';
			 						}
		 							// print both columns
		 							if(isset($field_columns_array['0'])) {
			 							//print right column
			 							echo '<div class="creativecontactform_clear"></div><div class="creative_field_box_wrapper creative_field_box_wrapper_0 creative_field_box_animation_state_1">';
			 								if(isset($field_columns_array['0'])) {
			 									$this->print_fields_array_html($field_columns_array['0']);
			 								}
			 							echo '</div>';
			 						}
		 						}

			 				}
			 				
			 				if($send_copy_enable == 1) { 
			 					// $field_index ++;
			 					echo '<div class="creativecontactform_clear"></div><div class="creativecontactform_field_box creative_hidden_animation_block_state1 creative_timing_sendmecopy creative_timing_6 creative_field_box_animation_state_1"><div class="creativecontactform_field_box_textarea_inner">';
				 					echo '
				 							<div class="answer_name"><label uniq_index="'.$module_id.'_0_0" class="twoglux_label without_parent_label">'.$send_copy_text.'</label></div>
				 							<div class="answer_input"><input id="'.$module_id.'_0_0" type="checkbox" class="creativeform_twoglux_styled" value="1" name="creativecontactform_send_copy_enable" data-color="blue" /></div><div class="creative_clear"></div>';
				 				echo '</div></div>';
			 				}
			 				?>
						<div class="creative_clear"></div>
		 			</div>
		 			<div class="creativecontactform_footer creative_footer_animation_state_1">
			 			<div class="creativecontactform_submit_wrapper creative_button_animation_state_1">
			 				<input type="button" value="<?php echo $send_text;?>" class="creativecontactform_send <?php echo $send_font_effect;?>" roll="<?php echo $form_id;?>" normal_effect_class="<?php echo $send_font_effect;?>" hover_effect_class="<?php echo $send_hover_font_effect;?>"/>
			 				<input type="button" value="<?php echo $send_new_text;?>" class="creativecontactform_send_new creativecontactform_hidden <?php echo $send_font_effect;?>"  roll="<?php echo $form_id;?>" normal_effect_class="<?php echo $send_font_effect;?>" hover_effect_class="<?php echo $send_hover_font_effect;?>"/>
			 				<div class="creativecontactform_clear"></div>
			 			</div>
			 			<div class="creativecontactform_clear"></div>
			 			<input type="hidden" name="<?php echo JSession::getFormToken();?>" class="creativecontactform_token" value="1" />
			 			<input type="hidden" value="<?php echo $REMOTE_ADDR;?>"  name="creativecontactform_ip" />
			 			<input type="hidden" value="<?php echo JURI::current();?>"  name="creativecontactform_referrer" />
			 			<input type="hidden" value="<?php echo $document->getTitle();?>"  name="creativecontactform_page_title" />
			 			<input type="hidden" value="<?php echo $browser;?>"  name="creativecontactform_browser" />
			 			<input type="hidden" value="<?php echo $op_s;?>"  name="creativecontactform_operating_system" />
			 			<input type="hidden" value="" class="ccf_sc_res"  name="creativecontactform_sc_res" />
			 			<input type="hidden" value="<?php echo $module_id;?>" class="creativecontactform_module_id" name="creativecontactform_module_id" />
			 			<input type="hidden" value="<?php echo $form_id;?>" class="creativecontactform_form_id" name="creativecontactform_form_id" />
		 			</div>
	 			</form>
	 		</div>
	 		</div>

	 		<?php
			//including custom javascript/////////////////////////////////////////////////////////////////////////////////////////////////
			$jsInclude = ' if (typeof creativecontactform_shake_count_array === \'undefined\') { var creativecontactform_shake_count_array = new Array();};';
			$jsInclude .= 'creativecontactform_shake_count_array['.$form_id.'] = "'.$shake_count.'";';

			$jsInclude .= ' if (typeof creativecontactform_shake_distanse_array === \'undefined\') { var creativecontactform_shake_distanse_array = new Array();};';
			$jsInclude .= 'creativecontactform_shake_distanse_array['.$form_id.'] = "'.$shake_distanse.'";';

			$jsInclude .= ' if (typeof creativecontactform_shake_duration_array === \'undefined\') { var creativecontactform_shake_duration_array = new Array();};';
			$jsInclude .= 'creativecontactform_shake_duration_array['.$form_id.'] = "'.$shake_duration.'";';

			$jsInclude .= 'var creativecontactform_path = "'.JURI::base(true).'/components/com_creativecontactform/";';

			$jsInclude .= ' if (typeof creativecontactform_redirect_enable_array === \'undefined\') { var creativecontactform_redirect_enable_array = new Array();};';
			$jsInclude .= 'creativecontactform_redirect_enable_array['.$form_id.'] = "'.$redirect_enable.'";';

			$jsInclude .= ' if (typeof creativecontactform_redirect_array === \'undefined\') { var creativecontactform_redirect_array = new Array();};';
			$jsInclude .= 'creativecontactform_redirect_array['.$form_id.'] = "'.$redirect.'";';

			$jsInclude .= ' if (typeof creativecontactform_redirect_delay_array === \'undefined\') { var creativecontactform_redirect_delay_array = new Array();};';
			$jsInclude .= 'creativecontactform_redirect_delay_array['.$form_id.'] = "'.$redirect_delay.'";';

			$jsInclude .= ' if (typeof creativecontactform_thank_you_text_array === \'undefined\') { var creativecontactform_thank_you_text_array = new Array();};';
			$jsInclude .= 'creativecontactform_thank_you_text_array['.$form_id.'] = "'.$thank_you_text.'";';

			// $jsInclude .= ' if (typeof creativecontactform_pre_text_array === \'undefined\') { var creativecontactform_pre_text_array = new Array();};';
			// $jsInclude .= 'creativecontactform_pre_text_array['.$form_id.'] = "'.$pretxt.'";';

			$jsInclude .= ' if (typeof close_alert_text === \'undefined\') { var close_alert_text = new Array();};';
			$jsInclude .= 'close_alert_text['.$form_id.'] = "'.$close_alert_text.'";';

			$jsInclude .= 'creativecontactform_juri = "'.JURI::base( true ).'";';

			if($this->type != 'plugin') {
				$document = JFactory::getDocument();
				$document->addScriptDeclaration ( $jsInclude );
			}
			else {
				echo $jstoinclude = '<script type="text/javascript">'.$jsInclude.'</script>';
			}
			
			// custom css
			if($custom_css != '') {
				$custom_css = str_replace('FORM_ID', $form_id, $custom_css);
				echo '<style type="text/css">'.$custom_css.'</style>';
			}			
			// custom js
			if($custom_js != '') {
				$custom_js = str_replace('FORM_ID', $form_id, $custom_js);
				echo '<script type="text/javascript">(function($) {$(document).ready(function() {'.$custom_js.'})})(creativeJ);</script>';
			}

			//google recaptcha javascript
			if(in_array('google-recaptcha',$types_array)) {

				$query_captcha = "
							SELECT 
								sp.id, 
								sp.recaptcha_site_key, 
								sp.recaptcha_theme, 
								sp.recaptcha_type
							FROM 
								`#__creative_fields` sp 
							WHERE sp.published = '1' 
							AND sp.id_type = '19' 
							AND sp.id_form = '".$form_id."'";
				$db->setQuery($query_captcha);
				$recaptcha_array = $db->loadAssocList();

				if(is_array($recaptcha_array)) {
					$rcp_verify_functions = '';
					$rcp_onload_functions = '';
					foreach ($recaptcha_array as $rcp_key => $rcp_data) {
						$el_id = 'ccf_recaptcha_'.$module_id.'_'.$rcp_data['id'];
						$element_id = 'google-recaptcha_'.$module_id.'_'.$rcp_data['id'];

						$rcp_verify_functions .= '
									var verify_callback_'.$el_id.' = function (response) {
									    document.getElementById("hidden_'.$el_id.'").value = "1";
									    var field_box_class = document.getElementById("'.$element_id.'").className;
									    field_box_class = field_box_class.replace(\' creativecontactform_error\',\'\');
									    document.getElementById("'.$element_id.'").className = field_box_class;
									};';
						$rcp_onload_functions .= 'var ccf_rcp_'.$el_id.' = grecaptcha.render(\''.$el_id.'\', {
											          \'sitekey\' : \''.$rcp_data["recaptcha_site_key"].'\',
											          \'theme\' : \''.$rcp_data["recaptcha_theme"].'\',
											          \'type\' : \''.$rcp_data["recaptcha_type"].'\',
											          \'callback\' : verify_callback_'.$el_id.'
											        });document.getElementById("'.$element_id.'").setAttribute("rcp_id", ccf_rcp_'.$el_id.');';
					}
					$rcp_js = '<script class="ccf_recaptcha_script" type="text/javascript">
									'.$rcp_verify_functions.'
									var ccf_recaptcha_onload = function() {
										'.$rcp_onload_functions.'
									}
								</script>
								';

					echo $rcp_js;
					echo '<script type="text/javascript">setTimeout(function() {try{ccf_recaptcha_onload();}catch(err){}},1200)</script>';
				}
			}
			//END google recaptcha javascript
		}
		else {
			echo 'Creative Contact Form: There is nothing to show!';
		}
 		?>

		<?php
		return $render_html = ob_get_clean();
	}
}
