var destinationCityHotList_list = [
 { Departure: "SHA", Arrival: "HKG" },
 { Departure: "SHA", Arrival: "TYO" },
 { Departure: "SHA", Arrival: "BKK" },
 { Departure: "SHA", Arrival: "SZX" },
 { Departure: "BJS", Arrival: "HKG" },
 { Departure: "BJS", Arrival: "TYO" },
 { Departure: "BJS", Arrival: "TAO" },
 { Departure: "BJS", Arrival: "SZX" },
 { Departure: "CAN", Arrival: "HKG" },
 { Departure: "CAN", Arrival: "TYO" },
 { Departure: "CAN", Arrival: "TAO" },
 { Departure: "CAN", Arrival: "SZX" }
];
/*
$.Dictionary = function(spec, src, popularSrc) {
	this.source = src;
	this.popularSource = popularSrc;
	this.specification = spec
	this.content = new Array();
	
	this._addToDictionary = function(character, value) {
		if (!this.content[character]) {
			this.content[character] = new Array();
		}
		this.content[character].push(value);
	};
	
	this._addWord = function(charUsed, word, value) {
		var i;
		var wordLength = word.length;
		for (i = 0; i < wordLength; i++) {
			var charSelected = word.charAt(i)
			if (charUsed.indexOf(charSelected) < 0) {
				charUsed += charSelected;
				this._addToDictionary(charSelected, value);
			}
		}
		return charUsed;
	};
	
	this._buildDictionary=function() {
		var sourceLength = this.source.length;
		var source;  	
		var dictionaryProperties;
		var addedChar;
	 	for (var i = 0; i < sourceLength; i++) {
	 		source = this.source[i];
	 		dictionaryProperties = this.specification.dictionaryProperties;
	 		addedChar = "";
	 		for (var j = 0; j < dictionaryProperties.length; j++) {
	 			addedChar = this._addWord(addedChar, source[dictionaryProperties[j]].toLowerCase(), source);
	 		}
	 	}
	};	
	
	this._scoreDescComparer = function(scoreObj1, scoreObj2) {
		if (scoreObj1.score < scoreObj2.score) {
			return 1;
		} else if (scoreObj1.score > scoreObj2.score) {
			return -1;
		} else {
			// Order by display Value if exist, otherwise keep orignal index order
			if (scoreObj1.displayValue && scoreObj2.displayValue) {
				if (scoreObj1.displayValue < scoreObj2.displayValue) {
					return -1;
				} else if (scoreObj1.displayValue > scoreObj2.displayValue) {
					return 1;
				} else {
					return 0;
				}
			} else {
				if (scoreObj1.index < scoreObj2.index) {
					return -1;
				} else if (scoreObj1.index > scoreObj2.index) {
					return 1;
				} else {
					return 0;
				}
			}
		}
	};	
	
	this._filterAndSort = function(checklist, word) {
		var score;
		var displayValue;
		var scoreList = new Array();
		var returnList = new Array();
		for (var i = 0; i < checklist.length; i++) {
			score = this.specification.scoreFunction(checklist[i], word);
			if (this.specification.displayField) {
				displayValue = checklist[i][this.specification.displayField];
			}
			if (score > 0) {
				scoreList.push({index:i, score:score, displayValue:displayValue});
			}
		}
		scoreList.sort(this._scoreDescComparer);
		for (var j = 0; j < scoreList.length; j++) {
			returnList.push(checklist[scoreList[j].index]);
		}
		return returnList;
	};	
	
	this.getMatchList = function(checkword) {
		var checklist;
		var word = checkword.toLowerCase();
		if (word.length > 0) {
			checklist = this.content[word.charAt(0)];
			if (checklist == undefined) {
				checklist = new Array(); //Null object
			}
			checklist = this._filterAndSort(checklist, word);
		} else {
			checklist = this.popularSource;
		}
		return checklist;
	};	
	
	this._buildDictionary();
};

$.AutoCompleteSpecification = function(dp, sf, df) {
  	this.dictionaryProperties = dp;
  	this.scoreFunction = sf;
  	this.displayField = df;
};
*/
function checkDomestic(city1,city2){
	var departureIATACode=$("#departureCountryIATACode").val();
	var arrivalIATACode=$("#destinationCountryIATACode").val();
	if (departureIATACode=="CN" && arrivalIATACode=="CN" ) {
		return true;
	} else {
		return false;
	}
}


