function submitSearch() {
    window.setTimeout(function(){
        document.searchByForm.submit();
    }, 500);
}

function setSearchField(name, value, linkText) {
	$(document.searchByForm[name]).val(value);
	try {
		if (linkText) {
			var linkStr = linkText.replace(/[\s\t\r\n]+/g, '');
			var trackingString = 'wa_custom41=shop:en_us:' + name.toLowerCase() + ':' + linkStr.toLowerCase() + '&wa_eCustom41=shop:en_us:' + name.toLowerCase() + ':' + linkStr.toLowerCase() + '&wa_events=se_cust15';
			waTrackAsLink('consumer:shop:en_us:links', 'o', trackingString);
		}
	} catch (e) {}
}

function resetSearchFields(fieldNames) {
	$.each(fieldNames, function() {
		$(document.searchByForm[this]).val('');
	});
}

function resetAllFilters(target) {
	resetAllFiltersAndDontSubmit();
    if (target.length > 0) {
        document.location.href = '/shop/' + target;
        return;
    }
	submitSearch();
}

function resetAllFiltersAndDontSubmit() {
	resetSearchFields([
        "cnetName",
		"usage",
		"priceRange",
		"brandName",
		"showAllBrands",
		"processorPlatformTech",
		"processorType",
		"memoryRange",
		"storageHardDriveCapacity",
		"displayDiagonalSize",
		"dimensionsWeights",
		"wirelessNetworkingProtocol",
		"osType",
		"batteryEnergy",
		"featured",
		"highestRated",
		"lowToHighPrice",
		"highToLowPrice",
        "sortProcessor",
		"alphabetical",
		"graphic",
		"page",
        "processorId",
        "wirelessDisplayReady",
        "wirelessBroadbandReady",
        "antiTheftSecurity",
        "ultraThin",
        "tbt",
        "builtInVisuals"
	]);
}

function submitPagination(val) {
	setSearchField("page", val);
	submitSearch();
}

//function submitUsage(val, linkText) {
//	setSearchField("usage", val, linkText);
//	resetSearchFields(["page"]);
//	submitSearch();
//}

function submitAlphabetical(val) {
	setSearchField("alphabetical", val);
	resetSearchFields([
		"featured",
        "sortProcessor",
        "lowToHighPrice",
		"highestRated",
		"highToLowPrice",
		"page"
	]);
	submitSearch();
}

function submitSystemsPerPage(val) {
	setSearchField("systemsPerPage", val);
	resetSearchFields(["page"]);
	submitSearch();
}

function submitOrderByFeatured(val) {
	setSearchField("featured", val);
	resetSearchFields([
		"highestRated",
		"alphabetical",
        "sortProcessor",
		"lowToHighPrice",
		"highToLowPrice",
		"page"
	]);
	submitSearch();
}

function submitLowToHighPrice(val) {
	setSearchField("lowToHighPrice", val);
	resetSearchFields([
		"featured",
        "sortProcessor",
		"highToLowPrice",
		"highestRated",
		"alphabetical",
		"page"
	]);
	submitSearch();
}

function submitHighToLowPrice(val) {
	setSearchField("highToLowPrice", val);
	resetSearchFields([
		"featured",
        "sortProcessor",
		"alphabetical",
		"lowToHighPrice",
		"highestRated",
		"page"
	]);
	submitSearch();
}

function submitSortProcessor(val) {
	setSearchField("sortProcessor", val);
	resetSearchFields([
		"featured",
		"highToLowPrice",
        "lowToHighPrice",
		"highestRated",
		"alphabetical",
		"page"
	]);
	submitSearch();
}

function submitOrderByHighestRate(val) {
	setSearchField("highestRated", val);
	resetSearchFields([
		"featured",
		"alphabetical",
		"lowToHighPrice",
        "sortProcessor",
		"highToLowPrice",
		"page"
	]);
	submitSearch();
}

function submitBatteryRunTime(val, linkText) {
	setSearchField("batteryEnergy", val, linkText);
	resetSearchFields([
		"featured",
		"highToLowPrice",
		"alphabetical",
		"lowToHighPrice",
        "sortProcessor",
		"highestRated",
		"page"
	]);
	submitSearch();
}

function submitOSSystem(val, linkText) {
	setSearchField("osType", val, linkText);
	resetSearchFields([
		"featured",
		"highToLowPrice",
		"alphabetical",
		"lowToHighPrice",
        "sortProcessor",
		"highestRated",
		"page"
	]);
	submitSearch();
}

