function showBenefit(index) {
    /* fail gracefully */
    if( index == null || index == NaN)
        return false;
   var oStage = document.getElementById('stage');
   var oBenefit = document.getElementById('core-benefits');
   var oCarousel = document.getElementById('carousel');

   /* fail gracefully */
   if( !oStage || !oBenefit || !oCarousel )
       return false;
   var colStage = oStage.getElementsByTagName('LI');
   var colBenefit = oBenefit.getElementsByTagName('LI');
   var colCarousel = oCarousel.getElementsByTagName('A');

   /* fail gracefully */
   if( !(colStage && (colStage.length == colBenefit.length) && (colStage.length == colCarousel.length)) )
        return false;
   for (i=0; i<colStage.length; i++) {
       colStage[i].className = (i == index) ? "on":"";
       colBenefit[i].className = (i == index) ? "on":"";
       colCarousel[i].className = (i == index) ? "on":"";
   }
}

function hideBenefits() {
    var oBenefits = document.getElementById('core-benefits');
    /* fail gracefully */
    if( oBenefits == null )
        return false;
    var colBenefits = oBenefits.getElementsByTagName('LI');
    /* fail gracefully */
    if( !(colBenefits && colBenefits.length) )
        return false;
    for( i=1; i<colBenefits.length; i++ ) {
        colBenefits[i].className = '';
    }
}


/*
 * Urchin Tracker Related functions
 **/

function urchinUrlDoc(locale,targetURL,segment,category,subcategory,identifier){
	var urchinURL = getUrchinURLproduct(locale,'downloads',segment,category,subcategory,identifier);
	if (urchinURL != null && urchinURL != ""){
		urchinTracker(urchinURL);
	}
	window.open(targetURL);
}

function urchinUrlEnergyStar(targetURL){
	urchinOutboundLinks(targetURL);
}

function urchinUrlAcrobat(targetURL){
	urchinOutboundLinks(targetURL);
}

function urchinPromoDoc(locale,promoID,targetURL)
{
	var urchinURL = getUrchinURLpromo(locale,'download','promotion',promoID);
	if (urchinURL != null && urchinURL != "") {
		urchinTracker(urchinURL);
	}
	window.open(targetURL);
}

function urchinOutboundLinks(URL){
	var urchinURL = "/outboundlinks/";
	if (URL != null && URL != ""){
		urchinURL += "URL=" + escape(URL);
		urchinTracker(urchinURL);
		window.open(URL);
	}
}

function urchinUrlFindStore(locale,targetURL,segment,category,subcategory,identifier){
	var urchinURL = getUrchinURLproduct(locale,'find-retailer',segment,category,subcategory,identifier);
	if (urchinURL != null && urchinURL != ""){
		urchinTracker(urchinURL);
	}
	window.location=targetURL;
}

function urchinUrlAddToCart(locale,targetURL,segment,category,subcategory,identifier){
	var urchinURL = getUrchinURLproduct(locale,'add_to_cart',segment,category,subcategory,identifier);
	if (urchinURL != null && urchinURL != ""){
		urchinTracker(urchinURL);
	}
	window.location=targetURL;
}

function urchinRetailer(dealerURL){
	var urchinURL = "/outboundlinks/retailers";
	if (dealerURL != null && dealerURL != ""){
		urchinURL += "/URL=" + escape(dealerURL);
	} 
	urchinTracker(urchinURL);
	window.open(dealerURL);
}

function urchinRetailerLocation(mapURL){
	var urchinURL = "/outboundlinks/retailers/Mapquest";
	if (mapURL != null && mapURL != ""){
		urchinURL += "/URL=" + escape(mapURL);
	} 
	urchinTracker(urchinURL);
	window.open(mapURL);
}

function printProductWrapper(locale,segment,category,subcategory,identifier){
	var urchinURL = getUrchinURLproduct(locale,'print_page',segment,category,subcategory,identifier);
	if (urchinURL != null && urchinURL != ""){
		urchinTracker(urchinURL);
	}
	print();
}

function printPromoWrapper(locale,promoID){
	var urchinURL = getUrchinURLpromo(locale,'print_page','promotion',promoID);
	if (urchinURL != null && urchinURL != ""){
		urchinTracker(urchinURL);
	}
	print();
}

function printCompareWrapper(url){
	var urchinURL = getUrchinURLcompare('print_page',url);
	if (urchinURL != null && urchinURL != ""){
		urchinTracker(urchinURL);
	}
	print();
}

