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 (form.textToSearch.value == "") { 
		return false;
	}
	return true;
}

// 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;
}

