var N   = (document.layers) ? true:false;                 // netscape 4
var I   = (document.all) ? true:false;                    // ie4+
var DOM = ((document.getElementById)&&(!I))?true:false;   // ns6 etc.
var hilighted = false; //global needed for stick functionality
var id_name;    //global needed for stick functionality

//------------------------------------------------
//-- J U M P T O
function jumpto(p_url, p_popup, p_width, p_height, p_name, p_scroll){
  if (p_popup == 'Y'){
    newWindow =
    window.open(p_url,p_name,config="resizable=yes,scrollbars="+p_scroll+",screenX=50,screenY=50,left=50,top=50,width="+
p_width+",height="+p_height);
    newWindow.focus();
  }
  else{
    location.href = p_url;
  }
}

//------------------------------------------------
//-- S U B M I T T Q F O R M
function submitTQForm(r) {
  r.submit();
  return false;
}

//---------------------
//-- D O L S E A R C H
function doLsearch(i,s) {
  var url;
  url = '/pls/html/think.lsearch?s='+i+'&cx=000315470966420306637%3Amwsjqlz8fvs&cof=FORID%3A11%3BNB%3A1&ie=UTF-8&q='+s+'&r=N';
  location.href = url;
}

//------------------------------------------------
//-- T E X T C O U N T E R
function textCounter(field, maxlimit) {
  if (field.value.length > maxlimit){
    field.value = field.value.substring(0, maxlimit);
  }
}

//------------------------------------------------
//-- S H O W _ E R R O R S
function show_errors(p_seqno) {
    var display_error;
    var errors        = new Array();
    var too_big       = '#TOO_BIG#';
    var wrong_type    = '#WRONG_TYPE#';
    var qs            = location.search;
    var params        = qs.split('&');
    var last_param    = params[params.length-1];
    var error_array   = last_param.split('=');
    var error_string  = error_array[1];
    if (error_array[0] != 'p_error') {//no errors
        display_error = '';//no errors
    }
    else if ((error_string.length < 1)&&(error_array[0] == 'p_error')) {//no errors
        display_error = '';//no errors
    }
    else {
        errors = error_string.split(','); //we have errors - build array
    }
    if (p_seqno == errors[0]) {//print errors for correct problem pic
        if (errors[1]) {
            display_error = wrong_type;
        }
        if (errors[2]) {
            display_error = too_big;
        }
        if (errors[1] && errors[2]) {
            display_error = wrong_type + '<br>' + too_big;
        }
    }
    else {
        display_error = '';//don't print errors for wrong pic
    }
    document.write(display_error);
}//end function

//------------------------------------------------
//-- N E W W I N
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();
}

