var skyscraperFormName = "SBform";
var childrenDropDowns = new Array('CHILD1', 'CHILD2', 'CHILD3');

var endDay = 30;
var endYearMonth = 200910;
var now = new Date();
var dd = now.getDate();
var mm = now.getMonth() + 1;
var yy = now.getYear();

if (yy < 1900) {
 yy += 1900;
}
if (mm < 10) {
 mmString = 0 + mm.toString();
}
else {
 mmString= mm.toString();
}

todayYearMonth = "" + yy + mmString;
todayYearMonth = parseInt(todayYearMonth, 10);


function setToSelIndex(path,val)
{
 for (i = 0; i < path.length; i++) {
  if (path[i].value == val) {
   path.selectedIndex = i;
  }
 }
}


function fillSelectors()
{
 setToSelIndex(document[skyscraperFormName].sector_1_d, dd);
 setToSelIndex(document[skyscraperFormName].sector_2_d, dd);
 setToSelIndex(document[skyscraperFormName].sector_1_m, yy + "-" + mmString);
 setToSelIndex(document[skyscraperFormName].sector_2_m, yy + "-" + mmString);
}


//document[skyscraperFormName].sector_2_d
Browser = 'E6';
var sector1_o = '';
var sector1_d = '';
var sector2_o = '';
var sector2_d = '';
var swap_icon_ok = true;
var calendarWindow = null;
var sector_2_month_index = 1;
var sector_2_day_index = 1;
var sector_2_range_index = 0;
var sector_2_org_prev = -1;
var sector_2_des_prev = -1;
var sector_2_m_prev = 1;
var sector_2_d_prev = 1;
var sector_2_r_prev = 0;
var oDay;
var oMonth;
var oYear;
var rDay;
var rMonth;
var rYear;

var searchFields = new Array();
searchFields["ControlGroupSearchView$AvailabilitySearchInputSearchView$RadioButtonMarketStructure"] = "(document.forms[0].travel_type[0].checked == true) ? 'RoundTrip' : 'OneWay'";
searchFields["ControlGroupSearchView$AvailabilitySearchInputSearchView$DropDownListMarketOrigin1"] = "document.forms[0].sector1_o.value";
searchFields["ControlGroupSearchView$AvailabilitySearchInputSearchView$DropDownListMarketDestination1"] = "document.forms[0].sector1_d.value";
searchFields["ControlGroupSearchView$AvailabilitySearchInputSearchView$DropDownListMarketDay1"] = "document.forms[0].sector_1_d.value";
searchFields["ControlGroupSearchView$AvailabilitySearchInputSearchView$DropDownListMarketMonth1"] = "document.forms[0].sector_1_m.value";
searchFields["ControlGroupSearchView$AvailabilitySearchInputSearchView$DropDownListMarketOrigin1"] = "document.forms[0].sector1_o.value";
searchFields["ControlGroupSearchView$AvailabilitySearchInputSearchView$DropDownListMarketDestination1"] = "document.forms[0].sector1_d.value";
searchFields["ControlGroupSearchView$AvailabilitySearchInputSearchView$DropDownListMarketDateRange1"] = "'2|2'";
searchFields["ControlGroupSearchView$AvailabilitySearchInputSearchView$DropDownListMarketOrigin2"] = "document.forms[0].sector1_d.value";
searchFields["ControlGroupSearchView$AvailabilitySearchInputSearchView$DropDownListMarketDestination2"] = "document.forms[0].sector1_o.value";
searchFields["ControlGroupSearchView$AvailabilitySearchInputSearchView$DropDownListMarketDay2"] = "document.forms[0].sector_2_d.value";
searchFields["ControlGroupSearchView$AvailabilitySearchInputSearchView$DropDownListMarketMonth2"] = "document.forms[0].sector_2_m.value";
searchFields["ControlGroupSearchView$AvailabilitySearchInputSearchView$DropDownListMarketDateRange2"] = "'2|2'";
searchFields["ControlGroupSearchView$AvailabilitySearchInputSearchView$DropDownListNewPassengerType_ADT"] = "document.forms[0].ADULT.value";
searchFields["ControlGroupSearchView$AvailabilitySearchInputSearchView$DropDownListNewPassengerType_CHILD_1"] = "document.forms[0].CHILD1.value";
searchFields["ControlGroupSearchView$AvailabilitySearchInputSearchView$DropDownListNewPassengerType_CHILD_2"] = "document.forms[0].CHILD2.value";
searchFields["ControlGroupSearchView$AvailabilitySearchInputSearchView$DropDownListNewPassengerType_CHILD_3"] = "document.forms[0].CHILD3.value";
searchFields["__EVENTTARGET"] = "'ControlGroupSearchView$AvailabilitySearchInputSearchView$LinkButtonNewSearch'";
searchFields["expanded"] = "'1'";
searchFields["culture"] = "(document.forms[0].language.value == 'DE') ? 'de-DE' : (document.forms[0].language.value == 'IT') ? 'it-IT' : (document.forms[0].language.value == 'ES') ? 'es-ES' : 'en-GB'";


