$(function() {
	$("#MakerId").change(function() {
		$('#priceLow').html('');
		$('#priceMax').html('');
		chkMaker();
	});
	$("#ModelId").change(function() {
		$('#priceLow').html('');
		$('#priceMax').html('');
		chkModel();
	});
	chkMaker();
	chkModel();
});

// 型番取得処理
function chkMaker() {
	$('#ModelId').empty();
	var optionItems = new Array();
	optionItems.push('<option value="">--</option>');
	$('#ModelId').append(optionItems.join());
	if ( $("#MakerId").val() ) {
		$.getJSON(
			'/jsonlist/fromMaker/'+$("#MakerId").val(),
			function(getlist){
				var optionItems = new Array();
				for (key in getlist) {
					optionItems.push('<option value="' + getlist[key]['MtModel']['id'] + '">' + getlist[key]['MtModel']['name'] + ' ( ' + getlist[key]['MtModel']['product_name'] + ' )</option>');
				}
				$('#ModelId').append(optionItems.join());
			}
		);
	}
}

// 金額取得処理
function chkModel() {
	if ( $("#ModelId").val() ) {
		$.getJSON(
			'/jsonlist/fromModel/'+$("#ModelId").val(),
			function(getlist){
				$('#priceLow').html(numberFormat(getlist['MtModel']['pricelow']));
				$('#priceMax').html(numberFormat(getlist['MtModel']['pricemax']));
			}
		);
	}
}
//3桁カンマ区切り
function numberFormat(str) {
	var num = new String(str).replace(/,/g, "");
	while(num != (num = num.replace(/^(-?\d+)(\d{3})/, "$1,$2")));
	return num;
}
