function printObject (obj) {
	var result = new String ();
	for (var field in obj) {
		
		result +=field + '=>' + obj[field]; 
		if (typeof obj[field] == 'object') {
			result += "\n" + printObject	 (obj[field]);
		}
	}

	return result;
}

function checkCityName (cityName) {
	for(var i=0;i<cities_names.length;i++) {
		if (cityName.toLowerCase() == cities_names[i].toLowerCase() ) {
			return cities_names[i];
		}
	}
	return null;
}

function findArportsByCity (cityName) {
	for (var i=0;i<cities.length;i++) {
		if (cities[i].name == cityName) {
			return cities[i].airports;
		}
	}
	for (var i=0;i<cities.length;i++) {
		for(var j=0;j<cities[i].airports.length;j++) {
			if (cities[i].airports[j].code == cityName) {
				return [cities[i].airports[j]];
			}
		}
	}
	return [];
}

function clearElement(elm) {
	while(elm.childNodes.length) {
		elm.removeChild(elm.childNodes[0]);
	}
}

function printAirports (airports, container, selectId) {
	var select = document.getElementById(selectId);
	clearElement(select);
	
	if (airports.length == 0) {	
		select.disabled = true;
		return false;
	}
	
	for(var i=0;i<airports.length;i++) {
		var option = document.createElement('option');
		option.setAttribute ('value' , airports[i].code);
		option.innerHTML = airports[i].name;
		select.appendChild (option);
	}
	select.disabled=false;
	return true;
}


function getIataByName (name) {
	for(var i=0;i<cities_names.length;i++) {
		if (name == cities_name[i]) {
			return cities_iatas[i];
		}
	}
	return false;
}