function changeDepartureTime(value) {
	document.getElementById("departureTimeOption").value = value;
}

function changeReturnTime(value) {
	document.getElementById("returnTimeOption").value = value;
}

function writeDataTo(val, desObj) {
	document.getElementById(desObj).value2 = val;
}

function checkFlightType(type) {
	if (type == 1) {
		document.getElementById("dateto").setAttribute("value2", "");
		document.getElementById("dateto").disabled = "";
		document.getElementById("dateto").value = "yyyy-mm-dd";
	} else if (type == 2) {
		document.getElementById("dateto").disabled = "disabled";
		document.getElementById("dateto").setAttribute("value2", "");
		document.getElementById("dateto").value = "yyyy-mm-dd";
	}
}

function getFlightType() {
	if (document.getElementById("way2").checked == true)
		checkFlightType(1);
	else if (document.getElementById("way1").checked == true)
		checkFlightType(2);
}

String.prototype.getElementsByName = function(name){
	var re = new RegExp("[\w\W]*<" + name + ">[^<]*</" + name + ">[\w\W]*","ig"),
	r,
	re2 = new RegExp("<" + name + ">"),
	re3 = new RegExp("</" + name + ">");
	if(r=this.match(re)){
  		return unescape((r[0].trin(re2)).trin(re3));
 	}
 	return null;
};

String.prototype.trin = function(re){ 
	return this.replace(re, "");
};

function doCheckCityPair(){
	checkCityPair=true; 
	var departureIATACode=$("#departureCityIATACode").val();
	var arrivalIATACode=$("#destinationCityIATACode").val();
	var fromcity=$("#cityfromDomestic").val();
    var tocity=$("#citytoDomestic").val();
	var url=root+"webservice/checkCityPair.php?departureIATACode="+departureIATACode+"&arrivalIATACode="+arrivalIATACode;	
	if (gjfValidLength(fromcity, 1) && gjfValidLength(tocity, 1)){//目的地和出发地都不为空
		$("#citytoDomestic_err1").text("");	
	    $("#citytoDomestic_err1").hide();
		if(!checkDomestic(tocity,fromcity)){//国外
			$.ajax({
				type: "GET",
				url: url,	 
				success: function(checkstate){ doCityPairStatusChange(checkstate)}
			}); 
		} else {
			$("#datefrom").focus();
	    }
	}
}

var checkCityPair=true;
function doCityPairStatusChange(checkstate){
	if(checkstate!=1) {					
		$("#cityfromDomestic").focus();
		checkCityPair=false;
	} else {
		checkCityPair=true;
	}
	$("#datefrom").focus();
}

function changeRouteType(type) {
	if (type == 1) {
		document.getElementById("returninfo").style.display="";
		var dateObj=document.getElementById("backdate");
		var monthObj=document.getElementById("backmonth");
		document.getElementById("dateto").setAttribute("value2", "");
		monthObj.disabled = "";
		dateObj.disabled = "";
		var day=(dateObj.value<10)?"0"+dateObj.value:dateObj.value;
		document.getElementById("dateto").value = monthObj.value+"-"+day;		
		document.getElementById('anchorto').onclick=function(){initDate('to');displayCalendar1();};
	} else if (type == 2) {
		document.getElementById("returninfo").style.display="none";
		var dateObj=document.getElementById("backdate");
		var monthObj=document.getElementById("backmonth");
		monthObj.disabled = "disabled";
		dateObj.disabled = "disabled";
		document.getElementById("dateto").setAttribute("value2", "");
		document.getElementById("dateto").value = "yyyy-mm-dd";
		document.getElementById('anchorto').onclick=function(){return false;};
	}
}

