var questions_clicked = new Array();

/**
 * Function for saving faq structure via Ajax.
 */
function save_faq_structure(data)
{
  if (xml_http) {
    //process request only when object is ready
    if ((xml_http.readyState == 4) || (xml_http.readyState == 0)) {
      try {
        xml_http.open('POST', SAVE_FAQ_STRUCTURE_SCRIPT, true);
        xml_http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
        xml_http.onreadystatechange = handle_change_save_faq_structure;
        xml_http.send(data);
      } catch(e) {
        alert("Can\'t connect to server:\n" + e.toString());
      }
    } else {
      setTimeout('save_faq_structure('+data+')', 500);
    }
  }
}

/**
 * Function to handle request state change when saving faq structure
 */
function handle_change_save_faq_structure()
{
  //ready state must be 4
  if (xml_http.readyState == 4)
  {
    //status must be "OK" (200)
    if (xml_http.status == 200) {
      try {
        handle_response_save_faq_structure();
      } catch(e) {
        alert("Error reading the response:\n" + e.toString());
      }
    } else {
      alert("There was a problem retrieving the data:\n" + xml_http.statusText);
    }
  }
}

/**
 * Function to handle server response when saving faq structure
 */
function handle_response_save_faq_structure()
{
  var xml_response = xml_http.responseXML;
  //catch IE and Opera errors
  if ((!xml_response) || (!xml_response.documentElement)) {
    throw("Invalid XML structure:\n" + xml_http.responseText);
  }
  //catch Firefox errors
  var root_node_name = xml_response.documentElement.nodeName;
  if (root_node_name == "parsererror") {
    throw('Invalid XML structure.');
  }

  //get root node
  var root_node = xml_response.documentElement;

  var result_nodes = root_node.getElementsByTagName('result');
  var result_node = result_nodes[0];

  var result;
  if (result_node.textContent) {
    result = result_node.textContent;
  } else {
    result = result_node.text;
  }

  var error_nodes = root_node.getElementsByTagName('error_nodes');
  if (error_nodes.length > 0) {
    var error_node = error_nodes[0];

    var nodes = error_node.getElementsByTagName('node');
    for (var i = 0; i < nodes.length; i++) {
      var node = nodes[i];

      var id;
      if (node.textContent) {
        id = node.textContent;
      } else {
        id = node.text;
      }

      if (document.getElementById('structure_item_'+id).getAttributeNode("class")) {
        var attr_class = document.getElementById('structure_item_'+id).getAttributeNode("class");
        attr_class.nodeValue += " wrong_place";
      } else {
        var attr_class = document.createAttribute("class");
        attr_class.nodeValue = "wrong_place";
        document.getElementById('structure_item_'+id).setAttributeNode(attr_class);
      }
    }
  }

  alert(result);
}

/**
 * Function for sending click event to question counter
 */
function click_question(id, category)
{
  // handle duplicit clicks
  for (var i = 0; i < questions_clicked.length; i++) {
    if (questions_clicked[i] == id) return;
  }

  questions_clicked[id] = id;

  if (xml_http) {
    //process request only when object is ready
    if ((xml_http.readyState == 4) || (xml_http.readyState == 0)) {
      try {
        xml_http.open('POST', CLICK_QUESTION_SCRIPT, true);
        xml_http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
        xml_http.send('id='+id+'&category='+category);
      } catch(e) {
        // ignore
      }
    } else {
      setTimeout('save_faq_structure('+data+')', 500);
    }
  }
}


