var marka;
var tipusok;
var cars;
var optionTest = true;

function AutoSearchInit() {	
	var agt=navigator.userAgent.toLowerCase();
    this.major = parseInt(navigator.appVersion);
    this.ie     = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
    this.ie3    = (this.ie && (this.major < 4));
    this.ie4    = (this.ie && (this.major == 4) && (agt.indexOf("msie 4")!=-1) );
	this.iemac  = (this.ie && (agt.indexOf("mac")!=-1));
	
	if(this.iemac || ie3 || ie4) return;
	
	if (optionTest)
	{
		marka = document.getElementById('marka');
		tipusok = document.getElementById('tipus');
		
		marka.onchange =  function (){ get_cars(this.value,''); };
		get_cars(marka.value,'');
	}
}


function processReqChange() 
{
	if (req.readyState == 4) {
		if (req.status == 200) {
			var response  = req.responseXML.documentElement;
			
			cars = response.getElementsByTagName('auto');
			list_cars(tipusok);

		} else {
			alert("There was a problem retrieving the XML data:\n" + req.statusText);
		}
	}
}
function get_cars(auto_marka,response)
{
	if (response != ''){ 
		message   = document.getElementById('nameCheckFailed');
		if (response == '1'){
			message.className = 'error';
		}else{
			message.className = 'hidden';
		} 
	}else{
		url  = 'xml/get_cars.php?marka=' + auto_marka;
		loadXMLDoc(url);
	}
}
function loadXMLDoc(url) {
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = function(){processReqChange();};
        req.open("GET", url, true);
        req.send(null);
    } else if (window.ActiveXObject) {
        isIE = true;
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = function(){processReqChange();};
            req.open("GET", url, true);
            req.send();
        }
    }
}


function list_cars(selectfield)
{
	selectfield.options.length = 1;
	ir = new Array();
	for (var i=0; i < cars.length; i++) {
		selectfield.options[i+1] = new Option(cars[i].firstChild.firstChild.nodeValue,cars[i].firstChild.nextSibling.firstChild.nodeValue);
	}
}

