{if $cid && $references}
<td>
    {icon name="document"}
</td>
<td class="nobr">
    <strong>{trans("Reference document:")}</strong>
</td>
<td class="nobr">
    <select name="document[reference]" class="refdoc"
        {tip class="lms-ui-advanced-select-test" text="Select reference document" trigger="reference"}>
        <option value=""{if !$document.reference} selected{/if}>{trans("— none —")}</option>
        {foreach $references as $doc}
            {number number=$doc.number template=$doc.template time=$doc.cdate customerid=$cid assign="number"}
            <option value="{$doc.docid}"
                {if isset($document.reference.id) && $document.reference.id == $doc.docid} selected{/if}
                >{t a=$_DOCTYPES[$doc.type] b=$number c=$doc.cdate|date_format:"Y-m-d"}$a no. $b issued on $c{/t}</option>
        {/foreach}
    </select>
</td>

<script>

    $(function() {
        $("#referencedocument").on("change", '[name="document[reference]"]', function() {
            $("#a_reference_document_limit").toggle(parseInt($(this).val()) > 0);
        });

        {if !empty($document.reference.id)}
            $("#a_reference_document_limit").show();
        {/if}
    });

</script>
{/if}
