// ******************************************************************************************
// BEGIN Selected Market functions
// ******************************************************************************************
// Check for the existence of a Selected Market cookie.
// If it exists, redirect.
// If it does not exist, do nothing. Links will allow setting/navigation to selected market.
/////////////////////////////////////////////////////////////////////////////////////////////


// VARIABLES //

// Selected Market cookie.
var marketCookie = 'MarketPref';

// Page locations.
var marketLocs = new Object();
marketLocs["USen"] = '/us/en/home.dhtml';
marketLocs["USes"] = '/us/es/home.dhtml';
marketLocs["USzh"] = '/us/zh/home.dhtml';
marketLocs["PRes"] = '/pr/es/home.dhtml';
marketLocs["CAen"] = '/ca/en/home.dhtml';
marketLocs["CAfr"] = '/ca/fr/home.dhtml';
marketLocs["CAzh"] = '/ca/zh/home.dhtml';
marketLocs["HKen"] = '/hk/en/home.dhtml';
marketLocs["HKzh"] = '/hk/zh/home.dhtml';
marketLocs["TWen"] = '/tw/en/home.dhtml';
marketLocs["TWzh"] = '/tw/zh/home.dhtml';
marketLocs["AUen"] = '/au/en/home.dhtml';
marketLocs["NZen"] = '/nz/en/home.dhtml';

// Misc. globals.
var expWeeks = 24; 
var cookieExp = new Date((new Date()).getTime() + expWeeks*604800000); // weeks times 7*24*60*60*1000
var domainName = 'isagenix.com';
var marketPath = '/';


// FUNCTIONS //

// Set the cookie to the version passed in setPref, then redirect to that version.
function setMarket(setPref) {
	setCookie(marketCookie, setPref, cookieExp, marketPath, domainName, null);
	window.location = marketLocs[setPref];
}

// Check to see if we're on the site specified as the parameter. Redirect if not.
// Parameter: curPref = passed value for current site version. Check valid values in "marketCookie".
function checkMarket() {
	var cookieVal = getCookie(marketCookie);
	
	if (cookieVal != null && cookieVal != '') {
			window.location = marketLocs[cookieVal]; // cookieVal should always contain the desired default site.
	} // No cookie set, don't set one unless directed by user.
}

// name - name of the cookie
// [path] - path of the cookie (must be same as path used to create cookie)
// [domain] - domain of the cookie (must be same as domain used to create cookie)
function resetMarket() {
  if (getCookie(marketCookie)) {
    document.cookie = marketCookie + "=" + 
    "; path=" + marketPath +
    "; domain=" + domainName +
    "; expires=Thu, 01-Jan-1970 00:00:00 GMT";
  }
	window.location = 'country_selector.dhtml';
}


// ******************************************************************************************
// END Selected Market functions
// ******************************************************************************************
