var nowDate = new Date();
//ciDate
var ciDate = new Date();
ciDate.setDate(nowDate.getDate());
var coDate = new Date();
var ciDate2 = new Date();
ciDate2.setDate(nowDate.getDate()+3);
var coDate2 = new Date();

//nextDate
//サイド用
function nextDate(){
	var nextY = $('#ciDateY').val();
	var nextM = $('#ciDateM').val()-1;
	var nextD = $('#ciDateD').val();
	coDate = new Date(nextY, nextM, nextD);
	coDate.setDate(coDate.getDate()+1);
	$('#coDateY').val(coDate.getFullYear());
	$('#coDateM').val(coDate.getMonth()+1);
	$('#coDateD').val(coDate.getDate());
	$('#coCal').val(coDate.getFullYear()+'/'+ (coDate.getMonth()+1) +'/'+coDate.getDate());
}
function nextDate2(){
	var nextY2 = $('#ciDateY2').val();
	var nextM2 = $('#ciDateM2').val()-1;
	var nextD2 = $('#ciDateD2').val();
	coDate2 = new Date(nextY2, nextM2, nextD2);
	coDate2.setDate(coDate2.getDate()+1);
	$('#coDateY2').val(coDate2.getFullYear());
	$('#coDateM2').val(coDate2.getMonth()+1);
	$('#coDateD2').val(coDate2.getDate());
	$('#coCal2').val(coDate2.getFullYear()+'/'+ (coDate2.getMonth()+1) +'/'+coDate2.getDate());
}
//メインコンテンツ下用
function nextDate3(){
	var nextY = $('#ciDateY1_2').val();
	var nextM = $('#ciDateM1_2').val()-1;
	var nextD = $('#ciDateD1_2').val();
	coDate = new Date(nextY, nextM, nextD);
	coDate.setDate(coDate.getDate()+1);
	$('#coDateY1_2').val(coDate.getFullYear());
	$('#coDateM1_2').val(coDate.getMonth()+1);
	$('#coDateD1_2').val(coDate.getDate());
	$('#coCal1_2').val(coDate.getFullYear()+'/'+ (coDate.getMonth()+1) +'/'+coDate.getDate());
}
function nextDate4(){
	var nextY2 = $('#ciDateY2_2').val();
	var nextM2 = $('#ciDateM2_2').val()-1;
	var nextD2 = $('#ciDateD2_2').val();
	coDate2 = new Date(nextY2, nextM2, nextD2);
	coDate2.setDate(coDate2.getDate()+1);
	$('#coDateY2_2').val(coDate2.getFullYear());
	$('#coDateM2_2').val(coDate2.getMonth()+1);
	$('#coDateD2_2').val(coDate2.getDate());
	$('#coCal2_2').val(coDate2.getFullYear()+'/'+ (coDate2.getMonth()+1) +'/'+coDate2.getDate());
}

//nextDate
function arrAirport(){
	$('input[name="arr_boardAirport"]').val($('select[name="dep_arrivalAirport"]').val());
	$('input[name="arr_arrivalAirport"]').val($('select[name="dep_boardAirport"]').val());
}

