<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE_AFL.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category    design
 * @package     default_default
 * @copyright   Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
 * @license     http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */
?>
<?php $_htmlId      = $this->getElement()->getHtmlId() ?>
<?php $_htmlClass   = $this->getElement()->getClass() ?>
<?php $_htmlName    = $this->getElement()->getName() ?>
<?php $_multiWebsite= $this->isMultiWebsites(); ?>
<tr>
    <td class="label"><?php echo $this->getElement()->getLabel() ?></td>
    <td colspan="10" class="grid weee">
    <table cellspacing="0" class="data border" id="<?php echo $_htmlId; ?>_table">
        <?php if ($_multiWebsite): ?>
        <col width="135" />
        <?php endif; ?>
        <col width="140" />
        <col />
        <col width="1" />
        <thead>
            <tr class="headings">
                <th <?php if (!$_multiWebsite): ?>style="display:none"<?php endif; ?>><?php echo Mage::helper('sales')->__('Website') ?></th>
                <th><?php echo Mage::helper('catalog')->__('Country/State') ?> <span class="required">*</span></th>
                <th><?php echo Mage::helper('catalog')->__('Tax') ?> <span class="required">*</span></th>
                <th class="last"><?php echo Mage::helper('catalog')->__('Action') ?></th>
            </tr>
            <tr id="<?php echo $_htmlId ?>_add_template" class="template no-display">
                <td <?php if (!$_multiWebsite): ?>style="display:none"<?php endif; ?>>
                <select disabled="no-template" class="<?php echo $_htmlClass ?> required-entry" name="<?php echo $_htmlName ?>[__index__][website_id]" id="#{prefix}_weee_tax_row___index___website">
                    <?php foreach ($this->getWebsites() as $_websiteId => $_info): ?>
                    <option value="<?php echo $_websiteId ?>"><?php echo $_info['name'] ?><?php if (!empty($_info['currency'])): ?> [<?php echo $_info['currency'] ?>]<?php endif; ?></option>
                    <?php endforeach ?>
                </select>
                </td>
                <td class="nobr">
                <select disabled="no-template" class="<?php echo $_htmlClass ?> country required-entry" name="<?php echo $_htmlName ?>[__index__][country]" id="#{prefix}_weee_tax_row___index___country">
                    <?php foreach ($this->getCountries() as $_country): ?>
                    <option value="<?php echo $_country['value'] ?>"><?php echo htmlspecialchars($_country['label']) ?></option>
                    <?php endforeach ?>
                </select>
                <br />
                <select disabled="no-template" class="<?php echo $_htmlClass ?> state" name="<?php echo $_htmlName ?>[__index__][state]" id="#{prefix}_weee_tax_row___index___state">
                    <option value="0">*</option>
                </select>
                </td>
                <td>
                    <input disabled="no-template" class="<?php echo $_htmlClass ?> required-entry validate-greater-than-zero" type="text" name="<?php echo $_htmlName ?>[__index__][price]" value="'#{price}'" />
                </td>
                <td class="last">
                    <input type="hidden" name="<?php echo $_htmlName ?>[__index__][delete]" class="delete" disabled="no-template" value=""  id="#{prefix}_weee_tax_row___index___delete" />
                    <button title="Delete WEEE Tax" class="scalable delete icon-btn delete-product-option" onclick="weeeTaxControl.deleteItem('<?php echo $_htmlId ?>', event);return false"><span><span><span>Delete</span></span></span></button>
                </td>
            </tr>
        </thead>
        <tfoot>
            <tr>
                <td <?php if (!$_multiWebsite): ?>style="display:none"<?php endif; ?>></td>
                <td colspan="3" class="a-right"><?php echo $this->getAddButtonHtml() ?></td>
            </tr>
        </tfoot>
        <tbody id="<?php echo $_htmlId ?>_container">
        </tbody>
    </table>

