var submit_tr = document.getElementById("submit_tr");
// hide the submit button table row...
submit_tr.style.visibility = 'hidden';

var no_models_found = document.getElementById("no_models_found");
no_models_found.style.display = "block";

var model_data = document.getElementById("model_data");
model_data.style.display = "none";

var submit_form = false;

// retrieves the XMLHttprequest object
	function createXmlHttpRequestObject()
	{
	    
	    // will store the reference to the XMLHttpRequest object
	    var xmlHttp;
	    // if running Internet Explorer
	    if(window.ActiveXObject)
	    {
		try {
		    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e){
		    xmlHttp = false;
		}
	    }
	    
	    // if running mozilla or other browsers
	    
	    else {
		
		try {
		    
		    xmlHttp = new XMLHttpRequest();
		    
		} catch (e) {
		    
		    xmlHttp = false;
		    
		}
		
	    }
	    
	    // return the created object or display an error message
	    if(!xmlHttp) {
		alert("Error creating the XMLHttpRequest object.");
		return false;
	    } else {
		return xmlHttp;
	    }
	}


	// stores the reference to the XMLHttpRequest object
	var xmlHttp = createXmlHttpRequestObject();

	var model_select = document.getElementById('Model_Select');
	var make_select_element = document.getElementById('Brand_Select');
	
	var make_value = '0';
	
	function updateMakeValue() {
		make_value = make_select_element.value;
	}
	
	function resetModelList() {
		while(model_select.firstChild) {
			model_select.removeChild(model_select.firstChild);
		}
		submit_tr.style.visibility = 'hidden';
		model_data.style.display = 'none';
		no_models_found.style.display = 'block';
	}
		
	
	function getModelList() {
		updateMakeValue();
		if(make_value == '0') {
			resetModelList();
			return false;
		} else {
			// proceed only if the xmlHttp object isn't busy
			if(xmlHttp.readyState == 4 || xmlHttp.readyState == 0) {
			    xmlHttp.open("GET", "get_model_list.php?make_id="+make_value, true);
			    //define the method to handle server response
			    xmlHttp.onreadystatechange = handleModelList;
			    // post variables
			    xmlHttp.send(null);
			} else {
			    alert("Sorry, the list of models could not be loaded in getModelList.");
			}
			return false;
		}
	}
	


	// executed automatically when a message is receieved from the server
	function handleModelList() {
		// move forward only if the transaction has completed
		if(xmlHttp.readyState == 4) {
			// status of 200 indicates the transaction completed successfully
			if(xmlHttp.status == 200) {
				// extract the XML retrieved from the server
				var xmlResponse = xmlHttp.responseXML;
				// obtain the document element (the root element) of the XML structure
				var xmlDocumentElement = xmlResponse.documentElement;
				var models = xmlDocumentElement.getElementsByTagName('model');
				
				resetModelList();
				
				
				
				for (j=0;j<models.length;j++) {
				    new_option = document.createElement('option');
				    new_option.value = models[j].getAttribute('id');
				    
				    new_text_node = document.createTextNode(models[j].firstChild.nodeValue);
				    new_option.appendChild(new_text_node);
				    
				    model_select.appendChild(new_option);
				}
				
				if(models.length > 0) {

				    submit_tr.style.visibility = 'visible';
				     no_models_found.style.display = "none";
				     model_data.style.display = "block";
				} else {
				    resetModelList();
				    no_models_found.style.display = "block";
				}

			} else {
				alert('Sorry, the list of models could not be loaded in handleModelList.');
			}
		}
	}

	
	window.onload = getModelList;
	make_select_element.onchange = getModelList;