function submitWirelessNetworkingProtocol(val, linkText) {
	setSearchField("wirelessNetworkingProtocol", val, linkText);
	resetSearchFields([
		"featured",
		"highToLowPrice",
		"alphabetical",
		"lowToHighPrice",
        "sortProcessor",
		"highestRated",
		"page"
	]);
	submitSearch();
}

function submitWeightRange(val, linkText) {
	setSearchField("dimensionsWeights", val, linkText);
	resetSearchFields([
		"featured",
		"highToLowPrice",
        "sortProcessor",
		"alphabetical",
		"lowToHighPrice",
		"highestRated",
		"page"
	]);
	submitSearch();
}

function submitUltraThin(val, linkText) {
	setSearchField("ultraThin", val, linkText);
	resetSearchFields([
		"featured",
		"highToLowPrice",
		"alphabetical",
        "sortProcessor",
		"lowToHighPrice",
		"highestRated",
		"page"
	]);
	submitSearch();
}

function submitScreenSizeRange(val, linkText) {
	setSearchField("displayDiagonalSize", val, linkText);
	resetSearchFields([
		"featured",
		"highToLowPrice",
		"alphabetical",
        "sortProcessor",
		"lowToHighPrice",
		"highestRated",
		"page"
	]);
	submitSearch();
}
function submitStorageRange(val, linkText) {
	setSearchField("storageHardDriveCapacity", val, linkText);
	resetSearchFields([
		"featured",
		"highToLowPrice",
        "sortProcessor",
		"alphabetical",
		"lowToHighPrice",
		"highestRated",
		"page"
	]);
	submitSearch();
}

function submitFamilyGroup(val, linkText) {
	setSearchField("processorFamily", val, linkText);
	resetSearchFields([
		"featured",
		"highToLowPrice",
		"alphabetical",
        "sortProcessor",
		"lowToHighPrice",
		"highestRated",
		"page"
	]);
	submitSearch();
}

function submitProcessorNumber(val, linkText) {
	setSearchField("processorNumber", val, linkText);
	resetSearchFields([
		"featured",
		"highToLowPrice",
        "sortProcessor",
		"alphabetical",
		"lowToHighPrice",
		"highestRated",
		"page"
	]);
	submitSearch();
}

function submitRamRange(val, linkText) {
	setSearchField("memoryRange", val, linkText);
	resetSearchFields([
		"featured",
		"highToLowPrice",
		"alphabetical",
        "sortProcessor",
		"lowToHighPrice",
		"highestRated",
		"page"
	]);
	submitSearch();
}

function submitProcessorType(processorVal, processorPlatTechVal) {
	setSearchField("processorType", processorVal);
	setSearchField("processorPlatformTech", processorPlatTechVal, processorPlatTechVal);
	resetSearchFields([
		"featured",
        "sortProcessor",
		"highToLowPrice",
		"alphabetical",
		"lowToHighPrice",
		"highestRated",
		"page"
	]);
	submitSearch();
}

function submitShowAllBrands(val) {
	setSearchField("showAllBrands", val);
	submitSearch();
}
function submitBrandName(val, linkText) {
	setSearchField("brandName", val, linkText);
	resetSearchFields([
		"featured",
		"highToLowPrice",
        "sortProcessor",
		"alphabetical",
		"lowToHighPrice",
		"highestRated",
		"page"
	]);
	submitSearch();
}

function submitId(val, linkText) {
	document.location.href = val;
}

function submitAntiTheftSecurity(val, linkText) {
	setSearchField("antiTheftSecurity", val, linkText);
	resetSearchFields([
		"featured",
		"highToLowPrice",
        "sortProcessor",
		"alphabetical",
		"lowToHighPrice",
		"highestRated",
		"page"
	]);
	submitSearch();
}

function submitTbt(val, linkText) {
	setSearchField("tbt", val, linkText);
	resetSearchFields([
		"featured",
		"highToLowPrice",
        "sortProcessor",
		"alphabetical",
		"lowToHighPrice",
		"highestRated",
		"page"
	]);
	submitSearch();
}

function submitBuiltInVisuals(val, linkText) {
	setSearchField("builtInVisuals", val, linkText);
	resetSearchFields([
		"featured",
		"highToLowPrice",
        "sortProcessor",
		"alphabetical",
		"lowToHighPrice",
		"highestRated",
		"page"
	]);
	submitSearch();
}

function submitProcessor(val, linkText) {
	setSearchField("processorId", val, linkText);
	resetSearchFields([
		"featured",
		"highToLowPrice",
        "sortProcessor",
		"alphabetical",
		"lowToHighPrice",
		"highestRated",
		"page"
	]);
	submitSearch();
}

