﻿//dirtdevil.com
String.prototype.trim = function () {
	return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

String.prototype.format = function () {
	var s = this,
        i = arguments.length;

	while (i--) {
		s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]);
	}
	return s;
}

//show or hide panels based on tabs
function activatePanel(clicked) {
	var tabs = new Array("description", "parts", "fits", "features", "manuals", "reviews", "products", "website", "manuals");
	var hash = (clicked != undefined ? clicked.replace("tab_", "") : (window.location.hash != "" ? window.location.hash.replace("#", "") : tabs[0]));
	hash = hash.substring(hash.indexOf("#"), hash.length).replace("#", "");

	for (var i = 0; i < tabs.length; i++) {
		var panel = document.getElementById("panel_" + tabs[i]);
		var tab = document.getElementById("tab_" + tabs[i]);

		if (panel != null && tab != null) {
			panel.style.display = (hash != tabs[i] ? "none" : "");
			tab.className = (hash != tabs[i] ? "image_text" : "image_text tab_" + tabs[i] + "_active");
		}
	}

	//prevent hash from displaying in url
	return false;
}

//allow only one radio button checked when nested in repeater
function distinctRadio(nameregex, current) {
	re = new RegExp(nameregex);

	for (i = 0; i < document.forms[1].elements.length; i++) {
		elm = document.forms[1].elements[i]

		if (elm.type == 'radio') {
			if (re.test(elm.name)) {
				elm.checked = false;
			}
		}
	}
	current.checked = true;
}

//main site search validation
function siteSearch() {
	return document.getElementById("q").value.replace(/ /g, "") != "" ? true : false;
}

//set search text box and select
function setSearch(searchTerm, searchType) {
	if (searchTerm != "") {
		document.getElementById("q").value = searchTerm;
	}

	if (searchType != "") {
		document.getElementById("t").value = searchType;
	}
}

//set compare item
function setCompare(chk) {
	var xmlhttp = new XMLHttpRequest();
	var url = "/products/compare/?action={0}&itemid={1}&timestamp={2}".format((chk.checked ? "add" : "remove"), chk.value, new Date().getTime());

	with (xmlhttp) {
		open("GET", url, true);
		send(null);
		onreadystatechange = function () {
			if (xmlhttp.readyState == 4) {
				if (xmlhttp.responseText != "") {
					chk.checked = false;
					if (confirm(xmlhttp.responseText)) {
						window.location = "/products/compare/";
					};
				}
			}
		}
	}
}

//update item quantity
function updateQuantity(index, qty) {
	var a = document.getElementById("update" + index);
	var href = a.href.substr(0, a.href.indexOf("&quantity="));
	a.href = href + "&quantity=" + qty;
}

function collapse(sender) {
	sender.className = sender.className.indexOf("collapsible") >= 0 ? sender.className.replace("collapsible", "expandable") : sender.className.replace("expandable", "collapsible");

	//change text
	var a = sender.getElementsByTagName("a");
	//a[0].innerHTML = a[0].innerHTML == "Collapse" ? "Expand" : "Collapse";

	//change icon
	var icon = sender.getElementsByTagName("span");
	icon[0].className = icon[0].className == "icon collapse image_text" ? "icon expand image_text" : "icon collapse image_text";

	return false;
}

