<script type="text/javascript">
function deleteTicketType(ticketTypeId)
{
    new Ajax.Request(
        'index.php?fuse=support&action=DeleteTicketType',
        {
            parameters  : 'tickeTypetId=' + ticketTypeId,
            onLoading   : function(){resetStatus(lang('Deleting...'))},
            onComplete  : deleteTicketTypeCompleted
        }
    );
}

function deleteTicketTypeCompleted(responseObj)
{
    unsetStatus(false);

    var responseArr = responseObj.responseText.split('|');

    if (responseArr[0] == '1') {
        alert(lang('Ticket type deleted successfully'));
        $('ticketTypeRow_'+responseArr[1]).parentNode.removeChild($('ticketTypeRow_'+responseArr[1]));
    } else if (responseArr[0] == '2') {
        alert(lang('Ticket type could not be removed: You need to delete the tickets belonging to this type first, or change their ticket type'));
    } else {
        alert(lang('An error occurred when trying to delete this entry'));
    }
}

function updateTicketType(ticketTypeId)
{
    new Ajax.Request(
        'index.php?fuse=support&action=UpdateTicketType',
        {
            parameters  : 'ticketTypeId=' + ticketTypeId + '&name=' + _escapeString(document.ticketTypeForm.elements['ticketTypeName_'+ticketTypeId].value) + '&enabled=' + (document.ticketTypeForm.elements['ticketTypeEnabled_'+ticketTypeId].checked? '1' : '0'),
            onLoading   : function(){resetStatus(lang('Updating...'))},
            onComplete  : updateTicketTypeCompleted
        }
    );
}

function updateTicketTypeCompleted(responseObj)
{
    unsetStatus(false);

    if (responseObj.responseText == '1') {
        alert(lang('Ticket type has been update successfully'));
    } else {
        alert(lang('An error occurred when trying to update this ticket type'));
    }
}
</script>
<form name="ticketTypeForm" method="post" action="index.php?fuse=support&action=AddTicketType">
    <!-- BEGIN DYNAMIC BLOCK: noticeOrder -->
    <table border="0" width="100%">
        <tr>
            <td>
                <div class="yellowbox"><h2>[l]Notice[/l]:</h2>[l]Drag and drop lines in the table to reorder the ticket types. Order will be saved automatically.[/l]</div>
            </td>
        </tr>
    </table>
    <!-- END DYNAMIC BLOCK: noticeOrder -->
    <table border="0" width="100%">
        <thead>
            <tr>
                <th>[l]Name[/l]</th>
                <th>[l]Description[/l]</th>
                <th>[l]Default assignee[/l]</th>
                <th>[l]Enabled[/l]</th>
                <th>[l]Tickets[/l]</th>
            </tr>
        </thead>
        <tbody id="ticketTypesTableBody">
            <!-- BEGIN DYNAMIC BLOCK: ticketTypeRow -->
            <tr class="{trClass}" id="ticketType_{ticketTypeId}">
                <td width="1%" nowrap="true">
                    <!-- BEGIN DYNAMIC BLOCK: canEditTicketTypes1 -->
                    <a href="index.php?fuse=support&view=AddEditTicketType&ticketTypeId={ticketTypeId}">
                    <!-- END DYNAMIC BLOCK: canEditTicketTypes1 -->
                    {ticketTypeName}
                    <!-- BEGIN DYNAMIC BLOCK: canEditTicketTypes2 -->
                    </a>
                    <!-- END DYNAMIC BLOCK: canEditTicketTypes2 -->
                </td>
                <td>{ticketTypeDescription}</td>
                <td>{ticketTypeDefaultAssignee}</td>
                <td width="1%">{ticketTypeEnabled}</td>
                <td width="1%">{numTickets}</td>
            </tr>
            <!-- END DYNAMIC BLOCK: ticketTypeRow -->
        </tbody>
    </table>
</form>
<!-- BEGIN DYNAMIC BLOCK: sortableCode -->
<script type="text/javascript">
    Sortable.create('ticketTypesTableBody', {tag: 'tr', onUpdate: updateOrder});

    function updateOrder()
    {
        var order = Sortable.serialize('ticketTypesTableBody');
        new Ajax.Request(   'index.php?fuse=support&action=UpdateTicketTypesOrder',
                            {
                                method      : 'post',
                                parameters  : order,
                                onLoading   : function(request) {resetStatus(lang('Updating'))},
                                onComplete  : updateOrderComplete
                            }
        );
    }

    function updateOrderComplete(responseObj)
    {
        unsetStatus(false);
        if (responseArr.responseText != 'OK') {
            alert(responseObj.responseText.stripTags());
            return;
        }
    }
</script>
<!-- END DYNAMIC BLOCK: sortableCode -->
