//////////////////////////////////////////////////////////////
// Global variables for form elements
// default values for flightInputForm set here
var FORM = "flightInputForm";
var DEPART_DAY = "selectedDay_1";
var DEPART_MONTH = "selectedMonth_1";
var RETURN_DAY = "selectedDay_2";
var RETURN_MONTH = "selectedMonth_2";
var FARE_CATEGORY = "selectedFareCategory";
var SEARCH_TYPE = "selectedSearchType";
var FLIGHT_TYPE = "selectedFlightType";
var LOC = "en";

//////////////////////////////////////////////////////////////
// This function populates the global variables for form elements
// This funciton should be called when values other than the default ones are needed.
function populateGlobalVars(newForm, newDepartDay, newDepartMonth, newReturnDay, newReturnMonth){
	FORM = newForm;
	DEPART_DAY = newDepartDay;
	DEPART_MONTH = newDepartMonth;
	RETURN_DAY = newReturnDay;
	RETURN_MONTH = newReturnMonth;
}

//////////////////////////////////////////////////////////////
// This function disables the return date fields when oneway is selected
function disableRtnDate(disable){
	if (disable == true){
		eval("document."+ FORM +"."+ RETURN_DAY +".disabled=true;");
		eval("document."+ FORM +"."+ RETURN_MONTH +".disabled=true;");
	}else{
		eval("document."+ FORM +"."+ RETURN_DAY +".disabled=false;");
		eval("document."+ FORM +"."+ RETURN_MONTH +".disabled=false;");
	}
}

//////////////////////////////////////////////////////////////
// This function disables the search fields when Business is selected
// It also ensures that the fixed option is selected for Business searches
function disableSearchFields(){
	var i;

	eval("var len = document."+ FORM +"."+ SEARCH_TYPE +".length");
	if (!len) {	len = 1	}

	eval("var selectedFareCategory = document."+ FORM +"."+ FARE_CATEGORY +".value");
	var intSelecetedFareCategory = parseInt(selectedFareCategory);
	// 2 is for return Business and 7 is for oneway Business
	if ( (intSelecetedFareCategory == 2) || (intSelecetedFareCategory == 7) ){
		// select the fixed search option and disable all options
		if (len == 1){
			eval("document."+ FORM +"."+ SEARCH_TYPE +".checked=true;");
		}else{
			eval("document."+ FORM +"."+ SEARCH_TYPE +"[0].checked=true;");
			//eval("document."+ FORM +"."+ SEARCH_TYPE +"[0].disabled=true;");
			eval("document."+ FORM +"."+ SEARCH_TYPE +"[1].disabled=true;");
		}
	}else{
		// endable all options
		if (len != 1){
			eval("document."+ FORM +"."+ SEARCH_TYPE +"[0].disabled=false;");
			eval("document."+ FORM +"."+ SEARCH_TYPE +"[1].disabled=false;");
		}
	}
}

//////////////////////////////////////////////////////////////
// This function changes the selected day on the return day combo box
// to match the selected day on the depart day combo box
// if the months are the same and the selected return day is less than the depart day
function changeReturnDay(){
	// get days
  eval("var selectedDepartDay = document."+ FORM +"."+ DEPART_DAY +".selectedIndex");
  eval("var selectedReturnDay = document."+ FORM +"."+ RETURN_DAY +".selectedIndex");
	// get months
	eval("var selectedDepartMonth = document."+ FORM +"."+ DEPART_MONTH +".value");
  eval("var selectedReturnMonth = document."+ FORM +"."+ RETURN_MONTH +".value");

	if ((selectedDepartMonth == selectedReturnMonth) && (selectedReturnDay < selectedDepartDay) ){
		eval("document."+ FORM +"."+ RETURN_DAY +".options[selectedDepartDay].selected=true");
	}
}

