// JavaScript Document
//
var Then = new Date();
Then.setTime(Then.getTime()+60*1000*60*24);

function addCookie(name,value,expireDays)
{
  var cookieString=name+"="+escape(value);
  //判断是否设置过期时间
  if(expireDays>0){
         var date=new Date();
         date.setTime(date.getTime()+expireDays*24*3600*1000);
         cookieString=cookieString+"; expires="+date.toGMTString();
  }
  document.cookie=cookieString;
}


//写酒店
function WriteHotelCookie()
{
	var hotelStr="";
	if(idObj("raCityName").value!="")
		hotelStr=hotelStr+"raCityName|"+idObj("raCityName").value+",";
	if(idObj("CheckInDate").value!=""||idObj("CheckInDate").value!="yyyy-mm-dd")
		hotelStr=hotelStr+"CheckInDate|"+idObj("CheckInDate").value+",";
	if(idObj("CheckOutDate").value!=""||idObj("CheckOutDate").value!="yyyy-mm-dd")
		hotelStr=hotelStr+"CheckOutDate|"+idObj("CheckOutDate").value;
	if(idObj("Price")!=null && idObj("Price").value!="")
		hotelStr=hotelStr+",Price|"+idObj("Price").value;
	if(idObj("Star")!=null && idObj("Star").value!="")
		hotelStr=hotelStr+",star|"+idObj("Star").value;
	if(idObj("RoomNum")!=null && idObj("RoomNum").value!="")
		hotelStr=hotelStr+",RoomNum|"+idObj("RoomNum").value;
	if(idObj("PersonNum")!=null && idObj("PersonNum").value!="")
		hotelStr=hotelStr+",PersonNum|"+idObj("PersonNum").value;

  addCookie("elong_hotel_cookie",hotelStr,1);
	//if(hotelStr!="")
	//	document.cookie = "elong_hotel_cookie="+escape(hotelStr)+";expires=" + Then.toGMTString() + ";path="+ "/" + ";domain="+"elong.com";
	//alert(document.cookie);
}

//读酒店
function ReadHotelCookie()
{
	if (typeof(ArriveCityName) != "undefined")
	  idObj("raCityName").value= ArriveCityName.length > 0 ? ArriveCityName : "";
	  	
	if(WM_readCookie("elong_hotel_cookie")!="")//如果cookie不为空,则开始读.
	{
		var hotelCookieStr=unescape(WM_readCookie("elong_hotel_cookie"));//读取cookie字符串;
		var hotelCookieAry=hotelCookieStr.split(",");//拆分cookie字符串
		for(var i=0; i<hotelCookieAry.length; i++)//分为数组进行处理
		{
			var index=hotelCookieAry[i].indexOf("|");//确定拆分位置
			var objStr=hotelCookieAry[i].slice(0,index);//取元素名
			var objStrValue=hotelCookieAry[i].slice(index+1,hotelCookieAry[i].length);//取元素值
			switch ( objStr.toLowerCase() )
			{
				case "raCityName".toLowerCase():
					idObj("raCityName").value=objStrValue;
					break;
				case "CheckInDate".toLowerCase():
					idObj("CheckInDate").value=objStrValue;
					break;
				case "CheckOutDate".toLowerCase():
					idObj("CheckOutDate").value=objStrValue;
					break;
				case "price":
					option_select( idObj("Price"), objStrValue);
					break;
				case "star":
					if (idObj("Star") != null)
						option_select( idObj("star"), objStrValue);
					break;
				case "RoomNum".toLowerCase():
					if ( idObj("RoomNum") != null ) {
						option_select( idObj("RoomNum"), objStrValue);
						if(idObj("PersonNum") != null && idObj("RoomNum").value != '')
						{
							RoomNumChange(idObj("PersonNum"), idObj("RoomNum"));
						}
					}
					break;
				case "PersonNum".toLowerCase():
					if ( idObj("PersonNum") != null ) {
						option_select( idObj("PersonNum"), objStrValue);
					}
			}
		}
	}
	else
	{ 
//	  idObj("CheckInDate").value="yyyy-mm-dd";
//	  idObj("CheckOutDate").value="yyyy-mm-dd";
//	  if (idObj("Price")!=null) idObj("Price").selectedIndex = 0;
	  if (idObj("Star")!=null) idObj("Star").selectedIndex = 0;
	  if (idObj("RoomNum")!=null) idObj("RoomNum").selectedIndex = 0;
	  if (idObj("RoomNum")!=null && idObj("PersonNum")!=null) RoomNumChange(idObj("PersonNum"), idObj("RoomNum"));
	}
}

