var tripNameTemp = '';

/**
 * 查询往返程特价
 * @param tripNameQry
 * @return
 */
function queryLowCabinRounds(tripNameQry) {
	tripNameTemp = tripNameQry;
	var url = 'QueryLowCabinRounds.action';
	var params = {
			tripName: tripNameQry
	};
	jQuery.post(url, params, fillLowCabinRoundTable, 'json');
}

function queryAllLowCabinRounds() {
	var url = 'QueryLowCabinRounds.action';
	var params = {
			tripName: tripNameTemp,
			allLowCabinRounds: 'Y'
	};
	jQuery.post(url, params, fillLowCabinRoundTable, 'json');
}

/**
 * 显示往返程特价
 * @param data
 * @return
 */
function fillLowCabinRoundTable(data) {
	var $table = $("#tab tr");
	var len = $table.length;
	//先进行删除
	for (var i = 0; i < len-1; i++) {
		$("tr[id=\'A"+i+"\']").remove();
		$("tr[id=\'B"+i+"\']").remove();
	}
	//然后添加
	for (var i = 0; i < data.lowCabinRoundList.length; i++) {
		var showStr = "";
		showStr += "<tr id=A"+i+">";
		showStr += "<td style=\"padding-left:10px;\">";
		showStr += data.lowCabinRoundList[i].ori+"-"+data.lowCabinRoundList[i].dest;
		showStr += "</td>";
		showStr += "<td>"+data.lowCabinRoundList[i].carrierName+data.lowCabinRoundList[i].carrier+data.lowCabinRoundList[i].flt_no_go+"</td>";
		showStr += "<td class=\"go\">"+data.lowCabinRoundList[i].goDateString+"</td>";
		showStr += "<td>"+data.lowCabinRoundList[i].go_ori_time+"</td>";
		showStr += "<td>"+data.lowCabinRoundList[i].go_dest_time+"</td>";
		showStr += "<td class=\"underline\">"+data.lowCabinRoundList[i].go_plane_type+"</td>";
		showStr += "<td rowspan=\"2\" class=\"tdunderline\">";
		showStr += "<span class=\"redprice\">￥"+data.lowCabinRoundList[i].priceGeneralString+"(含机建费)</span>";
		showStr += "<br />";
		if (data.needLogin == 'true') {
			showStr += "<a href=\"#\" onClick=\"orderFlight('"+data.lowCabinRoundList[i].cabin_id+"')\"><img src=\"../common/images/shenqing.jpg\" width=\"60\" height=\"23\" /></a>";
		} else {
			showStr += "<a href=\"#\" onClick=\"loginBeforeOrderFlight('"+data.lowCabinRoundList[i].cabin_id+"')\"><img src=\"../common/images/shenqing.jpg\" width=\"60\" height=\"23\" /></a>";
		}
		showStr += "</td>";
		showStr += "</tr>";
		showStr += "<tr id=\"B"+i+"\">";
		showStr += "<td style=\"padding-left:10px;\" class=\"tdunderline\">";
		showStr += data.lowCabinRoundList[i].dest+"-"+data.lowCabinRoundList[i].ori;
		showStr += "</td>";
		showStr += "<td class=\"tdunderline\">";
		showStr += data.lowCabinRoundList[i].carrierName+data.lowCabinRoundList[i].carrier+data.lowCabinRoundList[i].flt_no_back;
		showStr += "</td>";
		showStr += "<td class=\"back\">"+data.lowCabinRoundList[i].backDateString+"</td>";
		showStr += "<td class=\"tdunderline\">"+data.lowCabinRoundList[i].back_ori_time+"</td>";
		showStr += "<td class=\"tdunderline\">"+data.lowCabinRoundList[i].back_dest_time+"</td>";
		showStr += "<td class=\"tdunderline\">"+data.lowCabinRoundList[i].back_plane_type+"</td>";
		showStr += "</tr>";

		$("#tab").append(showStr);
	}
}

            
/**
 * TAB操作
 * @param showContent
 * @param selfObj
 * @return
 */
