// Have a function run after the page loads:
window.onload = init;

// Function that adds the Ajax layer:
function init() {
	// Get an XMLHttpRequest object:
	var ajax = getXMLHttpRequestObject();	
	if(ajax) { // Attach the function call to the form submission, if supported:
		// Check for DOM support:
		if(document.getElementById('dealerdisplay')) {
			document.getElementById('dlrform').onsubmit = function() {
				var state = document.getElementById('stateselect').value;
				// Open the connection:
				ajax.open('get', '/mm5/assets/v9/js/locater.php?state=' + encodeURIComponent(state));
				ajax.onreadystatechange = function() { // Function that handles the response
					handleResponse(ajax);
				}				
				ajax.send(null); // Send the request
				return false; // So form isn't submitted.
			} //end onsubmit function
		} //end DOM check
	} //end ajax IF
	
} //end init() function


// Function that handles the response from the PHP script:
function handleResponse(ajax) {
  if (ajax.readyState == 4) { // Checks that the transaction is complete
    if ((ajax.status == 200) || (ajax.status == 304) ) { // Check for a valid HTTP status code
      // Put the received response in the DOM:
      var dealerdisplay = document.getElementById('dealerdisplay');
      dealerdisplay.innerHTML = ajax.responseText;      
    } else { // Bad status code, submit the form.
      document.getElementById('dlrform').submit();
    }    
  } // End of readyState IF.  
} // End of handleResponse() function.