var data = 
{
  list :
  [
    {price1:"&yen;9,000", price2:"&yen;11,700", price3:"&yen;6,300", term_from1:"2", term_to1:"3", term_from2:"1", term_to2:"", term_from3:"5", term_to3:""},
    {price1:"&yen;18,000", price2:"&yen;23,400", price3:"&yen;12,600", term_from1:"3", term_to1:"4", term_from2:"2", term_to2:"", term_from3:"9", term_to3:""},
    {price1:"&yen;27,000", price2:"&yen;35,100", price3:"&yen;18,900", term_from1:"4", term_to1:"6", term_from2:"2", term_to2:"3", term_from3:"11", term_to3:""},
    {price1:"&yen;36,000", price2:"&yen;46,800", price3:"&yen;25,200", term_from1:"5", term_to1:"7", term_from2:"3", term_to2:"5", term_from3:"13", term_to3:""},
    {price1:"&yen;45,000", price2:"&yen;58,500", price3:"&yen;31,500", term_from1:"6", term_to1:"8", term_from2:"3", term_to2:"5", term_from3:"15", term_to3:""},
    {price1:"&yen;54,000", price2:"&yen;70,200", price3:"&yen;37,800", term_from1:"7", term_to1:"10", term_from2:"4", term_to2:"6", term_from3:"17", term_to3:""},
    {price1:"&yen;63,000", price2:"&yen;81,900", price3:"&yen;44,100", term_from1:"8", term_to1:"11", term_from2:"4", term_to2:"6", term_from3:"19", term_to3:""},
    {price1:"&yen;72,000", price2:"&yen;93,600", price3:"&yen;50,400", term_from1:"9", term_to1:"12", term_from2:"5", term_to2:"7", term_from3:"21", term_to3:""},
    {price1:"&yen;81,000", price2:"&yen;105,300", price3:"&yen;56,700", term_from1:"10", term_to1:"13", term_from2:"5", term_to2:"7", term_from3:"23", term_to3:""},
    {price1:"&yen;90,000", price2:"&yen;117,000", price3:"&yen;63,000", term_from1:"11", term_to1:"14", term_from2:"6", term_to2:"8", term_from3:"25", term_to3:""},
    {price1:"&yen;99,000", price2:"&yen;128,700", price3:"&yen;69,300", term_from1:"12", term_to1:"15", term_from2:"6", term_to2:"8", term_from3:"27", term_to3:""},
    {price1:"&yen;108,000", price2:"&yen;140,400", price3:"&yen;75,600", term_from1:"13", term_to1:"15", term_from2:"7", term_to2:"10", term_from3:"29", term_to3:""},
    {price1:"&yen;117,000", price2:"&yen;152,100", price3:"&yen;81,900", term_from1:"14", term_to1:"17", term_from2:"7", term_to2:"10", term_from3:"31", term_to3:""},
    {price1:"&yen;126,000", price2:"&yen;163,800", price3:"&yen;88,200", term_from1:"15", term_to1:"18", term_from2:"8", term_to2:"12", term_from3:"33", term_to3:""},
    {price1:"&yen;135,000", price2:"&yen;175,500", price3:"&yen;945,000", term_from1:"16", term_to1:"19", term_from2:"8", term_to2:"12", term_from3:"35", term_to3:""},
    {price1:"&yen;153,000", price2:"&yen;187,200", price3:"&yen;100,800", term_from1:"17", term_to1:"20", term_from2:"9", term_to2:"13", term_from3:"37", term_to3:""},
    {price1:"&yen;162,000", price2:"&yen;198,900", price3:"&yen;107,100", term_from1:"18", term_to1:"21", term_from2:"9", term_to2:"13", term_from3:"39", term_to3:""},
    {price1:"&yen;171,000", price2:"&yen;210,600", price3:"&yen;113,400", term_from1:"19", term_to1:"22", term_from2:"10", term_to2:"14", term_from3:"41", term_to3:""},
    {price1:"&yen;180,000", price2:"&yen;222,300", price3:"&yen;119,700", term_from1:"20", term_to1:"23", term_from2:"10", term_to2:"14", term_from3:"43", term_to3:""},
    {price1:"&yen;180,000", price2:"&yen;234,000", price3:"&yen;126,000", term_from1:"21", term_to1:"24", term_from2:"11", term_to2:"15", term_from3:"45", term_to3:""},
    {price1:"&yen;900,000", price2:"&yen;1,170,000", price3:"&yen;630,000", term_from1:"", term_to1:"", term_from2:"", term_to2:"", term_from3:"", term_to3:""},
    {price1:"&yen;1,800,000", price2:"&yen;2,340,000", price3:"&yen;1,260,000", term_from1:"", term_to1:"", term_from2:"", term_to2:"", term_from3:"", term_to3:""},
  ]
}