function selectTag(showContent, selfObj) {
	// 操作标签
	var tag = document.getElementById("tags").getElementsByTagName("li");
	var taglength = tag.length;
	for (i = 0; i < taglength; i++) {
		tag[i].className = "";
	}
	selfObj.parentNode.className = "selectTag";
	// 操作内容
	for (i = 0; j = document.getElementById("tagContent" + i); i++) {
		j.style.display = "none";
	}
	document.getElementById(showContent).style.display = "block";

}

/**
 * 单程、往返程的选择
 * @param val
 * @return
 */
function changeFlightType(val) {
	if (val == '1') {
		$("#backDateTdNameField").css("display", "none");
		$("#backDateTdValueField").css("display", "none");
		$("#flightType").val("1");
	}
	if (val == '2') {
		$("#backDateTdNameField").css("display", "block");
		$("#backDateTdValueField").css("display", "block");
		$("#flightType").val("2");
	}
}

/**
 * 舱位选择
 * @param val
 * @return
 */
function changeCabinType(val) {
	if (val == "Y")
		document.getElementById("cabinCode").value = "Y";
	if (val == "C")
		document.getElementById("cabinCode").value = "C";
	if (val == "F")
		document.getElementById("cabinCode").value = "F";
}

/**
 * 往返程打包价预定航班
 * 
 * @param val
 * @return
 */
function orderFlight(flightIndex) {
	document.getElementById("flightIndex").value = flightIndex;
	document.getElementById('ticketForm').action = 'InitLowCabinRoundBookInfoConfirmPage.action';
	document.getElementById('ticketForm').submit();
}

/**
 * 往返程打包价预定航班（没有登录是，先提醒登录）
 * 
 * @param flightIndex
 * @param seatCount
 * @return
 */
function loginBeforeOrderFlight(flightIndex) {
	document.getElementById("flightIndex").value = flightIndex;
	document.getElementById('ticketForm').action = '../user/InitLonginPage.action';
	document.getElementById('ticketForm').submit();
}

/**
 * 页面加载完成后的处理
 */
$(document).ready(function(){
	getAirlines();
	
	function getAirlines() {
		var url = 'GetAllFndAirlines.action';
		var params = {};
		jQuery.post(url, params, fillAirlinesCb, 'json');
	}
	
	function fillAirlinesCb(data) {
		for (var i = 0; i < data.airlineList.length; i++) {
			$("#airlineCb").append("<option value=\"" + data.airlineList[i].airline_code + "\">" + data.airlineList[i].name_cn + "</option>");
		}
	}
});

function queryFlight() {
	if ($("#cityfromDomestic").val() == '') {
		$("#cityfromDomestic").focus();
		return;
	}
	if ($("#citytoDomestic").val() == '') {
		$("#citytoDomestic").focus();
		return;
	}
	if ($("#orgDate").val() == '') {
		$("#orgDate").focus();
		return;
	}
	if (document.all.flightType.value == '2' && ($("#backDate").val() == null || $("#backDate").val() == '')) {
		$("#backDate").focus();
		return;
	}
	$("carrier").val($("#airlineCb").val());
	
	var orgCode = document.getElementById('orgCode').value;
	var destCode = document.getElementById('destCode').value;
	var orgSubmit = false;
	var destSubmit = false;
	for (var i = 0; i < departureCityListTmp.length; i++) {
		if (departureCityListTmp[i]['IATACode'] == orgCode && departureCityListTmp[i]['Country'] == 'CN') {
			orgSubmit = true;
			break;
		}
	}
		for (var i = 0; i < destinationCityListTmp.length; i++) {
		if (destinationCityListTmp[i]['IATACode'] == destCode && destinationCityListTmp[i]['Country'] == 'CN') {
			destSubmit = true;
			break;
		}
	}
	if (orgSubmit == true && destSubmit == true) {
		document.getElementById('ticketForm').action = '../airticket/DomesticAirticketSearchInterim.action';
		document.getElementById('ticketForm').submit();
	} else {
		alert('您选择的是国际航班，请重新选择！');
	}
}