// The URL advertised by the TellAFriend in the Scrapbook
// actually needs to be manufactured before getting passed
// as part of the query string to ensure that the
// TellAFriend template remains generic.

function tellFriendScrapbook(url)
{
	//get the scrapbook cookie value
	var scrapbook = ReadCookie('_scrapbook');

	var urchinURL = getUrchinURLscrapbook('tell_a_friend',url);
	if (urchinURL != null && urchinURL != ""){
		urchinTracker(urchinURL);
	}

	var URL = document.location.href;
	URL = URL.replace( /#/, "" );
	URL = URL.replace( /ScrapBook/, "ViewScrapBook" );
	URL = URL + "?scrapbook=" + scrapbook;

	winTellFriend = openCenteredWindow('../TellAFriend/default.html?url=' + escape(URL) + '&resourceDictionaryId=resTellAFriendScrapbookContent', 'tellFriend', 490, 500, false, false, false, this);
}

function tellFriendProduct(locale,segment,category,subcategory,identifier) {
	var urchinURL = getUrchinURLproduct(locale,'tell_a_friend',segment,category,subcategory,identifier);
	tellFriend(urchinURL);
}

function tellFriendPromo(locale,promoID) {
	var urchinURL = getUrchinURLpromo(locale,'tell_a_friend','promotion',promoID);
	tellFriend(urchinURL);
}

function tellFriendCompare(url){
	var urchinURL = getUrchinURLcompare('tell_a_friend',url);
	if (urchinURL != null && urchinURL != ""){
		urchinTracker(urchinURL);
	}
	var URL = document.location.href;
	winTellFriend = openCenteredWindow('../TellAFriend/default.html?url=' + escape(URL) + '&resourceDictionaryId=resTellAFriendContent', 'tellFriend', 550, 500, false, false, false, this);
}

function tellFriend(urchinURL){
	if (urchinURL != null && urchinURL != ""){
		urchinTracker(urchinURL);
	}

	var url = document.location.href;
	winTellFriend = openCenteredWindow('../TellAFriend/default.html?url=' + escape(url) + '&resourceDictionaryId=resTellAFriendContent', 'tellFriend', 490, 500, false, false, false, this);
}

//Adds a product into the scrapbook
function addToScrapbookProduct(locale,segment,category,subcategory,identifier) {


	var urchinURL = getUrchinURLproduct(locale,'add_to_scrapbook',segment,category,subcategory,identifier);

	if (urchinURL != null && urchinURL != ""){
		urchinTracker(urchinURL);
	}

	var url = document.location.href;

	var innerUrl = '../AddToScrapBook/default.html?url=' + escape(url) + '&contextType=1' + '&resourceDictionaryId=resAddToScrapBookForProduct';
	winAddToScrapbook = openCenteredWindow('../CookieDetector/default.html?targetUrl=' + escape(innerUrl), 'addToScrapbook', 550, 410, false, false, false, this);
}

//Adds a kitchen into the scrapbook
function addToScrapbook(identifier) {

	var url = document.location.href;

	var innerUrl = '../AddToScrapBook/default.html?url=' + escape(url) + '&contextType=2' + '&resourceDictionaryId=resAddToScrapBookForKitchen';
	winAddToScrapbook = openCenteredWindow('../CookieDetector/default.html?targetUrl=' + escape(innerUrl), 'addToScrapbook', 550, 410, false, false, false, this);

}


function getUrchinURLscrapbook(type,url){
	var urchinURL = "";
	if (type != null && type != ""){
		urchinURL += "/" + type;
	}
	urchinURL += "/Scrapbook";
	if (url != null && url != ""){
		urchinURL += "/URL=" + escape(url);
	}
	return urchinURL;
}

function getUrchinURLcompare(type,url){
	var urchinURL = "";
	if (type != null && type != ""){
		urchinURL += "/" + type;
	}
	urchinURL += "/Compare";
	if (url != null && url != ""){
		urchinURL += "/URL=" + escape(url);
	}
	return urchinURL;
}

function getUrchinURLpromo(locale,type,section,promoID){
	var urchinURL = "";
	if (locale != null && locale != ""){
		urchinURL += "/" + locale;
	}
	if (type != null && type != ""){
		urchinURL += "/" + type;
	}
	if (section != null && section != ""){
		urchinURL += "/" + section;
	}
	if (promoID != null && promoID != ""){
		urchinURL += "/" + promoID;
	}
	return urchinURL;
}

function getUrchinURLproduct(locale,type,segment,category,subcategory,identifier){
	var urchinURL = "";

	if (locale != null && locale != ""){
		urchinURL += "/" + locale;
	}
	if (type != null && type != ""){
		urchinURL += "/" + type;
	}
	if (segment != null && segment != ""){
		urchinURL += "/" + segment;
	}
	if (category != null && category != ""){
		urchinURL += "/" + category;
	}
	if (subcategory != null && subcategory != ""){
		urchinURL += "/" + subcategory;
	}
	if (identifier != null && identifier != ""){
		urchinURL += "/" + identifier;
	}
	return urchinURL;
}

function goToExternalLink(url,dealerName){
	urchinTracker(url);
	window.open(url,dealerName);
}

function modelSerialLocator() {
    winSerialLocator = openCenteredWindow('../RegistrationModels/default.html', 'modelSerialLocator', 570,600, false, true, false, this);
}

function enlargedPhoto(url,width,height) {
    winEnlargedPhoto = openCenteredWindow(url, 'enlargedPhoto', width,height, false, false, false, this);
}

/*******************************************************************************
	Popup Window openers
*******************************************************************************/

var winReference = null;


// Open a window at a given position on the screen
function openPositionedWindow(url, name, width, height, x, y, status, scrollbars, moreProperties, openerName) {
	// ie 4.5 and 5.0 mac - windows are 2 pixels too short; if a statusbar is used, the window will be an additional 15 pixels short
	var agent = navigator.userAgent.toLowerCase();
	if (agent.indexOf("mac") != -1 && agent.indexOf("msie") != -1 && (agent.indexOf("msie 4") != -1 || agent.indexOf("msie 5.0") != -1) ) {
		height += (status) ? 17 : 2;
	}

	// Adjust width if scrollbars are used (pc places scrollbars inside the content area; mac outside)
	width += (scrollbars != '' && scrollbars != null && agent.indexOf("mac") == -1) ? 16 : 0;

	var properties = 'width=' + width + ',height=' + height + ',screenX=' + x + ',screenY=' + y + ',left=' + x + ',top=' + y + ((status) ? ',status' : '') + ',scrollbars' + ((scrollbars) ? '' : '=no') + ((moreProperties) ? ',' + moreProperties : '');
	var reference = openWindow(url, name, properties, openerName);

	return reference;
}

// Open a window at the center of the screen
function openCenteredWindow(url, name, width, height, status, scrollbars, moreProperties, openerName) {
	var x, y = 0;
	if (screen) {
      x = (screen.availWidth - width) / 2;
	   y = (screen.availHeight - height) / 2;
   }
	if (!status) status = '';
	if (!openerName) openerName = '';
	var reference = openPositionedWindow(url, name, width, height, x, y, status, scrollbars, moreProperties, openerName);
	return reference;
}

// Core utility function that actually creates the window and gives focus to it
function openWindow(url, name, properties, openerName) {

	// ie4.x pc can't give focus to windows containing documents from a different domain
	// in this case, initially load a local interstisial page to allow focussing before loading final url
	var agent = navigator.userAgent.toLowerCase();
	if (agent.indexOf("msie") != -1 && parseInt(navigator.appVersion) == 4 && agent.indexOf("msie 5") == -1 && agent.indexOf("msie5") == -1 && agent.indexOf("win") != -1 && url.indexOf('http://') == 0) {
		winReference = window.open('about:blank', name, properties);

		setTimeout('if (winReference && !winReference.closed) winReference.location.replace("' + url + '")', 300);
	}
	else {
		winReference = window.open(url, name, properties);
	}

	// ie doesn't like giving focus immediately (to new window in 4.5 on mac; to existing ones in 5 on pc)
	setTimeout('if (winReference && !winReference.closed) winReference.focus()', 200);

	if (openerName) self.name = openerName;
	return winReference;
}

function gotoLink(sel)
{
	var s = sel.options[sel.selectedIndex].value;
	if (s != "")
		window.location = s;
}


function ReadCookie(cookieName) {

	var theCookie=""+document.cookie;
	var ind=theCookie.indexOf(cookieName);

	if (ind==-1 || cookieName=="")
		return "";

	var ind1=theCookie.indexOf(';',ind);
	if (ind1==-1)
		ind1=theCookie.length;

	return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}


