<?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_default
 * @package    Mage
 * @copyright  Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
 * @license    http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */
?>
<div class="content-header">
   <h3 class="icon-head head-newsletter-list"><?php echo $this->getHeaderText() ?></h3>
   <p class="content-buttons form-buttons">
                <?php echo $this->getBackButtonHtml(); ?>
                <?php echo $this->getResetButtonHtml(); ?>
                <?php if(!$this->isTextType()): ?>
                <?php echo $this->getToPlainButtonHtml(); ?>
                <?php echo $this->getToHtmlButtonHtml(); ?>
                <?php endif ?>
                <?php echo $this->getSaveButtonHtml(); ?>
                <?php if($this->getEditMode()): ?>
                <?php echo $this->getSaveAsButtonHtml(); ?>
                <?php endif ?>
                <?php echo $this->getPreviewButtonHtml(); ?>
                <?php if($this->getEditMode()): ?>
                <?php echo $this->getDeleteButtonHtml(); ?>
                <?php endif ?>
    </p>
</div>
<form action="<?php echo $this->getSaveUrl() ?>" method="post" id="newsletter_template_edit_form">
<div class="no-display">
    <input type="hidden" id="change_flag_element" name="_change_type_flag" value="" />
    <input type="hidden" id="save_as_flag" name="_save_as_flag" value="<?php echo $this->getSaveAsFlag() ?>" />
</div>
<?php echo $this->getForm() ?>
</form>
<form action="<?php echo $this->getPreviewUrl() ?>" method="post" id="newsletter_template_preview_form" target="_blank">
<div class="no-display">
    <input type="hidden" id="preview_type" name="type" value="<?php echo $this->isTextType()?1:2 ?>" />
    <input type="hidden" id="preview_text" name="text" value="" />
</div>
</form>
<script type="text/javascript">
//<![CDATA[
    var templateForm = new varienForm('newsletter_template_edit_form');
    var templatePreviewForm = new varienForm('newsletter_template_preview_form');
    var templateControl = {
        unconvertedText: '',
        typeChange: false,
        templateName: false,
        isEditor: false,
        init: function () {
        	if ($('convert_button_back')) {
        		$('convert_button_back').hide();
        	}
        },
        stripTags: function () {
            if(!window.confirm("<?php echo Mage::helper('newsletter')->__('Are you sure that you want to strip all tags?') ?>")) {
                return false;
            }
            if(this.isEditor) {
            	tinyMCE.execCommand('mceRemoveControl', false, 'text');
            }
            this.unconvertedText = $('text').value;
            $('convert_button').hide();
            $('text').value =  $('text').value.stripScripts().stripTags();
            $('convert_button_back').show();
            this.typeChange = true;
            return false;
        },
        unStripTags: function () {
            $('convert_button').show();
            $('convert_button_back').hide();
            $('text').value =  this.unconvertedText;

            this.typeChange = false;
            return false;
        },
        save: function() {
            if (this.typeChange) {
                $('change_flag_element').value = '1';
            }
            if(this.isEditor) {
            	tinyMCE.triggerSave();
            }
            templateForm.submit();
            return false;
        },
        saveAs: function() {
            if (this.typeChange) {
                $('change_flag_element').value = '1';
            }

            if($F('code').blank() || $F('code')==templateControl.templateName) {
               value = prompt('<?php echo Mage::helper('newsletter')->__('Please enter new template name') ?>', templateControl.templateName + '<?php echo Mage::helper('newsletter')->__(' Copy') ?>');
               if(!value) {
                   if(value !== null) {
                       $('code').value = '';
                       templateForm.submit();
                   }
                   return false;
               } else {
                   $('code').value = value;
               }
            }

            $('save_as_flag').value = '1';

            if(this.isEditor) {
            	tinyMCE.triggerSave();
            }
            templateForm.submit();
            return false;
        },
        preview: function() {
            if (this.typeChange) {
                $('preview_type').value = 1;
            }
            if (typeof tinyMCE == 'undefined' || !tinyMCE.getInstanceById('text')) {
                $('preview_text').value = $('text').value;
            } else {
                 $('preview_text').value = tinyMCE.getInstanceById('text').getHTML();
            }
            templatePreviewForm.submit();
            return false;
        },

        deleteTemplate: function() {
            if(window.confirm("<?php echo Mage::helper('newsletter')->__('Are you sure that you want to delete this template?') ?>")) {
               	window.location.href = '<?php echo $this->getDeleteUrl() ?>';
            }
        }
    };

    templateControl.init();
    templateControl.templateName = "<?php echo $this->getJsTemplateName() ?>";
//]]>
</script>
