<?php
defined('_JEXEC') or die('Direct Access to this location is not allowed.');

require_once(JPATH_SITE.'/administrator/components/com_facileforms/libraries/Zend/Json/Decoder.php');
require_once(JPATH_SITE.'/administrator/components/com_facileforms/libraries/Zend/Json/Encoder.php');

class BFQuickMode{
	
	/**
	 * @var HTML_facileFormsProcessor
	 */
	private $p = null;
	
	private $dataObject = array();
	
	private $rootMdata = array();
	
	private $fading = true;
	
	private $fadingClass = '';
	
	private $fadingCall = '';
	
	private $useErrorAlerts = false;
	
	private $rollover = false;
	
	private $rolloverColor = '';
	
	function __construct( HTML_facileFormsProcessor $p ){
		JHTML::_('behavior.tooltip');
		
		$this->p = $p;
		$this->dataObject = Zend_Json::decode( base64_decode($this->p->formrow->template_code) );
		$this->rootMdata = $this->dataObject['properties'];
		$this->fading = $this->rootMdata['fadeIn'];
		$this->useErrorAlerts = $this->rootMdata['useErrorAlerts'];
		$this->rollover = $this->rootMdata['rollover'];
		$this->rolloverColor = $this->rootMdata['rolloverColor'];
		
		if($this->fading || !$this->useErrorAlerts || $this->rollover){
			JFactory::getDocument()->addScript(JURI::root() . 'components/com_facileforms/libraries/jquery/jquery.min.js');
			JFactory::getDocument()->addScriptDeclaration('var $jq = jQuery.noConflict();');
			if(!$this->useErrorAlerts){
				JFactory::getDocument()->addScriptDeclaration('var bfUseErrorAlerts = false;'."\n");
				JFactory::getDocument()->addScriptDeclaration('
				function bfShowErrors(error){
					$jq(".bfErrorMessage").html("");
					$jq(".bfErrorMessage").css("display","none");
					$jq(".bfErrorMessage").fadeIn(1500);
					var allErrors = "";
					var errors = error.split("\n");
					for(var i = 0; i < errors.length; i++){
						allErrors += "<div class=\"bfError\">" + errors[i] + "</div>";
					}
					$jq(".bfErrorMessage").html(allErrors);
					$jq(".bfErrorMessage").css("display","");
				}');
			}
			if($this->fading){
				$this->fadingClass = ' bfFadingClass';
				$this->fadingCall  = 'bfFade();';
				JFactory::getDocument()->addScriptDeclaration('
					function bfFade(){
						$jq(".bfPageIntro").fadeIn(1000);
						var size = 0;
						$jq(".bfBlock, .bfInline, .bfElemWrap").each(function(i,val){
							var t = this;
							setTimeout(function(){$jq(t).fadeIn(1000)}, (i*100));
							size = i;
						});
						setTimeout(\'$jq(".bfSubmitButton").fadeIn(1000)\', size * 100);
						setTimeout(\'$jq(".bfPrevButton").fadeIn(1000)\', size * 100);
						setTimeout(\'$jq(".bfNextButton").fadeIn(1000)\', size * 100);
						setTimeout(\'$jq(".bfCancelButton").fadeIn(1000)\', size * 100);
					}
				');
			}
			if($this->rollover && trim($this->rolloverColor) != ''){
				JFactory::getDocument()->addScriptDeclaration('
					var bfElemWrapBg = "";
					function bfSetElemWrapBg(){
						bfElemWrapBg = $jq(".bfElemWrap").css("background-color");
					}
					function bfRollover() {
						$jq(".ff_elem").focus(
							function(){
								var parent = $jq(this).parent();
								if(parent && parent.attr("class").substr(0, 10) == "bfElemWrap"){
									parent.css("background-color","'.$this->rolloverColor.'");
								} else {
									parent = $jq(this).parent().parent();
									parent.css("background-color","'.$this->rolloverColor.'");
								}
							}
						).blur(
							function(){
								var parent = $jq(this).parent();
								if(parent && parent.attr("class").substr(0, 10) == "bfElemWrap"){
									parent.css("background-color",bfElemWrapBg);
								} else {
									parent = $jq(this).parent().parent();
									parent.css("background-color",bfElemWrapBg);
								}
							}
						);
					}
					function bfRollover2() {
						$jq(".bfElemWrap").mouseover(
							function(){
								$jq(this).css("background-color","'.$this->rolloverColor.'");
							}
						).mouseout(
							function(){
								$jq(this).css("background-color",bfElemWrapBg);
							}
						);
					}
				');
			}
			JFactory::getDocument()->addScriptDeclaration('
				$jq(document).ready(function() {
							if(typeof bfFade != "undefined")bfFade();
							if(typeof bfSetElemWrapBg != "undefined")bfSetElemWrapBg();
							if(typeof bfRollover != "undefined")bfRollover();
							if(typeof bfRollover2 != "undefined")bfRollover2();
						});
				');
		}
		// loading system css
		JFactory::getDocument()->addStyleSheet( JURI::root() . 'components/com_facileforms/themes/quickmode/system.css' );
		
		$stylelink = '<!--[if IE 7]>' ."\n";
		$stylelink .= '<link rel="stylesheet" href="'.JURI::root() . 'components/com_facileforms/themes/quickmode/system.ie7.css" />' ."\n";
		$stylelink .= '<![endif]-->' ."\n";
		JFactory::getDocument()->addCustomTag($stylelink);
		
		$stylelink = '<!--[if IE]>' ."\n";
		$stylelink .= '<link rel="stylesheet" href="'.JURI::root() . 'components/com_facileforms/themes/quickmode/system.ie.css" />' ."\n";
		$stylelink .= '<![endif]-->' ."\n";
		JFactory::getDocument()->addCustomTag($stylelink);
		
		// loading theme
		if($this->rootMdata['theme'] != 'none' && @file_exists(JPATH_SITE .'/components/com_facileforms/themes/quickmode/'. $this->rootMdata['theme'].'/theme.css')){
			JFactory::getDocument()->addStyleSheet( JURI::root() . 'components/com_facileforms/themes/quickmode/'. $this->rootMdata['theme'].'/theme.css' );
		}
	}
	
	public function process(&$dataObject, $parent = null, $parentPage = null, $index = 0, $childrenLength = 0){
		if(isset($dataObject['attributes']) && isset($dataObject['properties']) ){
			
			$options = array('type' => 'normal', 'displayType' => 'breaks');
			if($parent != null && $parent['type'] == 'section'){
				$options['type'] = $parent['bfType'];
				$options['displayType'] = $parent['displayType'];
			}
			
			$class = ' class="bfBlock'.$this->fadingClass.'"';
			if($options['displayType'] == 'inline'){
				$class = ' class="bfInline'.$this->fadingClass.'"';
			}
			
			$mdata = $dataObject['properties'];
			
			if($mdata['type'] == 'page'){
				
				$parentPage = $mdata;
				if($parentPage['pageNumber'] > 1){
					echo '</div>'."\n"; // closing previous pages
				}
				
				echo '<div id="bfPage'.$parentPage['pageNumber'].'" style="display:none">'."\n"; // opening current page
				
				if(trim($mdata['pageIntro'])!=''){
					echo '<p class="bfPageIntro'.$this->fadingClass.'">'."\n";
					echo $mdata['pageIntro']."\n";
					echo '</p>'."\n";
				}
				
				if(!$this->useErrorAlerts){
					echo '<span class="bfErrorMessage" style="display:none"></span>'."\n";
				}
				
			} else if($mdata['type'] == 'section'){
				if($mdata['bfType'] == 'section'){
					echo '<fieldset'.$class.'>'."\n";
					if(trim($mdata['title']) != ''){
						echo '<legend>'.htmlentities(trim($mdata['title']), ENT_QUOTES, 'UTF-8').'</legend>'."\n";
					}
				}
				
				if(trim($mdata['description'])!=''){
					echo '<p class="bfSectionDescription">'."\n";
					echo $mdata['description']."\n";
					echo '</p>'."\n";
				}
				
			} else if($mdata['type'] == 'element'){
				
				$onclick = '';
				if($mdata['actionClick'] == 1){
					$onclick = 'onclick="'.$mdata['actionFunctionName'] . '(this,\'click\');" ';	
				}
				
				$onblur = '';
				if($mdata['actionBlur'] == 1){
					$onblur = 'onblur="'.$mdata['actionFunctionName'] . '(this,\'onblur\');" ';	
				}
				
				$onchange = '';
				if($mdata['actionChange'] == 1){
					$onchange = 'onchange="'.$mdata['actionFunctionName'] . '(this,\'onchange\');" ';	
				}
				
				$onfocus = '';
				if($mdata['actionFocus'] == 1){
					$onfocus = 'onfocus="'.$mdata['actionFunctionName'] . '(this,\'onfocus\');" ';	
				}
				
				$onselect = '';
				if($mdata['actionSelect'] == 1){
					$onselect = 'onselect="'.$mdata['actionFunctionName'] . '(this,\'onselect\');" ';	
				}
				
				if($mdata['bfType'] != 'bfHidden'){
					
					$labelPosition = '';
					switch($mdata['labelPosition']){
						case 'top':
							$labelPosition = ' bfLabelTop';
							break;
						case 'right':
							$labelPosition = ' bfLabelRight';
							break;
						case 'bottom':
							$labelPosition = ' bfLabelBottom';
							break;
						default:
							$labelPosition = ' bfLabelLeft';
					}
					
					if($options['displayType'] == 'breaks'){
						echo '<p class="bfElemWrap'.$labelPosition.$this->fadingClass.'" id="bfElemWrap'.$mdata['dbId'].'">'."\n";
					} else {
						echo '<span class="bfElemWrap'.$labelPosition.$this->fadingClass.'" id="bfElemWrap'.$mdata['dbId'].'">'."\n";
					}
				}
				
				if(!$mdata['hideLabel']){
					
					$labelText = htmlentities(trim($mdata['label']), ENT_QUOTES, 'UTF-8');
					if(trim($mdata['hint']) != ''){
						$labelText = JHTML::tooltip(trim($mdata['hint']), trim($mdata['label']), '', trim($mdata['label']));	
					}
					
					echo '<label for="ff_elem'.$mdata['dbId'].'">'.$labelText."</label>\n";
				}
				
				$readonly = '';
				if($mdata['readonly']){
					$readonly = 'readonly="readonly" ';
				}
				
				$tabIndex = '';
				if($mdata['tabIndex'] != -1 && is_numeric($mdata['tabIndex'])){
					$tabIndex = 'tabindex="'.intval($mdata['tabIndex']).'" ';
				}
			
				
				for($i = 0; $i < $this->p->rowcount; $i++) {
					$row = $this->p->rows[$i];
					if($mdata['bfName'] == $row->name){
						if(isset($mdata['value'])
							&& 
							( 
								$mdata['bfType'] == 'bfTextfield' ||
								$mdata['bfType'] == 'bfTextarea' ||
								$mdata['bfType'] == 'bfCheckbox' ||
								$mdata['bfType'] == 'bfSubmitButton' ||
								$mdata['bfType'] == 'bfHidden' ||
								$mdata['bfType'] == 'bfCalendar'
							)
						){
							$mdata['value'] = $this->p->replaceCode($row->data1, "data1 of " . $mdata['bfName'], 'e', $mdata['dbId'], 0);
						}
						if(isset($mdata['checked']) && $mdata['bfType'] == 'bfCheckbox'){
							$mdata['checked'] = $row->flag1 == 1 ? true : false;
						}
						if($mdata['bfType'] == 'bfCheckboxGroup' || $mdata['bfType'] == 'bfRadioGroup'){
							$mdata['group'] = $row->data2; 
						}
						if($mdata['bfType'] == 'bfSelect'){
							$mdata['list'] = $row->data2; 
						}
						break;
					}
				}

				
				switch($mdata['bfType']){
					
					case 'bfTextfield':
						$type = 'text';
						
						if($mdata['password']){
							$type = 'password';
						}
						$maxlength = '';
						if(is_numeric($mdata['maxLength'])){
							$maxlength = 'maxlength="'.intval($mdata['maxLength']).'" ';
						}
						$size = '';
						if($mdata['size']!=''){
							$size = 'style="width:'.htmlentities(strip_tags($mdata['size'])).'" ';
						}
						
						echo '<input class="ff_elem" '.$size.$tabIndex.$maxlength.$onclick.$onblur.$onchange.$onfocus.$onselect.$readonly.'type="'.$type.'" name="ff_nm_'.$mdata['bfName'].'[]" value="'.htmlentities(trim($mdata['value']), ENT_QUOTES, 'UTF-8').'" id="ff_elem'.$mdata['dbId'].'"/>'."\n";
						if($mdata['mailbackAsSender']){
							echo '<input type="hidden" name="mailbackSender['.$mdata['bfName'].']" value="true"/>'."\n";
						}
						
						break;
						
					case 'bfTextarea':
						
						$width = '';
						if($mdata['width']!=''){
							$width = 'width:'.htmlentities(strip_tags($mdata['width'])).';';
						}
						$height = '';
						if($mdata['height']!=''){
							$height = 'height:'.htmlentities(strip_tags($mdata['height'])).';';
						}
						$size = '';
						if($height != '' || $width != ''){
							$size = 'style="'.$width.$height.'" ';
						}
						echo '<textarea class="ff_elem" '.$size.$tabIndex.$onclick.$onblur.$onchange.$onfocus.$onselect.$readonly.'name="ff_nm_'.$mdata['bfName'].'[]" id="ff_elem'.$mdata['dbId'].'">'.htmlentities(trim($mdata['value']), ENT_QUOTES, 'UTF-8').'</textarea>'."\n";
						
						break;
						
					case 'bfRadioGroup':
						
						if($mdata['group'] != ''){
							$wrapOpen = '';
							$wrapClose = '';
							if(!$mdata['wrap']){
								 $wrapOpen = '<span class="bfElementGroupNoWrap">'."\n";
								 $wrapClose = '</span>'."\n";
							} else {
								$wrapOpen = '<span class="bfElementGroup">'."\n";
								$wrapClose = '</span>'."\n";
							}
							$mdata['group'] = str_replace("\r", '', $mdata['group']);
							$gEx = explode("\n", $mdata['group']);
							$lines = count($gEx);
							echo $wrapOpen;
							for($i = 0; $i < $lines; $i++){
								$idExt = $i != 0 ? '_'.$i : '';
								$iEx = explode(";", $gEx[$i]);
								$iCnt = count($iEx);
								if($iCnt == 3){
									$lblRight = '<label for="ff_elem'.$mdata['dbId'].$idExt.'">'.htmlentities(trim($iEx[1]), ENT_QUOTES, 'UTF-8').'</label>';
									$lblLeft = ''; 
									if($mdata['labelPosition'] == 'right'){
										$lblLeft = $lblRight;	
										$lblRight = '';
									}
									echo $lblLeft . '<input '.($iEx[0] == 1 ? 'checked="checked" ' : '').' class="ff_elem" '.$tabIndex.$onclick.$onblur.$onchange.$onfocus.$onselect.$readonly.'type="radio" name="ff_nm_'.$mdata['bfName'].'[]" value="'.htmlentities(trim($iEx[2]), ENT_QUOTES, 'UTF-8').'" id="ff_elem'.$mdata['dbId'].$idExt.'"/>'.$lblRight."\n";
									if($mdata['wrap']){
										echo '<br/>'."\n";
									}
								}
							}
							echo $wrapClose;
						}
						
						break;
						
					case 'bfCheckboxGroup':
						
						if($mdata['group'] != ''){
							$wrapOpen = '';
							$wrapClose = '';
							if(!$mdata['wrap']){
								 $wrapOpen = '<span class="bfElementGroupNoWrap">'."\n";
								 $wrapClose = '</span>'."\n";
							} else {
								$wrapOpen = '<span class="bfElementGroup">'."\n";
								$wrapClose = '</span>'."\n";
							}
							$mdata['group'] = str_replace("\r", '', $mdata['group']);
							$gEx = explode("\n", $mdata['group']);
							$lines = count($gEx);
							echo $wrapOpen;
							for($i = 0; $i < $lines; $i++){
								$idExt = $i != 0 ? '_'.$i : '';
								$iEx = explode(";", $gEx[$i]);
								$iCnt = count($iEx);
								if($iCnt == 3){
									$lblRight = '<label for="ff_elem'.$mdata['dbId'].$idExt.'">'.htmlentities(trim($iEx[1]), ENT_QUOTES, 'UTF-8').'</label>';
									$lblLeft = ''; 
									if($mdata['labelPosition'] == 'right'){
										$lblLeft = $lblRight;	
										$lblRight = '';
									}
									echo $lblLeft . '<input '.($iEx[0] == 1 ? 'checked="checked" ' : '').' class="ff_elem" '.$tabIndex.$onclick.$onblur.$onchange.$onfocus.$onselect.$readonly.'type="checkbox" name="ff_nm_'.$mdata['bfName'].'[]" value="'.htmlentities(trim($iEx[2]), ENT_QUOTES, 'UTF-8').'" id="ff_elem'.$mdata['dbId'].$idExt.'"/>'.$lblRight."\n";
									if($mdata['wrap']){
										echo '<br/>'."\n";
									}
								}
							}
							echo $wrapClose;
						}
						
						break;
					
					case 'bfCheckbox':
						
						echo '<input class="ff_elem" '.($mdata['checked'] ? 'checked="checked" ' : '').$tabIndex.$onclick.$onblur.$onchange.$onfocus.$onselect.$readonly.'type="checkbox" name="ff_nm_'.$mdata['bfName'].'[]" value="'.htmlentities(trim($mdata['value']), ENT_QUOTES, 'UTF-8').'" id="ff_elem'.$mdata['dbId'].'"/>'."\n";
						if($mdata['mailbackAccept']){
							echo '<input type="hidden" class="ff_elem" name="mailbackConnectWith['.$mdata['mailbackConnectWith'].']" value="true_'.$mdata['bfName'].'"/>'."\n";
						}
						
						break;
						
					case 'bfSelect':
						
						if($mdata['list'] != ''){
							
							$mdata['list'] = str_replace("\r", '', $mdata['list']);
							$gEx = explode("\n", $mdata['list']);
							$lines = count($gEx);
							echo '<select class="ff_elem" '.($mdata['multiple'] ? 'multiple="multiple" ' : '').$tabIndex.$onclick.$onblur.$onchange.$onfocus.$onselect.$readonly.'name="ff_nm_'.$mdata['bfName'].'[]" id="ff_elem'.$mdata['dbId'].'">'."\n";
							for($i = 0; $i < $lines; $i++){
								$iEx = explode(";", $gEx[$i]);
								$iCnt = count($iEx);
								if($iCnt == 3){
									echo '<option '.($iEx[0] == 1 ? 'selected="selected" ' : '').'value="'.htmlentities(trim($iEx[2]), ENT_QUOTES, 'UTF-8').'">'.htmlentities(trim($iEx[1]), ENT_QUOTES, 'UTF-8').'</option>'."\n";
								}
							}
							echo '</select>'."\n";
						}
						
						break;
						
					case 'bfFile':
						
						echo '<input class="ff_elem" '.$tabIndex.$onclick.$onblur.$onchange.$onfocus.$onselect.$readonly.'type="file" name="ff_nm_'.$mdata['bfName'].'[]" id="ff_elem'.$mdata['dbId'].'"/>'."\n";
						if($mdata['attachToAdminMail']){
							echo '<input type="hidden" name="attachToAdminMail['.$mdata['bfName'].']" value="true"/>'."\n";
						}
						if($mdata['attachToUserMail']){
							echo '<input type="hidden" name="attachToUserMail['.$mdata['bfName'].']" value="true"/>'."\n";
						}
						break;
						
					case 'bfSubmitButton':
						
						$value = '';
						$type = 'submit';
						$src = '';
						if($mdata['src'] != ''){
							$type = 'image';
							$src = 'src="'.$mdata['src'].'" ';
						}
						if($mdata['value'] != ''){
							$value = 'value="'.htmlentities(trim($mdata['value']), ENT_QUOTES, 'UTF-8').'" ';
						}
						if($mdata['actionClick'] == 1){
							$onclick = 'onclick="if(document.getElementById(\'bfPaymentMethod\')){document.getElementById(\'bfPaymentMethod\').value=\'\';};'.$mdata['actionFunctionName'] . '(this,\'click\');" ';	
						} else {
							$onclick = 'onclick="if(document.getElementById(\'bfPaymentMethod\')){document.getElementById(\'bfPaymentMethod\').value=\'\';};" ';
						}
						echo '<input class="ff_elem" '.$value.$src.$tabIndex.$onclick.$onblur.$onchange.$onfocus.$onselect.$readonly.'type="'.$type.'" name="ff_nm_'.$mdata['bfName'].'[]" id="ff_elem'.$mdata['dbId'].'"/>'."\n";
						break;
						
					case 'bfHidden':
						
						echo '<input class="ff_elem" type="hidden" name="ff_nm_'.$mdata['bfName'].'[]" value="'.htmlentities(trim($mdata['value']), ENT_QUOTES, 'UTF-8').'" id="ff_elem'.$mdata['dbId'].'"/>'."\n";
						break;
						
					case 'bfCaptcha':
						
						echo '<span class="bfCaptcha">'."\n";
						echo '<img id="ff_capimgValue" class="ff_capimg" src="'.JURI::root().'ff_secimage.php?option=com_facileforms&bfCaptcha=true"/>'."\n";
						echo '<br/>';
						echo '<input class="ff_elem" type="text" name="bfCaptchaEntry" id="bfCaptchaEntry" />'."\n";
						echo '<a href="#" class="ff_elem" onclick="document.getElementById(\'bfCaptchaEntry\').value=\'\';document.getElementById(\'bfCaptchaEntry\').focus();document.getElementById(\'ff_capimgValue\').src = \''.JURI::root().'ff_secimage.php?option=com_facileforms&bfCaptcha=true?\' + Math.random(); return false"><img alt="captcha" src="'.JURI::root().'components/com_facileforms/images/captcha/refresh-captcha.png" border="0" /></a>'."\n";
						echo '</span>'."\n";
						
						break;
						
					case 'bfCalendar':
					
						$size = '';
						if($mdata['size']!=''){
							$size = 'style="width:'.htmlentities(strip_tags($mdata['size'])).'" ';
						}
						
						echo '<span class="bfElementGroupNoWrap">'."\n";
						echo '<input class="ff_elem" '.$size.'type="text" name="ff_nm_'.$mdata['bfName'].'[]"  id="ff_elem'.$mdata['dbId'].'" value=""/>'."\n";
						echo '<input onclick="showCalendar(\'ff_elem'.$mdata['dbId'].'\', \''.$mdata['format'].'\');" type="submit" class="bfCalendar" value="'.htmlentities(trim($mdata['value']), ENT_QUOTES, 'UTF-8').'"/>'."\n";
						echo '</span>'."\n";
						
						break;	
						
					case 'bfPayPal':
						
						$value = '';
						$type = 'submit';
						$src = '';
						if($mdata['image'] != ''){
							$type = 'image';
							$src = 'src="'.$mdata['image'].'" ';
						}else{
							$value = 'value="PayPal" ';
						}
						if($mdata['actionClick'] == 1){
							$onclick = 'onclick="document.getElementById(\'bfPaymentMethod\').value=\'PayPal\';'.$mdata['actionFunctionName'] . '(this,\'click\');" ';	
						} else {
							$onclick = 'onclick="document.getElementById(\'bfPaymentMethod\').value=\'PayPal\';" ';
						}
						echo '<input class="ff_elem" '.$value.$src.$tabIndex.$onclick.$onblur.$onchange.$onfocus.$onselect.$readonly.'type="'.$type.'" name="ff_nm_'.$mdata['bfName'].'[]" id="ff_elem'.$mdata['dbId'].'"/>'."\n";
						break;
						
					case 'bfSofortueberweisung':
						
						$value = '';
						$type = 'submit';
						$src = '';
						if($mdata['image'] != ''){
							$type = 'image';
							$src = 'src="'.$mdata['image'].'" ';
						}else{
							$value = 'value="Sofortueberweisung" ';
						}
						if($mdata['actionClick'] == 1){
							$onclick = 'onclick="document.getElementById(\'bfPaymentMethod\').value=\'Sofortueberweisung\';'.$mdata['actionFunctionName'] . '(this,\'click\');" ';	
						} else {
							$onclick = 'onclick="document.getElementById(\'bfPaymentMethod\').value=\'Sofortueberweisung\';" ';
						}
						echo '<input class="ff_elem" '.$value.$src.$tabIndex.$onclick.$onblur.$onchange.$onfocus.$onselect.$readonly.'type="'.$type.'" name="ff_nm_'.$mdata['bfName'].'[]" id="ff_elem'.$mdata['dbId'].'"/>'."\n";
						break;
				}
				
				if($mdata['required']){
					echo '<span class="bfRequired">*</span>'."\n";
				}
				
				if($mdata['bfType'] != 'bfHidden'){
					if($options['displayType'] == 'breaks'){
						echo '</p>'."\n";
					} else {
						echo '</span>'."\n";
					}
				}
			}
		}

		/**
		 * Paging and wrapping of inline element containers
		 */
		
		if($dataObject['properties']['type'] == 'section' && $dataObject['properties']['displayType'] == 'inline'){
			echo '<div class="bfClearfix">'."\n";
		}
		
		if(isset($dataObject['children']) && count($dataObject['children']) != 0){
			$childrenAmount = count($dataObject['children']);
			for($i = 0; $i < $childrenAmount; $i++){
				$this->process( $dataObject['children'][$i], $mdata, $parentPage, $i, $childrenAmount );
			}
		}	
		
		if($dataObject['properties']['type'] == 'section' && $dataObject['properties']['displayType'] == 'inline'){
			echo '</div>'."\n";
		}
		
		if($dataObject['properties']['type'] == 'section' && $dataObject['properties']['bfType'] == 'section'){
			
			echo '</fieldset>'."\n";
			
		} else if($dataObject['properties']['type'] == 'page'){

			$isLastPage = false;
			if($this->rootMdata['lastPageThankYou'] && $dataObject['properties']['pageNumber'] == count($this->dataObject['children']) && count($this->dataObject['children']) > 1){
				$isLastPage = true;
			}
			
			if(!$isLastPage){
			
				$last = 0;
				if($this->rootMdata['lastPageThankYou']){
					$last = 1;
				}
				
				if($this->rootMdata['pagingInclude'] && $dataObject['properties']['pageNumber'] > 1){
					echo '<input class="bfPrevButton'.$this->fadingClass.'" type="submit" onclick="ff_validate_prevpage(this, \'click\');" value="'.htmlentities(trim($this->rootMdata['pagingPrevLabel']), ENT_QUOTES, 'UTF-8').'"/>'."\n";
				}
	
				if($this->rootMdata['pagingInclude'] && $dataObject['properties']['pageNumber'] < count($this->dataObject['children']) - $last){
					echo '<input class="bfNextButton'.$this->fadingClass.'" type="submit" onclick="ff_validate_nextpage(this, \'click\');" value="'.htmlentities(trim($this->rootMdata['pagingNextLabel']), ENT_QUOTES, 'UTF-8').'"/>'."\n";
				}
	
				if($this->rootMdata['cancelInclude'] && $dataObject['properties']['pageNumber'] + 1 > count($this->dataObject['children']) - $last){
					echo '<input class="bfCancelButton'.$this->fadingClass.'" type="submit" onclick="ff_resetForm(this, \'click\');"  value="'.htmlentities(trim($this->rootMdata['cancelLabel']), ENT_QUOTES, 'UTF-8').'"/>'."\n";
				}
				
				if($this->rootMdata['submitInclude'] && $dataObject['properties']['pageNumber'] + 1 > count($this->dataObject['children']) - $last){
					echo '<input class="bfSubmitButton'.$this->fadingClass.'" type="submit" onclick="if(document.getElementById(\'bfPaymentMethod\')){document.getElementById(\'bfPaymentMethod\').value=\'\';};ff_validate_submit(this, \'click\');" value="'.htmlentities(trim($this->rootMdata['submitLabel']), ENT_QUOTES, 'UTF-8').'"/>'."\n";
				}
			
			}
		}
	}
	
	public function render(){
		echo '<style>
.bfClearfix:after {
content: "."; 
display: block; 
height: 0;  
clear: both; 
visibility: hidden;
}
.bfInline{
float:left;
}
.bfFadingClass{
display:none;
}
.bfSelected{
background-color: red;
}
</style>'."\n";
		$this->process($this->dataObject);
		echo '</div>'."\n"; // closing last page
		echo '<script>if(document.getElementById("bfPage'.$this->p->page.'"))document.getElementById("bfPage'.$this->p->page.'").style.display = "";</script>'."\n";
		echo '<noscript>Please turn on javascript to submit your data. Thank you!</noscript>'."\n";
	}
	
}