/***************************************************************************************/
/* Using cookies, we will set a temporary marker with the current region of
   think.com, and using a persistent cookie with the location of the visitor.

   -- The first visit to Think.com will set region to en_us.
   -- Once a user selects a country from the drop down list,
      the country selected's region will be stored in the temporary cookie
    and when the page reloads, the temporary cookie will be used to
    write links to TOS and Privacy Policies
  --Once a user checks the "Remember my Location" checkbox, a new persistent
    cookie will be set with the user's region.  Now, whenever a user visits think.com,
    it will redirect to the user's regional website
  --If a user enters the URL of a think.com location manually, such as /es/,
    then the country will be set to the most predominant country that uses that language.

*/

//window.onload = initCountryCookie;

var tq = (typeof tq == 'object') ? tq : {};

function setCookie(name,value,persistent) {
  //Sets a cookie taking in a name and value.
  //If persistent is set to TRUE, then the cookie
  //will have a much longer expiration date

  //check if a cookie with same name exists
//  if (getCookie(name))
//    deleteCookie(name)

  //set path to '/'
  var path = '/';
  //set domain to ''
  var domain = '';
  //set secure to ''
  var secure = '';
  //set expire for persistent
  var days_to_expire = 30;

  var today = new Date();
  today.setTime(today.getTime());

  var persistent_expires_date = new Date( today.getTime() + (days_to_expire * 1000 * 60 * 60 * 24) );

  var cookie_string = name + "=" + escape(value) +
  ( (persistent) ? ";expires=" + persistent_expires_date.toGMTString() : "" ) +
  ( (path) ? ";path=" + path : "" ) +
  ( (domain) ? ";domain=" + domain : "" ) +
  ( (secure) ? ";secure" : "" );

  document.cookie = cookie_string;
}

function getCookie(cookie_name) {
  //Checks if a cookie with a given name exists
  //if so, returns cookie value
  if (document.cookie.length>0)
    {
    c_start=document.cookie.indexOf(cookie_name + "=")
    if (c_start!=-1) {
      c_start=c_start + cookie_name.length+1
    c_end=document.cookie.indexOf(";",c_start)

    if (c_end==-1) c_end=document.cookie.length

    return unescape(document.cookie.substring(c_start,c_end))
    }
    }
  return ""
}

function deleteCookie(cookie_name) {
  var cookie_date = new Date ( );  // current date & time
  cookie_date.setTime ( cookie_date.getTime() - 1 );
  document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
}

function languageRedirect(region) {
  //Takes in a region, such as en_us,
  //and sets a new temprary cookie with the region
  //and redirects to selected language

  //this is called from the selectbox
  var directory;
  deleteCookie("tcTempCookie");
  setCookie("tcTempCookie",region);
  if (getCookie("tcTempCookie").substring(0,2) == 'zh'){
    directory = 'zh_cn';
  }
  else if (getCookie("tcTempCookie").substring(0,2) == 'pt'){
    directory = 'pt_br';
  }
  else{
    directory = getCookie("tcTempCookie").substring(0,2);
  }
  location.href = '/'+directory+'/';
}

function indexRedirect() {
  //check if tcPermRegion cookie exists.
  //if so, rediect to its region
  //if not, redirect to english by default
  if(getCookie("tcTempCookie")) {
    var permRegion = getCookie("tcTempCookie");
    languageRedirect(permRegion);
    }
  else {
    languageRedirect("en_us");
  }
}

/************************************
function setPermCookie() {
  //set a persistent cookie using region from
  //tcTempRegion if user checks the remember location checkbox
  var CountryRememberCheckBox = document.getElementById("CountryRememberCheckBox")
  if (CountryRememberCheckBox.checked) {
    var tempRegion = getCookie("tcTempCookie");
    setCookie("tcPermCookie",tempRegion,true);
    }
}
***************************************/

function getRegionFromURL() {
  var countryPath = location.href.substr(location.href.indexOf('/',7) + 1, 2);
  var region = "en_us";
  //Set Default countries for language directories
  switch(countryPath) {
    case "en":
      region = "en_us";
    break
    case "de":
      region = "de_de";
    break
    case "fr":
      region = "fr_fr";
    break
    case "es":
      region = "es_es";
    break
    case "zh_cn":
      region = "zh_cn";
    break
    case "it":
      region = "it_it";
    break
    case "hi":
      region = "hi_in";
    break
    case "nl":
      region = "nl_nl";
    break
    case "pt_br":
      region = "pt_br";
    break
    case "th":
      region = "th_th";
    break
    case "tr":
      region = "tr_tr";
    break
    }
  return region;
}

function initCountryCookie() {
  //run everytime page loads
  //checks if a temporary cookie exists
  //   yes: does it match the current language?
  //        no: set new cookie from url
  //   no:  set new cookie from url
  if (!getCookie('tcTempCookie')) {
  //   if (getCookie('tcTempCookie').substr(0,2) != location.href.substr(location.href.indexOf('/',7) + 1, 2)) {
  //     setCookie('tcTempCookie',getRegionFromURL()); }
  // }
  // else {
    setCookie('tcTempCookie',getRegionFromURL()); }
}

