<?php
/**
 * Systempay V2-Payment Module version 1.7.1 for Magento 1.4-1.9. Support contact : supportvad@lyra-network.com.
 *
 * NOTICE OF LICENSE
 *
 * This source file is licensed under the Open Software License version 3.0
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/osl-3.0.php
 *
 * @category  payment
 * @package   systempay
 * @author    Lyra Network (http://www.lyra-network.com/)
 * @copyright 2014-2017 Lyra Network and contributors
 * @license   http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */
?>

<?php
$_htmlId = $this->getHtmlId() ? $this->getHtmlId() : uniqid('_' . $this->getElement()->getId() . '_');
?>

<div id="<?php echo $this->getElement()->getId() ?>" class="grid">
    <table cellspacing="0" cellpadding="0" class="border" id="grid<?php echo $_htmlId ?>">
        <tbody>
            <tr class="headings" id="headings<?php echo $_htmlId ?>">
                <?php foreach ($this->_columns as $columnName => $column): ?>
                    <th><?php echo $column['label'] ?></th>
                <?php endforeach; ?>
            </tr>
        </tbody>
    </table>

    <input type="hidden" name="<?php echo $this->getElement()->getName() ?>[__empty]" value="" />
</div>

<script type="text/javascript">
//<![CDATA[
    // create row creator
    var arrayRow<?php echo $_htmlId ?> = {
        // define row prototypeJS template
        template : new Template(
            '<tr id="#{_id}">'
                <?php foreach ($this->_columns as $columnName => $column): ?>
                    +'<td>'
                        +'<?php echo $this->_renderCellTemplate($columnName) ?>'
                    +'<\/td>'
                <?php endforeach; ?>
            +'<\/tr>'
        ),

        rowsCount : 0,

        add : function(templateData, insertAfterId) {
            // generate default template data
            if ('' == templateData) {
                var d = new Date();
                var templateData = {
                    <?php foreach ($this->_columns as $columnName => $column): ?>
                        <?php echo $columnName ?> : '',
                    <?php endforeach; ?>
                    _id : '_' + d.getTime() + '_' + d.getMilliseconds()
                };
            }

            // insert after headings row
            if ('' == insertAfterId) {
                Element.insert($('headings<?php echo $_htmlId ?>'), {after: this.template.evaluate(templateData)});
            }
            // insert after specified row
            else {
                Element.insert($(insertAfterId), {after: this.template.evaluate(templateData)});
            }

            this.rowsCount += 1;
        }
    };

    // add existing rows
    <?php
    $_addAfterId = "headings{$_htmlId}";
    foreach ($this->getArrayRows() as $_rowId => $_row) {
        echo "arrayRow{$_htmlId}.add(" . $_row->toJson() . ", '{$_addAfterId}');\n";
        $_addAfterId = $_rowId;
    }
    ?>

    // toggle the grid, if element is disabled (depending on scope)
    <?php if ($this->getElement()->getDisabled()): ?>
    document.observe("dom:loaded", function() {
        toggleValueElements({checked: true}, $('<?php echo $this->getElement()->getId() ?>').parentNode);
    });
    <?php endif; ?>
//]]>
</script>