function setPassengerTypes()
{
 var paxTypes = "";

 var paxTypeCHILDNum = paxTypeCHILDNum + (document.forms[0].CHILD1.value.substr(document.forms[0].CHILD1.value.indexOf(';') + 1) != "none") ? 1 : 0;
 paxTypeCHILDNum += (document.forms[0].CHILD2.value.substr(document.forms[0].CHILD2.value.indexOf(';') + 1) != "none") ? 1 : 0;
 paxTypeCHILDNum += (document.forms[0].CHILD3.value.substr(document.forms[0].CHILD3.value.indexOf(';') + 1) != "none") ? 1 : 0;
 var paxTypeCHILDCode = "CHILD";
 paxTypes = paxTypes + paxTypeCHILDNum + paxTypeCHILDCode;
 
 var paxTypeADULTNum = document.forms[0].ADULT.value;
 var paxTypeADULTCode = "ADULT";
 paxTypes = paxTypes + paxTypeADULTNum + paxTypeADULTCode;
 
 document[skyscraperFormName].pT.value = paxTypes;
}


function submit_SB_Form()
{
 captureDates();
 
 sector1_o = document[skyscraperFormName].sector1_o.options[document[skyscraperFormName].sector1_o.selectedIndex].value;
 sector1_d = document[skyscraperFormName].sector1_d.options[document[skyscraperFormName].sector1_d.selectedIndex].value;
 
 if (sector1_o.length > 3) {
	 sector1_o = sector1_o.substring(1, 4);
 }
 
 if (rDay !== 0 && rMonth !== 0 && rYear !== 0) {
  sector2_o = sector1_d;
  sector2_d = sector1_o;
 }
 else {
  sector2_o = "XXX";
  sector2_d = "XXX";
 }
 
 if (checkCities() && checkDate() && checkPassenger() && finalDate()) {
  document[skyscraperFormName].m1.value = "" + oYear + oMonth + oDay + sector1_o + sector1_d;

  if (sector2_o == "XXX" || sector2_d == "XXX") {
   document[skyscraperFormName].m2.value = "";
   document[skyscraperFormName].nom.value = "1";
  }
  else {
   document[skyscraperFormName].m2.value = "" + rYear + rMonth + rDay + sector2_o + sector2_d;
   document[skyscraperFormName].nom.value = "2";
  }
     
  setPassengerTypes();
  appendNewSearchFields();
  document[skyscraperFormName].submit();
 }
}


function reconcile_dates()
{
 captureDates();
 
 if (rDay !== 0) {
  if (rMonth < oMonth && rYear == oYear || oYear > rYear) {
   document[skyscraperFormName].sector_2_m.options.selectedIndex = document[skyscraperFormName].sector_1_m.options.selectedIndex;
  } else if (rDay < oDay && rMonth == oMonth) {
   document[skyscraperFormName].sector_2_d.options.selectedIndex = document[skyscraperFormName].sector_1_d.options.selectedIndex + 1;
  }
 }
}


function captureDates()
{
 oDay  = document[skyscraperFormName].sector_1_d.options[document[skyscraperFormName].sector_1_d.selectedIndex].value;
 oMonth = document[skyscraperFormName].sector_1_m.options[document[skyscraperFormName].sector_1_m.selectedIndex].value.substring(5, 7);
 oYear = document[skyscraperFormName].sector_1_m.options[document[skyscraperFormName].sector_1_m.selectedIndex].value.substring(0, 4);
 rDay = document[skyscraperFormName].sector_2_d.options[document[skyscraperFormName].sector_2_d.selectedIndex].value;
 rMonth = document[skyscraperFormName].sector_2_m.options[document[skyscraperFormName].sector_2_m.selectedIndex].value.substring(5, 7);
 rYear = document[skyscraperFormName].sector_2_m.options[document[skyscraperFormName].sector_2_m.selectedIndex].value.substring(0, 4);
}


