// -- Common --

function Debug() {
  //alert("Debug");
  //alert(gLang+","+gUserEmail+","+gUserName+","+gUserScope);
  //ShowAlert("Test", "JCCACmate");
  //ShowConfirm("Test", "Are you sure? 取消", Test, "&1&2&3");
  
  var tHTML = document.getElementById("summary.table").innerHTML;
  //tHTML = "aabbcc\n\rbbaacc";
  alert(tHTML);
  //var tRE = new RegExp("<!--row start:20090422_133315_gj4AsjHbKG-->.*<!--row end:20090422_133315_gj4AsjHbKG-->", "gi");
  //var tRE = new RegExp("aa[.a-z\\s\\S]*aa", "gi");
  var tRE = new RegExp("<!--row start:20090422_133315_gj4AsjHbKG-->[\\s\\S]*<!--row end:20090422_133315_gj4AsjHbKG-->", "gi");
  tHTML = tHTML.replace(tRE, "");
  alert(tHTML);
  document.getElementById("summary.table").innerHTML = tHTML;
}

function Test(a, b, c) {
  alert(a+","+b+","+c);
}


// -- Login --

function ShowLogin(message, error) {
  //alert("ShowLogin");
  if(message==undefined) message = "";
  if(error==undefined) error = "";
  
  document.getElementById("login.message").innerHTML = message;
  document.getElementById("login.email").value = "";
  document.getElementById("login.password").value = "";
  document.getElementById("login.error").innerHTML = error;
  document.getElementById("login").style.display = "block";
  document.getElementById("login.email").focus();
}

function HideLogin() {
  document.getElementById("login").style.display = "none";
}

function ProcessLogin() {
  var email    = document.getElementById("login.email").value;
  var password = document.getElementById("login.password").value;

  email = email.trim();
  
  if(email=="") {
    document.getElementById("login.error").innerHTML = GetLangString(gLang, "Please input email!", "請輸入電郵！", "请输入电邮！");
    document.getElementById("login.email").focus();
    document.getElementById("login.email").select();
  }
  else {
    xmlHttp=GetXmlHttpObject();
  
    if (xmlHttp==null) {
      alert ("Your browser does not support AJAX!");
      return;
    }

    var url="";
    url=url+"login.ajax.php";
    url=url+"?action=login";
    url=url+"&email="   +encodeURIComponent(email);
    url=url+"&password="+encodeURIComponent(password);
    //alert(url);
    xmlHttp.onreadystatechange=ProcessLoginCompleted;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
  }
}

function ProcessLoginCompleted() {
  if (xmlHttp.readyState==4) {
    //alert("ProcessLoginCompleted");
    //alert("<bof>"+xmlHttp.responseText+"<eof>");
    var tResArray = xmlHttp.responseText.split("\n");
    //alert(tResArray[0]+","+tResArray[1]+","+tResArray[2]+","+tResArray[3]);
    
    if(tResArray[0]=="0") {
      window.location = window.location;
      /*
      gUserNumber = tResArray[1];
      gUserEmail  = tResArray[2];
      gUserName   = tResArray[3];
      gUserScope  = tResArray[4];
      HideLogin();
      Topbar_UpdateLogin();
      Sidebar_UpdateUserMenu();
      var tTitle = GetLangString(gLang, "Login", "用戶登入", "用户登入");
      var tText  = GetLangString(gLang, "Welcome, ", "歡迎，", "欢迎，");
      ShowAlert(tTitle, tText + gUserName);
      */
    }
    else {
      var tEmail = document.getElementById("login.email").value;
      ShowLogin("", "登入失敗！請再嘗試！");
      document.getElementById("login.email").value = tEmail;
    }
  }
}


// -- Logout --

function ConfirmLogout() {
  var tTitle = GetLangString(gLang, "Logout", "用戶登出", "用户登出");
  var tText  = GetLangString(gLang, "Are you sure to logout?", "確定登出？", "确定登出？");
  ShowConfirm(tTitle, tText, ProcessLogout, "");
}

function ProcessLogout() {
  xmlHttp=GetXmlHttpObject();
  
  if (xmlHttp==null) {
    alert ("Your browser does not support AJAX!");
    return;
  }

  var url="";
  url=url+"login.ajax.php";
  url=url+"?action=logout";
  //alert(url);
  xmlHttp.onreadystatechange=ProcessLogoutCompleted;
  xmlHttp.open("GET",url,true);
  xmlHttp.send(null);
}

function ProcessLogoutCompleted() {
  if (xmlHttp.readyState==4) {
    //alert("ProcessLogoutCompleted");
    //alert("<bof>"+xmlHttp.responseText+"<eof>");
    var resArray = xmlHttp.responseText.split("\n");
    //alert(resArray[0]+","+resArray[1]+","+resArray[2]+","+resArray[3]);
    if(resArray[0]=="0") {
      window.location = window.location;
      /*
      var tName = gUserName;
      gUserNumber = "";
      gUserEmail = "";
      gUserName  = "";
      gUserScope = "";
      Topbar_UpdateLogin();
      Sidebar_UpdateUserMenu();
      var tTitle = GetLangString(gLang, "Logout", "用戶登出", "用户登出");
      var tText  = GetLangString(gLang, "See you later, ", "再見，", "再见，");
      ShowAlert(tTitle, tText + tName);
      */
    }
    else {
      ShowAlert("", "登出失敗！請再嘗試！");
    }
  }
}


// -- Alert --

function ShowAlert(title, text) {
  //alert("ShowAlert");
  if(title==undefined) title = "";
  if(title=="")        title = "&nbsp;"
  if(text==undefined) text = "";
  if(text=="")        text = "&nbsp;";
  
  document.getElementById("alert.title").innerHTML = title;
  document.getElementById("alert.text").innerHTML = text;
  document.getElementById("alert").style.display = "block";
}

function HideAlert() {
  document.getElementById("alert").style.display = "none";
}


// -- Confirm --

var confirmNextFunc;
var confirmNextPara;

function ShowConfirm(title, text, nextFunc, nextPara) {
  //alert("ShowConfirm");
  if(title==undefined) title = "";
  if(text==undefined) text = "&nbsp;";
  
  document.getElementById("confirm.title").innerHTML = title;
  document.getElementById("confirm.text").innerHTML = text;
  document.getElementById("confirm").style.display = "block";
  document.getElementById("confirm.ok").focus();
  
  confirmNextFunc = nextFunc;
  confirmNextPara = nextPara;
}

function HideConfirm(action) {
  document.getElementById("confirm").style.display = "none";

  if(action==undefined) action = "";
  if(action=="ok" && confirmNextFunc!=undefined) {
    CallFunction(confirmNextFunc, confirmNextPara, "&");
  }
}

