<mt:setvarblock name="system_msg" append="1">
<mt:if name="request.error">
  <mtapp:statusmsg
     id="generic-error"
     class="error"
     can_close="0">
    <mt:var name="error">
  </mtapp:statusmsg>
</mt:if>
<mt:if name="request.saved">
  <mtapp:statusmsg
     id="saved"
     class="success">
    <__trans phrase="You have added [_1] to your address book." params="<strong><$mt:var name="saved" escape="html"$></strong>">
  </mtapp:statusmsg>
</mt:if>
<mt:if name="request.saved_deleted">
  <mtapp:statusmsg
     id="saved-deleted"
     class="success">
    <__trans phrase="You have successfully deleted the selected contacts from your address book.">
  </mtapp:statusmsg>
</mt:if>
</mt:setvarblock>

<mt:setvarblock name="content_header" append="1">
<form class="create-inline" id="create-inline-notification" style="display: none;">
    <mtapp:setting
       id="email"
       required="1"
       label="<__trans phrase="Email">">
      <input type="text" name="email" id="email" class="text required email" value="<mt:var name="email" escape="html">" />
    </mtapp:setting>
    <mtapp:setting
       id="website"
       label="<__trans phrase="Website URL">">
      <input type="text" name="url" id="url" class="text url" value="<mt:var name="url" escape="html">">
    </mtapp:setting>
    <div class="actions-bar settings-actions-bar">
        <button
           type="submit"
           id="submit-new-contact"
           class="button action primary">
          <__trans phrase="Add">
        </button>
        <button
           type="button"
           class="button action mt-cancel-action">
          <__trans phrase="Cancel">
        </button>
    </div>
</form>
</mt:setvarblock>

<mt:setvarblock name="jq_js_include" append="1">

    var current_edit = 0;
    var old_note_email = '';
    var old_note_url = '';

    function edit_note(id) {
        if (current_edit) return;
        var note_links = new Array();
        note_links.push( jQuery('#note-email-link-' + id) );
        note_links.push( jQuery('#note-url-link-'   + id) );
        var note_fields = new Array();
        note_fields.push( jQuery('#note-email-field-' + id) );
        note_fields.push( jQuery('#note-url-field-'   + id) );

        var $note_email = jQuery('#note-email-' + id);
        var $note_url   = jQuery('#note-url-'   + id);
        if (!note_links.length || !note_fields.length || !$note_email) return;
        current_edit = id;

        old_note_email = $note_email.val();
        old_note_url   = $note_url.val();
        for (i=0;i<2;++i) {
            note_links[i].hide();
            note_fields[i].show();
        }
        $note_email.focus();
        $note_email.select();
        return false;
    }

    function cancel_edit(id) {
        var note_links = new Array();
        note_links.push( jQuery('#note-email-link-' + id) );
        note_links.push( jQuery('#note-url-link-'   + id) );
        var note_fields = new Array();
        note_fields.push( jQuery('#note-email-field-' + id) );
        note_fields.push( jQuery('#note-url-field-'   + id) );

        var $note_email = jQuery('#note-email-' + id);
        var $note_url   = jQuery('#note-url-'   + id);
        if (!note_links.length || !note_fields.length || !$note_email) return;

        $note_email.val( old_note_email );
        old_note_email = '';
        $note_url.val( old_note_url );
        old_note_url = '';
        current_edit = 0;
        for (i=0;i<2;++i) {
            note_links[i].show();
            note_fields[i].hide();
        }
        $note_email.mtUnvalidate();
        $note_url.mtUnvalidate();
        return false;
    }

    function submit_edit(id) {
        if ((0 != current_edit) && (id != current_edit))
            id = current_edit;
        // if old email matches new email, ignore submission (same as cancel)
        // if new email is already in our noteList array, warn user
        // if not in noteList and not in range of noteList notes, check with server.
        var $note_email = jQuery('#note-email-' + id);
        var $note_url   = jQuery('#note-url-' + id);
        if ( !$note_email.val() || !$note_email.mtValidate() ) return false;
        if ( $note_url.val() && !$note_url.mtValidate() ) return false;
        var new_note_email = $note_email.val();
        var new_note_url = $note_url.val();
        if ((old_note_email == new_note_email) && (old_note_url == new_note_url)) {
            cancel_edit(id);
            return false;
        }
        $note_email.mtUnvalidate();
        $note_url.mtUnvalidate();
        return submit_edit_form(id);
    }

    function submit_edit_form(id) {
        var note_email = jQuery('#note-email-' + id).val();
        var note_url = jQuery('#note-url-' + id).val();
        renderList({
            __mode: 'save',
            _type: 'notification',
            email: note_email,
            url: note_url,
            id: id,
            forward_list: 1,
            magic_token: 1
        });
        cancelCreateMode();
        return false;
    }

    function setCreateMode () {
        jQuery('#create-inline-notification').show();
        jQuery('#url').val('');
        jQuery('#email').val('').focus();
        jQuery('#content-actions').hide();
    }

    function cancelCreateMode () {
        jQuery('#create-inline-notification').hide();
        jQuery('#create-inline-notification input').mtUnvalidate();
        jQuery('#content-actions').show();

        current_edit = 0;
    }

    jQuery('#submit-new-contact').click(function() {
        if ( !jQuery('#create-inline-notification input').mtValidate('simple') )
            return false;
        renderList({
            __mode: 'save',
            _type: 'notification',
            email: jQuery('#email').val(),
            url: jQuery('#url').val(),
            forward_list: 1,
            magic_token: 1
        });
        cancelCreateMode();
        return false;
    });

    jQuery('#create-inline-notification button.mt-cancel-action').click(function() {
        cancelCreateMode();
        return false;
    });

    jQuery('#action-create-contact').click( function() {
        setCreateMode();
        return false;
    });

    jQuery('.start-edit').live('click', function () {
        jQuery(this).attr('href').match(/#(\d+)/);
        var id = RegExp.$1;
        edit_note(id);
        return false;
    });

    jQuery('.cancel-edit').live('click', function () {
        jQuery(this).attr('href').match(/#(\d+)/);
        var id = RegExp.$1;
        cancel_edit(id);
        return false;
    });

    jQuery('.submit-note').live('click', function () {
        jQuery(this).attr('href').match(/#(\d+)/);
        var id = RegExp.$1;
        submit_edit(id);
        return false;
    });

</mt:setvarblock>

