
var aThemesCampaigns = new Array();

// Les paramètres aThemesCampaigns ci-dessous doivent être documentés lorsque la config automatique ne s'applique pas,
// notamment dans le cas de campagnes différentes en fonction du theme à la même période de l'année.

aThemesCampaigns[ 'LOC' ] = new Array( 'S08', 'S09' );
aThemesCampaigns[ 'EMP' ] = new Array( 'S08E' );


// Prestations supplémentaires
var aSupplements = new Array();
//aSupplements.push( new Array( 'BIB', '', 3 ) );

var nMaxOccupants = 15;

var g_aMonthsLabel = new Array( 'LANG_date_month_01', 'LANG_date_month_02', 'LANG_date_month_03', 'LANG_date_month_04',
                                'LANG_date_month_05', 'LANG_date_month_06', 'LANG_date_month_07', 'LANG_date_month_08',
                                'LANG_date_month_09', 'LANG_date_month_10', 'LANG_date_month_11', 'LANG_date_month_12');


// makes sure the mandatory fields of the form are filled
function checkSearchObject( sObjectName, pError )
{
  // si aucun etablissement n'est sélectionné, on prend tout ce qui est dans la liste
  if( !getDOMInputValue( sObjectName + '_etab' ) )
  {
    var sSelectedEtabID = "";
    var pSelectedEtab = getDOMObject( sObjectName + '_etab' );
    if( pSelectedEtab )
    {
      for( var nI=0; nI<pSelectedEtab.options.length; nI++ )
        sSelectedEtabID += ( sSelectedEtabID == "" ? "" : "," ) + pSelectedEtab.options[ nI ].value;
      setDOMInputValue( sObjectName + '_etab_list', sSelectedEtabID );
    }
  }
  
//  updateProductCode( sObjectName );
  updateCampaignList( sObjectName );
  updateSupplements( sObjectName );

  // ignore checks when an error was thrown
  if( !pError )
  {
	// On ne test la sélection d'un thème que s'il y a des thèmes et si aucun établissement n'est sélectionné
	var sCurrentSelectedEtabID = getDOMInputValue( sObjectName + '_etab' );
    if( aThemes && aThemes[ 'themes' ].length > 0 && aThemes[ 'themes' ][0] != '' && !getDOMInputValue( sObjectName + '_campaign_list' ) && !sCurrentSelectedEtabID )
    {
      alert( getUserMessage( 'LANG_webres_missing_theme' ) );
      return false;
    }
    var pNBDaysSelect = getDOMObject (sObjectName + '_nb_days');
    var pStayTypeSelect = getDOMObject (sObjectName + '_stay_type');
    
    if (pNBDaysSelect && pStayTypeSelect)
    {
        if (!pNBDaysSelect.options[1])
        {
            var aParams = new Array();
		      aParams['stay_type'] = pStayTypeSelect.options[pStayTypeSelect.selectedIndex].text.toLowerCase();
		      var sMessage = getUserMessage( 'LANG_webres_no_stay_type_for_etab', aParams);
            
            alert( sMessage );
            return false;
        }
    }
    
    if 
    ( 
        (
            !getDOMInputValue( sObjectName + '_nb_days' ) 
            && 
            !getDOMInputValue( sObjectName + '_free_nb_days' )
        ) 
        || 
        (
            !getDOMInputValue( sObjectName + '_lookup_month' ) 
            &&
            !getDOMInputValue( sObjectName + '_start_date' ) 
        )
    )
    {
      alert( getUserMessage( 'LANG_webres_missing_start_date' ) );
      return false;
    }
    
    
    // complexe date verification (based on the month list)
    if( getDOMInputValue( sObjectName + '_start_date' ) )
    {
      var dFirstDate;
      var dLastDate;
      var pMonthSelect = getDOMObject( sObjectName + '_lookup_month' );
      if( pMonthSelect && pMonthSelect.options && pMonthSelect.options.length > 1 )
      {
        var sFirstDate = pMonthSelect.options[ 1 ].value; // ignore the "please select" row
        var sLastDate = pMonthSelect.options[ pMonthSelect.options.length - 1 ].value;
        
        var aMatches1 = new RegExp( '^(.*)_(.*)$' ).exec( sFirstDate );
        if( aMatches1 != "" && aMatches1 != null )
        {
          dFirstDate = new Date( aMatches1[ 2 ], aMatches1[ 1 ] - 1, 1 );
        }

        var aMatches2 = new RegExp( '^(.*)_(.*)$' ).exec( sLastDate );
        if( aMatches2 != "" && aMatches2 != null )
        {
          if( aMatches2[ 1 ] == 12 )
            aMatches2[ 2 ]++;
          dLastDate = new Date( aMatches2[ 2 ], aMatches2[ 1 ], 1 ); // + 1
        }
      } else {
          pMonthSelect = false;
      }

      var dStartDate = parseDate( getDOMInputValue( sObjectName + '_start_date' ) );
      if((pMonthSelect &&
        ( !dFirstDate || datesCompare( dStartDate, dFirstDate ) > 0 ||
          !dLastDate  || datesCompare( dStartDate, dLastDate )  <= 0 ||
          !dStartDate || datesCompare( dStartDate, getCurrentDate() ) >= 0 )
          ) 
        || 
          (!pMonthSelect &&
        ( !dStartDate || datesCompare( dStartDate, getCurrentDate() ) >= 0 )
      )
        )
      {
        alert( getUserMessage( 'LANG_webres_invalid_start_date' ) );
        return false;
      }
    }

    var nOccupants = 0;
    try
    {
      nOccupants += 1 * getDOMInputValue( sObjectName + "_nb_adults" );
      if( window.aAgeFields )
      {
        for( var nI=0; nI < aAgeFields.length; nI++ )
        {
          nOccupants += 1 * getDOMInputValue( aAgeFields[ nI ][0] );
        }
      }
    }
    catch( ex ) { }

    if( nOccupants > nMaxOccupants )
    {
      alert( getUserMessage( 'LANG_webres_contact_group_service' ) );
      return false;
    }
  }
  
  return true;
}

