/** * createRequestObject() * ================================== * Creates an XMLHttpRequest object * ================================== */ function createRequestObject(){ var request_o; var xmlhttp /*@cc_on @*/ /*@if (@_jscript_version >= 5) try { xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); }catch(e){ try { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } catch (E) { xmlhttp=false; } } @else xmlhttp=false @end @*/ if (!xmlhttp && typeof XMLHttpRequest!='undefined') { try { xmlhttp = new XMLHttpRequest(); } catch (e) { xmlhttp=false; } } if (!xmlhttp && window.createRequest) { try { xmlhttp = window.createRequest(); } catch (e) { xmlhttp=false; } } return xmlhttp; } var http; /** * getSuburbs() * ================================== * Called by a change to the town dropdown * Calls the DB to retrieve the list of suburbs for that town * ================================== */ function getSuburbs(){ if(document.searchform.town.selectedIndex){ // Empties the suburbs dropdown document.forms['searchform'].suburb.options.length = 0; // Sets the suburbs dropdown to say "Loading" document.forms['searchform'].suburb.options[0] = new Option("Loading... ",""); // Sets index to the selected dropdown option (NOT it's value) var index=document.searchform.town.selectedIndex; // Retrieves XML from the database with an AJAX call. // Sends the VALUE of the selected dropdown index (ie the town id) // And a random value to stop caching // Initialises a request object http = createRequestObject(); http.open('get', 'http://www.funguide.co.nz/ajax_town.php?town='+ document.searchform.town.options[index].value+'&time='+Math.random()); http.onreadystatechange = handleSuburbs; http.send(null); }else{ // If selectedIndex has no value, the towns dropdown only says "Waiting for region" document.forms['searchform'].suburb.options.length = 0; document.forms['searchform'].suburb.options[0] = new Option("Waiting for town...",""); } } /** * handleSuburbs() * ================================== * Called by getSuburbs to handle the various states of the AJAX request * ================================== */ function handleSuburbs(){ // If the request has been completed... and we have XML if((http.readyState == 4)&&(http.responseXML)){ // Setup variables var XMLResponse = http.responseXML; // Get handles to the XML data suburb_list = XMLResponse.getElementsByTagName('town').item(0).firstChild.data; suburb_id = XMLResponse.getElementsByTagName('suburb'); // Empty the suburb dropdown document.forms['searchform'].suburb.options.length = 0; // Set the first option to "Choose..." document.forms['searchform'].suburb.options[0] = new Option("Choose...",""); // Populate the dropdown for(i=0; i",""); } }