//update image index on image gallery
function updateImage(intIndex) {
	if (index >= 0 && intIndex < count) {
		with (document.getElementById("imgLarge")) {
			src = document.getElementsByName("aGalleryThumb")[intIndex].href;
			width = images[intIndex][0];
			height = images[intIndex][1];
			alt = images[intIndex][2];
		}

		var a = document.getElementsByName("aGalleryThumb")
		for (var i = 0; i < a.length; i++) {
			a[i].className = (i == intIndex) ? "selected" : "";
		}

		//move nav list
		if (count > 4) {
			var ul = document.getElementById("gallery_thumbnails");

			if (intIndex < 3) {
				ul.style.top = 0;
			} else if (intIndex >= (count - 1) || index == (count - 1)) {
				//don't move
			} else if (intIndex > index) {
				ul.style.top = (-85 * (intIndex - 2)) + "px";
			} else if (intIndex < index) {
				ul.style.top = (-85 * (intIndex - 1)) + "px";
			}
			/*
			if ((intIndex < 3 && intIndex % 3 == 0) || (intIndex >= 3)) {
			document.getElementById("gallery_thumbnails").style.top = (-170) + "px";
			}
			*/
		}

		//set new index
		index = intIndex;

		//dirt devil doesn't have a counter
		//document.getElementById("counter").innerHTML = index + 1;

		//set button state
		var prev = document.getElementById("prev").getElementsByTagName("a")[0];
		var next = document.getElementById("next").getElementsByTagName("a")[0];
		if (intIndex <= 0) {
			prev.className += " prev_disabled";
			next.className = next.className.replace(" next_disabled", "");
		} else if (intIndex + 1 >= count) {
			next.className += " next_disabled";
			prev.className = prev.className.replace(" prev_disabled", "");
		} else {
			prev.className = prev.className.replace(" prev_disabled", "");
			next.className = next.className.replace(" next_disabled", "");
		}
	}

	return false;
}

//bubble
function createBubble(sender, content) {
	if (document.getElementById("bubble") == null) {
		var container = document.createElement("div"); //need separate container for css3 pie effects to work
		container.id = "bubble_container";
		container.style.position = "absolute";
		var bubble = document.createElement("div");
		bubble.id = "bubble";
		bubble.className = "bubble rounded_full"
		bubble.innerHTML = document.getElementById(content).innerHTML;

		//exit if no savings
		if (bubble.innerHTML.trim() == "") {
			return;
		}

		container.appendChild(bubble);

		//document.body.appendChild(bubble);
		sender.parentNode.insertBefore(container, sender.parentNode.firstChild);

		container.style.left = 0;
		container.style.top = -bubble.offsetHeight - 10 + "px";
	} else {
		//document.body.removeChild(document.getElementById("bubble"));
		sender.parentNode.removeChild(document.getElementById("bubble_container"));
	}
}

function swapCheckBox(sender) {
	var c = document.getElementById("partSearchList").getElementsByTagName("input");

	if (sender.value == "All") {
		for (var i = 0; i < c.length; i++) {
			if (c[i].value != sender.value) {
				c[i].checked = false;
			}
		}

		sender.checked = true;
	} else {
		var checked = false;
		for (var i = 0; i < c.length; i++) {
			if (c[i].value != "All") {
				if (c[i].checked) {
					checked = true;
					break;
				}
			}
		}

		c[0].checked = !checked;
	}
}

//show or hide form bubble tips
function formTip(strControl, blnVisible, strTip) {
	if (blnVisible == undefined || !blnVisible) {
		strControl.parentNode.removeChild(document.getElementById("divFormTip"));
	} else {
		var objTips = new Object;
		objTips["promo"] = "<p>After entering a discount code or free shipping code, the savings amount will appear below the subtotal when you click \"apply.\"</p><p>You can NOT apply Free Standard Shipping codes to other types of shipping.</p>";
		objTips["address1"] = "<p>If paying with credit card, the billing address must match what the credit card company has on file.</p>";
		objTips["zip"] = "<p>If the County/State box appears locked, hit Tab or Enter after completing your Zip Code.</p>";
		objTips["city"] = "<p>Don't see your town or township? Just select \"Other\" and a blank field will appear. Then enter your city or township name.</p>";
		objTips["email1"] = "<p>We need your email address to send you order confirmation and notification of shipment emails.</p>";
		objTips["email2"] = "<p>Please re-enter email address exactly. This will help eliminate typos or inconsistencies.</p>";
		objTips["ccname"] = "<p>Enter name exactly as it appears on the card.</p>";
		objTips["ccnumber"] = "<p>Enter the card number with NO dashes or spaces.</p>";
		objTips["contact_name"] = "<p>Used only to address your return email. Sending us email does not put you on any mailing lists.</p>";
		objTips["contact_email"] = "<p>Used to send your response only, your email address will not be included in any mailing lists.</p>";
		objTips["contact_zip"] = "<p>We ask for this information in case we need to help you find a store or service center, asking ahead of time allows us to respond to you more quickly.</p>";

		var divTip = document.createElement("div");
		divTip.id = "divFormTip";
		divTip.className = "form_tip";
		divTip.style.marginLeft = (strTip == "promo" ? 115 : 15) + strControl.offsetWidth + "px";

		var divTipTop = document.createElement("div");
		divTipTop.className = "form_tip_top";
		divTipTop.innerHTML = "&nbsp;";

		var divTipContent = document.createElement("div");
		divTipContent.className = "form_tip_content";
		divTipContent.innerHTML = "Tip: " + objTips[strTip];

		var divTipBottom = document.createElement("div");
		divTipBottom.className = "form_tip_bottom";
		divTipBottom.innerHTML = "&nbsp;";

		divTip.appendChild(divTipTop);
		divTip.appendChild(divTipContent);
		divTip.appendChild(divTipBottom);

		strControl.parentNode.insertBefore(divTip, strControl.parentNode.firstChild)
	}
}