// correspondance Theme-Produit
function updateProductCode( sObjectName )
{
  //for( var sThemeCode in aProductThemes )
  //{
  //  if( getDOMInputValue( sObjectName + '_theme_codes_' + sThemeCode ) )
  //  {
  //    setDOMInputValue( sObjectName + '_product_code', aProductThemes[ sThemeCode ] );
  //    break; // un seul produit, on pourrait l'adapter pour 0 ou plusieurs
  //  }
  //}
}

// correspondance Theme-Campagne
function getCampaignList( sObjectName )
{
  var aCampaigns = new Array();
  for( var sThemeCode in aThemesCampaigns )
  {
    var bIsSelected = false;
    
    // 3 facons de savoir si le thème est sélectionné ou non:
    // - soit on a un champ avec le nom du thème dedans et le code de catégorie dans le nom
    if( getDOMObject( sObjectName + '_theme_codes_' + sThemeCode ) )
    {
      if( getDOMInputValue( sObjectName + '_theme_codes_' + sThemeCode ) )
          bIsSelected = true;
    }
    // - soint un champ caché ou un select
    if( getDOMObject( sObjectName + '_theme_codes' ) )
    {
      if( getDOMInputValue( sObjectName + '_theme_codes' ) )
        bIsSelected = true;
    }
    // - soit on a une liste avec tous les thèmes
    //   mais le problème est qu'on ne connait pas le nom de la liste
    //   (mais on sait qu'il contient le nom de la catégorie du thème)
    else
    {
      for( var sThemeCatCode in aThemes )
      {
        if( getDOMObject( sObjectName + "_theme_code_" + sThemeCatCode ) )
        {
          if( getDOMInputValue( sObjectName + "_theme_code_" + sThemeCatCode ) == sThemeCode )
          {
            bIsSelected = true;
            break;
          }
        }
      }
    }

    if( bIsSelected )
    {
      for( var nJ=0; nJ<aThemesCampaigns[ sThemeCode ].length; nJ++ )
      {
        var bFound = false;
        var sCampaignCode = aThemesCampaigns[ sThemeCode ][ nJ ];
        for( var nK=0; nK<aCampaigns.length; nK++ )
        {
          if( aCampaigns[ nJ ] == sCampaignCode )
          {
            bFound = true;
            break;
          }
        }
        if( !bFound )
          aCampaigns.push( sCampaignCode );
      }
    }
  }
  
  return aCampaigns;
}

