// List of sites from where the user can come to choose a Dove Website for country/language

// If referrer is one of these sites, then display the Dove Website selection
var listSitesReferrer = new Array(
	"http://www.doveworld.ch",
	"http://doveworld.ch",
	"http://www.dove.com",
	"http://dove.com",
	"http://www.dove.com.es",
	"http://dove.com.es",
	"http://www.mydove.com.au",
	"http://www.dove.at",
	"http://www.dovebody.be",
	"http://www.dovebeauty.be",
	"http://www.dove.ca",
	"http://www.dove.cl",
	"http://www.dovekosmetika.cz",
	"http://www.dove.de",
	"http://www.dove.hu",
	"http://www.mydove.jp",
	"http://www.dove.co.kr",
	"http://www.mydove.co.nz",
	"http://www.idoveyou.pl",
	"http://www.dove.pt",
	"http://www.dove.ru",
	"http://www.dove.co.uk",
	"http://dove.co.uk",
	"http://dovesurvey.co.uk",
	"http://www.dovesurvey.co.uk",
	"http://campaignforrealbeauty.co.uk",
	"http://www.campaignforrealbeauty.co.uk",
	"http://www.dove.us",
	"http://dove.com",
	"http://mydove.com.au",
	"http://dove.at",
	"http://dovebody.be",
	"http://dovebeauty.be",
	"http://dove.ca",
	"http://dove.cl",
	"http://dovekosmetika.cz",
	"http://dove.de",
	"http://dove.hu",
	"http://mydove.jp",
	"http://dove.co.kr",
	"http://mydove.co.nz",
	"http://idoveyou.pl",
	"http://dove.pt",
	"http://dove.ru",
	"http://dove.us",
	"http://www.dove.ro",
	"http://dove.ro",
	"http://dove.se",
	"http://www.dove.se",
	"http://www.dovebeauty.nl",
	"http://dovebeauty.nl",
	"http://www.dovekozmetika.sk",
	"http://dovekozmetika.sk",
	"http://www.doveproge.de",
	"http://www.idoveyou.pl",
	"http://idoveyou.pl",
	"http://www.dovebg.com",
	"http://dove.bg",
	"http://www.dove-srbija.com",
	"http://dove.srbija.com",
	"http://dove.com.ua",
	"http://www.dove.com.ua"
	
);

// List of extensions of websites from which user won't see the Dove Website selection page
// but will be automatically redirected to the corresponding site or language selection (if more than one)

var listDoveExtensions = new Array("BE", "DE", "UK", "EE", "ES", "NL", "CH", "RO", "PT", "AT", "SE", "HU", "CZ", "SK", "SE","PL","RU","RS","BG", "LT", "LV", "UA");

// List of corresponding ISO codes from website extension if this website extension is not ISO
var listDoveExtensionsCorrespondance = {
"UK": [{"ISOcode": 'GB'}]
}

// List of international language not directly associated to a country and association to country
var listLanguagesWithoutCountry = {
"AT": [{"languageCountry": 'AT-DE'}],
"DE": [{"languageCountry": 'DE-DE'}],
"FR": [{"languageCountry": 'FR-FR'}],
"ES": [{"languageCountry": 'ES-ES'}],
"EN": [{"languageCountry": 'EN-US'}],
"ET": [{"languageCountry": 'EE-ET'}],
"IT": [{"languageCountry": 'IT-IT'}],
"JA": [{"languageCountry": 'JP-JP'}],
"RU": [{"languageCountry": 'RU-RU'}],
"CS": [{"languageCountry": 'CS-CZ'}],
"HU": [{"languageCountry": 'HU-HU'}],
"KO": [{"languageCountry": 'KR-KR'}],
"NL": [{"languageCountry": 'NL-NL'}],
"PL": [{"languageCountry": 'PL-PO'}],
"PT": [{"languageCountry": 'PT-PT'}],
"RO": [{"languageCountry": 'RO-RO'}],
"SE": [{"languageCountry": 'SE-SV'}],
"SK": [{"languageCountry": 'SK-SK'}],
"FI": [{"languageCountry": 'FI-FI'}],
"DK": [{"languageCountry": 'DK-DA'}],

"D1": [{"languageCountry": 'DE-DE'}],
"RU": [{"languageCountry": 'RU-RU'}],
"GR": [{"languageCountry": 'GR-EL'}],
"BG": [{"languageCountry": 'BG-BG'}],
"RS": [{"languageCountry": 'RS-SR'}],

"UK": [{"languageCountry": 'UA-RU'}]};

