function MM_swapImgRestore() {//v3.0
	var i, x, a = document.MM_sr;
	for( i = 0; a && i < a.length && ( x = a[i]) && x.oSrc; i++)
	x.src = x.oSrc;
}

function MM_preloadImages() {//v3.0
	var d = document;
	if(d.images) {
		if(!d.MM_p)
			d.MM_p = new Array();
		var i, j = d.MM_p.length, a = MM_preloadImages.arguments;
		for( i = 0; i < a.length; i++)
		if(a[i].indexOf("#") != 0) {
			d.MM_p[j] = new Image;
			d.MM_p[j++].src = a[i];
		}
	}
}

function MM_findObj(n, d) {//v4.01
	var p, i, x;
	if(!d)
		d = document;
	if(( p = n.indexOf("?")) > 0 && parent.frames.length) {
		d = parent.frames[n.substring(p + 1)].document;
		n = n.substring(0, p);
	}
	if(!( x = d[n]) && d.all)
		x = d.all[n];
	for( i = 0; !x && i < d.forms.length; i++)
	x = d.forms[i][n];
	for( i = 0; !x && d.layers && i < d.layers.length; i++)
	x = MM_findObj(n, d.layers[i].document);
	if(!x && d.getElementById)
		x = d.getElementById(n);
	return x;
}

function MM_swapImage() {//v3.0
	var i, j = 0, x, a = MM_swapImage.arguments;
	document.MM_sr = new Array;
	for( i = 0; i < (a.length - 2); i += 3)
	if(( x = MM_findObj(a[i])) != null) {
		document.MM_sr[j++] = x;
		if(!x.oSrc)
			x.oSrc = x.src;
		x.src = a[i + 2];
	}
}

function MM_showHideLayers() {//v6.0
	var i, p, v, obj, args = MM_showHideLayers.arguments;
	for( i = 0; i < (args.length - 2); i += 3)
	if(( obj = MM_findObj(args[i])) != null) {
		v = args[i + 2];
		if(obj.style) {
			obj = obj.style;
			v = (v == 'show') ? 'block' : (v == 'hide') ? 'none' : v;
		}
		obj.display = v;
	}
}

/*
 Written by Jonathan Snook, http://www.snook.ca/jonathan
 Add-ons by Robert Nyman, http://www.robertnyman.com
 */

function getElementsByClassName(oElm, strTagName, strClassName) {
	var arrElements = (strTagName == "*" && oElm.all) ? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/\-/g, "\\-");
	var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	var oElement;
	for(var i = 0; i < arrElements.length; i++) {
		oElement = arrElements[i];
		if(oRegExp.test(oElement.className)) {
			arrReturnElements.push(oElement);
		}
	}
	return (arrReturnElements)
}

function submitform() {
	document.searchsite.submit();
}

function locatedealer() {
	document.locatedealer.submit();
}

function validateZips(zip, distance) {
	var reg = /^\d{5}([\-]\d{4})?$/;
	var valid = true;

	//clear old validation errors
	document.getElementById("zipAlert").innerHTML = "";
	document.getElementById("distanceAlert").innerHTML = "";
	document.getElementById("zip").style.background = "#fff";
	document.getElementById("distance").style.background = "#fff";

	if(reg.test(zip) == false || zip == null || zip == '') {
		document.getElementById('zipAlert').innerHTML = "Invalid Zipcode";
		document.getElementById('zip').style.background = "#fac6d2";
		valid = false;
	}

	if(isNaN(distance) == true || distance == 0) {
		document.getElementById('distanceAlert').innerHTML = "Invalid Distance";
		document.getElementById('distance').style.background = "#fac6d2";
		valid = false;
	}

	if(valid == false) {
		return false;
	}
}