// update the selected campaigns using the themes
function updateCampaignList( sObjectName )
{
  var aCampaigns = getCampaignList( sObjectName );

  var sCampaigns = "";
  for( var nL=0; nL<aCampaigns.length; nL++ )
  {
    sCampaigns += ( sCampaigns == "" ? "" : "," ) + aCampaigns[ nL ];
  }
  setDOMInputValue( sObjectName + '_campaign_list', sCampaigns );
  return sCampaigns;
}

function updateSupplements( sObjectName )
{
  // TODO : on pourrait avoir des supplements differents selon l'établissement
  
  //var aSelectedEtab = new Array();
  //if( getDOMInputValue( sObjectName + '_etab' ) )
  //{
  //  aSelectedEtab.push( getDOMInputValue( sObjectName + '_etab' ) );
  //}
  //else
  //{
  //  var pSelectedEtab = getDOMObject( sObjectName + '_etab' );
  //  for( var nI=0; nI<pSelectedEtab.options.length; nI++ )
  //    aSelectedEtab.push( pSelectedEtab.options[ nI ].value );
  //}
  
  var aSelectedSupplements = aSupplements;
  if( !aSelectedSupplements )
    aSelectedSupplements = new Array(); // just in case

  var nAdults        = getDOMInputValue( sObjectName + "_nb_adults" );

  var sSupplements = "";
  for( var nI=0; nI<aSelectedSupplements.length; nI++ )
  {
    var sProduct  = aSelectedSupplements[ nI ][ 0 ];
    var sTimeslot = aSelectedSupplements[ nI ][ 1 ] ? aSelectedSupplements[ nI ][ 1 ] : '';
    var nAge      = aSelectedSupplements[ nI ][ 2 ] ? aSelectedSupplements[ nI ][ 2 ] : '';
    var nQuantity = 0;

    if( window.aAgeFields )
    {
      for( var nI=0; nI < aAgeFields.length; nI++ )
      {
        if(aAgeFields[ nI ][1] < nAge && aAgeFields[ nI ][2] >= nAge)
        {
           nQuantity = 1 * getDOMInputValue( aAgeFields[ nI ][0] );
        }
      }
    }
    if( nQuantity == 0 ) nQuantity = nAdults;

    if( nQuantity > 0 )
      // quantity,product,timeslot,age
      sSupplements += ( sSupplements == "" ? "" : ";" ) + ( nQuantity+","+sProduct+","+sTimeslot+","+nAge );
  }
  setDOMInputValue( sObjectName + '_supplement_list', sSupplements );
  
  return sSupplements;
}

function updateSearchForm( sObjectName, bNoRefresh )
{
  try
  {
    var sXMLForm = serializeSearchForm( sObjectName, true );
    var sXMLRet = remoteCall( "custom::remote::webres::search_form", "updateSearchForm", sXMLForm );
    if( sXMLRet )
    {
      deserializeSearchForm( sObjectName, sXMLRet, true );
    }
  }
  catch( ex )
  {
    if( !bNoRefresh )
    {
      if( ex instanceof Error )
        ex = ex.toString();

      refreshSearchForm( ex );
    }
  }
}

// j'essaie d'avoir le meme XML en entrée et en sortie,
// on a donc des choses qui ne servent pas dans les 2 sens...