<script type="text/javascript">
//<![CDATA[
    if (typeof itemsCount == 'undefined') {
        var itemsCount = 0;
    }
    var weeeTaxControl = {
        deleteButton: false,
        addItem : function () {
            var data = {};
            data.prefix = '';
            data.website_id = 0;
            data.country    = '';
            data.state      = '';
            data.price      = '';
            data.index      = itemsCount++;
            if(arguments.length == 5) {
                data.prefix     = arguments[0];
                data.website_id = arguments[1];
                data.country    = arguments[2];
                data.state      = arguments[3];
                data.price      = arguments[4];
            } else if (arguments.length == 1) {
                data.prefix     = arguments[0];
            }

            var template = new Template('<tr>' + $(data.prefix + '_add_template').innerHTML.replace(/__index__/g, '#{index}').replace(/ disabled="?no-template"?/g, '').replace(/ disabled/g, '').replace(/="'([^']*)'"/g, '="$1"') + '</tr>');
            Element.insert($(data.prefix + '_container'), {'bottom':template.evaluate(data)});

            $(data.prefix + '_weee_tax_row_'+data.index+'_country').value = data.country;
            $(data.prefix + '_weee_tax_row_'+data.index+'_website').value = data.website_id;

            updater = new RegionUpdater(data.prefix + '_weee_tax_row_'+data.index+'_country', null, data.prefix + '_weee_tax_row_'+data.index+'_state', <?php echo $this->helper('directory')->getRegionJson() ?>, 'disable', true);
            updater.update();

            $(data.prefix + '_weee_tax_row_'+data.index+'_state').value   = data.state;
        },

        deleteItem : function(prefix, event) {
            var tr = Event.findElement(event, 'tr');
            if (tr) {
                Element.select(tr, '.delete').each(function(elem){elem.value='1'});
                Element.select(tr, ['input', 'select']).each(function(elem){elem.hide()});
                Element.hide(tr);
                Element.addClassName(tr, 'no-display template');
            }
        },

        priceTypeChangeEventObserver : function() {
            val = $('price_type').options[$('price_type').selectedIndex].value;
            for (i=0;i<weeeEditorPrefixes.length;i++) {
                elem = $(weeeEditorPrefixes[i] + '_table');

                if (val != 1) {
                    elem.up(1).hide();
                    weeeTaxControl.disableEditElements(weeeEditorPrefixes[i]);
                } else {
                    weeeTaxControl.enableEditElements(weeeEditorPrefixes[i]);
                    elem.up(1).show();
                }
            }
        },

        bindPriceTypeChangeObserver : function() {
            if ($('price_type')) {
                Event.observe($('price_type'), 'change', weeeTaxControl.priceTypeChangeEventObserver);
                weeeTaxControl.priceTypeChangeEventObserver();
            }
        },

        enableEditElements : function (prefix) {
            nodes = $(prefix + '_container').childNodes;
            for (a=0;a<nodes.length;a++) {
                tr = nodes[a];
                Element.select(tr, ['input', 'select']).each(function(elem){
                    elem.disabled=weeeEditorState[elem.id];
                    if (elem.id.indexOf('delete') != -1) {
                        elem.value = ((typeof weeeEditorDeleted[elem.id] == undefined || !weeeEditorDeleted[elem.id]) ? 0 : 1);
                    }
                });
            }
        },

        disableEditElements : function (prefix) {
            nodes = $(prefix + '_container').childNodes;
            for (b=0;b<nodes.length;b++) {
                tr = nodes[b];
                Element.select(tr, ['input', 'select']).each(function(elem){
                    if (elem.id.indexOf('delete') != -1) {
                        weeeEditorDeleted[elem.id]=(elem.value?elem.value:0);
                        elem.value = 1;
                    }
                    weeeEditorState[elem.id]=elem.disabled;elem.disabled=true;
                });
            }
        }
    }
    <?php foreach ($this->getValues() as $_item): ?>
    weeeTaxControl.addItem('<?php echo $_htmlId ?>', '<?php echo $_item['website_id'] ?>', '<?php echo $_item['country'] ?>', '<?php echo $_item['state'] ?>', '<?php echo sprintf('%.2f', $_item['value']) ?>');
    <?php endforeach; ?>

    if (typeof weeeEditorPrefixes == 'undefined') {
        var weeeEditorPrefixes = new Array();
        var weeeEditorState = new Array();
        var weeeEditorDeleted = new Array();
        Event.observe(window, 'load', weeeTaxControl.bindPriceTypeChangeObserver);
    }
    weeeEditorPrefixes.push('<?php echo $_htmlId ?>');
//]]>
</script>
</td>
</tr>
