function maxDays(mm, yy){
	var mDay;htlInrCookie
	switch(mm){
	case 1:	case 3:	case 5:	case 7:	case 8:	case 10:case 12:
		mDay = 31;
		return mDay;
	case 4:	case 6:	case 9:	case 11:
		mDay = 30;
		return mDay;
	case 2:
		if (yy/4 - parseInt(yy/4) != 0){//判斷是否潤年
			mDay = 28;
		}else{
			mDay = 29;
		}
		return mDay;
	}
}	
function sYearChange(htlFormObj){
	htlFormObj.SEL_OUT_Y.selectedIndex = htlFormObj.SEL_IN_Y.selectedIndex;
}
function sMonthChange(htlFormObj) {
	var cmonDay,cnextDay,ctmpChkDay;
	cmonDay = maxDays(Number(htlFormObj.SEL_IN_M.value),  htlFormObj.SEL_IN_Y.value);//目前這個月的天數
	ctmpChkDay = htlFormObj.SEL_IN_D.value;
	htlFormObj.SEL_IN_D.options.length = 0;
	htlFormObj.SEL_IN_D.options[0] = new Option("不限","");
	for( i=1; i<=cmonDay; i++){
		if ( i < 10 )
			htlFormObj.SEL_IN_D.options[i]=new Option("0" + i + "日","0"+i);
		else
			htlFormObj.SEL_IN_D.options[i]=new Option(i + "日",i);
	}
	if ( Number(ctmpChkDay) >= Number(cmonDay) ){
		htlFormObj.SEL_IN_D.selectedIndex = cmonDay;
		htlFormObj.SEL_OUT_M.selectedIndex = Number(htlFormObj.SEL_IN_M.value ) + 1;
		nextMonthDay(htlFormObj);
		htlFormObj.SEL_OUT_D.selectedIndex = 1;
	}else{
		htlFormObj.SEL_IN_D.selectedIndex = ctmpChkDay;
		htlFormObj.SEL_OUT_M.selectedIndex = Number(htlFormObj.SEL_IN_M.value );
		nextMonthDay(htlFormObj);
		htlFormObj.SEL_OUT_D.selectedIndex = Number(htlFormObj.SEL_IN_D.selectedIndex) + 1;
	}
} 


function sDayChange(htlFormObj) {
	if ( Number(htlFormObj.SEL_IN_D.value) == Number(maxDays(Number(htlFormObj.SEL_IN_M.value),  htlFormObj.SEL_IN_Y.value))){
		htlFormObj.SEL_OUT_M.selectedIndex = Number(htlFormObj.SEL_IN_M.value ) + 1;
		nextMonthDay(htlFormObj);
		htlFormObj.SEL_OUT_D.selectedIndex = 1;
	}else{
		htlFormObj.SEL_OUT_M.selectedIndex = Number(htlFormObj.SEL_IN_M.value );
		htlFormObj.SEL_OUT_D.selectedIndex = Number(htlFormObj.SEL_IN_D.selectedIndex) + 1;
	}
}   
function eYearChange(htlFormObj){
	htlFormObj.SEL_IN_Y.selectedIndex = htlFormObj.SEL_OUT_Y.selectedIndex;
}
function eMonthChange(htlFormObj) {
	var emonDay,enextDay,etmpChkDay;
	emonDay = maxDays(Number(htlFormObj.SEL_OUT_M.value),  htlFormObj.SEL_OUT_Y.value);//目前這個月的天數
	etmpChkDay = htlFormObj.SEL_OUT_D.value;
	htlFormObj.SEL_OUT_D.options.length = 0;
	htlFormObj.SEL_OUT_D.options[0] = new Option("不限","");
	for( i=1; i<=emonDay; i++){
		if ( i < 10 )
			htlFormObj.SEL_OUT_D.options[i]=new Option("0" + i + "日","0"+i);
		else
			htlFormObj.SEL_OUT_D.options[i]=new Option(i + "日",i);
	}
	if ( Number(etmpChkDay) >= Number(emonDay) ){
		htlFormObj.SEL_OUT_D.selectedIndex = emonDay;
	}else{
		htlFormObj.SEL_OUT_D.selectedIndex = etmpChkDay;
	}
} 