function serializeSearchForm( sObjectName, bFast )
{
  var pXMLDoc = XmlUtils_createDocument();

  var pXMLRoot = pXMLDoc.createElement( "search_form" );

  // serialize the themes

  var pThemeCategories = pXMLRoot.appendChild( pXMLDoc.createElement( "theme_categories" ) );
  if( getDOMObject( sObjectName + "_theme_codes" ) ) //un seul input (hidden ou select)
  {
    var pThemeCategory = pThemeCategories.appendChild( pXMLDoc.createElement( "theme_category" ) );
    pThemeCategory.appendChild( XmlUtils_createTextElement( pXMLDoc, "theme_code", getDOMInputValue( sObjectName + "_theme_codes"), "" ) );
  } 
  else
  {
    for( var sThemeCatCode in aThemes )
    {
      var pThemeCategory = pThemeCategories.appendChild( pXMLDoc.createElement( "theme_category" ) );
      pThemeCategory.setAttribute( "category_code", sThemeCatCode );  
      if( getDOMObject( sObjectName + "_theme_code_" + sThemeCatCode ) )
      {
        pThemeCategory.appendChild( XmlUtils_createTextElement( pXMLDoc, "theme_code", getDOMInputValue( sObjectName + "_theme_code_" + sThemeCatCode ), "" ) );
      }
      else
      {
        for( var nI=0; nI< aThemes[ sThemeCatCode ].length; nI++ )
        {
          // pas forcement une checkbox
          if( getDOMInputValue( sObjectName + "_theme_codes_" + aThemes[ sThemeCatCode ][ nI ] ) )
            pThemeCategory.appendChild( XmlUtils_createTextElement( pXMLDoc, "theme_code", aThemes[ sThemeCatCode ][ nI ], "" ) );
        }
      }
    }
  }
  var pStationCategory = pThemeCategories.appendChild( pXMLDoc.createElement( "theme_category" ) );
  pStationCategory.setAttribute( "category_code", 'station' );  
  var sSelectedStation = getDOMInputValue( sObjectName + '_station' );
  if( sSelectedStation )
    pStationCategory.appendChild( XmlUtils_createTextElement( pXMLDoc, "theme_code", sSelectedStation, "" ) );
  
  // serialize the stations

  if( !bFast )
  {
    var pStationsSelect = getDOMObject( sObjectName + '_station' );
    if( pStationsSelect && pStationsSelect.options && pStationsSelect.options.length > 0 )
    {
      var pStations = pXMLRoot.appendChild( pXMLDoc.createElement( "stations" ) );
      for( var nJ=0; nJ < pStationsSelect.options.length; nJ++ )
      {
        if( pStationsSelect.options[ nJ ].value )
        {
          var pStation = pStations.appendChild( pXMLDoc.createElement( "station" ) );
          pStation.appendChild( XmlUtils_createTextElement( pXMLDoc, "code", pStationsSelect.options[ nJ ].value, "" ) );
          pStation.appendChild( XmlUtils_createTextElement( pXMLDoc, "name", pStationsSelect.options[ nJ ].text, "" ) );
        }
      }
    }
  }

  // serialize the etab list

  var pEtabSelect = getDOMObject( sObjectName + '_etab' );
  if( pEtabSelect && pEtabSelect.options && pEtabSelect.options.length > 0 )
  {
    var pEtabs = pXMLRoot.appendChild( pXMLDoc.createElement( "etabs" ) );
    for( var nK=0; nK < pEtabSelect.options.length; nK++ )
    {
      if( pEtabSelect.options[ nK ].value )
      {
        var pEtab = pEtabs.appendChild( pXMLDoc.createElement( "etab" ) );
        pEtab.appendChild( XmlUtils_createTextElement( pXMLDoc, "id", pEtabSelect.options[ nK ].value, "" ) );
        pEtab.appendChild( XmlUtils_createTextElement( pXMLDoc, "name", pEtabSelect.options[ nK ].text, "" ) );
      }
    }
  }

  // stay type
  var nStayType = getDOMInputValue( sObjectName + "_stay_type" );
  if ( nStayType ) pXMLRoot.appendChild( XmlUtils_createTextElement( pXMLDoc, "selected_stay_type", nStayType, "" ) );

  // serialize the stay length

  if( !bFast )
  {
    // La durée du séjour peut être dans un sélecteur ou être un champ libre
    var pLengthSelect = getDOMObject( sObjectName + '_nb_days' );
    var pFreeNbDayInput = getDOMObject( sObjectName + '_free_nb_days' );
    if( pLengthSelect && pLengthSelect.options && pLengthSelect.options.length > 0 )
    {
      var pStayLengths = pXMLRoot.appendChild( pXMLDoc.createElement( "stay_lengths" ) );
      for( var nL=0; nL < pLengthSelect.options.length; nL++ )
        if( pLengthSelect.options[ nL ].value )
          pStayLengths.appendChild( XmlUtils_createTextElement( pXMLDoc, "stay_length", pLengthSelect.options[ nL ].value, "" ) );
    } else if(pFreeNbDayInput && pFreeNbDayInput.value){
      pStayLengths.appendChild( XmlUtils_createTextElement( pXMLDoc, "stay_length", pLengthSelect.value, "" ) );
    }
  }

  // serialize the months

  if( !bFast )
  {
    var pMonthSelect = getDOMObject( sObjectName + '_lookup_month' );
    if( pMonthSelect && pMonthSelect.options && pMonthSelect.options.length > 0 )
    {
      var pMonths = pXMLRoot.appendChild( pXMLDoc.createElement( "months" ) );
      for( var nM=0; nM < pMonthSelect.options.length; nM++ )
      {
        if( pMonthSelect.options[ nM ].value )
        {
          var sMonthKey = pMonthSelect.options[ nM ].value;
          var aMatches = new RegExp( '^(.*)_(.*)$' ).exec( sMonthKey );
          if( sMonthKey == "" || aMatches == null )
            continue;
          
          var pMonth = pMonths.appendChild( pXMLDoc.createElement( "arrivalmonth" ) );
          pMonth.appendChild( XmlUtils_createTextElement( pXMLDoc, "month", aMatches[ 1 ], "" ) );
          pMonth.appendChild( XmlUtils_createTextElement( pXMLDoc, "year", aMatches[ 2 ], "" ) );
        }
      }
    }
  }

  // campaigns
  
  var aCampaigns = getCampaignList( sObjectName );
  if( aCampaigns.length > 0 )
  {
    var pCampaigns = pXMLRoot.appendChild( pXMLDoc.createElement( "campaigns" ) );
    for( var nN=0; nN < aCampaigns.length; nN++ )
      pCampaigns.appendChild( XmlUtils_createTextElement( pXMLDoc, "campaign_code", aCampaigns[ nN ], "" ) );
  }


  // selected values

  // etab id
  var sSelectedEtabID = getDOMInputValue( sObjectName + '_etab' );
  if( sSelectedEtabID )
    pXMLRoot.appendChild( XmlUtils_createTextElement( pXMLDoc, "selected_etab_id", sSelectedEtabID, "" ) );

  if( !bFast )
  {
    // Station
    var sSelectedStation = getDOMInputValue( sObjectName + '_station' );
    if( sSelectedStation )
      pXMLRoot.appendChild( XmlUtils_createTextElement( pXMLDoc, "selected_station", sSelectedStation, "" ) );

    // room type
    var sSelectedRoomTypeID = getDOMInputValue( sObjectName + '_room_type' );
    if( sSelectedRoomTypeID )
      pXMLRoot.appendChild( XmlUtils_createTextElement( pXMLDoc, "selected_room_type", sSelectedRoomTypeID, "" ) );
  }

  // stay length
  var nStayLength = getDOMObject( sObjectName + '_free_nb_days' ) ? getDOMInputValue( sObjectName + "_free_nb_days" ) : getDOMInputValue( sObjectName + "_nb_days" );
  if( nStayLength )
    pXMLRoot.appendChild( XmlUtils_createTextElement( pXMLDoc, "selected_stay_length", nStayLength, "" ) );

    // selected month(s?)
    var nMonth = getDOMInputValue( sObjectName + "_lookup_month" );
    if( nMonth )
      pXMLRoot.appendChild( XmlUtils_createTextElement( pXMLDoc, "selected_month", nMonth, "" ) );

    // start date
    var sStartDate = getDOMInputValue( sObjectName + "_start_date" );
    if( sStartDate && parseDate(sStartDate) )
      pXMLRoot.appendChild( XmlUtils_createDateElement( pXMLDoc, "selected_start_date", parseDate(sStartDate), "" ) );

  // occupants
  if( !bFast )
  {
    var nAdults = getDOMInputValue( sObjectName + "_nb_adults" );
    var nBabies = getDOMInputValue( sObjectName + "_nb_babies" );
    var nChildren = 0;
    if( window.aAgeFields )
    {
      for( var nI=0; nI < aAgeFields.length; nI++ )
      {
        nChildren += 1 * getDOMInputValue( aAgeFields[ nI ][0] );
      }
    }
    if( nAdults || nChildren || nBabies )
    {
      var pOccupants = pXMLRoot.appendChild( pXMLDoc.createElement( "occupants" ) );
      if( nAdults > 0 )
        pOccupants.appendChild( XmlUtils_createTextElement( pXMLDoc, "adults", nAdults, "" ) );
      if( nBabies > 0 )
        pOccupants.appendChild( XmlUtils_createTextElement( pXMLDoc, "babies", nBabies, "" ) );
      if( nChildren )
      {
        var nOccupants;
        for( var nI=0; nI < aAgeFields.length; nI++ )
        {
          nOccupants = getDOMInputValue( aAgeFields[ nI ][0] );
          if(nOccupants > 0)
            pOccupants.appendChild( XmlUtils_createTextElement( pXMLDoc, "children_".nI, nOccupants, "" ) );
        }
      }        
    }      
  }
  
  pXMLDoc.appendChild( pXMLRoot );

  return XmlUtils_getXMLString( pXMLDoc );
}