// returns a complete URL taking into account <base> tag (IE6 and IE7 ignore the <base> tag
// when you set location.href in JavaScript)
function getCompleteURL(p_url) {
  var l_base = '';

  if (String(p_url).substr(0, 1) == '/') {
    if (document.getElementsByTagName) {
      var l_base_tags = document.getElementsByTagName('base');
      if (l_base_tags.length > 0) {
        l_base = String(l_base_tags[0].href).replace(/\/$/, '');
      }
    }
  }

  return l_base + p_url;
} // getCompleteURL()

function sendToCountryPage(url) {
  //  First check if a cookie exists.
  //  If it does: make the links on the external pages point to that lang
  //  If it does not: make links point to USA.
  var country_code;
  if(getCookie("tcTempCookie")) {
    var permRegion = String(getCookie("tcTempCookie"));
    if (permRegion.match(/^pt_/i)) {
      location.href = getCompleteURL('/pt_br'+url);
      }
    else if (permRegion.match(/^zh_/i)) {
      location.href = getCompleteURL('/zh_cn'+url);
      }
    else {
      location.href = getCompleteURL('/'+permRegion.substring(0,2)+url);
      }
    }
  else {
    // If no cookie for country exists,
    // send to engl version.
    location.href = getCompleteURL('/'+'en'+url);
    }
}

/*****************************
function checkRememberBox() {
    if (getCookie('tcPermCookie')) {
        if ( (getCookie('tcPermCookie')) == (getCookie('tcTempCookie')) ) {
            document.getElementById("CountryRememberCheckBox").checked = true;
        }
    }
}
*****************************/

function printCountrylist(countryName,countryDir) {
  currentRegion = getCookie('tcTempCookie')
  for (i=0; i < countryDir.length; i++) {
    if (countryDir[i] == currentRegion) {
      document.write('<option value="' + countryDir[i] + '" selected>' + countryName[i] + '</option>');
      }
  else {
    document.write('<option value="' + countryDir[i] + '">' + countryName[i] + '</option>');
    }
  }
}

// Get link to documents

function getCountryLink(link_type, old_country, document_type){
  var l_country;
  var l_language = window.navigator.userLanguage || window.navigator.language;
  if(l_language.indexOf('-') > 0){
    l_country = l_language.substr(3,2);
  }
  else{
    l_country = l_language.substr(0,2);
  }
  if (link_type == 'support_loginhelp') {
    location.href = getCompleteURL('/pls/html/think.support?p=loginhelp&c='+l_country);
  }
  else if (link_type == 'support_feedback') {
    location.href = getCompleteURL('/pls/html/think.support?p=feedback&c='+l_country);
  }
  else if (link_type == 'document') {
    //  Send them to the proper URL for competition page vs projects page
    if (location.href.substr(location.href.indexOf('/',7)+1,11) == 'competition' || location.href.substr(location.href.indexOf('/',7)+1,7) == 'library'  || location.href.substr(location.href.indexOf('f?p=',7)+4,5) == '57700') {
      location.href = getCompleteURL('/pls/html/think.document?t='+document_type+'_comp'+'&c=us');
    }
    else {
      location.href = getCompleteURL('/pls/html/think.document?t='+document_type+'&c='+l_country);
    }
  }
  else if (link_type == 'registration_preview') {
    newWin('/pls/html/registration.previewagreement?c='+country+'','550','450','fullpopup','yes');
  }
  else if (link_type == 'help') {
    location.href = getCompleteURL('/pls/html/think.help?id=');
  }
  else if (link_type == 'faq_library') {
    location.href = getCompleteURL('/pls/html/think.help?id=199737');
  }
}

// Do Login
function doLogin() {
  document.login.action = 'https://' + location.hostname + '/pls/html/think.web_login';
  document.login.submit();
}//end doLogin function

function newRWin(url_path, win_width, win_height, win_name, win_scroll)
{
  if (win_scroll == ''){
    win_scroll = 'no';
  }
  newWindow =
    window.open(url_path,win_name,config="resizable=yes,scrollbars=no,screenX=50,screenY=50,left=50,top=50,width="+
    win_width+",height="+win_height);
    windowWidth = parseInt(win_width);
    windowHeight = parseInt(win_height);
  newWindow.resizeTo(windowWidth,windowHeight+50)
  newWindow.focus();
}


//------------------------------------------------
//-- S U B M I T E N T E R
function submitenter(myfield,e)
{
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
else return true;
if (keycode == 13)
   {
   doLogin();
   return false;
   }
else
   return true;
}

function newWin(url_path, win_width, win_height, win_name, win_scroll)
{
  newWindow =
  window.open(url_path,win_name,config="resizable=yes,scrollbars="+
  win_scroll+",screenX=50,screenY=50,left=50,top=50,width="+
  win_width+",height="+win_height);
  newWindow.focus();
}

//------------------------------------------------
//-- P U T F O C U S
function putFocus() {
  document.login.p_username.focus();
}

tq.setInitCookie = function(p_langir) {
  var region = "en_us";
  //Set Default countries for language directories
  switch(p_langir) {
    case "en":
      region = "en_us";
    break
    case "de":
      region = "de_de";
    break
    case "fr":
      region = "fr_fr";
    break
    case "es":
      region = "es_es";
    break
    case "zh_cn":
      region = "zh_cn";
    break
    case "it":
      region = "it_it";
    break
    case "hi":
      region = "hi_in";
    break
    case "nl":
      region = "nl_nl";
    break
    case "pt_br":
      region = "pt_br";
    break
    case "th":
      region = "th_th";
    break
    case "tr":
      region = "tr_tr";
    break
    }
  setCookie('tcTempCookie', region);
};

