<script type="text/javascript">

function toggleRoutingType(type)
{
    switch (type) {
        case '0':
            $('pipeSetup').style.display = 'none';
            $('pop3Setup').style.display = 'none';
            $('publicSectionSetup').style.display = 'none';
            $('trAssignTo').style.display = document.emailRoutingForm.useDefaultAssignee.value == '0'? '' : 'none';
            $('trAssignToPublic').style.display = 'none';
            break;
        case '1':
            $('pipeSetup').style.display = '';
            $('pop3Setup').style.display = 'none';
            $('publicSectionSetup').style.display = 'none';
            $('trAssignTo').style.display = document.emailRoutingForm.useDefaultAssignee.value == '0'? '' : 'none';
            $('trAssignToPublic').style.display = 'none';
            break;
        case '2':
            $('pipeSetup').style.display = 'none';
            $('pop3Setup').style.display = '';
            $('publicSectionSetup').style.display = 'none';
            $('trAssignTo').style.display = document.emailRoutingForm.useDefaultAssignee.value == '0'? '' : 'none';
            $('trAssignToPublic').style.display = 'none';
            break;
        case '3':
            $('pipeSetup').style.display = 'none';
            $('pop3Setup').style.display = 'none';
            $('trAssignTo').style.display = 'none';
            $('publicSectionSetup').style.display = '';
            $('trAssignToPublic').style.display = '';
            break;
        default:
            break;
    }

    toggleTicketOptions($('openTicket').checked);
}

function toggleGroupsCombo(show)
{
    $('groupsTable').style.display = (show? '' : 'none');
}

function changeTargetType(newTypeId)
{
    if (newTypeId == 0) {
        $('defaultAssignees').style.display = 'none';
        return;
    }

    $('defaultAssignees').style.display = 'block';

    var arrDefaultAssignees = $('defaultAssignees').getElementsByTagName('span');
    for (var i = 0; i < arrDefaultAssignees.length; i++) {
        if (arrDefaultAssignees[i].id == 'overrideDefaultAssignee') {
            continue;
        }

        if (arrDefaultAssignees[i].id == 'defaultAssignee_' + newTypeId) {
            arrDefaultAssignees[i].style.display = 'inline';
        } else {
            arrDefaultAssignees[i].style.display = 'none';
        }
    }
}

function overrideDefaultAssignee()
{
    $('defaultAssignees').style.display = 'none';
    $('trAssignTo').style.display = '';

    document.emailRoutingForm.useDefaultAssignee.value = '0';
}

function setAssigneeToDefault()
{
    $('trAssignTo').style.display = 'none';
    $('defaultAssignees').style.display = '';

    document.emailRoutingForm.useDefaultAssignee.value = '1';
}

function toggleTicketOptions(checked)
{
    if (checked) {
        if ($('routingType').value != '3') {
            $('trPriority').style.display = '';
            $('trPriorityFixed').style.display = 'none';
            $('trType').style.display = '';
            $('trTypeFixed').style.display = 'none';
            $('trAssignToPublic').style.display = 'none';
            if (document.emailRoutingForm.useDefaultAssignee.value == '0') {
                $('trAssignTo').style.display = '';
            }
        } else {
            $('trPriority').style.display = 'none';
            $('trPriorityFixed').style.display = '';
            $('trType').style.display = 'none';
            $('trTypeFixed').style.display = '';
            $('trAssignToPublic').style.display = '';
        }
    } else {
        $('trPriority').style.display = 'none';
        $('trPriorityFixed').style.display = 'none';
        $('trType').style.display = 'none';
        $('trTypeFixed').style.display = 'none';
        $('trAssignTo').style.display = 'none';
        $('trAssignToPublic').style.display = 'none';
    }
}

function validateForm(form)
{
    if ($('routingType').value != '3' && form.openTicket.checked && form.targetType.value == 0) {
        alert(lang('If you\'re opening a ticket, you have to select a ticket type'));
        return false;
    }

    if ($('name').value == "") {
        alert(lang('Please enter a Rule name'));
        return false;
    }

    return true;
}

