//////////////////////////////////////////
//                                      //
//              XML TOOLS               //
//                                      //
//////////////////////////////////////////

XmlTools.ATTR_ID = "id";
XmlTools.ATTR_PARENT_ID = "parentId";

XmlTools.isXMLLoaded = false;

function XmlTools () {
}


// XML request handler //
XmlTools.importXML = function (file) 
{
  var xmlDoc; 

  if (System.moz) 
  { 
    xmlDoc = document.implementation.createDocument("", "", null) ;    
    xmlDoc.onload = XmlTools.onloadFunction;
    xmlDoc.async = false;
  } 
  else if (System.ie) 
  { 
    xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); 
    xmlDoc.async = false;   
    while(xmlDoc.readyState != 4) {};
  }

  xmlDoc.load(file);    
  XmlTools.waitXMLLoading();          
  return xmlDoc;
}

XmlTools.onloadFunction = function() 
{
  XmlTools.isXMLLoaded = true;
}

XmlTools.waitXMLLoading = function() 
{
  if(System.moz)
  {
    while(XmlTools.isXMLLoaded == false) {};
    XmlTools.isXMLLoaded = true;
  }
}




XmlTools.getChildNodes = function(parent, parentId, childTagName) 
{
  var childs = parent.getElementsByTagName(childTagName);
  var newChilds = new Array();
  var newChildsIndex = 0;
  for(var i=0;i<childs.length; i++)
  {
    var child = childs[i];
    if(parentId == null || (parentId != null && child.getAttribute(XmlTools.ATTR_PARENT_ID) == parentId))
    {
      newChilds[newChildsIndex] = child;
      newChildsIndex++;
    }
  }
  return newChilds;
}

XmlTools.getChildListCount = function(parent, parentId, childTagName) 
{
  var childs = parent.getElementsByTagName(childTagName);
  var count = 0;
  for(var i=0;i<childs.length; i++)
  {
    var child = childs[i];
    if(parentId == null || (parentId != null && child.getAttribute(XmlTools.ATTR_PARENT_ID) == parentId))
    {
      count++;
    }
  }  
  return count;
}

XmlTools.getNodeValue = function(items, itemIndex)
{
  var firstChild = null;
  if(itemIndex > -1)
  {
    firstChild = items[itemIndex].firstChild;
  }
  else
  {
    firstChild = items.firstChild;
  }
  return (firstChild == null) ? "" : firstChild.nodeValue;
}

XmlTools.isXMLFormatValid = function(xmlElements, filePath)
{
  if(xmlElements.length > 0)
  {
    return true;
  }
  else
  {
    alert("Datoteko '" + filePath + "' ne najdem\nali pa je neustreznega formata!")
    return false;  
  }
}


XmlTools.getLanguageNodeValue = function(nodeTag)
{
  var languageTag = nodeTag.getElementsByTagName(system.getLanguageName());
  var nodeValue = XmlTools.getNodeValue(languageTag, 0);
  return nodeValue
}
