<?php
/**
 * Maho
 *
 * @category    design
 * @package     rwd_default
 * @copyright   Copyright (c) 2006-2020 Magento, Inc. (https://magento.com)
 * @copyright   Copyright (c) 2021-2022 The OpenMage Contributors (https://openmage.org)
 * @copyright   Copyright (c) 2024 Maho (https://mahocommerce.com)
 * @license     https://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */
?>
<script type="text/javascript">countryRegions = <?= Mage::helper('directory')->getRegionJsonByStore() ?></script>

<div class="page-title">
    <h1><?php if($data->getAddressId()): ?><?= $this->__('Edit Address Entry') ?><?php else: ?><?= $this->__('New Address Entry') ?><?php endif ?></h1>
</div>
<?= $this->getMessagesBlock()->toHtml() ?>
<form action="<?= $action ?>" method="post" id="form-validate" class="scaffold-form">
    <div class="fieldset">
    <input type="hidden" name="address_id" value="<?= $data->getAddressId() ?>" />
    <input type="hidden" name="customer_id" id="address_id" value="<?= $data->getCustomerId() ?>" />
        <h2 class="legend"><?= $this->__('Personal Information') ?></h2>
        <p class="required"><?= $this->__('* Required Fields') ?></p>
        <ul class="form-list">
            <li class="fields">
                <?= $this->getLayout()->createBlock('customer/widget_name')->setObject($data)->toHtml() ?>
            </li>
            <li>
                <label for="company"><?= $this->__('Company') ?></label>
                <div class="input-box">
                    <input type="text" name="company" id="company" title="<?= Mage::helper('core')->quoteEscape($this->__('Company')) ?>" value="<?= $this->escapeHtml($data->getCompany()) ?>" class="input-text <?= $this->helper('customer/address')->getAttributeValidationClass('company') ?>" />
                </div>
            </li>
        </ul>
    </div>
    <div class="fieldset">
        <h2 class="legend"><?= $this->__('Address') ?></h2>
        <ul class="form-list">
        <?php $_streetValidationClass = $this->helper('customer/address')->getAttributeValidationClass('street'); ?>
            <li class="wide">
                <label for="street_1" class="required"><em>*</em><?= $this->__('Street Address') ?></label>
                <div class="input-box">
                    <input type="text" name="street[]" value="<?= $this->escapeHtml($data->getStreet(1)) ?>" title="<?= Mage::helper('core')->quoteEscape($this->__('Street Address')) ?>" id="street_1" class="input-text <?= $_streetValidationClass ?>" />
                </div>
            </li>
        <?php $_streetValidationClass = trim(str_replace('required-entry', '', $_streetValidationClass)); ?>
        <?php for ($_i = 2, $_n = $this->helper('customer/address')->getStreetLines(); $_i <= $_n; $_i++): ?>
            <li class="wide">
                <label for="billing:street<?= $_i ?>"><?= $this->__('Street Address %s', $_i) ?></label>
                <div class="input-box">
                    <input type="text" name="street[]" value="<?= $this->escapeHtml($data->getStreet($_i)) ?>" title="<?= Mage::helper('core')->quoteEscape($this->__('Street Address %s', $_i)) ?>" id="street_<?= $_i ?>" class="input-text <?= $_streetValidationClass ?>" />
                </div>
            </li>
        <?php endfor ?>
            <li class="fields">
                <div class="field">
                    <label for="city" class="required"><em>*</em><?= $this->__('City') ?></label>
                    <div class="input-box">
                        <input type="text" name="city" value="<?= $this->escapeHtml($data->getCity()) ?>" title="<?= Mage::helper('core')->quoteEscape($this->__('City')) ?>" class="input-text <?= $this->helper('customer/address')->getAttributeValidationClass('city') ?>" id="city" />
                    </div>
                </div>
                <div class="field">
                    <label for="region_id" class="required"><em>*</em><?= $this->__('State/Province') ?></label>
                    <div class="input-box">
                        <select id="region_id" name="region_id" title="<?= Mage::helper('core')->quoteEscape($this->__('State/Province')) ?>" class="validate-select" style="display:none;">
                            <option value=""><?= $this->__('Please select region, state or province') ?></option>
                        </select>
                        <script type="text/javascript">
                            document.getElementById('region_id').setAttribute('defaultValue', "<?= $this->getAddress()->getRegionId() ?>");
                        </script>
                        <input type="text" id="region" name="region" value="<?= $this->escapeHtml($this->getAddress()->getRegion()) ?>"  title="<?= Mage::helper('core')->quoteEscape($this->__('State/Province')) ?>" class="input-text <?= $this->helper('customer/address')->getAttributeValidationClass('region') ?>" style="display:none;" />
                    </div>
                </div>
            </li>
            <li class="fields">
                <div class="field">
                    <label for="zip" class="required"><em>*</em><?= $this->__('Zip/Postal Code') ?></label>
                    <div class="input-box">
                        <input type="text" name="postcode" value="<?= $this->escapeHtml($data->getPostcode()) ?>" title="<?= Mage::helper('core')->quoteEscape($this->__('Zip/Postal Code')) ?>" id="zip" class="input-text validate-zip-international <?= $this->helper('customer/address')->getAttributeValidationClass('postcode') ?>" />
                    </div>
                </div>
                <div class="field">
                    <label for="country" class="required"><em>*</em><?= $this->__('Country') ?></label>
                    <div class="input-box">
                        <select name="country_id" id="country" title="<?= Mage::helper('core')->quoteEscape($this->__('Country')) ?>" class="validate-select">
                            <?= $countries->toHtmlOptions($data->getCountryId()) ?>
                        </select>
                    </div>
                </div>
            </li>
            <li class="fields">
                <div class="field">
                    <label for="telephone" class="required"><em>*</em><?= $this->__('Telephone') ?></label>
                    <div class="input-box">
                        <input type="tel" name="telephone" value="<?= $this->escapeHtml($data->getTelephone()) ?>" title="<?= Mage::helper('core')->quoteEscape($this->__('Telephone')) ?>" class="input-text <?= $this->helper('customer/address')->getAttributeValidationClass('telephone') ?>" id="telephone" />
                    </div>
                </div>
                <div class="field">
                    <label for="fax"><?= $this->__('Fax') ?></label>
                    <div class="input-box">
                        <input type="tel" name="fax" value="<?= $this->escapeHtml($data->getFax()) ?>" title="<?= Mage::helper('core')->quoteEscape($this->__('Fax')) ?>" class="input-text <?= $this->helper('customer/address')->getAttributeValidationClass('fax') ?>" id="fax" />
                    </div>
                </div>
            </li>
        <?php foreach ($primaryTypes as $code=>$type): ?>
            <li<?php if (!$address->isPrimary($type['address_type_id'])) echo ' class="control"' ?>>
            <?php if ($address->isPrimary($type['address_type_id'])): ?>
                <strong><?= $this->__("This is My Default %s Address", ucfirst($type['name'])) ?></strong>
            <?php else: ?>
                <input type="checkbox" id="primary_<?= $code ?>" name="primary_types[]" value="<?= $type['address_type_id'] ?>" class="checkbox" /><label for="primary_<?= $code ?>"><?= $this->__("Use as My Default %s Address", ucfirst($type['name'])) ?></label>
            <?php endif ?>
            </li>
        <?php endforeach ?>
        </ul>
    </div>
    <div class="buttons-set">
        <p class="back-link"><a href="<?= $this->getUrl('customer/address/') ?>"><small>&laquo; </small><?= $this->__('Back') ?></a></p>
        <button type="submit" class="button" title="<?= Mage::helper('core')->quoteEscape($this->__('Save Address')) ?>"><?= $this->__('Save Address') ?></button>
    </div>
</form>
<script type="text/javascript">
    var dataForm = new VarienForm('form-validate', true);
    new RegionUpdater('country', 'region', 'region_id', countryRegions, undefined, 'zip');
</script>
