{% extends "site/layout/base_text.html" %}
{% load i18n %}
{% load com %}

{% block head %}
<script type="text/javascript">
<!--

function check_save(){
    needToConfirm = false;

    var newVersion = $('#id_new_version').attr('checked') ;
    var commentsKept = $('#id_keep_comments').attr('checked') ;

    var new_content = $('#id_content').val()
    var new_format = $('#id_format').val()
    
    if (commentsKept) {
        var pre_edit_url = tb_conf['pre_edit_url'] ;

        $.ajax({
           url: pre_edit_url,
           type:'POST',
           dataType:"json",
           data: { "new_content": new_content,  "new_format": new_format},
           success: function(obj){
               nb_removed = obj['nb_removed'];
               if (newVersion) {
                    if (nb_removed == 0) {
                        $('#edit_form').submit();
                    }
                    else {
                        var message = "{% blocktrans %}{nb_comments} comment(s) will be removed because the text they apply to has been changed. Do you want to continue?{% endblocktrans %}".replace("{nb_comments}", ""+nb_removed);
                        $('#check_save_dialog').html(message) ;
                        $('#check_save_dialog').dialog('open') ;
                    }
               }
               else {                  
                   if (nb_removed == 0) {
                        $('#edit_form').submit();
                    }
                   else {
                        var message = "{% blocktrans %}{nb_comments} comment(s) will be lost, do you want to continue?{% endblocktrans %}".replace("{nb_comments}", ""+nb_removed);
                        message = message + "<br />" + "({% blocktrans %}We suggest you create a new version{% endblocktrans %})" ;
                       $('#check_save_dialog').html(message) ;
                       $('#check_save_dialog').dialog('open') ;
                   }
               }
           },
           error: function(msg){
               alert("error: " + msg);
           }
        });
    }
    else {
        if (!newVersion) {
                var message = "{% blocktrans %}Since you chose not to create a new version all comments will be deleted, do you want to continue?{% endblocktrans %}" ;
                $('#check_save_dialog').html(message) ;
                $('#check_save_dialog').dialog('open') ;
        }
        else {
            $('#edit_form').submit();
        }
    }
}

$(function() {
	var buttons = {};
	buttons[gettext('No')] = function() {$(this).dialog('close');} ;
	buttons[gettext('Yes')] = function() {$(this).dialog('close');$('#edit_form').submit();} ;

    $('#check_save_dialog').dialog({
        bgiframe: true, 
        autoOpen: false,        
        title :gettext('Warning'),
        modal: true,
        buttons:buttons
    }) ;
    
    $("#save").click(function() { check_save() ;}) ;
}) ;
-->
</script>
{% endblock %}

{% block main %}
    {% include "site/macros/text_editor.html" %}

<script type="text/javascript">
<!--
tb_conf['current_tab'] = 'edit';
// keep get parameters!! 
tb_conf['pre_edit_url'] = '{% url text-preedit text.key %}?{{ request.GET.urlencode }}';
-->
</script>

<div style="display: none;">
    <div id="check_save_dialog"></div>
</div>

<div id="text_edit" class="tab-meta">
<form id="edit_form" class="text-form wizard-form" action="." method="post">
    <table class="wide_form">
        <tbody>
            {% include "site/macros/form_fields.html" %}
    <tr>
        <td style="vertical-align: top; width: 20%; text-align:right;">
        </td>
        <td>
            <label></label>
            <input name="save" id="save" type="button" value="{% blocktrans %}Save{% endblocktrans %}"/>            
        </td>
    </tr>
        </tbody>
    </table>
</form>
</div>

{% endblock %}
