function $(element){
	if (typeof element == "string"){
		return document.getElementById(element);
	}else{
		return null;
	}
}

function initSelectMenu(){
	var ulObj = $("flightMenu");
	var hiddenArea = $("hiddenArea");
	var showArea = $("fliCC");
	var childNodes = ulObj.getElementsByTagName("li");
	var lastSelectMenu = childNodes[0];
	var lastSelectDiv = document.getElementById("flit1");
	var hkspan = $("hkflightMenu").getElementsByTagName("li");
	
	hkspan[0].onclick = function (){
		this.className = "current";
		hkspan[1].className = "";
		H_M();
	}
	
	hkspan[1].onclick = function (){
		this.className = "current";
		hkspan[0].className = "";
		M_H();
	}
	
	var interspan = $("interflightMenu").getElementsByTagName("li");
	interspan[0].onclick = function (){
		this.className = "current";
		interspan[1].className = "";
		I_M();
	}
	
	
	interspan[1].onclick = function (){
		this.className = "current";
		interspan[0].className = "";
		M_I();
	}
	
	$("fstype").value = 1;
	var now=new Date();	
	$("to1").value=now.getUTCFullYear() + '-' + (now.getUTCMonth()+1) + '-' + now.getUTCDate();
	$("tr1").value=$("to1").value;
	$("tm1").value=$("to1").value;
	$("D1").value=$("to1").value;
	$("DI1").value=$("to1").value;
	for (var i = 0;i<childNodes.length;i++){
		childNodes[i].setAttribute ("index",i);
		childNodes[i].onclick = function (){
			var index = parseInt(this.getAttribute("index"));
			if (this !=lastSelectMenu ){
				$("fstype").value = index +1;
				lastSelectMenu.className = "";
				hiddenArea.appendChild(lastSelectDiv);
				showArea.appendChild(document.getElementById("flit" + (index+1)));
				
				this.className = "current";
				lastSelectDiv = document.getElementById("flit" + (index+1));
				lastSelectMenu = this;
			}
		}		
	}
}
 
 
function checkFlightDomain(oForm){
	type = parseInt($("fstype").value);
	switch (type){
	case 1:
		co1 = $("co1").value;
		co2 = $("co2").value;
		t = $("to1").value;
		
		if (!checkCityError_Empt(co1,$("co1"),"Please input or select a departure city!","The depature city you input doesn't exist!")) return false;
		if (!checkCityError_Empt(co2,$("co2"),"Please input or select an arrival city!","The arrival city you input doesn't exist!")) return false;
		if (co1 == co2){
			alert("Please input valid departure and arrival cities!");
			$("co2").focus();
			return false;
		}
		var dtArr = t.split("-");
		var dt = new Date(dtArr[0],dtArr[1],dtArr[2]);
		if (dt == "NaN" || dt == "Invalid Date"){
			alert("Please input the Departing as 'yyyy-mm-dd'!");
			$("to1").focus;
			return false;
		}
		break;
	case 2:
		cr1 = $("cr1").value;
		cr2 = $("cr2").value;
		tr1 = $("tr1").value;
		tr2 = $("tr2").value;
		
		if (!checkCityError_Empt(cr1,$("cr1"),"Please input or select a departure city!","Flight1.The city you input doesn't exist, please input or select a valid one!")) return false;
		if (!checkCityError_Empt(cr2,$("cr2"),"Please input or select a departure city!","Flight1.The city you input doesn't exist, please input or select a valid one!")) return false;
		
		if (cr1 == cr2){
			alert("Please input valid departure and arrival cities!");
			$("cr2").focus();
			return false;
		}
		var dtArr1 = tr1.split("-");
		var dtArr2 = tr2.split("-");
		
		var dt1 = new Date(dtArr1[0],dtArr1[1],dtArr1[2]);
		
		if (dt1 == "NaN" || dt1 == "Invalid Date"){
			alert("Please input the Departing as 'yyyy-mm-dd'!");
			$("tr1").select();
			return false;
		}
		var dt2 = new Date(dtArr2[0],dtArr2[1],dtArr2[2]);
		if (dt2 == "NaN" || dt2 == "Invalid Date") {
			alert("Please input the return date as 'yyyy-mm-dd'!");
			$("tr2").select();
			return false;
		}
		if ((dt1-dt2)>0){
			alert("Please input valid departure and arrival dates!");
			$("tr1").select();
			return false;
		}
		break;
	case 3:
		cm1 = $("cm1").value;
		cm2 = $("cm2").value;
		
		cm3 = $("cm3").value;
		cm4 = $("cm4").value;
		
		cm5 = $("cm5").value;
		cm6 = $("cm6").value;
		
		tm1 = $("tm1").value;
		tm2 = $("tm2").value;
		tm3 = $("tm3").value;
		
		if (!checkCityError_Empt(cm1,$("cm1"),"Please input or select a departure city for Flight 1!","The depature city you input for 'Flight 1' doesn't exist!")) return false;
		if (!checkCityError_Empt(cm2,$("cm2"),"Please input or select a departure city for Flight 1!","The arrival city you input for 'Flight 1' doesn't exist!")) return false;
		if (!checkCityError_Empt(cm3,$("cm3"),"Please input or select a departure city for Flight 2!","The depature city you input for 'Flight 2' doesn't exist!")) return false;
		if (!checkCityError_Empt(cm4,$("cm4"),"Please input or select a departure city for Flight 2!","The arrival city you input for 'Flight 2' doesn't exist!")) return false;
		if (!checkCityError_Empt(cm5,$("cm5"),"Please input or select a departure city for Flight 3!","The depature city you input for 'Flight 3' doesn't exist!")) return false;
		if (!checkCityError_Empt(cm6,$("cm6"),"Please input or select a departure city for Flight 3!","The arrival city you input for 'Flight 3' doesn't exist!")) return false;

		if (cm1 == cm2){
			alert("Flight1.Please input valid departure and arrival cities!");
			$("cm2").focus();
			return false;
		}
		
		if (cm3 == cm4){
			$("cm4").focus();
			alert("Flight2.Please input valid departure and arrival cities!");
			return false;
		}
		
		if (cm5 == cm6){
			$("cm6").focus();
			alert("Flight3.Please input valid departure and arrival cities!");
			return false;
		}
		
		var dtArr1 = tm1.split("-");
		var dtArr2 = tm2.split("-");
		var dtArr3 = tm3.split("-");
		
		var dt1 = new Date(dtArr1[0],dtArr1[1],dtArr1[2]);
		if (dt1 == "NaN" || dt1 == "Invalid Date"){
			alert("Please input the Departing as 'yyyy-mm-dd'!");
			$("tm1").select();
			return false;
		}
		
		var dt2 = new Date(dtArr2[0],dtArr2[1],dtArr2[2]);
		if (dt2 == "NaN" || dt2 == "Invalid Date"){
			alert("Please input the Departing date as 'yyyy-mm-dd'!");
			$("tm2").select();
			return false;
		}
		
		var dt3 = new Date(dtArr3[0],dtArr3[1],dtArr3[2]);
		if (dt3 == "NaN" || dt3 == "Invalid Date"){
			alert("Please input the Departing date as 'yyyy-mm-dd'!");
			$("tm3").select();
			return false;
		}
		break;
	}
	return true;
}