//写机票;
function WriteAirCookie()
{ 
  /*
  var airStr="";
  var airForm=document.forms[0];
  if(airForm.elements["DEPART_CITY"].selectedIndex!=0)
   airStr=airStr+"DEPART_CITY|"+airForm.elements["DEPART_CITY"].selectedIndex+",";
  if(airForm.elements["DEPART_CITY2"].selectedIndex!=0)
   airStr=airStr+"DEPART_CITY2|"+airForm.elements["DEPART_CITY2"].selectedIndex+",";
  if(airForm.elements["DEST_CITY"].selectedIndex!=0)
   airStr=airStr+"DEST_CITY|"+airForm.elements["DEST_CITY"].selectedIndex+",";
  if(airForm.elements["FCY"].selectedIndex!=0)
   airStr=airStr+"FCY|"+airForm.elements["FCY"].selectedIndex+",";
  if(airForm.elements["DELEVER_CITY"].selectedIndex!=0)
   airStr=airStr+"DELEVER_CITY|"+airForm.elements["DELEVER_CITY"].selectedIndex+","; 
  if(airForm.elements["FLY_DATE"].value!=""||airForm.elements["FLY_DATE"].value!="yyyy-mm-dd")
    airStr=airStr+"FLY_DATE|"+airForm.elements["FLY_DATE"].value+",";
  if(airForm.elements["FLY_TIME"].selectedIndex!=0)
    airStr=airStr+"FLY_TIME|"+airForm.elements["FLY_TIME"].selectedIndex+",";
  if(airForm.elements["RETURN_DATE"].value!=""||airForm.elements["RETURN_DATE"].value!="yyyy-mm-dd")
    airStr=airStr+"RETURN_DATE|"+airForm.elements["RETURN_DATE"].value+",";
  if(airForm.elements["RETURN_TIME"].selectedIndex!=0)
    airStr=airStr+"RETURN_TIME|"+airForm.elements["RETURN_TIME"].selectedIndex+",";
  if(airForm.elements["FLY_TIME2"].selectedIndex!=0)
    airStr=airStr+"FLY_TIME2|"+airForm.elements["FLY_TIME2"].selectedIndex+",";
  if(airForm.elements["DEST_CITY2"].selectedIndex!=0)
    airStr=airStr+"DEST_CITY2|"+airForm.elements["DEST_CITY2"].selectedIndex+",";
  if(airForm.elements["FLY_DATE2"].value!=""||airForm.elements["FLY_DATE2"].value!="yyyy-mm-dd")
    airStr=airStr+"FLY_DATE2|"+airForm.elements["FLY_DATE2"].value;
  if(airStr!="")
   document.cookie = "elong_air_cookie="+escape(airStr)+";expires=" + Then.toGMTString() + ";path="+ "/" + ";domain="+"elong.com";	
 // alert(document.cookie);
  */
  
  /***********新版首页机票写Cookie******************/
  
  var airStr = "";
  var airForm = document.forms[0];
  if (airForm.elements["DEPART_CITY_input"] != null)
    airStr += "DEPART_CITY_input|"+airForm.elements["DEPART_CITY_input"].value+",";
  if (airForm.elements["DEPART_CITY2_input"] != null)
    airStr += "DEPART_CITY2_input|"+airForm.elements["DEPART_CITY2_input"].value+",";
  if (airForm.elements["DEST_CITY_input"] != null)
    airStr += "DEST_CITY_input|"+airForm.elements["DEST_CITY_input"].value+",";
  if (airForm.elements["DEST_CITY2_input"] != null)
    airStr += "DEST_CITY2_input|"+airForm.elements["DEST_CITY2_input"].value+",";
  if (airForm.elements["FLY_DATE"].value!=""||airForm.elements["FLY_DATE"].value!="yyyy-mm-dd")
    airStr=airStr+"FLY_DATE|"+airForm.elements["FLY_DATE"].value+",";
  if (airForm.elements["FLY_TIME"].selectedIndex!=0)
    airStr=airStr+"FLY_TIME|"+airForm.elements["FLY_TIME"].selectedIndex+",";
  if (airForm.elements["RETURN_DATE"].value!=""||airForm.elements["RETURN_DATE"].value!="yyyy-mm-dd")
    airStr=airStr+"RETURN_DATE|"+airForm.elements["RETURN_DATE"].value+",";
  if (airForm.elements["RETURN_TIME"].selectedIndex!=0)
    airStr=airStr+"RETURN_TIME|"+airForm.elements["RETURN_TIME"].selectedIndex+",";
  if (airForm.elements["FLY_DATE2"].value!=""||airForm.elements["FLY_DATE2"].value!="yyyy-mm-dd")
    airStr=airStr+"FLY_DATE2|"+airForm.elements["FLY_DATE2"].value+",";
  if (airForm.elements["FCY"].selectedIndex!=0)
   airStr=airStr+"FCY|"+airForm.elements["FCY"].value+",";
  if (airForm.elements["DELEVER_CITY_input"] != null)
    airStr += "DELEVER_CITY_input|"+airForm.elements["DELEVER_CITY_input"].value;
    
  addCookie("elong_air_cookie",airStr,1);
//  if (airStr!="")
//   document.cookie = "elong_air_cookie="+escape(airStr)+";expires=" + Then.toGMTString() + ";path="+ "/" + ";domain="+"elong.com";	
// alert(document.cookie);
  /*************************************************/
 	
}