function nextMonthDay(htlFormObj){
	cnextDay = maxDays(Number(htlFormObj.SEL_OUT_M.value ),  htlFormObj.SEL_IN_Y.value);//下個月的天數
	htlFormObj.SEL_OUT_D.options.length = 0;
	htlFormObj.SEL_OUT_D.options[0] = new Option("不限","");
	for( i=1; i<=cnextDay; i++){
	if ( i < 10 )
		htlFormObj.SEL_OUT_D.options[i]=new Option("0" + i + "日","0"+i);
	else
		htlFormObj.SEL_OUT_D.options[i]=new Option(i + "日",i);
	}
}
function searchInit(){
  var formObj1 = document.htl ;
  var inner_agent = GetCookie("inner_agent");
  var htlInrCookie = GetCookie("htlInrCookie");
  var htlInrList;
  var hins1=null,hins2=null,hinm1=null,hinm2=null,hind1=null,hind2=null;
  var natn1=null,city1=null,roomtype=null,babyroom=null,htlcl=null;
  if (htlInrCookie!=null){
    htlInrList = htlInrCookie.split(",");
    hins1 = htlInrList[2];
    hinm1 = htlInrList[3];
    hind1 = htlInrList[4];
    hins2 = htlInrList[5];
    hinm2 = htlInrList[6];
    hind2 = htlInrList[7];
    roomtype = htlInrList[8];
    babyroom = htlInrList[9];
    htlcl = htlInrList[10];
  }
	for(var i=0;i<Htlseacharr.length-1;i++){
		if(Htlseacharr[i][2]=='TOP' ){
		  formObj1.SEL_NATN1.options[formObj1.SEL_NATN1.options.length]=new Option(Htlseacharr[i][1],Htlseacharr[i][0]);
		}
	}
	
	
	
  formObj1.SEL_NATN1.selectedIndex = 0 ;
  formObj1.SEL_CITY1.selectedIndex = 0 ;
  formObj1.SEL_IN_Y.options.length=0;
  formObj1.SEL_OUT_Y.options.length=0;
  formObj1.SEL_IN_Y.options[formObj1.SEL_IN_Y.options.length]= new Option("不限","");
  formObj1.SEL_IN_Y.options[formObj1.SEL_IN_Y.options.length]= new Option(year0+"年",year0);
  formObj1.SEL_IN_Y.options[formObj1.SEL_IN_Y.options.length]= new Option(year1+"年",year1);
  formObj1.SEL_OUT_Y.options[formObj1.SEL_OUT_Y.options.length]= new Option("不限","");
  formObj1.SEL_OUT_Y.options[formObj1.SEL_OUT_Y.options.length]= new Option(year0+"年",year0);
  formObj1.SEL_OUT_Y.options[formObj1.SEL_OUT_Y.options.length]= new Option(year1+"年",year1);

  var nowY = parseInt(today0.substring(0,4),10);  // 今天年份
  //==========2009-06-17 Ian 住宿日期調整為加三天===============
  //var startY = parseInt(today5.substring(0,4),10);  // 加五天日期
  //var startM = parseInt(today5.substring(4,6),10);
  //var startd = parseInt(today5.substring(6,8),10);

  //var endY = parseInt(today6.substring(0,4),10);  // 加六天日期
  //var endM = parseInt(today6.substring(4,6),10);
  //var endd = parseInt(today6.substring(6,8),10);

  var startY = parseInt(today3.substring(0,4),10);  // 加三天日期
  var startM = parseInt(today3.substring(4,6),10);
  var startd = parseInt(today3.substring(6,8),10);

  var endY = parseInt(today4.substring(0,4),10);  // 加三天日期
  var endM = parseInt(today4.substring(4,6),10);
  var endd = parseInt(today4.substring(6,8),10);

  
  if ( startY != nowY ) {  // 跨年判別
	formObj1.SEL_IN_Y.selectedIndex = hins1==null?2:parseInt(hins1,10);
  } else {
	formObj1.SEL_IN_Y.selectedIndex = hins1==null?1:parseInt(hins1,10);
  }
  
  formObj1.SEL_IN_M.selectedIndex = hinm1==null?startM:parseInt(hinm1,10);
  formObj1.SEL_IN_D.selectedIndex = hind1==null?startd:parseInt(hind1,10);

  formObj1.SEL_OUT_Y.selectedIndex =  hins2==null?endY-year0+1:parseInt(hins2,10);
  formObj1.SEL_OUT_M.selectedIndex = hinm2==null?endM:parseInt(hinm2,10)  ;
  formObj1.SEL_OUT_D.selectedIndex = hind2==null?endd:parseInt(hind2,10)  ;
  
  if ( roomtype!=null ) formObj1.SEL_ROOM_H.selectedIndex = roomtype;
  if ( roomtype!=null ) formObj1.BABY_ROOM_H.selectedIndex = babyroom;
  if ( htlcl!=null) formObj1.HTL_CL.selectedIndex = htlcl;  
  
  

  if ( inner_agent != null ) {
    if (inner_agent == 'htlInr') {  
      natn1 = htlInrList[0];
      city1 = htlInrList[1];
      for(i=0;i<formObj1.SEL_NATN1.length ; i++){
        if (formObj1.SEL_NATN1.options[i].value == natn1) {
          formObj1.SEL_NATN1.selectedIndex = i ;
          break ;
        }
      }                  
      setHArea(formObj1.SEL_NATN1,2,formObj1);
      for(i=0;i<formObj1.SEL_CITY1.length ; i++){
        if (formObj1.SEL_CITY1.options[i].value == city1) {
          formObj1.SEL_CITY1.selectedIndex = i ;
          break ;
        }
      }
    }
  } 
  DeleteCookie("htlInrCookie"); 
  DeleteCookie("inner_agent");   
    
}



