/*
*/

var sity_sel_num = 0;
var sity_sel_max = 5;
var sity_sel_input_id = "";
var load_flag = 0;
var AirData = new Array();
var sity_cur_sel = 0;

function sity_sel_show(num)
{
		document.getElementById("sity_sel"+num).className = 'sity_light';
		sity_cur_sel=num;
}

function sity_sel_hide(num)
{
		document.getElementById("sity_sel"+num).className = '';
		sity_cur_sel=0;
}

function sity_sel_select(num)
{
	//alert(2);
	if(num==0) 
	{
		document.getElementById("sity_select").style.display='none';
		return;
	}
	document.getElementById(sity_sel_input_id).value = AirData[num];
	if(document.getElementById("from2").disabled) document.getElementById("from2").value = document.getElementById("to1").value;
	if(document.getElementById("to2").disabled) document.getElementById("to2").value = document.getElementById("from1").value;
	sity_sel_input_id="";
	sity_cur_sel=0;
	document.getElementById("sity_select").style.display='none';
	show_elem(3);
}

function close_sel()
{
	//if(sity_cur_sel!=0) sity_sel_select(sity_cur_sel);
	//else 
	document.getElementById("sity_select").style.display='none';
}

function sel_analize(id)
{
//	if(sity_sel_input_id!=id)
//	{
//		sity_sel_num = 0;
//		sity_sel_input_id = id;
//
//		for(var j=1; j<6; j++)
//		{
//			document.getElementById("sity_sel"+j).innerHTML = '';
//			document.getElementById("sity_sel"+j).className = '';			
//		}
//	}
}

function sel_get_texts() {
//	if(sity_sel_input_id=="") return;
//	if(document.getElementById(sity_sel_input_id).value=="") return;
//	
//	if(calendar_flag!=0)
//	{
//		hide_calendar();
//	}
//
//	hide_elem(3);
//	
//	$.getJSON('data/cities_with_airports.txt', sel_add_airports);
//
//	document.getElementById("sity_select").style.top = (getElementTop(sity_sel_input_id)+20) + "px";
//	document.getElementById("sity_select").style.left = (getElementLeft(sity_sel_input_id)+0) + "px";
//	document.getElementById("sity_select").style.display='';
	
	if(document.getElementById("from2").disabled) {
		document.getElementById("from2").value = document.getElementById("to1").value;
		$('input[name=FlightSearchForm.departureLocation.1.CODE]').val($('input[name=FlightSearchForm.arrivalLocation.0.CODE]').val());
		alert($('input[name=FlightSearchForm.arrivalLocation.0.CODE]').val());
	}
	if(document.getElementById("to2").disabled) {
		document.getElementById("to2").value = document.getElementById("from1").value;
		alert($('input[name=FlightSearchForm.departureLocation.0.CODE]').val());
		$('input[name=FlightSearchForm.arrivalLocation.1.CODE]').val($('input[name=FlightSearchForm.departureLocation.0.CODE]').val());
	}
}

function sel_add_airports(json)
{
	if(json.status == "success")
	{
		var index = 1;
		for(var i=0; i<json.data.length; i++)
		{
			document.getElementById("sity_sel"+index).innerHTML = json.data[i].name + " (" + json.data[i].code + ")";
			AirData[index] = json.data[i].name + " (" + json.data[i].code + ")";
			index++;
			if(index==6) return;
			
			for(var j=0; j<json.data[i].aurports.length; j++)
			{
				document.getElementById("sity_sel"+index).innerHTML = json.data[i].name + ", <br>" + json.data[i].aurports[j].name + " (" + json.data[i].aurports[j].code + ")";
				AirData[index] = json.data[i].aurports[j].name + " (" + json.data[i].aurports[j].code + ")";
				index++;
				if(index==6) return;
			}
		}
	}
}

function handleArrowKeys(evt)
{
	sel_get_texts();
	evt = (evt) ? evt: ((window.event) ? event : null);
	if(evt)
	{
		switch(evt.keyCode)
		{
			case 38:
			{
				if(sity_sel_num>1) sity_sel_hide(sity_sel_num);
				sity_sel_num--;
				if(sity_sel_num<=1) sity_sel_num=1;
				sity_sel_show(sity_sel_num);
				break;
			}
			case 40:
			{
				if((sity_sel_num<sity_sel_max)&&(sity_sel_num>0)) sity_sel_hide(sity_sel_num);
				sity_sel_num++;
				if(sity_sel_num>=sity_sel_max) sity_sel_num=sity_sel_max;
				sity_sel_show(sity_sel_num);
				break;
			}
			case 13:
			{
				sity_sel_select(sity_sel_num);
				break;
			}
		}
	}
}
