var xmlDoc;


// _____________________________________________________________________

//     Codigo para expandir la funcionalidad selectnodes y xml a FireFox
// ______________________________________________________________________

/*
Prefix-correcting evaluate statement from http://www.faqts.com/knowledge_base/view.phtml/aid/34022/fid/119
*/

if( document.implementation.hasFeature("XPath", "3.0") ){
 XMLDocument.prototype.selectNodes = function(cXPathString, xNode){
  if( !xNode ) {
   xNode = this;
  }
    
  var defaultNS = this.defaultNS;

  var aItems = this.evaluate(cXPathString, xNode,{
   normalResolver:
    this.createNSResolver(this.documentElement),
        lookupNamespaceURI : function (prefix) {
           switch (prefix) {
             case "dflt":
                return defaultNS;
             default:
                return this.normalResolver.lookupNamespaceURI(prefix);
           }
        }
      },XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null);

  var aResult = [];
  for( var i = 0; i < aItems.snapshotLength; i++){
           aResult[i] =  aItems.snapshotItem(i);
  }
  return aResult;
 }

 Element.prototype.selectNodes = function(cXPathString){
  if(this.ownerDocument.selectNodes){
   return this.ownerDocument.selectNodes(cXPathString, this);
  }else{
   throw "For XML Elements Only";
  }
 }

 /* set the SelectionNamespaces property the same for NN or IE: */
 XMLDocument.prototype.setProperty = function(p,v){
  if(p=="SelectionNamespaces" && v.indexOf("xmlns:dflt")==0){
   this.defaultNS = v.replace(/^.*=\'(.+)\'/,"$1");
  }
 }

 XMLDocument.prototype.defaultNS;

}

function serializeXml(oNode)
{
  var oSerializer = new XMLSerializer();
  return oSerializer.serializeToString(oNode);
}
// _____________________________________________________________________

// Fin Codigo para expandir la funcionalidad selectnodes y xml a FireFox
// ______________________________________________________________________





function ListOnChange(bloque,obj,prop,filtro,dst)
{
  var to=document.getElementById("adv_"+dst);
  to.innerHTML="<img src='pages/images/loading.gif' align='absmiddle'>";

  var blklist = document.getElementById(bloque);
  var selecteditem = blklist.options[blklist.selectedIndex].value;

  xmlDoc=GetXmlHttpObject();
  if (xmlDoc==null)
  {
    alert ("Browser does not support HTTP Request");
    return;
  } 
  var url="pages/xml_data_provider.php" + "?obj="+obj+"&prop="+prop+"&filtro="+filtro+"&valor="+encodeURIComponent(selecteditem)+"&dst="+dst;
  xmlDoc.onreadystatechange=stateChanged;
  xmlDoc.open("GET",url,true);
  xmlDoc.send(null);
}

function stateChanged()
{ 
  if (xmlDoc.readyState==4 || xmlDoc.readyState=="complete")
  {
    PopulateList(xmlDoc.responseXML.documentElement);
  } 
} 


function PopulateList(itemNode)
{	
  var nodes=xmlDoc.responseXML.selectNodes("//bloque");
  var dst=nodes[0].getAttribute('dst');

  var itemList = document.getElementById(dst);
	for (var count = itemList.options.length-1; count >-1; count--)
	{
		itemList.options[count] = null;
	}

	var itemNodes = itemNode.getElementsByTagName('item');
	var textValue; 
	var optionItem;
	for (var count = 0; count < itemNodes.length; count++)
	{ 
   	textValue = (GetInnerText(itemNodes[count]));
		idValue=count;		
    idValue=itemNodes[count].getAttribute('id')
		optionItem = new Option( textValue, idValue,  false, false);
		itemList.options[itemList.length] = optionItem;
	}
  var to=document.getElementById("adv_"+dst);
  to.innerHTML="";
}


function GetInnerText (node)
{
	 return (node.textContent || node.innerText || node.text) ;
}


function GetXmlHttpObject()
{ 
  var objXMLHttp=null;
  if (window.XMLHttpRequest)
  {
    objXMLHttp=new XMLHttpRequest();
  }
  else if (window.ActiveXObject)
  {
    objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  return objXMLHttp;
}











/*
    var nodes=xmlDoc.responseXML.selectNodes("//datos");

    if (document.getElementById)
    {
      element1 = document.getElementById('date');
      element2 = document.getElementById('actualizando');
      element1.textContent=nodes[0].getAttribute('fecha');
      element2.textContent=nodes[0].getAttribute('actualizando');
    }
    else
    {
      element1 = document.all['date'];
      element2 = document.all['actualizando'];
      element1.innerText=nodes[0].getAttribute('fecha');
      element2.innerText=nodes[0].getAttribute('actualizando');
    }
    
    var nodes=xmlDoc.responseXML.selectNodes("//dato");
    for (i=0;i<nodes.length;i++)
    {
      if (document.getElementById)
      {
        element = document.getElementById(nodes[i].getAttribute('nombre'));
      }
      else
      {
        element = document.all[nodes[i].getAttribute('nombre')];
      }

      if ((nodes[i].getAttribute('tipo')=='A') || (nodes[i].getAttribute('tipo')=='F'))
      {
        element.value=nodes[i].getAttribute('valor');
      }
      if (nodes[i].getAttribute('tipo')=='D')
      {
        element.src='./images/'+nodes[i].getAttribute('valor')+'.gif';
      }
    }
*/
var Utf8 = {
    //Convierte de UTF-8 a ISO
    decode : function (utftext){
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;
        while ( i < utftext.length ) {
            c = utftext.charCodeAt(i);
            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }
        }
        return string;
    }
}



function StateChangeHandler()
{
  alert("3:");
	if(XmlHttpObj.readyState == 4)
	{
    alert("4:"+dst);
		if(XmlHttpObj.status == 200)
		{			
			PopulateList(dst,XmlHttpObj.responseXML.documentElement);
		}
		else
		{
			alert("Código de error: "  + XmlHttpObj.status);
		}
	}
}