function setHArea (obj,kind,formObj1){
	var arrData = Htlseacharr ;
	var obj1 = formObj1.SEL_CITY1 ;
	if (kind == 1){
	  obj1.options.length=0;
	  obj1.options[0]=new Option("選擇城市","");
    for(var i=0;i<arrData.length-1;i++){
      if( arrData[i][2]==obj.value ){
        if ( kind == 1 ){
          obj1.options[obj1.options.length]=new Option(arrData[i][1],arrData[i][0]);
        }
      }
    }
  }
}


function checkGoHtl(formObj){
	if (formObj.SEL_NATN1.selectedIndex == 0) {
	  alert("請選擇國家");
		formObj.SEL_NATN1.focus();
		return false;
	}
	if (formObj.SEL_CITY1.selectedIndex == 0) {
	  alert("請選擇城市");
		formObj.SEL_CITY1.focus();
		return false;
	}
	var checkInDate = new Date(formObj.SEL_IN_Y.value,Number(formObj.SEL_IN_M.value) -1,formObj.SEL_IN_D.value);//入住日
	var checkOutDate = new Date(formObj.SEL_OUT_Y.value,Number(formObj.SEL_OUT_M.value) -1,formObj.SEL_OUT_D.value);//退房日
	//從今天起5天後的日期
	//===2008-02-20 Hamburger 調整日期限制由五天後調整為10天後 ===
	//var monDate = new Date( 5 * 86400000 + ( new Date()).getTime());
	var monDate = new Date( 2 * 86400000 + ( new Date()).getTime());
	//============================================================
	if ( checkInDate < monDate ){
	  //===2008-02-20 Hamburger 錯誤訊息調整 ===
	  //alert("入住日期需大於" + Number(monDate.getYear() + 1900) + "年" + Number(monDate.getMonth() +1) + "月" + monDate.getDate() + "日");
	  alert("入住日期需大於" + Number(monDate.getYear() + 1900) + "年" + Number(monDate.getMonth() +1) + "月" + monDate.getDate() + "日");
	  //========================================
	  return false;
  }
	if ( checkOutDate <= checkInDate ){
	  alert("退房日期需大於入住日期");
	  return false;
  }
  if ( parseInt(Math.abs(checkOutDate  -  checkInDate)  /  1000  /  60  /  60  /24) > 8 ){
    alert("住宿日期最多8日");
    return false;
  }
	if ( formObj.SEL_ROOM_H.selectedIndex == 0 && formObj.BABY_ROOM_H.selectedIndex != 0 ){
	  alert("單人房間內不能夠加床(兒童或成人)");
	  return false;
	}

	SetCookie("search_agent","htl",new Date(today.getTime() +  24 * 60 * 60 * 1000));
	var htlstr = formObj.SEL_NATN1.value + "," + formObj.SEL_CITY1.value + ",";
	htlstr = htlstr + formObj.SEL_IN_Y.selectedIndex + "," + formObj.SEL_IN_M.selectedIndex + "," + formObj.SEL_IN_D.selectedIndex + ",";
	htlstr = htlstr + formObj.SEL_OUT_Y.selectedIndex + "," + formObj.SEL_OUT_M.selectedIndex + "," + formObj.SEL_OUT_D.selectedIndex + ",";
	htlstr = htlstr + formObj.SEL_ROOM_H.selectedIndex + "," + formObj.BABY_ROOM_H.selectedIndex + "," + formObj.HTL_CL.selectedIndex;
	SetCookie("htlInrCookie",htlstr,new Date(today.getTime() +  24 * 60 * 60 * 1000));	
	return true ;	
	
	
}
document.write("<form action=\"/ggogoWeb/htlSearch.do\" method=\"get\" name=\"htl\" target=\"_parent\" onSubmit=\"return checkGoHtl(this);\">");
document.write("<table width=\"212\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");
document.write("<tr> ");
document.write("<td height=\"10\" colspan=\"3\"></td>");
document.write("</tr>");
document.write("<tr><tr><td colspan=\"3\" class=\"t_bD\">&nbsp;&nbsp;<img src=\"/img05/a02.gif\" align=\"absmiddle\"> 國 外 訂 房 搜 尋</td>");
document.write("</tr>");
document.write("<tr><td></td><td><br>請選擇目的地：<br></td><td></td></tr>");
document.write("<tr><td></td><td height=\"30\" colspan=\"2\"><select name=\"SEL_NATN1\" onChange=\"setHArea(this,1,this.form)\">");
document.write("<option value=\"\" selected>選擇國家</option>");
document.write("</select></td></tr>");
document.write("<tr><td></td><td height=\"30\" colspan=\"2\"><select name=\"SEL_CITY1\">");
document.write("<option value=\"\" selected>選擇城市</option>");
document.write("</select></td>");
document.write("</tr>");

