let models = {0 : {0: '' }, 7 : { 0: '', 101: 'Q5', 166: 'Q7', 124: 'А5', 31: 'А6'}, 13 : { 0: '', 149: 'Х5'}, 5 : { 0: '', 226: '300C 2004-2011', 19: '300М', 260: 'Avenger 2007-2010', 197: 'Caliber', 14: 'Caravan', 12: 'Cirrus', 21: 'Concorde', 29: 'Grand Caravan', 8: 'Intrepid 1997-2004', 136: 'Journey', 9: 'Neon', 20: 'Pacifica', 224: 'PT Cruiser', 192: 'Ram 1994-2001', 327: 'RAM 2011-', 10: 'Sebring 2001-2006', 223: 'Sebring 2007-', 191: 'Stratus', 233: 'Town & Country', 221: 'Voyager 1996-2000', 225: 'Voyager 2001-2007'}, 11 : { 0: '', 67: 'Escape', 88: 'Focus'}, 22 : { 0: '', 256: 'Deer'}, 10 : { 0: '', 34: 'Elantra', 47: 'IX35', 55: 'Santa Fe', 118: 'Solaris'}, 26 : { 0: '', 74: 'FX37'}, 6 : { 0: '', 234: 'Cherokee XJ', 158: 'Compass', 7: 'Grand Cherokee', 202: 'Grand Cherokee 1993-1998', 24: 'Liberty', 17: 'Wrangler'}, 20 : { 0: '', 60: 'Cerato', 64: 'Mohave', 248: 'Optima', 328: 'Picanto', 79: 'Rio', 134: 'Sorento'}, 18 : { 0: '', 93: 'Freelander 2', 85: 'Range Rover Evoque', 319: 'Range Rover Sport'}, 15 : { 0: '', 61: 'LX 570', 39: 'LX470', 62: 'RX 330', 151: 'RX350'}, 9 : { 0: '', 33: 'СХ7'}, 12 : { 0: '', 54: 'C', 51: 'G Klass', 91: 'S 600', 177: 'W210', 36: 'Виана'}, 16 : { 0: '', 56: 'Galant', 46: 'Lancer 9', 331: 'Montero sport', 117: 'Outlander', 52: 'Pajero'}, 17 : { 0: '', 120: 'Pathfinder', 288: 'Terrano'}, 24 : { 0: '', 100: 'Antara', 293: 'Astra', 69: 'Astra H'}, 28 : { 0: '', 241: '107', 242: 'Boxer'}, 27 : { 0: '', 274: 'Megane'}, 14 : { 0: '', 71: 'Fabia', 53: 'Octavia', 38: 'Superb', 78: 'Yeti'}, 21 : { 0: '', 65: 'Kyron', 282: 'Rexton'}, 47 : { 0: '', 181: 'Outback'}, 2 : { 0: '', 212: 'Grand Vitara 1998-2005', 215: 'Grand Vitara 2005-2012', 209: 'Grand Vitara XL-7 2001-2006', 261: 'Kizashi', 18: 'Liana', 214: 'SX4 2006-2014', 48: 'Tahoe', 2: 'Tracker', 28: 'Tracker cabrio', 26: 'Vitara 1998-2005'}, 8 : { 0: '', 49: 'Camry', 150: 'Corolla', 63: 'Fortuner/Hilux', 250: 'Land Cruiser 100', 32: 'Land Cruiser 200', 87: 'Land Cruiser Prado', 114: 'RAV4', 153: 'Sienna'}, 23 : { 0: '', 144: 'Passat', 128: 'Sharan', 68: 'Touareg', 329: 'Transporter'}, 19 : { 0: '', 271: '940', 129: 'S 60', 245: 'V50', 58: 'XC70', 57: 'XC90'}, 35 : { 0: '', 313: 'LYNX'}, }; function setModels(markId) { let form = document.forms['main_search']; let select = form.elements['model']; select.options.length = 0; let modelsList = models[markId]; for (var key in modelsList) { select.options[select.options.length] = new Option(modelsList[key], key); } } function setNamesList() { let form = document.forms['main_search']; let markId = form.elements['mark'].options[form.elements['mark'].selectedIndex].value; let modelId = form.elements['model'].options[form.elements['model'].selectedIndex].value; getList(markId, modelId); } function setList(namesList) { let list = document.getElementById('autopart-names'); if (!Array.isArray(namesList)) namesList = []; while (list.firstChild) { list.firstChild.remove(); } namesList.forEach(function(item){ let option = document.createElement('option'); option.text = item; list.appendChild(option); }); } function getList(markId, modelId) { let xhr = new XMLHttpRequest(); //console.log('/catalog/names.php?mark_id=' + markId + '&model_id=' + modelId); xhr.open('GET', '/catalog/names.php?mark_id=' + markId + '&model_id=' + modelId, true); xhr.onreadystatechange = function() { if (xhr.readyState != 4) return; if (xhr.status != 200) { // status = 0 при ошибках сети, иначе status=HTTP-код ошибки return; } // обработка результата setList(JSON.parse(xhr.responseText)); } xhr.send(null); } function initForm() { if (!document.forms['main_search']) return; let form = document.forms['main_search']; form.elements['mark'].onchange = function() { let newId = this.options[this.selectedIndex].value; setModels(newId); setNamesList(); } ; form.elements['model'].onchange = setNamesList; } document.addEventListener("DOMContentLoaded", initForm);