
























                                                  


























     

 






















      













var isLandingPage = false;
try {
	if (landingPage) isLandingPage = true;
} catch (e) {
}

var useFriendlyURL = false;
if (location.href.indexOf("content") != -1) {
	useFriendlyURL = true;
}

var pattern = location.href;
pattern = pattern.substring(7);
pattern = pattern.substring(pattern.indexOf('/'));
if (pattern.indexOf('?') != -1) {
	pattern = pattern.substring(0, pattern.indexOf('?'));	
}
var friendlyURLReplacementPattern = pattern;

countryCookieName = "CountrySelect";
languageCookieName = "LanguageSelect";

expireDate = new Date();
expireDate.setTime(new Date().getTime() + (5000 * 24 * 60 * 60 * 1000));
cookiePath = "/";

countries = new Object();

	countries.angola = new Object();
	countries.angola.name = "Angola";
	countries.angola.languages = "en fr pt";
	countries.angola.landingPage = "page570";
	countries.angola.friendlyURL = "/dstvafrica/content/en/angola/angola_home";

	countries.benin = new Object();
	countries.benin.name = "Benin";
	countries.benin.languages = "en";
	countries.benin.landingPage = "page5969";
	countries.benin.friendlyURL = "/dstvafrica/content/en/benin/benin_home";

	countries.botswana = new Object();
	countries.botswana.name = "Botswana";
	countries.botswana.languages = "en";
	countries.botswana.landingPage = "page3732";
	countries.botswana.friendlyURL = "/dstvafrica/content/en/botswana/botswana_home";

	countries.burkina_faso = new Object();
	countries.burkina_faso.name = "Burkina Faso";
	countries.burkina_faso.languages = "en";
	countries.burkina_faso.landingPage = "page6201";
	countries.burkina_faso.friendlyURL = "/dstvafrica/content/en/burkina_faso/burkina_faso_home";

	countries.burundi = new Object();
	countries.burundi.name = "Burundi";
	countries.burundi.languages = "en fr";
	countries.burundi.landingPage = "page5426";
	countries.burundi.friendlyURL = "/dstvafrica/content/en/burundi/burundi_home";

	countries.cameroon = new Object();
	countries.cameroon.name = "Cameroon";
	countries.cameroon.languages = "en";
	countries.cameroon.landingPage = "page6146";
	countries.cameroon.friendlyURL = "/dstvafrica/content/en/cameroon/cameroon_home";

	countries.cape_verde = new Object();
	countries.cape_verde.name = "Cape Verde";
	countries.cape_verde.languages = "en";
	countries.cape_verde.landingPage = "page4710";
	countries.cape_verde.friendlyURL = "/dstvafrica/content/en/cape_verde/cape_verde_home";

	countries.central_african_republic = new Object();
	countries.central_african_republic.name = "Central African Republic";
	countries.central_african_republic.languages = "en";
	countries.central_african_republic.landingPage = "page5863";
	countries.central_african_republic.friendlyURL = "/dstvafrica/content/en/central_african_republic/central_african_republic_home";

	countries.chad = new Object();
	countries.chad.name = "Chad";
	countries.chad.languages = "en";
	countries.chad.landingPage = "page6068";
	countries.chad.friendlyURL = "/dstvafrica/content/en/chad/chad_home";

	countries.comores = new Object();
	countries.comores.name = "Comores";
	countries.comores.languages = "en";
	countries.comores.landingPage = "page3934";
	countries.comores.friendlyURL = "/dstvafrica/content/en/comores/comores_home";

	countries.congo = new Object();
	countries.congo.name = "Congo";
	countries.congo.languages = "en";
	countries.congo.landingPage = "page7617";
	countries.congo.friendlyURL = "/dstvafrica/content/en/congo/congo_home";

	countries.cote_divoire = new Object();
	countries.cote_divoire.name = "Cote d'Ivoire";
	countries.cote_divoire.languages = "en";
	countries.cote_divoire.landingPage = "page6560";
	countries.cote_divoire.friendlyURL = "/dstvafrica/content/en/cote_divoire/cote_divoire_home";

	countries.djibouti = new Object();
	countries.djibouti.name = "Djibouti";
	countries.djibouti.languages = "en";
	countries.djibouti.landingPage = "page6473";
	countries.djibouti.friendlyURL = "/dstvafrica/content/en/djibouti/djibouti_home";

	countries.drc = new Object();
	countries.drc.name = "DRC";
	countries.drc.languages = "en fr";
	countries.drc.landingPage = "page5276";
	countries.drc.friendlyURL = "/dstvafrica/content/en/drc/drc_home";

	countries.equatorial_guinea = new Object();
	countries.equatorial_guinea.name = "Equatorial Guinea";
	countries.equatorial_guinea.languages = "en";
	countries.equatorial_guinea.landingPage = "page6324";
	countries.equatorial_guinea.friendlyURL = "/dstvafrica/content/en/equatorial_guinea/equatorial_guinea_home";

	countries.eritrea = new Object();
	countries.eritrea.name = "Eritrea";
	countries.eritrea.languages = "en";
	countries.eritrea.landingPage = "page6208";
	countries.eritrea.friendlyURL = "/dstvafrica/content/en/eritrea/eritrea_home";

	countries.ethiopia = new Object();
	countries.ethiopia.name = "Ethiopia";
	countries.ethiopia.languages = "en";
	countries.ethiopia.landingPage = "page4588";
	countries.ethiopia.friendlyURL = "/dstvafrica/content/en/ethiopia/ethiopia_home";

	countries.gabon = new Object();
	countries.gabon.name = "Gabon";
	countries.gabon.languages = "en";
	countries.gabon.landingPage = "page6009";
	countries.gabon.friendlyURL = "/dstvafrica/content/en/gabon/gabon_home";

	countries.gambia = new Object();
	countries.gambia.name = "Gambia";
	countries.gambia.languages = "en";
	countries.gambia.landingPage = "page4040";
	countries.gambia.friendlyURL = "/dstvafrica/content/en/gambia/gambia_home";

	countries.ghana = new Object();
	countries.ghana.name = "Ghana";
	countries.ghana.languages = "en";
	countries.ghana.landingPage = "page4498";
	countries.ghana.friendlyURL = "/dstvafrica/content/en/ghana/ghana_home";

	countries.guniea_ = new Object();
	countries.guniea_.name = "Guinea ";
	countries.guniea_.languages = "en";
	countries.guniea_.landingPage = "page6643";
	countries.guniea_.friendlyURL = "/dstvafrica/content/en/guinea/guinea_home";

	countries.guinea_bissau = new Object();
	countries.guinea_bissau.name = "Guinea Bissau ";
	countries.guinea_bissau.languages = "en";
	countries.guinea_bissau.landingPage = "page6645";
	countries.guinea_bissau.friendlyURL = "/dstvafrica/content/en/guinea_bissau/guinea_bissau_home";

	countries.kenya = new Object();
	countries.kenya.name = "Kenya";
	countries.kenya.languages = "en";
	countries.kenya.landingPage = "page4213";
	countries.kenya.friendlyURL = "/dstvafrica/content/en/kenya/kenya_home";

	countries.liberia = new Object();
	countries.liberia.name = "Liberia ";
	countries.liberia.languages = "en";
	countries.liberia.landingPage = "page6647";
	countries.liberia.friendlyURL = "/dstvafrica/content/en/liberia/liberia_home";

	countries.madagascar_ = new Object();
	countries.madagascar_.name = "Madagascar ";
	countries.madagascar_.languages = "en";
	countries.madagascar_.landingPage = "page6649";
	countries.madagascar_.friendlyURL = "/dstvafrica/content/en/madagascar/madagascar_home";

	countries.malawi = new Object();
	countries.malawi.name = "Malawi";
	countries.malawi.languages = "en";
	countries.malawi.landingPage = "page5502";
	countries.malawi.friendlyURL = "/dstvafrica/content/en/malawi/malawi_home";

	countries.mali_ = new Object();
	countries.mali_.name = "Mali ";
	countries.mali_.languages = "en";
	countries.mali_.landingPage = "page6651";
	countries.mali_.friendlyURL = "/dstvafrica/content/en/mali/mali_home";

	countries.mauritania_ = new Object();
	countries.mauritania_.name = "Mauritania ";
	countries.mauritania_.languages = "en";
	countries.mauritania_.landingPage = "page7129";
	countries.mauritania_.friendlyURL = "/dstvafrica/content/en/mauritania/mauritania_home";

	countries.mauritius = new Object();
	countries.mauritius.name = "Mauritius";
	countries.mauritius.languages = "en";
	countries.mauritius.landingPage = "page4761";
	countries.mauritius.friendlyURL = "/dstvafrica/content/en/mauritius/mauritius_home";

	countries.mozambique = new Object();
	countries.mozambique.name = "Mozambique";
	countries.mozambique.languages = "en pt";
	countries.mozambique.landingPage = "page2688";
	countries.mozambique.friendlyURL = "/dstvafrica/content/en/mozambique/mozambique_home";

	countries.namibia = new Object();
	countries.namibia.name = "Namibia";
	countries.namibia.languages = "en";
	countries.namibia.landingPage = "page3116";
	countries.namibia.friendlyURL = "/dstvafrica/content/en/namibia/namibia_home";

	countries.niger = new Object();
	countries.niger.name = "Niger";
	countries.niger.languages = "en";
	countries.niger.landingPage = "page7132";
	countries.niger.friendlyURL = "/dstvafrica/content/en/niger/niger_home";

	countries.nigeria = new Object();
	countries.nigeria.name = "Nigeria";
	countries.nigeria.languages = "en";
	countries.nigeria.landingPage = "page4901";
	countries.nigeria.friendlyURL = "/dstvafrica/content/en/nigeria/nigeria_home";

	countries.rwanda = new Object();
	countries.rwanda.name = "Rwanda";
	countries.rwanda.languages = "en fr";
	countries.rwanda.landingPage = "page5278";
	countries.rwanda.friendlyURL = "/dstvafrica/content/en/rwanda/rwanda_home";

	countries.sao_tome_principe = new Object();
	countries.sao_tome_principe.name = "Sao Tome & Principe";
	countries.sao_tome_principe.languages = "en";
	countries.sao_tome_principe.landingPage = "page7134";
	countries.sao_tome_principe.friendlyURL = "/dstvafrica/content/en/sao_tome_principe/sao_tome_principe_home";

	countries.senegal_ = new Object();
	countries.senegal_.name = "Senegal ";
	countries.senegal_.languages = "en";
	countries.senegal_.landingPage = "page7137";
	countries.senegal_.friendlyURL = "/dstvafrica/content/en/senegal/senegal_home";

	countries.seychelles = new Object();
	countries.seychelles.name = "Seychelles";
	countries.seychelles.languages = "en";
	countries.seychelles.landingPage = "page4224";
	countries.seychelles.friendlyURL = "/dstvafrica/content/en/seychelles/seychelles_home";

	countries.sierra_leone = new Object();
	countries.sierra_leone.name = "Sierra Leone";
	countries.sierra_leone.languages = "en";
	countries.sierra_leone.landingPage = "page7235";
	countries.sierra_leone.friendlyURL = "/dstvafrica/content/en/sierra_leone/sierra_leone_home";

	countries.southafrica = new Object();
	countries.southafrica.name = "South Africa";
	countries.southafrica.languages = "en";
	countries.southafrica.landingPage = "page14156";
	countries.southafrica.friendlyURL = "/dstvafrica/content/en/south_africa/south_africa_home";

	countries.st_helena = new Object();
	countries.st_helena.name = "St Helena";
	countries.st_helena.languages = "en";
	countries.st_helena.landingPage = "page7116";
	countries.st_helena.friendlyURL = "/dstvafrica/content/en/st_helena/st_helena_home";

	countries.sudan = new Object();
	countries.sudan.name = "Sudan";
	countries.sudan.languages = "en";
	countries.sudan.landingPage = "page4894";
	countries.sudan.friendlyURL = "/dstvafrica/content/en/sudan/sudan_home";

	countries.swaziland = new Object();
	countries.swaziland.name = "Swaziland";
	countries.swaziland.languages = "en";
	countries.swaziland.landingPage = "page5623";
	countries.swaziland.friendlyURL = "/dstvafrica/content/en/swaziland/swaziland_home";

	countries.tanzania = new Object();
	countries.tanzania.name = "Tanzania";
	countries.tanzania.languages = "en";
	countries.tanzania.landingPage = "page4296";
	countries.tanzania.friendlyURL = "/dstvafrica/content/en/tanzania/tanzania_home";

	countries.togo = new Object();
	countries.togo.name = "Togo";
	countries.togo.languages = "en";
	countries.togo.landingPage = "page6999";
	countries.togo.friendlyURL = "/dstvafrica/content/en/togo/togo_home";

	countries.uganda = new Object();
	countries.uganda.name = "Uganda";
	countries.uganda.languages = "en";
	countries.uganda.landingPage = "page4215";
	countries.uganda.friendlyURL = "/dstvafrica/content/en/uganda/uganda_home";

	countries.zambia = new Object();
	countries.zambia.name = "Zambia";
	countries.zambia.languages = "en";
	countries.zambia.landingPage = "page3865";
	countries.zambia.friendlyURL = "/dstvafrica/content/en/zambia/zambia_home";

	countries.zimbabwe = new Object();
	countries.zimbabwe.name = "Zimbabwe";
	countries.zimbabwe.languages = "en";
	countries.zimbabwe.landingPage = "page3731";
	countries.zimbabwe.friendlyURL = "/dstvafrica/content/en/zimbabwe/zimbabwe_home";


