	$(function(){
		//机票搜索类型切换事件
		$("input[name='tripType']").click(function(){
			switchTripType(this.value);
		})[0].click();
		//城市选择事件
		$('#startCity').searchInfo({type:'gnjp',width:'220',initData:hotAirportsGn,callback:function(item){$('#startCityCode').val(item[2]);}});
		$('#transferCity').searchInfo({type:'gnjp',width:'220',initData:hotAirportsGn,callback:function(item){$('#transferCityCode').val(item[2]);}});
		$('#targetCity').searchInfo({type:'gnjp',width:'220',initData:hotAirportsGn,callback:function(item){$('#targetCityCode').val(item[2]);}});
		$('#moreCity').searchInfo({type:'gnjp',width:'220',initData:hotAirportsGn,callback:function(item){$('#moreCityCode').val(item[2]);}});
		//日期选择控件
		$('#startDate').doubleDatePicker();   
	   	$('#transferDate').doubleDatePicker({firstCal:'startDate'});
	   	$('#backDate').doubleDatePicker({firstCal:'startDate'});
	   	//搜索机票按钮事件
	   	$('#searchButton').click(searchEntry);
	   	//国际票事件
		$("input[name='tripTypeGJ']").click(function(e){
			if(this.value=="1")
			{
				$("#backDateNodeGJ").hide();
			}
			else
			{
				$("#backDateNodeGJ").show();
			}
		})[1].click();
		//城市选择事件
		$('#startCityGJ').searchInfo({type:'jp',width:'280',initData:hotAirportsGj,callback:function(item){$('#startCityCodeGJ').val(item[2]);}});
	   	$('#targetCityGJ').searchInfo({type:'jp',width:'280',initData:hotAirportsGj,callback:function(item){$('#targetCityCodeGJ').val(item[2]);}});
		//日期选择控件
		$('#startDateGJ').doubleDatePicker();   
	   	$('#backDateGJ').doubleDatePicker({firstCal:'startDate'});
	   	// 查询国际票
	   	$("#submitGJ").click(function(){
	   		searchGj();
	   	});
	})
	/**
	 * 保存用户选择信息对象
	 *       行程类型       行程步骤        起始城市           中转城市              目的城市            出发日期          中转日期             返回日期        缓存数据   产品编码
	 * 格式：{tt:tripType,ts:step,ad:'',stc:'startCity',trc:'transferCity',tac:'targetCity',sd:'startDate',td:'transferDate',bd:'backDate',cache:[],code:[]}
	 */
	var TripInfo=null;
	function initTripInfo()
	{
		TripInfo={};
		TripInfo.tt=parseInt($(":radio[name='tripType']:checked").val());
		TripInfo.ts=0;
		TripInfo.ad='';
		TripInfo.stc=$('#startCityCode').val();
		TripInfo.trc=$('#transferCityCode').val();
		TripInfo.tac=$('#targetCityCode').val();
		TripInfo.sd=$("#startDate").val();
		TripInfo.td=$("#transferDate").val();
		TripInfo.bd=$("#backDate").val();
		TripInfo.cache=[];
		TripInfo.code=[];
	}
	/**
	 * 切换机票搜索类型
	 * param type ：1单程 2往返 3连程
	*/
	function switchTripType(type)
	{
		switch(type)
		{
			case '1':
				$('#startCityNode').show().next().hide().next().show().next().show().next().hide().next().hide();
				break;
			case '2':
				$('#startCityNode').show().next().hide().next().show().next().show().next().hide().next().show();
				break;
			case '3':
				$('#startCityNode').show().next().show().next().show().next().show().next().show().next().hide();
				break;
		}
	}
	/**
	 * 机票查询入口 通过此方法来调用searchFlight
	 */
	 function searchEntry(e)
	 {
	 	e.target.blur();
	 	//如果来自点击搜索 重置TripInfo
	 	if(e.target.id=='searchButton')
	 	{
	 		initTripInfo();
	 		if(!checkSearch())
	 			return;
	 		document.jpgn.startCode.value=TripInfo.stc;
	 		document.jpgn.transferCode.value=TripInfo.trc;
	 		document.jpgn.targetCode.value=TripInfo.tac;
	 		$('#gnSearchInfo').html('');
	 		$('#jpgn').submit();
	 	}
	 }
	 /**
	  * 搜索表单校验 
	 */
	 function checkSearch()
	 {
	 	if(!TripInfo)
	 		return false;
	 	//startCity
	 	if($.trim($('#startCity').val())=='' || $('#startCity').val().indexOf('请')!=-1)
		{
			$('#gnSearchInfo').html("请选择出发城市！");
			return false;
		}
		//transferCity
		if(($.trim($('#transferCity').val())=='' || $('#transferCity').val().indexOf('请')!=-1) && TripInfo.tt==3)
		{
			$('#gnSearchInfo').html("请选择中转城市！");
			return false;
		}
		//targetCity
		if($.trim($('#targetCity').val())=='' || $('#targetCity').val().indexOf('请')!=-1)
		{
			$('#gnSearchInfo').html("请选择目的城市！");
			return false;
		}
		//startDate
		if(!/^\d{4}(-\d{1,2}){2}$/.test($.trim($('#startDate').val())))
		{
			$('#gnSearchInfo').html("请选择出发日期！");
			return false;
		}
		//transferDate
		if(!/^\d{4}(-\d{1,2}){2}$/.test($.trim($('#transferDate').val())) && TripInfo.tt==3)
		{
			$('#gnSearchInfo').html("请选择中转日期！");
			return false;
		}
		if($('#startDate').val()>$('#transferDate').val() && TripInfo.tt==3)
		{
			$('#gnSearchInfo').html("中转日期不能早于出发日期！");
			return false;
		}
		//backDate
		if(!/^\d{4}(-\d{1,2}){2}$/.test($.trim($('#backDate').val())) && TripInfo.tt==2)
		{
			$('#gnSearchInfo').html("请选择返回日期！");
			return false;
		}
		if($('#startDate').val()>$('#backDate').val() && TripInfo.tt==2)
		{
			$('#gnSearchInfo').html("返回日期不能早于出发日期！");
			return false;
		}
	 	return true;
	 }	  
	 /**
	*国际机票搜索
	*/
	function searchGj()
	{
		if($.trim($('#startCityGJ').val()=='') || $('#startCityGJ').val().indexOf('请')!=-1)
  		{
  			$('#gjSearchInfo').html("请选择出发城市！");
  			return;
  		}
  		if($.trim($('#targetCityGJ').val()=='') || $('#targetCityGJ').val().indexOf('请')!=-1)
  		{
  			$('#gjSearchInfo').html("请选择目地城市！");
  			return;
  		}
  		if(!/^\d{4}(-\d{1,2}){2}$/.test($("#startDateGJ").val()) || $("#startDateGJ").val().indexOf('请')!=-1)
  		{
  			$('#gjSearchInfo').html("请选择出发日期！");
  			return;
  		}
  		else if($("#roundGJ")[0].checked && (!/^\d{4}(-\d{1,2}){2}$/.test($("#backDateGJ").val()) || $("#backDateGJ").val().indexOf('请')!=-1))
  		{
  			$('#gjSearchInfo').html("请选择返回日期！");
  			return;
  		}
  		if($("#roundGJ")[0].checked && $("#backDateGJ").val()<$("#startDateGJ").val())
  		{
  			$('#gjSearchInfo').html("返回日期不能早于出发日期！");
  			return;
  		}
  		$('#gjSearchInfo').html('');
  		$("#flightGJ").submit();
	}