var ts_class="tire-selector-method";
var parent	="homepage-tire-selector";

function ts_dl() {
// purpose: adds events to various elements on homepage load and populates dynamic forms with data
	var ts_root=document.getElementById(parent);
	var ts_node;
	for (var i=0; i<ts_root.childNodes.length; i++) {
		ts_node=ts_root.childNodes[i];
		if (ts_node.nodeName.toLowerCase()=="div") {
			for (var x=0; x<ts_node.attributes.length; x++) {
				if (ts_node.attributes[x].nodeName.toLowerCase()=="class") {
					if (ts_node.attributes[x].nodeValue.substring(0,20)==ts_class) {
						// assign 'onclick' to anchors
						for (var j=0; j<ts_node.childNodes.length; j++) {
							if (ts_node.childNodes[j].nodeName.toLowerCase()=="a") {
								eval('ts_node.childNodes[j].onclick=function() {toggle(this.parentNode.id);}');
							}
						}
					}
				}
			}
		}
	}
	
	// add 'onclick' to the DL radio button
	eval('document.getElementById("city-radio").onclick=function() {dl_ex();}');
	
	// add 'onfocus' to the input field
	eval('document.getElementById("dl-f-zip").onfocus=function() {clear_value(this);}');
	eval('document.getElementById("hd-dl-f-zip").onfocus=function() {clear_value(this);}');
	
	// display disabled select boxes and button
	dis_select('make');
	dis_select('model');
	dis_select('year');
	dis_select('option');
	dis_button("by-vehicle-btn");
	
	if (make.length>0 && model.length>0 && year.length>0 && option.length>0) {
		// prepopulate boxes with vehicle data
		show_items_preselected();
	}else{
		// populate the 'make' select box
		show_items('year','years');
	}
	// set empty cookie to clear the dealer search results pagination value
	set_cookie('dlr_page',1);
}

function toggle(id) {
// purpose: changes display property of the 'tire-selector-method' elements
	dl();
	var obj=document.getElementById(id);
	var obj_content=document.getElementById(id+"-content");
	var obj_state=obj.className.substring(obj.className.length-3);
	if(obj_state=="-on") {
		obj.className=obj.className.replace("-on","-off");
		obj_content.style.display="none";
	}else{
		hide_ts_content();
		obj.className=obj.className.replace("-off","-on");
		obj_content.style.display="block";
		if (id=="find-tire-size") {
			// display disabled select boxes and button
			dis_select('tireSizeType');
			dis_select('tireSectionWidth');
			dis_select('tireAspectRatio');
			dis_select('tireRimDiameter');
			dis_button("tire-size-btn");
			// populate the 'tireSectionWidth' select box
			//show_items('tireSectionWidth','widths');
			show_items('tireSizeType','types');
		}
		if (id=="find-by-tire-name") {
			// display disabled select boxes and button
			dis_select('prodId');
			dis_button("tire-name-btn");
			// populate the 'prodId' select box
			show_tire_names('prodId');
		}
	}
}

function hide_ts_content() {
// purpose: hides all open 'tire-selector-method' elements and their corresponding content
	var ts_root=document.getElementById(parent);
	for (var i=0; i<ts_root.childNodes.length; i++) {
		ts_node=ts_root.childNodes[i];
		if (ts_node.nodeName.toLowerCase()=="div") {
			for (var x=0; x<ts_node.attributes.length; x++) {
				if (ts_node.attributes[x].nodeName.toLowerCase()=="class") {
					if (ts_node.attributes[x].nodeValue.substring(0,20)==ts_class && ts_node.attributes[x].nodeValue.substring(ts_node.attributes[x].nodeValue.length-3)=="-on") {
						ts_node.className=ts_node.className.replace("-on","-off");
						document.getElementById(ts_node.id+"-content").style.display="none";
					}
				}
			}
		}
	}
}

function dl_ex() {
// purpose: displays the extended DL search form
	hide_ts_content();
	
	document.getElementById("homepage-dealer-locator").style.display="none";
	document.getElementById("homepage-dealer-locator-extended").style.display="block";
	
	var dl_form=document.getElementById("dl-extended-form-input");
	//assign 'onfocus' to all inputs except 'image' and 'radio' types
	for (var x=0; x<dl_form.childNodes.length; x++) {
		if (dl_form.childNodes[x].nodeName.toLowerCase()=="input") {
			for (var y=0; y<dl_form.childNodes[x].attributes.length; y++) {
				if (dl_form.childNodes[x].attributes[y].nodeName.toLowerCase()=="type") {
					if (dl_form.childNodes[x].attributes[y].nodeValue!="image" && dl_form.childNodes[x].attributes[y].nodeValue!="radio") {
						eval('dl_form.childNodes[x].onfocus=function() {clear_value(this);}');
					}
				}
			}
		}
	}
	// check and add 'onclick' to the DL radio button
	eval('document.getElementById("zip-radio-ex").onclick=function() {toggle("find-by-vehicle");}');
	if (document.getElementById("city-radio-ex")) document.getElementById("city-radio-ex").checked="checked";
}

function dl() {
// purpose: displays the short DL search form
	document.getElementById("homepage-dealer-locator").style.display="block";
	document.getElementById("homepage-dealer-locator-extended").style.display="none";
	
	// add 'onfocus' to the input field
	eval('document.getElementById("dl-f-zip").onfocus=function() {clear_value(this);}');
	
	// check and add 'onclick' to the DL radio button
	if (document.getElementById("zip-radio")) document.getElementById("zip-radio").checked="checked";

}

function clear_hdzip() {
	eval('document.getElementById("hd-dl-f-zip").onfocus=function() {clear_value(this);}');
}