function submitWirelessBroadbandReady(val, linkText) {
	setSearchField("wirelessBroadbandReady", val, linkText);
	resetSearchFields([
		"featured",
		"highToLowPrice",
		"alphabetical",
		"lowToHighPrice",
        "sortProcessor",
		"highestRated",
		"page"
	]);
	submitSearch();
}

function submitWirelessDisplayInstalled(val, linkText) {
	setSearchField("wirelessDisplayReady", val, linkText);
	resetSearchFields([
		"featured",
		"highToLowPrice",
		"alphabetical",
        "sortProcessor",
		"lowToHighPrice",
		"highestRated",
		"page"
	]);
	submitSearch();
}

function submitUsage(val, linkText) {
	setSearchField("usage", val, linkText);
	resetSearchFields([
        "sortProcessor",
		"featured",
		"highToLowPrice",
		"alphabetical",
		"lowToHighPrice",
		"highestRated",
		"page"
	]);
	submitSearch();
}
function submitPriceRange(val, linkText) {
	setSearchField("priceRange", val, linkText);
	resetSearchFields([
        "sortProcessor",
		"featured",
		"highToLowPrice",
		"alphabetical",
		"lowToHighPrice",
		"highestRated",
		"page"
	]);
	submitSearch();
}

function submitGraphic(val, linkText) {
	setSearchField("graphic", val, linkText);
	resetSearchFields([
        "sortProcessor",
		"featured",
		"highToLowPrice",
		"alphabetical",
		"lowToHighPrice",
		"highestRated",
		"page"
	]);
	submitSearch();
}

function getNextPage(pageNum) {
	setSearchField("page", pageNum);
	submitSearch();
}

plus = new Image();
plus.src = "/images/btn_plus.gif";
minus = new Image();
minus.src = "/images/btn_minus.gif";
function displayRow(row) {
	var btnz = "btn"+row;
	var trObj = (document.getElementById) ? document.getElementById(row) : eval("document.all['" + row + "']");
	if (trObj.style.display == "none") {
		document.images[btnz].src=minus.src;
		trObj.style.display = "";
	} else {
		document.images[btnz].src=plus.src;
		trObj.style.display = "none";
	}
}

function displayCompareList() {
	var compListDiv = document.getElementById("compareListDiv");
	if (compListDiv.style.display == "none") {
		compListDiv.style.display = "";
	} else {
		compListDiv.style.display = "none";
	}
}