// document.write("<tr> ");
// document.write("<td></td>");
// document.write("<td height=\"30\">飯店級別</td>");
// document.write("<td><select name=\"HTL_CL\">");
// document.write("<option value=\"\" selected>不限</option>");
// document.write("<option value=\"1\">一星級</option>");
// document.write("<option value=\"2\">二星級</option>");
// document.write("<option value=\"3\">三星級</option>");
// document.write("<option value=\"4\">四星級</option>");
// document.write("<option value=\"5\">五星級</option>");
// document.write("</select></td>");
// document.write("</tr>");
document.write("<input type='hidden' name='HTL_CL' value=''>") ;
// document.write("<tr> ");
// document.write("<td></td>");
// document.write("<td height=\"30\">房型</td>");
// document.write("<td><select name=\"SEL_ROOM_H\" >");
// document.write("<option value=\"1\" >單人房</option>");
// document.write("<option value=\"2\" selected>雙人房</option>");
// document.write("<option value=\"3\" >三人房</option>");
// document.write("<option value=\"4\">四人房</option>");
// document.write("</select> </td>");
// document.write("</tr>");
document.write("<input type='hidden' name='SEL_ROOM_H' value='2'>") ;
// document.write("<tr> ");
// document.write("<td></td>");
// document.write("<td height=\"30\">嬰兒床</td>");
// document.write("<td><select name=\"BABY_ROOM_H\" >");
// document.write("<option value=\"0\" selected>0張</option>");
// document.write("<option value=\"1\">1張</option>");
// document.write("<option value=\"2\">2張</option>");
// document.write("</select></td>");
// document.write("</tr>");
document.write("<input type='hidden' name='BABY_ROOM_H' value='0'>") ;

