G_itemSplitter = "<!itemSplitter>";
G_varSplitter = "<!varSplitter>";
G_boundary = '------GCMS1234567890';
G_errorToken = '<!errorToken>';

function getHttp()
{
  var obj;

  if (typeof XMLHttpRequest != 'undefined') 
    obj = new XMLHttpRequest();
  else if (typeof ActiveXObject != 'undefined') 
    obj = new ActiveXObject('Microsoft.XMLHTTP');
  return obj;
}

function rpcQuery(P_url, P_variables, P_urlIsAbsolute)
{
  var http = getHttp();
  var body = "--" + G_boundary;

  var dummy = {} 

  // Prepare data to send
  if ( P_variables != null )
  {
    for ( var name in P_variables )
    {
      // those line were added to get rid of extra properties and methods added by mootools
      //if ( typeof(dummy[name]) != 'undefined' )
      //  continue;
      // but actually it should be done in different way:
      // params parameter must be defined as an object ( {} ), not as an array


      var variable = P_variables[name]
      body += "\nContent-Disposition: form-data; name=\"" + name + "\"\n" +
              "Content-Type: text/plain; charset=iso-8859-2\n" +
              "\n" + variable.toString() + "\n--" + G_boundary;
    }
  }
  body += "--";

  if ( typeof (P_urlIsAbsolute) == "undefined" )
  {
    // compose full script url
    var url = document.location.href;
    url = url.substring(0, url.lastIndexOf('/')) + '/' + P_url;
  }
  else
  {
    var url = P_url
  }

  try {
    http.open('POST', url, false);
    http.setRequestHeader("Content-Type", "multipart/form-data; boundary=" + G_boundary);
    http.setRequestHeader("Host", url.replace(/^https?:\/{2}([:\[\]\-\w\.]+)\/?.*/, '$1'));
  }
  catch(e)
  {
    alert('http exception: ' + e.message);
    return false;
  }
  try {
    http.send(body);
  }
  catch(e)
  {
    alert('http excption: ' +e.message);
    return false;
  }
  if ( http.status == 200 )
  {
    if (http.responseText && http.responseText.length > 0)
    {
      // if ( response[0] != 'OK' && response[0].indexOf('Fatal error') != -1 )
      if ( http.responseText.indexOf(G_errorToken) != -1 )
      {
        var emergencyResponse = new Array();
        emergencyResponse[0] = 'ERROR';
        emergencyResponse[1] = 'PHP reports: <br />' + http.responseText;
        return emergencyResponse;
      } 
      var response = http.responseText.split(G_itemSplitter);
      if ( response[0] == 'NOTLOGGED')
        top.location = '.' 
      return response;
    }
    return false;
  }
  else
  {
    alert('HTTP Status: ' + http.status);
    return false;
  }
}

function rpcSendForm(P_formName, P_rpcPlugin, P_okMsg, P_redirect, P_redirectCurrent)
{
  var params = getFormAsArray(P_formName);
  var result = rpcQuery('rpc.php?function=' + P_rpcPlugin, params);
  if ( result[0] != 'OK' )
  {
    // some error ocurred
    clearStyles();
    if ( result[2] && result[2].length > 0 )             // styles are to be set
      setStyle('td_' + result[2], 'description_error');
    if ( result[1] && result[1].length > 0 )             // expected answer
      parent.showAlert(result[1]);
                            /* previous solution: */
    /*
    else if ( result[0] && result[0].length > 0 )        // unexpected answer 
      parent.showAlert('Unexpected answer: ' +  result[0]);
    else                                                 // all is corrupt
      alert(result);
    */
  }
  else
  {
    if ( P_redirect )
      document.location = P_redirect;
    else if ( P_redirectCurrent )
    {
      var result = rpcQuery('rpc.php?function=getCurrentOid', null);
      if ( result[0] == 'OK' )
        document.location = 'page.php?page=content&id=' + result[1];
    }
    else
    {
      clearStyles();
      parent.showAlert(P_okMsg, 'info');
      if ( result[1] == 'REFRESH' )
        parent.document.location.reload();
      if ( result[1] == 'REFRESHFRAME' )
        location.reload();
    }
  }
}


