function attachmentFields2()
{
    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) == 'imagen_') {
            fields[fields.length] = document.compose.elements[i];
        }
    }
    return fields;
}

function attachmentsExist2()
{   
    if (document.compose.attachmentAction.value) {
        if (document.compose.attachmentAction.value == 'add') {
            var fields = attachmentFields2();
            for (var i = 0; i < fields.length; i++) {
                if (fields[i].value.length > 0) {
                    return true;
                }
            }
           // alert('Usted debe especificar por lo menos un archivo para archivo.');
            //document.compose.imagen_1.focus();
            document.compose.attachmentAction.value == 'update'
            return true;
        } else if (document.compose.attachmentAction.value == 'update') {
            return true;
        }
    }
}
function attach2(actionid, action)
{
    document.compose.actionID.value = actionid;
    document.compose.attachmentAction.value = action;
    if (attachmentsExist2()) {
        uniqSubmit();
    }
}

function attachmentChanged2()
{

    var usedFields = 0;
    var fields = attachmentFields2();


    for (var i = 0; i < fields.length; i++) {
        if (fields[i].value.length > 0) {
            usedFields++;
        }
    }

    if (usedFields == fields.length) {
        var lastRow = document.getElementById('fila_' + usedFields);
        if (lastRow) {
            var newRow = document.createElement('TR');
            newRow.id = 'fila_' + (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('Imagen ' + (usedFields + 1) + ': '));
            td.appendChild(document.createTextNode(' '));
            var file = document.createElement('INPUT');
            file.type = 'file';
            td.appendChild(file);
            file.name = 'imagen_' + (usedFields + 1);
            file.onchange = function() { attachmentChanged2(); };
            file.size = 25;
            file.className = 'fixed';
            td = document.createElement('TD');
            newRow.appendChild(td);
            td.align = 'left';
            lastRow.parentNode.insertBefore(newRow, lastRow.nextSibling);
        }
    }
}

function attachmentCheck2()
{
    var notUploaded = false;
    var fields = attachmentFields2();
    for (var i = 0; i < fields.length; i++) {
        if (fields[i].value.length > 0) {
            notUploaded = true;
        }
    }
    if (notUploaded) {
        return confirm('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 {
    document.compose.message.focus();
}



////////////////////////////////////////////
///////////////////////////////////////////
//esta sentencias son para manejo de archivos
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) == 'archiv_') {
            fields[fields.length] = document.compose.elements[i];
        }
    }
    return fields;
}

function attachmentsExist()
{   
    if (document.compose.attachmentAction.value) {
        if (document.compose.attachmentAction.value == 'add') {
            var fields = attachmentFields();
            for (var i = 0; i < fields.length; i++) {
                if (fields[i].value.length > 0) {
                    return true;
                }
            }
           // alert('Usted debe especificar por lo menos un archivo para archivo.');
            //document.compose.imagen_1.focus();
            document.compose.attachmentAction.value == 'update'
            return true;
        } 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()
{

    var usedFields = 0;
    var fields = attachmentFields();


    for (var i = 0; i < fields.length; i++) {
        if (fields[i].value.length > 0) {
            usedFields++;
        }
    }

    if (usedFields == fields.length) {
        var lastRow = document.getElementById('rows_' + usedFields);
        if (lastRow) {
            var newRow = document.createElement('TR');
            newRow.id = 'rows_' + (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('Archivo ' + (usedFields + 1) + ': '));
            td.appendChild(document.createTextNode(' '));
            var file = document.createElement('INPUT');
            file.type = 'file';
            td.appendChild(file);
            file.name = 'archiv_' + (usedFields + 1);
            file.onchange = function() { attachmentChanged(); };
            file.size = 25;
            file.className = 'fixed';
            td = document.createElement('TD');
            newRow.appendChild(td);
            td.align = 'left';
            lastRow.parentNode.insertBefore(newRow, lastRow.nextSibling);
        }
    }
}

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('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 {
    document.compose.message.focus();
}


