var WaitForModels=0;
var CategoryId= 130;
try {
	CategoryId=document.getElementById("categoryid").value;
}catch(e){;}

var currentMarka="";
var currentModel="";
var reloadModel=0;
function GetMarki()
{
	CategoryId=document.getElementById("categoryid").value;
	
	if (WaitForModels==0)
	{
		WaitForModels=1;
		
		if (window.XMLHttpRequest) { 
			http_request = new XMLHttpRequest();
			if (http_request.overrideMimeType) {
				http_request.overrideMimeType('text/xml');
			}
		} else if (window.ActiveXObject) { 
			try {
				http_request = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
					http_request = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {}
			}
		}
		if (!http_request) {
			alert('Error');
			return false;
		}
		http_request.onreadystatechange = function() { alertGetMarki(http_request); };
		url="/lista-modeli.asp?categoryId="+CategoryId+"&out=marki";
		http_request.open('GET', url, true);
		http_request.send(null);
	}
}

function GetModels()
{
	var marka=document.getElementById("marka").value;
	var CategoryId=document.getElementById("CategoryId").value;
	currentMarka=marka;
	if (marka!='' && WaitForModels==0)
	{
		WaitForModels=1;
		
		if (window.XMLHttpRequest) { 
			http_request = new XMLHttpRequest();
			if (http_request.overrideMimeType) {
				http_request.overrideMimeType('text/xml');
			}
		} else if (window.ActiveXObject) { 
			try {
				http_request = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
					http_request = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {}
			}
		}
		if (!http_request) {
			alert('Error');
			return false;
		}
		http_request.onreadystatechange = function() { alertGetModelsNewSelect(http_request); };
		url="/lista-modeli.asp?categoryId="+CategoryId+"&marka="+marka;
		http_request.open('GET', url, true);
		http_request.send(null);
	}
}

function alertGetMarki(http_request) {
    if (http_request.readyState == 4) {
        if (http_request.status == 200) {
			var j=0;
            var out = http_request.responseText;
			tmp=out.split(";");
			tmp.sort();

			for (i=1;i<tmp.length;i++)
			{	
				if (tmp[i]!=''){
					j++;
					document.getElementById('marka').options[j]=new Option(tmp[i], tmp[i]);	
					if (currentMarka==tmp[i])
					{
						document.getElementById('marka').options[j].selected=true;
					}
				}
			}
			reloadModel=1;
			WaitForModels=0;
						
			if (reloadModel==1)
			{	
				GetModels();
				reloadModel=0;
			}
			
        } else {
            alert('Wystąpił problem z zapytaniem.');
        }
    }
}


function alertGetModelsNewSelect(http_request) {
    if (http_request.readyState == 4) {
        if (http_request.status == 200) {
			var j=0;
            var out = http_request.responseText;
			if (out.indexOf('<end>')>0){
				out=out.substr(0,out.indexOf('<end>')-1);
			}
			tmp=out.split(";");
			tmp.sort();
			try{
				var modelsarray  = new Array();
				modelsarray[modelsarray.length] = new Array("Wszystkie","",1,"model1");
				for (i=1;i<tmp.length;i++)
				{	
					if (tmp[i]!=''){
						j++;
						var aselected = 0;
						if (currentModel==tmp[i]) aselected = 1;
						modelsarray[modelsarray.length] = new Array(tmp[i],tmp[i],aselected,"modelxx01");
					}
				}
				try{
					selarraymodelxx01 = modelsarray;
				}catch(e){;}
				fillSelect(modelsarray,"modelxx01");
				setSize("modelxx01");
				WaitForModels=0;
			}catch(e){;}
        } else {
            alert('Wystąpił problem z zapytaniem.');
        }
    }	
}
function setModelsAfterBack(){
	try{
		var aMarka = document.getElementById('marka').value;
		var aModel = document.getElementById('modelxx01').value;
		if (aModel.length > 0) currentModel = aModel;
		if (aMarka.length > 0) GetModels();
	}catch(e){;}
}

