var cities_check_result = [] ;

var cities_check_callback_funcs = [] ;
cities_check_callback_funcs['dep'] = function( str_result, xml_result ) { check_city_callback( str_result, xml_result, 'dep' ) ; } ; 
cities_check_callback_funcs['arr'] = function( str_result, xml_result ) { check_city_callback( str_result, xml_result, 'arr' ) ; } ; 



var citites_input_field_ids = [] ;
citites_input_field_ids['dep'] = 'from_city_field' ;
citites_input_field_ids['arr'] = 'to_city_field' ;

function check_city( input_field, cities_type )
{
	//check that we have a non-empty city-name
	if ( '' == input_field.value )
	{
		cities_check_result[ cities_type ] = str_error_empty_city_name[ cities_type ] ; 
		
		output_cities_check_result() ;
		
		return false ;
	}	
	//check if this city entered from drop-down-list (or at list it looks like it is )
	var is_fm_ajax = /^[A-Z]+,\s.{3}/.exec( input_field.value ) ;
	if ( is_fm_ajax )
	{
		cities_check_result[ cities_type ] = true ; 
		
		return true ;
	}
	//so far we're failing the check
	cities_check_result[ cities_type ] = undefined ; 
	//manually entered city: let's try to process it
	send_check_city_request( cities_type, input_field.value, cities_check_callback_funcs[ cities_type ] ) ;
		
	return false ;//we need to check all this out first
}//function check_city


function send_check_city_request( cities_type, entered_str, backcall_func ) 
{
	//check city with AJAX-request
	var url = cities_types[ cities_type ] ;
	var fields = new Array( 1 ) ;
	fields[ 'str' ] = entered_str ;
	//create ajax handler
	var ajax_handler = new CAjaxHandler ;
	//make request
	ajax_handler.MakeRequest( 'GET', url, fields, backcall_func, true ) ;
}

function output_cities_check_result()
{
	if ( 
		   true == cities_check_result[ 'arr' ]
		&& true == cities_check_result[ 'dep' ]
	  )
	{//great...it seems both citites are alright
		if ( check_dates( can_dates_be_the_same_day ) )
		{//submit the form: we're alright		
			document.flight_form.submit() ;
		}
	}
	else if ( 
		   undefined != cities_check_result[ 'arr' ]
		&& undefined != cities_check_result[ 'dep' ]
	  )
	{//we checked both cities, so far they didn't succeed
		var str_error = '' ;
		if ( true != cities_check_result[ 'arr' ] )
		{
			str_error += cities_check_result[ 'arr' ] + "\n"  ;
			document.getElementById( citites_input_field_ids[ 'arr' ] ).focus() ;
		}
		if ( true != cities_check_result[ 'dep' ] )
		{
			str_error = cities_check_result[ 'dep' ] + "\n" + str_error ;
			document.getElementById( citites_input_field_ids[ 'dep' ] ).focus() ;
		}
		
		alert( str_error ) ;
		
		//make them undefined: so error will not be alerted twice
		cities_check_result[ 'arr' ] = undefined ;
		cities_check_result[ 'dep' ] = undefined ;
	}
}

function check_city_callback( str_result, xml_result, cities_type ) 
{
	var cities = xml_result.getElementsByTagName( 'city' ) ;
	
	if ( 0 == cities.length )
	{
		is_alright = str_error_unknown_or_wrong_city_name[ cities_type ] ;
	}
	else if ( cities.length > 1 )
	{
		is_alright = 'Ambigious ' + cities_str_names[ cities_type ] + ' city-name was entered.' ;
	}
	else if ( 1 == cities.length )
	{
		is_alright = true ;
		document.getElementById( citites_input_field_ids[ cities_type ] ).value = get_city_txt_representation_from_xml( cities[0] ) ;
	}
	
	cities_check_result[ cities_type ] = is_alright ;
//alert( cities_checked[ 'arr' ] ) ;
//alert( cities_checked[ 'dep' ] ) ;	

	output_cities_check_result() ;
	
}//function check_city_backcall