// List of countries/language the user can choose from
var countryDB = {
"AU": [{"languageName": 'English', "url": 'http://www.mydove.com.au', "language": 'EN'}],
"AT": [{"languageName": 'German', "url": 'http://www.dove.at/at_de/at_de/index.html', "language": 'DE'}],
"BE": [{"languageName": 'Fran&ccedil;ais', "url": 'http://www.dovebeauty.be/be_fr/be_fr/index.html', "language": 'FR'},
			 {"languageName": 'Nederlands', "url": 'http://www.dovebeauty.be/be_nl/be_nl/index.html', "language": 'NL'}],
"CA": [{"languageName": 'English', "url": 'http://www.dove.ca/en/', "language": 'EN'},
			 {"languageName": 'Fran&ccedil;ais', "url": 'http://www.dove.ca/fr/', "language": 'FR'}],
"CH": [{"languageName": 'Fran&ccedil;ais', "url": 'http://www.doveworld.ch/ch_fr/ch_fr/index.html', "language": 'FR'},
			 {"languageName": 'German', "url": 'http://www.doveworld.ch/ch_de/ch_de/index.html', "language": 'DE'}],
"CL": [{"languageName": 'Spanish', "url": 'http://www.dove.cl', "language": 'ES'}],
"CZ": [{"languageName": 'Czech', "url": 'http://www.dovekosmetika.cz/cz_cs/cz_cs/index.html', "language": 'CS'}],
"DE": [{"languageName": 'German', "url": 'http://www.dove.de/de_de/de_de/index.html', "language": 'DE'}],
"EE": [{"languageName": 'Estonian', "url": 'http://www.dove.ee/ee_et/ee_et/index.html', "language": 'ET'}],
"GR": [{"languageName": 'Elina', "url": 'http://www.dove.com/gr_el/gr_el/index.html', "language": 'EL'}],

"DK": [{"languageName": 'Danish', "url": 'http://vind-med-dove.dk', "language": 'DK'}],
"ES": [{"languageName": 'Spanish', "url": 'http://www.dove.com.es/es_es/es_es/index.html', "language": 'ES'}],
"FR": [{"languageName": 'French', "url": 'http://www.dove.com/fr_fr/fr_fr/index.html', "language": 'FR'}],
"HU": [{"languageName": 'Hungarian', "url": 'http://www.dove.hu', "language": 'HU'}],
"IE": [{"languageName": 'English', "url": 'http://www.dove.com/ie_en/ie_en/index.html', "language": 'EN'}],
"IT": [{"languageName": 'Italian', "url": 'http://www.dove.com/it_it/it_it/index.html', "language": 'IT'}],
"JP": [{"languageName": 'Japanese', "url": 'http://www.mydove.jp', "language": 'JP'}],
"KR": [{"languageName": 'Korean', "url": 'http://www.dove.co.kr', "language": 'KR'}],
"LV": [{"languageName": 'Latvia', "url": 'http://www.dove.lv/lv_lv/lv_lv/index.html', "language": 'LV'}],
"LT": [{"languageName": 'Lithuanian', "url": 'http://www.dove.lt/lt_lt/lt_lt/index.html', "language": 'LT'}],
"MK": [{"languageName": 'Macedonia', "url": 'http://www.dove.com/mk_mk/mk_mk/index.html', "language": 'MK'}],
"NL": [{"languageName": 'Flemish', "url": 'http://www.dove.com/nl_nl/nl_nl/index.html', "language": 'NL'}],
"NZ": [{"languageName": 'English', "url": 'http://www.mydove.co.nz', "language": 'EN'}],
"PL": [{"languageName": 'Polish', "url": 'http://www.idoveyou.pl', "language": 'PO'}],
"PT": [{"languageName": 'Portugueses', "url": 'http://www.dove.pt/pt_pt/pt_pt/index.html', "language": 'PT'}],
"RU": [{"languageName": 'Russian', "url": 'http://www.dove.ru/ru_ru/ru_ru/index.html', "language": 'RU'}],
"SK": [{"languageName": 'Slovak', "url": 'http://www.dovekozmetika.sk/sk_sk/sk_sk/index.html', "language": 'SK'}],
"UA": [{"languageName": 'Ukraine', "url": 'http://www.dove.com.ua/ua_uk/ua_uk/index.html', "language": 'RU'}],
"RO": [{"languageName": 'Romanian', "url": 'http://www.dove.ro/ro_ro/ro_ro/index.html', "language": 'RO'}],
"FI": [{"languageName": 'Finish', "url": 'http://www.dove.com/fi_fi/fi_fi/index.html', "language": 'FI'}],
"DK": [{"languageName": 'Danish', "url": 'http://www.dove.com/dk_da/dk_da/index.html', "language": 'DA'}],
"SE": [{"languageName": 'Swedish', "url": 'http://www.dove.se/se_sv/se_sv/index.html', "language": 'SV'}],
"GB": [{"languageName": 'English', "url": 'http://www.dove.co.uk', "language": 'EN'}],

"D1": [{"languageName": 'German', "url": 'http://www.dove.de/de_de/de_de/index.html?prm_content=products/products_overview_proage.xml&prm_asaction=cat_x_proage', "language": 'DE'}],

"BG": [{"languageName": 'Bulgarian', "url": 'http://www.dovebg.com/bg_bg/bg_bg/index.html', "language": 'BG'}],
"RS": [{"languageName": 'Serbian', "url": 'http://www.dove-srbija.com/rs_sr/rs_sr/index.html', "language": 'SR'}],


"US": [{"languageName": 'English', "url": 'http://www.dove.us', "language": 'EN'}]};