function checkCities()
{
  var okay = false;
  var message = "";
  
  // Make sure cities are entered, and aren't same
  if (sector1_o == 'XXX') {
   message = txtChooseDepartureAirport;
  }
  if (sector1_d == 'XXX') {
   message += txtChooseArrivalAirport;
  }
/*  if ((sector2_o == 'XXX' && sector2_d != 'XXX') || (sector2_o != 'XXX' && sector2_d == 'XXX')) {
   message += txtChooseAirports;
  }*/
  
  if (message !== "") {
   alert(message);
  }
  else {
   okay = true;
  }
  
  return okay;
}  // end checkCities


function checkDate()
{
  var okay = true;
  
  var oMonthYear= '' + oYear + oMonth;
  var rMonthYear= '' + rYear + rMonth;
  
  if (!checkCalendarDate("1", oDay, oMonth, oYear)) {
   okay = false;
  }
  if (!checkCalendarDate("2", rDay, rMonth, rYear)) {
   okay = false;
  }
  
  if (oDay < dd && oMonthYear <= todayYearMonth) {
   alert(txtPastDate);
   document[skyscraperFormName].sector_1_d.options.selectedIndex = dd;
   document[skyscraperFormName].sector_1_m.options.selectedIndex = mm-1;
   captureDates();
   okay = false;
  }
  
  if (oMonthYear < todayYearMonth) {
   alert(txtPastDate);
   document[skyscraperFormName].sector_1_d.options.selectedIndex = dd;
   document[skyscraperFormName].sector_1_m.options.selectedIndex = mm-1;
   captureDates();
   okay = false;
  }
  
  if ( (oDay > 31 && oMonthYear >= endYearMonth) || (rDay > 31 && rMonthYear >= endYearMonth) ) {
   alert(txtFlightScheduleEnde);
   
   if (oDay > 31 && oMonthYear >= endYearMonth) {
     document[skyscraperFormName].sector_1_d.options.selectedIndex = 31;
   }
   if (rDay > 31 && rMonthYear >= endYearMonth) {
     document[skyscraperFormName].sector_2_d.options.selectedIndex = 31;
   }
   
   captureDates();
   okay = false;
  }

  var oneway = false;
  var eles = document.getElementsByName("travel_type");
  var length = eles.length;
  for(var i=0; i<length;i++){
    if(eles[i].checked && eles[i].value == "1"){
        oneway = true
    }
  }
  
  if ( rMonthYear < oMonthYear || (rMonthYear <= oMonthYear && rDay < oDay) ) {
   if (rDay !== 0 && rMonth !== 0 && !oneway) {
    alert(txtArrivalDateBeforeDepartureDate);
    
    document[skyscraperFormName].sector_2_d.options.selectedIndex = eval(document[skyscraperFormName].sector_1_d.options.selectedIndex) + 1;
    document[skyscraperFormName].sector_2_m.options.selectedIndex = eval(document[skyscraperFormName].sector_1_m.options.selectedIndex) + 1;
    
    captureDates();
    
    okay = false;
   }
  }
  else if ((sector2_o == 'XXX' || sector2_d == 'XXX') && (rMonth !== 0 || rDay !== 0)) {
   alert(txtNoAirportsButDatesSelected);
   okay = false;
  }
  
  return okay;
} // checkDate
 
 
function checkCalendarDate(mkt, day, month, year)
{
  var okay = true;
  
  if (month == 2 && day > 29 && isLeapYear(year)) {
   okay = correctDay(29, mkt, txtDateYearFeb);
  }
  else if (month == 2 && day > 28 && !isLeapYear(year)) {
   okay = correctDay(28, mkt, txtDateYearFeb);
  }
  else if (month == 4 && day > 30) {
   okay = correctDay(30, mkt, txtDateApr);
  }
  else if (month == 6 && day > 30) {
   okay = correctDay(30, mkt, txtDateJun);
  }
  else if (month == 9 && day > 30) {
   okay = correctDay(30, mkt, txtDateSep);
  }
  else if (month == 11 && day > 30) {
   okay = correctDay(30, mkt, txtDateNov);
  }
  
  return okay;
}//end checkCalendarDates 


function isLeapYear(year)
{
 var leapYear = false;
 
 if (year%4 === 0) {
  leapYear = true;
  
  if (year%100 === 0 && year%400 !== 0) {
   leapYear = false;  
  }
 }
 
 return leapYear;
}


