
/* Mets à jour la liste des marques
 */
function updateSelectMakes() {

  var catId = document.forms['searchForm'].elements['category_id'].value;

  var xhr = getXhr();

  if (! xhr) {

    return false;
  }

  xhr.onreadystatechange = function() {

    if (xhr.readyState == 4 && xhr.status == 200) {

      document.getElementById('makesBloc').innerHTML = xhr.responseText;
      updateSelectRanges();
    }
  }

  xhr.open('GET', '/app/www/index.php?module=stock&action=search:updateSelectMakes&category_id=' + catId);
  xhr.send(null);
  return true;
}


/* Mets à jour la liste des gammes
 */
function updateSelectRanges() {

  var makeId = document.forms['searchForm'].elements['make_name'].value;
  var catId = document.forms['searchForm'].elements['category_id'].value;

  if (typeof(makeId) == 'undefined') return false;
  if (typeof(catId) == 'undefined') return false;

  var xhr = getXhr();

  if (! xhr) {

    return false;
  }

  xhr.onreadystatechange = function() {

    if (xhr.readyState == 4 && xhr.status == 200) {

      document.getElementById('rangesBloc').innerHTML = xhr.responseText;
      //updateResults();
    }
  }

  xhr.open('GET', '/app/www/index.php?module=stock&action=search:updateSelectRanges&make_name=' + makeId + '&category_id=' + catId);
  xhr.send(null);
  return true;
}


/* Construit la requète de recherche
 */
function buildQuery() {

  var makeHtml = document.forms['searchForm'].elements['make_name'];
  var catHtml = document.forms['searchForm'].elements['category_id'];
  var fuelHtml = document.forms['searchForm'].elements['fuel_type'];
  var rangeHtml = document.forms['searchForm'].elements['range'];
  var gearboxHtml = document.forms['searchForm'].elements['gearbox'];
  
  if (! makeHtml) return false;
  if (! catHtml) return false;
  if (! fuelHtml) return false;
  if (! rangeHtml) return false;
  if (! gearboxHtml) return false;

  var ppminHtml = document.forms['searchForm'].elements['price_public_min'];
  var ppmaxHtml = document.forms['searchForm'].elements['price_public_max'];
  var rpminHtml = document.forms['searchForm'].elements['price_reseller_min'];
  var rpmaxHtml = document.forms['searchForm'].elements['price_reseller_max'];

  var priceQuery = '';
  var type = 'public';

  var typeHtml = document.getElementById('search-type');
  if (typeHtml) type = typeHtml.value;

  if (ppminHtml && ppmaxHtml && rpminHtml && rpmaxHtml) {

    var priceQuery = '&price_public_min=' + ppminHtml.value + '&price_public_max=' + ppmaxHtml.value;
    priceQuery = priceQuery + '&price_reseller_min=' + rpminHtml.value + '&price_reseller_max=' + rpmaxHtml.value;
  }
  else if (ppminHtml && ppmaxHtml) {

    var priceQuery = '&price_public_min=' + ppminHtml.value + '&price_public_max=' + ppmaxHtml.value;
  }
  else if (rpminHtml && rpmaxHtml) {

    var priceQuery = '&price_reseller_min=' + rpminHtml.value + '&price_reseller_max=' + rpmaxHtml.value;
  }
 
  var catId = catHtml.value;
  var makeId = makeHtml.value;
  var fuelType = fuelHtml.value;
  var range = rangeHtml.value;
  var gearbox = gearboxHtml.value;

  var pgHtml = document.forms['pageChanger'];  

  if (pgHtml) {

    var resByPage = pgHtml.elements['resultsByPage'].value;
  }
  else {

    var resByPage = 20;
  }

  return 'type=' + type + '&resultsByPage=' + resByPage + 
    '&category_id=' + catId + '&make_name=' + makeId + 
    '&range=' + range + '&fuel_type=' + fuelType + 
    '&gearbox=' + gearbox + priceQuery;
}


/* Trie par colonne
 */
function orderBy(name, type, nbPage) {

  var order = '';
  var sort = 0;

  if (! nbPage) nbPage = 1;

  var resHtml = document.getElementById('stock-results');

  if (! name) {
    
    order = 'price';
  }
  else {

    order = name;
  }

  if (! type) {
    
    sort = 1;
  }
  else {

    sort = type;
  }

  var xhr = getXhr();
  if (! xhr) return false;

  xhr.onreadystatechange = function() {

    if (xhr.readyState == 4 && xhr.status == 200) {

      resHtml.innerHTML = xhr.responseText;
    }
  }    
    
  xhr.open('GET', '/app/www/index.php?module=stock&action=search:updateResults&' + buildQuery() + '&orderBy=' + order + '&sortBy=' + sort 
      + '&pageNumber=' + nbPage);
  xhr.send(null);
  return true;
}


/* Mets à jour les résultats de recherche
 */
function updateResults() {

  var resHtml = document.getElementById('stock-results');
  var query = buildQuery();

  if (query == false) return false;
  
  if (! resHtml) {
   
    var rpminHtml = document.forms['searchForm'].elements['price_reseller_min'];
    var rpmaxHtml = document.forms['searchForm'].elements['price_reseller_max'];

    if (rpminHtml && rpmaxHtml) {

      window.location.href = '/reseller/stock/search?' + query;
    }
    else {

      window.location.href = '/stock/search?' + query;
    }
  }
  else {

    var xhr = getXhr();
    if (! xhr) return false;

    xhr.onreadystatechange = function() {

      if (xhr.readyState == 4 && xhr.status == 200) {

        resHtml.innerHTML = xhr.responseText;
      }
    }    
    
    xhr.open('GET', '/app/www/index.php?module=stock&action=search:updateResults&' + query);
    xhr.send(null);
  }

  return true;
}


function resetForm() {

  var xhr = getXhr();

  if (! xhr) {

    return false;
  }

  xhr.onreadystatechange = function() {

    if (xhr.readyState == 4 && xhr.status == 200) {

      window.location.href = window.location.pathname;
    }
  }

  xhr.open('GET', '/app/www/index.php?module=stock&action=search:resetForm');
  xhr.send(null);
  return true;
}

