var to = null;
var next = null;
function init(id) {
	var flag = (id == "sortTable") ? 1 : 0;
	if (flag)
		to = new TableOrder(id);
	else 
		next = new TableOrder(id);
}
function ClearCss() {
	forEach(arrOrder, function (o) {
		o.className = "";
	});
}
function SetOrder(options, obj) {
	var order = obj.Creat(options);
	order.endSort = function () {
		$Display(options.wait, "none");
	};
	return order;
}

function beforSort(obj, id) {
	$Display(id, "");
	obj.blur();
}

function changeSort(id, index, waitId) {
	changeImage(id);
	var flag = (waitId == "waiting") ? 1 : 0;
	sort(id, index, waitId, flag ? to : next);
}

function changeSort1(id, index, waitId) {
	changeClass(id);
	var flag = (waitId == "waiting") ? 1 : 0;
	sort(id, index, waitId, flag ? to : next);
}
// obj=to or next

var flagPrice = true,flagStartTime = true, flagEndTime = true;//是否按指定字段降序排序

function sort(id, index, waitId, obj) {
	if (index == 1) {
		obj.price = flagPrice;
		obj.Sort(SetOrder({Index:0, DataType:"int", Attri:"price", Desc:obj.price, wait:waitId}, obj), obj.odID);
		flagPrice = !flagPrice;
	} else {
		if (index == 2) {
			obj.startTime = flagStartTime;
			obj.Sort(SetOrder({Index:2, Attri:"starttime", Desc:obj.startTime, wait:waitId}, obj), obj.odID);
			flagStartTime = !flagStartTime;
		} else {
			if (index == 3) {
				obj.endTime = flagEndTime;
				obj.Sort(SetOrder({Index:2, Attri:"endtime", Desc:obj.endTime, wait:waitId}, obj), obj.odID);
				flagEndTime = !flagEndTime;
			} else {
				obj.planeStyle = !obj.planeStyle;
				obj.Sort(SetOrder({Index:3, DataType:"int", Attri:"planeStyle", Desc:obj.planeStyle, wait:waitId}, obj), obj.odID);
			}
		}
	}
}

function changeImage(id) {
	if ($I(id).src.indexOf("2") != -1) {
	    jQuery("#"+id).attr("src","images/fy_zk_pxdot.gif");
	} else {
		jQuery("#"+id).attr("src","images/fy_zk_pxdot2.gif");
	}
}
function changeClass(id) {
	for(i=1;i<4;i++){
		var _ind = id.substring(4);
		if(_ind.length == 1){
			if(id!=("sort"+i)){
				jQuery("#sort"+i).attr("class","noCheck");
			}
		}else{
			if(id!=("sort"+i+i)){
				jQuery("#sort"+i+i).attr("class","noCheck");
			}
		}
	}
	if ($I(id).className == 'noCheck') {
		flagPrice = false;
		flagStartTime = false;
		flagEndTime = false;
		jQuery("#"+id).attr("class","checkedUp");//升序
	} else if($I(id).className == 'checkedUp'){
		flagPrice = true;
		flagStartTime = true;
		flagEndTime = true;
		jQuery("#"+id).attr("class","checked");//降序
	}else if($I(id).className == 'checked'){
		flagPrice = false;
		flagStartTime = false;
		flagEndTime = false;
		jQuery("#"+id).attr("class","checkedUp");//升序
	}
}
function displayTr(tableid, obj, index1, index2, flightType, attName1, attName2) {
	var val1 = obj.value;
	var ta = $I(tableid);
	var count = 0;
	var num = jQuery("#resultCount2").text();//往返程中有相同的resultCount，第二程为resultCount2
	var tripType = jQuery("#typeOfFlight").val();
	if(tripType&&tripType==1&&num&&num>=0)
		count = parseInt(num);
	 else count= parseInt($I("resultCount").innerHTML);
	for (var m = 0; m < ta.rows.length; m++) {
		if(typeof ta.rows[m].cells[index1] == "undefined") continue;
		var val2 = ta.rows[m].cells[index1].getAttribute(attName1);
		if(val2==null) continue;
		
		if (obj.checked == false) {
			if (val2.indexOf(val1) != -1 && !$EqualDisplay(ta.rows[m], "none")) {
				$SetDisplay(ta.rows[m], "none");
				if (ta.rows[m].id) {
					count--;
				}
			}
		} else {
			if (val2.indexOf(val1) != -1) {
				count = filterFlightType(flightType, ta, m, index2, attName2, count);
			}
		}
	}
	if(tripType&&tripType==1&&num&&num>=0)jQuery("#resultCount2").text(count < 0 ? 0 : count);
	else $I("resultCount").innerHTML = count < 0 ? 0 : count;
}
function filterFlightType(flightType, ta, m, index2, attName2, count) {
	var fltype = $N(flightType);
	var val3 = ta.rows[m].cells[index2].getAttribute(attName2);
	for (var f = 0; f < fltype.length; f++) {
		var val4 = fltype[f].value;
		if (fltype[f].checked == true) {
			if (val3.indexOf(val4) != -1 && ta.rows[m].id && $EqualDisplay(ta.rows[m], "none")) {
				$SetDisplay(ta.rows[m], "");
				count++;
				break;
			}
		} else {
			if (val3.indexOf(val4) != -1 && !$EqualDisplay(ta.rows[m], "none")) {
				$SetDisplay(ta.rows[m], "none");
				if (ta.rows[m].id) {
					count--;
				}
				break;
			}
		}
	}
	return count;
}
function $I(id) {
	return document.getElementById(id);
}
function $N(na) {
	return document.getElementsByName(na);
}
function $EqualDisplay(obj, value) {
	return obj.style.display == value;
}
function $SetDisplay(obj, value) {
	obj.style.display = value;
}


