/*
 * 分類1の選択によって分類2の選択肢を変更する.
 */
function updateClassification2(name) {
    new Ajax.Updater(
        'classification2',
        contextPath + '/masterManage/listClassification2', {
            asynchronous: true,
            evalScripts: true,
            parameters: 'name=' + name
        }
    );
}

/*
 * 地域の選択によって国・地方の選択肢を変更する.
 * 国・地方が1件でも存在する場合、情報源の選択肢を最初の国・地方の情報源に変更する.
 */
function updateCountryAndInformationSource(id, noSelection) {
    new Ajax.Updater(
        'countryId',
        contextPath + '/masterManage/listCountry', {
            asynchronous: true,
            evalScripts: true,
            parameters: 'id=' + id + '&noSelection=' + noSelection,
            onSuccess: function(request) {
                if ($('informationSourceId') && request.responseText.match('<option value=')) {
                    var countryId = request.responseText.substring(15, 18);
                    updateInformationSource(countryId, noSelection);
                }
            }
        }
    );
}

/*
 * 地域の選択によって国・地方の選択肢を変更する.
 */
function updateCountry(id, noSelection) {
    new Ajax.Updater(
        'countryId',
        contextPath + '/masterManage/listCountry', {
            asynchronous: true,
            evalScripts: true,
            parameters: 'id=' + id + '&noSelection=' + noSelection
        }
    );
}

/*
 * 国・地方の選択によって情報源の選択肢を変更する.
 */
function updateInformationSource(id, noSelection) {
    var updater = new Ajax.Updater(
        'informationSourceId',
        contextPath + '/masterManage/listInformationSource', {
            asynchronous: true,
            evalScripts: true,
            parameters: 'id=' + id + '&noSelection=' + noSelection
        }
    );
}

/*
 * 所管の選択によってテキストフィールドに所管をは反映する.
 */
function selectJurisdiction(value) {
    $('jurisdiction').value = value;
}

/*
 * ファイル入力欄を追加する.
 */
function addFileEntry() {
    var tbody = $('attachedFilesTable').lastChild;
    var tr = document.createElement('tr');
    var fileTd = document.createElement('td');
    var titleTd = document.createElement('td');
    var fileInput = document.createElement('input');
    var titleInput = document.createElement('input');
    
    var index = tbody.children.length;
    fileInput.type = 'file';
    fileInput.name = 'files[' + index + ']'
    titleInput.type = 'text';
    titleInput.name = 'titles[' + index + ']'
    
    fileTd.appendChild(fileInput);
    titleTd.appendChild(titleInput);
    tr.appendChild(fileTd);
    tr.appendChild(titleTd);
    tbody.appendChild(tr);
}

