var active_tab_id = null; // must be declared, even if main page doesn't do so

function selectActiveTab () {
	if (active_tab_id) {
		var active_tab = document.getElementById(active_tab_id);
		if (active_tab) {
			active_tab.className += " selected";
		}
	}
}

function setupNavBarMenuAsOnload () {
	var temp = window.onload;
	window.onload = function () {
		if (temp) temp();
		setupNavBarMenu();
	};
}

function setupNavBarMenu () {
	selectActiveTab();
	var menu = new DropDownMenuX("nav_header_menu");
	menu.init();
	menu.activateRollovers();
	/*
	  If this code is NOT called as an onload, and the tab images don't
	  have widths or heights specified, then the dropdowns will be
	  positioned wrong in Firefox if the page is reloaded via Ctrl+F5.
	*/
}

function validateProductInfoSearchForm (form) {
	if (form.MODELNUMBER && form.MODELNUMBER.value.length < 3) {
		window.alert("Please enter at least three characters.");
		form.MODELNUMBER.focus();
		form.MODELNUMBER.select();
		return false;
	}
	return true;
}

function isValidEmailAddress (string) {
	return /^\w+((-\w+)|(\.\w+)|(\+\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9][A-Za-z0-9]+$/.test(string);
}

function getQueryStringParameter (param_name) {
	var q = document.location.search.slice(1);
	var qa = q.split("&");
	for (var i = 0; i < qa.length; ++i) {
		var kv = qa[i].split("=");
		if ((kv.length >= 2) &&
		    (decodeURIComponent(kv[0]) === param_name)) {
			return decodeURIComponent(kv[1]);
		}
	}
	return null;
}

/*****************************************************************************/

function openPrivacyPolicy (url) {
	var features = "width=640,height=500,scrollbars,resizable";
	var newwindow = window.open(url, 'privacypolicy', features);
	newwindow.focus();
}

function openPhotoGallery (url) {
	var features = 'height=590,width=750';
	var newwindow = window.open(url, 'photo_gallery', features);
	newwindow.focus();
}

function openInteractiveFeatureGallery (url) {
	var features = 'height=572,width=776';
	var newwindow = window.open(url, 'interactive_feature_gallery',
		features);
	newwindow.focus();
}

function openDesignContestPopup (url, name) {
	var features = 'width=600,height=500,scrollbars=yes,resizable=yes';
	var newwindow = window.open(url, name, features);
	newwindow.focus();
}

function openDesignerDetailsPopup (url) {
	var features = 'width=700,height=550,scrollbars=yes,resizable=yes';
	var newwindow = window.open(url, "designer_details", features);
	newwindow.focus();
}

function openDesignerDetailsPopup (url) {
	var features = 'width=700,height=550,scrollbars=yes,resizable=yes';
	var newwindow = window.open(url, "designer_details", features);
	newwindow.focus();
}

function openWineVaultDealersPopup (url) {
	var features = 'width=450,height=500,scrollbars=yes,resizable=yes';
	var newwindow = window.open(url, "wine_vault_dealers", features);
	newwindow.focus();
}
function openExKitchenPop(url) {
	var features = 'width=1000,height=740,scrollbars=yes,resizable=yes';
	var newwindow = window.open(url, "exquisite_kitchens", features);
	newwindow.focus();
}

// Function to open video gallery 
// Uses the 'openPop' function
function openVideoGalleryPopup(url) {
	openPop(url, 701, 637, 'resizable');
}


// "only 1 open at a time" popup function, used on spec pages, copied from GEA
//-----------------------------------------------------------------------------
// The fifth argument, windowname, is optional.  If specified, it is passed as
// the second argument to window.open().  This is required in order to leave
// the popup opener page (without closing the popup), come back to it, and NOT
// have a duplicate popup window.
//-----------------------------------------------------------------------------
var popupWin;
var scrnwidth = screen.width;
var scrnheight = screen.height;
function openPop (url, width, height, addloptions, windowname) {
	if (windowname == null) {
		windowname = "";
	}
	// scrnwidth
	// scrnheight
	// popupWin
	var xspot = Math.round((scrnwidth / 2) - (width / 2));
	var yspot = Math.round((scrnheight / 2) - (height / 2) - 30);
	features = "height=" + height + ",width=" + width + 
		",top=" + yspot + ",screenY=" + yspot + 
		",left=" + xspot + ",screenX=" + xspot;
	if (addloptions && addloptions != "") {
		features += "," + addloptions;
	}
	if (!popupWin || popupWin.closed) {
		popupWin = window.open(url, windowname, features);
	}
	else {
		window.popupWin.close();
		popupWin = window.open(url, windowname, features);
	}
	popupWin.focus();
}

// For "Find a Dealer" and "Register Your Appliance" pages.
// options is an optional object parameter with the following optional
// properties:
//   min_height - minimum iframe height to override the default.
//                set really low for scrolling iframes to avoid dual
//                scrollbars.
function activateIframeAutoResize (iframe, /*optional*/ options) {
	/* ASSUMPTIONS: The iframe is the LAST element in the document.  No
	   bottom margin or padding.  Proper DOCTYPE declaration. */
	var min_iframe_height = 425;
	if (options && ("min_height" in options)) {
		min_iframe_height = options["min_height"];
	}
	var resize_iframe = function () {
		var header_height = iframe.offsetTop;
		var window_height = document.documentElement.clientHeight;
		var iframe_height = window_height - header_height;
		if (iframe_height < min_iframe_height) {
			iframe_height = min_iframe_height;
		}
		iframe.style.height = iframe_height + "px";
	};
	resize_iframe();
	window.onresize = resize_iframe; // We assume no handler already exists
}

// for Compare pages
function fnFriendPrepare() { 
	var docloc;
	var completeurl;
	docloc = new String(document.location);
	docloc = docloc.replace(/ /g, '%20');
        completeurl = "http://www.geappliances.com/shop/rbt_offr/email_compare.htm?" + docloc;
        openPop(completeurl, "580", "570", "scrollbars,resizable"); 
}

// for site search form in header
function validateSiteSearchForm (form) {
	if ("textToSearch" in form) {
		if (!/\S/.test(form.textToSearch.value)) { 
			return false;
		}
	}
	else if ("q" in form) {
		if (!/\S/.test(form.q.value)) {
			return false;
		}
	}
	return true;
}

// for advanced search forms
function validateAdvancedSearchForm (form) {
	if (/\S/.test(form.as_q.value) ||
	    /\S/.test(form.as_epq.value) ||
	    /\S/.test(form.as_oq.value)) {
		return true;
	}
	alert("Please enter a search term in at least one of the first three fields.");
	form.as_q.focus();
	form.as_q.select();
	return false;
}

// for email subscribe form in footer
function validateFooterSubscribeForm (form) {
	if (!isValidEmailAddress(form.i_emailgeneric.value)) {
		window.alert("Please enter a valid email address.");
		form.i_emailgeneric.focus();
		form.i_emailgeneric.select();
		return false;
	}
	return true;
}

// for literature searches
function validateDocSearch (form) {
	if ("q" in form) {
		var q = form.q.value;
		var q_is_a_model_number = 
			/^[-\%\"\#a-z0-9]+$/i.test(q) && 
			q.length >= 3 &&
			((q.length < 5  && /[a-z]/i.test(q)) ||
			 (q.length >= 5 && /[a-z]/i.test(q) && /[0-9]/i.test(q)));
		if (!q_is_a_model_number) {
			alert("Please enter a valid model number.");
			form.q.focus();
			form.q.select();
			return false;
		}
	}
	return true;
}

/**************** BEGIN GOOGLE ANALYTICS CODE ****************/
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-23305009-1']);
_gaq.push(['_trackPageview']);

(function() {
	var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
	ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
	var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
/**************** END GOOGLE ANALYTICS CODE ****************/



/**************** BEGIN MOBILE REDIRECT CODE ****************/
var mobileKeywords = [
	"mobi", "phone", "blackberry", "android", "iphone", "BenQ-Siemens",
	"SIE-S68", "SIE-EF81", "Windows CE", "LG/U880", "MIDP", "CLDC",
	"LG-G5200", "LG-G7000", "UP.Browser", "LG/U8120", "LG/U8130",
	"LG/U8138", "LG/U8180", "SymbianOS", "webOS", "PalmSource",
	"NetFront", "SGH-i900", "SonyEricsson", "skyfire"
];

// Generic flag: Is the user surfing from a tablet?  This flag is used
// to determine whether mobileRedirect() redirects.  This check will
// probably be modified in the future to include other types of tablet
// devices on which we also do not want mobile redirects to take place.
var isTablet = /ipad/i.test(navigator.userAgent); // genericism


// Is the user surfing from a mobile device?  THIS INCLUDES TABLETS
// running a mobile platform.  The isTablet flag will be used to exclude
// tablets.
var isMobileDevice;

// tl;dr: this code block sets the isMobileDevice flag to true or false.
(function () {
	var i, ua, kw;
	ua = navigator.userAgent.toLowerCase();
	for (i = 0; i < mobileKeywords.length; i += 1) {
		mobileKeywords[i] = mobileKeywords[i].toLowerCase();
	}
	isMobileDevice = false;
	for (i = 0; i < mobileKeywords.length; i += 1) {
		kw = mobileKeywords[i];
		if (ua.indexOf(kw) !== -1) {
			isMobileDevice = true;
			break;
		}
	}
})();

// tl;dr: this function redirects the user to the URL specified in its
// argument.
function mobileRedirect (url) {
	var $ = (typeof(jQuery) !== "undefined") ? jQuery : null;
	if ($ && !$.cookie("preventMobileRedirect") && 
	    isMobileDevice && !isTablet) {
		location.replace(url);
	}
}
/**************** END MOBILE REDIRECT CODE ****************/