//List of countries used for selection dropdown, should be in alphabetical order
var countries = new Array(
"AU", "Australia",
"AT", "Austria",
"BE", "Belgium",
"BG", "Bulgarian",
"CA", "Canada",
"CL", "Chile",
"CZ", "Czech Republic",
"DK", "Denmark",
"EE", "Estonia",
"FI", "Finland",
"FR", "France",
"DE", "Germany",
"GR", "Greece",
"HU", "Hungary",
"IT", "Italy",
"IE", "Ireland",
"JP", "Japan",
"KR", "Korea",
"LV", "Latvia",
"LT", "Lithuania",
"MK", "Macedonia",
"NL", "Netherlands",
"NZ", "New Zealand",
"PL", "Poland",
"PT", "Portugal",
"RO", "Romania",
"RU", "Russia",
"RS", "Serbia",
"SK", "Slovakia",
"ES", "Spain",
"SE", "Sweden",
"CH", "Switzerland",
"UA", "Ukraine",
"GB", "United Kingdom",
"US", "United States"
);

// If user language is one of these items, use it instead of geoips
var listUserLanguages = new Array("EN-IE", "EN-US","EN-AU","EN-BZ","EN-GB","EN-JM","EN-CA","EN-NZ","EN-PH","EN-ZA","EN-TT","EN-ZW");

