var request=false;
var loading_timer;
var carsXML;
var modelIndex;
var badgeIndex;
var seriesIndex;
var yearIndex;

try
{
	request=new XMLHttpRequest();
	if (request.overrideMimeType)
	{
		request.overrideMimeType('text/xml');
	}
}
catch (trymicrosoft)
{
	try
	{
		request=new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (othermicrosoft)
	{
		try
		{
			request=new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (failed)
		{
			request=false;
		}
	}
}

if(!request)
	alert("Sorry, your browser is not supported!");

function manufacturerChange()
{
	if(document.getElementById('manufacturer_selection')[0].value=='')
		document.getElementById('manufacturer_selection').remove(0);
	
	request.open("GET", scriptLocation+'get_cars.php?manufacturer='+escape(document.getElementById('manufacturer_selection')[document.getElementById('manufacturer_selection').selectedIndex].value), true);
	request.onreadystatechange=getCarReturn;
	request.send(null);
}

function getCarReturn()
{
	try
	{
		if(request.readyState==1)
		{
			hideShowLoader('show');
			document.getElementById('model_selection').setAttribute('disabled', 'disabled');
			document.getElementById('badge_selection').setAttribute('disabled', 'disabled');
			document.getElementById('series_selection').setAttribute('disabled', 'disabled');
			document.getElementById('year_selection').setAttribute('disabled', 'disabled');
		}
		if(request.readyState==4)
		{
			hideShowLoader('hide');
			if(request.status==200)
			{
				carsXML=request.responseXML.documentElement;
				var car_models=carsXML.getElementsByTagName('car');
				
				var modelSelect=document.createElement('select');
				modelSelect.setAttribute('id', 'model_selection');
				modelSelect.className='select_box';
				
				var model=document.createElement('option');
				model.value='%';
				model.innerHTML='Any Model';
				
				modelSelect.appendChild(model);
				
				for(var i=0; i<car_models.length; i++)
				{
					var model=document.createElement('option');
					model.value=car_models[i].getAttribute('model');
					model.innerHTML=car_models[i].getAttribute('model');
					
					modelSelect.appendChild(model);
				}
				
				modelSelect.onchange=modelChange;
				document.getElementById('nav_models').replaceChild(modelSelect, document.getElementById('model_selection'));
				
				var badgeSelect=document.createElement('select');
				badgeSelect.setAttribute('id', 'badge_selection');
				badgeSelect.className='select_box_disabled';
				badgeSelect.setAttribute('disabled', 'disabled');
	
				var badge=document.createElement('option');
				badge.value='%';
				badge.innerHTML='Any Badge';
				
				badgeSelect.appendChild(badge);
				document.getElementById('nav_badges').replaceChild(badgeSelect, document.getElementById('badge_selection'));
				
				var seriesSelect=document.createElement('select');
				seriesSelect.setAttribute('id', 'series_selection');
				seriesSelect.className='select_box_disabled';
				seriesSelect.setAttribute('disabled', 'disabled');
				
				var serie=document.createElement('option');
				serie.value='%';
				serie.innerHTML='Any Series';
				
				seriesSelect.appendChild(serie);
				document.getElementById('nav_series').replaceChild(seriesSelect, document.getElementById('series_selection'));
				
				var yearsSelect=document.createElement('select');
				yearsSelect.setAttribute('id', 'year_selection');
				yearsSelect.className='select_box_disabled';
				yearsSelect.setAttribute('disabled', 'disabled');
				
				var yearOption=document.createElement('option');
				yearOption.value='%';
				yearOption.innerHTML='Any Year';
				
				yearsSelect.appendChild(yearOption);
				document.getElementById('nav_year').replaceChild(yearsSelect, document.getElementById('year_selection'));
				
				if((modelIndex+'').length>0)
				{
					modelSelect.selectedIndex=modelIndex;
					modelIndex='';
					modelChange();
				}
			}
			else if(request.status==404)
				alert("Required chat functions not found on this server!");
			else if(request.status!=0)
				alert("Error: status code is " + request.status);
		}
	}
	catch(noCars)
	{
	
	}
}

function modelChange()
{	
	if(document.getElementById('model_selection')[document.getElementById('model_selection').selectedIndex].value!='%')
	{
		var car_models=carsXML.getElementsByTagName('car');
		for(var i=0; i<car_models.length; i++)
		{
			if(car_models[i].getAttribute('model')==document.getElementById('model_selection')[document.getElementById('model_selection').selectedIndex].value)
			{
				var badges=car_models[i].getElementsByTagName('badge');
			}
		}
		
		var badgeSelect=document.createElement('select');
		badgeSelect.setAttribute('id', 'badge_selection');
		badgeSelect.className='select_box';
		
		var badge=document.createElement('option');
		badge.value='%';
		badge.innerHTML='Any Badge';
		
		badgeSelect.appendChild(badge);
		
		for(var i=0; i<badges.length; i++)
		{
			var badge=document.createElement('option');
			badge.value=badges[i].getAttribute('badge');
			badge.innerHTML=badges[i].getAttribute('badge');
					
			badgeSelect.appendChild(badge);
		}
		
		badgeSelect.onchange=badgeChange;
		document.getElementById('nav_badges').replaceChild(badgeSelect, document.getElementById('badge_selection'));
		
		if((badgeIndex+'').length>0)
		{
			badgeSelect.selectedIndex=badgeIndex;
			badgeIndex='';
			badgeChange();
		}
		else
		{
			var seriesSelect=document.createElement('select');
			seriesSelect.setAttribute('id', 'series_selection');
			seriesSelect.className='select_box_disabled';
			seriesSelect.setAttribute('disabled', 'disabled');
			
			var serie=document.createElement('option');
			serie.value='%';
			serie.innerHTML='Any Series';
			
			seriesSelect.appendChild(serie);
			document.getElementById('nav_series').replaceChild(seriesSelect, document.getElementById('series_selection'));
			
			var yearsSelect=document.createElement('select');
			yearsSelect.setAttribute('id', 'year_selection');
			yearsSelect.className='select_box_disabled';
			yearsSelect.setAttribute('disabled', 'disabled');
			
			var yearOption=document.createElement('option');
			yearOption.value='%';
			yearOption.innerHTML='Any Year';
			
			yearsSelect.appendChild(yearOption);
			document.getElementById('nav_year').replaceChild(yearsSelect, document.getElementById('year_selection'));
		}
	}
	else
	{
		var badgeSelect=document.createElement('select');
		badgeSelect.setAttribute('id', 'badge_selection');
		badgeSelect.className='select_box_disabled';
		badgeSelect.setAttribute('disabled', 'disabled');
		
		var badge=document.createElement('option');
		badge.value='%';
		badge.innerHTML='Any Badge';
		
		badgeSelect.appendChild(badge);
		document.getElementById('nav_badges').replaceChild(badgeSelect, document.getElementById('badge_selection'));
		
		var seriesSelect=document.createElement('select');
		seriesSelect.setAttribute('id', 'series_selection');
		seriesSelect.className='select_box_disabled';
		seriesSelect.setAttribute('disabled', 'disabled');
		
		var serie=document.createElement('option');
		serie.value='%';
		serie.innerHTML='Any Series';
		
		seriesSelect.appendChild(serie);
		document.getElementById('nav_series').replaceChild(seriesSelect, document.getElementById('series_selection'));
		
		var yearsSelect=document.createElement('select');
		yearsSelect.setAttribute('id', 'year_selection');
		yearsSelect.className='select_box_disabled';
		yearsSelect.setAttribute('disabled', 'disabled');
		
		var yearOption=document.createElement('option');
		yearOption.value='%';
		yearOption.innerHTML='Any Year';
		
		yearsSelect.appendChild(yearOption);
		document.getElementById('nav_year').replaceChild(yearsSelect, document.getElementById('year_selection'));
	}
}

function badgeChange()
{
	if(document.getElementById('badge_selection')[document.getElementById('badge_selection').selectedIndex].value!='%')
	{
		var car_models=carsXML.getElementsByTagName('car');
		for(var i=0; i<car_models.length; i++)
		{
			if(car_models[i].getAttribute('model')==document.getElementById('model_selection')[document.getElementById('model_selection').selectedIndex].value)
			{
				var badges=car_models[i].getElementsByTagName('badge');
			}
		}
		
		for(var i=0; i<badges.length; i++)
		{
			if(badges[i].getAttribute('badge')==document.getElementById('badge_selection')[document.getElementById('badge_selection').selectedIndex].value)
			{
				var series=badges[i].getElementsByTagName('series');
			}
		}
		
		var seriesSelect=document.createElement('select');
		seriesSelect.setAttribute('id', 'series_selection');
		seriesSelect.className='select_box';
		
		var serie=document.createElement('option');
		serie.value='%';
		serie.innerHTML='Any Series';
		
		seriesSelect.appendChild(serie);
		
		for(var i=0; i<series.length; i++)
		{
			var serie=document.createElement('option');
			serie.value=series[i].getAttribute('series');
			serie.innerHTML=series[i].getAttribute('series');
			
			seriesSelect.appendChild(serie);
		}
		
		seriesSelect.onchange=seriesChange;
		document.getElementById('nav_series').replaceChild(seriesSelect, document.getElementById('series_selection'));
		
		if((seriesIndex+'').length>0)
		{
			seriesSelect.selectedIndex=seriesIndex;
			seriesIndex='';
			seriesChange();
		}
		else
		{
			var yearsSelect=document.createElement('select');
			yearsSelect.setAttribute('id', 'year_selection');
			yearsSelect.className='select_box_disabled';
			yearsSelect.setAttribute('disabled', 'disabled');
			
			var yearOption=document.createElement('option');
			yearOption.value='%';
			yearOption.innerHTML='Any Year';
			
			yearsSelect.appendChild(yearOption);
			document.getElementById('nav_year').replaceChild(yearsSelect, document.getElementById('year_selection'));
		}
	}
	else
	{
		var seriesSelect=document.createElement('select');
		seriesSelect.setAttribute('id', 'series_selection');
		seriesSelect.className='select_box_disabled';
		seriesSelect.setAttribute('disabled', 'disabled');
		
		var serie=document.createElement('option');
		serie.value='%';
		serie.innerHTML='Any Series';
		
		seriesSelect.appendChild(serie);
		document.getElementById('nav_series').replaceChild(seriesSelect, document.getElementById('series_selection'));
		
		var yearsSelect=document.createElement('select');
		yearsSelect.setAttribute('id', 'year_selection');
		yearsSelect.className='select_box_disabled';
		yearsSelect.setAttribute('disabled', 'disabled');
		
		var yearOption=document.createElement('option');
		yearOption.value='%';
		yearOption.innerHTML='Any Year';
		
		yearsSelect.appendChild(yearOption);
		document.getElementById('nav_year').replaceChild(yearsSelect, document.getElementById('year_selection'));
	}
}

function seriesChange()
{
	if(document.getElementById('series_selection')[document.getElementById('series_selection').selectedIndex].value!='%')
	{
		var car_models=carsXML.getElementsByTagName('car');
		for(var i=0; i<car_models.length; i++)
		{
			if(car_models[i].getAttribute('model')==document.getElementById('model_selection')[document.getElementById('model_selection').selectedIndex].value)
			{
				var badges=car_models[i].getElementsByTagName('badge');
			}
		}
		
		for(var i=0; i<badges.length; i++)
		{
			if(badges[i].getAttribute('badge')==document.getElementById('badge_selection')[document.getElementById('badge_selection').selectedIndex].value)
			{
				var series=badges[i].getElementsByTagName('series');
			}
		}
		
		for(var i=0; i<series.length; i++)
		{
			if(series[i].getAttribute('series')==document.getElementById('series_selection')[document.getElementById('series_selection').selectedIndex].value)
			{
				var years=series[i].getElementsByTagName('year');
			}
		}
		
		var yearsSelect=document.createElement('select');
		yearsSelect.setAttribute('id', 'year_selection');
		yearsSelect.className='select_box';
		
		var yearOption=document.createElement('option');
		yearOption.value='%';
		yearOption.innerHTML='Any Year';
		
		yearsSelect.appendChild(yearOption);
		
		for(var i=0; i<years.length; i++)
		{
			var yearOption=document.createElement('option');
			yearOption.value=years[i].firstChild.nodeValue;
			yearOption.innerHTML=years[i].firstChild.nodeValue;
			
			yearsSelect.appendChild(yearOption);
		}
		
		//yearsSelect.onchange=yearChange;
		document.getElementById('nav_year').replaceChild(yearsSelect, document.getElementById('year_selection'));
		
		if((yearIndex+'').length>0)
		{
			yearsSelect.selectedIndex=yearIndex;
			yearIndex='';
			//seriesChange();
		}
		
	}
	else
	{
		var yearsSelect=document.createElement('select');
		yearsSelect.setAttribute('id', 'year_selection');
		yearsSelect.className='select_box_disabled';
		yearsSelect.setAttribute('disabled', 'disabled');
		
		var yearOption=document.createElement('option');
		yearOption.value='%';
		yearOption.innerHTML='Any Year';
		
		yearsSelect.appendChild(yearOption);
		document.getElementById('nav_year').replaceChild(yearsSelect, document.getElementById('year_selection'));
	}
}

function navigate()
{
	
	var selectedManufacturer=document.getElementById('manufacturer_selection')[document.getElementById('manufacturer_selection').selectedIndex].value;
	var selectedModel=document.getElementById('model_selection')[document.getElementById('model_selection').selectedIndex].value;
	var selectedBadge=document.getElementById('badge_selection')[document.getElementById('badge_selection').selectedIndex].value;
	var selectedSeries=document.getElementById('series_selection')[document.getElementById('series_selection').selectedIndex].value;
	var selectedYear=document.getElementById('year_selection')[document.getElementById('year_selection').selectedIndex].value;
	
	var newURL=urlBase+'car/';
	
	if(selectedManufacturer!='%' && selectedManufacturer!='')
	{
		newURL+=selectedManufacturer;
		if(selectedModel!='%' && selectedModel!='')
		{
			newURL+='_'+selectedModel;
			if(selectedBadge!='%' && selectedBadge!='')
			{
				newURL+='_'+selectedBadge;
				if(selectedSeries!='%' && selectedYear!='%' && selectedSeries!='' && selectedYear!='')
				{
					newURL+='_('+selectedYear+'-'+selectedSeries+')';
				}
				else if((selectedSeries!='%' && selectedSeries!='') && (selectedYear=='%' || selectedYear==''))
				{
					newURL+='_('+selectedSeries+')';
				}
			}
		}
	}
	
	window.location.href=newURL;
}

function hideShowLoader(status)
{
	if(status=='show' && document.getElementById('feedback_div').style.display=='none')
	{
		loading_timer=setTimeout("new Effect.Appear('feedback_div', { duration: 1 , to: 1})", 500);
		document.getElementById('feedback_div').innerHTML = '<img src="'+urlBase+'images/site/loader.gif" alt="" /> Loading...&nbsp;&nbsp;&nbsp;';
	}
	else
	{
		new Effect.Fade('feedback_div', { duration: 0.5 });
		clearTimeout(loading_timer);
	}
}

function showFeedback(text, fadeOut)
{
	document.getElementById('feedback_div').innerHTML = text;
	new Effect.Appear('feedback_div', { duration: 1 , to: 1});
	if(fadeOut)
		setTimeout("new Effect.Fade(\'feedback_div\', { duration: 1 })", 5000);
}