/**
 * Performs an XMLHTTP request to the given target
 *
 * @param string
 * @param string
 * @param object (callback function)
 * @return void
 */
function mc_xmlHttpCommand(method, target, callback) {
  var xmlhttp = new MC_XmlHttp();
  
  xmlhttp.setReadyStateChangeHandler(
    function () {
      if (4 != xmlhttp.getReadyState()) {
        return;
      }
      
      // check http status code
      if (!(xmlhttp.getStatus() >= 200 && xmlhttp.getStatus() < 300)) {
        xmlhttp.error('Error retrieving XML data');
        return;
      }
      
      // invoke callback-function, pass xml document object along
      callback(xmlhttp.getResponseXML());
    }
  );
  
  // todo - support for http post
  xmlhttp.open(method, target);
  xmlhttp.send();
} // end func mc_xmlHttpCommand


//
// actions
//

/**
 * Attemps to move a catalog category
 *
 * @return void
 */
function moveCategory() {
  // first, set buttons inactive
  document.getElementById('move').disabled = true;
  document.getElementById('cancel').disabled = true;
  
  var cid      = document.getElementById('cid').getAttribute('value');
  var src_cid  = document.getElementById('src_cid').getAttribute('value');
  var dest_cid = document.getElementById('dest_cid').getAttribute('value');
  
  var url = MC_ADMIN_BASE_URL +'articles/catalog_ajax.php'+
    '?node_method=moveCategory'+
    // todo todo todo -- extract node type ('ac' or 'vc'?)
    '&node_type=ac'+
    '&node_id='+ encodeURIComponent(cid) +
    '&node_parent_id='+ encodeURIComponent(src_cid) +
    '&node_method_args='+ encodeURIComponent('dest_cid:'+ dest_cid);
  
  mc_xmlHttpCommand('GET', url,
    function(xml) {
      var udata = xml.getElementsByTagName('userdata')[0];
      
      if ('err' == udata.getAttribute('name')) {
        parent.showUserInfo(udata.firstChild.nodeValue);
      } else {
        parent.moveTreeItem();
        parent.showUserInfo(udata.firstChild.nodeValue);
        location = MC_ADMIN_BASE_URL +'articles/catalog/tree_start_page.php';
      }
    }
  );
} // end func moveNode

/**
 * Cancels the move-into operation
 *
 * @return void
 */
function cancelMove() {
  // first, set buttons inactive
  document.getElementById('move').disabled = true;
  document.getElementById('cancel').disabled = true;
  
  // move to start page (or whatever...)
  location = MC_ADMIN_BASE_URL +'articles/catalog/tree_start_page.php';
} // end func cancelMove


/**
 * Creates and article link in the selected category
 *
 * Note: One cannot create a link to a variant article, so we don't have to
 * care about node type.
 *
 * @param int (id of the article to link)
 * @param int (id of article's parent category)
 * @param int (id of the category to link the article into)
 * @return void
 */
function linkArticle(aid, src_cid, dest_cid) {
  var url = MC_ADMIN_BASE_URL +'articles/catalog_ajax.php'+
    '?node_method=linkArticle'+
    '&node_type=a'+
    '&node_id='+ encodeURIComponent(aid) +
    '&node_parent_id='+ encodeURIComponent(src_cid) +
    '&node_method_args='+ encodeURIComponent('dest_cid:'+ dest_cid);
  
  mc_xmlHttpCommand('GET', url,
    function(xml) {
      var udata = xml.getElementsByTagName('userdata')[0];
      
      if ('err' == udata.getAttribute('name')) {
        showUserInfo(udata.firstChild.nodeValue);
      } else {
        showUserInfo(udata.firstChild.nodeValue);
        // create article link node in tree
      }
    }
  );
} // end func linkArticle
