var OptionsList = new Object();
OptionsList.selectedChannel = "mbk";
OptionsList.responseCallBack = function(owner)
{	
	if(Ajax.CheckReadyState(owner.request))
	{
		var xmlDoc = owner.request.responseXML;
		if(xmlDoc) 
		{
			OptionsList.Xml2Options(xmlDoc, owner.selectId, owner.selectedValue, owner.groupedValue);
		}		
	}
}
OptionsList.setMake = function(makeId, modelId, channelId, makeValue)
{
	var owner = new Object();
	owner.selectId = makeId;
	owner.selectedValue = makeValue;
	// reset
	resetSelect(modelId);
	resetSelect(makeId);

	if(!isNull(channelId) && getValue(channelId) != '' && getValue(channelId) != 0)
		this.selectedChannel = getValue(channelId);

	var url="xml/it.mbk.xml";
	Ajax.Request(url, this.responseCallBack, owner);
}

OptionsList.setModel = function(makeId, modelId, makeValue, modelValue, bGroup)
{
	var selectedMake = makeValue;
	if (isNull(selectedMake)) selectedMake = getValue(makeId);			
	if(selectedMake != '' && selectedMake != 0)
	{
		var owner = new Object();
		owner.selectId = modelId;
		owner.selectedValue = modelValue;
		owner.groupedValue = bGroup;
		selectedMake = zeroPad(selectedMake, 3);		
		var url="xml/it.mbk"+selectedMake+".xml";
		Ajax.Request(url, this.responseCallBack, owner);		
	}
	else resetSelect(modelId);
}
OptionsList.SetDes= function(){
	var obj = document.getElementById('Make');
	var obj2 = document.getElementById('Model');
	var obj3 = document.getElementById('Type');
	document.getElementById('MakeDes').value = obj.options[obj.selectedIndex].text;
	if(obj2.length!=0){
		document.getElementById('ModelDes').value = obj2.options[obj2.selectedIndex].text;
	}
	if(obj3!=undefined){
		if(obj3.length!=0){
			document.getElementById('TypeDes').value = obj3.options[obj3.selectedIndex].text;
		}
	}
}
OptionsList.SetDesSearch= function(name){
	var obj = document.getElementById(name);
	document.getElementById(name+'Des').value = obj.options[obj.selectedIndex].text;
	
}
OptionsList.SetDesRegion= function(name1,name2,name3,name4){
	
	document.getElementById(name4).value=0;
	var obj = document.getElementById(name1);
	var obj2 = document.getElementById(name2);
	
	document.getElementById(name3).value = obj.options[obj.selectedIndex].text;
	if(obj2.length!=0){
		document.getElementById(name4).value = obj2.options[obj2.selectedIndex].text;
	}
	
}
OptionsList.SetDesCountry= function(name1,name2,name3,name4,name5,name6){
	
	document.getElementById(name3).value=0;
	document.getElementById(name4).value=0;
	var obj = document.getElementById(name1);//regioneid
	var obj2 = document.getElementById(name2);//provinciaid
	var obj3 = document.getElementById(name5);//statoid
	
	document.getElementById(name6).value = obj3.options[obj3.selectedIndex].text;
	if(obj2.length!=0){
		document.getElementById(name4).value = obj2.options[obj2.selectedIndex].text;
	}
	if(obj.length!=0){
		document.getElementById(name3).value = obj.options[obj.selectedIndex].text;
	}
	
}
OptionsList.check = function(){
	if(document.getElementById('Model').selectedIndex != 0 && document.getElementById('Make').selectedIndex != 0){
		document.getElementById('search').submit();
	}else{
		alert("Seleziona una marca e un modello per proseguire con la ricerca.");
	}
}
OptionsList.setCountry = function(regionId, cityId, countryId, countryValue, regionValue, cityValue, loader)
{
	
	
		
	// reset
	
	
	
		var owner = new Object();
		owner.selectId = countryId;
		owner.selectedValue = countryValue;
		var url="xml/it.country.xml";
		
		Ajax.Request(url, this.responseCallBack, owner, loader);
	
}
OptionsList.setRegion = function(regionId, cityId, countryId, countryValue, regionValue,loader)
{
	var selectedCountry =  countryValue;		// default country
	if(isNull(countryValue)) {  selectedCountry = 1;}
	/*if(!isNull(countryId)) selectedCountry = getValue(countryId);
	if(!isNull(countryValue)) {  selectedCountry = countryValue;}*/
		
	// reset
	resetSelect(regionId);
	resetSelect(cityId);
	
	if(selectedCountry != '' && selectedCountry != 0)
	{
		var owner = new Object();
		owner.selectId = regionId;
		owner.selectedValue = regionValue;
	
		selectedCountry = zeroPad(selectedCountry, 3);
		var url="xml/it.region"+selectedCountry+".xml";
		
		Ajax.Request(url, this.responseCallBack, owner, loader);
	}
}

OptionsList.setCity = function(regionId, cityId, regionValue, cityValue)
{
	var selectedRegion = regionValue;
	if (isNull(selectedRegion)) selectedRegion = getValue(regionId);
	
	resetSelect(cityId);
		
	if(selectedRegion != '' && selectedRegion != 0)
	{
		var owner = new Object();
		owner.selectId = cityId;
		owner.selectedValue = cityValue;
	
		selectedRegion = zeroPad(selectedRegion, 3);
		var url="xml/it.city"+selectedRegion+".xml";
		Ajax.Request(url, this.responseCallBack, owner);
	}
}
OptionsList.Xml2Options = function(xmlDoc, selectId, opValue, bGroup)
{
	var nodes = xmlDoc.documentElement.childNodes;
	
	var item = null;
	var subItem = null;
	var option = null;
	var id = 0;
	var data = '';
	bGroup = (isNull(bGroup))?true:bGroup;
	// reset
	resetSelect(selectId);
	
	for (var i = 0; i < nodes.length; i++) 
	{
		item = nodes.item(i);
		if (item.nodeType != 1) continue;
				
		id = item.getAttribute('id');
		data = item.firstChild.data;
	
		if(data == '') continue;
		
		if(item.nodeName == 'group') 
		{
			// add group option
			if(bGroup)
			{
				option = new Option(data, id);
				option.className = "group";
				$(selectId).options[$(selectId).options.length] = option;
				if(id == opValue) option.selected = true;
			}
			
			for(var x=0; x<item.childNodes.length; x++)
			{
				subItem = item.childNodes[x];
				if (subItem.nodeType != 1) continue;
				
				id = subItem.getAttribute('id');
				data = subItem.firstChild.nodeValue;
				
				// add child option
				option = new Option(data, id);
				option.className = "subitem";
				$(selectId).options[$(selectId).options.length] = option;
				if(id == opValue) option.selected = true;
			}
			
			// go to next element
			continue;
		}
		
		// add item option
		option = new Option(data, id);
		
		document.getElementById(selectId).options[document.getElementById(selectId).options.length] = option;
		if(id == opValue) option.selected = true;
	}
}