//读机票
function ReadAirCookie()
{ 
	var airForm=document.forms[0];//读取form
	
	 if (typeof(ArriveCity3Code) != "undefined" && LeaveCity3Code!= "undefined" )
	 {
		if (ArriveCity3Code.length > 0)
		{
			option_select(idObj("DEST_CITY"), ArriveCity3Code);
			option_select(idObj("DEPART_CITY2"), ArriveCity3Code);
		}
		
		if (LeaveCity3Code.length > 0 && LeaveCity3Code != idObj("DEST_CITY").value)
		{
			option_select(idObj("DEPART_CITY"), LeaveCity3Code);
		}
		else if (idObj("DEST_CITY").value == 'BJS' )
		{
			option_select(idObj("DEPART_CITY"), 'SHA');
		}
		
		if (idObj("DEST_CITY2").length>0) idObj("DEST_CITY2").selectedIndex = 0;
		/*if (idObj("DEPART_CITY").value != 'BJS' && idObj("DEST_CITY").value != 'BJS')
		{
			option_select(idObj("DEST_CITY2"), 'BJS');
		}
		else if (idObj("DEPART_CITY").value != 'SHA' && idObj("DEST_CITY").value != 'SHA')
		{
			option_select(idObj("DEST_CITY2"), 'SHA');
		}*/
		
		select_sendticket(idObj("DEPART_CITY").selectedIndex);
	}
	
		
	if(WM_readCookie("com.eLong.BusinessRules.Flights.FlightCookie")!="")//如果cookie不为空,则开始读.
	{	
		/*var airCookieStr=unescape(WM_readCookie("elong_air_cookie"));//读取cookie字符串;
		
		var airCookieAry=airCookieStr.split(",");//拆分cookie字符串
	
		for(i=0;i<airCookieAry.length;i++)//分为数组进行处理
		{
			var index=airCookieAry[i].indexOf("|");//确定拆分位置
			var objStr=airCookieAry[i].slice(0,index);//取元素名
			var objStrValue=airCookieAry[i].slice(index+1,airCookieAry[i].length);//取元素值
		
			if ( idObj(objStr)!= null )
			{
				if ( objStr=="DEPART_CITY" || objStr=="DEST_CITY" || objStr=="DEPART_CITY2" || objStr=="DEST_CITY2") continue;
				if(airForm.elements[objStr].type=="text")
					airForm.elements[objStr].value=objStrValue;
				else		
					airForm.elements[objStr].selectedIndex=objStrValue;
					
			}
		}
		*/
		/*
		idObj("FLY_DATE").value=getFlightsCookie("DepartDate");
		idObj("FLY_DATE2").value=getFlightsCookie("DepartDate2");
		idObj("RETURN_DATE").value=getFlightsCookie("ReturnDate");
		option_select(idObj("DEPART_CITY"),getFlightsCookie("DepartCity"));
		option_select(idObj("DEPART_CITY2"),getFlightsCookie("DepartCity2"));
		option_select(idObj("DEST_CITY"),getFlightsCookie("DestCity"));
		option_select(idObj("DEST_CITY2"),getFlightsCookie("DestCity2"));
		option_select(idObj("FCY"), getFlightsCookie("Fcy"));
		option_select(idObj("DELEVER_CITY"), getFlightsCookie("SendTicket"));
		*/
	}
	else
	{	
		/** 原默认日期、城市下拉列表
		idObj("FLY_DATE").value="yyyy-mm-dd";
		idObj("FLY_DATE2").value="yyyy-mm-dd";
		idObj("RETURN_DATE").value="yyyy-mm-dd";
		option_select(idObj("DEPART_CITY"), 'BJS');
		option_select(idObj("DEST_CITY"), 'SHA');
		option_select(idObj("DEST_CITY2"), 'CAN');
		*/
		/** 热门目的地城市下拉列表 **/
	 
	}
	
	/****************新版首页机票读Cookie***********************/
	if(WM_readCookie("elong_air_cookie")!="")
	{
		var airCookieStr=unescape(WM_readCookie("elong_air_cookie"));//读取cookie字符串;
		var airCookieAry=airCookieStr.split(",");//拆分cookie字符串
		for(var i=0; i<airCookieAry.length; i++)//分为数组进行处理
		{
			var index=airCookieAry[i].indexOf("|");//确定拆分位置
			var objStr=airCookieAry[i].slice(0,index);//取元素名
			var objStrValue=airCookieAry[i].slice(index+1,airCookieAry[i].length);//取元素值
			switch (objStr.toLowerCase())
			{
			  case "depart_city_input":
			    airForm.elements["DEPART_CITY_input"].value = objStrValue;
			    break;
			  case "depart_city2_input":
			    airForm.elements["DEPART_CITY2_input"].value = objStrValue;
			    break;
			  case "dest_city_input":
			    airForm.elements["DEST_CITY_input"].value = objStrValue;
			    break;
			  case "dest_city2_input":
			    airForm.elements["DEST_CITY2_input"].value = objStrValue;
			    break;
			  case "fly_date":
			    airForm.elements["FLY_DATE"].value = CheckDateForFlight(objStrValue);
			    break;
			  case "return_date":
			    airForm.elements["RETURN_DATE"].value = objStrValue;
			    break;
			  case "fly_date2":
			    airForm.elements["FLY_DATE2"].value = objStrValue;
			    break;
			  case "fcy":
			    option_select(idObj("FCY"), objStrValue);
			    break;
			  case "delever_city_input":
			    airForm.elements["DELEVER_CITY_input"].value = objStrValue;
			    break;
			}
	  }
	}
	else
	{
	  airForm.elements["DEPART_CITY_input"].value = "Beijing 北京";
	  airForm.elements["DEST_CITY_input"].value = "Shanghai 上海";
	  airForm.elements["DEPART_CITY2_input"].value = "Shanghai 上海";
	  airForm.elements["DEST_CITY2_input"].value = "Beijing 北京";
	  airForm.elements["DELEVER_CITY_input"].value = "Beijing 北京";
	}
	/***************************************/
	
}

    function CheckDateForFlight( date )
    {
        var tempDate;
        var nowDate = new Date();
        
        if( date == "yyyy-mm-dd" )
        {
            tempDate = date;
        }
        else
        {
            try
            {
                var arr = date.split("-");
                if( arr[0] < nowDate.getFullYear() || arr[1] < nowDate.getMonth() + 1 || arr[2] < nowDate.getDate() )
                    tempDate = nowDate.getFullYear() + "-" + eval(nowDate.getMonth() + 1 ) + "-" + nowDate.getDate();
                else
                    tempDate = date;    
            }
            catch(ex)
            {
                tempDate = date;
            }    
        }
        return tempDate;
    }

function WM_readCookie(name)
{
	if(document.cookie == "")
		return "";
	else
		return WM_getCookieValue(name);
}
function WM_getCookieValue(name)
{
	var firstChar,lastChar;
	var theBigCookie = document.cookie;
	firstChar = theBigCookie.indexOf(name);
	if(firstChar != -1)
	{
		firstChar +=name.length + 1;
		lastChar = theBigCookie.indexOf(';', firstChar);
		if(lastChar == -1) lastChar = theBigCookie.length;
			return theBigCookie.substring(firstChar, lastChar);
	}
	else
	{
		return "";
	}
}
function getCookie(name)
{
    var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
    if(arr !=null) return unescape(arr[2]); return null;
}
function DecodeCookie(str)
{
	var strArr;
	var strRtn="";
	strArr=str.split("a");
	for (var i=strArr.length-1;i>=0;i--) 
		strRtn+=String.fromCharCode(eval(strArr[i]));
	return strRtn;
}
