// Vars for storing the tyre search objects
var ts_tyreSearch = null;
var ts_measurement = null;

// Pre-Load the images so there isn't a wait.
var ts_images = new Array();

ts_images['default'] = new Image();
ts_images['default'].src = '/images/tyre_search_bg.gif';
ts_images['section_width'] = new Image();
ts_images['section_width'].src = '/images/tyre_search_bg_section_width.gif';
ts_images['aspect_ratio'] = new Image();
ts_images['aspect_ratio'].src = '/images/tyre_search_bg_aspect_ratio.gif';
ts_images['speed_rating'] = new Image();
ts_images['speed_rating'].src = '/images/tyre_search_bg_speed_rating.gif';
ts_images['rim_diameter'] = new Image();
ts_images['rim_diameter'].src = '/images/tyre_search_bg_rim_diameter.gif';

var ts_text = new Array();
ts_text['default'] = '&nbsp;&nbsp;';
ts_text['section_width'] = 'Section Width';
ts_text['aspect_ratio'] = 'Aspect Ratio';
ts_text['speed_rating'] = 'Speed Rating';
ts_text['rim_diameter'] = 'Rim Diameter';


function tyreSearch_init()
{
	ts_tyreSearch = document.getElementById('tyre_search');
	ts_measurement = document.getElementById('tyre_search_measurement');
	
	if(!window.location.search.substring(1)) {
		ts_tyreSearch.section_width.value = 0;
		ts_tyreSearch.aspect_ratio.disabled = true;
		ts_tyreSearch.aspect_ratio.value = 0;
		ts_tyreSearch.speed_rating.disabled = true;
		ts_tyreSearch.speed_rating.value = 0;
		ts_tyreSearch.rim_diameter.disabled = true;
		ts_tyreSearch.rim_diameter.value = 0;
		
		tyreSearch_updateBg(ts_tyreSearch.section_width);
	}
}

function tyreSearch_updateBg(obj) {
	if(obj == null) { var key = 'default'; }
	else if(!ts_images[obj.id]) { return false; }
	else { var key = obj.id; }

	ts_tyreSearch.style.backgroundImage = 'url(' + ts_images[key].src + ')';
	ts_measurement.innerHTML = ts_text[key];
}


function tyreSearch_changed(obj) {
	switch(obj.id) {
		case 'section_width':
			if(ts_tyreSearch.section_width.value != 0) {
				eval(getAspectRatios(ts_tyreSearch.section_width.value, {type: 'r'}));
				ts_tyreSearch.aspect_ratio.disabled = false;
				ts_tyreSearch.aspect_ratio.focus();
			} else {
				ts_tyreSearch.aspect_ratio.value = 0;
				ts_tyreSearch.aspect_ratio.disabled = true;
			}	
			ts_tyreSearch.speed_rating.value = 0;
			ts_tyreSearch.speed_rating.disabled = true;
			ts_tyreSearch.rim_diameter.value = 0;
			ts_tyreSearch.rim_diameter.disabled = true;
		break;
		
		case 'aspect_ratio':
			if(ts_tyreSearch.aspect_ratio.value != 0) {
				eval(getRimDiameters(ts_tyreSearch.section_width.value, ts_tyreSearch.aspect_ratio.value, {type: 'r'}));
				ts_tyreSearch.rim_diameter.disabled = false;
				ts_tyreSearch.rim_diameter.focus();
			} else {
				ts_tyreSearch.rim_diameter.value = 0;
				ts_tyreSearch.rim_diameter.disabled = true;
			}
			ts_tyreSearch.speed_rating.value = 0;
			ts_tyreSearch.speed_rating.disabled = true;
		break;
		
		case 'rim_diameter':
			if(ts_tyreSearch.rim_diameter.value != 0) {
				eval(getSpeedRatings(ts_tyreSearch.section_width.value, ts_tyreSearch.aspect_ratio.value, ts_tyreSearch.rim_diameter.value, {type: 'r'}));
				ts_tyreSearch.speed_rating.disabled = false;
				ts_tyreSearch.speed_rating.focus();
			} else {
				ts_tyreSearch.speed_rating.value = 0;
				ts_tyreSearch.speed_rating.disabled = true;
			}
		break;
		
		case 'speed_rating':
			if(ts_tyreSearch.speed_rating.value != 0) {
				tyreSearch_updateBg(null);
			}
		break;
	}
}