Magento: Display Subcategories of current category

Display subcategories of the current category if it has sub categories.

If it doesn't have the sub categories then display parent category with its sub categories.

Category structure will be displayed upto two child levels.

If there is no current category register then it will not show up any list.

[php]

<?php
$currentCat = Mage::registry('current_category');
if (!$currentCat) {
return;
}

$curCatId = $currentCat->getId();

/* @var $currentCat Mage_Catalog_Model_Category */
$categories = $currentCat->getChildrenCategories();
if(count($categories) == 0) {
if ( $currentCat->getParentId() != Mage::app()->getStore()->getRootCategoryId() ) {
$currentCat = Mage::getModel('catalog/category')->load($currentCat->getParentId());
}
}
$categories = $currentCat->getChildrenCategories();
?>
<?php if(count($categories) > 0): ?>
<div class="block left-categorys">
<div class="block-title"><h2><?php echo $currentCat->getName(); ?></h2></div>
<div class="block-content left-categorys-container">
<ul class="left-main-nav">
<?php foreach ( $categories as $subCategory ): ?>
<?php if($subCategory->getIsActive()): ?>
<li <?php if($curCatId == $subCategory->getId()): ?> class="active"<?php endif; ?>>

<a href="<?php echo $subCategory->getURL(); ?>"><?php echo $subCategory->getName(); ?></a>

<?php $subCategories = $subCategory->getChildrenCategories(); ?>
<?php if(count($subCategories) > 0): ?>
<ul class="left-sub-nav">
<?php foreach ( $subCategories as $subSubCategory ): ?>
<?php if($subSubCategory->getIsActive()): ?>
<li><a href="<?php echo $subSubCategory->getURL(); ?>"><?php echo $subSubCategory->getName(); ?></a></li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
<?php endif; ?>

</li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
</div>
</div>
<?php endif; ?>

[/php]