
/**
 * Fill Search form with data from search-form.tpl
 */
function fillSearchForm() {
  fillCountries(true);
  fillRegions(true);
  fillLocations(true);
  fillStyles(true);
  fillDays();
  fillMonths();
}
/**
 * Fill Country Select
 */
function fillCountries() {
  var select = (arguments.length>0) ? arguments[0] : false;
  x$('countrySel').clearOptions();
  x$('countrySel').fillOptions(search_form_countries);
  if(select && typeof search_form_pub_search == 'object')
    x$('countrySel').setValue(search_form_pub_search['idcountry']);
}
/**
 * Fill Days Select
 */
function fillDays() {
  var today = search_form_today.substr(8,2);
  x$('dayselect').clearOptions();
  x$('dayselect').fillOptions(search_form_days);
  x$('dayselect').setValue(today);
}
/**
 * Fill Months Select
 */
function fillMonths() {
  x$('monthyearselect').clearOptions();
  x$('monthyearselect').fillOptions(search_form_months);
}
/**
 * Fill Region select dependant on the selected value of country
 */
function fillRegions() {
  var select = (arguments.length>0) ? arguments[0] : false;
  var regions = new Object();
  var locations = new Object();
  x$('regionSel').clearOptions();
  idcountry = x$('countrySel').getValue();
  var locations_regions = searchData.locations[idcountry];
  regions[0] = '-- All --';

  for(var i in locations_regions) {
    regions[i] = locations_regions[i].name;
  }
  x$('regionSel').fillOptions(regions);
  if(select && typeof search_form_pub_search == 'object')
    x$('regionSel').setValue(search_form_pub_search['idregion']);

}
/**
 * Fill Locations dependant on selected region 
 */
function fillLocations(searchData) {
  x$('locationSel').clearOptions();
  x$('locationSel').fillOptions({0: {id: '0', name: '-- All --'}},null, true);
  var locations = new Array();
  var top = true;
  for (var i in searchData) {
    if (top && (searchData[i].position == '' || (searchData[i].idregion == 0 && searchData[i].is_capital == 0))) {        
        searchData[i-1].name = OPTION_SEPARATOR;
      top = false;
    } else {
      searchData[i].id = searchData[i].idlocation;
    } // end if else        
  } // end for
  x$('locationSel').fillOptions(searchData, currLocation,true);
}
/**
 * Fill Styles Dependant on location
 * 
 */
function fillStyles(searchData) {
  x$('styleSel').clearOptions();
  var default_value = {0: {id: '0', name: '-- All --'}};
  x$('styleSel').fillOptions(default_value, null, true);

  for (var i in searchData)
  {
        searchData[i].id = searchData[i].idstyle;
  }

  x$('styleSel').fillOptions(searchData, currStyle,true);
}



/**
 * Gets all styles from list of locations
 */
function getAllStyles(idlocations) {
  var styles = new Array();
  for(var i in idlocations) {
    try {
      x$(styles).combine(searchData.style_locations[idlocations[i]].split('.'));
    } catch(e) {
      
    }
  }
  return styles;
}

var currLocation;
var currStyle;

/**
 * Update form with relevant content
 * Example: formChange(this);
 * @param elem {object}
 */
function formChange(elem) {
  currLocation = x$('locationSel').getValue();
  currStyle = x$('styleSel').getValue();
  if (elem.id == 'regionSel') {
    sendAjaxMessage('Location', 'select_Location_array', {'idcountry':x$('countrySel').getValue(),'idregion':x$('regionSel').getValue(),'idlocation':x$('locationSel').getValue()});
    sendAjaxMessage('TTG_Style', 'select_Style_array', {'idcountry':x$('countrySel').getValue(),'idregion':x$('regionSel').getValue(),'idlocation':0});
  } else if (elem.id == 'locationSel') {
    sendAjaxMessage('TTG_Style', 'select_Style_array', {'idcountry':x$('countrySel').getValue(),'idregion':x$('regionSel').getValue(),'idlocation':x$('locationSel').getValue()});
  } // end if
/*
  if(searchData != undefined && elem.id == 'countrySel') {
    fillRegions();
    fillLocations();
    fillStyles();
  } else if(searchData != undefined && elem.id == 'regionSel') {
    fillLocations();
    fillStyles();
  } else if(searchData != undefined && elem.id == 'locationSel') {
    fillStyles();
  } else {
    
  }
  x$(elem).setFormDataExtras({hasSearchData:searchData!=undefined}).setAsynchronous().submitForm('PublicAjax','updateSearchForm');
*/
}
/**
 * @param data {object} multiselect data
 */
function ajaxFillMultiselect(data) {
  for(var i in data) {
    fill_multiselect(i,data[i][0],data[i][1]); 
  } 
}

/**
 *
 */
function showSearch() {
  x$('constrain').removeClassName('short-search');
}