function deserializeSearchForm( sObjectName, sXMLForm, bFast )
{
  var pXMLDoc = XmlUtils_getXMLDOM( sXMLForm );
  var pXMLRoot = pXMLDoc.documentElement;

  // un cache est il encore possible???

  // update the stations

  var pXMLStations = XmlUtils_getElementByTagName( pXMLRoot, "stations" );
  var pXMLStation = XmlUtils_getChildElements( pXMLStations, "station" );

  var aStationOptions = new Array();
  for( var nI1=0; nI1 < pXMLStation.length; nI1++ )
  {
    var sStationCode = XmlUtils_getElementTextByTagName( pXMLStation[ nI1 ], "code" );
    var sStationName = XmlUtils_getElementTextByTagName( pXMLStation[ nI1 ], "name" );

    aStationOptions.push( new Option( sStationName, sStationCode ) );
  }
  
  var pStationSelect = getDOMObject( sObjectName + '_station' );
  if( pStationSelect && pStationSelect.type == "select-one" )
    setSelectObjectOptions( pStationSelect, aStationOptions, false, true, getUserMessage( 'LANG_webres_please_select' ) );

  // update the etab list
  
  var pXMLEtabs = XmlUtils_getElementByTagName( pXMLRoot, "etabs" );
  var pXMLEtab = XmlUtils_getChildElements( pXMLEtabs, "etab" );

  var aEtabOptions = new Array();
  for( var nI2=0; nI2 < pXMLEtab.length; nI2++ )
  {
    var nEtabID = XmlUtils_getElementTextByTagName( pXMLEtab[ nI2 ], "id" );
    var sEtabName = XmlUtils_getElementTextByTagName( pXMLEtab[ nI2 ], "name" );
    aEtabOptions.push( new Option( sEtabName, nEtabID ) );
  }
  
  var pEtabSelect = getDOMObject( sObjectName + '_etab' );
  if( pEtabSelect && pEtabSelect.type == "select-one" )
    setSelectObjectOptions( pEtabSelect, aEtabOptions, false, true, getUserMessage( 'LANG_webres_please_select_etab' ) );


  // update room type list
  
  var pXMLRoomTypes = XmlUtils_getElementByTagName( pXMLRoot, "room_types" );
  var pXMLRoomType = XmlUtils_getChildElements( pXMLRoomTypes, "room_type" );

  var aRoomTypeOptions = new Array();
  for( var nI2=0; nI2 < pXMLRoomType.length; nI2++ )
  {
    var nRoomTypeID = XmlUtils_getElementTextByTagName( pXMLRoomType[ nI2 ], "id" );
    var sRoomTypeName = XmlUtils_getElementTextByTagName( pXMLRoomType[ nI2 ], "name" );
    aRoomTypeOptions.push( new Option( sRoomTypeName, nRoomTypeID ) );
  }
  
  var pRoomTypeSelect = getDOMObject( sObjectName + '_room_type' );
  if( pRoomTypeSelect )
    setSelectObjectOptions( pRoomTypeSelect, aRoomTypeOptions, false, true, getUserMessage( 'LANG_webres_please_select' ) );


  // update the stay length list
  
  var pXMLStayLengths = XmlUtils_getElementByTagName( pXMLRoot, "stay_lengths" );
  var pXMLStayLength = XmlUtils_getChildElements( pXMLStayLengths, "stay_length" );

  var aLengthOptions = new Array();
  for( var nI3=0; nI3 < pXMLStayLength.length; nI3++ )
  {
    var nNbDays = XmlUtils_getElementText( pXMLStayLength[ nI3 ] );

    aLengthOptions.push( new Option( nNbDays + " " + ( getUserMessage( nNbDays > 1 ? 'LANG_webres_days' : 'LANG_webres_day' ) ), nNbDays ) );
  }
  
  var pLengthSelect = getDOMObject( sObjectName + "_nb_days" );
  if( pLengthSelect && pLengthSelect.type == "select-one" )
    setSelectObjectOptions( pLengthSelect, aLengthOptions, false, false, getUserMessage( 'LANG_webres_please_select' ) );

  // update the month list
  
  var pXMLMonths = XmlUtils_getElementByTagName( pXMLRoot, "months" );
  var pXMLArrivalMonth = XmlUtils_getChildElements( pXMLMonths, "arrivalmonth" );

  var aMonthOptions = new Array();
  for( var nI4=0; nI4 < pXMLArrivalMonth.length; nI4++ )
  {
    var nMonth = XmlUtils_getElementTextByTagName( pXMLArrivalMonth[ nI4 ], "month" );
    var nYear = XmlUtils_getElementTextByTagName( pXMLArrivalMonth[ nI4 ], "year" );

    aMonthOptions.push( new Option( getUserMessage( g_aMonthsLabel[ nMonth-1 ] ) +" "+nYear, nMonth+'_'+nYear ) );
  }
  
  var pMonthSelect = getDOMObject( sObjectName + "_lookup_month" );
  if( pMonthSelect && pMonthSelect.type == "select-one" )
    setSelectObjectOptions( pMonthSelect, aMonthOptions, false, false, getUserMessage( 'LANG_webres_please_select' ) );

  var sSelectedEtabID = XmlUtils_getElementTextByTagName( pXMLRoot, "selected_etab_id" );
  if( sSelectedEtabID )
    setDOMInputValue( sObjectName + '_etab', sSelectedEtabID );

  // Station
  var sSelectedStation = XmlUtils_getElementTextByTagName( pXMLRoot, "selected_station" );
  if( sSelectedStation )
    setDOMInputValue( sObjectName + '_station', sSelectedStation );

  // stay length
  var nSelectedStayLength = XmlUtils_getElementTextByTagName( pXMLRoot, "selected_stay_length" );
  if( nSelectedStayLength )
    setDOMInputValue( sObjectName + '_nb_days', nSelectedStayLength );

  // selected month
  var nSelectedMonth = XmlUtils_getElementTextByTagName( pXMLRoot, "selected_month" );
  if( nSelectedMonth )
    setDOMInputValue( sObjectName + '_lookup_month', nSelectedMonth );
    
  // Date Range
  var pXMLDateRange = XmlUtils_getElementByTagName( pXMLRoot, "daterange" );
  
  if(pXMLDateRange)
  {
    var dStartDate = new Date(getDateFromFormat( XmlUtils_getElementTextByTagName( pXMLDateRange, "startdate" ), 'YYYY-MM-DD'));
    var dEndDate = new Date(getDateFromFormat( XmlUtils_getElementTextByTagName( pXMLDateRange, "enddate" ), 'YYYY-MM-DD'));
    
  	var pXMLStartDates = XmlUtils_getElementByTagName( pXMLRoot, "startdates" );
  	pCalendar.disabledDatesExpression = "";
  	if (pXMLStartDates)
  	{
		var pXMLStartDate = XmlUtils_getChildElements( pXMLStartDates, "startdate" );
	  	var aStartDatesOptions = new Array();
	  	for( var nI5=0; nI5 < pXMLStartDate.length; nI5++ )
	    {
	    	var sStartDate = XmlUtils_getElementText( pXMLStartDate[ nI5 ]);
	    	var dStartDate = new Date(getDateFromFormat( sStartDate, 'YYYY-MM-DD'));
	    	
	    	aStartDatesOptions.push( dStartDate );
	  	}  	
	  	if (aStartDatesOptions.length > 0)
	  	{
	  		
	  		for( var nI6=0; nI6 < aStartDatesOptions.length; nI6++ )
	  		{
	  			if (nI6==0)
	  			{
	  				pCalendar.addDisabledDates(null, formatDate(aStartDatesOptions[0], 'DD/MM/YYYY' ));
	  			}
	  			else if (nI6 == (aStartDatesOptions.length-1))
	  			{
	  				pCalendar.addDisabledDates(formatDate(aStartDatesOptions[nI6-1], 'DD/MM/YYYY' ), formatDate(aStartDatesOptions[nI6], 'DD/MM/YYYY' ));
	  				pCalendar.addDisabledDates(formatDate(aStartDatesOptions[nI6], 'DD/MM/YYYY' ), null);
	  			}
	  			else
	  			{
	  				pCalendar.addDisabledDates(formatDate(aStartDatesOptions[nI6-1], 'DD/MM/YYYY' ), formatDate(aStartDatesOptions[nI6], 'DD/MM/YYYY' ));
	  			}
	      	}
	  	}
	}
  	else if ( dStartDate && dEndDate)
    {
      pCalendar.addEnabledRangeDates( formatDate( dStartDate, 'DD/MM/YYYY' ), formatDate( dEndDate, 'DD/MM/YYYY' ) );
    }
  }

  // other fields (useless)
  
  if( !bFast )
  {
    // start date
    var sSelectedStartDate = XmlUtils_getElementTextByTagName( pXMLRoot, "selected_start_date" );
    if( sSelectedStartDate )
      setDOMInputValue( sObjectName + '_start_date', sSelectedStartDate );
  
    // occupants
    var pOccupants = XmlUtils_getElementByTagName( pXMLRoot, "occupants" );
    if( pOccupants )
    {
      var nAdults = XmlUtils_getElementTextByTagName( pOccupants, "adults" );
      if( nAdults )
        setDOMInputValue( sObjectName + '_nb_adults', nAdults );
      var nBabies = XmlUtils_getElementTextByTagName( pOccupants, "babies" );
      if( nBabies )
        setDOMInputValue( sObjectName + '_nb_babies', nBabies );
        
      var nChildren = 0;
      if( window.aAgeFields )
      {
        for( var nI=0; nI < aAgeFields.length; nI++ )
        {
          nChildren = XmlUtils_getElementTextByTagName( pOccupants, "children_".nI );
          if(nChildren)
            setDOMInputValue( aAgeFields[ nI ][0], nChildren );
        }
      }
    }
  }
}

