// JavaScript Document
//工具函數，為指定的select對象添加內容
 function addOption(selObj,valueAry,textAry,selectIndex)
 { 
   //selobj:select對象;
  //valueAry:value數組;
  //textAry:text數組;
  //telectIndex:被選擇的option索引;
   var d=document;
   var o=document.getElementById(selObj).options;
   for(i=o.length-1;i>=0;i--)
   o[i]=null;
   for(j=0;j<valueAry.length;j++)
   o[j]=new Option(textAry[j],valueAry[j]);
   o[selectIndex].selected=true;
  }
//添加日曆
//
function setTempLayer(objStr)
{ 
  if(objStr=="FLY_DATE1"||objStr=="FLY_DATE1Anchor")
    TempLayer=2;
   if(objStr=="FLY_DATE2"||objStr=="FLY_DATE2Anchor"||objStr=="RETURN_DATE1"||objStr=="RETURN_DATE1Anchor")
    TempLayer=3;
   if(objStr=="FLY_DATE3"||objStr=="FLY_DATE3Anchor"||objStr=="RETURN_DATE2"||objStr=="RETURN_DATE2Anchor")
    TempLayer=4;
  }
 function addTimeClick(objId,idStr1,idStr2,idStr3,idStr4)
 { 
   //獲取對象；
    var o=document.getElementById(objId);
	//針對ie的處理程式;
	if(isIE)
	 {o.onclick=function()
       {
		event.cancelBubble=true;
        showCalendar(idStr1,false,idStr2,idStr3,idStr4,'2004-12-01','2050-12-30','','','','','','','','1');
		setTempLayer(objId);
		if(o.tagName=="A") return false;

       }
	 }
	 //針對netscape的處理程式;
	 if(isNN)
	 {
		 o.onclick=function()
       {
	     showCalendar(idStr1,false,idStr2,idStr3,idStr4,'2004-12-01','2050-12-30','','','','','','','','1');
		 setTempLayer(objId);
		 if(o.tagName=="A") return false;

       }
	   }
	  //給錨點添加行為
	 if(o.tagName=="A")
	 {o.onmouseout=function(){window.status="";}
	  o.onmouseover=function(){window.status="Calendar";return true;}
	 }
}
//公用函數
function CheckName(Name) {
	var ErrStr;
	var ErrChar;
	var ArryErrStr;
	ErrChar="!@#$%^&*()_+|-=\\~`;'[]{}\"':;,./<>?∼！@#¥％^&×（）……＋|－＝、〔〕｛｝：“；‘《》？，。、";
	ErrStr="虛擬,傻冒,先生,小姐,代訂";
	ArryErrStr = ErrStr.split(",");
	//是否含有非法字符
	for(var k=0;k<Name.length;k++){
		if(ErrChar.indexOf(Name.charAt(k))>-1){
			return false;
		}
	}
	//是否含有非法字符串
	for (k=0; k<ArryErrStr.length; k++){
		if (Name.indexOf(ArryErrStr[k])>-1){
			return false;
		}
	}
	return true;
}

//日期格式檢查
function isDateString(str) {
	var reg=/^\d{4}-[0-1]?\d{1}-[0-3]?\d{1}$/;
	return reg.test(str);
}
function IsDate(DateString , Dilimeter) 
{ 
		
		if (DateString==null) return false; 

		if (Dilimeter=='' || Dilimeter==null) 
			Dilimeter = '-'; 
			
		var tempy=''; 
		var tempm=''; 
		var tempd=''; 
		
		var tempArray; 
		
		if (!isDateString(DateString))
		return false;
		
		if ( DateString.length<8 || DateString.length>10) 
			return false;  
			
		tempArray = DateString.split(Dilimeter); 
		
		if (tempArray.length!=3) 
			return false; 
		if (tempArray[0].length==4) 
			{ 
				tempy = tempArray[0]; 
				tempd = tempArray[2]; 
			} 
		else 
			{ 
				tempy = tempArray[2]; 
				tempd = tempArray[1]; 
			} 
		tempm = tempArray[1]; 
		
		var tDateString = tempy + '/'+tempm + '/'+tempd+' 8:0:0';//加八小時是因為我們處於東八區 
		
		var tempDate = new Date(tDateString); 
		if (isNaN(tempDate)) 
			return false; 
		if (((tempDate.getUTCFullYear()).toString()==tempy) && (tempDate.getMonth()==parseInt(tempm)-1) && (tempDate.getDate()==parseInt(tempd))) 
			{ 
				return true; 
			} 
			else 
			{ 
				
				return false; 
			} 
} 

function CheckDate(strDate1,strDate2) {
	//var tmp1=strDate1.split('-');
	//var tmp2=strDate2.split('-');
	//var date1=eval('new Date('+tmp1[0]+','+tmp1[1]+','+tmp1[2]+')');
	//var date2=eval('new Date('+tmp2[0]+','+tmp2[1]+','+tmp2[2]+')');
	//return (date2>date1);
	return(stringToDate(strDate1, true)<stringToDate(strDate2, true));
}