function locatormap_redirect(country, provstate) {
	var destination = '/dealers';

	if(country == 'namerica') {
		destination = '/locator/northamerica';
	} else {
		if(country) {
			destination = destination + '/' + country;
		}
		if(provstate != 0) {
			destination = destination + '/' + provstate;
		}
	}
	location.href = destination;
}

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function checkDealerType() {
	var onlineRetailer = document.getElementById('online_retailer').checked;
	var distributor = document.getElementById('distributor').checked;
	var dealer = document.getElementById('dealer').checked;
	var countrybox = document.locatedealer.opt_country;
	var country = countrybox.options[countrybox.selectedIndex].value;
	var provstate = document.locatedealer.opt_provstate;
	if(provstate) {
		var selectedState = provstate.options[provstate.selectedIndex].value;
	}
	var city = document.locatedealer.opt_city;

	//update country drop down menu
	if(window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp = new XMLHttpRequest();
	} else {// code for IE6, IE5
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			var countryResponseText = xmlhttp.responseText;
			document.getElementById("countrybox").innerHTML = countryResponseText;
		}
	}
	xmlhttp.open("GET", "locator_country.php?onlineRetailer=" + onlineRetailer + "&distributor=" + distributor + "&dealer=" + dealer + "&country=" + country, true);
	xmlhttp.send();

	//update state drop down menu
	if(provstate) {
		if(window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttpState = new XMLHttpRequest();
		} else {// code for IE6, IE5
			xmlhttpState = new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttpState.onreadystatechange = function() {
			if(xmlhttpState.readyState == 4 && xmlhttpState.status == 200) {
				var stateResponseText = xmlhttpState.responseText;
				document.getElementById("statebox").innerHTML = stateResponseText;
				//remove city dropdown box if state dropdown is reset
				var newSelectedState = document.locatedealer.opt_provstate;
				var selectedStateChanged = newSelectedState.options[newSelectedState.selectedIndex].value;
				if(selectedStateChanged == 0) {
					document.getElementById("citybox").innerHTML = "";
				}
			}
		}
		xmlhttpState.open("GET", "locator_states.php?onlineRetailer=" + onlineRetailer + "&distributor=" + distributor + "&dealer=" + dealer + "&country=" + country + "&provstate=" + selectedState, true);
		xmlhttpState.send();
	}
	//update city drop down menu
	if(city) {
		update_provstate(selectedState);
	}
}

function update_country(onlineRetailer, distributor, dealer) {
	var countrybox = document.locatedealer.opt_country;
	var country = countrybox.options[countrybox.selectedIndex].value;
	var destination = '/locator';
	//reset zipcode display for switching from canada to usa
	document.getElementById('zipcodebox').style.display = "block";

	if(country) {
		if((country == 'USA' || country == 'Canada') && (onlineRetailer == false || (distributor == true || dealer == true))) {
			document.getElementById('citystatezipbox').style.display = "block";
			if(country == 'Canada') {
				document.getElementById('zipcodebox').style.display = "none";
			}

			if(window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
				xmlhttp = new XMLHttpRequest();
			} else {// code for IE6, IE5
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			xmlhttp.onreadystatechange = function() {
				if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
					document.getElementById("statedropdown").innerHTML = xmlhttp.responseText;
				}
			}
			xmlhttp.open("GET", "locator_states.php?country=" + country, true);
			xmlhttp.send();
		} else {
			document.locatedealer.submit();
		}
	}
}

