<?php
/*
 * \SentraDesigner\fills\Form\render.scripts\pscript.vtd.php
 * Rendering script for form preview
 */
If (defined('ErrorLogging_ALL')) {
	error_reporting(E_ALL);		// trap everything!
	ini_set('display_errors', TRUE);
	ini_set('html_errors', TRUE);
}
If (!defined('SENTRA_START_TIME'))
	define('SENTRA_START_TIME',"Test");
/* Define some functions if server is running under Windows */
If (substr(PHP_OS,0,3) == 'WIN') {
	If (!function_exists('getmxrr')) {
		function getmxrr($hostname, &$mxhosts) {
  		$mxhosts = array();
  		exec('nslookup -type=mx '.$hostname, $result_arr);
  		foreach($result_arr as $line) {
				if (preg_match("/.*mail exchanger = (.*)/", $line, $matches))
					$mxhosts[] = $matches[1];
  	  }
  	 	return( count($mxhosts) > 0 );
		}//--End of workaround
	}
}

$TotalWidth = '100';
$LeftWidth 	= '30';
$RightWidth = '70';
require(DESIGNRENDERSCRIPTS.'formgriddy.widthes.php');



If (file_exists(DESIGNRENDERSCRIPTS.'formgriddy.class.php')) {
	require_once(DESIGNRENDERSCRIPTS."formgriddy.class.php");
} Else {
	require_once("./php_scripts/formgriddy.class.php");
}
$FormGriddy = new FormGriddy(array($LeftWidth,$RightWidth),$TotalWidth);

require_once("form_file_io.php");
If (file_exists(DESIGNRENDERSCRIPTS.'form.class.functions.php')) {
	require_once (DESIGNRENDERSCRIPTS.'form.class.functions.php');
} Else {
	require_once("form.class.functions.php");
}
require_once("../../common/gen.form.class.php");
$_SERVER['SERVER_NAME'] = "DUMMY";
// Create or retrieve the unique UserID which is an ID allocated to a certain visitor
// Collect user input vars from POST & GET ports
$GETPOSTvars = array();
$GETPOSTvars = array_merge($_POST, $_GET);
//file_put_contents('T:/temp/I_am_NOT_ONLINE.txt',__FILE__.': Zeile: '.__LINE__.PHP_EOL, FILE_APPEND);

	$UserID=md5(uniqid(rand(),true));
	$UserID = str_replace(".","",form_get_real_ip())."__".md5(uniqid(rand(),true));

// Handover some defines via formpara
$formpara['FORMsID'] 				= "{{ID}}";
$formpara['uniqueID'] 			= "{{UNIQUE}}";
$formpara['UPLOAD'] 				= "savedupload";
$formpara['THUMBFILEPREFIX']= "Thumb.";
$formpara['UserID'] 				= $UserID;
$formpara['SelectMode'] 		= 'standard';
$formpara['RadioMode']			= 'fancy';
$formpara['CheckMode']			= 'fancy';
$formpara['thumb_width'] 		= '64';
$formpara['form_tpl'] 			= '
<!--
	\SentraDesigner\fills\Form\Formtpl\form.tpl

	Template with form frame work
 	-->
<if name="Compile">
<div id="fmw{{form_ID}}" class="wells wells-{{fmwstyle}} hidden">
	{{WarningText}}
</div>
<else />
	<if name="ShowWarning">
<div id="fmw{{form_ID}}" class="wells wells-{{fmwstyle}}">
	<else />
<div id="fmw{{form_ID}}" class="wells wells-{{fmwstyle}} hidden">
	</if>
	{{WarningText}}
</div>
</if>
<form data-piwik-ignore class="form{{formclass}}" id="{{form_ID}}" name="{{formname}}" enctype="{{formenctype}}" method="{{formmethod}}" action="{{formaction}}">
{{FieldSets}}
</form>

<script src="{{ValidatePass}}jquery.validate.js"></script>
<script>
{{validation}}
$("#{{form_ID}}").validate({
	rules:{
		captcha_value:{
			required:true,
			remote:"{{captchacheckPath}}captcha-check.php"
		}
	},
	errorLabelContainer: $("#fmw{{form_ID}}")

});
</script>

<!--
	END:: Template with form frame work
 	-->
';

$GLOBALS['FormError'] = $FormError;/* $FormError is filled by plugin_render.php */

	// *********************************************************************************************
	// *********************************************************************************************
	//                                        Main Program
	// *********************************************************************************************
	// *********************************************************************************************
		$form = new vtdHTML5form($formXMLdata, $formpara, $FormGriddy);

	// Fill user input array with dummy values in order to prevent failures during rendering
		$nTABs = count($formXMLdata['page'][0]['tab']);
		If ($nTABs > 6) $nTABs = 6;
		$user_input = $formXMLdata;
		for ($it=0; $it < $nTABs; $it++) {
			$user_input['page'][0]['tab'][$it]['status'] = TRUE;
			for ($fs=0; $fs < count($formXMLdata['page'][0]['tab'][$it]['fieldset']); $fs++) {
				$user_input['page'][0]['tab'][$it]['fieldset'][$fs] = $form -> form_prefill_fieldset_user_input(
								                                              $user_input['page'][0]['tab'][$it]['fieldset'][$fs],
								                                              $formXMLdata['page'][0]['tab'][$it]['fieldset'][$fs]);
			}
		}
//file_put_contents('T:/temp/I_am_NOT_ONLINE.txt',__FILE__.': Zeile: '.__LINE__.PHP_EOL.PHP_EOL, FILE_APPEND);

	// 2. Render form
		$formHTML = $form -> form_rendering($user_input);
?>