var flightSearchForm = flightSearchForm || document.forms.flightSearch || document.forms.main;
function getFlightSearchForm() {
  return flightSearchForm = flightSearchForm || document.forms.flightSearch || document.forms.main;
}

function setLetterCodes(depCity, arrCity, depCabin, arrCabin, departLetterCodesSelect, arriveLetterCodesSelect) {
	var route = getRoute(depCity, arrCity);
	if (route.letterCodeId) {
		letterCodes = allLetterCodes[route.letterCodeId];
		setLetterCode(depCabin, departLetterCodesSelect);
		setLetterCode(arrCabin, arriveLetterCodesSelect);
	} else {
		clearOptions(departLetterCodesSelect);
		clearOptions(arriveLetterCodesSelect);
		departLetterCodesSelect.options[0] = new Option('', '');
		arriveLetterCodesSelect.options[0] = new Option('', '');
	}
}
/* Sets the letter codes select box with the appropriate values for the selected cabin. */
function setLetterCode(cabinId, classLeterSelect) {
	var selectedValue = classLeterSelect.value;
	clearOptions(classLeterSelect);
	var index = 1;
	classLeterSelect.options[0] = new Option('', '');
	if (letterCodes[cabinId]) {
		for ( var i = 0; i < letterCodes[cabinId].length; i++) {
			var letter = letterCodes[cabinId][i];
			classLeterSelect.options[index] = new Option(letter, letter);
			if(letter == selectedValue){
				classLeterSelect.options[index].selected = true;
			}
			index++;
		}
	}
}

/* Updates the inbound letter code based on the chosen outbound letter code only for same cabins. */
function changeInboundLetterCode() {
	var depCabin = getFlightSearchForm().classType.value;
	var arrCabin = flightSearchForm.classTypeReturn.value;
	if (depCabin == arrCabin) {
        clearOptions(flightSearchForm.classLetterReturn);
        for (var i = 0; i < flightSearchForm.classLetter.length; i++) {
            var letterCode = flightSearchForm.classLetter.options[i].value;
            flightSearchForm.classLetterReturn.options[i] = new Option(letterCode, letterCode);
        }
        flightSearchForm.classLetterReturn.options[flightSearchForm.classLetter.selectedIndex].selected = true;
    }
}

function doCommomSettingsForLetterCodes(depCityValue, arrCityValue){
	setStyle(getFlightSearchForm().classLetter);
	setStyle(flightSearchForm.classLetterReturn);
	setLetterCodes(depCityValue, arrCityValue, flightSearchForm.classType.value, flightSearchForm.classTypeReturn.value,
			flightSearchForm.classLetter, flightSearchForm.classLetterReturn);
}
/*Set the default letter code*/
function setDefaultLetterCode(letterCodesSelect) {
  var initialClassLetterValue = getFlightSearchForm().classLetter.value;
  for (var i = 0; i < letterCodesSelect.options.length; i++) {
    var letter = letterCodesSelect.options[i].value;
    if (initialClassLetterValue == letter && '' != initialClassLetterValue) {
    	letterCodesSelect.options[i].selected = true;
      break;
    }
    else
      if (letter == 'B' || letter == 'S' || letter == 'D') {
    	  letterCodesSelect.options[i].selected = true;
      }
  }
}

function updateInboundCabin(classType, classTypeReturn) {
  var selectedCabin = classType.options[classType.selectedIndex];
  // clear down the select box
  clearOptions(classTypeReturn);
  classTypeReturn.options[0] = new Option(selectedCabin.text, selectedCabin.value);
}

function updateCabinAndLetterCodes(depCityPanel, arrCityPanel, classType, classTypeReturn, classLetter, classLetterReturn) {
  setLetterCodes(depCityPanel.value, arrCityPanel.value, classType.value, classTypeReturn.value, classLetter, classLetterReturn);
  setDefaultLetterCode(classLetter);
  setDefaultLetterCode(classLetterReturn);
}