function update_provstate(provstate) {
	var countrybox = document.locatedealer.opt_country;
	var country = countrybox.options[countrybox.selectedIndex].value;
	var onlineRetailer = document.getElementById('online_retailer').checked;
	var distributor = document.getElementById('distributor').checked;
	var dealer = document.getElementById('dealer').checked;
	
	if (provstate == 'USA') {
		document.locatedealer.submit();
	}
	
	if(country == 'Canada' && provstate) {
		document.locatedealer.submit();
	} else if(country) {
		if(window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttpCity = new XMLHttpRequest();
		} else {// code for IE6, IE5
			xmlhttpCity = new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttpCity.onreadystatechange = function() {
			if(xmlhttpCity.readyState == 4 && xmlhttpCity.status == 200) {
				document.getElementById("citybox").innerHTML = xmlhttpCity.responseText;
			}
		}
		xmlhttpCity.open("GET", "locator_city.php?onlineRetailer=" + onlineRetailer + "&distributor=" + distributor + "&dealer=" + dealer + "&provstate=" + provstate, true);
		xmlhttpCity.send();
	}
}

function update_city() {
	var citybox = document.locatedealer.opt_city;
	var city = citybox.options[citybox.selectedIndex].value;

	if(city) {
		document.locatedealer.submit();
	}
}

function popup(url, x, y) {
	window.open(url, 'popup', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=' + x + ',height=' + y + ',left=0,top=0');
}

function clickProductMenuButton(a, b) {
	var obja, objb;
	obja = MM_findObj(a);
	objb = MM_findObj(b);

	if(a == 'category_btn_div') {
		obja.className = 'productmenu_option_button_left_b';
		objb.className = 'productmenu_option_button_right';
	} else if(a == 'application_btn_div') {
		obja.className = 'productmenu_option_button_right_b';
		objb.className = 'productmenu_option_button_left';
	}
}

function initPanelCells() {
	var cellid = document.getElementById("pline_products");
	if(cellid) {
		var cellgroup = cellid.getElementsByTagName("td");
		for( i = 0; i < cellgroup.length; i++) {
			var link = cellgroup[i].getElementsByTagName("a")
			if(link.length == 1) {
				cellgroup[i].onclick = clickCell;
				cellgroup[i].onmouseover = mouseoverCell;
				cellgroup[i].onmouseout = mouseoutCell;
			}
		}
	}

	var cellid = document.getElementById("subcategory_panels");
	if(cellid) {
		var cellgroup = cellid.getElementsByTagName("td");
		for( i = 0; i < cellgroup.length; i++) {
			var link = cellgroup[i].getElementsByTagName("a")
			if(link.length == 1) {
				cellgroup[i].onclick = clickCell;
				cellgroup[i].onmouseover = mouseoverCell;
				cellgroup[i].onmouseout = mouseoutCell;
			}
		}
	}
}

function clickCell() {
	var link = this.getElementsByTagName("a");
	window.location = link[0].href;
}

function mouseoverCell() {
	this.className = "highlight";
	if(this.getAttribute('rollover') != '') {
		MM_swapImage('subcat_rollover', '', this.getAttribute('rollover'), 1);
		//	if(this.getAttribute('title')!='') {
		//		MM_swapImage('subcat_rollover','',this.getAttribute('title'),1);

	}
}

function mouseoutCell() {
	this.className = "panel";
	MM_swapImgRestore();
}

function initProductTabs() {
	var tabgroup = document.getElementById("product_tabwrapper");
	if(tabgroup) {
		var tabs = tabgroup.getElementsByTagName("div");
		for( i = 0; i < tabs.length; i++) {
			var link = tabs[i].getElementsByTagName("a")
			if(link.length == 1) {
				tabs[i].onclick = clickTab;
				tabs[i].onmouseover = mouseoverTab;
				tabs[i].onmouseout = mouseoutTab;
			}
		}
	}
}

function clickTab() {
	var link = this.getElementsByTagName("a");
	window.location = link[0].href;
}

function mouseoverTab() {
	var link = this.getElementsByTagName("a");
	link.className = "selected";
	this.style.cursor = "pointer";
}

function mouseoutTab() {
	var link = this.getElementsByTagName("a");
	link.className = "";
	this.style.cursor = "default";
}

function productTabRollover(url) {
	window.location = url;
}

function show_related(pos) {
	var sections = getElementsByClassName(document, "*", "related_products_section");
	var sectiondivs;
	var sectionlinks;
	for( i = 0; i < sections.length; i++) {
		sectiondivs = sections[i].getElementsByTagName("div");
		if(i == pos) {
			sectiondivs[0].style.backgroundImage == "url(images/product_downarrow.gif)"

			for( x = 1; x < sectiondivs.length; x++) {
				sectiondivs[x].style.position = "relative";
				sectiondivs[x].style.visibility = "visible";
			}
		} else {
			sectiondivs[0].style.backgroundImage = "url(images/slideshow_rightarrow.gif)";
			for( x = 1; x < sectiondivs.length; x++) {
				sectiondivs[x].style.position = "absolute";
				sectiondivs[x].style.visibility = "hidden";
			}
		}
	}
}

function getAbsPos(oId, tl) {
	var o = ( typeof oId == 'String' ) ? document.getElementById(oId) : oId;
	var val = 0;
	while(o.nodeName != "BODY") {
		val += parseInt((tl == 'top' ) ? o.offsetTop : o.offsetLeft);
		o = o.parentNode;
	}
	return val;
}

function move_lineup_left() {
	var lineup = document.getElementById("image_lineup");
	var pos_left = lineup.offsetLeft;
	if(pos_left < 0) {
		pos_left += 42;
		lineup.style.left = pos_left + "px";
	}
}

function move_lineup_right(maxitems) {
	var max_left = 0 - ((maxitems - 4) * 42)
	var lineup = document.getElementById("image_lineup");
	var pos_left = lineup.offsetLeft;
	if(pos_left > max_left) {
		pos_left -= 42;
		lineup.style.left = pos_left + "px";
	}
}

function show_photo(displaypath, originalpath) {
	var imageobj = document.getElementById("image_display_box");
	var imagelink = document.getElementById("image_display_link");
	// alert(displaypath+' and '+originalpath);
	imageobj.src = displaypath;
	imagelink.href = originalpath;
}

function validateDealerCreate(form_id, email) {

	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	var address = document.forms[form_id].elements[email].value;
	var username = document.forms[form_id].elements["desired_username"].value;
	var password1 = document.forms[form_id].elements["password1"].value;
	var password2 = document.forms[form_id].elements["password2"].value;
	var valid = true;
	//clear old validation errors
	document.getElementById("usernameAlert").innerHTML = "";
	document.getElementById("passwordAlert").innerHTML = "";
	document.getElementById("emailAlert").innerHTML = "";

	if(username == null || username == "") {
		document.getElementById("usernameAlert").innerHTML = "Must Enter Username";
		valid = false;
	}

	if(password1 == "" || [password2] == "") {
		document.getElementById("passwordAlert").innerHTML = 'Must Enter Password';
		valid = false;
	}
	if(reg.test(address) == false) {
		document.getElementById("emailAlert").innerHTML = 'Invalid Email Address';
		valid = false;
	}

	if(valid == false) {
		return false;
	}

}

