//v7.0

//****************** Global vars **************
var g_titleUriEnc = escape(document.title).replace(/%20/g, "+");
var g_urlUriEnc = escape(document.location).replace(/\//g, "%2f");

// ********** Common ****************************************************
function OpenWindow(url, val, height, width, ShowHeader) {
    docwidth = width;
    docheight = height;
    leftVal = (screen.width - docwidth) / 2;
    topVal = (screen.height - docheight) / 2;
    var args = 'toolbar=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1';
    if (ShowHeader != true) {
        args = 'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1';
    }
    var newWin = window.open(url + val, "view", "height=" + docheight + ",width=" + docwidth + ",left=" + leftVal + ",top=" + topVal + "toolbar=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes");
    if (newWin != null) newWin.focus();
}

// ********* Custom *****************************************************

function audio(audioNbr) {
    var leftVal = (screen.width - 220) / 2;
    var topVal = (screen.height - 100) / 2;
    var url = 'http://walmartstores.com/Audio/AudioPlayer.htm?id=' + audioNbr;
    window.open(url, 'Audio', 'width=220,height=100,menubar=0,toolbar=0,status=0,scrollbars=0,resizable=0,top=' + topVal + ',left=' + leftVal);
}

function video(videoNbr) {
    var url = 'http://walmartstores.com/Video/VideoPlayer.htm?id=' + videoNbr;
    window.open(url, 'Video', 'width=400,height=336,menubar=0,toolbar=0,status=0,scrollbars=0,resizable=0');
}

//****************** Controls ******************

//* Share This
function ShowShareThis() {
    $('#dShareThisModal').modal();
    $('#aDelicious').attr("href", "http://del.icio.us/post?url=" + g_urlUriEnc + "&title=" + g_titleUriEnc);
    $('#aDigg').attr("href", "http://digg.com/submit?phase=2&url=" + g_urlUriEnc + "&title=" + g_titleUriEnc);
    $('#aStumbleUpon').attr("href", "http://www.stumbleupon.com/submit?url=" + g_urlUriEnc + "&title=" + g_titleUriEnc);
    $('#aFurl').attr("href", "http://furl.net/storeIt.jsp?u=" + g_urlUriEnc + "&t=" + g_titleUriEnc);
    $('#aNetscape').attr("href", "http://www.netscape.com/submit/?U=" + g_urlUriEnc + "&T=" + g_titleUriEnc);
    $('#aYahoo').attr("href", "http://myweb2.search.yahoo.com/myresults/bookmarklet?u=" + g_urlUriEnc + "&t=" + g_titleUriEnc);
    $('#aTechnorati').attr("href", "http://www.technorati.com/faves?add=" + g_urlUriEnc);
    $('#aGoogle').attr("href", "http://www.google.com/bookmarks/mark?op=edit&bkmk=" + g_urlUriEnc + "&title=" + g_titleUriEnc);
    $('#aNewsvine').attr("href", "http://www.newsvine.com/_wine/save?u=" + g_urlUriEnc + "&h=" + g_titleUriEnc);
    $('#aBlinkList').attr("href", "http://blinklist.com/index.php?Action=Blink/addblink.php&Url=" + g_urlUriEnc + "&Title=" + g_titleUriEnc);
    $('#aReddit').attr("href", "http://reddit.com/submit?url=" + g_urlUriEnc + "&title=" + g_titleUriEnc);
    $('#aBlogmarks').attr("href", "http://blogmarks.net/my/new.php?mini=1&url=" + g_urlUriEnc + "&title=" + g_titleUriEnc);
    $('#aWindowsLive').attr("href", "http://favorites.live.com/quickadd.aspx?marklet=1&mkt=en-us&url=" + g_urlUriEnc + "&title=" + g_titleUriEnc + "&top=1");
    $('#aTailrank').attr("href", "http://tailrank.com/share/?link_href=" + g_urlUriEnc + "&title=" + g_titleUriEnc);
    $('#aSlashdot').attr("href", "http://slashdot.org/bookmark.pl?url=" + g_urlUriEnc + "&title=" + g_titleUriEnc);
    $('#aFacebook').attr("href", "http://www.facebook.com/share.php?u=" + g_urlUriEnc);
}

//* Email This
function ShowEmailThis() {
    OpenWindow("http://walmartstores.com/P/EmailContent.aspx?title=" + g_titleUriEnc + "&url=" + g_urlUriEnc, "", "630", "420", "False"); return false;
}

// ****************** JSCookies ******************

// gets a cookie from the existing document and returns its value
function get_text_cookie(cookie_name) {
    var results = document.cookie.match(cookie_name + '=(.*?)(;|$)');

    if (results) {
        return (unescape(results[1]));
    }
    else { return null; }
}

// creates a cookie with the given parameters
function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    } else {
        var expires = "";
    }
    document.cookie = name + "=" + value + expires + "; path=/";
}

