var a_optionsCashe = [], o_request; function f_updateOptions (e_source) { var s_source = e_source.name; var e_form = e_source.form; var s_value = e_source.options[e_source.options.selectedIndex].value; var a_values = s_value.split('|'); var n_parentID = a_values[0]; var s_link = a_values[1]; var s_target = a_values[2]; // open URL if supplied if (s_link) { window.open(s_link, '_blank'); } // reset and disalbe dependent selects if option doesn't have sub-options if (!n_parentID) { var b_under = false; for (var i = 0; i < a_hierarchy.length; i++) { if (b_under) { e_form[a_hierarchy[i]].options.length = 1; e_form[a_hierarchy[i]].disabled = true; } else if (a_hierarchy[i] == s_source) b_under = true; } return; } // determine direcly dependent select var s_target; for (var i = 0; i < a_hierarchy.length; i++) if (a_hierarchy[i] == s_source) { s_target = a_hierarchy[i + 1] break; } var e_target = e_form.elements[s_target]; if (!e_target) return; // use cached values if available if (a_optionsCashe[n_parentID]) { f_populateOptions(e_target, a_optionsCashe[n_parentID]); return; } // submit new ajax request for options window.o_request = { 'target': e_target, 'source': e_source, 'parent_id' : n_parentID }; if (window.ActiveXObject) o_request.o_http = new ActiveXObject("Microsoft.XMLHTTP"); else if (window.XMLHttpRequest) o_request.o_http = new XMLHttpRequest(); else throw 'AJAX is not supported'; e_source.disabled = true; o_request.o_http.onreadystatechange = f_processRequest; o_request.o_http.open("GET", 'asbackend.php?id=' + n_parentID, true); o_request.o_http.send(null); } function f_populateOptions (e_target, a_options) { if (!e_target) return; e_target.options.length = 1; for (var i = 0; i < a_options.length; i++) e_target.options[e_target.options.length] = new Option ( a_options[i]['text'] + (a_options[i]['children'] ? ' (' + a_options[i]['children'] + ')' : ''), (a_options[i]['id'] ? a_options[i]['id'] : '') + '|' + (a_options[i]['link'] ? a_options[i]['link'] : '') + (a_options[i]['target'] ? '|' + a_options[i]['target'] : '') ); e_target.disabled = false; e_target.focus(); } function f_processRequest () { if (!window.o_request || o_request.o_http.readyState != 4) return; o_request['source'].disabled = false; if (o_request.o_http.status != 200 && o_request.o_http.status != 304) throw 'AJAX response error'; // parse XML var e_xml = o_request.o_http.responseXML; var o_options = e_xml.getElementsByTagName('option'); var a_options = [], o_option, s_id, s_link, s_target; for (var i = 0; i < o_options.length; i++) { o_option = o_options[i]; var a_option = { 'text': o_option.firstChild.nodeValue, 'id' : o_option.getAttribute('id'), 'children' : o_option.getAttribute('children'), 'link' : o_option.getAttribute('href'), 'target': o_option.getAttribute('target') }; a_options[a_options.length] = a_option; } // add to cache and update the selectbox a_optionsCashe[o_request['parent_id']] = a_options; f_populateOptions(o_request['target'], a_options); }