function correctDay(dayToSelect, mkt, monthText)
{
 var i = 27;
 okay = true;
 
 if (mkt == 1) {
  while (eval(document[skyscraperFormName].sector_1_d.options[i].value) != dayToSelect) {
   i++;
  }
  
  alert(txtOutgoingDepartureDatePre + monthText + txtOutgoingDepartureDateIn + dayToSelect + txtOutgoingDepartureDatePost);
  document[skyscraperFormName].sector_1_d.options.selectedIndex = i;
  okay = false;
  captureDates();
 }
 else if (mkt == 2) {
  while (eval(document[skyscraperFormName].sector_2_d.options[i].value) != dayToSelect) {
   i++;
  }
  
  alert(txtIncomingArrivalDatePre + monthText + txtIncomingArrivalDateIn + dayToSelect + txtIncomingArrivalDatePost);
  document[skyscraperFormName].sector_2_d.options.selectedIndex = i;
  captureDates();
  okay = false;
 }
 
 return okay;
}


function checkPassenger()
{
 if (SetHiddenPax() === false) {
  return false;
 }
 
 var adt = searchFields["ControlGroupSearchView$AvailabilitySearchInputSearchView$DropDownListPassengerType_ADT"];
 var chd = searchFields["ControlGroupSearchView$AvailabilitySearchInputSearchView$DropDownListPassengerType_CHD"];
 var chdd = searchFields["ControlGroupSearchView$AvailabilitySearchInputSearchView$DropDownListPassengerType_CHDD"];
 var infants = searchFields["ControlGroupSearchView$AvailabilitySearchInputSearchView$DropDownListPassengerType_INFANTS"];
 var paxTotal = adt + chd + chdd;
 var paxMax = 9;
 
 if (paxTotal > paxMax) {
   alert(txtPaxMaxPre + paxMax + txtPaxMaxPost);
   return false;
 }
 
 if (paxTotal <= 0 || adt < infants) {
  document.forms[skyscraperFormName].page.value='SEARCH';
 }
 else {
  document.forms[skyscraperFormName].page.value='SELECT';
 }
 
 return true;
} //end checkPassenger


function SetHiddenPax()
{
 var debug = false;
 var ds = document['SBform'];

 if (ds) {
  searchFields['ControlGroupSearchView$AvailabilitySearchInputSearchView$DropDownListPassengerType_ADT'] = ds['ADULT'].selectedIndex;

  var infants = 0, chdd = 0, chd = 0;
  for (var dropDownIndex in childrenDropDowns) {
   var nonAdultType;
   if (debug) { alert('childrenDropDowns: ' + childrenDropDowns); }
   if (debug) { alert('dropDownIndex: ' + dropDownIndex); }
   if (ds[childrenDropDowns[dropDownIndex]]) {
    var dropDownControl = ds[childrenDropDowns[dropDownIndex]];
    if (debug) { alert('dropDownValue: ' + dropDownValue); }
    if (dropDownControl && dropDownControl.value) {
     dropDownValue = dropDownControl.value;
     nonAdultType = dropDownValue.substring(dropDownValue.indexOf(';') + 1);
    }
    if (debug) { alert('nonAdultType: ' + nonAdultType); }
    if (nonAdultType == 'INFANT') {
     infants++;
    } else if (nonAdultType == 'CHDD') {
     chdd++;
    } else if (nonAdultType == 'CHD') {
     chd++;
    }
   }
  }
 } else {
  return false;
 }

 if (debug) { alert('CHD: ' + chd + ' CHDD: ' + chdd + ' INFANTS: ' + infants); }
 searchFields["ControlGroupSearchView$AvailabilitySearchInputSearchView$DropDownListPassengerType_CHD"] = chd;
 searchFields["ControlGroupSearchView$AvailabilitySearchInputSearchView$DropDownListPassengerType_CHDD"] = chdd;
 searchFields["ControlGroupSearchView$AvailabilitySearchInputSearchView$DropDownListPassengerType_INFANT"] = infants;

 return true;
}


