var fdq = new FDQ.Fdata_query('./fdata.xml');

var fdatas = new Array();

// purposeのlabelの配列
if($('purpose').innerHTML.match(/^( |\n)*$/)) {
    var p_labels = fdq.getPurposeLabels();
    var options = new Array();
    options[0] = document.createElement('option');
    options[0].value = "-1" ;
    options[0].innerHTML = "選択";
    $('purpose').appendChild(options[0]);
    for(var i = 0 ; i < p_labels.length ; i++) {
        options[i] = document.createElement('option');
        options[i].value = fdq.obj.purpose[i].id;
        options[i].innerHTML = p_labels[i];
        $('purpose').appendChild(options[i]);
    }
}
else {
    var options = $A($('purpose').getElementsByTagName('option'));
    options.each(function(option){
        if(option.value != -1) {
            var purpose = fdq.getPurposeById([option.value]);
            option.innerHTML = purpose.label;
        }
    });
}

function dispFdata(iD){
    var output = $(iD);
    var fdatas = new Array();
    if($('purpose').value == '-1') {
        fdatas = fdq.obj.fdata;
    }
    else {
        var purpose = fdq.getPurposeById($('purpose').value);
        var fdatas = fdq.getFdatasByIds(purpose.dataID);
    }

    if($('apr_u').checked) {
        for (var i = 0; i < fdatas.length - 1 ; i++) {
            for (var j = 0 ; j < fdatas.length - i - 1 ; j++ ) {
                if(fdatas[j].APR && fdatas[j+1].APR) {
                    if(fdatas[j].APR['-min'] && fdatas[j+1].APR['-min']) {
                        if (Number(fdatas[j].APR['-min']) > Number(fdatas[j+1].APR['-min'])) {
                            var f = fdatas[j];
                            fdatas[j] = fdatas[j+1];
                            fdatas[j+1] = f;
                        }
                    }
                }
            }
        }
    }

    if($('time_u').checked) {
        for (var i = 0; i < fdatas.length - 1 ; i++) {
            for (var j = 0 ; j < fdatas.length - i - 1 ; j++ ) {
                if(fdatas[j].examinationtime && fdatas[j+1].examinationtime) {
                    if(fdatas[j].examinationtime['-time'] && fdatas[j+1].examinationtime['-time']) {
                        if (Number(fdatas[j].examinationtime['-time']) > Number(fdatas[j+1].examinationtime['-time'])) {
                            var f = fdatas[j];
                            fdatas[j] = fdatas[j+1];
                            fdatas[j+1] = f;
                        }
                    }
                }
            }
        }
    }

    if($('lim_f_u').checked) {
        for (var i = 0; i < fdatas.length - 1 ; i++) {
            for (var j = 0 ; j < fdatas.length - i - 1 ; j++ ) {
                if(fdatas[j].limitedcost && fdatas[j+1].limitedcost) {
                    if(fdatas[j].limitedcost['-first'] && fdatas[j+1].limitedcost['-first']) {
                        if (Number(fdatas[j].limitedcost['-first']) > Number(fdatas[j+1].limitedcost['-first'])) {
                            var f = fdatas[j];
                            fdatas[j] = fdatas[j+1];
                            fdatas[j+1] = f;
                        }
                    }
                }
            }
        }
    }
    var tbody = $('output').getElementsByTagName('tbody');
    if(tbody[0]) {
        $('output').removeChild(tbody[0]);
    }
    $('output').appendChild(fdq.makeftable(fdatas));
}