function stringToDate(sDate, bIgnore)
{	var bValidDate, year, month, day;
	var iaDate = new Array(3);
	
	if (bIgnore) 
	 bValidDate = true
	else
	 bValidDate = isDateString(sDate);
	
	if (bValidDate)
	{  iaDate = sDate.toString().split("-");
		year = parseFloat(iaDate[0]);
		month = parseFloat(iaDate[1]) - 1;
		day=parseFloat(iaDate[2]);
		return (new Date(year,month,day));
	}
	else 
	return (new Date(1900,1,1));
}

function compareDate(strDate1,strDate2) {
	var tmp1=strDate1.split('-');
	var tmp2=strDate2.split('-');
	var date1=eval('new Date('+tmp1[0]+','+tmp1[1]+','+tmp1[2]+')');
	var date2=eval('new Date('+tmp2[0]+','+tmp2[1]+','+tmp2[2]+')');
	if ((date2.valueOf()-date1.valueOf())>=20*24*60*60*1000)
	{
		return true;
	}
	else
	{
		return false;
	}
}

function compareDate1(strDate1,strDate2) {
	var tmp1=strDate1.split('-');
	var tmp2=strDate2.split('-');
	var date1=eval('new Date('+tmp1[0]+','+tmp1[1]+','+tmp1[2]+')');
	var date2=eval('new Date('+tmp2[0]+','+tmp2[1]+','+tmp2[2]+')');
	if ((date2.valueOf()-date1.valueOf())>=60*24*60*60*1000)
	{
		return true;
	}
	else
	{
		return false;
	}
}
function key_up1()
{
	var s=document.hotelsearch.CityName.value;
	var b="";
	var a="";
	if (/[^\x00-\xff]/g.test(s))
	{
		//alert("含有漢字");
	}
	else
	{
		a=s.substr(0,1)
		a=a.toUpperCase();
		b=s.substr(1);
		b=b.toLowerCase();
		document.hotelsearch.CityName.value=a+b;
			//alert("全是字符");
	}
}
function compareFlyDate(strDate1,strDate2) {
	var tmp1=strDate1.split('-');
	var tmp2=strDate2.split('-');
	var date1=eval('new Date('+tmp1[0]+','+tmp1[1]+','+tmp1[2]+')');
	var date2=eval('new Date('+tmp2[0]+','+tmp2[1]+','+tmp2[2]+')');
	if ((date2.valueOf()-date1.valueOf())>=0)
	{
		return true;
	}
	else
	{
		return false;
	}
}

//表單檢查程式
//酒店表但檢查
function chkHotelForm(objForm)
{
	//WriteHotelCookie();
	if (objForm.CityName.value=="") {
		alert("請輸入城市！");
		objForm.CityName.focus();
		return false;
	}
	if (objForm.CityName.value!=""){
		if (CheckName(objForm.CityName.value)==false){
			objForm.CityName.focus();
			alert("城市名稱中含有非法字符！");
			return false;
		}
	}	
	if (! isDateString(objForm.CheckInDate.value)) {
		alert('日期格式不正確，請重新填寫！');
		objForm.CheckInDate.focus();
		return false;
	}
	if (! isDateString(objForm.CheckOutDate.value)) {
		alert('日期格式不正確，請重新填寫！');
		objForm.CheckOutDate.focus();
		return false;
	}
/*	
	if (! IsDate(objForm.CheckInDate.value)) {
		alert('無效的日期，請重新填寫！');
		objForm.CheckInDate.focus();
		return false;
	}
	if (! IsDate(objForm.CheckOutDate.value)) {
		alert('無效的日期，請重新填寫！');
		objForm.CheckOutDate.focus();
		return false;
	}
*/
	if (! CheckDate(preday,objForm.CheckInDate.value)) {
		alert('入住日期不能小于當前日期！');
		objForm.CheckInDate.focus();
		return false;
	}

	if (! CheckDate(preday,objForm.CheckOutDate.value)) {
		alert('離店日期不能小于當前日期！');
		objForm.CheckOutDate.focus();
		return false;
	}	

	if (! CheckDate(objForm.CheckInDate.value,objForm.CheckOutDate.value)) {

		alert('離店日期必須大於入住日期！');
		objForm.CheckInDate.focus();
		return false;
	}
	if (compareDate(objForm.CheckInDate.value,objForm.CheckOutDate.value))
	{
		alert('如果您需要在酒店入住20天以上，請和東遊酒店預訂中心聯繫：'+GetPicName()+'，我們會竭誠為您服務。');
		objForm.CheckInDate.focus();
		return false;
	}
	if (compareDate1(nowday,objForm.CheckInDate.value))
	{
		alert('如果您需要預定晚于或等於'+objForm.CheckInDate.value+'號的酒店，請您致電東遊預訂中心'+GetPicName()+'，我們會竭誠為您服務');
		objForm.CheckInDate.focus();
		return false;
	}
	if (objForm.HotelName.value!=""){
		if (CheckName(objForm.HotelName.value)==false){
			objForm.HotelName.focus();
			alert("酒店名稱中含有非法字符！");
			return false;
		}
	}		
	return true;
}

