function getHTTPObject(){
	var xmlhttp;
	// Attempt to initialize xmlhttp object
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
    catch (e) {
		// Try to use different activex object
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (E) {
			xmlhttp = false;
		}
	}
	// If not initialized, create XMLHttpRequest object
	if (!xmlhttp) {
		if (typeof XMLHttpRequest != 'undefined') {
			xmlhttp = new XMLHttpRequest();
		}
		else {
			alert("This browser does not support AJAX.");
			return null;
		}
	}
	return xmlhttp;
}
 

// Change the value of the outputText field
function arata_orase(){
	var combo = document.getElementById('localitate');
	combo.options.length = 0;
	if(httpObject.readyState == 4){
		var response = httpObject.responseText;
		var items = response.split(";");
		var count = items.length;
		for (var i=0;i<count;i++) {
			if (items[i] == '') continue;
			var options = items[i].split("-");
			if (options[1] == 'false')
				combo.options[i] =	new Option(options[0], options[0]);
			else
				combo.options[i] =	new Option(options[0], options[0], options[1]);
		}
	}
	else 
		combo.options[0] = new Option('asteptati...');
}

// Implement business logic
function orase(){
	if (document.getElementById('judet').value == '') return false;
	
	httpObject = getHTTPObject();
	if (httpObject != null) {
		httpObject.open("GET", '/localitati/arata/'+document.getElementById('judet').value, true);
		httpObject.onreadystatechange = arata_orase;
		httpObject.send(null);
	}
}

function arata_modele(){
	var combo = document.getElementById('model');
	combo.options.length = 0;
	if(httpObject.readyState == 4){
		var response = httpObject.responseText;
		var items = response.split(";");
		var count = items.length;
		if (count == 1) 
			combo.options[0] = new Option('Fara model', 0);
		else {
			for (var i = 0; i < count; i++) {
				if (items[i] == '') 
					continue;
				var options = items[i].split("-");
				combo.options[i] = new Option(options[1], options[0]);
			}
		}
	}
	else 
		combo.options[0] = new Option('asteptati...');
}

function schimba_modele(marca){
	httpObject = getHTTPObject();
	if (httpObject != null) {
		httpObject.open("GET", '/localitati/schimba_modele/'+marca, true);
		httpObject.onreadystatechange = arata_modele;
		httpObject.send(null);
	}
}
