var FDQ = {};

FDQ.Fdata_query = function(url_xml) {
    this.url_xml = url_xml;
    this.xml;
    this.obj;
    this.getDataFromXml(url_xml);
};

FDQ.Fdata_query.prototype.getDataFromXml = function (url_xml) {
    var xml;
    new Ajax.Request(
        url_xml,
        {
            method: 'get',
            asynchronous: false,
            onComplete: function(res) {
                xml = res.responseText;
            }
        });
    this.xml = xml;
    var xotree = new XML.ObjTree();
    xotree.force_array = ['fdata','purpose','feature','dataID'];
    var tree = xotree.parseXML(this.xml);
    this.obj = tree.root;
}

FDQ.Fdata_query.prototype.getPurposeLabels = function () {
    var purposes = this.obj.purpose;
    var p_labels = new Array();
    purposes.each(function(purpose){
        p_labels[p_labels.length] = purpose.label;
    });
    return p_labels;
}

FDQ.Fdata_query.prototype.getFdatasByIds = function (ids) {
    var fdatas = this.obj.fdata;
    var r = new Array();
    for(var i = 0 ; i < ids.length ; i++) {
        for(var j = 0 ; j < fdatas.length ; j++) {
            if(fdatas[j]['-id'] == ids[i]) {
                r[r.length] = fdatas[j];
            }
        }
    }
    return r;
}

FDQ.Fdata_query.prototype.makeftable = function (fdatas) {
    var tbody = document.createElement('tbody');
    var trs = new Array();
    var ths = new Array();
    var tds = new Array();
    var titles = [
        "会社・サービス",
        "実質年率",
        "初回最高額(限度額)",
        "審査時間(最短振込)",
        "来店要/不要",
        "特徴",
        "申込"
    ];

    trs[0] = document.createElement('tr');
    for(var i = 0 ; i <= 6 ; i++) {
        ths[i] = document.createElement('th');
        ths[i].innerHTML = titles[i];
        trs[0].appendChild(ths[i]);
    }
    tbody.appendChild(trs[0]);

    for(var i = 0 ; i < fdatas.length ; i++){
        trs[i + 1] = document.createElement('tr');
        tds[i] = new Array();

        for(var j = 0 ; j <= 6 ; j++) {
            tds[i][j] = document.createElement('td');
        }

        tds[i][0].innerHTML = fdatas[i].markURL  +  '<br />' + fdatas[i]['-name'];
        tds[i][1].innerHTML = fdatas[i].APR['-min'] + '%<br />' + (fdatas[i].APR['-max'] ? '&nbsp;&nbsp;～<br />' + fdatas[i].APR['-max'] + '%' : '&nbsp;');

        tds[i][2].innerHTML = fdatas[i].limitedcost['-first'] + '万円<br />' + (fdatas[i].limitedcost['-limit'] ? '<br />(' + fdatas[i].limitedcost['-limit'] + '万円)' : '&nbsp;');
        if(fdatas[i].examinationtime) {
            if(fdatas[i].examinationtime['-label']) {
                tds[i][3].innerHTML = fdatas[i].examinationtime['-label'].replace(/\[/g,'<').replace(/\]/g,'>');
            }
            else { tds[i][3].innerHTML = "&nbsp;"; }
        }
        else { tds[i][3].innerHTML = "&nbsp;"; }

        tds[i][4].innerHTML = (fdatas[i].visit ? fdatas[i].visit.replace(/\[/g,'<').replace(/\]/g,'>') : '&nbsp;');
        tds[i][5].innerHTML = (fdatas[i].feature ? '・' + fdatas[i].feature.join('<br />・').replace(/\[/g,'<').replace(/\]/g,'>') : '&nbsp;');
        tds[i][6].innerHTML = fdatas[i].inquire.replace(/\[/g,'<').replace(/\]/g,'>');

        for(var j = 0 ; j <= 6 ; j++) {
            trs[i + 1].appendChild(tds[i][j]);
        }
        tbody.appendChild(trs[i + 1]);
    }

    return tbody;
}

FDQ.Fdata_query.prototype.getPurposeById = function (id) {
    var purposes = this.obj.purpose;
    var r;
    for(var i = 0 ; i < purposes.length ; i++) {
        if(purposes[i].id == id) {
            r = purposes[i];
        }
    }
    return r;
}

FDQ.Fdata_query.prototype.makepurposetable = function (id) {
    var purpose = this.getPurposeById(id);
    var fdatas = this.getFdatasByIds(purpose.dataID);
    return this.makeftable(fdatas);
}
