var english = "1033";
var german = "1031";
var spanish = "1034";
var french = "1036";
var japanese = "1041";
var russian = "1049";
var chinese = "2052";

var defaultLang = english;

var baseUrl = 'http://www.husky.ca/';
var allowed = "";
var langtype = "";


var cookieLocation = getCookieValue('huskyLocation');
var cookieHuskyLang = getCookieValue('huskyLang');
var cookieLang = getCookieValue('SiteLanguage');

var url = window.location.toString();

if (cookieLocation != "") {

  //find the requested page and folder
  var requestedPage = url.replace(baseUrl,'');
  requestedPage = requestedPage.replace('','');
  requestedPage = requestedPage.replace('','');

  //remove the langtype from the url
  //requestedPage = requestedPage.substr(0, requestedPage.indexOf('?'));

  //determine what url they should be at
  var redirectUrl = baseUrl;

  switch (cookieLocation) {
    case "emea":
      allowed = english + "," + german + "," + spanish + "," + french + "," + russian;
      redirectUrl += ''
      break;
    case "asia": 
      allowed = english + "," + japanese + "," + chinese;
      redirectUrl += ''
      break;
    default:
      allowed = english + "," + french + "," + spanish;
      break;
  } 

  redirectUrl += requestedPage;

  //get the language from cookie

  if (url.toUpperCase().indexOf("LANGTYPE=") != -1) {
    langtype = url.substr(url.toUpperCase().indexOf("LANGTYPE=") + 9,4);
    //setCookie(cookieLocation, langtype);
  } else { 
    if (cookieLang != "") {
      //if (cookieLang.toUpperCase().indexOf("SITELANGUAGE=") != -1) {
      //  langtype = cookieLang.substr(cookieLang.toUpperCase().indexOf("SITELANGUAGE=") + 13,4);
       
      langtype = cookieLang;
      //override the cookie if it's not the same as the huskyLang cookie

      
      if(langtype != cookieHuskyLang)
      {
        langtype = cookieHuskyLang;
        setCookie(cookieLocation, langtype);
        redirectUrl = redirectUrl.substr(0, redirectUrl.indexOf('?'));
        redirectUrl += '?langtype=' + langtype;
      }
    //}
    }
  }


  //check if language not allowed for cookie location
  var langOK = true;
  if (langtype != "") {
    if (allowed.indexOf(langtype) == -1) {

    //language not ok, update cookie
     setCookie(cookieLocation, defaultLang);
     langOK = false;

     //add the default langtype to the redirect url
     redirectUrl = redirectUrl.substr(0, redirectUrl.indexOf('?'));
     redirectUrl += '?langtype=' + defaultLang;
    }
  }

  if (redirectUrl.toUpperCase() != url.toUpperCase()) {
    //the user is in the wrong location so redirect
    window.location = redirectUrl;
  }

}
function getCookieValue(key)
{
  var val = "";
  var cookies = document.cookie.toString().split(';');
  for (var i = 0; i < cookies.length; i++) {
     var cookieVals = cookies[i].split('&');
     for (var j = 0; j < cookieVals.length; j++) {
       var indx = cookieVals[j].indexOf('=');
       if (cookieVals[j].substring(0,indx).replace(" ","") == key) {
         val = cookieVals[j].substr(indx + 1);
         break;
       }
     }
  }
  return val;  
}

function setCookie(loc, lang)
{
    var cookieText = 'huskyLocation=' + loc;
    cookieText += '&huskyLang=' + lang + ';';
    cookieText +='ecm=user_id=0&site_id=&username=&new_site=/CMS400Example/&unique_id=0&site_preview=0&langvalue=0&DefaultLanguage=1033&NavLanguage=1033&LastValidLanguageID=1033&ContType=&UserCulture=1033&SiteLanguage=' + lang + ';';


   //set the expiry
   var exdate=new Date();
   exdate.setDate(exdate.getDate()+30);

   cookieText += 'expires=' + exdate.toUTCString() + ';';
   cookieText += 'path=/;';
   document.cookie = cookieText;

}


 
