/*-------------------------------------------------------
airtop.js
depends on prototype.js(http://prototype.conio.net/)
@version  V1.0
@author   youngjin.shin (2007/04/20)
Copyright (c) 2007 Rakuten Travel, Inc. All Rights Reserved.
--------------------------------------------------------*/
 	var tabList = ["tokyo","osaka","nagoya","fukuoka"]; 
        var tabList2 = ["asia","america","europ","beach"];
 	var des     = ["","帰着地を選択してください"];
    var dep     = ["","出発地を選択してください"];

    
 	function initiate(){
		//
    	Util.createSuggest("sg1", "destination1", "sg2", "suggest1", "mask1", list);
    	Util.createSuggest("sg2", "departure2"  , "",    "suggest2", "mask2", list);
    	Util.createSuggest("sg3", "destination2", "sg4", "suggest3", "mask3", list);
    	Util.createSuggest("sg4", "departure3"  , "",    "suggest4", "mask4", list);
    	Util.createSuggest("sg5", "destination3", "sg6", "suggest5", "mask5", list);
    	Util.createSuggest("sg6", "departure4"  , "",    "suggest6", "mask6", list);
    	Util.createSuggest("sg7", "destination4", "sg8", "suggest7", "mask7", list);
    	Util.createSuggest("sg8", "departure5"  , "",    "suggest8", "mask8", list);    
   	 
    	//
    	$("round").checked=true;
    	activateRoundTrip();
    	Util.getInstance("sg3").syncId = "sg8";
    	Util.createOptionList("destination5", Util.appendList(des,domesticAirport));
    	Util.createOptionList("departure1"  , Util.appendList(dep,domesticAirport));
    	Util.createOptionList("codeAirline" , airline);
		
		//     
    	Util.createDate("date5", "startYear5", "startMonth5", "startDay5", null                     , 0);
    	Util.createDate("date4", "startYear4", "startMonth4", "startDay4", Util.getInstance("date5"), 2);
    	Util.createDate("date3", "startYear3", "startMonth3", "startDay3", Util.getInstance("date4"), 2);
    	Util.createDate("date2", "startYear2", "startMonth2", "startDay2", Util.getInstance("date3"), 2);
    	Util.createDate("date1", "startYear1", "startMonth1", "startDay1", Util.getInstance("date2"), 2);
    	Util.getInstance("date1").addDay(getDiffDate());
    	//var li = ["adultNum","childNum","infantNum"];
    	Util.createLimitNumber("l1",["adultNum","childNum","infantNum"] ,6);
    	
 		MM_preloadImages('images/search/search_flight_plan1.gif','images/search/search_flight_plan2.gif');  
 	}
 
 	function changeTab(tabid){
		var flg=false;
 		for (i=0;i<tabList.length;i++){
 			if(tabList[i] == tabid){
 		    	$("tabimg_"+tabList[i]).style.display="";
            	$("tab_"+tabList[i]).style.display="none";
    			$(tabList[i]).style.display="";
				flg=true;
 			} else {
	    		$("tabimg_"+tabList[i]).style.display="none";
       	    	$("tab_"+tabList[i]).style.display="";
  				$(tabList[i]).style.display="none";
  			}
 		}
        if(!flg){
            $("tabimg_"+tabList[0]).style.display="";
            $("tab_"+tabList[0]).style.display="none";
            $(tabList[0]).style.display="";
        }
 		return false;
 	}

	function changeTab1(tabid){
		var flg = false;
		for (i=0; i<tabList.length; i++){
			if(tabList[i] == tabid){
				document.getElementById(tabList[i]).style.display = "";
				flg = true;
			} else {
				document.getElementById(tabList[i]).style.display = "none";
			}
		}
		if (!flg) {
			document.getElementById(tabList[0]).style.display = "";
		}
		return false;
	}

	function changeTab2(tabid){
		var flg = false;
		for (i=0; i<tabList2.length; i++){
			if(tabList2[i] == tabid){
				for (j=0; j<tabList.length; j++){
					document.getElementById(tabList[j] + "_" + tabList2[i]).style.display = "";
				}
				flg = true;
			} else {
				for (j=0; j<tabList.length; j++){
					document.getElementById(tabList[j] + "_" + tabList2[i]).style.display = "none";
				}
			}
		}
		if (!flg) {
			for (j=0; j<tabList.length; j++){
				document.getElementById(tabList[j] + "_" + tabList2[0]).style.display = "";
			}
		}
		return false;
	}

 	function displaySearch(){
    	if($("current").value == 3){
        	$("search3_sub").style.display="";
    		$("search4").style.display="";
	    	$("current").value=4;
	    	// transfer value
	    	var backupText = Util.getInstance("sg8").getInputText();
	    	Util.getInstance("sg4").setInputText(backupText);
			Util.getInstance("sg4").syncInputText(backupText);
	    	Util.getInstance("sg8").resetText();
	    	
	    	Util.getInstance("sg3").syncId = "sg4";
	    	Util.getInstance("sg5").syncId = "sg8";
	    	return true;
    	}
    	if($("current").value == 4){
    		$("search4_sub").style.display="";
    		$("search5").style.display="";
	    	$("current").value=5;
   			$("addForm").style.display="none";
	    	// transfer value
	    	var backupText = Util.getInstance("sg8").getInputText();
	    	Util.getInstance("sg6").setInputText(backupText);
			Util.getInstance("sg6").syncInputText(backupText);
	    	Util.getInstance("sg8").resetText();

   	    	Util.getInstance("sg5").syncId = "sg6";	
	    	return true;
    	}
 	}
	var previous ="";
	function activateRoundTrip(){
		if(previous != "round" ){
			initValues();
			previous = "round";
		}
    	if($("round").checked ){
	 		$("current").value=2;
    		$("search2").style.display="";
    		$("search2_sub").style.display="none";
    		$("search3").style.display="none";
    		$("search3_sub").style.display="none";
    		$("search4").style.display="none";
    		$("search4_sub").style.display="none";
    		$("search5").style.display="none";
    		$("search_last").style.display="";
    		$("addForm").style.display="none";
    		Util.getInstance("sg1").syncId = "sg8";
    		$("image1").src="images/search/search_flight_going.gif";
    		$("image1").alt="区間1（行き）";
    		$("image2").src="images/search/search_flight_return.gif";
    		$("image2").alt="区間2（帰り）";
    	} 
	}
	
	function activateOneWayTrip(){
		if(previous != "oneway" ){
			initValues();
			previous = "oneway";
		}
    	if($("oneWay").checked ){
	 		$("current").value=1;
    		$("search2").style.display="none";
    		$("search2_sub").style.display="none";
    		$("search3").style.display="none";
    		$("search3_sub").style.display="none";
    		$("search4").style.display="none";
    		$("search4_sub").style.display="none";
    		$("search5").style.display="none";
    		$("search_last").style.display="none";
    		Util.getInstance("sg1").syncId = "";
    		$("image1").src="images/search/search_flight_plan1.gif";
    		$("image1").alt="区間1";
    	} 
	}
	
	function activateOpenTrip(){
		if(previous != "open" ){
			initValues();
			previous = "open";
		}
    	if($("open").checked ){
	 		$("current").value=3;
    		$("search2").style.display="";
    		$("search2_sub").style.display="";
    		$("search3").style.display="";
    		$("search3_sub").style.display="none";
    		$("search4").style.display="none";
    		$("search4_sub").style.display="none";
    		$("search5").style.display="none";
    		$("search_last").style.display="";
    		$("addForm").style.display="";
    		Util.getInstance("sg1").syncId = "sg2";
    		Util.getInstance("sg3").syncId = "sg8";
    		$("image1").src="images/search/search_flight_plan1.gif";
    		$("image1").alt="区間1";
    		$("image2").src="images/search/search_flight_plan2.gif";
    		$("image2").alt="区間2";
    	}
	}
	
	function initValues(){
		Util.getInstance("sg1").resetText();
		Util.getInstance("sg2").resetText();
		Util.getInstance("sg3").resetText();
		Util.getInstance("sg4").resetText();
		Util.getInstance("sg5").resetText();
		Util.getInstance("sg6").resetText();
		Util.getInstance("sg7").resetText();
		Util.getInstance("sg8").resetText();
	}
	
	function makeDestination(id)
	{
		var obj1 = id;
		//var obj2 = Util.getInstance(id).getSyncObj().input.id;
		var url = "";
		url += "js/des.html?obj1="+obj1;
		//url += "&obj2="+obj2
		window.open(url,"des","width=289,height=139,status=no");
	}
	
	function makeMap(id)
	{
		var obj1 = id;
		//var obj2 = Util.getInstance(id).getSyncObj().input.id;
		var url = "";
		url += "js/map.html?obj1="+obj1;
		//url += "&obj2="+obj2
		window.open(url,"map","width=454,height=177,status=no");
	}
	
	function makeCalendar(dateObjName)
	{
		var dateObj = Util.getInstance(dateObjName);
		var url = "";
	
		getStrYear  = dateObj.getYear();
		getStrMonth = dateObj.getMonth();
	
		url = ""
		url += "js/cal.html?year=" + getStrYear + "&mon=" + getStrMonth;
		url += "&oj1="+dateObjName;
		url += "&oj1name="+dateObjName;
		url += "&oj2="+dateObjName;
		url += "&oj2name="+dateObjName;
		url += "&oj3="+dateObjName;
		url += "&oj3name="+dateObjName;
		window.open(url,"cal","width=400,height=280,status=no");
	}
	
	function sync(){
		$("destination5").value = $("departure1").value;
		return ;
	}
	
	function checkForm(){
	    var departure1 = document.getElementById("departure1");
		//空港選択チェック
		if(departure1.value==null || departure1.value=='undefined' || departure1.value==''){
			alert("出発地を選択してください");
			return false; 
		}
		
		var current = document.getElementById("current");
		//入力チェック
		 
		var destination1 = document.getElementById("destination1");
		var destination2 = document.getElementById("destination2");
		var destination3 = document.getElementById("destination3");
		var destination4 = document.getElementById("destination4");
		
		var departure2 = document.getElementById("departure2");
		var departure3 = document.getElementById("departure3");
		var departure4 = document.getElementById("departure4");
		var departure5 = document.getElementById("departure5");
		
		if(destination1.value==null || destination1.value=='undefined' || destination1.value==''){
			alert("目的地1を入力してください");
			return false; 
		}
		if(current.value=='2'){
			if(departure5.value==null || departure5.value=='undefined' || departure5.value==''){
				alert("出発地2を入力してください");
				return false; 
			}
		}
		if(current.value>'2'){
			if(departure2.value==null || departure2.value=='undefined' || departure2.value==''){
				alert("出発地2を入力してください");
				return false; 
			}
			if(destination2.value==null || destination2.value=='undefined' || destination2.value==''){
				alert("目的地2を入力してください");
				return false; 
			}
		}
		if(current.value=='3'){
			if(departure5.value==null || departure5.value=='undefined' || departure5.value==''){
				alert("出発地3を入力してください");
				return false; 
			}						
		}
		if(current.value>'3'){
			if(departure3.value==null || departure3.value=='undefined' || departure3.value==''){
				alert("出発地3を入力してください");
				return false; 
			}
			if(destination3.value==null || destination3.value=='undefined' || destination3.value==''){
				alert("目的地3を入力してください");
				return false; 
			}
		}
		if(current.value=='4'){
			if(departure5.value==null || departure5.value=='undefined' || departure5.value==''){
				alert("出発地4を入力してください");
				return false; 
			}						
		}
		if(current.value>'4'){
			if(departure4.value==null || departure4.value=='undefined' || departure4.value==''){
				alert("出発地4を入力してください");
				return false; 
			}
			if(destination4.value==null || destination4.value=='undefined' || destination4.value==''){
				alert("目的地4を入力してください");
				return false; 
			}
			if(departure5.value==null || departure5.value=='undefined' || departure5.value==''){
				alert("出発地5を入力してください");
				return false; 
			}
		}
		var destination5 = document.getElementById("destination5");
		var oneWay = document.getElementById("oneWay");
		if(!oneWay.checked && (destination5.value==null || destination5.value=='undefined' || destination5.value=='')){
			alert("帰着地を選択してください");
			return false; 
		}
		//日時チェック
		var date1 = Util.getInstance("date1").getDate();
		var date2 = Util.getInstance("date2").getDate();
		var date3 = Util.getInstance("date3").getDate();
		var date4 = Util.getInstance("date4").getDate();
		var date5 = Util.getInstance("date5").getDate();
		if(!Util.getInstance("date1").isValidDate()){
			alert("出発日1に正しい日付を設定してください。");
			return false;
		}
		if(!Util.getInstance("date2").isValidDate()){
			alert("出発日2に正しい日付を設定してください。");
			return false;
		}
		if(!Util.getInstance("date3").isValidDate()){
			alert("出発日3に正しい日付を設定してください。");
			return false;
		}
		if(!Util.getInstance("date4").isValidDate()){
			alert("出発日4に正しい日付を設定してください。");
	    	return false;
		}
		if(!Util.getInstance("date5").isValidDate()){
			alert("出発日5に正しい日付を設定してください。");
			return false;
		}
		var currentDate =  new Date();
		var diff = getDiffDate();
		if( date1<currentDate+diff){
			alert("出発日1は今日より"+diff+"以降に設定してください。");
			return false;
		}
		if( date2 < date1){
			alert("出発日2は出発日1以降に設定してください。");
			return false;
		}
		if( date3 < date2){
			alert("出発日3は出発日2以降に設定してください。");
			return false;
		}
		if( date4 < date3){
			alert("出発日4は出発日3以降に設定してください。");
			return false;
		}
		if( date5 < date4){
			alert("出発日5は出発日4以降に設定してください。");
			return false;
		}
		//人数チェック
		totalNumber = eval(document.getElementById("adultNum").value)+
		              eval(document.getElementById("childNum").value)+ 
		              eval(document.getElementById("infantNum").value);
		if(totalNumber > 6){
			alert("人数が6人以下を設定してください。");
			return false;
		}
		

		//販売日付制限
		//step1 check current date
		var tmpDep1Y = date1.getFullYear();
		var tmpDep1M = date1.getMonth() + 1;
		var tmpDep1D = date1.getDate();
		if ( tmpDep1M < 10 ) {
			tmpDep1M = "0" + tmpDep1M;
		}
		if ( tmpDep1D < 10 ) {
			tmpDep1D = "0" + tmpDep1D;
		}
		var dep1 = tmpDep1Y + tmpDep1M + tmpDep1D;

		var localDate = new Date();
		var localYear	= localDate.getFullYear();
		var localMonth	= localDate.getMonth() + 1;
		var localDay	= localDate.getDate();
		var localHours  = localDate.getHours();
		if ( localMonth < 10 ) {
			localMonth = "0" + localMonth;
		}
		if ( localDay < 10 ) {
			localDay = "0" + localDay;
		}
		var localDateStr = localYear + localMonth + localDay ;
		//alert("localDateStr=" + localDateStr +"   hours=" + localHours);
		if((localDateStr == "20100428" && localHours >= 15) || localDateStr == "20100429" || (localDateStr == "20100430" && localHours < 15)){
			if(dep1 == "20100430" || dep1 == "20100431" || dep1 == "20100501" || dep1 == "20100502" || dep1 == "20100503" || dep1 == "20100504"  || dep1 == "20100505" || dep1 == "20100506" ){
				alert("大変申し訳ございません。\n連休にともなう発券業務停止のため下記日程の予約をお受けすることができません。\n出発日：2010年04月30日から2010年05月07日まで\nご迷惑をおかけいたしますが、今後とも楽天トラベルをよろしくお願いいたします。");
				return false;			
			}

		}else if((localDateStr == "20100430" && localHours >= 15) || localDateStr == "20100431" || localDateStr == "20100501" || localDateStr == "20100502" || localDateStr == "20100503" || localDateStr == "20100504" || localDateStr == "20100505"   ){
			if(dep1 == "20100430" || dep1 == "20100431" || dep1 == "20100501" || dep1 == "20100502" || dep1 == "20100503" || dep1 == "20100504"  || dep1 == "20100505" || dep1 == "20100506"|| dep1 == "20100507" ){
				alert("大変申し訳ございません。\n連休にともなう発券業務停止のため下記日程の予約をお受けすることができません。\n出発日：2010年04月30日から2010年05月07日まで\nご迷惑をおかけいたしますが、今後とも楽天トラベルをよろしくお願いいたします。");
				return false;			
			}
		}		

		document.forms[0].submit();
	}
	
	function getDiffDate(){
	   	var currentDate =  new Date();
		var diff = 3;
		if ( currentDate.getHours() < 15 ){
			diff=2;
		}
		diff=8;
		//年末緊急対応 start 
		var curYear = currentDate.getFullYear();
		var curMonth = currentDate.getMonth() + 1;
		var curDay = currentDate.getDate();
		var targetDate = new Date('2010','0','6');

		if(curYear == "2009" &&  curMonth == "12" && (curDay == "29" || curDay == "30" || curDay == "31")){
			 diff = (targetDate.getDate() + 31) - curDay;
		}
		if(curYear == "2010" &&  curMonth == "1" && (curDay == "1" || curDay == "2" || curDay == "3")){
			diff = (targetDate.getDate()) - curDay;
		}
		//年末緊急対応 end
		return diff;
	}
	
	function MM_openBrWindow(theURL,winName,features) { //v2.0
		window.open(theURL,winName,features);
	}
	
	function MM_preloadImages() { //v3.0
  		var d=document; 
  		if(d.images){ 
  			if(!d.MM_p) d.MM_p=new Array();
    		var i,j=d.MM_p.length,a=MM_preloadImages.arguments; 
    		for(i=0; i<a.length; i++)
    		if (a[i].indexOf("#")!=0){ 
    			d.MM_p[j]=new Image; 
    			d.MM_p[j++].src=a[i];
    		}
    	}
    	//alert(d.MM_p.lenght);
	}
	function rss(){
		window.open("/air/special/rss/index.html","rss",
		"toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=500,height=300,left=100,top=100");
	}
	