/*
/////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////flight中,根据出发地查找目的地,根据目的地查找出发地/////////
/////////////////////////////////////////////////////////////////////////////////////

function getAirport(type,divtype) {
	var CityArray = Array();
	var myvalue = document.getElementById("departureCityIATACode").value;
	if(myvalue=="") {
		return ;
	}
	for(var i=0;i<destinationCityHotList_list.length;i++) {
		if(destinationCityHotList_list[i]['Departure']==myvalue)
		{
			CityArray.push(destinationCityHotList_list[i]['Arrival']);
		}
	}
	if(type==1) {
		var myarray =  getCitys(CityArray);
	} else {
		var myarray =  GetDescList(CityArray);
	}
	return myarray;
}

function getCitys(cityarray) {
	var LastArray = Array();
	for(var i=0;i<cityarray.length;i++) {
		for(var j =0;j<destinationCityHotList_bak.length;j++) {
			if(cityarray[i]==destinationCityHotList_bak[j]["IATACode"]) {
				LastArray.push(destinationCityHotList_bak[j]);
			}
		}
	}
	return LastArray;
}

function GetDescList(cityarray) {
	var LastArray = Array();
	for(var i=0;i<cityarray.length;i++) {
		for(var j =0;j<destinationCityList_bak.length;j++) {
			if(cityarray[i]==destinationCityList_bak[j]["IATACode"]) {
				LastArray.push(destinationCityList_bak[j]);
			}
		}
	}
	return LastArray;
}

function ChangeFrom(divtype) {
	document.getElementById("citytoDomestic").value="";
	destinationCityList=getAirport(2,divtype);
	destinationCityHotList=getAirport(1,divtype);
	//	if(destinationCityList.length<1&&destinationCityHotList.length<1){
	//		destinationCityHotList=[{ SCName: "not found", ENName: "", Pinyin: "", Country: "", IATACode: "", CountrySCName: "", CountryENName: "" }];
	//	}

	//	ResultArray();
}

function ResultArray(langtype) {
	ChangeFrom(2);
	var lang;
	var countrylang;
	var langalert;
	if(langtype==1) {
		lang ="SCName";
		countrylang = "CountrySCName";
		langalert = "对不起，没有找到与出发地相匹配的目的地城";
	} else {
		lang ="ENName";
		countrylang = "CountryENName";
		langalert = "sorry, we didn't find the destination city matching to the source ";
	}
	var toList		= document.getElementById( "citytoDomestic" );
	//toList.length	= 1;
	while( toList.childNodes.length ) {
		toList.removeChild( toList.childNodes[0] );
	}
	if(destinationCityHotList.length<1&&destinationCityList.length<1) {
		var myelement = document.getElementById("citytoDomestic_err");
		myelement.style.display='';
		myelement.innerHTML=langalert;
	}else{
		var myelement = document.getElementById("citytoDomestic_err");
		myelement.style.display='none';
		myelement.innerHTML="";
		var myselect = document.getElementById("citytoDomestic");
		var cityList;
		var cityHotList;
		cityHotList=destinationCityHotList;
		cityList=destinationCityList;
		var optgroup = document.createElement("optgroup");
		optgroup.label = 'Popular Cities';

		for(var i=0;i<cityHotList.length;i++){
			selText=cityHotList[i][lang];
			selValue=cityHotList[i][lang]+","+cityHotList[i]['Country']+","+cityHotList[i]['IATACode'];
			var opt = document.createElement("option");
			opt.innerHTML = selText;
			opt.value = selValue;
			optgroup.appendChild( opt );
		}
		myselect.appendChild(optgroup);

		optgroup = document.createElement("optgroup");
		optgroup.label = '- - - - - - - - - - - -';
		myselect.appendChild(optgroup);

		var country = "";
		for(var i=0;i<cityList.length;i++){
			if (cityList[i][countrylang] != country) {
				country = cityList[i][countrylang];
				optgroup = document.createElement("optgroup");
				optgroup.label = cityList[i][countrylang];
			}
			selText=cityList[i][lang];
			selValue=cityList[i][lang]+","+cityList[i]['Country']+","+cityList[i]['IATACode'];
			var opt = document.createElement("option");
			opt.innerHTML = selText;
			opt.value = selValue;
			optgroup.appendChild( opt );
			myselect.appendChild(optgroup);
		}
	}
}


//根据目的地选择出发地
function getAirport2(type,divtype)
{
	var CityArray = Array();
	var myvalue = document.getElementById("destinationCityIATACode").value;
	if(myvalue=="")
	{
		return ;
	}
	for(var i=0;i<destinationCityHotList_list.length;i++)
	{
		if(destinationCityHotList_list[i]['Arrival']==myvalue)
		{
			CityArray.push(destinationCityHotList_list[i]['Departure']);
		}
	}
	if(type==1)
	{
		var myarray =  getCitys(departureCityList_bak,CityArray);
	}
	else
	{
		var myarray =   getCitys(departureCityList_bak,CityArray);
	}
	return myarray;
}
//根据出发地选择目的地
function getAirport(type,divtype)
{
	var CityArray = Array();
	var myvalue = document.getElementById("departureCityIATACode").value;
	if(myvalue=="")
	{
		return ;
	}
	for(var i=0;i<destinationCityHotList_list.length;i++)
	{
		if(destinationCityHotList_list[i]['Departure']==myvalue)
		{
			CityArray.push(destinationCityHotList_list[i]['Arrival']);
		}
	}
	if(type==1)
	{
		var myarray = getCitys(destinationCityList_bak,CityArray);
	}
	else
	{
		var myarray = getCitys(destinationCityList_bak,CityArray);
	}
	return myarray;
}
function getCitys(departarray,cityarray)
{
	var LastArray = Array();
	for(var i=0;i<cityarray.length;i++)
	{
		for(var j =0;j<departarray.length;j++)
		{
			if(cityarray[i]==departarray[j]["IATACode"])
			{
				LastArray.push(departarray[j]);
			}
		}
	}
	return LastArray;
}
function ChangeFrom(divtype)
{

	if(document.getElementById("departureCityIATACode").value=="")
	{
		destinationCityList=destinationCityList_bak;
		destinationCityHotList=destinationCityHotList_bak;
	}else{
		document.getElementById("citytoDomestic").value="";
		destinationCityList=getAirport(2,divtype);
		destinationCityHotList=getAirport(1,divtype);
	}
}
function ChangeFrom2(divtype)
{
	var myvalue = document.getElementById("destinationCityIATACode").value;
	if(myvalue==""||myvalue=="undefined")
	{
		departureCityHotList=destinationCityHotList_bak;
		departureCityList=destinationCityList_bak;
	}else{
		document.getElementById("cityfromDomestic").value="";
		departureCityHotList=getAirport2(1,divtype);
		departureCityList=getAirport2(2,divtype);
	}
}
function ResultArray(langtype,selecttype)
{
	var lang;
	var countrylang;
	var langalert;
	var cityList;
	var cityHotList;
	var selectid;
	var errorid;
	if(langtype==1)
	{
		lang ="SCName";
		countrylang = "CountrySCName";
		langalert = "对不起，没有找到相匹配的城市";
		selectoption = "请选择目的地";
	}
	else
	{
		lang ="ENName";
		countrylang = "CountryENName";
		langalert = "sorry, we didn't find the destination city matching to the source ";
		selectoption = "Please choose a city";
	}
	if(selecttype==1)
	{
		ChangeFrom(2);
		cityHotList=destinationCityHotList;
		cityList=destinationCityList;
		selectid = "citytoDomestic";
		errorid = "citytoDomestic_err";
	}
	else
	{
		ChangeFrom2(2);
		cityHotList=departureCityHotList;
		cityList=departureCityList;
		selectid = "cityfromDomestic";
		errorid = "cityfromDomestic_err";
	}
	var toList		= document.getElementById(selectid);
	//toList.length	= 1;
	while( toList.childNodes.length )
	{
		toList.removeChild( toList.childNodes[0] );
	}
	if(cityHotList.length<1&&cityList.length<1)
	{
		var myelement = document.getElementById(errorid);
		myelement.style.display='';
		myelement.innerHTML=langalert;
	}else{
		var myelement = document.getElementById(errorid);
		myelement.style.display='none';
		myelement.innerHTML="";
		var myselect = document.getElementById(selectid);
		var opt=new Option(selectoption,'');
		myselect.options[0]=opt;

		var optgroup = document.createElement("optgroup");
		optgroup.label = 'Popular Cities';

		for(var i=0;i<cityHotList.length;i++){
			selText=cityHotList[i][lang];
			selValue=cityHotList[i][lang]+","+cityHotList[i]['Country']+","+cityHotList[i]['IATACode'];
			var opt = document.createElement("option");
			opt.innerHTML = selText;
			opt.value = selValue;
			optgroup.appendChild( opt );
		}
		myselect.appendChild(optgroup);

		optgroup = document.createElement("optgroup");
		optgroup.label = '- - - - - - - - - - - -';
		myselect.appendChild(optgroup);

		var country = "";
		for(var i=0;i<cityList.length;i++){
			if (cityList[i][countrylang] != country) {
				country = cityList[i][countrylang];
				optgroup = document.createElement("optgroup");
				optgroup.label = cityList[i][countrylang];
			}
			selText=cityList[i][lang];
			selValue=cityList[i][lang]+","+cityList[i]['Country']+","+cityList[i]['IATACode'];
			var opt = document.createElement("option");
			opt.innerHTML = selText;
			opt.value = selValue;
			optgroup.appendChild( opt );
			myselect.appendChild(optgroup);
		}
	}

}
*/

