﻿//main site search validation
function siteSearch(){
	return document.getElementById("q").value != "" ? true : false;
}

//show or hide panels based on tabs (product detail, search, etc.)
function activatePanel(clicked){
	var tabs = new Array("description","features","parts","products","website","manuals");	
	var hash = (clicked != undefined ? clicked.replace("tab_","") : (window.location.hash != "" ? window.location.hash.replace("#","") : tabs[0]));

	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" : "block");
			tab.className = (hash != tabs[i] ? "image_text" : "image_text tab_" + tabs[i] + "_active");
		}
	}	
}

//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 above 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 modalWindow(strURL){
	if(document.getElementById("modal_window") == null){
		//Layer Doesn't Exist - Create
		divModal = document.createElement("div");

		//close button
		aClose = document.createElement("a");
		aClose.innerHTML = "Close";
		aClose.className = "btn close image_text form_field";
		aClose.onclick = function(){
			document.body.removeChild(document.getElementById("modal_window"));
			document.body.removeChild(document.getElementById("modal_overlay"));
		}

		//iframe
		ifContent = document.createElement("iframe");
		ifContent.frameBorder = "0";
		ifContent.src = strURL + (strURL.indexOf("?") > 0 ? "&master=blank" : "?master=blank");
		//ifContent.src = strURL.indexOf("master=nothing") > 0 ? strURL.replace("master=nothing","master=blank") : strURL + "?master=blank";
				
		//content layer (close button and iframe container)
		divModal.appendChild(aClose);
		divModal.appendChild(ifContent);
		
		document.body.appendChild(divModal);
		
		divModal.id = "modal_window";
		ifContent.id = "ilayer_content";
		
		if(strURL.indexOf("registermodel.aspx") >= 0){
			divModal.style.width = "800px";
		}
		
		var yPos = (window.pageYOffset == undefined || window.pageYOffset <= 0) ? document.documentElement.scrollTop : window.pageYOffset;
		var left = (document.body.offsetWidth - divModal.offsetWidth) / 2;
		var top = yPos + ((document.documentElement.clientHeight - divModal.offsetHeight) / 2)
		divModal.style.left =  (document.body.offsetWidth - divModal.offsetWidth) / 2 + "px";
		divModal.style.top = (top < 0 ? 0 : top) + "px";
		
		//transparent overlay
		var olWidth = document.body.parentNode.offsetWidth;
		var olHeight = document.getElementById("container").offsetHeight + document.getElementById("shadow_bottom").offsetHeight;
		olHeight = olHeight < document.documentElement.clientHeight ? document.documentElement.clientHeight : olHeight;
		var olLeft = (document.body.offsetWidth - document.body.parentNode.offsetWidth) / 2;
		var olTop = 0;
		divOverlay = document.createElement("div");
		with(divOverlay){
			id = "modal_overlay";
			style.mozOpacity = "0.75";
			style.opacity = "0.75";
			style.filter = "alpha(opacity=75)";
			style.width = olWidth + "px";
			style.height = olHeight + "px";
			style.left = olLeft + "px";
			style.top = olTop + "px";
		}
		document.body.appendChild(divOverlay);
	}else{
		//Layer Already Exists - New iFrame src?
		var objFrame = document.getElementById("ilayer_content");
		if(objFrame != null && objFrame.src.indexOf(strURL) < 0){
			objFrame.src = strURL + (strURL.indexOf("?") > 0 ? "&master=blank" : "?master=blank");
			//objFrame.src = strURL.indexOf("master=nothing") > 0 ? strURL.replace("master=nothing","master=blank") : strURL + "?master=blank";
		}
	}
}

//update image index on image gallery
function updateImage(intIndex){
	if(intIndex >= 0 && intIndex < intImgCount){
		with(document.getElementById("imgLarge")){
			src = document.getElementsByName("aGalleryThumb")[intIndex].href;
			width = aryImg[intIndex][0];
			height = aryImg[intIndex][1];
			alt = aryImg[intIndex][2];
		}
		
		var a = document.getElementsByName("aGalleryThumb")
		for(var i = 0; i < a.length; i++){
			a[i].className = (i == intIndex) ? "selected" : "";
		}
		
		//set new index
		intImgIndex = intIndex;
		
		//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 >= intImgCount){
			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","");
		}
	}
}

