<?php
/**
 * OpenMage
 *
 * 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 at https://opensource.org/license/afl-3-0-php
 *
 * @category    design
 * @package     rwd_default
 * @copyright   Copyright (c) 2006-2020 Magento, Inc. (https://www.magento.com)
 * @copyright   Copyright (c) 2021-2022 The OpenMage Contributors (https://www.openmage.org)
 * @license     https://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */
?>
<?php $prefix = $this->getFieldNamePrefix();?>
<div class="fieldset">
    <ul class="form-list form-list-narrow">
    <li id="<?php echo $prefix ?>-address-form" class="address-form">
        <div class="fieldset">
            <input type="hidden" name="<?php echo $prefix ?>[address_id]" value="<?php echo $this->getAddress()->getId() ?>" id="<?php echo $prefix ?>:address_id" />
            <ul>
                <li class="fields"><?php echo $this->getLayout()->createBlock('customer/widget_name')->setObject($this->getAddress()->getFirstname() ? $this->getAddress() : $this->getQuote()->getCustomer())->setForceUseCustomerRequiredAttributes(!$this->isCustomerLoggedIn())->setFieldIdFormat($prefix . ':%s')->setFieldNameFormat($prefix . '[%s]')->toHtml() ?></li>
                <li class="fields">
                    <div class="field">
                        <label for="<?php echo $prefix ?>:company"><?php echo $this->__('Company') ?></label>
                        <div class="input-box">
                            <input type="text" id="<?php echo $prefix ?>:company" name="<?php echo $prefix ?>[company]" value="<?php echo $this->escapeHtml($this->getAddress()->getCompany()) ?>" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Company')) ?>" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('company') ?>" />
                        </div>
                    </div>
        <?php if(!$this->isCustomerLoggedIn() && !$this->getHideEmailAddress()): ?>
                    <div class="field">
                        <label for="<?php echo $prefix ?>:email" class="required"><em>*</em><?php echo $this->__('Email Address') ?></label>
                        <div class="input-box">
                            <input type="email" autocapitalize="off" autocorrect="off" spellcheck="false" name="<?php echo $prefix ?>[email]" id="<?php echo $prefix ?>:email" value="<?php echo $this->escapeHtml($this->getAddress()->getEmail()) ?>" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Email Address')) ?>" class="input-text validate-email required-entry" />
                        </div>
                    </div>
        <?php endif ?>
                </li>
        <?php $_streetValidationClass = $this->helper('customer/address')->getAttributeValidationClass('street'); ?>
                <li class="fields">
                    <div class="field">
                        <label for="<?php echo $prefix ?>:street1" class="required"><em>*</em><?php echo $this->__('Address') ?></label>
                        <div class="input-box">
                            <input type="text" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Street Address')) ?>" name="<?php echo $prefix ?>[street][]" id="<?php echo $prefix ?>:street1" value="<?php echo $this->escapeHtml($this->getAddress()->getStreet(1)) ?>" class="input-text <?php echo $_streetValidationClass ?>" />
                        </div>
                    </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="fields">
                    <div class="field">
                        <label for="billing:street<?php echo $_i ?>"><?php echo $this->__('Street Address %s', $_i) ?></label>
                        <div class="input-box">
                            <input type="text" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Street Address %s', $_i)) ?>" name="<?php echo $prefix ?>[street][]" id="<?php echo $prefix ?>:street<?php echo $_i?>" value="<?php echo $this->escapeHtml($this->getAddress()->getStreet($_i)) ?>" class="input-text <?php echo $_streetValidationClass ?>" />
                        </div>
                    </div>
                </li>
        <?php endfor ?>
                <?php if ($this->helper('customer/address')->isVatAttributeVisible()) : ?>
                <li class="fields">
                    <div class="field">
                        <label for="<?php echo $prefix ?>:vat_id"><?php echo $this->__('VAT Number') ?></label>
                        <div class="input-box">
                            <input type="text" id="<?php echo $prefix ?>:vat_id" name="<?php echo $prefix ?>[vat_id]" value="<?php echo $this->escapeHtml($this->getAddress()->getVatId()) ?>" title="<?php echo Mage::helper('core')->quoteEscape($this->__('VAT Number')) ?>" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('vat_id') ?>" />
                        </div>
                    </div>
                </li>
                <?php endif ?>
                <li class="fields">
                    <div class="field">
                        <label for="<?php echo $prefix ?>:city" class="required"><em>*</em><?php echo $this->__('City') ?></label>
                        <div class="input-box">
                            <input type="text" title="<?php echo Mage::helper('core')->quoteEscape($this->__('City')) ?>" name="<?php echo $prefix ?>[city]" value="<?php echo $this->escapeHtml($this->getAddress()->getCity()) ?>" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('city') ?>" id="<?php echo $prefix ?>:city" />
                        </div>
                    </div>
                    <div class="field">
                        <label for="<?php echo $prefix ?>:region_id" class="required"><em>*</em><?php echo $this->__('State/Province') ?></label>
                        <div class="input-box">
                            <select id="<?php echo $prefix ?>:region_id" name="<?php echo $prefix ?>[region_id]" title="<?php echo Mage::helper('core')->quoteEscape($this->__('State/Province')) ?>" class="validate-select" style="display:none;">
                                <option value=""><?php echo $this->__('Please select region, state or province') ?></option>
                            </select>
                            <script type="text/javascript">
                            //<![CDATA[
                                $('<?php echo $prefix ?>:region_id').setAttribute('defaultValue',  "<?php echo $this->getAddress()->getRegionId() ?>");
                            //]]>
                            </script>
                            <input type="text" id="<?php echo $prefix ?>:region" name="<?php echo $prefix ?>[region]" value="<?php echo $this->escapeHtml($this->getAddress()->getRegion()) ?>"  title="<?php echo Mage::helper('core')->quoteEscape($this->__('State/Province')) ?>" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('region') ?>" style="display:none;" />
                        </div>
                    </div>
                </li>
                <li class="fields">
                    <div class="field">
                        <label for="<?php echo $prefix ?>:postcode" class="required"><em>*</em><?php echo $this->__('Zip/Postal Code') ?></label>
                        <div class="input-box">
                            <input type="text" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Zip/Postal Code')) ?>" name="<?php echo $prefix ?>[postcode]" id="<?php echo $prefix ?>:postcode" value="<?php echo $this->escapeHtml($this->getAddress()->getPostcode()) ?>" class="input-text validate-zip-international <?php echo $this->helper('customer/address')->getAttributeValidationClass('postcode') ?>" />
                        </div>
                    </div>
                    <div class="field">
                        <label for="<?php echo $prefix ?>:country_id" class="required"><em>*</em><?php echo $this->__('Country') ?></label>
                        <div class="input-box">
                            <?php echo $this->getCountryHtmlSelect($prefix) ?>
                        </div>
                    </div>
                </li>
                <li class="fields">
                    <div class="field">
                        <label for="<?php echo $prefix ?>:telephone" class="required"><em>*</em><?php echo $this->__('Telephone') ?></label>
                        <div class="input-box">
                            <input type="tel" name="<?php echo $prefix ?>[telephone]" value="<?php echo $this->escapeHtml($this->getAddress()->getTelephone()) ?>" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Telephone')) ?>" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('telephone') ?>" id="<?php echo $prefix ?>:telephone" />
                        </div>
                    </div>
                    <div class="field">
                        <label for="<?php echo $prefix ?>:fax"><?php echo $this->__('Fax') ?></label>
                        <div class="input-box">
                            <input type="tel" name="<?php echo $prefix ?>[fax]" value="<?php echo $this->escapeHtml($this->getAddress()->getFax()) ?>" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Fax')) ?>" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('fax') ?>" id="<?php echo $prefix ?>:fax" />
                        </div>
                    </div>
                </li>
                <?php echo $this->getChildHtml('form.additional.info'); ?>
                <?php if ($this->getShowAsShippingCheckbox()): ?>
                    <li class="control">
                        <div class="field">
                            <div class="input-box">
                                <input type="checkbox" name="<?php echo $prefix ?>[as_shipping]" class="checkbox" value="1" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Same as shipping')) ?>" id="<?php echo $prefix ?>:as_shipping" <?php if ($this->getAddress()->getSameAsBilling()):?>checked="checked"<?php endif ?>/>
                            </div>
                            <label for="<?php echo $prefix ?>:as_shipping"><?php echo $this->__('Same as shipping') ?></label>
                        </div>
                    </li>
                <?php endif ?>
            </ul>
        </div>
     </li>
    </ul>