// for legal popup
$(document).ready(function() {
	var currBuyNowLink = null, buyNowContinueTimer;

	function buyNowContinue() {
		setBuyNowWindowViewed();
		buyNow(currBuyNowLink);
		$("#popup-screen").hide();
		$("#shp-buy-now-popup").hide();
	};

	$("#shp-buy-now-popup .close").click(function(event) {
		event.preventDefault();
		$("#popup-screen").hide();
		$("#shp-buy-now-popup").hide();
		clearTimeout(buyNowContinueTimer);
	});

	$("#shp-now-now-continue").click(function(event) {
		event.preventDefault();
		clearTimeout(buyNowContinueTimer);
		buyNowContinue();
	});
	if (location.pathname.indexOf('/desktops') !== -1) {
		formFactor = 'desktop';
	} else if (location.pathname.indexOf('/laptops') !== -1) {
		formFactor = 'laptop';
	} else if (location.pathname.indexOf('/netbooks') !== -1) {
		formFactor = 'netbook';
	}

	$("a.buy-button").bind('click',function(event) {
        if (location.pathname.indexOf('/desktops') !== -1) {
            formFactor = 'desktop';
        } else if (location.pathname.indexOf('/laptops') !== -1) {
            formFactor = 'laptop';
        } else if (location.pathname.indexOf('/netbooks') !== -1) {
            formFactor = 'netbook';
        }
		event.preventDefault();
		var href = $(this).attr('href'), formFactor,
		newHref = href.substring(0, href.lastIndexOf('#'));
		procName = (href.getParam('processor').toLowerCase() === 'core i7e') ? 'core i7 extreme' : href.getParam('processor').toLowerCase();
		waCustomLink(this,'consumer:shop:en_us:links','o','wa_custom42=shop:en_us:buy_now&wa_eCustom25=shop:en_us:buy_now&wa_custom47=' + formFactor + '&wa_custom13='+href.getParam('manufacturer')+'&wa_custom14='+procName+'&wa_custom15='+href.getParam('offerSeller')+'&wa_custom40='+href.getParam('model')+'&wa_custom46='+href.getParam('prc')+'&wa_eCustom10=shop:buy_referral&wa_events=se_buy,se_points,se_cust15');
		currBuyNowLink = newHref;
		tryBuyNow();
		return false;
	});

	$('.img-name-specs .img a, .img-name-specs .name-specs h3 a, .img-name-specs .name-specs p a').click(function (e) {
		waCustomLink(this,'consumer:shop:en_us:links','o','wa_custom42=shop:en_us:' + formFactor + ':list:learn_more&wa_eCustom25=shop:en_us:' + formFactor + ':list:learn_more&wa_events=se_cust15');
	});

	$('.toolsLink a').click(function (e) {
		waTrackAsLink('shop:en_us:links','o','wa_custom42=shop:en_us:rhc:link:'+ this.href +'&wa_eCustom25=shop:en_us:rhc:link:'+ this.href +'&wa_events=se_cust15');
	});

	tryBuyNow = function(link) {
		if (currBuyNowLink == null)
			currBuyNowLink = link;
		if (!hasBuyNowWindowView()) {
			if (typeof $('#popup-screen').bgiframe === 'function') {
				$('#popup-screen').bgiframe();
			}
			$("#popup-screen").css({
				'height':$(document).height() + 'px',
				'width':$(document).width() + 'px'
			}).show();
			var left = ($(window).width() * 0.5) - 307;
			$("#shp-buy-now-popup").css({'left':left+'px'});
			$("#shp-buy-now-popup").css({'z-index':'99999999'});
			$("#shp-buy-now-popup").show();
			window.scrollTo(0,0);
			buyNowContinueTimer = setTimeout(buyNowContinue, 5000);
			return false;
		} else {
			buyNow(currBuyNowLink);
			return true;
		}
	};

	$(".show-full-disclaimer").click(function(e) {
		e.preventDefault();
		$("#shp-buy-now-extra-warnings").toggle();
	});

	buyNow = function(link) {
		if (link != null) {
			window.open(link);
		}
	}

	setBuyNowWindowViewed = function() {
		var d = new Date();
		d = new Date(d.getTime() + 1000*60*60*24*7); // expire in 1 week
		var domain = location.host.replace(/.*\.(\w+\.\w+)$/, "$1"); // loosen domian to e.g. intel.com
		document.cookie = "buynowviewed=true; domain=" + domain + "; expires=" + d.toGMTString();
	};

	hasBuyNowWindowView = function() {
		if (document.cookie) {
			if (document.cookie.indexOf("buynowviewed") > -1) {
				return true;
			}
		}
		return false;
	};
});
function createCookie(name,value) {
    //var expires = "; expires=-1";
    document.cookie = name+"="+value+"; path=/;";
}
function isIdExistInCookie(id) {
    var currentIds = readCookie('compareids');
    if (currentIds != null && currentIds.indexOf(id)>=0) {
        return 1;
    } else {
        return 0;
    }
}
function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return '';
}

function eraseCookie(name) {
    createCookie(name,"",-1);
}

$(function() {
	// set overlay defaults
	if ($.browser.msie && $.browser.version=="6.0")
		$("#wimax-dialog").dialog({
			bgiframe: true,
			width: 650,
			height: 470,
			autoOpen: false,
			modal: true,
			buttons: {
				Continue: function() {
					$(this).dialog('close');
				}
			}
		});

	else
		$("#wimax-dialog").dialog({
			bgiframe: true,
			width: 650,
			height: 300,
			autoOpen: false,
			modal: true,
			buttons: {
				Continue: function() {
					$(this).dialog('close');
				}
			}
		});

	// trigger overlays
	//$('#wimax-dialog').dialog('open');

	$('.wimax-click').click(function() {
		$('#wimax-dialog').dialog('open');
	})

	$('.wimax-hover').hover(function() {
		$('#wimax-dialog').dialog('open');
	})

	if ($('.core2-item').length > 0 && $('.core1-item').length > 0) {
		$('.core1-item').hide();
		$('.core1-head').toggle(function () {
			// show items
			$(this).parent().find('.core1-item').show();
			$(this).removeClass('left-nav-closed');
			$(this).addClass('left-nav-opened');
		}, function () {
			// hide items
			$(this).parent().find('.core1-item').hide();
			$(this).removeClass('left-nav-opened');
			$(this).addClass('left-nav-closed');
		}).css({'cursor': 'pointer'}).addClass('left-nav-closed');
	}
});

