function RemoteRequest() {};
RemoteRequest.prototype = {
    _method: 'POST',
    _URL: '',
    _encoding: 'UTF-8',
    _isSynchronized: false,
    _xmlHttpReq: '',
    _resultString: '',
    open: function(method, URL, isSynchronized) {
        this._method = method;
        this._URL = URL;
        this._isSynchronized = isSynchronized;
    },
    initial: function() {
        var xmlHttpReq;
        // Mozilla/Safari
        if (window.XMLHttpRequest) {
            xmlHttpReq = new XMLHttpRequest();
            if (xmlHttpReq.overrideMimeType) {
                xmlHttpReq.overrideMimeType('text/xml');
            }
        }
        // IE
        else if (window.ActiveXObject) {
                xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
        }
        this._xmlHttpReq = xmlHttpReq;
    },
    request: function(body) {
        this.initial();
        var request = this;
        this._xmlHttpReq.onreadystatechange = function() {
            handleResponse(request);
        }
        this._xmlHttpReq.open(this._method, this._URL, false);
        this._xmlHttpReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded;charset=utf-8');       
        this._xmlHttpReq.send(body);
        if(navigator.userAgent.indexOf("MSIE")>0){
            
        }else{
            this._xmlHttpReq.onreadystatechange = handleResponse(request);
        }
        return this._resultString;
    },
    asychronizedRequest: function(body) {
        this.initial();
        this._xmlHttpReq.open(this._method, this._URL, this._isSynchronized);
        this._xmlHttpReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
        var request = this;
        this._xmlHttpReq.onreadystatechange = function() {
            handleResponse(request);
        }
        this._xmlHttpReq.send(body);
    },
    overload: function(result) {
    }
}

function handleResponse(request) {
    if (request._xmlHttpReq.readyState == 4) {
        var isBackendEorror = false;
        request._resultString = request._xmlHttpReq.responseText;
        switch (request._xmlHttpReq.status) {
           // Page-not-found error
           case 404:
               alert('Error: Not Found. The requested URL ' + request._URL + ' could not be found.');
               break;
           // Display results in a full window for server-side errors
           case 500:
               alert("server side error: \n" + request._resultString);
               break;
           default:
	       // Call JS alert for custom error or debug messages
               if (request._resultString.indexOf('errorMsgKey') > -1 || 
                       request._resultString.indexOf('Debug:') > -1) {
                       isBackendEorror = true;
                       //goToError(request._resultString);
                         
               }
               break;
        }
        if (isBackendEorror == true) {
            goToError(request._resultString);
        } else {
        	request.overload(request._resultString);
        }
   }
}

function goToError(message) {
  eval("var errorMessage = " + message + ";");
  var error = errorMessage.errorMsgKey;
  
  if (error.indexOf("WrapException") > -1) {
      goToPage("/hrms/resource/jsp/util/wrapError.jsp?error=" + error.substr(14)); 
  } else {
      goToPage("/hrms/resource/jsp/util/error2.jsp?error=" + error);
  }
   
  
}