SearchForm = {
	type : 'roundTrip' ,
	setType : function (type) {
		this.type = type;
		this.changeView();
	},
	search_id : '{APP.SEARCH_ID}',	
	progresInterval : null,
	
	changeProgress : function () {
		this.progress += Math.round(Math.random() * 5);
		jQuery("#progressbar").progressbar( 'value' , this.progress );
		if (this.progress >= 100) {
			this.progress = 0;
		}
	},

	hideTime : function () {
		jQuery('#returnTimeCont').hide('fast');
		jQuery('#returnTime').hide('fast');
		jQuery('#departureTime').hide('fast');
		jQuery('#departureTimeCont').hide('fast');
	} ,

	showTime : function () {
		jQuery('#returnTimeCont').show('fast');
		jQuery('#returnTime').show('fast');
		jQuery('#departureTime').show('fast');
		jQuery('#departureTimeCont').show('fast');
	},

	changeTimeState : function (checkbox) {
		if (checkbox.checked) {
			this.hideTime();
		} else {
			this.showTime();
		}
	},
	
	fakeProgress : function () {
		jQuery("#progressbar").show('fast');
		this.stopProgress ();
		this.in_progress = true;
		this.progresInterval = setInterval("SearchForm.changeProgress();" , 500);
	},

	stopProgress : function () {
		this.progress = 0;
		if ( this.progresInterval ) {
			clearInterval (this.progresInterval);
			this.progresInterval = null;
		}
		this.in_progress = false;
	},

	changeView : function () {
		var types = ['roundTrip' , 'oneWay' , 'multiply'];
		for (var i=0;i<types.length;i++) {
			if (types[i] != this.type) {
				document.getElementById(types[i]).className = ''; 
			}else{
				document.getElementById(types[i]).className = 'button1'; 
			}
		}
		this.processType();
	} , 

	processType : function () {
		document.getElementById('error_messages').innerHTML = '';
		jQuery("#error_messages").hide('fast');
		
		switch(this.type) {
			case 'roundTrip':
				jQuery('#returnDateCont').show();
				jQuery('#returnDate').show();
				
				jQuery('#returnTimeCont').show();
				jQuery('#returnTime').show();
				break;

			case 'oneWay':
				jQuery('#returnDateCont').hide();
				jQuery('#returnDate').hide();
				jQuery('#returnTimeCont').hide();
				jQuery('#returnTime').hide();

				break;

			case 'multiply':
				location.href = '/air/multi_search';
				break;
		}
	} ,

	
	in_progress : false,
	
	submit : function () {

		if (this.in_progress) return false;
		
		var errorMessages = this.validate();
		if (errorMessages.length == 0) {
			//this.fakeProgress();
			jQuery("#error_messages").hide('fast');

			var classes = document.getElementsByName('class');
			var className = '';
			
			for (var i=0;i<classes.length;i++) {
				if (classes[i].checked) {
					className = classes[i].value;
					break;
				}
			}

			var is_one_way = SearchForm.type != 'roundTrip' ? 1:0;
			if (!is_one_way && document.getElementById('returnDate').value == '') {
				is_one_way = 1;
			}
			
			var vendor = null;
			if ( jQuery('#is_specific_airline').length &&jQuery('#is_specific_airline')[0].checked && jQuery.trim( jQuery('#airline').val() ) != '') {
				vendor = window.AIRLINE;
			}

			var sender = new ProgressSender('/progress', 'post');
			
			// server settings
			if (window.BACK_URL) {
				sender.setParam('back_url', window.BACK_URL);
			} else {
				sender.setParam('back_url', '/');
			}
			sender.setParam('goto', '/air/multi_search/procesor');
			sender.setParam('search_id', SearchForm.search_id);
			
			// itinerary 1
			sender.setParam('departure_ariports[]'	, window.DEPARTURE_AIRPORT);
			sender.setParam('arrival_ariports[]'	, window.ARRIVAL_AIRPORT);
			sender.setParam('departure_dates[]'		, jQuery('#departureDate').val());
			sender.setParam('departure_times[]'		, jQuery('#departureTime').val());
			
			// itinerary 2
			if (!is_one_way) {
				sender.setParam('departure_ariports[]'	, window.ARRIVAL_AIRPORT);
				sender.setParam('arrival_ariports[]'	, window.DEPARTURE_AIRPORT);
				sender.setParam('departure_dates[]'		, jQuery('#returnDate').val());
				sender.setParam('departure_times[]'		, jQuery('#returnTime').val());
			}

			// traveleres info
			sender.setParam('adults', jQuery('#adults').val());
			sender.setParam('seniors', jQuery('#seniors').val());
			sender.setParam('children', jQuery('#children').val());
			sender.setParam('infant', jQuery('#infant').val());
			
			// children information
			var chooser = new ChildAgeChooser();
			var values = chooser.getValues ();
			for (var i=0;i < values.length; i++) {
				sender.setParam('child_age[]' , values[i]);
			}
			
			// preferences
			if (jQuery('#is_none_stop')[0].checked) {
				sender.setParam('is_none_stop', 'yes');
			}
			sender.setParam('className', className);
			sender.setParam('vendor', vendor);
			var _tmp_action;
			if (location.href.match(/rockingfares/i)) {
				_tmp_action = '/save-air-search';
			} else {
				if (location.href.match(/^https:\/\/.*/i)){
					_tmp_action = 'https://www.rockinfares.com/save-air-search-secure';
				} else {
					_tmp_action = 'http://www.rockinfares.com/save-air-search';
				}
			}
			sender.saveParamsSession(_tmp_action,
				function (data) {
					if (data.success) {
						location.href =  ('/air-search?id=' + data.id);
					}
				}
			);
		} else {
			
			var errorDialog = new ErrorDialog("Error" ,  errorMessages.join("<br />"));
			errorDialog.show ();
		}
		
	} ,

	converStrToDate : function (str, hour) {
		if (str) {
			var tmp = str.split("/");
			var d = new Date();
			
			d.setFullYear(parseInt(tmp[2]),parseInt(tmp[0]) - 1,parseInt(tmp[1]));

			if (hour) {
				d.setHours(hour, 0, 0, 0);
			}
			return d;
		} else {
			return null;
		}
	},

	validate : function () {
		
		var arivalCity = document.getElementById('arivalCity');
		var departureCity = document.getElementById('departureCity');
		var departureDate = document.getElementById('departureDate');
		var returnDate = document.getElementById('returnDate');
		var departureTime = document.getElementById('departureTime');
		var returnTime = document.getElementById('returnTime');

		// number of pass
		var adults = parseInt(document.getElementById('adults').value);
		var seniors = parseInt(document.getElementById('seniors').value);
		var children = parseInt(document.getElementById('children').value);
		var infant = parseInt(document.getElementById('infant').value);
		
		var messages = [];
		var errorId = 0;
		
		var chooser = new ChildAgeChooser();
		var values = chooser.getValues ();
		
		if ( children ) {
			for (var i=0;i<values.length;i++) {
				if (values[i] == '') {
					messages.push ((++errorId) + '.' + pe('CHILD_AGES_REQUIRE'));
					break;
				}
			}
		}
		
		var checker = new PassengerNumberChecker (adults, values, infant , seniors);
		if ( ! checker.isValid ()) {
			messages.push ((++errorId)  + '.' + pe('ONE_INFANT_ONE_ADULT'));
		} else if ( checker.isNeedToCallCustomerCare() ) {
			messages.push ((++errorId)  + '.' + pe('AIR_SEARCH_FORCHILD_RESTRICTION'));
		}
		
		if (!arivalCity.value) {
			messages.push ((++errorId) + '.' + pe('SELECT_ARRIVAL_AIRPORT'));
		}

		if (!adults && !seniors && !children) {
			messages.push ((++errorId) + '.' + pe('SELECT_ONE_PASSENGER_AT_LEAST'));
		}

		if (!departureCity.value) {
			messages.push ((++errorId) + '.' + pe('SELECT_DEPARTURE_AIRPORT'));
		}

		if (arivalCity.value == departureCity.value && departureCity.value) {
			messages.push ((++errorId) + '.' + pe('ARRIVAL_DEPARTURE_THE_SAME'));
		}

		departureDate = jQuery(departureDate).datepicker('getDate');

		if (!departureDate) {
			messages.push ((++errorId) + '.' + pe('SELECT_DEPARTURE_DATE'));
		}
		returnDate = jQuery(returnDate).datepicker('getDate');

		if (this.type == 'roundTrip' && !returnDate) {
			messages.push ((++errorId) + '.' + pe('SELECT_RETURN_DATE'));
		}

		if (this.type == 'roundTrip') {
			if ((departureDate && returnDate) && departureDate.getTime() > returnDate.getTime()) {
				messages.push ((++errorId) + '.' + pe('DEPARTURE_DATE_MORE_THAN_RETURN'));
			}
		}
		
		if (jQuery('#is_specific_airline').length > 0 && jQuery('#is_specific_airline')[0].checked && jQuery.trim(jQuery('#airline').val()) == '') {
			messages.push ((++errorId) + '.' + pe('AIRLINE_IS_REQUIRED'));
		}

		return messages;
	}
};

		
		