//create a modal window
function createModal(url, heading, width, height) {
	if (document.getElementById("modal_window") == null) {
		//layer doesn't exist
		var modal = document.createElement("div");
		modal.className = "shadow";

		//external domain
		var internalUrl = /(https?:\/\/)(dirtdevil|beta\.dirtdevil\.com|dirtdevil\.com)/ig;
		var isExternal = !internalUrl.test(url);

		//close button
		var close = document.createElement("a");
		close.className = "btn close image_text form_field";
		close.innerHTML = "Close";
		close.onclick = function () {
			document.body.removeChild(document.getElementById("modal_window"));
			document.body.removeChild(document.getElementById("modal_overlay"));
		}

		/*
		width = width != undefined ? width : (960 / 1.5);
		height = height != undefined ? height : 400; //document.body.offsetHeight / 1.25 + 36

		//external urls--can't change default margin / padding of body
		width = isExternal ? width + 16 : width;
		height = isExternal ? height + 16 : height;
		*/

		width = 500;
		height = 550;

		//account for url with hash
		var hash = "";
		if (url.indexOf("#") >= 0) {
			hash = url.substring(url.indexOf("#"));
			url = url.replace(hash, "");
		}

		//iframe
		var iframe = document.createElement("iframe");
		with (iframe) {
			frameBorder = "0";
			src = url + (url.indexOf("?") > 0 ? "&master=modal" : "?master=modal") + hash;
		}
		iframe.height = height - 36;

		modal.appendChild(iframe);
		modal.appendChild(close);

		document.body.appendChild(modal);

		modal.id = "modal_window";
		iframe.id = "ilayer_content";

		if(url.indexOf("product-registration") >= 0){
			modal.style.width = "800px";
		}else if(url.indexOf("video") >= 0) {
			modal.style.width = "983px";
			modal.style.height = "546px";
		}else if(url.indexOf("gator") >= 0){
			modal.style.width = "776px";
			//modal.style.height = "460px";
		}

		var top = ((document.documentElement.clientHeight - height) / 2); //fixed position
		with (modal) {
			//style.width = width + "px";
			//style.height = height + "px";
			style.left = (document.body.parentNode.offsetWidth - modal.offsetWidth) / 2 + "px";
			style.top = (top < 0 ? 0 : top) + "px";
		}

		//transparent overlay
		var overlay = document.createElement("div");
		with (overlay) {
			id = "modal_overlay";
			style.mozOpacity = "0.75";
			style.opacity = "0.75";
			style.filter = "alpha(opacity=75)";
		}
		document.body.appendChild(overlay);
	} else {
		//layer exists
		var iframe = document.getElementById("ilayer_content");
		if (iframe != null && iframe.src.indexOf(url) < 0 && url != undefined) {
			//account for url with hash
			var hash = "";
			if (url.indexOf("#") >= 0) {
				hash = url.substring(url.indexOf("#"));
				url = url.replace(hash, "");
			}
			iframe.src = url + (url.indexOf("?") > 0 ? "&master=modal" : "?master=modal") + hash;
		} else {
			document.body.removeChild(document.getElementById("modal_window"));
			document.body.removeChild(document.getElementById("modal_overlay"));
		}
	}

	return false;
}
