// PHP compatible functions
var dir_mtrclub = '/mtrclub/';

function php_urlencode(s) { // support ASCII code only
  return escape(s.toString()).replace(/\+/g,'%2B').replace(/%20/g,'+').replace(/\*/g,'%2A').replace(/\//g,'%2F').replace(/@/g,'%40');
}
  
function php_urldecode(s) { // support ASCII code only
  return unescape(s.toString().replace(/\+/g,' '));
}
// eof PHP compatible functions

// cookie functions
function setCookie(name, new_value, expires, path_link, domain_name, secure) {
  var curCookie = name + "=" + php_urlencode(new_value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path_link) ? "; path=" + path_link : "") +
      ((domain_name) ? "; domain=" + domain_name : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

function getCookieVal(offset) {
  var endstr = document.cookie.indexOf(";", offset);
  if (endstr == -1){
    endstr = document.cookie.length;
  }
  return php_urldecode(document.cookie.substring(offset, endstr));
}

function getCookie(name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;

  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg) {
      return getCookieVal(j);
    }
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) { break; }
  }
  return null;
}
// eof cookie function

// dreamweaver functions
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
// eof dreamweaver function

// freeze page functions
function freezePage() {
  var freeze_obj = document.getElementById("freezePageDiv");
  if (freeze_obj) {
    var w_height = Math.max(document.body.clientHeight, document.body.scrollHeight);
    w_height = (isNaN(document.body.offsetHeight) ? w_height : Math.max(w_height, document.body.offsetHeight));
    w_height = (isNaN(document.documentElement.clientHeight) ? w_height : Math.max(w_height, document.documentElement.clientHeight));
    w_height = (isNaN(document.documentElement.scrollHeight) ? w_height : Math.max(w_height, document.documentElement.scrollHeight));
    w_height = (isNaN(self.innerHeight) ? w_height : Math.max(w_height, self.innerHeight));

    freeze_obj.style.height = (w_height ? (w_height + "px") : "100%");
    freeze_obj.style.display = "block";
    showIndicator();
    
    var is_ie = (navigator.appName.toLowerCase().indexOf("microsoft") > -1);
    if (is_ie) { // for fixing IE windows control bug
      hiddenSelectMenu();
      hiddenControl("object");
      var freeze_obj_iframe = document.getElementById("freezeIFrame"); 
      if (freeze_obj_iframe) {
        var indicator_obj_table = document.getElementById("waitIndicatorTable");
        if (indicator_obj_table) {
          freeze_obj_iframe.style.top    = (indicator_obj_table.offsetTop + "px");
          freeze_obj_iframe.style.left   = (indicator_obj_table.offsetLeft + "px");
          freeze_obj_iframe.style.width  = (indicator_obj_table.offsetWidth + "px");
          freeze_obj_iframe.style.height = (indicator_obj_table.offsetHeight + "px");
        }
        freeze_obj_iframe.style.display = "block";
      }
    }
  }
}

function unfreezePage() {
  var freeze_obj = document.getElementById("freezePageDiv");
  if (freeze_obj) {
    removeIndicator();
    var is_ie = (navigator.appName.toLowerCase().indexOf("microsoft") > -1);
    if (is_ie) { // for fixing IE windows control bug
      showControl("object");
      showSelectMenu();
      var freeze_obj_iframe = document.getElementById("freezeIFrame");
      if (freeze_obj_iframe) { freeze_obj_iframe.style.display = "none"; }
    }
    freeze_obj.style.display = "none";
  }
}

function showIndicator() {
  var indicator_obj_table = document.getElementById("waitIndicatorTable");
  if (indicator_obj_table) {
    var w_yoffset;
    if (typeof(window.pageYOffset) == 'number') { w_yoffset = window.pageYOffset; }
    else if(document.body && document.body.scrollTop) { w_yoffset = document.body.scrollTop; }
    else { w_yoffset = document.documentElement.scrollTop; }
    indicator_obj_table.style.top = w_yoffset + 250;
  }
  var indicator_obj = document.getElementById("waitIndicatorDiv");
  if (indicator_obj) { indicator_obj.style.display = "block"; }
}

function removeIndicator() {
  var indicator_obj = document.getElementById("waitIndicatorDiv");
  if (indicator_obj) { indicator_obj.style.display = "none"; }
}

function showSelectMenu() {
  var tab_obj = document.getElementsByTagName("select");
  for (var i = 0, n = tab_obj.length; i < n; i++) {
    tab_obj[i].className = "enable_select_menu";
  }
}

