--- SF_FormPrinter.php	2012-10-28 13:51:50.000000000 +0100
+++ SF_FormPrinter-new.php	2012-10-28 13:56:10.000000000 +0100
@@ -229,92 +229,7 @@
 		return '@insertHTML_' . $str . '@';
 	}
 
-	/**
-	 * Creates the HTML for the inner table for every instance of a
-	 * multiple-instance template in the form.
-	 */
-	function multipleTemplateInstanceTableHTML( $form_is_disabled, $mainText ) {
-		global $sfgTabIndex, $sfgScriptPath;
-
-		$attributes = array(
-			'tabindex' => $sfgTabIndex,
-			'class' => 'remover',
-		);
-
-		$rearranger = 'class="rearrangerImage"';
-
-		if ( $form_is_disabled ) {
-			$attributes['disabled'] = 'disabled';
-			$rearranger = '';
-		}
-
-		$removeButton = Html::input( null, wfMsg( 'sf_formedit_remove' ), 'button', $attributes );
-
-		$text = <<<END
-			<table>
-			<tr>
-			<td>$mainText</td>
-			<td class="removeButton">$removeButton</td>
-			<td class="instanceRearranger">
-			<img src="$sfgScriptPath/skins/rearranger.png" $rearranger />
-			</td>
-			</tr>
-			</table>
-END;
-
-		return $text;
-	}
-
-	/**
-	 * Creates the HTML for a single instance of a multiple-instance template;
-	 * plus the end tags for the full multiple-instance HTML.
-	 */
-	function multipleTemplateInstanceHTML( $form_is_disabled, $all_instances_printed, &$section, $instance_num, $add_button_text ) {
-		global $sfgTabIndex;
 
-		if ( ! $all_instances_printed ) {
-			// Add the character "a" onto the instance number of this input
-			// in the form, to differentiate the inputs the form starts out
-			// with from any inputs added by the Javascript.
-			$section = str_replace( '[num]', "[{$instance_num}a]", $section );
-
-			$text = "\t\t" . Html::rawElement( 'div',
-				array(
-					// The "multipleTemplate" class is there for
-					// backwards-compatibility with any custom CSS on people's
-					// wikis before SF 2.0.9.
-					'class' => "multipleTemplateInstance multipleTemplate"
-				),
-				$this->multipleTemplateInstanceTableHTML( $form_is_disabled, $section )
-			) . "\n";
-
-		} else { // if ( $all_instances_printed ) {
-			// This is the last instance of this
-			// template - print all the sections
-			// necessary for adding additional
-			// instances.
-			$text = "\t\t" . Html::rawElement( 'div',
-				array(
-					'class' => "multipleTemplateStarter",
-					'style' => "display: none",
-				),
-				$this->multipleTemplateInstanceTableHTML( $form_is_disabled, $section )
-			) . "\n";
-
-			$attributes = array(
-				'tabindex' => $sfgTabIndex,
-				'class' => 'multipleTemplateAdder',
-			);
-			if ( $form_is_disabled ) $attributes['disabled'] = true;
-			$button = Html::input( null, Sanitizer::decodeCharReferences( $add_button_text ), 'button', $attributes );
-			$text .= <<<END
-	</div><!-- multipleTemplateList -->
-		<p>$button</p>
-	</div><!-- multipleTemplateWrapper -->
-END;
-		}
-		return $text;
-	}
 
 	/**
 	 * This function is the real heart of the entire Semantic Forms
@@ -550,8 +465,10 @@
 						// multiple template form's HTML into the main form's HTML.
 						// So, the HTML will be stored in $multipleTemplateString.
 						if ( $allow_multiple ) {
-							$multipleTemplateString .= "\t" . '<div class="multipleTemplateWrapper">' . "\n";
-							$multipleTemplateString .= "\t" . '<div class="multipleTemplateList">' . "\n";
+							//we want to wrap here
+							$multipleTemplateInstance = SFMultipleTemplate::getInstance($template_name);//,$form_is_disabled,$section,$instance_num);
+							$multipleTemplateInstance->setDisabled($form_is_disabled);
+							$multipleTemplateString.=$multipleTemplateInstance->beforeHTML();
 						}
 					}
 					if ( $curPlaceholder == null ) {
@@ -587,6 +504,7 @@
 							// the same tags, but I don't know what it is.
 							if ( $found_instance ) {
 								$instance_num++;
+								$multipleTemplateInstance->increment();
 							} else {
 								$all_instances_printed = true;
 							}
@@ -734,6 +652,7 @@
 					$allow_multiple = false;
 					$all_instances_printed = false;
 					$instance_num = 0;
+					$multipleTemplateInstance->instance = 0;
 				// =====================================================
 				// field processing
 				// =====================================================
@@ -1426,16 +1345,36 @@
 			}
 
 			if ( $allow_multiple ) {
+				$multipleTemplateInstance->setSection($section);
 				if ( $curPlaceholder == null ) {
 					// The normal process.
-					$form_text .= $this->multipleTemplateInstanceHTML( $form_is_disabled, $all_instances_printed, $section, $instance_num, $add_button_text );
+
+					// If there are still instances to add, add them, otherwise add the hidden template fields
+					// and add the add button
+					if(!$all_instances_printed){
+						$form_text .= $multipleTemplateInstance->itemHTML();
+					}else{
+						//if all the instances have been added
+						$form_text .= $multipleTemplateInstance->adderHTML();
+						$form_text .= $multipleTemplateInstance->afterHTML();
+					}
+					
 				} else { // if ( $curPlaceholder != null ){
 					// The template text won't be appended at the end of the template like for usual multiple template forms.
 					// The HTML text will then be stored in the $multipleTemplateString variable,
 					// and then added in the right @insertHTML_".$placeHolderField."@"; position
 					// Optimization: actually, instead of separating the processes, the usual multiple
 					// template forms could also be handled this way if a fitting placeholder tag was added.
-					$multipleTemplateString .= $this->multipleTemplateInstanceHTML( $form_is_disabled, $all_instances_printed, $section, $instance_num, $add_button_text );
+
+					// If there are still instances to add, add them, otherwise add the hidden template fields
+					// and add the add button
+					if(!$all_instances_printed){
+						$multipleTemplateString .= $multipleTemplateInstance->itemHTML();
+					}else{
+						$multipleTemplateString .= $multipleTemplateInstance->adderHTML();
+						$multipleTemplateString .= $multipleTemplateInstance->afterHTML();
+					}
+
 					// We replace the $multipleTemplateString HTML into the
 					// current placeholder tag, but also add another
 					// placeholder tag, to keep track of it.
