﻿var pipe5Temp = document.URL.substr(0, document.URL.lastIndexOf('/')+1);

function callServer(idx, val, ptr, backURL) { // idx: ReqUserName / ReqPassword

    var MxHttp = new XmlHttpObject;
    var currThis = ptr;
    if (MxHttp.Get() == null)
        return;
    var url = pipe5Temp + "board/acctAjax.php?f=" + idx + "&d=" + val + "&sid=" + Math.random();
    MxHttp.httpObj.onreadystatechange = function() {
        if (MxHttp.httpObj.readyState == 4 || MxHttp.httpObj.readyState == "complete") {
            if (MxHttp.httpObj.responseText.substring(0, 3) == String.fromCharCode(0xef, 0xbb, 0xbf))  // BOM
              MxHttp.httpObj.responseText = MxHttp.httpObj.responseText.substring(3);
            var temp = MxHttp.httpObj.responseText.split("<$$$>");
            if (temp.length > 1 && temp[0].indexOf("ALERT") >= 0)
                alert(temp[1]);
            else
                currThis(MxHttp.httpObj.responseText, backURL);
        }
    }
    MxHttp.httpObj.open("GET", url, true);  // if post, '&'ed name-value pair in $par is sent in xmlHttp.send($par);
    MxHttp.httpObj.send(null);
}

function doLogin(id, pass, nextPath, failPtr) {

  MxmlHttp = new XmlHttpObject();
  var nPath = nextPath;
  var fPtr = failPtr;
  if (MxmlHttp.Get()==null)
    return;

  var d=new Date();
  var parm = "UserId=" + encodeURI(id) + "&UserPass=" + encodeURI(pass) + "&tZone=" + (-(d.getTimezoneOffset() * 60));
  MxmlHttp.httpObj.onreadystatechange = function() {
      if (MxmlHttp.httpObj.readyState == 4 || MxmlHttp.httpObj.readyState == "complete") {
          if (MxmlHttp.httpObj.responseText.substring(0, 3) == String.fromCharCode(0xef, 0xbb, 0xbf))  // BOM
              MxmlHttp.httpObj.responseText = MxmlHttp.httpObj.responseText.substring(3);
          if (MxmlHttp.httpObj.responseText.length > 2) {
              var temp = MxmlHttp.httpObj.responseText.split("<$$$>");
              if (temp[0].indexOf("SUCCESS") >= 0) {
                  document.getElementById('UserId').value = "";  // use session to relogin
                  document.getElementById('UserPass').value = "";
                  window.location.href = pipe5Temp + nPath;
              }
              else
                  failPtr(temp[0]);
          }
      }
  }
  MxmlHttp.httpObj.open("POST", pipe5Temp + nPath, true);  // if post, '&'ed name-value pair in $par is sent in xmlHttp.send($par);
  MxmlHttp.httpObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  MxmlHttp.httpObj.setRequestHeader("Content-length", parm.length);
  MxmlHttp.httpObj.send(parm);
  //MxmlHttp.httpObj.setRequestHeader("Connection", "close");  
}

function XmlHttpObject() {  
  this.httpObj = null;
  this.Get = function () {
    try {  // Firefox, Opera 8.0+, Safari    
      this.httpObj=new XMLHttpRequest();    
    } catch (e) {    // Internet Explorer    
      try  {  
        this.httpObj=new ActiveXObject("Msxml2.XMLHTTP");      
      } catch (e) {      
        try {        
          this.httpObj=new ActiveXObject("Microsoft.XMLHTTP");        
        }  catch (e) {        
          alert("Your browser does not support AJAX!");               
        }      
      }   
    }  
    return this.httpObj;
  }
}