function hiddenSelectMenu() {
  var tab_obj = document.getElementsByTagName("select");
  for (var i = 0, n = tab_obj.length; i < n; i++) {
    tab_obj[i].className = "disable_select_menu";
  }
}

function showControl(tag_name) {
  var tab_obj = document.getElementsByTagName(tag_name);
  for (var i = 0, n = tab_obj.length; i < n; i++) {
    if (tab_obj[i].style.visibility != "visible") { tab_obj[i].style.visibility = "visible"; }
  }
}

function hiddenControl(tag_name) {
  var tab_obj = document.getElementsByTagName(tag_name);
  for (var i = 0, n = tab_obj.length; i < n; i++) {
    if (tab_obj[i].style.visibility != "hidden") { tab_obj[i].style.visibility = "hidden"; }
  }
}

function showElement(name) {
  var obj = document.getElementById(name);
  if (obj) { obj.style.display = 'inline'; }
  else { 
    obj = document.getElementsByName(name);
    if (obj[0]) { obj[0].style.display = 'inline'; }
  }
}

function hideElement(name) {
  var obj = document.getElementById(name);
  if (obj) { obj.style.display = 'none'; }
  else { 
    obj = document.getElementsByName(name);
    if (obj[0]) { obj[0].style.display = 'none'; }
  }
}
function enableBtn(button_name) {
  var btn_obj = document.getElementById(button_name);
  if (btn_obj) { btn_obj.disabled = false; }
  else { 
    btn_obj = document.getElementsByName(button_name);
    if (btn_obj[0]) { btn_obj[0].disabled = false; }
  }
}

function disableBtn(button_name) {
  var btn_obj = document.getElementById(button_name);
  if (btn_obj) { btn_obj.disabled = true; }
  else { 
    btn_obj = document.getElementsByName(button_name);
    if (btn_obj[0]) { btn_obj[0].disabled = true; }
  }
}
// eof freeze page function

Array.prototype.in_array = function(stringToSearch) {
  for(var i = 0, n = this.length; i < n; i++) {
    if (this[i].toString() == stringToSearch) return true;
  }
  return false;
}

String.prototype.trim = function() {
  return this.replace(/^\s\s*/, "").replace(/\s\s*$/, "");
}

String.prototype.getBytes = function() {
  var cArr = this.match(/[^\x00-\xff]/ig);
  return this.length + (cArr == null ? 0 : cArr.length);
}

function trim(str) { // for version compatibility
  return str.trim();
}

function isFilled(elm) {
  if (elm == null) { return false; }
  if (elm.trim() == "") { return false; }
  return true;
}

function isInt(elm) {
  var elmstr = elm.value + "";

  if (elmstr == "") { return false; }

  for (var i=0; i<elmstr.length; i++) {
    if (elmstr.charAt(i) < "0" || elmstr.charAt(i) > "9") {
      return false;
    }
  }
  return true;
}

function isLogin() {
  if (getCookie("session_var") != null) {
    return true;
  }
  return false;
}

function loadScript(filename) {
  var filename_lower = filename.toLowerCase();

  if (filename_lower.indexOf(".js") > 0) {
    document.write('<script language="javascript" src="' + filename + '" type="text/javascript"></script>');
  }
  else if (filename_lower.indexOf(".css") > 0) {
    document.write('<link rel="stylesheet" href="' + filename + '" type="text/css">');
  }
}

function setURL(path_link) {
  window.location = path_link;
}

function redirect(path_link) {
  document.location.href = path_link;
  return false;
}

function updateSessionLang(new_lang) {
  //var today = new Date();
  //var expire = new Date();
  //expire.setTime(today.getTime() + lang_sess_expiry);
  //setCookie(lang_sess_id, new_lang, expire, "/");
  setCookie(lang_sess_id, new_lang, 0, "/"); // changed period to a session only
}