jQuery.noConflict()(function($){
    var date  = new Date();
    var year  = date.getFullYear();
    var month = date.getMonth() + 1;
    var day   = date.getDate();
    var optionValue;
    var optionOutput;
    for (var i = 0; i <= 31; i++) {
        date   = computeDate(year, month, day, i);
        optionYear   = date.getFullYear();
        optionMonth  = date.getMonth() + 1;
        optionDay    = date.getDate();
        optionValue  = optionYear + "/" + optionMonth + "/" + optionDay;
        optionOutput = optionYear + "年" + optionMonth + "月" + optionDay + "日";
        $('#start').append($('<option>').attr({ value: optionValue }).text(optionOutput));
        $('#start').width();
    }
    $("#text2").html(data.list[$('#time').val()].price1 + "&nbsp;" + mekeDeriveryString($('#start').val(), data.list[$('#time').val()].term_from1, data.list[$('#time').val()].term_to1));
    $("#text1").html(data.list[$('#time').val()].price2 + "&nbsp;" + mekeDeriveryString($('#start').val(), data.list[$('#time').val()].term_from2, data.list[$('#time').val()].term_to2));
    $("#text3").html(data.list[$('#time').val()].price3 + "&nbsp;" + mekeDeriveryString($('#start').val(), data.list[$('#time').val()].term_from3, data.list[$('#time').val()].term_to3));

    $("#text1").fadeIn("slow"); 
    $("#text2").fadeIn("slow"); 
    $("#text3").fadeIn("slow"); 

    $("#time,#start").change(function(){ 
        if ($('#time').val() == '' || $('#start').val() == '') {
            return false;
        }

        $("#text1").hide();
        $("#text2").hide();
        $("#text3").hide();

        $("#text2").html(data.list[$('#time').val()].price1 + "&nbsp;" + mekeDeriveryString($('#start').val(), data.list[$('#time').val()].term_from1, data.list[$('#time').val()].term_to1));
        $("#text1").html(data.list[$('#time').val()].price2 + "&nbsp;" + mekeDeriveryString($('#start').val(), data.list[$('#time').val()].term_from2, data.list[$('#time').val()].term_to2));
        $("#text3").html(data.list[$('#time').val()].price3 + "&nbsp;" + mekeDeriveryString($('#start').val(), data.list[$('#time').val()].term_from3, data.list[$('#time').val()].term_to3));

        $("#text1").fadeIn("slow"); 
        $("#text2").fadeIn("slow"); 
        $("#text3").fadeIn("slow"); 
    }); 
}); 

function mekeDeriveryString(dateString, from, to) {
    var deriveryString = '';
    if (from != '') {
        var dateParts = dateString.split("/");
        date = computeDate(dateParts[0], dateParts[1], dateParts[2], from);
        year   = date.getFullYear();
        month  = date.getMonth() + 1;
        day    = date.getDate();
        if (from > 100) {
            deriveryString = "(約" + month + "月" + day + "日";
        } else {
            deriveryString = "(" + month + "月" + day + "日";
        }
    } else {
        deriveryString = "(応相談)";
    }

    if (to != '') {
        date = computeDate(dateParts[0], dateParts[1], dateParts[2], to);
        year   = date.getFullYear();
        month  = date.getMonth() + 1;
        day    = date.getDate();
        deriveryString += "～" + month + "月" + day + "日納品)";
    } else {
        if (from != '') {
            deriveryString += "納品)";
        }
    }

    return deriveryString;
}

function computeDate(year, month, day, addDays) {
    var dt = new Date(year, month - 1, day);
    var baseSec = dt.getTime();
    var addSec = addDays * 86400000;
    var targetSec = baseSec + addSec;
    dt.setTime(targetSec);
    return dt;
}


