<?php
/**
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/osl-3.0.php
 *
 * @category   BL
 * @package    BL_CustomGrid
 * @copyright  Copyright (c) 2012 Benoît Leulliette <benoit.leulliette@gmail.com>
 * @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */
?>
<?php $_jsObjectName = $this->getEditorJsObjectName() ?>
<?php if ($this->getErrorMessage()): ?>
    <?php echo $this->getErrorMessage() ?>
    <script type="text/javascript">
    //<![CDATA[
    document.observe('dom:loaded', function(){
        <?php if ($_jsObjectName): ?>
        if (typeof(<?php echo $_jsObjectName ?>) != 'undefined') {
            <?php echo $_jsObjectName ?>.cancelEdit(false, '<?php echo $this->jsQuoteEscape($this->getErrorMessage()) ?>');
        } else {
            alert('<?php echo $this->jsQuoteEscape($this->__('Can\'t find editor JS object')) ?>');
        }
        <?php else: ?>
            alert('<?php echo $this->jsQuoteEscape($this->__('Can\'t find editor JS object')) ?>');
        <?php endif ?>
    });
    //]]>
    </script>
<?php else: ?>
    <?php echo $this->getFormInitScripts() ?>
    <p class="form-buttons blcg-column-editor-form-buttons"><?php echo $this->getButtonsHtml('header') ?></p>
    <div class="blcg-column-editor-form">
        <?php echo $this->getFormHtml() ?>
    </div>
    <?php if ($this->hasFooterButtons()): ?>
        <div class="content-footer">
            <p class="form-buttons"><?php echo $this->getButtonsHtml('footer') ?></p>
        </div>
    <?php endif; ?>
    <script type="text/javascript">
    //<![CDATA[
        blcgGridEditForm = new varienForm('<?php echo $this->getFormElementId() ?>');
        blcgGridEditForm.submit = function(url){
            if (typeof(varienGlobalEvents) != 'undefined') {
                varienGlobalEvents.fireEvent('blcgGridEditFormStartSubmit', this.formId);
            }
            if (this.validator && this.validator.validate()) {
                <?php if ($_jsObjectName): ?>
                if (typeof(<?php echo $_jsObjectName ?>) != 'undefined') {
                    if (typeof(varienGlobalEvents) != 'undefined') {
                        varienGlobalEvents.fireEvent('formSubmit', this.formId);
                    }
                    
                    this.errorSections = $H({});
                    this.canShowError  = true;
                    this.submitUrl = url;
                    
                    if (typeof(tinyMceEditors) != 'undefined') {
                        tinyMCE.triggerSave();
                    }
                    if (typeof(varienGlobalEvents) != 'undefined') {
                        varienGlobalEvents.fireEvent('blcgGridEditFormLaunchSubmit', this.formId);
                    }
                    
                    <?php echo $_jsObjectName ?>.validateEdit($(this.formId).serialize(true));
                } else {
                    alert('<?php echo $this->jsQuoteEscape($this->__('Can\'t save value : editor JS object can\'t be found')) ?>');
                }
                <?php else: ?>
                    alert('<?php echo $this->jsQuoteEscape($this->__('Can\'t save value : editor JS object can\'t be found')) ?>');
                <?php endif ?>
            }
            return false;
        }.bind(blcgGridEditForm);
    //]]>
    </script>
    <?php echo $this->getFormScripts() ?>
<?php endif ?>