function switchLang(new_lang) {
  var new_loc  = window.location + "";

  // for the path like /chi/ or /eng/
  if (!lang_id_array.in_array(new_lang)) { new_lang = lang_default_id; }
  for(i = 0, n = lang_id_array.length; i < n; i++) {
    lang_dir = "/" + lang_id_array[i] + "/";
    if (new_loc.indexOf(lang_dir) > 0) {
      updateSessionLang(new_lang);
      new_loc = new_loc.replace(lang_dir, "/" + new_lang + "/");
      break;
    }
  }

  // for the path like http://.../promotion.php?name=abc&lang=chi
  if (new_loc.indexOf("lang=") > 0) {
    new_loc = new_loc.replace(/lang=[\w]*/, "lang=" + new_lang);
  }
  else {
    // for the path like http://.../promotion.php?name=abc
    var new_loc_lower = new_loc.toLowerCase();
    if (new_loc_lower.indexOf(".php") > 0) {
      if (new_loc.indexOf("?") > 0) {
        new_loc = new_loc.replace(/(\.php[\d]?\?)/i, "$1" + "lang=" + new_lang + "&");
      }
      else {
        new_loc = new_loc.replace(/(\.php[\d]?)/i, "$1" + "?lang=" + new_lang);
      }
    }
    else if (new_loc_lower.indexOf(".htm") < 0 && new_loc.charAt(new_loc.length - 1) == "/") {
      // for the path like http://.../
      new_loc += "index.php?lang=" + new_lang;
    }
  }
  window.location = new_loc;
}

function popup(path_link, w_width, w_height, w_name) {
  if (!w_width)  { w_width  = 800; }
  if (!w_height) { w_height = 500; }
  if (!w_name)   { w_name   = 'mtr_reward'; }
  var t_top  = Math.max(0, (screen.height/2)-(w_height/2));
  var t_left = Math.max(0, (screen.width/2)-(w_width/2));
  msgWin = window.open(path_link, w_name, 'menubar=no,resizable=yes,scrollbars=yes,width=' + w_width + ',height=' + w_height + ",top=" + t_top + ",left=" + t_left);
  msgWin.focus();
}

function popupShop(path_link, w_width, w_height, w_name) {
  popup(path_link, w_width, w_height, 'mtrshop');
}

function getMenuPosByPageID(menu_array, page_id) {
  var pos_id = null;
  if (page_id != "") {
    for (var i = 0, n = menu_array.length; i < n; i++) {
      if (page_id == menu_array[i][0]) { pos_id = i; break; }
    }
  }
  return pos_id;
}

function updateMenuBtnOn(menu_array, page_id) {
  var menu_selected = getMenuPosByPageID(menu_array, page_id);
  if (menu_selected != null) {
    var image_obj = document.getElementById("menu_btn_" + page_id);
    var line_obj = document.getElementById("menu_line_id");
    if (image_obj) {
      image_obj.src = menu_array[menu_selected][4];
      image_obj.onmouseover = "";
      image_obj.onmouseout = "";
    }
    if (line_obj) {
      line_obj.style.backgroundColor = menu_array[menu_selected][5];
    }
  }
}

function reset_form(mf, except_arr) {
  if (except_arr == null) {
    except_arr = new Array();
  }
  for(var i = 0, n = mf.elements.length; i < n; i++) {
    var elem = mf.elements[i];
    if (elem.disabled != true && elem.readonly != true) {
      if (except_arr.in_array(elem.name) == false) {
        if ((elem.type == "text") || (elem.type == "textarea")) {
          elem.value = "";
        }
        else {
          if (elem.type == "select-multiple") {
            for(var j = 0, m = elem.options.length; j < m; j++) {
              elem.options[j].selected = false;
            }
          }
          else {
            if (elem.type == "select-one") {
              elem.selectedIndex = 0;
            }
            else {
              if (elem.type == "checkbox") {
                elem.checked = false;
              }
            }
          }
        }
      }
    }
  }
}

function getDateString(input_date) {
  var month = new String(input_date.getMonth() + 1);
  var day = new String(input_date.getDate());
  var year = new String(input_date.getFullYear());

  if (month.length < 2) {
    month = "0" + month;
  }

  if (day.length < 2) {
    day = "0" + day;
  }
  return year + month + day;
}

function reloadCaptchaImage(target_image_id, submit_btn_id, upd_cookie_name) { // required jquery lib
  var jq_target_image_id = "#" + target_image_id;
  $.ajax({
    type: "GET",
    url: "image.php",
    data: {action: "reload_code"},
    dataType: "text",
    beforeSend: function() { disableBtn(submit_btn_id); },
    error: function() { enableBtn(submit_btn_id); },
    success: function(msg) {
      msg = trim(msg);
      if (msg != '') {
        $(jq_target_image_id).fadeOut("normal", function() {
          if (typeof upd_cookie_name != 'undefined') {
            setCookie(upd_cookie_name, msg, 0, "/", "", 1);
          }
          $(jq_target_image_id).attr('src', "image.php?img=" + php_urlencode(msg));
          $(jq_target_image_id).fadeIn("slow", function() { enableBtn(submit_btn_id); });
        });
      }
    }
  });
}