//xmlhttp request object
function getXmlHttp(){
	var xmlHttp = null;
	try{
		// Firefox, Opera 8.0+, Safari, IE 7+
		xmlHttp = new XMLHttpRequest();
	}catch (e){
		//IE 6-
		try{
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}catch (e){
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

//set compare item
function setCompare(chk){
	var objXmlHttp = getXmlHttp();
	strURL = "/products/compare.aspx?" + (chk.checked ? "add=" : "remove=") + chk.value;	

	objXmlHttp.open("GET",strURL,true);
	objXmlHttp.send(null);
	
	objXmlHttp.onreadystatechange = function(){
		if(objXmlHttp.readyState == 4){
			if(objXmlHttp.responseText != ""){
				chk.checked = false;
				if(confirm(objXmlHttp.responseText)){
					window.location = "/products/compare.aspx";
				};
			}
		}
	}
}

//social network bookmarks
function socialBookmark(site) {
	var strURL = encodeURIComponent(location.href);
	var strTitle = encodeURIComponent(document.title);
	
	var linklist = {
		ask :         'http://myjeeves.ask.com/mysearch/BookmarkIt?v=1.2&t=webpages&url=' + strURL + '&title=' + strTitle + '&abstext=&tagField=',
		blinklist :   'http://www.blinklist.com/index.php?Action=Blink/addblink.php&Description=&Tag=&Url=' + strURL + '&Title=' + strTitle,
		delicious :   'http://del.icio.us/post?v=2&url=' + strURL + '&notes=&tags=&title=' + strTitle,
		digg :        'http://digg.com/submit?phase=2&url=' + strURL + '&bodytext=&tags=&title=' + strTitle,
		diigo :       'http://www.diigo.com/post?url=' + strURL + '&title=' + strTitle + '&tag=&comments=',
		facebook :    'http://www.facebook.com/sharer.php?u=' + strURL + '&t=' + strTitle,
		fark	:     'http://cgi.fark.com/cgi/fark/submit.pl?new_url=' + strURL + '&new_comment=' + strTitle + '&linktype=',
		faves :       'http://faves.com/Authoring.aspx?u=' + strURL + '&t=' + strTitle,
		furl :        'http://www.furl.net/storeIt.jsp?u=' + strURL + '&keywords=&t=' + strTitle,
		google :      'http://www.google.com/bookmarks/mark?op=add&bkmk=' + strURL + '&annotation=&labels=&title=' + strTitle,
		magnolia :    'http://ma.gnolia.com/bookmarklet/add?url=' + strURL + '&title=' + strTitle,
		mixx :        'http://www.mixx.com/submit?page_url=' + strURL,
		myspace :     'http://www.myspace.com/Modules/PostTo/Pages/?u=' + strURL + '&t=' + strTitle, 
		newsvine :    'http://www.newsvine.com/_wine/save?popoff=1&u=' + strURL + '&tags=&blurb=' + strTitle,
		reddit :      'http://reddit.com/submit?url=' + strURL + '&title=' + strTitle,
		simpy :       'http://www.simpy.com/simpy/LinkAdd.do?title=' + strTitle + '&tags=&note=&href=' + strURL,
		slashdot :    'http://slashdot.org/bookmark.pl?url=' + strURL + '&title=' + strTitle,
		stumbleupon : 'http://www.stumbleupon.com/submit?url=' + strURL + '&title=' + strTitle,
		technorati :  'http://technorati.com/faves/?add=' + strURL + '&tag=',
		twitter :     'http://twitter.com/home?status=Add+This:+' + strURL,
		yahoo :       'http://myweb2.search.yahoo.com/myresults/bookmarklet?t=' + strTitle + '&d=&tag=&u=' + strURL,
		yahoobm :     'http://bookmarks.yahoo.com/toolbar/savebm?opener=tb&u=' + strURL + '&t=' + strTitle
	};
	var scwindow = window.open(linklist[site],"sbmwindow","menubar=yes,location=yes,resizable=yes,scrollbars=yes,status=yes,width=595,height=495");
}