//////////////////////////////////////////////////////////////
// This function gets the number of days for the selected month
// and populates the dayCombo with the appropriate number of days.
// If on the depart leg this function also sets the returning month
// to be the same month (if the return month is before the new selected month)
// and updates the return dayCombo accordingly.
function changeMonth(leg){
	// decide if the depart leg or return leg month was changed
	var dayCombo, monthCombo;
	if (leg == "depart"){
		dayCombo = DEPART_DAY;
		monthCombo = DEPART_MONTH;
	}else if (leg == "return"){
		dayCombo = RETURN_DAY;
		monthCombo = RETURN_MONTH;
	}

	// get the month selected index and the month value
	eval("var depMonthSelectedIndex = document."+ FORM +"."+ monthCombo +".selectedIndex");
	eval("var depMonthSelected = document."+ FORM +"."+ monthCombo +".options[depMonthSelectedIndex].value");
  var intMonthSelected = parseInt(depMonthSelected,10);

	// check to see if the month is for this year or next year,
	// e.g if it's less than todays month it in next year so change the year to the following year
  var currDate = new Date();
  var currMonth = (currDate.getUTCMonth());
	var currYear = currDate.getYear();
  if (intMonthSelected < currMonth) {
    var numOfDays = getLenMonth(currYear + 1, intMonthSelected);
  } else {
    var numOfDays = getLenMonth(currYear, intMonthSelected);
  }

  // change the days in the corresponding day combo box
	// to equal the number of days for the selected month
  popDays(numOfDays, dayCombo);

	// if depart month was changed and if return month is less than selected depart month
	// change it to depart month and change the days in the returning day combo boxes
	// to equal the number of days for the selected month.
	if (leg == "depart"){
		eval("var retMonthSelectedIndex = document."+ FORM +"."+ RETURN_MONTH +".selectedIndex");
		if (retMonthSelectedIndex < depMonthSelectedIndex){
			eval("document."+ FORM +"."+ RETURN_MONTH +".options[depMonthSelectedIndex].selected=true");
	  	popDays(numOfDays, RETURN_DAY);
		}
		// need to call changeReturnDay() to alter the return day if it is before the depart day
		changeReturnDay();
	}
}

//////////////////////////////////////////////////////////////
// Helper function that gets the number of days in a month
function getLenMonth(theYear, theMonth){
	var oneHour = 1000 * 60 * 60;
	var oneDay = oneHour * 24;
	var thisMonth = new Date(theYear, theMonth, 1);
	var nextMonth = new Date(theYear, theMonth + 1, 1);
	var len = Math.ceil((nextMonth.getTime() - thisMonth.getTime() - oneHour)/oneDay);
	return len;
}

//////////////////////////////////////////////////////////////
// Helper function that populates the dayCombo box with
// the number of days in the selected month
function popDays(numOfDays, daySel) {
	eval("var len = document."+ FORM +"."+ daySel +".length");
  eval("var selectedDay = document."+ FORM +"."+ daySel +".selectedIndex + 1");

  // clear the array
  for (var i = 0; i < len; i++) {
		eval("document"+"."+ FORM +"."+ daySel +".options[i] = null");
	}

	// populate the array
	for(var i = 0; i < numOfDays; i++) {
		eval("document"+"."+ FORM +"."+ daySel +".options[i] = new Option(i+1)");
		eval("document"+"."+ FORM +"."+ daySel +".options[i].value ="+ (i+1));
		if (i == (selectedDay - 1)) {
			eval("document"+"."+ FORM +"."+ daySel +".options[i].selected = true");
		}
	}
}

//////////////////////////////////////////////////////////////
// This function opens the calendar passing in the selected
// day, month and leg.
function openCalendar(strPath, leg, locale) {
	var width=255,height=240;

	var monthSel, daySel;
  if (leg == "depart") {
	  monthSel = DEPART_MONTH;
		daySel = 	DEPART_DAY;
	} else   if (leg == "return") {
	  monthSel = RETURN_MONTH;
		daySel = 	RETURN_DAY;
	}

	eval("var daySelectedIndex = document."+ FORM +"."+ daySel +".selectedIndex");
	eval("var monthSelectedIndex = document."+ FORM +"."+ monthSel +".selectedIndex");
	eval("var daySelected = document."+ FORM +"."+ daySel +".options[daySelectedIndex].value");
    eval("var monthSelected = document."+ FORM +"."+ monthSel +".options[monthSelectedIndex].value");

    if ((locale == null)||(locale == "")){
        locale = LOC;
    }
  strPath = strPath +"calendar.do?daySelected="+ daySelected +"&monthSelected="+ monthSelected +"&leg="+ leg+"&bookingLocale="+locale;
  window.open(strPath,'Calendar','width=' + width + ',height=' + height);
}

