<script language="JavaScript" type="text/javascript">
<!--

function attachmentFields()
{
    fields = new Array();
    for (var i = 0; i < document.compose.elements.length; i++) {
        if (document.compose.elements[i].type == 'file' &&
            document.compose.elements[i].name.substr(0, 7) == 'upload_') {
            fields[fields.length] = document.compose.elements[i];
        }
    }
    return fields;
}

function attachmentsExist()
{   
    if (document.compose.attachmentAction.value) {
        if (document.compose.attachmentAction.value == 'add') {
<?php if ($browser->hasQuirk('empty_file_input_value')): ?>
            return true;
<?php else: ?>
            var fields = attachmentFields();
            for (var i = 0; i < fields.length; i++) {
                if (fields[i].value.length > 0) {
                    return true;
                }
            }
            alert('<?php echo addslashes(_("You must specify at least one file to attach.")) ?>');
            document.compose.upload_1.focus();
            return false;
<?php endif; ?>
        } else if (document.compose.attachmentAction.value == 'update') {
            return true;
        }
    }
}

function attach(actionid, action)
{
    document.compose.actionID.value = actionid;
    document.compose.attachmentAction.value = action;
    if (attachmentsExist()) {
        uniqSubmit();
    }
}

function attachmentChanged()
{
<?php if ($browser->hasQuirk('empty_file_input_value') ||
          !$browser->hasFeature('dom')): ?>
    return;
<?php else: ?>

    var usedFields = 0;
    var fields = attachmentFields();

<?php
$max_attachments = $imp_compose->additionalAttachmentsAllowed();
if ($max_attachments !== true):
?>
    if (fields.length == <?php echo $max_attachments ?>) {
        return;
    }
<?php endif; ?>

    for (var i = 0; i < fields.length; i++) {
        if (fields[i].value.length > 0) {
            usedFields++;
        }
    }

    if (usedFields == fields.length) {
        var lastRow = document.getElementById('attachment_row_' + usedFields);
        if (lastRow) {
            var newRow = document.createElement('TR');
            newRow.id = 'attachment_row_' + (usedFields + 1);
            var td = document.createElement('TD');
            newRow.appendChild(td);
            td.align = 'left';
            var b = document.createElement('B');
            td.appendChild(b);
            b.appendChild(document.createTextNode('<?php echo _("File") ?> ' + (usedFields + 1) + ':'));
            td.appendChild(document.createTextNode(' '));
            var file = document.createElement('INPUT');
            file.type = 'file';
            td.appendChild(file);
            file.name = 'upload_' + (usedFields + 1);
            file.onchange = function() { attachmentChanged(); };
            file.size = 25;
            file.className = 'fixed';
            td = document.createElement('TD');
            newRow.appendChild(td);
            td.align = 'left';
            var select = document.createElement('SELECT');
            td.appendChild(select);
            select.name = 'upload_disposition_' + (usedFields + 1);
            select.options[0] = new Option('<?php echo _("Attachment") ?>', 'attachment', true);
            select.options[1] = new Option('<?php echo _("Inline") ?>', 'inline');
            lastRow.parentNode.insertBefore(newRow, lastRow.nextSibling);
        }
    }
<?php endif; ?>
}

function attachmentCheck()
{
    var notUploaded = false;
    var fields = attachmentFields();
    for (var i = 0; i < fields.length; i++) {
        if (fields[i].value.length > 0) {
            notUploaded = true;
        }
    }
    if (notUploaded) {
        return confirm('<?php echo addslashes(_("You have at least one attachment selected that you didn't upload yet. Are you sure you want to send this message without these attachments?")) ?>');
    }
    return true;
}

if (document.compose.to && document.compose.to.value == "") {
    document.compose.to.focus();
} else if (document.compose.subject.value == "") {
    document.compose.subject.focus();
} else {
<?php if ($rtemode): ?>
    document.compose.editor.focus();
<?php else: ?>
    document.compose.message.focus();
<?php endif; ?>
}

// -->
</script>