</div>
<script type="text/javascript">
//<![CDATA[
    var <?php echo $prefix ?>RegionUpdater = new RegionUpdater('<?php echo $prefix ?>:country_id', '<?php echo $prefix ?>:region', '<?php echo $prefix ?>:region_id', <?php echo Mage::helper('directory')->getRegionJsonByStore() ?>, undefined, '<?php echo $prefix ?>:postcode');
    <?php echo $prefix ?>RegionUpdater.update();
//]]>
</script>
<?php if ($this->getShowAsShippingCheckbox()): ?>
<script type="text/javascript">
    //<![CDATA[
    OrderReviewController.prototype._copyShippingToBilling_parent = OrderReviewController.prototype._copyShippingToBilling;
    OrderReviewController.prototype._copyShippingToBilling = function (event) {
        this._copyShippingToBilling_parent(event);

        if (!this._copyElement) {
            return;
        }

        if (this._copyElement.checked) {
            if (this.copyShippingToBilling_allowScrollTo && Modernizr.mq && Modernizr.mq("only screen and (max-width: 767px)")) {
                $('shipping-address').scrollTo();
            }
        } else {
            $$('[id^="billing:"]').each(function (el) {
                if ($(el).up('.field')) {
                    $(el).up('.field').removeClassName('inactive');
                }
            })
        }
    }

    OrderReviewController.prototype._copyElementValue_parent = OrderReviewController.prototype._copyElementValue;
    OrderReviewController.prototype._copyElementValue = function (el) {
        <?php // Note: We cannot call this._copyElementValue_parent because review.js is using each() to iterate without giving it context ?>
        OrderReviewController.prototype._copyElementValue_parent(el);

        var newId = el.id.replace('shipping:','billing:');
        if (newId && $(newId) && $(newId).type != 'hidden') {
            if ($(newId).up('.field')) {
                $(newId).up('.field').addClassName('inactive');
            }
        }
    }
    //]]>
</script>
<?php endif ?>