$(function(){
	//日付セット
	//サイド用
	$('#ciDateY').val(ciDate.getFullYear());
	$('#ciDateM').val(ciDate.getMonth()+1);
	$('#ciDateD').val(ciDate.getDate());
	$('#ciCal').val(ciDate.getFullYear()+'/'+ (ciDate.getMonth()+1) +'/'+ciDate.getDate());
	nextDate();
	
	$('#ciDateY2').val(ciDate2.getFullYear());
	$('#ciDateM2').val(ciDate2.getMonth()+1);
	$('#ciDateD2').val(ciDate2.getDate());
	$('#ciCal2').val(ciDate2.getFullYear()+'/'+ (ciDate2.getMonth()+1) +'/'+ciDate2.getDate());
	nextDate2();
	
	//メインコンテンツ下用
	$('#ciDateY1_2').val(ciDate.getFullYear());
	$('#ciDateM1_2').val(ciDate.getMonth()+1);
	$('#ciDateD1_2').val(ciDate.getDate());
	$('#ciCal1_2').val(ciDate.getFullYear()+'/'+ (ciDate.getMonth()+1) +'/'+ciDate.getDate());
	nextDate3();
	
	$('#ciDateY2_2').val(ciDate2.getFullYear());
	$('#ciDateM2_2').val(ciDate2.getMonth()+1);
	$('#ciDateD2_2').val(ciDate2.getDate());
	$('#ciCal2_2').val(ciDate2.getFullYear()+'/'+ (ciDate2.getMonth()+1) +'/'+ciDate2.getDate());
	nextDate4();
	
	//coセット
	//サイド用
	$('#ciDateY, #ciDateM, #ciDateD').change(function(){
		nextDate();
	});
	$('#ciDateY2, #ciDateM2, #ciDateD2').change(function(){
		nextDate2();
	});
	//メインコンテンツ下用
	$('#ciDateY1_2, #ciDateM1_2, #ciDateD1_2').change(function(){
		nextDate3();
	});
	$('#ciDateY2_2, #ciDateM2_2, #ciDateD2_2').change(function(){
		nextDate4();
	});
	
	//復路セット
	arrAirport();
	$('select[name="dep_arrivalAirport"], select[name="dep_boardAirport"]').change(function(){
		arrAirport();
	});
	
	//予約エリア
	//サイド用
	if($('input[name="type"]:radio').val() == "h"){
		$('#form1').show();
		$('#form2').hide();
		$('#formBgBox').css("margin-left",0);
		$('#reserveForm').height(332);
	}
	if($('input[name="type"]:radio').val() == "hp"){
		$('#form2').show();
		$('#form1').hide();
			$('#formBgBox').css("margin-left",-250);
		$('#reserveForm').height(422);
	}
	$('input[name="type"]:radio').change(function(){
		if($(this).val() == 'h'){
			$('#form1').show();
			$('#form2').hide();
		$('#formBgBox').css("margin-left",0);
			$('#reserveForm').height(332);
		}
		if($(this).val() == 'hp'){
			$('#form2').show();
			$('#form1').hide();
			$('#formBgBox').css("margin-left",-250);
			$('#reserveForm').height(422);
		}
	});
	$('input[name="type"]:radio').click(function(){
		if($(this).val() == 'h'){
			$('#form1').show();
			$('#form2').hide();
			$('#formBgBox').css("margin-left",0);
			$('#reserveForm').height(332);
		}
		if($(this).val() == 'hp'){
			$('#form2').show();
			$('#form1').hide();
			$('#formBgBox').css("margin-left",-250);
			$('#reserveForm').height(422);
		}
	});
	//メインコンテンツ下用
	if($('input[name="type2"]:radio').val() == "h2"){
		$('#form1_2').show();
		$('#form2_2').hide();
	}
	if($('input[name="type2"]:radio').val() == "hp2"){
		$('#form2_2').show();
		$('#form1_2').hide();
	}
	$('input[name="type2"]:radio').change(function(){
		if($(this).val() == 'h2'){
			$('#form1_2').show();
			$('#form2_2').hide();
		}
		if($(this).val() == 'hp2'){
			$('#form2_2').show();
			$('#form1_2').hide();
		}
	});
	$('input[name="type2"]:radio').click(function(){
		if($(this).val() == 'h2'){
			$('#form1_2').show();
			$('#form2_2').hide();
		}
		if($(this).val() == 'hp2'){
			$('#form2_2').show();
			$('#form1_2').hide();
		}
	});
	
	//予約カレンダー
	$('.calendar').datepicker({
		showOn : 'button',
		buttonText : 'カレンダーから探す',
		numberOfMonths : 3,
		beforeShowDay: function(date){
			//日曜日
		    	if(date.getDay() == 0) {
			    	return [true,"ui-datepicker-sunday"];
			    //土曜日	
		    	} else if(date.getDay() == 6){
			    	return [true,"ui-datepicker-saturday"];
			    //平日
		    	} else {
			    	return [true];
		    	}
		},
		onClose : function(date){
			//サイド用
			if($(this).is('#ciCal')){
				$('#ciDateY').val(date.split('/')[0]);
				$('#ciDateM').val(date.split('/')[1]);
				$('#ciDateD').val(date.split('/')[2]);
				nextDate();
			}
			else if($(this).is('#coCal')){
				$('#coDateY').val(date.split('/')[0]);
				$('#coDateM').val(date.split('/')[1]);
				$('#coDateD').val(date.split('/')[2]);
			}
			else if($(this).is('#ciCal2')){
				$('#ciDateY2').val(date.split('/')[0]);
				$('#ciDateM2').val(date.split('/')[1]);
				$('#ciDateD2').val(date.split('/')[2]);
				nextDate2();
			}
			else if($(this).is('#coCal2')){
				$('#coDateY2').val(date.split('/')[0]);
				$('#coDateM2').val(date.split('/')[1]);
				$('#coDateD2').val(date.split('/')[2]);
			}
			//メインコンテンツ下用
			if($(this).is('#ciCal1_2')){
				$('#ciDateY1_2').val(date.split('/')[0]);
				$('#ciDateM1_2').val(date.split('/')[1]);
				$('#ciDateD1_2').val(date.split('/')[2]);
				nextDate();
			}
			else if($(this).is('#coCal1_2')){
				$('#coDateY1_2').val(date.split('/')[0]);
				$('#coDateM1_2').val(date.split('/')[1]);
				$('#coDateD1_2').val(date.split('/')[2]);
			}
			else if($(this).is('#ciCal2_2')){
				$('#ciDateY2_2').val(date.split('/')[0]);
				$('#ciDateM2_2').val(date.split('/')[1]);
				$('#ciDateD2_2').val(date.split('/')[2]);
				nextDate2();
			}
			else if($(this).is('#coCal2_2')){
				$('#coDateY2_2').val(date.split('/')[0]);
				$('#coDateM2_2').val(date.split('/')[1]);
				$('#coDateD2_2').val(date.split('/')[2]);
			}
		}
	});
	$.datepicker.regional['ja'];


  // added 2011 07 18
	//サイド用
  $("#form2").submit(function(){
    var ciMon = $(this).find("#ciDateM2").val() - 1
      , ciDay = $(this).find("#ciDateD2").val()
      , coMon = $(this).find("#coDateM2").val() -1
      , coDay = $(this).find("#coDateD2").val()
      , thisYear = new Date().getFullYear()
      , ciDate = new Date(thisYear, ciMon, ciDay)
      , coDate = new Date(thisYear, coMon, coDay)
      , diff = (coDate.getTime()-ciDate.getTime())/(1000*60*60*24)
      ;
    $("input[name='period']").val(diff);
  });
	//メインコンテンツ下用
  $("#form2_2").submit(function(){
    var ciMon = $(this).find("#ciDateM2_2").val() - 1
      , ciDay = $(this).find("#ciDateD2_2").val()
      , coMon = $(this).find("#coDateM2_2").val() -1
      , coDay = $(this).find("#coDateD2_2").val()
      , thisYear = new Date().getFullYear()
      , ciDate = new Date(thisYear, ciMon, ciDay)
      , coDate = new Date(thisYear, coMon, coDay)
      , diff = (coDate.getTime()-ciDate.getTime())/(1000*60*60*24)
      ;
    $("input[name='period']").val(diff);
  });
});	

