function createRequestObject() {
    var theRequestObject;
    if (window.XMLHttpRequest) { // Firefox, others...
        theRequestObject = new XMLHttpRequest();
    } else if (window.ActiveXObject) { // IE
        try {
            theRequestObject = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                theRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }
    if (!theRequestObject) {
        alert('Error: the Request Object is not created.');
        return false;
    }
    return theRequestObject;
}

//Make the XMLHttpRequest Object
var http = createRequestObject();

function sendRequest(method, url){
	if(method == 'get' || method == 'GET')
	{
		http.open(method,url,true);
		http.onreadystatechange = handleResponse;
		http.send(null);
	}
}

function handleResponse(){
	if(http.readyState == 4 && http.status == 200)
	{
		var response = http.responseText;
		if(response)
		{
			document.getElementById("ajax_res").innerHTML = response;
		}
	}
}

// get all models for the brand
function sendGetModels(bId){

	var loadingdiv = document.getElementById('loading');
	loadingdiv.style.display = "block";

	method = 'get';
	url = 'getmodels.php?brandid='+bId;
	http.open(method,url,true);
	http.onreadystatechange = handleSendGetModelsResponse;
	http.send(null);
}

function handleSendGetModelsResponse(){
	if(http.readyState == 4 && http.status == 200)
	{
		var response = http.responseText;
		if(response)
		{
			var loadingdiv = document.getElementById('loading');
			loadingdiv.style.display = "none";

			responseArray = response.split(":");
			var bId = responseArray[0];
			var models = responseArray[1].split(",");
			var str = "";
			for(var i=0;i<models.length;i++)
			{
				str = str + models[i] + "<br>";
			}
			var listdiv = document.getElementById(""+bId);
			listdiv.innerHTML = str;
			listdiv.style.display = "block";
		}
	}
}