function msg_container()
{
  this.value = [];

  this.clear = msg_container_clear;
  this.add_msg = msg_container_add_msg;
  this.is_msg = msg_container_is_msg;

  this.get_msg_urlencode = msg_container_get_msg_urlencode;
  this.get_msg_formencode = msg_container_get_msg_formencode;
}

function msg_container_clear()
{
  this.value = [];
}

function msg_container_add_msg(key, item)
{
  if(!is_string(key) || strlen(key)===0) return false;

  this.value[key] = item;

  //alert('ins: ' + this.value[key]);

  return true;
}

function msg_container_get_msg_urlencode()
{
  //if(this.value.length === 0) return '';

  var rc = '';
  var is_first = true;
  var cur_value = null;

  for(var key in this.value)
  {
    if(is_first) is_first = false;
    else rc += '&';

    //alert(key);

    cur_value = this.value[key];

    rc += key + '=' + encodeURIComponent(cur_value);
  }

  return rc;
}

function msg_container_get_msg_formencode(boundary)
{
  if(!is_string(boundary) || strlen(boundary) === 0) boundary = '---lkjsdfl342j5lksdjfl23j5sdflkj';

  var js_ret = "\r\n"; // "\r\n"

  var rc = boundary + js_ret;
  var is_first = true;
  var cur_value = null;

  for(var key in this.value)
  {
    if(is_first) is_first = false;
    else rc += boundary + js_ret;

    cur_value = this.value[key];

    rc += 'Content-Disposition: form-data; name="' + key + '"' + js_ret + /*'Content-Type: text/plain; charset=ISO-8859-1;' + js_ret +*/ js_ret + cur_value + js_ret;
  }

  rc += boundary + js_ret;

  return rc;
}


function msg_container_is_msg(key)
{
  return (is_string(key) && strlen(key)>0 && array_key_exists(key, this.value));
}

function ajax_request()
{
  this.req = null;
  this.dom_hnd = null;

  this.init = ajax_init;
  this.isAjax = ajax_isAjax;
  this.read = ajax_read;
  this.post = ajax_post;
  this.post_urlencode = ajax_post_urlencode;
  this.post_formencode = ajax_post_formencode;
  
  this.getXmlObj = ajax_getXmlObj;

  this.init();

  return this;
}

function ajax_init()
{
  try
  {
    this.req = new ActiveXObject('MSXML2.XMLHTTP');
  }
  catch(err_MSXML2)
  {
    try
    {
      this.req = new ActiveXObject('Microsoft.XMLHTTP');
    } catch(err_Microsoft)
    {
      if(typeof XMLHttpRequest != 'undefined')
        this.req = new XMLHttpRequest;
    }
  }
}

function ajax_isAjax()
{
  return (this.req != null);
}

function ajax_read(file_name, with_info)
{
  if(!this.isAjax() ||
     typeof file_name != 'string' || file_name.length === 0) return false;

  if(!is_bool(with_info)) with_info = true;

  try
  {
    this.req.open('GET', file_name, false);

    this.req.send(null);
  } catch(noRead)
  {
    if(with_info) alert('NO FILE: ' + file_name);
    return false;
  }

  if(this.req.readyState === 4) return this.req.responseText;

  return false;
}

function ajax_post(file_name, msg, with_info)
{
  if(!this.isAjax() ||
     typeof file_name != 'string' || file_name.length === 0) return false;

  if(!is_bool(with_info)) with_info = true;

  try
  {
    this.req.open('POST', file_name, false);

    this.req.send(msg);
  } catch(noRead)
  {
    if(with_info) alert('NO FILE: ' + file_name + ', STATUS: ' + this.req.status);
    return false;
  }

  if(this.req.readyState === 4) return this.req.responseText;

  return false;
}

function ajax_post_urlencode(file_name, msg, with_info)
{
  if(!this.isAjax() ||
     typeof file_name != 'string' || file_name.length === 0) return false;

  if(!is_bool(with_info)) with_info = true;

  try
  {
    var to_send = msg.get_msg_urlencode();

    this.req.open('POST', file_name, false);

    this.req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
    this.req.setRequestHeader('Content-length', to_send.length);
    this.req.setRequestHeader('Connection', 'close');

    this.req.send(to_send);
  } catch(noRead)
  {
    if(with_info) alert('NO FILE: ' + file_name + ', STATUS: ' + this.req.status);
    return false;
  }

  if(this.req.readyState === 4) return this.req.responseText;

  return false;
}

function ajax_post_formencode(file_name, msg, with_info)
{
  if(!this.isAjax() ||
     typeof file_name != 'string' || file_name.length === 0) return false;

  if(!is_bool(with_info)) with_info = true;

  try
  {
    var boundaryString = 'FORMENCODE';
    var boundary = '--' + boundaryString;
    var to_send = msg.get_msg_formencode(boundary);

    //document.tree.get_in.value = to_send;

    //alert(to_send);

    this.req.open('POST', file_name, false);

    // charset=UTF-8;
    // charset=ISO-8859-1;

    //this.req.setRequestHeader('Accept-Charset', 'iso-8859-1,*,utf-8');

    this.req.setRequestHeader('Connection', 'close');
    this.req.setRequestHeader('Content-type', 'multipart/form-data; boundary="' + boundaryString + '"');
    this.req.setRequestHeader('Content-length', to_send.length);

    this.req.send(to_send);
  } catch(noRead)
  {
    if(with_info) alert('NO FILE: ' + file_name + ', STATUS: ' + this.req.status);
    return false;
  }

  //alert(this.req.getAllResponseHeaders());

  if(this.req.readyState === 4) return this.req.responseText;

  return false;
}

function ajax_getXmlObj(xml_content)
{
	if(!is_string(xml_content) || strlen(xml_content) === 0) return null;
	
	if(this.dom_hnd === null)
	{
    if(window.ActiveXObject) 
    {
      this.dom_hnd = new ActiveXObject('Microsoft.XMLDOM');
    } 
    else if(document.implementation) 
    {
      this.dom_hnd = new DOMParser();
    }		
	}
	
	var rc = this.dom_hnd;
	
  if(window.ActiveXObject) 
  {
    rc.loadXML(xml_content);
  } 
  else if(document.implementation) 
  {
    rc = rc.parseFromString(xml_content, 'text/xml');
  }	
      
  return rc;
}