//------------------------------------------------
//-- N E W R W I N
function newRWin(url_path, win_width, win_height, win_name, win_scroll)
{
  var scrollbar = 'no';
  if (!win_scroll) {
    scrollbar = 'yes';
  }

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

//------------------------------------------------
//-- N E W    F U L L    R W I N
function newFullRWin(url_path, win_width, win_height, win_name, win_scroll)
{
  if (!win_scroll) {
    win_scroll = 'yes';
  }

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

//------------------------------------------------
//-- M O V E F R O M L I S T
function moveFromList(fromList, toList)
{
    for (i=0; i < fromList.length; i++)
    {
        if (fromList.options[i].selected)
        {
            found = false;
            for (j=0; j<toList.length; j++)
            {
                if (toList.options[j].value == fromList.options[i].value)
                    found = true;
            }
            if (found == false)
            {
                toList.options[toList.length] = new Option(fromList.options[i].text,fromList.options[i].value);
                fromList.options[i] = null;
            }
        }
    }
}

//------------------------------------------------
//-- S E L E C T A L L
function selectAll(fromList)
{
 for ( i = 0; i < fromList.length; i++ )
  fromList.options[i].selected = false;
 for ( i = 1; i < fromList.length; i++ )
  fromList.options[i].selected = true;
  return true;
}

//------------------------------------------------
//-- S E L E C T A L L T O G G L E
function selectAllToggle(p_checkbox, p_value)
{
  if (p_checkbox) {
    if (p_checkbox.length) {
      for (i = 0; i < p_checkbox.length; i++) {
        p_checkbox[i].checked = p_value;
      }
    } else {
      p_checkbox.checked = p_value;
    }
  }
  return true;
}

//------------------------------------------------
//-- S E T _ H O U R S
function set_hours(hours) {
    if (hours < 1)
    {
      hours = 'Less than 1 Hour';
    }
else if ((hours > 1)&&(hours < 24))
    {
      hours = hours + ' Hours';
    }
else
    {
      hours = '24+ Hours';
    }
return hours;
}//end function

//------------------------------------------------
//-- W H O L E S T I C K Y C L O S E A N D G O T O
function WholeStickyCloseandGoto(sid,ut,md,url,un){
if (ut != 'MODERATOR') {
  window.opener.location='think.go?u='+sid;
  window.close();}
else {
  window.opener.location=url+un.toLowerCase()+'@'+md;
  window.close();}
}

//------------------------------------------------
//-- 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;
}

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

//------------------------------------------------
//-- C H A R C O U N T
function charCount(tArea,maxNo,ctrField,maxField,ctrBlock,allowExtra){
    textArea = document.getElementById(tArea);
    ctrF     = document.getElementById(ctrField);
    maxF     = document.getElementById(maxField);
    ctrBlk   = document.getElementById(ctrBlock);

    pctFull  = textArea.value.length / maxNo * 100;

    if (allowExtra != 'Y')
        {if (textArea.value.length >= maxNo)
            {textArea.value = textArea.value.substring(0, maxNo);
             textArea.style.color = 'red';
            }
         else
            {msg = null;
             textArea.style.color = 'black';}
        }
    ctrF.innerHTML = textArea.value.length;
    maxF.innerHTML = maxNo;
    if (textArea.value.length > 0)
        {ctrBlk.style.visibility = 'visible';}
    else
        {ctrBlk.style.visibility = 'hidden';}
    if (pctFull >= 90)
        {ctrBlk.style.color='red';}
    else if (pctFull >= "80")
        {ctrBlk.style.color='#EAA914';}
    else
        {ctrBlk.style.color='black';}
} // End charCount()

//------------------------------------------------
//-- S H U T T L E I T E M
function shuttleItem(theSource, theDest, moveAll) {
    srcList  = document.getElementById(theSource);
    destList = document.getElementById(theDest);
    var arrsrcList = new Array();
    var arrdestList = new Array();
    var arrLookup = new Array();
    var i;

    if (moveAll){
        for ( i = 0; i <= srcList.length-1; i++ )
              srcList.options[i].selected = true;
    }
    for (i = 0; i < destList.options.length; i++) {
        arrLookup[destList.options[i].text] = destList.options[i].value;
        arrdestList[i] = destList.options[i].text;}
    var fLength = 0;
    var tLength = arrdestList.length;
    for(i = 0; i < srcList.options.length; i++) {
        arrLookup[srcList.options[i].text] = srcList.options[i].value;
        if (srcList.options[i].selected && srcList.options[i].value != "") {
            arrdestList[tLength] = srcList.options[i].text;
            tLength++;}
        else {
            arrsrcList[fLength] = srcList.options[i].text;
            fLength++;}
    }
    arrsrcList.sort();
    arrdestList.sort();
    srcList.length = 0;
    destList.length = 0;
    var c;
    for(c = 0; c < arrsrcList.length; c++) {
        var no = new Option();
        no.value = arrLookup[arrsrcList[c]];
        no.text = arrsrcList[c];
        srcList[c] = no;
    }
    for(c = 0; c < arrdestList.length; c++) {
        var no = new Option();
        no.value = arrLookup[arrdestList[c]];
        no.text = arrdestList[c];
        destList[c] = no;
       }
} // End shuttleItem()

//------------------------------------------------
//-- D O L O G I N
//-- If they are logging into production or try use ssl otherwise don't
function doLogin(){
  var username = document.login.p_username.value.toUpperCase();
  document.login.action = 'https://' + location.hostname + '/pls/html/think.web_login';
  document.login.submit();
}//end doLogin function


//------------------------------------------------
//-- P O P _ P R I N T
function pop_print(page_id) {
    var  url = 'f?p=think:print';
    newWin(url, '515', '350', 'print_window', 'yes');
}


//------------------------------------------------
//-- S T A T I C _ P O P _ P R I N T
function static_pop_print() {
    var  url = '/common/print_window.html';
    newWin(url, '515', '350', 'print_window', 'yes');
}

//------------------------------------------------
//-- G E T N E W L E N G T H
function getnewLength(s, maxbytelimit) {
var r = 0;
var i = 0;
var x = 0;
while ( x == 0 ) {
  var c = s.charCodeAt(i);
  i += 1;
  if ( c >= 0 && c <= 255 ) { r += 1; }
  else if (c > 255 && c <= 2047) { r += (2*3); }
  else if (c > 2047 && c <= 65535) { r += (3*3); }
  else { r += (4*3); }
  //check to see if we are past the limit yet, if we are then set the new length
  if (r > maxbytelimit) {
    x = i - 1;
  }
}
  return x;
}// end function

//------------------------------------------------
//-- C H E C K U C L E N G T H
function checkucLength(s) {
var r = 0;
for (var i = 0; i < s.length; i++) {
  var c = s.charCodeAt(i);
  if ( c >= 0 && c <= 255 ) { r += 1; }
  else if (c > 255 && c <= 2047) { r += (2*3); }
  else if (c > 2047 && c <= 65535) { r += (3*3); }
  else { r += (4*3); }
}
  return r;
}// end function

//------------------------------------------------
//-- U C T E X T C O U N T E R
function uctextCounter(id, maxbytelimit) {
if (N) {}
else if (I) {
  //set field
  var field = document.all[id];
  }
else if (DOM) {
  //set field
  var field = document.getElementById(id);
}

if (checkucLength(field.value) > maxbytelimit) { // if too long...trim it!
  // trim the string back to the last character that adds up to the byte limit
  field.value = field.value.substring(0, getnewLength(field.value,maxbytelimit));
}
}// end function


//------------------------------------------------
//-- G M T G O T O
function GMTgoto(type, old_country, topic_id){

  country = getCookie('tcTempCookie').substr(3,2);

  if (type == 'support_loginhelp') {
    location.href = '/pls/html/think.support?p=loginhelp&c='+country+'';
  }
  else if (type == 'support_feedback') {
    location.href = '/pls/html/think.support?p=feedback&c='+country;
  }
  else if (type == 'parentsdirectory') {
    location.href = '/pls/html/think.parentsdirectory?c='+old_country;
  }
  else if (type == 'document') {
    location.href = '/pls/html/think.document?t='+topic_id+'&c='+country;
 }
  else if (type == 'registration_preview') {
    newWin('/pls/html/registration.previewagreement?c='+country+'','550','450','fullpopup','yes');
 }
  else if (type == 'try_register') {
    location.href = '/pls/html/try.register?c='+country;
 }
} // end GMTgoto()

//------------------------------------------------
//-- W R I T E _ B R O C H U R E _ T E X T
function write_brochure_text() {
  var pagedir = location.href.substr(location.href.indexOf('/',7) + 1, location.href.length);
  var slashPos = pagedir.lastIndexOf("/training/");
  var langdir = pagedir.substr(0,slashPos);
  var dir = langdir.substr(0,3)
  document.writeln('<strong>Think.com Brochure</strong><br>');
  document.writeln('Take a look at this printable <a href="/common'+dir+'/think_brochure.pdf" target="_blank">brochure</a> ');
  document.writeln('for more information about Think.com. This is a great resource ');
  document.writeln('to give to other teachers, parents, or administrators.');
}

//------------------------------------------------
//-- G E T   I T E M
function getItem(id)
 {
   var itm = false;
   if(document.getElementById)
     {itm = document.getElementById(id);}
   else if(document.all)
     {itm = document.all[id];}
   else if(document.layers)
     {itm = document.layers[id];}

   return itm;
 }

//------------------------------------------------
//-- S E T   D A T E   D I S P L A Y
function setDateDisplay(p)
{
  l_ends  = getItem(p+'_ENDS');
  l_month = getItem(p+'_MONTH');
  l_day   = getItem(p+'_DAY');
  l_year  = getItem(p+'_YEAR');

  if (l_ends.value == 'N')
  {
    l_month.style.display = 'none';
    l_day.style.display   = 'none';
    l_year.style.display  = 'none';
  }
  else
  {
    l_month.style.display = '';
    l_day.style.display   = '';
    l_year.style.display  = '';
  }
  return false;
}

//------------------------------------------------
//-- C H A N G E   D I V   H E I G H T
function changeDivHeight() {

  var wh; //defined for window height
  var h; //defined for height amount to add to bottom_margin region after calculated

  //get height of topfull area and put it into top variable
  var top;
  if (document.getElementById('topfull')) {
    top = document.getElementById('topfull').offsetHeight;
  }
  else {
    top = 80;
  }

  //get height of subtabs area and put it into subtabs variable
  var subtabs;
  if (document.getElementById('subtabsBg')) {
    subtabs = document.getElementById('subtabsBg').offsetHeight;
  }
  else {
    subtabs = 24;
  }

  //get height of mainBg area and put it into main variable
  var main;
  if (document.getElementById('mainBg')) {
    main = document.getElementById('mainBg').offsetHeight;
  }
  else {
    main = 200;
  }

  //get height of bottomfull area and put it into top variable
  var bottom;
  if (document.getElementById('bottomfull')) {
    bottom = document.getElementById('bottomfull').offsetHeight;
  }
  else {
    bottom = 40;
  }

  //add height of 4 outer-most divs that define the page height to determine page height variable
  var ph  = top + subtabs + main + bottom; //defined as "page height" variable

  //determine the window's height
  if (self.innerHeight)
  {
    wh = self.innerHeight;
  }
  else if (document.documentElement && document.documentElement.clientWidth)
  {
    wh = document.documentElement.clientHeight;
  }
  else if (document.body)
  {
    wh = document.body.clientHeight;
  }
  else return true;

  //height to add to bottom_margin region is window height - page height
  h = wh - ph;

  //if height is less than 10 make it 10 to keep the bottom margin's
  //10px of space between page text end and footer begin
  if (h < 10){
    h = 10;
  }

  //Now that we have the correct height amount we can redefine the height of the bottom_margin region
  if (document.getElementById('bottom_margin')) {
    document.getElementById('bottom_margin').style.height = h + "px";
  }
  return true;
}//end of changeDivHeight

//------------------------------------------------
//-- A N N C   E X P A N D
function anncExpand(id,img){
  var elem = document.getElementById(id);
  var btn  = document.getElementById(img);

  //If it's hidden make it visible and change the arrow to the down arrow (expand)
  if (elem.style.display == 'none'){
    elem.style.display = 'block';
    btn.src = '/common/down_blue_arrow.gif';
  }
  //else make it hidden and change the arrow to the right pointing arrow (collapse)
  else{
    elem.style.display = 'none';
    btn.src = '/common/right_blue_arrow.gif';
  }

}//end of anncExpand

//------------------------------------------------
//-- H T M L _ G E T   E L E M E N T
function html_GetElement(pNd){
  try{
    var node;
    switch(typeof (pNd)){
      case 'string':node = document.getElementById(pNd); break;
      case 'object':node = pNd; break;
      default:node = false; break;
    }
    return node;
  }catch(e){return false;}
 }

//------------------------------------------------
//-- H T M L _ C A S C A D E   U P   T I L L
function html_CascadeUpTill(pThis,pToTag,pToClass,pCount){
 var node = html_GetElement(pThis);
 if (node) {
  var tPar = node.parentNode;
  if(pToClass){
    while(tPar.nodeName != pToTag && tPar.className != pToClass){
      tPar = tPar.parentNode;
    }
  }else{
    while(tPar.nodeName != pToTag){
      tPar = tPar.parentNode;
    }
  }
 return tPar;
 } else {
  return null;
 }
 }

//------------------------------------------------
//-- J U M P   T O   I T
function JumpToIt(list) {
    var newPage = list.options[list.selectedIndex].value;
    if (newPage != "None") {
        location.href = newPage;
    }
}

//------------------------------------------------
//-- G E T   A P P   H O S T   U R L
function GetAppHostUrl(type) {
  var domain;
  var url;
  if (location.href.indexOf('try') > 0) {
    domain = location.href.substr(0, location.href.indexOf('try') + 3);
  }
  else if (location.href.indexOf('www') > 0) {
    domain = location.href.substr(0, location.href.indexOf('www') + 3);
  }
  else if (location.href.indexOf('dev') > 0) {
    domain = location.href.substr(0, location.href.indexOf('dev') + 4);
  }
  else if (location.href.indexOf('beta') > 0) {
    domain = location.href.substr(0, location.href.indexOf('beta') + 4);
  }
  else if (location.href.indexOf('stage') > 0) {
    domain = location.href.substr(0, location.href.indexOf('stage') + 5);
  }
  else if (location.href.indexOf('alpha') > 0) {
    domain = location.href.substr(0, location.href.indexOf('alpha') + 5);
  }
  else {
    domain = 'http://www';
  }

  if (type == 'TC') {
    url = domain + '.think.com/'
  }
  else if (type == 'TQ') {
    url = domain + '.thinkquest.org/'
  }
  else if (type == 'LIB') {
    if (domain == 'http://try'){
      domain = 'http://www';
    }
    url = domain + '.thinkquest.org/library/'
  }

  return url;

}

//------------------------------------------------
//-- D I S P L A Y   D I V
function displayDiv(id){
  var div    = new Array();
  var header = new Array();
  var hid    = 'fph' + id;
  var did    = 'fp' + id;

   //Do a changeDiHeight to keep the footer consistent
  changeDivHeight;

  //Define arrays
  div[0] = document.getElementById('fpoverview');
  div[1] = document.getElementById('fpwebsite');
  div[2] = document.getElementById('fpemail');
  div[3] = document.getElementById('fppeople');
  div[4] = document.getElementById('fpprojects');

  header[0] = document.getElementById('fphoverview');
  header[1] = document.getElementById('fphwebsite');
  header[2] = document.getElementById('fphemail');
  header[3] = document.getElementById('fphpeople');
  header[4] = document.getElementById('fphprojects');

  //Make all styles hidden or off state
  for (i=0;i<5;i++) {
    div[i].style.display = 'none';
    header[i].className = 'fpheaderoff';
  }

  //Change the state of the current id to be on and visible
  document.getElementById(hid).className = 'fpheaderon';
  document.getElementById(did).style.display = 'block';

  //Do a changeDiHeight to keep the footer consistent
  changeDivHeight;

}//end of displayDiv

// tool tip section
var tt_target;
function calEvt_init(){
  if ( document && document.body) {
        var tt_tipobj=html_GetElement("dhtmlcalevt");
        if (tt_tipobj == null || typeof(tt_tipobj) != 'object' ){
              tt_tipobj = document.createElement('DIV');
              tt_tipobj.id = "dhtmlcalevt";
              tt_tipobj.className = "CalEvt";
              tt_tipobj.style.position = "relative";
              tt_tipobj.style.left = ( tt_tipobj.offsetLeft - 215 ) + "px";
              tt_tipobj.style.top = ( tt_tipobj.offsetTop - 44 ) + "px";
              tt_tipobj.style.padding = "2px";
              tt_tipobj.style.width = "211px";
              tt_tipobj.style.height = "103px";
              tt_tipobj.style.overflow = "hidden";
              tt_tipobj.style.background = "transparent url(/common/cal_rollover.gif) no-repeat";
              tt_tipobj.style.visibility = "hidden";
              tt_tipobj.style.zIndex = 10000;
              document.body.appendChild(tt_tipobj);
        }
     return true;
    } else {
     return false;
    }
}

function calEvt_disable(){
    if ( calEvt_init() ) {
    var tt_tipobj=html_GetElement("dhtmlcalevt");

    tt_target = null;
    tt_tipobj.style.visibility = "hidden";
    tt_tipobj.style.background = '';
    tt_tipobj.style.width = '';
    tt_tipobj.innerHTML = '';
  }
}

function calEvt_enable(obj, day, month, event, project, description){
    if ( calEvt_init() ) {
    var tt_tipobj=html_GetElement("dhtmlcalevt");

    obj.style.position = "relative";

    tt_target = obj;
    tt_tipobj.style.width = "211px";
    tt_tipobj.style.position = "absolute";
    tt_tipobj.style.background = "transparent url('/common/cal_rollover.gif') no-repeat";
    tt_tipobj.innerHTML = '<div class="calnote"><div class="textbrt">' + month + '</div>' +
                          '<img src="/common/cal_' + day + '.gif" width="26" height="26" alt="'+ month +' '+ day +'" ' +
                          'class="calitem" /></div><div class="calnotedetails"><span class="textbrt">' + event +
                          '</span><br /><span class="textsmbrt">' + project + '</span><div class="textsmdim" style="padding-top:4px;">' +
                          description + '</div></div>';
    tt_tipobj.style.left = ( obj.offsetLeft - 215 ) + "px";
    tt_tipobj.style.top  = ( obj.offsetTop - 44 ) + "px";
    tt_tipobj.style.visibility = "visible";
    tt_tipobj.style.zIndex = 10000;

    try {
        obj.addEventListener("mouseout", calEvt_disable, false);
     } catch(E) {
      obj.attachEvent('onmouseout', calEvt_disable);
     }
   }
    return false;
}


/***************************************************************************************/
/* 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;

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() : ";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":
      region = "zh_cn";
    break
    case "it":
      region = "it_it";
    break
    case "hi":
      region = "hi_in";
    break
    case "nl":
      region = "nl_nl";
    break
    case "pt":
      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()); }
}

/*****************************
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>');
    }
  }
}

function enrollRedirect() {
  var cookie_value = getCookie('tcTempCookie');
  if (cookie_value == '') {
    cookie_value = 'en';
  }
  var url = 'https://' + location.hostname + '/pls/reghtml/registration.beginapplication?p=tc&l=' + cookie_value;
  location.href = url;
}