function submitAllDepartAirport() {
    var e = document.getElementsByName('departAirport[]');
    e[0].checked = true;
    for (var i = 1; i < e.length; i++) {
        e[i].checked = false;
    }
}
function submitDepartAirport() {
    var e = document.getElementsByName('departAirport[]');
    var oneItemSelected = false;
    for (var i = 1; i < e.length; i++) {
        if (e[i].checked) {
            oneItemSelected = true;
        }
    }
    e[0].checked = !oneItemSelected;
}
function submitAllArrAirport() {
    var e = document.getElementsByName('arrAirport[]');
    e[0].checked = true;
    for (var i = 1; i < e.length; i++) {
        e[i].checked = false;
    }
}
function submitArrAirport() {
    var e = document.getElementsByName('arrAirport[]');
    var oneItemSelected = false;
    for (var i = 1; i < e.length; i++) {
        if (e[i].checked) {
            oneItemSelected = true;
        }
    }
    e[0].checked = !oneItemSelected;
}
function submitAllTime() {
    var e = document.getElementsByName('TimeIndex[]');
    e[0].checked = true;
    for (var i = 1; i < e.length; i++) {
        e[i].checked = false;
    }
}
function submitTime() {
    var e = document.getElementsByName('TimeIndex[]');
    var oneItemSelected = false;
    for (var i = 1; i < e.length; i++) {
        if (e[i].checked) {
            oneItemSelected = true;
        }
    }
    e[0].checked = !oneItemSelected;
}
function submitAllAirline() {
    var e = document.getElementsByName('Airline[]');
    e[0].checked = true;
    for (var i = 1; i < e.length; i++) {
        e[i].checked = false;
    }
}
function submitAirline() {
    var e = document.getElementsByName('Airline[]');
    var oneItemSelected = false;
    for (var i = 1; i < e.length; i++) {
        if (e[i].checked) {
            oneItemSelected = true;
        }
    }
    e[0].checked = !oneItemSelected;
}