// old stuff

function updateEtabList( sObjectName )
{
  updateSearchForm( sObjectName );
}  

function updateStayLengthList( sObjectName )
{
  updateSearchForm( sObjectName );
}

function updateMonthList( sObjectName )
{
  updateSearchForm( sObjectName );
}

function updateBirthdateFields()
{
  // afficher/cacher les champs date de naissances (voir le code de Blanco)
  var nChildren = parseInt( getDOMInputValue( 'search_form_nb_children' ) );
  if ( !nChildren || nChildren == 0 )
  {
    hideLayer( 'children_birthdates_table' );
    for( var nID = 1 ; nID <= 10 ; nID++ )
    {
      setDOMInputValue( 'search_form_child_birthdate_' + nID, '' );
    }
  }
  else
  {
    showLayer( 'children_birthdates_table' );
    
    var sBirthdateFieldID = "search_form_child_birthdate_";
    for( var nID = 1 ; nID <= 10 ; nID++ )
    {
      if( nID <= nChildren )
      {
        showLayer( sBirthdateFieldID + nID + '_row', null, null, 'block' );
        if (getDOMInputValue( sBirthdateFieldID + nID) == '' )
          setDOMInputValue( sBirthdateFieldID + nID, 'jj/mm/aaaa' );
      }
      else
      {
        //// si on dépasse le nombre d'enfants :
        // on efface la valeur
        setDOMInputValue( sBirthdateFieldID + nID, '' );
        // et on cache la ligne qui affiche le champ date de naissance
        hideLayer( sBirthdateFieldID + nID + '_row' );
      }
    }
  }
}