// locates and reads the value of a cookie with a specified name
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 null;
}

// deletes a cookie with a specified name
function eraseCookie(name) {
    createCookie(name, "", -1);
}

// -------------------------------------------------------------------------------------
// New Methods from http://techpatterns.com/downloads/javascript_cookies.php
// -------------------------------------------------------------------------------------

function Set_Cookie(name, value, expires, path, domain, secure) {
    // set time (milliseconds)
    var today = new Date();
    today.setTime(today.getTime());

    /*
    if the expires variable is set, make the correct 
    expires time, the current script below will set 
    it for x number of days, to make it for hours, 
    delete * 24, for minutes, delete * 60 * 24
    */
    if (expires) {
        expires = expires * 1000 * 60 * 60 * 24;
    }
    var expires_date = new Date(today.getTime() + (expires));

    document.cookie = name + "=" + escape(value) +
        ((expires) ? ";expires=" + expires_date.toGMTString() : "") +
        ((path) ? ";path=" + path : "") +
        ((domain) ? ";domain=" + domain : "") +
        ((secure) ? ";secure" : "");
}

function Get_Cookie(check_name) {
    // first we'll split this cookie up into name/value pairs
    // note: document.cookie only returns name=value, not the other components
    var a_all_cookies = document.cookie.split(';');
    var a_temp_cookie = '';
    var cookie_name = '';
    var cookie_value = '';
    var b_cookie_found = false; // set boolean t/f default f

    for (i = 0; i < a_all_cookies.length; i++) {
        // now we'll split apart each name=value pair
        a_temp_cookie = a_all_cookies[i].split('=');

        // and trim left/right whitespace while we're at it
        cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

        // if the extracted name matches passed check_name
        if (cookie_name == check_name) {
            b_cookie_found = true;
            // we need to handle case where cookie has no value but exists (no = sign, that is):
            if (a_temp_cookie.length > 1) {
                cookie_value = unescape(a_temp_cookie[1].replace(/^\s+|\s+$/g, ''));
            }
            // note that in cases where cookie is initialized but no value, null is returned
            return cookie_value;
            break;
        }
        a_temp_cookie = null;
        cookie_name = '';
    }
    if (!b_cookie_found) {
        return null;
    }
}

function Get_Int_Cookie(check_name) {
    var intVal = null;
    if (Get_Cookie(check_name)) {
        intVal = parseInt(Get_Cookie(check_name));
    }
    return intVal;
}

