/**
 * サブミットを行う（保存）
 */
$(function() {
	$('#btnInput').click(function() {
		$('#type').val('input');
		submitForm();
	});

	$('#btnConfirm').click(function() {
		$('#type').val('confirm');
		submitForm();
	});

	$('#btnComplete').click(function() {
		$('#type').val('complete');
		submitForm();
	});

	$('#btnBack').click(function() {
		$('#type').val('input');
		submitForm();
	});

	$('#birth_year').change(function() {
		var mybirthYear = $('#birth_year option:selected').attr('value');
		var mybirthMonth = $('#birth_month option:selected').attr('value');
		var d = new Date();
		var year = d.getFullYear()-20;
		var month = d.getMonth()+1;
		var date = d.getDate();
		var daysInaMonth = new Date(year, month, 0).getDate();
		var daysInSelectedMonth = new Date(mybirthYear, mybirthMonth, 0).getDate();
		var n = 12-month;
		var m = daysInaMonth - date;

		for(var i=0; i<12; i++){
			 val = i+1;
			$('select[id=birth_month] > option[value = '+val+']').attr('disabled', false);

		}

		$('#birth_month').val('--MM--');

		if(mybirthMonth == '--MM--'){
			if(mybirthYear == year){

				for(var i=0; i<n; i++){
					 val = month + i+1;
					$('select[id=birth_month] > option[value = '+val+']').attr('disabled', true);

				}
			}else{
				for(var i=0; i<n; i++){
					val = month + i+1;
					$('select[id=birth_month] > option[value = '+val+']').attr('disabled', false);
				}
			}
		}else{

			if(mybirthYear == year){

				for(var i=0; i<n; i++){
					 val = month + i+1;
					$('select[id=birth_month] > option[value = '+val+']').attr('disabled', true);

				}
			}else{
				for(var i=0; i<n; i++){
					val = month + i+1;
					$('select[id=birth_month] > option[value = '+val+']').attr('disabled', false);
				}
			}

			$('#birth_day').empty();
			$('#birth_day').append("<option>--DD--</option>");
			for(var i=1; i<=daysInSelectedMonth; i++){
				$('#birth_day').append("<option value='"+ i +"'>" + i + "</option>");
			}

			if(mybirthYear == year){
				if(mybirthMonth == month){
					for(var i=0; i<=m; i++){
						val = date + i+1;
						$('select[id=birth_day] > option[value = '+val+']').attr('disabled', true);
					}
				}
			}else{
				for(var i=0; i<=m; i++){
					val = date + i+1;
					$('select[id=birth_day] > option[value = '+val+']').attr('disabled', false);
				}
			}
		}



	});

	$('#birth_month').change(function() {
		var mybirthYear = $('#birth_year option:selected').attr('value');
		var mybirthMonth = $('#birth_month option:selected').attr('value');
		var d = new Date();
		var year = d.getFullYear()-20;
		var month = d.getMonth()+1;
		var date = d.getDate();
		var daysInaMonth = new Date(year, month, 0).getDate();
		var daysInSelectedMonth = new Date(mybirthYear, mybirthMonth, 0).getDate();
		var n = daysInaMonth - date;
		$('#birth_day').empty();
		$('#birth_day').append("<option>--DD--</option>");
		for(var i=1; i<=daysInSelectedMonth; i++){
			$('#birth_day').append("<option value='"+ i +"'>" + i + "</option>");
		}

		if(mybirthYear == year){
			if(mybirthMonth == month){
				for(var i=0; i<=n; i++){
					val = date + i+1;
					$('select[id=birth_day] > option[value = '+val+']').attr('disabled', true);
				}
			}
		}else{
			for(var i=0; i<=n; i++){
				val = date + i+1;
				$('select[id=birth_day] > option[value = '+val+']').attr('disabled', false);
			}
		}


	});
});
