﻿
function countryOptionChanged(evt_) {
  var ctySelect = document.getElementById("CountrySelect");
  var langSelect = document.getElementById("LanguageSelect");

  populateLanguageSelect(ctySelect.options[ctySelect.selectedIndex]);
}

function populateLanguageSelect(option_) {
  var langSelect = document.getElementById("LanguageSelect");
  var value = option_.value;
  var array = value.split("|");

  langSelect.options.length = 0;
  for (var idx = 0; array != null && idx < array.length; idx++) {
    var lang = array[idx].split("=");
    var opt = document.createElement("option");
    opt.id = lang[1];
    opt.label = lang[0];
    opt.value = option_.id + "?lang=" + lang[1];
    opt.text = lang[0];
    langSelect.options[idx] = opt;
  }
}

function languageOptionChanged(evt_) {
}

function submitButtonClicked(evt_) {
  var ctySelect = document.getElementById("CountrySelect");
  var ctyOpt = ctySelect.options[ctySelect.selectedIndex];

  var langSelect = document.getElementById("LanguageSelect");
  var langOpt = langSelect.options[langSelect.selectedIndex];

  var chkbox = document.getElementById("RememberCheckBox");
  if (chkbox.checked) {
    createCookie("ColliersCountryPath", ctyOpt.id);
    createCookie("ColliersLanguage", langOpt.id);
  }
  else {
    //removeCookie("ColliersCountryPath");
    //removeCookie("ColliersLanguage");
  }

//  window.alert("country:=" + getCookie("country"));
  //  window.alert("lang:=" + getCookie("lang"));
  langOpt.value = langOpt.value.replace("/global", "");
  //createCookie("RedirectPath", ctyOpt.id);
  //alert(langOpt.value);
  //window.location = langOpt.value;
  if (ctyOpt.id.indexOf("/global") != -1) {
      var redirectPath = ctyOpt.id;
  } else {
  var redirectPath = ctyOpt.id + "?lang=" + langOpt.id;
  }
  
  if (redirectPath.indexOf('NullCountrySelected') != -1) {
      removeCookie("ColliersCountryPath");
      removeCookie("ColliersLanguage");
      ctyOpt.id = "/global/";
      redirectPath = "/global/?lang=en-us";
  }

  redirectPath = redirectPath.replace("/global", "");
  if (redirectPath.charAt(0) != '/') {
     redirectPath = '/' + redirectPath;
  }
  createCookie("RedirectPath", redirectPath);
  //alert(redirectPath);
  window.location = redirectPath;
}

function createCookie(name_, value_) {
  var d = new Date();
  d.setDate(d.getDate() + 90);
  document.cookie = name_ + "=" + escape(value_) + ";expires=" + d.toUTCString() + ";path=/"
}

function removeCookie(name_) {
  var date = new Date();
  date.setDate(date.getDate() - 10000);
  document.cookie = name_ + "=;expires=" + date.toUTCString()
}

function getCookie(name_) {

  if (document.cookie.length > 0) {

    start = document.cookie.indexOf(name_ + "=");
    if (start != -1) {
      start = start + name_.length + 1;
      end = document.cookie.indexOf(";", start);
      if (end == -1) {
        end = document.cookie.length;
      }

      return unescape(document.cookie.substring(start, end));
    }
  }

  return "";
}

function setCountrySelectValue(path_) {
  if (path_ != null) {
    var e = document.getElementById("CountrySelect");
    if (e != null) {
      for (var idx = 0; idx < e.options.length; idx++) {
        option = e.options[idx];
        if (option != null && option.value == path_) {
          e.selectedIndex = idx;
          break;
        }
      }
    }
  }
}

function setLanguageSelectValue(lang_) {
  if (lang_ != null) {
    var e = document.getElementById("LanguageSelect");
    if (e != null && e.selectedIndex > 0) {
      var option = e.options[e.selectedIndex];
      for (var idx = 0; idx < e.options.length; idx++) {
        option = e.options[idx];
        if (option != null && option.value == path_) {
          e.selectedIndex = idx;
          break;
        }
      }
    }
  }
}

function initSelectValues() {
  var countryPath = getCookie("ColliersCountryPath");
  if (countryPath == null) {
    countryPath = location.pathname;
  }

  if (countryPath != null && setCountrySelectValue(countryPath)) {
    var language = getCookie("ColliersLanguage");
    if (language == null) {
      var str = location.search;
      str = str.toLowerCase();
      str.replace("?", "");
      if (str.indexOf("&") >= 0) {
        var array = str.split("&");
        for (var idx = 0; idx < array.length; idx++) {
          if (array[idx] != null && array[idx].indexOf("lang=") >= 0) {
            str = array[idx];
            break;
          }
        }
      }

      language = str;
    }

    setLanguageSelectValue(language);
  }
}

Event.observe(window, 'load', function() {

  if($('CityLangToggle') && $('CtyLangCtrl')) {
    $('CityLangToggle').onclick = function(e) {
      if($('CtyLangCtrl').style.display == 'none') {
        Effect.BlindDown('CtyLangCtrl', { duration: 0.2 });
      } else {
        Effect.BlindUp('CtyLangCtrl', { duration: 0.2 });
      }
      return false;
    };
    $('CtyLangCtrl').onclick = function(e) {
      if (!e) var e = window.event;
      e.cancelBubble = true;
      if (e.stopPropagation) e.stopPropagation();
    }
  }

});