// this deletes the cookie when called
function Delete_Cookie(name, path, domain) {
    if (Get_Cookie(name)) document.cookie = name + "=" +
        ((path) ? ";path=" + path : "") +
        ((domain) ? ";domain=" + domain : "") +
        ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

// gets the current domain, removing a leading www.
function GetDomain() {
    var dm = window.location.hostname;
    dm = dm.replace(/www./i, '');
    return dm;
}

//****************** Font-Sizes ******************
// dependency on JSCookies.js

// text resizing
var currentTextSize = null;
var currentLineHeight = null;
// check for cookie
if (get_text_cookie("ihtfontsize")) {
    currentTextSize = get_text_cookie("ihtfontsize");
    if (currentTextSize == .85) {
        currentLineHeight = 1.45;
    }
    else if ((currentTextSize == 1) || (currentTextSize == 1.15)) {
        currentLineHeight = 1.4;
    }
    else if (currentTextSize == 1.35) {
        currentLineHeight = 1.3;
    }
} else {
    currentTextSize = 1;
    currentLineHeight = 1.4;
    createCookie("ihtfontsize", currentTextSize, 90);
}

function textSize(dir) {
    if (dir == 'up') {
        if (currentTextSize == 1.15) {
            currentTextSize = 1.35;
            currentLineHeight = 1.3;
        }
        else if (currentTextSize == 1) {
            currentTextSize = 1.15;
            currentLineHeight = 1.4;
        }
        else if (currentTextSize == .85) {
            currentTextSize = 1;
            currentLineHeight = 1.4;
        }
    } else if (dir == 'down') {
        if (currentTextSize == 1) {
            currentTextSize = .85;
            currentLineHeight = 1.45;
        }
        else if (currentTextSize == 1.15) {
            currentTextSize = 1;
            currentLineHeight = 1.4;
        }
        else if (currentTextSize == 1.35) {
            currentTextSize = 1.15;
            currentLineHeight = 1.4;
        }
    }
    //    currentLineHeight = currentTextSize + .3;

    SetContentStyles();
    // write/rewrite cookie
    createCookie("ihtfontsize", currentTextSize, 90);
}

function SetContentStyles() {
    var div = document.getElementById('divContent');
    if (div != null) {
        div.style.fontSize = currentTextSize + 'em';
        div.style.lineHeight = currentLineHeight + 'em';
    }
}


//setup StringBuilder
function StringBuilder() { this._buffer = []; };
StringBuilder.prototype.Append = function(str) { this._buffer[this._buffer.length] = str; };
StringBuilder.prototype.ToString = function() { return this._buffer.join(''); }; 

//DETERMINE IF ON SECURE SERVER
var cURL = location.href;
var protocol = cURL.slice(0, cURL.indexOf(":")) + "://"; //grab the beginning of URL
//Used to change HTTP to HTTPS for images, js, address etc.
if (protocol.search(/s/) > 0) {
    protocol = protocol + "secure." //if https, add "secure. to address"
}

var url = "http://walmartstores.com/"
var hdr = ''
var ftr = ''
var breadcrumb = ''
var breadcrumbURL = ''
var active = ' class="active" '

var homeObj = ''

var aboutObj = ''
var factsObj = ''
var careersObj = ''
var investorsObj = ''
var suppliersObj = ''
var diversityObj = ''
var communityObj = ''
var healthObj = ''
var sustainabilityObj = ''

//BUILD NAVIGATION ADDRESS
var aboutURL = url + 'AboutUs'
var factsURL = url + 'FactsNews'
var careersURL = url + 'Careers'
var investorsURL = 'http://investors.walmartstores.com/'
var suppliersURL = url + 'Suppliers'
var diversityURL = url + 'Diversity'
var communityURL = url + 'CommunityGiving'
var healthURL = url + 'HealthWellness'
var sustainabilityURL = url + 'Sustainability'

var home = 'Home'
var about = 'About Us'
var facts = 'Facts &amp; News'
var careers = 'Careers'
var investors = 'Investors'
var suppliers = 'Suppliers'
var diversity = 'Diversity'
var community = 'Community &amp; Giving'
var health = 'Health &amp; Wellness'
var sustainability = 'Sustainability'


function header(menu, tHideBreadcrumb) {
    switch (menu) {
        case 'home':
            homeObj = active
            breadcrumbURL = url
            break
        case 'about':
        case 'AboutUs':
            aboutObj = active
            breadcrumb = about
            breadcrumbURL = aboutURL
            break
        case 'facts':
        case 'FactsNews':
            factsObj = active
            breadcrumb = facts
            breadcrumbURL = factsURL
            break
        case 'careers':
        case 'Careers':
            careersObj = active
            breadcrumb = careers
            breadcrumbURL = careersURL
            break
        case 'investors':
        case 'Investors':
            investorsObj = active
            breadcrumb = investors
            breadcrumbURL = investorsURL
            break
        case 'suppliers':
        case 'Suppliers':
            suppliersObj = active
            breadcrumb = suppliers
            breadcrumbURL = suppliersURL
            break
        case 'diversity':
        case 'Diversity':
            diversityObj = active
            breadcrumb = diversity
            breadcrumbURL = diversityURL
            break
        case 'community':
        case 'CommunityGiving':
            communityObj = active
            breadcrumb = community
            breadcrumbURL = communityURL
            break
        case 'health':
        case 'HealthWellness':
            healthObj = active
            breadcrumb = health
            breadcrumbURL = healthURL
            break
        case 'sustainability':
        case 'Sustainability':
            sustainabilityObj = active
            breadcrumb = sustainability
            breadcrumbURL = sustainabilityURL
    }
    //omniture(breadcrumb);

    hdr += '	<div class="header">'
    hdr += '		<div class="headerLogo">'
    hdr += '			<a href="' + url + '" style="text-decoration: none;"><img src="' + protocol + 'rfi-walmart.com/wmstores/images/layout/logo.gif" border="0" alt="Walmart" /></a>'
    hdr += '		</div>'
    hdr += '		<div class="globalNav">'
    hdr += '			<div class="globalLinks">'
    hdr += '				<a href=" http://www.walmart.com/cservice/ca_storefinder.gsp?adid=1500000000000006858130" target="blank"> Store Finder</a> | <a href="' + url + '317.aspx"> FAQs</a> | <a href="' + url + '7663.aspx"> Contact Us</a> '
    hdr += '			</div>'
    hdr += '			<div class="searchForm">'
    hdr += '				<form name="getString" class="searchBox">'
    hdr += '					<input id="tbSearch" type="text" class="searchText" value="Search" type="text"  name="getString" onkeypress="return submitSearchBox(event);" onfocus="clearSearchText(this);" onblur="resetSearchText(this);" />'
    hdr += '					<img class="go" style="cursor:hand;" src="' + protocol + 'rfi-walmart.com/wmstores/images/layout/search_button.png" onclick="goSearch();"/>'
    hdr += '				</form>'
    hdr += '			</div>'
    hdr += '		</div>'
    hdr += '	</div>'
    hdr += '   <div class="primaryNav">'
    hdr += '	<div class="primaryNavContent">'
    hdr += '		<ul class="primaryNavList">	'
    hdr += '		<li' + homeObj + '><a href="' + url + '">' + home + '</a></li>'
    hdr += '		<li' + aboutObj + '><a href="' + aboutURL + '">' + about + '</a></li>'
    hdr += '		<li' + factsObj + '><a href="' + factsURL + '">' + facts + '</a></li>'
    hdr += '		<li' + healthObj + '><a href="' + healthURL + '">' + health + '</a></li>'
    hdr += '		<li' + careersObj + '><a href="' + careersURL + '">' + careers + '</a></li>'
    hdr += '		<li' + communityObj + '><a href="' + communityURL + '">' + community + '</a></li>'
    hdr += '		<li' + diversityObj + '><a href="' + diversityURL + '">' + diversity + '</a></li>'
    hdr += '		<li' + sustainabilityObj + '><a href="' + sustainabilityURL + '">' + sustainability + '</a></li>'
    hdr += '		<li' + investorsObj + '><a href="' + investorsURL + '">' + investors + '</a></li>'
    hdr += '		<li' + suppliersObj + '><a href="' + suppliersURL + '">' + suppliers + '</a></li>'
    hdr += '		</ul>'
    hdr += '	</div>'
    hdr += '</div>'
    if (tHideBreadcrumb != true) {
        hdr += '<div class="breadcrumb">'
        hdr += '	<a href="' + url + '">' + home + '</a> > <a href="' + breadcrumbURL + '">' + breadcrumb + '</a> > ' + document.title
        hdr += '</div>	'
    }
    document.write(hdr);
}
function footer() {
    ftr += '	<div class="footer" title="Footer">'
    ftr += '		<div class="footerContent">'
    ftr += '	             <p>'
    ftr += '						<a href="http://www.walmart.com/?adid=1500000000000006858130" title="Shop at Walmart.com">Shop Walmart.com</a> | '
    ftr += '						<a href="http://www.samsclub.com/?pid=WalmartStores" title="Shop at Samsclub.com">Shop Samsclub.com</a> | '
    ftr += '						<a href="' + url + 'AboutUs/246.aspx" title="International">International</a> | '
    ftr += '						<a href="' + url + 'Careers/7745.aspx" title="Jobs">Jobs</a> | '
    ftr += '						<a href="' + url + 'Video/" title="Video Center">Video Center</a> | '
    ftr += '						<a href="' + url + 'Recalls/" title="Recalls">Recalls</a> | '
    ftr += '						<a href="' + url + '7662.aspx" title="Other Sites">Other Sites</a> | '
    ftr += '						<a href="' + url + 'Twitter" title="Twitter">Twitter</a> | '
    ftr += '						<a href="' + url + '7737.aspx" title="Return Policy">Return Policy</a> | '
    ftr += '						<a href="' + url + '7735.aspx" title="Privacy Policy">Privacy Policy</a> | '
    ftr += '                        <a href="' + url + 'SiteMap.aspx" title="Site Map">Site Map</a> | '
    ftr += '                        <a href="' + url + '7736.aspx" title="Terms of Use">Terms of Use</a> '
    ftr += '				</p>'
    ftr += '		</div>'
    ftr += '	</div>'                      
    document.write(ftr);
}

//search engine
function gosearch(str) {
    alert(str);
    alert(document.getElementById('tbSearch'));
    var str = escape(str);
    location.href = url + "Search/?q=" + str.replace("%20", "+");
}

//make title case
function toTitleCase(str) {
    return str.replace(/\w+/g, function(txt) { return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase(); });
}
//fix ie flicker - used for buttons
try {
    document.execCommand("BackgroundImageCache", false, true);
} catch (err) { }

//****************** Search engine ******************

function RunSearch(sTerm) {
    var searchTerm = escape(sTerm);
    searchTerm = searchTerm.replace("<", "").replace("&#", "").replace("%20", "+");
    if (searchTerm == "Search" || searchTerm == "") {
        location.href = url + "Search/";
    } else {
        location.href = url + "Search/?q=" + searchTerm;
    }
}

function goSearch() {
    RunSearch(document.getElementById('tbSearch').value);
}

function submitSearchBox(e) {
    var keynum = GetEventKeyNum(e);

    // don't allow '<' in textbox
    if (keynum == 60) return false;

    // don't allow '&#' in textbox
    // check and see if current character is a '#'
    if (keynum == 35) {
        // check and see if previous character is a '&'
        var temp = document.getElementById('tbSearch').value;
        var ampTest = temp.endsWith("&");
        if (ampTest === true) return false;
    }

    if (keynum == 13) {
        goSearch();
        return false;
    }
}

function clearSearchText(txtSearch) {
    if (txtSearch.value == 'Search') txtSearch.value = "";
}

function resetSearchText(txtSearch) {
    var trimmedText = txtSearch.value.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
    if (trimmedText == '') txtSearch.value = 'Search';
}

function GetEventKeyNum(e) {
    var keynum;
    // IE
    if (window.event) keynum = e.keyCode;
    // Netscape/Firefox/Opera
    else if (e.which) keynum = e.which;
    return keynum;
}