<?php
/**
 * Feel free to contact me via Facebook
 * http://www.facebook.com/rebimol
 *
 *
 * @author 		Vladimir Popov
 * @copyright  	Copyright (c) 2011 Vladimir Popov
 * @license    	http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */

if(Mage::registry('webform')->getIsActive() == VladimirPopov_WebForms_Model_Webforms::STATUS_DISABLED){
	echo Mage::helper('webforms')->__('Web-form is not active.');
} else {

if(Mage::registry('show_form_name')){?>
<div class="page-title">
	<h1><?php echo Mage::registry('webform')->getName();?></h1>
</div>
<?php } ?>

<?php
if(Mage::registry('show_success')){?>
<div class="std">
	<?php echo Mage::registry('webform')->getSuccessText()?>
</div>
<?php } else {
?>

<div class="std">
	<?php echo Mage::registry('webform')->getDescription();?>
</div>

<form action="<?php echo Mage::helper('core/url')->getCurrentUrl()?>" method="post" name="webform_<?php echo Mage::registry('webform')->getId()?>" id="webform_<?php echo Mage::registry('webform')->getId()?>">

	<input type="hidden" name="submitWebform_<?php echo Mage::registry('webform')->getId()?>" value="1"/>

	<?php
	foreach (Mage::registry('fields_to_fieldsets') as $fieldset){	
	?>
	
	<div class="fieldset">
		
		<?php	if($fieldset['name']) { 	?>
		<h2 class="legend"><?php echo $fieldset['name']?></h2>
		<?php	}	?>
		
		<ul class="form-list">
		<?php
		$k=0;
		$j=0;
		$prev = false;
		$fields_count =  count($fieldset['fields']);
		foreach($fieldset['fields'] as $field)
		{
			
			$size_class ="fields";
			$field_class= "field";
			if($field->getSize()=='wide'){
				$size_class = "wide";
				$field_class="";
			}
			
		?>
		<?php if ($j%2 ==0 || $field->getSize()=='wide' || $prev->getSize()=='wide') { $j=0;?>
			<?php if ($k>1) {?></li><?php }?>
			<li class="<?php echo $size_class?>">
		<?php }?>
				<div class="<?php echo $field_class?>">

					<label for="field_<?php echo $field->getId()?>" <?php if($field->getRequired()){ ?>class="required"<?php }?>>

						<?php if($field->getRequired()){ ?> 
							<em>*</em>
						<?php }?>

						<?php echo $field->getName()?>

					</label>
					
					<div class="input-box">
						<?php	echo $field->toHtml();?>
					</div>

				</div>
		<?php if($k == $fields_count-1){?>
			</li>
		<?php } ?>
		<?php 
			$k++; $j++;
			$prev = $field;
		}?>
		</ul>
		
	</div>
	
	<?php
	} // foreach 'fields_to_fieldsets'
	?>
	
	<?php if(Mage::registry('use_captcha')) { ?>
	<div class="fieldset">
		<h2 class="legend">Captcha</h2>
		<ul class="form-list">
			<li class="fields">
				<div class="field">
					<?php echo $this->getCaptcha()->getHTML()?>
				</div>
			</li>
		</ul>
	</div>
	<?php }?>
	
	<div class="buttons-set">
		<p class="required"><?php echo $this->__('* Required Fields')?></p>
		<button type="submit" class="button" title="<?php echo $this->__('submit')?>">
			<span>
				<span><?php echo $this->__('Submit')?></span>
			</span>
		</button>
	</div>
	

</form>

<script type="text/javascript">
//<![CDATA[
var webform_<?php echo Mage::registry('webform')->getId()?> = new VarienForm('webform_<?php echo Mage::registry('webform')->getId()?>',true);
//]]>
</script>

<?php
} // if(show_success)
} // is active
?>