function change_sector_2_fields(whichDot, fromOnload)
{
 var roundtrip = false;
 
 if (whichDot === 0) { // workaround for radio button problem in Netscape6
  roundtrip = true;
 }
 else if (!whichDot && document[skyscraperFormName].travel_type[0].checked === true) {
  roundtrip = true;
 }
 
 if (roundtrip === true) {
  if (fromOnload !== true) {
   // change to round trip settings
   var temp_m = document[skyscraperFormName].sector_1_m.selectedIndex + 1;
   var temp_d = document[skyscraperFormName].sector_1_d.selectedIndex + 1;
   if (sector_2_month_index < temp_m || (sector_2_month_index == temp_m && sector_2_day_index < temp_d)) {
    document[skyscraperFormName].sector_2_m.selectedIndex = temp_m;
    document[skyscraperFormName].sector_2_d.selectedIndex = temp_d;
   }
   else {
    document[skyscraperFormName].sector_2_m.selectedIndex = sector_2_month_index;
    document[skyscraperFormName].sector_2_d.selectedIndex = sector_2_day_index;
   }
  }
 }
 else {
  // change to one-way settings
  sector_2_month_index = document[skyscraperFormName].sector_2_m.selectedIndex;
  if (sector_2_month_index === 0) {
    sector_2_month_index = 1;
  }
  
  sector_2_day_index  = document[skyscraperFormName].sector_2_d.selectedIndex;
  if (sector_2_day_index === 0) {
    sector_2_day_index = 1;
  }
  
  document[skyscraperFormName].sector_2_m.selectedIndex = 0;
  document[skyscraperFormName].sector_2_d.selectedIndex = 0;
 }
}


function askAboutRoundTrip(returnOptionField, fromOpenJaw)
{
  // If sector_2_d OR sector_2_m is changed, and one-way is set, a confirmation pops up
  var returnOptionFieldIndex = returnOptionField.options.selectedIndex;
  
  if (document[skyscraperFormName].sector_2_d.selectedIndex !== 0) {
    sector_2_d_prev = document[skyscraperFormName].sector_2_d.selectedIndex;
  }
  
  if (document[skyscraperFormName].sector_2_m.selectedIndex !== 0) {
    sector_2_m_prev = document[skyscraperFormName].sector_2_m.selectedIndex;
  }
  
  if (document[skyscraperFormName].travel_type[1].checked) {
//   if (confirm("\nOh! Sie wollten einen Hin- und Rückflug buchen?\n")) {
    document[skyscraperFormName].travel_type[0].checked = true;    // set to 'Round Trip'
    document[skyscraperFormName].travel_type[1].checked = false;    // set to 'Round Trip'
    change_sector_2_fields(0, false);
    returnOptionField.selectedIndex = returnOptionFieldIndex; // set sector_2_d or sector_2_m to whatever the user chose.
//   }
//   else {
//    document[skyscraperFormName].travel_type[0].checked = false;   // set to 'One Way'
//    document[skyscraperFormName].travel_type[1].checked = true;   // set to 'One Way'
//    returnOptionField.selectedIndex = 0;
//   }
  }
  // If sector_2_d OR sector_2_m are set to 0, and roundtrip is set, a confirmation pops up
  else if (document[skyscraperFormName].travel_type[0].checked && returnOptionField.selectedIndex === 0) {
//   if (confirm("\nKein Rückflugtermin oder Rückflug? Okay. Sollen wir Ihre\nAngaben auf \'One Way\' ändern?\n")) {
    document[skyscraperFormName].travel_type[0].checked = false;    // set to 'One Way'
    document[skyscraperFormName].travel_type[1].checked = true;    // set to 'One Way'
    change_sector_2_fields(1, false);
//   }
//   else {
//    if (document[skyscraperFormName].sector_2_d.selectedIndex == 0) {
//	    document[skyscraperFormName].sector_2_d.selectedIndex = sector_2_d_prev;
//	  }
//    if (document[skyscraperFormName].sector_2_m.selectedIndex == 0) {
//      document[skyscraperFormName].sector_2_m.selectedIndex = sector_2_m_prev;
//    }
//   }
  }
} // end askAboutRoundTrip


function finalDate()
{
  var okay = true;
  
  if (okay === true && oMonth == rMonth && oDay == rDay) {
//   okay = confirm("Ihr Hin- und Rückflug liegen am gleichen Tag.\n\nWollen Sie wirklich am gleichen Tag hin und zurück fliegen?\n");
   okay = true;
  }
  
  var oMonthYear= "" + oYear + oMonth;
/*  
  if (okay === true && oDay == dd && oMonthYear == todayYearMonth) {
   alert("Da nur der heutige Tag ausgewählt wurde, \nwerden auf der nächsten Seite alle Abflüge von heute 3 Stunden \nnach der aktuellen Uhrzeit angezeigt.\n");
  }
*/  
  return okay;
} //end finalDate


fillSelectors();


function appendNewSearchFields()
{
 for (var input in searchFields) {
  if (document.getElementById(input)) {
   document.getElementById(input).value = eval(searchFields[input]);
  }
  else {
   var newNode = document.createElement("input");
   newNode.type = "hidden";
   newNode.id = input;
   newNode.name = input;
   newNode.value = eval(searchFields[input]);
   document.getElementsByTagName("form")[0].appendChild(newNode);
  }
 }
}