//////////////////////////////////////////////////////////////
// This function sets the dayCombo and monthCombo with the
// date that was selected in the calendar pop-up
function setDate(newDay, newMonth, leg) {
	var monthSel, daySel;
	var monthSelectedIndex = 0;

  if (leg == "depart") {
	  monthSel = eval("document."+ FORM +"."+ DEPART_MONTH);
		daySel = 	eval("document."+ FORM +"."+ DEPART_DAY);
	} else if (leg == "return") {
	  monthSel = eval("document."+ FORM +"."+ RETURN_MONTH);
		daySel = 	eval("document."+ FORM +"."+ RETURN_DAY);
	}

	// set the month to the newly selected month
	for (i = 0; i < 12; i++) {
		if(newMonth == (parseInt(monthSel.options[i].value,10))) {
  	 	monthSelectedIndex = i;
    }
  }
  monthSel.options[monthSelectedIndex].selected = true;
  changeMonth(leg);

  // set the day to the newly selectd day
	var intNewDay = parseInt(newDay) - 1;
	daySel.options[intNewDay].selected = true;

	// if setting the depart leg call changeDay to see if return leg needs to be changed
	if (leg == "depart") {
		changeReturnDay();
	}
}

//////////////////////////////////////////////////////////////
// this function updates the destination drop down with the
// list of airports that are destinations for the selected origin
//
// NOTE: because of different behaviour in different browsers
// it is necessary to use the sessDest var if it is not ""
// can talk to warrecl about this if you are curious.
function updateDestinations(origBox, destBox, fromOnload, sessDest) {

	// get destination value
	var destIndex = 0;
	var destValue = "";
	destIndex += eval(destBox.selectedIndex);
	if (destIndex == -1){
		destIndex = 0;
	} else{
		destValue = destBox.options[destIndex].value;
	}
	// sessDest overrides destValue if it is available
	if (sessDest != ""){
		destValue = sessDest;
	}

	var origIndex = eval(origBox.selectedIndex);
	if (origIndex!=-1){
		var orig = origBox.options[origBox.selectedIndex].value;
		//alert(orig);
		if ((orig != "null") && (orig != "")) {
			//alert("went here");
			orig = eval(orig);
			destBox.length = 0;

			for (var i = 0; i < orig.length - 1; i++) {
				destBox.length = i+1;
				destBox.options[i] = new Option(orig[i].meaning);
				destBox.options[i].value = orig[i].code;
				// set selected element in drop down
				if ((destValue != "") && (orig[i].code == destValue)){
					destBox.options[i].selected = true;
				}
			}

		} else {
			destBox.length = 1;
			destBox.options[0] = new Option("--- none ---");
			destBox.options[0].value = "null";
			var i = 1;
		}
		if(destBox.length==0)
		{
			destBox.options[0] = new Option("--- none ---");
			destBox.options[0].value = "null";
			document.getElementById('Submit').disabled=true;
		}else
		{
			document.getElementById('Submit').disabled=false;
		}
	}
}

//////////////////////////////////////////////////////////////
// this function stores the currently selected destination
// into a hidden form field
function updateDestinationOnChange(destBox, destHiddenField) {
	var detailIndex = eval(destBox.selectedIndex);
	if (detailIndex!=-1) {
		destHiddenField.value = destBox.options[detailIndex].value;
 	} else {
		destHiddenField.value = '';
	}
}

//////////////////////////////////////////////////////////////
// this function updates Return field in Change PNR screen
// and also updates a hidden form field if necessary
function updateReturnDestinationAirport(formname,destOut,destReturn,destReturnHidden){
    eval("var outText = "+formname+"."+destOut+".options["+formname+"."+destOut+".selectedIndex].text;");
    eval("var outValue = "+formname+"."+destOut+".options["+formname+"."+destOut+".selectedIndex].value;");
    eval(formname+"."+destReturn+".value = outText;");
    if(destReturnHidden.length > 0){
        eval(formname+"."+destReturnHidden+".value = outValue;");
    }
}

//This function establishes an initial outbound destination airport
//on plan new pnr screen
function selectOutboundDestination(formName, optionsName, valueName){
	eval("var opts = "+formName+"."+optionsName);
	var i;
	for(i=0;i!=opts.length;i++){
		if(opts[i].value == valueName){
			opts[i].selected = true;
		}

	}
}