// Special hosts and urls to redirect
var listSpecialUrls = new Array(
"http://dovenetzwerk.com","PROAGE",
"http://www.dovenetzwerk.com","PROAGE",
"http://dove-netzwerk.com","PROAGE",
"http://www.dove-netzwerk.com","PROAGE",
"http://doveproagenetzwerk.com","PROAGE",
"http://www.proagenetzwerk.com","PROAGE",
"http://www.doveproagenetzwerk.com","PROAGE",
"http://dove-proage-netzwerk.com","PROAGE",
"http://www.dove-proage-netzwerk.com","PROAGE",
"http://proagenetzwerk.com","PROAGE",
"http://proage-netzwerk.com","PROAGE",
"http://www.proage-netzwerk.com","PROAGE",
"http://proagenetwork.net","PROAGE",
"http://www.proagenetwork.net","PROAGE",
"http://proage-network.net","PROAGE",
"http://www.proage-network.net","PROAGE",
"http://proagenetzwerk.net","PROAGE",
"http://www.proagenetzwerk.net","PROAGE",
"http://proage-netzwerk.net","PROAGE",
"http://www.proage-netzwerk.net","PROAGE",
"http://proagenetwork.de","PROAGE",
"http://www.proagenetwork.de","PROAGE",
"http://proage-network.de","PROAGE",
"http://www.proage-network.de","PROAGE",
"http://www.dovespa.de","SPA",
"http://dovespa.de","SPA",
"http://www.dove-spa.de","SPA",
"http://dove-spa.de","SPA",
"http://www.dove.com/imagetool","IMAGETOOL",
"http://www.dove.com/backtoschool","BACKSCHOOL"
);

var listSpecialUrlTargets = {
  "PROAGE":     [{"target": 'http://www.dove.de/de_de/de_de/proage_netzwerk/index.html', "language": 'DE'}],
  "SPA":        [{"target": 'http://www.dove.de/de_de/de_de/index.html?prm_content=specials/spa/start.xml', "language": 'DE'}],
  "IMAGETOOL":  [{"target": 'http://www.dove.us/#/cfrb/girlsonly/image_manipulation.aspx', "language": 'US'}],
  "BACKSCHOOL": [{"target": 'http://content.dove.us/redirects/backtoschool/', "language": 'US'}]
};

// Function called when the user selects a country in the select box of the landing page
 	function getCountry(countryCode, redirect) {
	      if(countryCode) {
			if (countryDB[countryCode].length == 1) {
				// Automatically Redirects to URL because only one language
				setCAOP(countryDB[countryCode][0].url,countryDB[countryCode][0].language+"-"+countryCode, redirect, true);
			} else {
		        // More than one language - display selection
				var newLayerContent = "";
		        if (document.getElementById) {
		          for(i=0; i < countryDB[countryCode].length; i++) {
		            newLayerContent += '<a href="javascript:setCAOP(\'' + countryDB[countryCode][i].url + '\',\''+countryDB[countryCode][i].language+'-'+countryCode+'\',' + redirect + ',true)" class="navLink"><span style="float:left;"><img src="tracking/redirection/common/_img/1x1_trans.gif"></span><span style="float:left;">' + countryDB[countryCode][i].languageName + '</span></a>';
		          }
			      document.getElementById("navigationLayer").innerHTML = newLayerContent;
			    }
			document.getElementById("navigationLayer").innerHTML = newLayerContent;
			}
	      }
		  if (countryCode==0) {
				if (document.getElementById) {
					document.getElementById("navigationLayer").innerHTML = "";
				}
		  }
	}

// Saves the user choice into a cookie for next use and then redirects to the choosen website
		function setCAOP(url, countryLanguage, redirect, cookie) {
	  	if (cookie) {
	  	  SetCookie('eudovelandingselection',countryLanguage);
	  	}
			if (redirect) {
					document.location.href=url;
			}
		}
		
// Stores cookie value
		function SetCookie (name, value) {
			var argv=SetCookie.arguments;
			var argc=SetCookie.arguments.length;
			document.cookie=name+"="+escape(value)+"; expires=Mon,01-Jan-35 00:00:01 GMT; path=/";
		}