document.write("<tr> ");
document.write("<td></td>");
document.write("<td><br>請選擇住宿日期：</td>");
document.write("<td>&nbsp;</td>");
document.write("</tr>");
document.write("<tr> ");
document.write("<td></td>");
document.write("<td>起始日</td>");
document.write("<td>&nbsp;</td>");
document.write("</tr>");
document.write("<tr> ");
document.write("<td></td>");
document.write("<td height=\"30\" colspan=\"2\"><select name=\"SEL_IN_Y\"  onChange=\"sYearChange(this.form);\">");
document.write("<option value=\"0\" selected>不限</option>");
document.write("<option value=\"2005\" >2005年</option>");
document.write("<option value=\"2006\" >2006年</option>");
document.write("</select> <select name=\"SEL_IN_M\"  onChange=\"sMonthChange(this.form);\">");
document.write("<option value=\"0\" selected>不限</option>");
document.write("<option value=\"01\" >01月</option>");
document.write("<option value=\"02\" >02月</option>");
document.write("<option value=\"03\" >03月</option>");
document.write("<option value=\"04\" >04月</option>");
document.write("<option value=\"05\" >05月</option>");
document.write("<option value=\"06\" >06月</option>");
document.write("<option value=\"07\" >07月</option>");
document.write("<option value=\"08\" >08月</option>");
document.write("<option value=\"09\" >09月</option>");
document.write("<option value=\"10\" >10月</option>");
document.write("<option value=\"11\" >11月</option>");
document.write("<option value=\"12\" >12月</option>");
document.write("</select> <select name=\"SEL_IN_D\"  onChange=\"sDayChange(this.form);\">");
document.write("<option value=\"0\" selected>不限</option>");
document.write("<option value=\"01\" >01日</option>");
document.write("<option value=\"02\" >02日</option>");
document.write("<option value=\"03\" >03日</option>");
document.write("<option value=\"04\" >04日</option>");
document.write("<option value=\"05\" >05日</option>");
document.write("<option value=\"06\" >06日</option>");
document.write("<option value=\"07\" >07日</option>");
document.write("<option value=\"08\" >08日</option>");
document.write("<option value=\"09\" >09日</option>");
document.write("<option value=\"10\" >10日</option>");
document.write("<option value=\"11\" >11日</option>");
document.write("<option value=\"12\" >12日</option>");
document.write("<option value=\"13\" >13日</option>");
document.write("<option value=\"14\" >14日</option>");
document.write("<option value=\"15\" >15日</option>");
document.write("<option value=\"16\" >16日</option>");
document.write("<option value=\"17\" >17日</option>");
document.write("<option value=\"18\" >18日</option>");
document.write("<option value=\"19\" >19日</option>");
document.write("<option value=\"20\" >20日</option>");
document.write("<option value=\"21\" >21日</option>");
document.write("<option value=\"22\" >22日</option>");
document.write("<option value=\"23\" >23日</option>");
document.write("<option value=\"24\" >24日</option>");
document.write("<option value=\"25\" >25日</option>");
document.write("<option value=\"26\" >26日</option>");
document.write("<option value=\"27\" >27日</option>");
document.write("<option value=\"28\" >28日</option>");
document.write("<option value=\"29\" >29日</option>");
document.write("<option value=\"30\" >30日</option>");
document.write("<option value=\"31\" >31日</option>");
document.write("</select> </td>");
document.write("</tr>");