</script>
<form name="emailRoutingForm" method="post" action="index.php?fuse=support&action=SaveEmailRouting{returnToDept}">
    <input type="hidden" name="routingId" value="{routingId}" />
    <input type="hidden" name="useDefaultAssignee" value="{useDefaultAssignee}" />
    <div class="yellowbox" style="margin-bottom:5px">
       <h2>[l]Handling Support Requests[/l]</h2>
       [l]Use this screen for creating rules to determine how incoming E-mails and support requests entered in the public section will be handled.[/l]<br />
       [l]The "Autoresponder template", "Open ticket" and "Send copy to" fields determine which actions are taken.[/l]<br />
    </div>
    <table width="100%">
        <tr>
            <td align="right" class="formlabel" width="1%">[l]Rule name[/l]:</td>
            <td class="formfield">
                <input type="text" id="name" name="name" value="{name}" size="50" />
            </td>
        </tr>
        <tr>
            <td align="right" class="formlabel">[l]Request source[/l]:</td>
            <td class="formfield">
                <select id="routingType" name="routingType" onchange="toggleRoutingType(this.value)">
                    <option value="1" {routingTypePipeSelected} >[l]E-mail pipe forwarding[/l]&nbsp;&nbsp;</option>
                    <option value="2" {routingTypePopSelected} >[l]E-mail POP3 fetching[/l]&nbsp;&nbsp;</option>
                    <option value="3" {routingTypePublicSectionSelected} >[l]Public section[/l]&nbsp;&nbsp;</option>
                </select>
            </td>
        </tr>
        <tr>
            <td align="right" valign="top" class="formlabel" width="1%">[l]User type[/l]:</td>
            <td class="formfield">
                <table border="0">
                    <tr>
                        <td>
                            <table border="0">
                                <tr>
                                    <td>
                                        <input type="radio" name="userType" value="both" {userTypeBothChecked} onclick="toggleGroupsCombo(false)" / >
                                    </td>
                                    <td>
                                        [l]Any (Guests and Registered)[/l]
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <input type="radio" name="userType" value="anonymous" {userTypeAnonymousChecked} onclick="toggleGroupsCombo(false)" / >
                                    </td>
                                    <td>
                                        [l]Guests[/l]
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <input type="radio" name="userType" value="registered" {userTypeRegisteredChecked} onclick="toggleGroupsCombo(false)" / >
                                    </td>
                                    <td>
                                        [l]Registered[/l]
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <input type="radio" name="userType" value="groups" {userTypeGroupsChecked} onclick="toggleGroupsCombo(true)" / >
                                    </td>
                                    <td>
                                        [l]Registered, from selected groups[/l]
                                    </td>
                                </tr>
                            </table>
                        </td>
                        <td valign="top" style="padding-left:30px">
                            <table id="groupsTable" style="display:{displayGroupsTable}">
                                <tr>
                                    <td valign="top">
                                        <b>[l]Select groups[/l]:</b>
                                    </td>
                                    <td>
                                        <select name="targetGroups[]" size="6" multiple style="width:200px">
                                        <!-- BEGIN DYNAMIC BLOCK: targetGroups -->
                                          <option value="{targetGroupId}" {targetGroupSelected}>{targetGroupName}</option>
                                        <!-- END DYNAMIC BLOCK: targetGroups -->
                                        </select>
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
        <tr>
            <td align="right" class="formlabel" width="1%">[l]Autoresponder template[/l]:</td>
            <td class="formfield">
                <select name="autoresponderTemplateId">
                    <option value="0">[l]None[/l]</option>
                    <!-- BEGIN DYNAMIC BLOCK: autoresponderTemplates -->
                    <option value="{autoresponderTemplateId}" {autoresponderTemplateSelected} >{autoresponderTemplateName}&nbsp;&nbsp;&nbsp;&nbsp;</option>
                    <!-- END DYNAMIC BLOCK: autoresponderTemplates -->
                </select>
            </td>
        </tr>
        <tr>
            <td align="right" class="formlabel" width="1%">[l]Open Ticket?[/l]</td>
            <td class="formfield">
                <input type="checkbox" id="openTicket" name="openTicket" onclick="toggleTicketOptions(this.checked)" {openTicketChecked} />
            </td>
        </tr>
        <tr id="trPriority" style="display:{displayTrPriority}">
            <td align="right" class="formlabel" width="1%">[l]Set to priority[/l]:</td>
            <td class="formfield">
                <select name="targetPriority">
                    <option value="3" {targetPriorityLowSelected} >[l]Low[/l]</option>
                    <option value="2" {targetPriorityMediumSelected} >[l]Medium[/l]</option>
                    <option value="1" {targetPriorityHighSelected} >[l]High[/l]</option>
                </select>
            </td>
        </tr>
        <tr id="trPriorityFixed" style="display:{displayTrPriorityFixed}">
            <td align="right" class="formlabel" width="1%">[l]Set to priority[/l]:</td>
            <td class="formfield">
                [l]Selected by customer[/l]
            </td>
        </tr>
        <tr id="trType" style="display:{displayTrType}">
            <td align="right" valign="top" class="formlabel" width="1%">[l]Set to type[/l]:</td>
            <td class="formfield">
                <select name="targetType" onchange="changeTargetType(this.value)">
                    <option value="0">-- [l]Select type[/l] --&nbsp;&nbsp;</option>
                    <!-- BEGIN DYNAMIC BLOCK: ticketTypes -->
                    <option value="{targetTypeId}" {targetTypeSelected} >{targetTypeName}</option>
                    <!-- END DYNAMIC BLOCK: ticketTypes -->
                </select>
                <div id="defaultAssignees" style="display:none">
                    [l]Assigned by default to[/l]:
                    <!-- BEGIN DYNAMIC BLOCK: defaultAssignees -->
                    <span id="defaultAssignee_{targetTypeId}">{defaultAssigneeLabel}</span>
                    <!-- END DYNAMIC BLOCK: defaultAssignees -->
                    <span id="overrideDefaultAssignee">&nbsp;&nbsp;&nbsp;(<a href="#" onclick="overrideDefaultAssignee()">[l]override[/l]</a>)</span>
                </div>
            </td>
        </tr>
        <tr id="trTypeFixed" style="display:{displayTrTypeFixed}">
            <td align="right" valign="top" class="formlabel" width="1%">[l]Set to type[/l]:</td>
            <td class="formfield">
                [l]Selected by customer[/l]
            </td>
        </tr>
        <tr id="trAssignTo" style="display:none">
            <td align="right" class="formlabel">[l]Assign to[/l]:</td>
            <td class="formfield">
                 <select name="assignee">
                    <option value="0">[l]Leave unassigned[/l]&nbsp;&nbsp;</option>
                    <!-- BEGIN DYNAMIC BLOCK: assigneeList -->
                    <option value="{assigneeId}" {assigneeSelected}>{assigneeLabel}</option>
                    <!-- END DYNAMIC BLOCK: assigneeList -->
                </select><br />
                &nbsp;&nbsp;&nbsp;(<a href="#" onclick="setAssigneeToDefault()">[l]set to ticket type default[/l]</a>)
            </td>
        </tr>
        <tr id="trAssignToPublic" style="display:{displayTrAssignToPublic}">
            <td align="right" class="formlabel">[l]Assign to[/l]:</td>
            <td class="formfield">
                 <select name="assigneePublic">
                    <option value="{ticketTypeDefaultAssigneeFlag}">[l]Ticket type default assignee[/l]&nbsp;&nbsp;</option>
                    <option value="0">[l]Leave unassigned[/l]&nbsp;&nbsp;</option>
                    <!-- BEGIN DYNAMIC BLOCK: assigneeList -->
                    <option value="{assigneeId}" {assigneeSelected}>{assigneeLabel}</option>
                    <!-- END DYNAMIC BLOCK: assigneeList -->
                </select><br />
            </td>
        </tr>
        <tr>
            <td align="right" class="formlabel" nowrap="true">[l]Send copy to[/l]{addressesHelp2}:</td>
            <td class="formfield">
                <textarea name="copyDestinataries" class="smallTextarea" rows="3">{copyDestinataries}</textarea>
            </td>
        </tr>
    </table>
    <div id="pipeSetup" style="display:{displayPipeSetup}">
        <table width="100%">
            <tr>
                <td colspan="2" >
                    <TABLE cellSpacing=0 cellPadding=0 width="100%" background=templates/images/box_bgt.gif border=0>
                        <TBODY>
                        <TR>
                          <TD width=18><IMG height=18 alt="" src="templates/images/box_bgt.gif" width=18 border=0></TD>
                          <TD  class=boxtitle align=left>
                                [l]Pipe Forwarding Set-up[/l]:
                          </TD>
                          <td align=right class=boxtitle nowrap>&nbsp;&nbsp;</tD>
                          <TD align=right width=22><IMG height=18 alt="" src="templates/images/box_end.gif" width=22 border=0></TD>
                        </TR>
                        </TBODY>
                    </TABLE>
                </td>
            </tr>
            <tr>
                <td align="right" class="formlabel" width="1%" nowrap="true">[l]E-mails[/l]{addressesHelp}:</td>
                <td class="formfield">
                    <textarea name="piping_emails" class="smallTextarea" rows="3" style="width:300px">{emails}</textarea>
                </td>
            </tr>
            <tr>
                <td align="right" class="formlabel" width="1%" nowrap="true">[l]Filter out[/l]{filteroutHelp}:</td>
                <td class="formfield">
                    <textarea name="piping_filterout" class="smallTextarea" rows="3" style="width:300px">{filterout}</textarea>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    [l]Have each of the above E-mail addresses forward to[/l]:
                    <div class="warningtext">| {pipeCommand}</div>
                </td>
            </tr>
        </table>
    </div>
    <div id="pop3Setup" style="display:{displayPop3Setup}">
        <table width="100%">
            <tr>
                <td colspan="2">
                    <TABLE cellSpacing=0 cellPadding=0 width="100%" background=templates/images/box_bgt.gif border=0>
                        <TBODY>
                        <TR>
                          <TD width=18><IMG height=18 alt="" src="templates/images/box_bgt.gif" width=18 border=0></TD>
                          <TD  class=boxtitle align=left>
                            [l]Pop3 Fetching Settings[/l]:
                          </TD>
                          <td align=right class=boxtitle nowrap>&nbsp;&nbsp;</tD>
                          <TD align=right width=22><IMG height=18 alt="" src="templates/images/box_end.gif" width=22 border=0></TD>
                        </TR>
                        </TBODY>
                    </TABLE>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <div class="yellowbox">
                        <h2>[l]NOTICE[/l]:</h2>
                        [l]This works in tandem with the "Tickets Generator" automation service, so you need to have it enabled for this routing rule to work.[/l]<br />
                        [l]Be aware that this rule will only apply to E-mails sent to this account, to the E-mail(s) address(es) specified below. You can use a same account in many routing rules, as long as you set the E-mails below consistently.[/l]
                    </div>
                </td>
            </tr>
             <tr>
                <td align="right" class="formlabel" width="1%">[l]E-mail server hostname[/l]:</td>
                <td class="formfield"><input name="pop3Hostname" value="{pop3Hostname}" type="text" style="width:300px" size="35" /></td>
            </tr>
            <tr>
                <td align="right" class="formlabel">[l]E-mail server port[/l]:</td>
                <td class="formfield"><input type="text" name="pop3Port" value="{pop3Port}" size="4" /></td>
            </tr>
            <tr>
                <td align="right" class="formlabel" width="1%" nowrap="true">[l]E-mails[/l]{addressesHelp}:</td>
                <td class="formfield">
                    <textarea name="pop3_emails" class="smallTextarea" rows="3" style="width:300px">{emails}</textarea>
                </td>
            </tr>
            <tr>
                <td align="right" class="formlabel" width="1%">[l]E-mail account username[/l]:</td>
                <td class="formfield"><input type="text" name="pop3Username" value="{pop3Username}" size="35" style="width:300px" /></td>
            </tr>
            <tr>
                <td align="right" nowrap="true" class="formlabel" width="1%">[l]E-mail account password[/l]:</td>
                <td class="formfield"><input type="text" name="pop3Password" value="{pop3Password}" size="35" style="width:300px" /></td>
            </tr>
            <tr>
                <td align="right" nowrap="true"  class="formlabel">[l]Delete E-mails on server[/l]<br />[l]after retrieval[/l]?</td>
                <td class="formfield"><input type="checkbox" name="pop3DeleteEmails" {checkedPop3DeleteEmails} /></td>
            </tr>
            <tr>
                <td align="right" class="formlabel" width="1%" nowrap="true">[l]Filter out[/l]{filteroutHelp}:</td>
                <td class="formfield">
                    <textarea name="pop3_filterout" class="smallTextarea" rows="3" style="width:300px">{filterout}</textarea>
                </td>
            </tr>
        </table>
    </div>
    <div id="publicSectionSetup" class="yellowbox" style="display:{displayPublicSectionSetup}; margin-top:10px">
        <h2>[l]NOTICE[/l]:</h2>
        [l]By creating this rule, you will be giving access to the public support section under the directory /public of your installation.[/l]<br />
        [l]If the type of customer (Guest or Registered) that enters a request in the public section doesn't have an associated routing rule, then his request will be silently ignored. Therefore we recomend to have rules for all customer types, setting autoresponders with the appropriate messages for those you don't wish to process.[/l]
    </div>
    <div style="padding-top:10px">
        <input type="submit" value="[l]Save[/l]" class="button_medium" onclick="return validateForm(this.form);" />&nbsp;&nbsp;
        <!-- BEGIN DYNAMIC BLOCK: deleteButton -->
        <input type="submit" value="[l]Delete[/l]" onclick="this.form.action='index.php?fuse=support&action=DeleteRoutingRule'" class="button_medium" />
        <!-- END DYNAMIC BLOCK: deleteButton -->
    </div>
</form>
<script type="text/javascript">
    changeTargetType({selectedTargetTypeId});
    <!-- BEGIN DYNAMIC BLOCK: overrideDefaultAssignee -->
    overrideDefaultAssignee();
    <!-- END DYNAMIC BLOCK: overrideDefaultAssignee -->
</script>