// Get cookie value		
		function GetCookie (name) {
			var arg=name+"=";
			var alen=arg.length;
			var clen=document.cookie.length;
			var i=0;
			while (i<clen) {
				var j=i+alen;
				if (document.cookie.substring(i, j)==arg) {
					return getCookieVal (j);
		        }
				i=document.cookie.indexOf(" ",i)+1;
		        if (i==0) {
					break;
				}
			}
			return null;
		}
	
// Get cookie value	(used in function GetCookie)
		function getCookieVal(offset) {
		   endstr=document.cookie.indexOf(";",offset);
		   if (endstr == -1) {endstr=document.cookie.length;}
		   return unescape(document.cookie.substring(offset,endstr));
		}

// Get the URL of a website from the country-language corresponding code
	 	function getURLFromCountryLanguage(code) {
			var url = "";
		  if(code && (code.length == 5) && (code.charAt(2) == "-")) {
		    var language = code.substring(0,2);
				var country = code.substring(3,5);

				if(countryDB[country] != null) {
					if (countryDB[country].length == 1) {
						url = countryDB[country][0].url;
					} else {
						for(i=0; i < countryDB[country].length; i++) {
							if (countryDB[country][i].language == language) {
								url = countryDB[country][i].url;
							}
					  }
					}
				}
			}
			return url;
		}

// Get a hostname from URL (eg : "http://www.dove.com" from "http://www.dove.com/test/clej/test.html")
	function getHostFromURL (url) {
		url += "";
		url = url.toLowerCase();
		var urlSpl = url.split("/");

		var hostName = "";
		if (urlSpl[0] == "http:" && urlSpl[1] == "") {
			hostName = "http://"+urlSpl[2];
		}
		return hostName
	}

// Get the value of a parameter with the given name
	function getParameter ( queryString, parameterName ) {
	   var parameterName = parameterName + "=";
	   if ( queryString.length > 0 ) {
	     begin = queryString.indexOf ( parameterName );
	     if ( begin != -1 ) {
	        begin += parameterName.length;
	        end = queryString.indexOf ( "&" , begin );
		      if ( end == -1 ) {
		         end = queryString.length
		      }
		      return unescape ( queryString.substring ( begin, end ) );
		   }
	   }
     return "";
	}

// Get website extension from hostname (eg : "http://www.dove.de" will return "DE")
	function getExtensionFromHost (theHost) {
		
		theHost += "";
		theHost = theHost.toUpperCase();
		var hostSpl = theHost.split(".");

		// Workaround for BG and RS
		if (theHost.indexOf('DOVEBG.COM') > -1){
			return "BG";
		} else {
			if (theHost.indexOf('DOVE-SRBIJA.COM') > -1){
				return "RS";
			} else {
				return hostSpl[hostSpl.length-1];
			}
		}
	}

// Returns the ISOcode corresponding to a website extension ("DE" will return "DE", but "UK" will return "GB")
	function getISOcodeFromExtension (extension) {
		if (eval(listDoveExtensionsCorrespondance[extension]) != null) {
			extension = listDoveExtensionsCorrespondance[extension][0].ISOcode;
		}
	  return extension;
	}

// Returns true if user language sholud be used instead of geoips
  function isSpecialUserLanguage (aUserLanguage) {
		for (var i=0;i<listUserLanguages.length;i++) {
			if(listUserLanguages[i] == aUserLanguage) {
				return true;
			}
		}
		return false;
  }

// Returns new url if url or host matches else ""
  function getSpecialUrl (aUrl) {
		for (var i=0;i<listSpecialUrls.length;i+=2) {
			if(aUrl.match("^"+listSpecialUrls[i]) == listSpecialUrls[i]) {
				return listSpecialUrls[i+1];
			}
		}
		return "";
  }

  