function changeLocation(href) {
   currentPage = "page-1";
   if (location.href != href) { //we don't want to redirect to the same page that we are on... avoid recursive redirects
      location.href = href;
   }
}

//sets cookie according to country selection and redirects to landing page of this country
function countrySelected(country, stayOnPage) {
   if (country.trim()!="southafrica")  {
	   var url;
	   var new_url;
	   if (country == null || country == "") {
	      deleteCookie(countryCookieName);
	      deleteCookie(languageCookieName);
	      url = location.href;
	      //new_url = url.replace(/(\/en\/|\/fr\/|\/pt\/)page[0-9]+/, "/en/Page [Landing page] v1");
	      if(useFriendlyURL) {     	
	      	new_url = url.replace(friendlyURLReplacementPattern, "/dstvafrica/content/en/dstvafrica/landing");
	      	new_url = new_url.replace(/\/en\/|\/fr\/|\/pt\//, "/en/");
	      } else {
	      	new_url = url.replace(/(\/en\/|\/fr\/|\/pt\/)page[0-9]+/, "/en/Page [Landing page] v1");
	      }
	   } else {
	      setCookie(countryCookieName, country, expireDate, cookiePath);
	      setCookie(languageCookieName, "en", expireDate, cookiePath);
	      url = location.href;
	      //new_url = url.replace(/(\/en\/|\/fr\/|\/pt\/)page[0-9]+/, "/en/" + countries[country].landingPage);
	      if(useFriendlyURL) {
	      	new_url = url.replace(friendlyURLReplacementPattern, countries[country].friendlyURL);
	      	new_url = new_url.replace(/\/en\/|\/fr\/|\/pt\//, "/en/")
	      } else {
	      	new_url = url.replace(/(\/en\/|\/fr\/|\/pt\/)page[0-9]+/, "/en/" + countries[country].landingPage);
	      }
	   }
	   if (!stayOnPage) changeLocation(new_url);
   } else {
   		location.href="http://www.dstv.com";
   }
}

//sets cookie according to language selection and redirects to that language
function languageSelected(lang) {
   if (lang == "") {
      deleteCookie(languageCookieName);
   } else {
      setCookie(languageCookieName, lang, expireDate, cookiePath);
      var url = location.href;
      var new_url = url.replace(/\/en\/|\/fr\/|\/pt\//, "/" + lang + "/");
      changeLocation(new_url);
   }
}

//populates the langauage select fields according to country configuration
function setLanguages(country) {
   var langSelect = document.getElementById("langSelect");
   var index;
   if (langSelect.options != null) {
      for (i = 0; i < langSelect.options.length; i++) {
         if (langSelect.options[i].value != "") {
            index = countries[country].languages.indexOf(langSelect.options[i].value);
            if (index < 0) {
               langSelect.options[i] = null;
               i--;
            }
         }
      }
   }
}

//preselect the country and language according to the cookie values - is called by checkSettings();
function setDropdowns(country, lang) {
   if (document.getElementById("countrySelect")) {
      selectOptionByValue(document.getElementById("countrySelect"), country);
   }

   if (document.getElementById("langSelect")) {
      selectOptionByValue(document.getElementById("langSelect"), lang);
   }
}

function selectOptionByValue(obj, val) {
   if (obj.options && obj.options.length > 0) {
      for (var i = 0; i < obj.options.length; i++) {
         if (obj.options[i].value == val) {
            obj.options[i].selected = true;
         }
      }
   }
}

function detectCountry() {
	var currentUrl = '/dstvafrica/content/en/botswana/botswana_home'; //'/dstvafrica/applications/dstvafrica/custom/countries/javascript.jsp';
	var country = "";
	for (var countryName in countries) {
		var friendly = countries[countryName].friendlyURL;
		if (friendly.indexOf(currentUrl) != -1) {
			country = countryName;			
			break;				
		}
	}

	if (country != null) {
		countrySelected(country, true);	
	}
	
}

function checkSettings() {
   var url;
   var new_url;
   var countryCookie = getCookie(countryCookieName);
   var languageCookie = getCookie(languageCookieName);

	if (!isLandingPage && countryCookie==null) {
		//Set country cookie from the URL
		detectCountry();
	   var countryCookie = getCookie(countryCookieName);
	   var languageCookie = getCookie(languageCookieName);
	}

   //set cookie in list
   if (countryCookie!=null) setDropdowns(countryCookie, languageCookie);

   if (isLandingPage) {
      if (countryCookie != null && countryCookie != "") {
         var lang;
         if (languageCookie != null && languageCookie != "") {
            //use cookie language
            lang = languageCookie;
         } else {
            //user default language
            lang = "en";
         }
         url = location.href;
        
		  if(useFriendlyURL) { 
			new_url = url.replace(friendlyURLReplacementPattern, countries[countryCookie].friendlyURL);
			new_url = new_url.replace(/\/en\/|\/fr\/|\/pt\//, "/en/")
		  } else {
			new_url = url.replace(/(\/en\/|\/fr\/|\/pt\/)page[0-9]+/, "/en/" + countries[countryCookie].landingPage);
		  }



         changeLocation(new_url);
      }
   } else {
		var url = location.href;
		url = url.split("/");
		var sectionDir = url[url.length-2];
		var sectionLang = "";
		var urlLang = url[url.length-3];

		for (var countryName in countries) {
			var friendly = countries[countryName].friendlyURL;
			if (friendly.indexOf(sectionDir) != -1) {
				sectionLang = countries[countryName].languages;	
				countryCookie = countryName;
				break;				
			}
		}
		if (sectionLang.indexOf(urlLang) == -1) {
			languageCookie = "en";
		} else {
			languageCookie = urlLang;
		}
        languageSelected(languageCookie);
		countrySelected(countryCookie, true);

		setLanguages(countryCookie);
		//set cookie in list

		 if (countryCookie!=null) setDropdowns(countryCookie, languageCookie);
	}
}

window.setTimeout("checkSettings()", 1000);

