/* --------------------------------------------------------------- */
// Search page - Multi city Trip tab
//
// var author = Remi Palard;
// author.email = remi.palard@gmail.com;
//
/* --------------------------------------------------------------- */

function multicitySubmit() { 	
	var f = document.forms['multicityTravel'];
	
	if ($('multicityunaccompaniedWarning').style.display != 'none')
		return;
	if ($('multicitymaxWarning').style.display != 'none')
		return;		
	
	if ($('city2FromMulticity').value != '' || $('city2ToMulticity').value != '' || $('l2MulticityDate').value != '')
		$('skipValidation2').value = 'false';
		
	if ($('city3FromMulticity').value != '' || $('city3ToMulticity').value != '' || $('l3MulticityDate').value != '')
		$('skipValidation3').value = 'false';

	if ($('multicityunaccompaniedWarning').style.display != 'none')
		return;
		
	if ($('city1FromMulticity').value != '' && $('city1ToMulticity').value != '')
		$('segmentQuantity').value = 1;	
	if ($('city2FromMulticity').value != '' && $('city2ToMulticity').value != '')
		$('segmentQuantity').value = 2;
	if ($('city3FromMulticity').value != '' && $('city3ToMulticity').value != '')
		$('segmentQuantity').value = 3;

				 
	if (!isPosted){
		var params = $H({"idd":"4.1.1", "trip_type":f.currentTripTab.value});
		pingSurfaid(params);
		isPosted = true;

		// Patch the airport control fields since they may be not synchronized with their visual pairs. 
		for(var index = 0; index < 2; index++) {
			["from", "to"].each(function(direction) {
				var name = "city" + (index + 1) + direction.capitalize() + "Multicity";
				var field = $(name);
				if(field.value.length > 2) {
					$(name + "Code").value = field.value.substr(0, 3).toUpperCase();
				}
			});
		}

		f.submit();
	}
	
	// Close city popup
	if (WindowCityPopup != '') WindowCityPopup.close();	
}
