<?php

class renderFormClass extends renderClass_abstract
{
	private $_formData = array();

	public function createForm($formData)
	{
		$arrFields = explode(';', $formData['content']);
		$fieldCounter = 0;

		$new_node = $this->_tpl->createDocumentFragment();
		$node = $this->_tpl->createElement('form');
		$node->setAttributeNode(new DOMAttr ('action', $formData['action']));
		$node->setAttributeNode(new DOMAttr ('method', $formData['method']));
		$node->setAttributeNode(new DOMAttr ('class', $formData['class']));

		if (!is_null($this->_errors))
		{
			foreach ($this->_errors as $error)
			{
				$errorNode = $this->_tpl->createElement('p');
				$errorNode->setAttributeNode(new DOMAttr ('class', 'error'));
				$errorNode->appendChild(new DOMText ($this->_langArray['error'][$error]));
				$node->appendChild($errorNode);
			}

		}

		foreach ($arrFields as $singleField)
		{
			$arrData = explode(':', $singleField);
			$arrDefinition = explode('_', $arrData[0]);
			$arrValue = explode('_', $arrData[1]);

			/* Inputs */
			if ($arrDefinition[0] === 'input' && ($arrDefinition[1] !== 'hidden' && $arrDefinition[1] !== 'submit'))
			{
				$tmp_div = $this->_tpl->createElement('div');
				++$fieldCounter;
				if ($fieldCounter % 2 === 1)
				{
					$tmp_div->setAttributeNode(new DOMAttr ('class', 'left'));
				}
			}

			$tmp_node = $this->_tpl->createElement($arrDefinition[0]);

			if ($arrDefinition[0] === 'input')
			{
				$tmp_node->setAttributeNode(new DOMAttr ('type', $arrDefinition[1]));
				$tmp_node->setAttributeNode(new DOMAttr ('id', $arrValue[0]));
				$tmp_node->setAttributeNode(new DomAttr ('name', $arrValue[0]));
				$tmp_node->setAttributeNode(new DomAttr ('autocomplete', 'off'));

				if ($arrDefinition[1] !== 'hidden' && $arrDefinition[1] !== 'submit' && isset ($this->_postData[$arrValue[0]]))
				{
					$tmp_node->setAttributeNode(new DomAttr ('value', $this->_postData[$arrValue[0]]));
				}
			}

			if ($arrDefinition[1] === 'submit' && isset ($this->_langArray['form'][$arrValue[0]]))
			{
				$tmp_node->setAttributeNode(new DOMAttr ('value', $this->_langArray['form'][$arrValue[0]]));
			}
			elseif (isset ($arrValue[1]))
			{
				$tmp_node->setAttributeNode(new DOMAttr ('value', $arrValue[1]));
			}
			elseif (isset ($this->_langArray['formText'][$arrValue[0]]))
			{
				$tmp_node->appendChild(new DOMText ($this->_langArray['formText'][$arrValue[0]]));
			}

			if ($arrDefinition[0] === 'input' && ($arrDefinition[1] !== 'hidden' && $arrDefinition[1] !== 'submit'))
			{
				$tmp_div->appendChild($tmp_node);

				$tmp_node = $this->_tpl->createElement('label');
				$tmp_node->setAttributeNode(new DOMAttr ('for', $arrValue[0]));
				$tmp_node->appendChild(new DOMText ($this->_langArray['form'][$arrValue[0]]));
				$tmp_div->appendChild($tmp_node);

				$node->appendChild($tmp_div);
				if ($fieldCounter % 2 === 0)
				{
					$tmp_br = $this->_tpl->createElement('br');
					$tmp_br->setAttributeNode(new DOMAttr ('class', 'fix'));
					$node->appendChild($tmp_br);
				}

			}
			else
			{
				$node->appendChild($tmp_node);
			}

		}
		$new_node->appendChild($node);

		return $new_node;
	}


}