<?php
/**
 * Avalanche for Magento 1.8+
 * Designed by Fast Division (http://fastdivision.com)
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the EULA
 * that is bundled with this package in the file license.txt.
 * It is also available through the world-wide-web at this URL:
 * http://fastdivision.com/legal/license.txt
 *
 * @author     Fast Division
 * @version    1.5.0
 * @copyright  Copyright 2013 Fast Division
 * @license    http://fastdivision.com/legal/license.txt
 */
?>

<script type="text/javascript">
//<![CDATA[
    i=0;
    var recipCount  = 1;
    var maxRecip    = <?php echo $this->getMaxRecipients() ?>;
    function remove_recipient(i){
        $('recipients_name'+i).up(2).remove();
        recipCount--;
        if(recipCount<maxRecip && maxRecip != 0) {
            $('add_recipient_button').show();
            $('max_recipient_message').hide();
        }
        return false;
    }

    function add_recipient(){
        ul = $('recipients_options');
        var li_mail = Element.extend(document.createElement("LI"));
        li_mail.addClassName('fields additional-row');
        li_mail.innerHTML = '<p><a href="delete_email" title="<?php echo $this->jsQuoteEscape($this->__('Remove Email')) ?>" onclick="remove_recipient('+i+'); return false" class="btn-remove"><?php echo $this->jsQuoteEscape($this->__('Remove Email')) ?><\/a><\/p>';
        li_mail.innerHTML += '<div class="field"><label for="recipients_name'+i+'" class="required"><?php echo $this->__('Name') ?><em>*<\/em><\/label><div class="input-box"><input name="recipients[name][]" type="text" class="input-text required-entry" id="recipients_name'+i+'" /><\/div>';
        li_mail.innerHTML += '<div class="field"><label for="recipients_email'+i+'" class="required"><?php echo $this->__('Email') ?><em>*<\/em><\/label><div class="input-box"><input name="recipients[email][]" value="" title="<?php echo $this->__('Email') ?>" id="recipients_email'+i+'" type="text" class="input-text required-entry validate-email" /><\/div><\/div>';
        i++;
        recipCount++;
        if(recipCount>=maxRecip && maxRecip != 0) {
            $('add_recipient_button').hide();
            $('max_recipient_message').show();
        }

        ul.appendChild(li_mail);
    }
//]]>
</script>

<?php echo $this->getMessagesBlock()->getGroupedHtml() ?>

<div class="grid_12 white-box">
	<h1><?php echo $this->__('Email to a Friend') ?></h1>
	<form action="<?php echo $this->getSendUrl() ?>" method="post" id="product_sendtofriend_form" class="site-form">
		<div class="clearfix">
			<div class="grid_6 omega">
				<h3><?php echo $this->__('Sender:') ?></h3>
				<div class="inner-box">		
				    <div class="fieldset">
				        <?php echo $this->getBlockHtml('formkey')?>
				        <ul class="form-list" id="sender_options">
				            <li class="fields">
				                <div class="field">
				                    <label for="sender_name" class="required"><?php echo $this->__('Name') ?><em>*</em></label>
				                    <div class="input-box">
				                        <input name="sender[name]" value="<?php echo $this->escapeHtml($this->getUserName()) ?>" title="<?php echo $this->__('Name') ?>" id="sender_name" type="text" class="input-text required-entry" />
				                    </div>
				                </div>
				                <div class="field">
				                   <label for="sender_email" class="required"><?php echo $this->__('Email') ?><em>*</em></label>
				                   <div class="input-box">
				                       <input name="sender[email]" value="<?php echo $this->escapeHtml($this->getEmail()) ?>" title="<?php echo $this->__('Email') ?>" id="sender_email" type="text" class="input-text required-entry validate-email" />
				                   </div>
				                </div>
				            </li>
				            <li class="wide">
				                <label for="sender_message" class="required"><?php echo $this->__('Message') ?><em>*</em></label>
				                <div class="input-box">
				                    <textarea name="sender[message]" class="input-text required-entry" id="sender_message" cols="50" rows="6"><?php echo $this->escapeHtml($this->getMessage())?></textarea>
				                </div>
				            </li>
				        </ul>
				    </div>
				    <div class="buttons-set">
				        <button type="submit" class="action-button<?php if (!$this->canSend()):?> disabled<?php endif ?>"<?php if (!$this->canSend()):?> disabled="disabled"<?php endif ?>><span><?php echo $this->__('Send Email') ?></span></button>
				    </div>
				</div>
			</div>
			<div class="grid_6 omega">
				<h3><?php echo $this->__('Recipient:') ?></h3>
				<div class="inner-box">
				    <div class="fieldset">
				        <ul class="form-list" id="recipients_options">
				            <li class="fields">
				                <div class="field">
				                    <label for="recipients_name" class="required"><?php echo $this->__('Name') ?><em>*</em></label>
				                    <div class="input-box">
				                        <input name="recipients[name][]" type="text" class="input-text required-entry" id="recipients_name" />
				                    </div>
				                </div>
				                <div class="field">
				                    <label for="recipients_email" class="required"><?php echo $this->__('Email') ?><em>*</em></label>
				                    <div class="input-box">
				                        <input name="recipients[email][]" value="" title="<?php echo $this->__('Email Address') ?>" id="recipients_email" type="text" class="input-text required-entry validate-email" />
				                    </div>
				                </div>
				            </li>
				        </ul>
				    </div>
					<div id="max_recipient_message" style="display:none;">
			            <?php if ($this->getMaxRecipients()): ?>
			              <p class="limit"><?php echo $this->__('Maximum %d email addresses allowed.', $this->getMaxRecipients()) ?></p>
			            <?php endif; ?>
			        </div>
					<?php if (1 < $this->getMaxRecipients()): ?>
			            <p id="add_recipient_button">
			                <button type="button" onclick="add_recipient();" class="button"><span><span><?php echo $this->__('Add Recipient') ?></span></span></button>
			            </p>
			        <?php endif; ?>
				</div>
			</div>
		</div>
		<div class="box-action">
			<p class="back-link"><a href="#" onclick="history.back(); return false;"><small>&laquo; </small><?php echo $this->__('Back to Product') ?></a></p>
		</div>
		<script type="text/javascript">
		//<![CDATA[
		    var productSendtofriendForm = new VarienForm('product_sendtofriend_form');
		    productSendtofriendForm.submit = function() {
		        if(this.validator.validate()) {
		            this.form.submit();
		        }
		    }.bind(productSendtofriendForm);
		//]]>
		</script>
	</form>
</div>