// define price matrix
var data = 
{
  list :
  [
    {pSpeedy:4000, pRegular:2500, pSlow:2000, speedy:1, regular:2, slow:3},
    {pSpeedy:12000, pRegular:7500, pSlow:6000, speedy:1, regular:3, slow:6},
    {pSpeedy:24000, pRegular:15000, pSlow:12000, speedy:1, regular:4, slow:8},
    {pSpeedy:27000, pRegular:22500, pSlow:18000, speedy:2, regular:4, slow:9},
    {pSpeedy:36000, pRegular:30000, pSlow:24000, speedy:2, regular:5, slow:10},
    {pSpeedy:45000, pRegular:37500, pSlow:30000, speedy:2, regular:6, slow:10},
    {pSpeedy:54000, pRegular:45000, pSlow:36000, speedy:3, regular:7, slow:10},
    {pSpeedy:63000, pRegular:52500, pSlow:42000, speedy:3, regular:8, slow:10},
    {pSpeedy:72000, pRegular:60000, pSlow:48000, speedy:4, regular:8, slow:11},
    {pSpeedy:81000, pRegular:67500, pSlow:54000, speedy:4, regular:9, slow:11},
    {pSpeedy:90000, pRegular:75000, pSlow:60000, speedy:5, regular:10, slow:12},
    {pSpeedy:108000, pRegular:90000, pSlow:72000, speedy:6, regular:10, slow:13},
    {pSpeedy:126000, pRegular:105000, pSlow:84000, speedy:6, regular:11, slow:14},
  ]
}

jQuery.noConflict()(function($)
{
	// initialize variables
	var date  = new Date();
	var year  = date.getFullYear();
	var month = date.getMonth() + 1;
	var day   = date.getDate();
	var optionValue;
    var optionOutput;
	
	// fill dropdown list
    for (var i = 0; i <= 30; 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 + "日";
        $('#deadline').append($('<option>').attr({ value: optionValue }).text(optionOutput));
        $('#deadline').width();
    }
	
	// display result	
	$("#slow").html(data.list[$('#length').val()].pSlow.formatMoney(0, '.', ',') + "円&nbsp;" + makeDeliveryString(data.list[$('#length').val()].slow));
	$("#regular").html(data.list[$('#length').val()].pRegular.formatMoney(0, '.', ',') + "円&nbsp;" + makeDeliveryString(data.list[$('#length').val()].regular));		
	$("#speedy").html(data.list[$('#length').val()].pSpeedy.formatMoney(0, '.', ',') + "円&nbsp;" + makeDeliveryString(data.list[$('#length').val()].speedy));
		
	// bind script to dropdown event
    $("#length,#deadline").change(function(){ 
		calculateQuote();										   
    }); 
	
	// bind script to checkbox
	$('#firstTime').click (function (){
		calculateQuote();
	});	
	
	// calculate transcription quote
	function calculateQuote()
	{
		if ($('#length').val() == '' || $('#deadline').val() == '') {
			return false;
		}
	
		// set effect
		$("#slow").hide();
		$("#regular").hide();
		$("#speedy").hide();
	
		// check if checkbox is checked
		if ($('#firstTime').is(':checked'))
		{
			pSlow = data.list[$('#length').val()].pSlow * 0.9;
			pRegular = data.list[$('#length').val()].pRegular * 0.9;
			pSpeedy = data.list[$('#length').val()].pSpeedy * 0.9;		
		}
		else
		{
			pSlow = data.list[$('#length').val()].pSlow;
			pRegular = data.list[$('#length').val()].pRegular;
			pSpeedy = data.list[$('#length').val()].pSpeedy;
		}
			
		// display result	
		$("#slow").html(pSlow.formatMoney(0, '.', ',') + "円&nbsp;" + makeDeliveryString(data.list[$('#length').val()].slow));
		$("#regular").html(pRegular.formatMoney(0, '.', ',') + "円&nbsp;" + makeDeliveryString(data.list[$('#length').val()].regular));		
		$("#speedy").html(pSpeedy.formatMoney(0, '.', ',') + "円&nbsp;" + makeDeliveryString(data.list[$('#length').val()].speedy));
		
		// set effect
		$("#slow").fadeIn("slow");
		$("#regular").fadeIn("slow");
		$("#speedy").fadeIn("slow");	
	}
	
}); 

// make result string
function makeDeliveryString(days) 
{
	// initialize variables
	var currentDate  = new Date();
	var year  = currentDate.getFullYear();
	var month = currentDate.getMonth() + 1;
	var day   = currentDate.getDate();

	date   = computeDate(year, month, day, days);		
	year   = date.getFullYear();
	month  = date.getMonth() + 1;
	day    = date.getDate();
	if (days > 100) 
	{
		deliveryString = "(" + month + "月" + day + "日納品)";			
		deliveryString = "(" + month + "月" + day + "日納品)";
	} 
	else 
	{
		deliveryString = "(" + month + "月" + day + "日納品)";
	}

    return deliveryString;
}

// add days to date
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;
}

Number.prototype.formatMoney = function(c, d, t)
{
	var n = this, c = isNaN(c = Math.abs(c)) ? 2 : c, d = d == undefined ? "," : d, t = t == undefined ? "." : t, s = n < 0 ? "-" : "", i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
    return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
};


