function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}
function getLang() {
  u = window.location.pathname;
  if (u.search(/(\.htm$|\.asp$)/) != -1) { /*** only htm and asp, to be safe ***/
    l = u.replace(/.*\.([c])\..*/, "$1");
    l = (l.length > 1) ? '' : l;           /*** .?. not found, l.length > 1, assign '' ***/
    return l;
  }
  return null;
}
function writeLang() {
  l = getLang();
  if (l != null) {
    if (l != '') document.write('<div id="langselect"><a href="javascript:changeLang(\'\');">English</a></div>');
    if (l != 'c') document.write('<div id="langselect"><a href="javascript:changeLang(\'c\');">中 文</a></div>');
  }
}
function changeLang(l) {
  if (getLang() != null) {
    createCookie("lang", 'PleaseSetMe', 365);
    cookieReset = readCookie("lang");
    if (cookieReset != '' && cookieReset !='c') { /*** only '' and c lang so far ***/
      l = (l.length > 0) ? '.' + l : l;
      u = window.location.href;
      hs=window.location.hash+window.location.search;
      hs=hs.replace(/(\?)/, "\\$1");
      re = new RegExp("(\\.[c])?(" + "\\.htm"+hs+"$|\\.asp"+hs+"$)");
      u = u.replace(re, l+"$2");
      window.location.replace(u);
    }
  }
}



