// JavaScript Document
//设置搜索图片切换
var imgHotel=new Image();imgHotel.src="images/ButtonHotelA.gif";
var imgHotelB=new Image();imgHotelB.src="images/ButtonHotelB.gif";
var imgAir=new Image();imgAir.src="images/ButtonFlightA.gif";
var imgAirB=new Image();imgAirB.src="images/ButtonFlightB.gif";
var imgTours=new Image();imgTours.src="images/ButtonTrainA.gif";
var imgToursB=new Image();imgToursB.src="images/ButtonTrainB.gif";

//工具函数,用于切换显示
function whichShow(obj1,arg1,obj2,arg2,obj3,arg3)
{
 var arg=whichShow.arguments;
 for(count=0;count<arg.length;count+=2)
 {var obj=document.getElementById(arg[count]).style;
    obj.display=arg[count+1]=="block"?"block":"none";
   }
 if(obj1=="air_main_1"&&obj2=="air_main_2"&&obj3=="air_main_3")
	WriteAirCookie();
	ReadAirCookie();
}
function getCookie(name)
{
    var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
    if(arr !=null) return unescape(arr[2]); return null;
}


//
//显示hotel，隐藏flight,tour
function showHotel(objA,argA,objB,argB,objC,argC)
{
	WriteAirCookie();
	ReadHotelCookie();
	document.ihotel.src=imgHotel.src;
	document.iair.src=imgAirB.src;
	document.itours.src=imgToursB.src;
	whichShow(objA,argA,objB,argB,objC,argC);
}
//
//显示flight,隐藏hotel,tours
function showAir(objA,argA,objB,argB,objC,argC)
{
	WriteHotelCookie();
	ReadAirCookie();
	document.ihotel.src=imgHotelB.src;
	document.iair.src=imgAir.src;
	document.itours.src=imgToursB.src;
	whichShow(objA,argA,objB,argB,objC,argC);
	document.forms["airsearch"].elements["FLY_TYPE"][1].checked=true;
	whichShow('air_main_1','none','air_main_2','block','air_main_3','none');
	}
//
//显示tours,隐藏hote,flight
function showTrain(objA,argA,objB,argB,objC,argC)
{
	WriteHotelCookie();//写cookie
	WriteAirCookie();//写cookie
	document.ihotel.src=imgHotelB.src;
	document.iair.src=imgAirB.src;
	document.itours.src=imgTours.src;
	whichShow(objA,argA,objB,argB,objC,argC);
//	ProdTypeChanged(document.forms["form1"].elements["RadioGroup"][0].value);document.form1.prodType.value=document.forms["form1"].elements["RadioGroup"][0].value
}
//
//工具函数，为指定的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=3;
   if(objStr=="FLY_DATE2"||objStr=="FLY_DATE2Anchor"||objStr=="RETURN_DATE1"||objStr=="RETURN_DATE1Anchor")
    TempLayer=4;
   if(objStr=="FLY_DATE3"||objStr=="FLY_DATE3Anchor"||objStr=="RETURN_DATE2"||objStr=="RETURN_DATE2Anchor")
    TempLayer=5;
  }
 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','2007-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','2007-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="!@#$%^&*()_+|-=\\~`;'[]{}\"':;,./<>?～！@#￥％^&×（）……＋|－＝、〔〕｛｝：“；‘《》？，。、0123456789";
	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.raCityName.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.raCityName.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.raCityName.value=="") {
		alert("请输入城市！");
		objForm.raCityName.focus();
		return false;
	}
	if (objForm.raCityName.value!=""){
		if (CheckName(objForm.raCityName.value)==false){
			objForm.raCityName.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天以上，请和e龙酒店预订中心联系：8008101010或01064329999，我们会竭诚为您服务。');
		objForm.CheckInDate.focus();
		return false;
	}
	if (compareDate1(nowday,objForm.CheckInDate.value))
	{
		alert('如果您需要预定晚于或等于'+objForm.CheckInDate.value+'号的酒店，请您致电e龙预订中心010-64329999、8008101010，我们会竭诚为您服务');
		objForm.CheckInDate.focus();
		return false;
	}
	if (objForm.HotelName.value!=""){
		if (CheckName(objForm.HotelName.value)==false){
			objForm.HotelName.focus();
			alert("酒店名称中含有非法字符！");
			return false;
		}
	}		
	return true;
}
//机票单程检查
function chkAirForm1(objForm) {
//	WriteAirCookie();
	if (objForm.city1.value==objForm.city2.value) {
		alert('出发城市不能与目的城市相同！');
		return false;
	}
	if (! isDateString(objForm.FLY_DATE1.value)) {
		alert('日期格式不正确，请重新填写！');
		objForm.FLY_DATE1.focus();
		return false;
	}
	if (!CheckDate(preday,objForm.FLY_DATE1.value)) { 
		alert('出发日期不能早于当前日期！');
		objForm.FLY_DATE1.focus();
		return false;
	}
	return true;
}
//机票往返检查
function chkAirForm2(objForm) {
	WriteAirCookie();
	if (objForm.city3.value==objForm.city4.value) {
		alert('出发城市不能与目的城市相同！');
		return false;
	}
	if (! isDateString(objForm.FLY_DATE2.value)) {
		alert('日期格式不正确，请重新填写！');
		objForm.FLY_DATE2.focus();
		return false;
	}
	if (! isDateString(objForm.RETURN_DATE1.value)) {
		alert('日期格式不正确，请重新填写！');
		objForm.RETURN_DATE1.focus();
		return false;
	}
	if (!CheckDate(preday,objForm.FLY_DATE2.value)) { 
		alert('出发日期不能早于当前日期！');
		objForm.FLY_DATE2.focus();
		return false;
	}
	if (! compareFlyDate(objForm.FLY_DATE2.value,objForm.RETURN_DATE1.value)) {
		alert('出发日期不能晚于返程日期！');
		return false;
	}
	return true;
}
//机票连程检查
function chkAirForm3(objForm) {
	WriteAirCookie();
	if (objForm.city5.value==objForm.city6.value) {
		alert('出发城市不能与目的城市相同！');
		return false;
	}
	if (objForm.city6.value==objForm.city7.value) {
		alert('到达城市1不能与到达城市2相同！');
		return false;
	}
	if (! isDateString(objForm.FLY_DATE3.value)) {
		alert('日期格式不正确，请重新填写！');
		objForm.FLY_DATE3.focus();
		return false;
	}
	if (! isDateString(objForm.RETURN_DATE2.value)) {
		alert('日期格式不正确，请重新填写！');
		objForm.RETURN_DATE2.focus();
		return false;
	}
	if (!CheckDate(preday,objForm.FLY_DATE3.value)) {
		alert('出发日期1不能早于当前日期！');
		objForm.FLY_DATE3.focus();
		return false;
	}
	if (! compareFlyDate(objForm.FLY_DATE3.value,objForm.RETURN_DATE2.value)) {
		alert('出发日期1不能晚于出发日期2！');
		return false;
	}
	return true;
}
//内容检查