document.write("<tr> ");
document.write("<td></td>");
document.write("<td>終止日</td>");
document.write("<td>&nbsp;</td>");
document.write("</tr>");

document.write("<tr> ");
document.write("<td></td>");
document.write("<td height=\"30\" colspan=\"2\"> <select name=\"SEL_OUT_Y\"  onChange=\"eYearChange(this.form);\">");
document.write("<option value=\"0\" selected>不限</option>");
document.write("<option value=\"2005\" >2005年</option>");
document.write("<option value=\"2006\" >2006年</option>");
document.write("</select> <select name=\"SEL_OUT_M\"  onChange=\"eMonthChange(this.form);\">");
document.write("<option value=\"0\" selected>不限</option>");
document.write("<option value=\"01\" >01月</option>");
document.write("<option value=\"02\" >02月</option>");
document.write("<option value=\"03\" >03月</option>");
document.write("<option value=\"04\" >04月</option>");
document.write("<option value=\"05\" >05月</option>");
document.write("<option value=\"06\" >06月</option>");
document.write("<option value=\"07\" >07月</option>");
document.write("<option value=\"08\" >08月</option>");
document.write("<option value=\"09\" >09月</option>");
document.write("<option value=\"10\" >10月</option>");
document.write("<option value=\"11\" >11月</option>");
document.write("<option value=\"12\" >12月</option>");
document.write("</select> <select name=\"SEL_OUT_D\">");
document.write("<option value=\"0\" selected>不限</option>");
document.write("<option value=\"01\" >01日</option>");
document.write("<option value=\"02\" >02日</option>");
document.write("<option value=\"03\" >03日</option>");
document.write("<option value=\"04\" >04日</option>");
document.write("<option value=\"05\" >05日</option>");
document.write("<option value=\"06\" >06日</option>");
document.write("<option value=\"07\" >07日</option>");
document.write("<option value=\"08\" >08日</option>");
document.write("<option value=\"09\" >09日</option>");
document.write("<option value=\"10\" >10日</option>");
document.write("<option value=\"11\" >11日</option>");
document.write("<option value=\"12\" >12日</option>");
document.write("<option value=\"13\" >13日</option>");
document.write("<option value=\"14\" >14日</option>");
document.write("<option value=\"15\" >15日</option>");
document.write("<option value=\"16\" >16日</option>");
document.write("<option value=\"17\" >17日</option>");
document.write("<option value=\"18\" >18日</option>");
document.write("<option value=\"19\" >19日</option>");
document.write("<option value=\"20\" >20日</option>");
document.write("<option value=\"21\" >21日</option>");
document.write("<option value=\"22\" >22日</option>");
document.write("<option value=\"23\" >23日</option>");
document.write("<option value=\"24\" >24日</option>");
document.write("<option value=\"25\" >25日</option>");
document.write("<option value=\"26\" >26日</option>");
document.write("<option value=\"27\" >27日</option>");
document.write("<option value=\"28\" >28日</option>");
document.write("<option value=\"29\" >29日</option>");
document.write("<option value=\"30\" >30日</option>");
document.write("<option value=\"31\" >31日</option>");
document.write("</select></td>");
document.write("</tr>");
document.write("<tr> ");
document.write("<td></td>");
document.write("<td height=\"30\" colspan=\"2\" align=\"center\"><br><input type=\"Submit\" name=\"sb1\" value=\"商品查詢\">");
document.write("&nbsp; <input type=\"reset\" name=\"sb2\" value=\"重填\"> ");
// document.write("&nbsp; <a href=\"http://www.ggogo.com/ht/ht_global.htm\" class=\"l_b\" target=\"_blank\">更多訂房</a> ");
document.write("</td>");
document.write("</tr>");
document.write("<tr> ");
document.write("<td height=\"10\" colspan=\"3\"></td>");
document.write("</tr>");
document.write("</table>");
document.write("</form>");