function checkCityError_Empt(va,obj,msg1,msg2){
	if (va == ""){
		alert(msg1);
		obj.select();
		return false;
	}
	return true;
}

function checkhkflight(ofrom){
	var co1 = $("T1").value;
	var co2 = $("T2").value;
	
	if (!checkCityError_Empt(co1,$("T1"),"Please input or select a departure city!","The depature city you input doesn't exist!")) return false;
	if (!checkCityError_Empt(co2,$("T2"),"Please input or select an arrival city!","The arrival city you input doesn't exist!")) return false;
	if (co1 == co2){
		alert("Please input valid departure and arrival cities!");
		$("co2").focus();
		return false;
	}
	
	if ($("D1").value.length == 0){
		alert("Please input the Departing as 'yyyy-mm-dd'!");
		$("D1").focus();
		return false;
	}
	return true;
}

function checkinterflight(ofrom){
	var co1 = $("TI1").value;
	var co2 = $("TI2").value;
	
	if (!checkCityError_Empt(co1,$("TI1"),"Please input or select a departure city!","The depature city you input doesn't exist!")) return false;
	if (!checkCityError_Empt(co2,$("TI2"),"Please input or select an arrival city!","The arrival city you input doesn't exist!")) return false;
	if (co1 == co2){
		alert("Please input valid departure and arrival cities!");
		$("co2").focus();
		return false;
	}
	
	if ($("DI1").value.length == 0){
		alert("Please input the Departing as 'yyyy-mm-dd'!");
		$("DI1").focus();
		return false;
	}
	return true;
}	