
var ERROR_DIV = 1; 
var INFO_DIV = 2;
var aCachePageElements = new Array();


function XS_createHTTP()
{
var xmlHttp;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }
}


function XS_DOMDocument(sTag)
{
    this.XML = null
    this.Root = null
    this.version = null    
    if (sTag != null)
        var sXml = '<'+sTag + '/>'
    else
        var sXml = '<XML/>';

        try //Internet Explorer
          {
              var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
              xmlDoc.async="false";
              xmlDoc.loadXML(sXml);
              this.XML = xmlDoc;
              this.version = 'Explorer'  
              this.Root = this.XML.firstChild;
          }
        catch(e)
          {
                try //Internet Explorer 8
                  {
                      var xmlDoc = new ActiveXObject("MSXML2.DOMDocument");
                      xmlDoc.async="false";
                      xmlDoc.loadXML(sXml);
                      this.XML = xmlDoc;
                      this.version = 'Explorer'  
                      this.Root = this.XML.firstChild;
                  }
                catch(e)
                  {
                      try //Firefox, Mozilla, Opera, etc.
                        {
                            this.version = 'Other'  
                            parser=new DOMParser();
                            var xmlDoc=parser.parseFromString(sXml,"text/xml");
                            this.XML = xmlDoc;
                            this.Root = this.XML.firstChild;
                        }
                      catch(e) 
                        {
                            alert('XS_DOMDocument: ' + e.message)
                        }
                  }      
          }
        
     this.createRoot = function(sTag)
        {
	        this.Root = this.XML.createElement(sTag);
	        this.XML.replaceChild(this.Root, this.XML.firstChild)
	        return this.XML.firstChild;;
        }
        
    this.createChild = function(oParentNode, sTag, sValue)
        {
	        var oNode = this.XML.createElement(sTag);
	        if (sValue != null)
	            oNode.appendChild( this.XML.createTextNode(sValue) ) ;
	        oParentNode.appendChild(oNode);
	        return oNode;
        }
          
    this.createChildWithAttrs = function(oParentNode, sTag, aAttrs, sValue)    
    {
//	    if (FireFoxDetect() || OperaDetect() )
	    {
		    var oNode = this.XML.createElement(sTag);
		    for (var i = 0; i < aAttrs.length; i++)
		    {
			    //var sAtt = aAttrs[i];
			    //var t = sAtt.split('=')
			    var t = aAttrs[i];
			    var key = t[0];
			    var val = t[1];
			    oNode.setAttribute(key,val);
		    }
	    } 
/*	    
	    else 
	    {
		    var sAdd = '';
		    for (var i = 0; i < aAttrs.length; i++)
		    {
			    var t = aAttrs[i];
			    sAdd += ' ' + t[0] + '="'+ t[1] + '"';
		    }
		    sTag = '<' + sTag + sAdd + '>';
		    var oNode = this.XML.createElement(sTag);
	    }
*/
	    
        if (sValue != null)
	        oNode.appendChild( this.XML.createTextNode(sValue) ) ;
	    oParentNode.appendChild(oNode);
	    return oNode;
    }

    this.loadXML = function(sXml)
        {
            if (this.version == null)
                alert('XS_DOMDocument: ' + 'No se definido el container')
            else
            if (this.version == 'Explorer')
                this.XML.loadXML(sXml)
            else
            if (this.version == 'Others')
                this.XML=parser.parseFromString(sXml,"text/xml");
        }
        
    this.getXML = function()
        {
            if (this.version == null)
                alert('XS_DOMDocument: ' + 'No se definido el container')
            else
            if (this.version == 'Explorer')
            {
                var sXml = this.XML.xml;
                return sXml;
            }    
            else
            if (this.version == 'Others')
                return '????';
        }              
}
  


function SafariDetect()
{
	return navigator.userAgent.indexOf("Safari") >= 0;
}

function FireFoxDetect()
{
	return navigator.userAgent.indexOf("Mozilla") >= 0;
}

function OperaDetect()
{
	return navigator.userAgent.indexOf("Opera") >= 0;
}
  
function CreateErrorBar()
{
	if (document.getElementById("error"))
	{
		aCachePageElements[ERROR_DIV] = document.getElementById("error");
	}
}

function HideErrorBar()
{
	var error = aCachePageElements[ERROR_DIV];
	while (error.hasChildNodes()) error.removeChild(error.lastChild);
}

function ShowErrorBar(text)
{
	var error = aCachePageElements[ERROR_DIV];
	while (error.hasChildNodes()) error.removeChild(error.lastChild);
	var error_div = XS_CreateChildWithAttrs(error,'div',[['class','XS_error_div'],['align','center']]);
	var header_container = XS_CreateChild(error_div,'div');
	var error_container = XS_CreateChild(error_div,'div');
	var button_container = XS_CreateChild(error_div,'div');
	header_container.className = 'XS_error_header';
	header_container.innerHTML = 'ERROR';
	error_container.className= 'XS_error_text';
	error_container.innerHTML = text;
	button_container.innerHTML = '<div class="XS_error_button"><a href="" class="XS_reg" onclick="javascript:HideErrorBar(); return false;">' + strOK + '</a></div>';
}


//Descripcion: Recupera un archivo de y el contenido a divTarget
//Autor: RZ 
// 15/06/2008    : Creación
function XS_RequestFileText(sPath, sFile, divTarget)
{

  var prm = 'pPath=' + sPath + '&pFile=' + sFile; 
  var url = 'XS_RequestFileText.asp?nocache=' + Math.random();

  var peticion =  new Ajax.Request(url,   
		  {     
				 method:'post', 
				 postBody: prm ,
				 asynchronous: false,
				 onComplete: function(transport)
		 		 {   
		 				 if (transport.statusText == 'OK')
		 				 {		 		
							  var sResult = transport.responseText;															
						  	  if (sResult+'' == '')
						  	    alert('No se recuperó : ' + sFile)  
							  divTarget.innerHTML= sResult;
							  return sResult;
						 }						 
						 else
						 {
						 		alert(transport.statusText)		
						 }	
		 		 },     
		 		 onFailure: function()
		 		 { 
						alert('Error de conexión...') 
						alert(prm) 
						return null;
		 		 }   
		 	}
	);   	
}


function XS_RequestFileXML(sPath, sFileXML, divTarget)
{

  var prm = 'pPath=' + sPath + '&pFile=' + sFileXML; 
  var url = 'XS_RequestFileXml.asp?nocache=' + Math.random();

  var peticion =  new Ajax.Request(url,   
		  {     
				 method:'post', 
				 postBody: prm ,
				 asynchronous: false,
				 onComplete: function(transport)
		 		 {   
		 				 if (transport.statusText == 'OK')
		 				 {		 		
							  var sResult = transport.responseText;															
						  	  if (sResult == '')
						  	    alert('No se recuperó : ' + sFile)  
						  	
							  divTarget.innerHTML= sResult;
							  return sResult;
						 }						 
						 else
						 {
						 		alert(transport.statusText)		
						 }	
		 		 },     
		 		 onFailure: function()
		 		 { 
						alert('Error de conexión...') 
						alert(prm) 
						return null;
		 		 }   
		 	}
	);   	
}



// ***************************** DATA SERVER *************************************	



//Descripcion: Crea objeto DOM segun navegador
//Autor: RAZ
//15/06/2008    Creacion

function loadXMLDoc(sXml) 
{
try //Internet Explorer
  {
  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  }
catch(e)
  {
  try //Firefox, Mozilla, Opera, etc.
    {
    xmlDoc=document.implementation.createDocument("","",null);
    }
  catch(e) {alert(e.message)}
  }
  
try 
  {
  xmlDoc.async=false;
  xmlDoc.load(sXml);
  return(xmlDoc);
  }
catch(e) {alert(e.message)}

return(null);
}


function loadXMLString(sXml) 
{
try //Internet Explorer
  {
  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  xmlDoc.async="false";
  xmlDoc.loadXML(sXml);
  return(xmlDoc);  
  }
catch(e)
  {
    try //Internet Explorer 8
      {
          var xmlDoc = new ActiveXObject("MSXML2.DOMDocument");
          xmlDoc.async="false";
          xmlDoc.loadXML(sXml);
          return(xmlDoc);
      }
    catch(e)
      {
          try //Firefox, Mozilla, Opera, etc.
            {
            parser=new DOMParser();
            xmlDoc=parser.parseFromString(sXml,"text/xml");
            return(xmlDoc);
            }
          catch(e) {alert(e.message)}
      }    
  }
  
return(null);
}


//Descripcion: Objeto para el acceso de datos a través de la componente XS_DataAccess()
//Autor: RAZ
//15/06/2008    Creacion
function XS_Conexion(sServer, sRepository)
{
    if (sServer == "")
    {
        alert('Servidor no definido');
        return false
    }
    if (sRepository == "")
    {
        alert('Repositorio no definido');
        return false
    }
    
    this.Server = sServer
    this.Repository = sRepository
}

function getNode(objXML, sField)
{
    var sValue = null;
    var aNodos = objXML.getElementsByTagName(sField)
    if (aNodos.length>0)
    {
        if (aNodos[0].firstChild != null)
            sValue = aNodos[0].firstChild.nodeValue
        else
            sValue = '';
    }
    return sValue    
}


function nodeValue(objXML, sField)
{
    var sValue = null;
    var aNodos = objXML.getElementsByTagName(sField)
    if (aNodos.length>0)
        sValue = aNodos[0].firstChild.nodeValue;
    return sValue    
}


//Descripcion: Objeto para el acceso de datos a través de la componente XS_DataAccess()
//Autor: RAZ
//15/06/2008    Creacion
function XS_DataAccess(oConexion)
{
      this.Conexion = oConexion;
      this.XS_prmXml = '';
      this.XS_FormatParam = '';
      this.XS_Repository = '';	
      this.XS_Xsl = '';
      this.Trace = 0;
	  this.TimeOut=0;
      this.Debug = 0;
      this.xParam = '';
      this.prmHead = '';
      this.XS_Page = '';
      this.XS_Length = '';
      this.XML = null;
      this.HTML = null;
      this.XSL = '';
      this.EOF = true;     
      this.Records = null;    
      this.numRecords = 0;   
      this.QueryType=null;      
      this.Info = '';
	this.OpenServer = function(sPrmXml,sType)
					 {
					 	this.XS_prmXml = sPrmXml
					 	this.xParam = '';
					 	this.XS_Repository=this.Conexion.Repository;
					 	if (sType == null)
					 	    this.QueryType = 'SELECT'
					 	else
					 	    this.QueryType = sType;					 	
					 }

    this.ParamValue = function(sParam, sValue)
					 {
						if (this.xParam != '' && this.xParam != null)
						this.xParam = this.xParam + ',';							
						var prm = sParam.stripScripts()	 
						this.xParam = this.xParam + '{' + prm.toUpperCase() + '=' + escape(sValue) + '}'
					 }

	this.OpenSession = function(sPrmXml, sConnection, sType)
					 {
					  this.XS_prmXml = sPrmXml;
					 	this.XS_Repository=this.Conexion.Repository;
					    this.XS_FormatParam = 'XML';
					 	this.xParam = '';
					 	if (sType == null)
					 	    this.QueryType = 'SELECT'
					 	else
					 	    this.QueryType = sType.toUpperCase();
					 	    					 	
					 	this.prmHead = '<CONSULTA><CONEXION>' + sConnection + '</CONEXION><SESION>ajax</SESION><PROCEDIMIENTO>' + sPrmXml + '</PROCEDIMIENTO><TIPO>' + this.QueryType + '</TIPO><TIMEOUT>' + this.TimeOut +'</TIMEOUT><PARAMETROS>';					 	
					 }

    this.addParam = function(sParam, sValue, sType, sLength)
					 {
						var sTag = sParam.toUpperCase();								
						if (sLength == null || sType == 'adInteger')
						    var sLar = '""'
						else
						    var sLar = '"' + sLength + '"'
						var sItem = '<' + sTag + '|_tipo="' + sType + '"|_largo=' + sLar + '|_acceso="adParamInput">' + escape(sValue) + '</' + sTag + '>';    
						this.xParam = this.xParam + sItem;
					 }
	  							 		
	this.resetParams = function()
	                {
	                    this.xParam = '';
	                }
	                  							 								 
	this.SetParam = function(sParams)
					 {
						this.xParam = this.xParam + escape(sParams)
					 };

	this.GetParams = function()
	                 {
	                    var xParam = this.xParam
	                    xParam = Replace(xParam, '|_',' ')
	                    return xParam 
	                 };   				    
									 
	this.PageHTML = function(sXsl, sPagina, sLargo, sTarget)
									{										
										this.XS_Page = sPagina;
									  this.XS_Length = sLargo;
									  return  this.ResultHTML(sXsl, sTarget)
									};
									
    this.Execute = function(sXsl, sTarget)
                    {   
                        if (sXsl == null)
                            return this.ResultDOM()             
                        else
                            return this.ResultHTML(sXsl, sTarget);
                    };  

									
    this.ResultHTML = function(sXsl, sTarget) 
                    {
                    var sAux = sXsl + ''
                    if(sXsl.substr(0,1) == '/' )
                        this.XS_Xsl = sXsl
                    else
				        this.XS_Xsl = this.Conexion.Repository + "xsl\\" + sXsl
                        
					if (sTarget!=null)
                    {
					    var divTarget = $(sTarget);
					    if (divTarget + '' == 'undefined')
				   	    {
						    alert('No se identificó el Target ' + XS_Target);
						    return false;
					    } 
					}
					else
					    var divTarget = null;

                      if (this.Conexion.Server == '')
                      {
                        alert('Parámetros del Servidor no definidos');
                        return false;
                      }

                      if ( this.XS_FormatParam == 'XML')
                      {
                           this.xParam  = this.prmHead + this.xParam + '</PARAMETROS></CONSULTA>';
                      }
                      var prm = 'XS_Repository=' + this.XS_Repository + '&XS_prmXml=' + this.XS_prmXml + '.xml&XS_Param=' + this.xParam + '&XS_FormatParam=' + this.XS_FormatParam + '&XS_Xsl=' + this.XS_Xsl + '&XS_Page=' + this.XS_Page + '&XS_Length=' + this.XS_Length + '&XS_Trace=' + this.Trace + '&XS_Debug=' + this.Debug;
                      
                      var url = this.Conexion.Server + '?nocache=' + Math.random();
                      var nTrace =  this.Trace + 0;
                      this.HTML = '';
                      var sResult = ''; 
                      var lEOF = true;
                      var lOK = false;
                      var peticion =  new Ajax.Request
                         (     
                         url,   
                         {     
		                     method:'post', encoding:'iso-8859-1', postBody: prm , asynchronous: false,
		                     onComplete: function(transport)
 			                    {   
 				                     if (transport.statusText == 'OK')
 				                     {		 
			                     									
					                    if (nTrace == 1)
					                      {		 
					                      	 alert(prm)
					                      	 lEOF = true;	
					  	                     sResult = transport.responseText;
			 		                         $('divDebug').innerHTML = sResult;	
					                      }
				                        else
					                    if (nTrace == 2 || nTrace == 3)
					                      {				 						  
					                      	  alert(prm)
					                      	  lEOF = true;	
					  	                      sResult = transport.responseText;
				  		                      alert(sResult);	
			 			                      $('divDebug').innerHTML = sResult;	
					                      }
				                        else
					                      {				 						  		
			  	 		                      var sText = transport.responseText;
                                              var nPos = sText.indexOf("|::")
		                                      var sRetorno = sText.substr(0,nPos+3)
		                                      var aRetorno = sRetorno.split('|')  		                                      
		                                      if ( aRetorno[0] == 'ERROR' )
		                                      {
		                                        var sMsg = 'ERROR XS_DataServer\n'
		                                        sMsg = sMsg + 'Codigo: ' + aRetorno[1] + '\n'
		                                        sMsg = sMsg + 'Servicio: ' + aRetorno[2] + '\n'
		                                        sMsg = sMsg + 'Info: ' + aRetorno[3] + '\n'
		                                        alert(sMsg)
		                                        return false
		                                      }
                                              lOK =  ( aRetorno[0] == 'OK'  );
                                              lEOF = ( aRetorno[0] == 'EOF' );
                                              if ( lEOF )                                              
                                              {
                                                 sResult = '';
                                                 lOK = true;          
                                              }
                                              else
                                              {
			  	 		                          if ( lOK )
	                                                  sResult = sText.substr(nPos+3)
                                                  else
                                                      sResult = '';       
                                              }     
						                      if (divTarget != null) 
						                          divTarget.innerHTML = sResult;
						                    return sResult;
					                      }							 	  	
				                     }						 
				                     else
				                     {
                                        alert(transport.statusText)		
				                     }	
 			                    },     
 			                    onFailure: function()
 			                    { 
	 			                    alert('Error de conexión...') 
	 			                    alert(prm) 
	 			                    return null;
 			                    }   
 	                        }	 
 	                     );   //peticion 
											this.Info = '';
											this.Records = null
											if (nTrace == 0) 
											{
                         if (lEOF)
                         {
                            if(this.QueryType == 'SELECT')
                                this.Info = 'EOF'
                            else    
                                this.Info = 'OK';
                         }   
                         else
                             if (lOK)
                                this.Info = 'OK'
                             else   
                                this.Info = 'ERROR';
                         this.EOF = lEOF;
                       }
											 else 
													this.Info = 'TRACE';
                       this.HTML = sResult;   
                         
			             return sResult;

        } //ResultHTML
                    
      this.ResultDOM = function()
                   {
                      var objDOM = null
                      
                      if (this.Conexion.Server == '')
                      {
                        alert('Parámetros del Servidor no definidos');
                        return false;
                      }
                      var url = this.Conexion.Server + '?nocache=' + Math.random();
                      var nTrace =  this.Trace + 0; 


                      var lEOF = true;
                      var lOK = false;

                      if ( this.XS_FormatParam == 'XML')
                      {
                           this.xParam  = this.prmHead + this.xParam + '</PARAMETROS></CONSULTA>';
                      }

                      
                      this.prm = 'XS_Repository=' + this.XS_Repository + '&XS_prmXml=' + this.XS_prmXml + '.xml&XS_Param=' + this.xParam + '&XS_FormatParam=' + this.XS_FormatParam + '&XS_Trace=' + this.Trace + '&XS_Debug=' + this.Debug;
                      if (nTrace == 1)
                      {		 
                          var sMsg = __ShowParams('*** Trace 1 ***', this.xParam);  
	                      var oDiv = $('divDebug')
                          if( oDiv != null)
                              oDiv.innerHTML = '<textarea style="font-size:11px" rows=80 cols=120>' + sMsg + '</textarea>' ;	
                          else
                              alert(sMsg);
                          return;    
                      }
					  
					  //alert('url '+url);
					  //alert('this.prm '+this.prm);
					  				
	                  var peticion =  new Ajax.Request
	                  (
	                        url,   
	                        {     
      			                 method:'post', 
      			                 postBody:this.prm, 
      			                 encoding:'iso-8859-1', 
      			                 asynchronous:false,
      			                 onComplete: function(transport)
	 			                 {   
	 				                 if (transport.statusText == 'OK')
	 				                 {  
										
						                if (nTrace != 0)
				                        {		 
						                      var oDiv = $('divDebug')
			  	                              if( oDiv == null)
						                         sLf = '\n';
						                      else
						                         sLf = String.fromCharCode(10);
											  
			  		                          var sMsg = '';
				  	                          var sResult = transport.responseText;
			  	 		                      var sText = sResult;
                                              var nPos = sText.indexOf("|::");
		                                      var sRetorno = sText.substr(0,nPos+3);
		                                      var aRetorno = sRetorno.split('|');  		                                      


		                                      if ( aRetorno[0] == 'ERROR' )
		                                      {
		                                        sMsg += 'ERROR XS_DataServer' + sLf
		                                        sMsg = sMsg + 'Trace: '  + nTrace + sLf
		                                        sMsg = sMsg + 'Codigo: ' + aRetorno[1] + sLf
		                                        sMsg = sMsg + 'SP: '     + aRetorno[2] + sLf
		                                        sMsg = sMsg + 'Info: '   + aRetorno[3] + sLf

                                                sMsg = __ShowParams('*** Trace ' + nTrace + ' ***', prm) +  sLf + sMsg;
				  	                            if( oDiv != null)
						                            oDiv.innerHTML = '<textarea style="font-size:11px" rows=80 cols=120>' + sMsg + '</textarea>' ;	
                                                else
                                                    alert(sMsg);						                              

		                                        return false
		                                      }

		                                      if ( aRetorno[0] == 'TRACE')
		                                      {
		                                        if( nTrace == 2 ) 
		                                            sMsg = 'prmXML: '  + sLf + aRetorno[1] ;
		                                        else    
		                                        if( nTrace == 3 ) 
		                                            sMsg = 'Query: '  + sLf + aRetorno[1];
		                                        else    
		                                        if( nTrace == 4 ) 
		                                            sMsg = 'ResultXML: ' + sLf + aRetorno[1];

				  	                            if( oDiv != null)
						                            oDiv.innerHTML = '<textarea style="font-size:11px" rows=80 cols=120>' + sMsg + '</textarea>' ;	
                                                else
                                                    alert(sMsg);						                              
		                                        return false
		                                      }
		                                      else//Error
		                                      {
				  	                              if( oDiv != null)
						                              oDiv.innerHTML = '<textarea rows=80 cols=80>' + sResult + '</textarea>' ;	
		                                      }
                                              lOK =  true
                                              lEOF = false

						                          
				                        } //Trace
 					                    else
 						                {				 						  
			  	 		                      var sText = transport.responseText;
                                              var nPos = sText.indexOf("|::");
		                                      var sRetorno = sText.substr(0,nPos+3);
		                                      var aRetorno = sRetorno.split('|');  		                                      
		                                      if ( aRetorno[0] == 'ERROR' )
		                                      {
		                                        var sMsg = 'ERROR XS_DataServer\n';
		                                        sMsg = sMsg + 'Codigo: ' + aRetorno[1] + '\n';
		                                        sMsg = sMsg + 'SP: ' + aRetorno[2] + '\n';
		                                        sMsg = sMsg + 'Info: ' + aRetorno[3] + '\n';
		                                        alert(sMsg)
		                                        return false
		                                      }
                                              lOK =  ( aRetorno[0] == 'OK'  );
                                              lEOF = ( aRetorno[0] == 'EOF' );
                                              
			  	 		                      if ( lOK )
			  	 		                      {
	                                                  var sResult = sText.substr(nPos+3);	
                                                      objDOM = loadXMLString(sResult);
									                  if ( objDOM == null )																	  
							                          {
							                                lOK = false;
		                                                    var sMsg = 'ERROR retorno XML\n';
		                                                    sMsg = sMsg + 'Info: ' + objDOM.parseError.reason + '\n';
		                                                    sMsg = sMsg + sResult;
		                                                    alert(sMsg);
							                          }								                  
							                   }
							              }
					                 }						 
					                 else
					                 {
					 		            alert('XS_DataAccess: Error: ' + transport.statusText)		
                                        return 'ERROR'
					                 }
									 return objDOM

	 			                } <!-- Complete -->
	 			                ,     
	 			                onFailure: function()
	 			                { 
	 			                        alert('XS_DataAccess: Error de conexión: ' + transport.responseText) 
	 			                        return null;
	 			                }   
	 	                    }
	 	                
                     ); //peticion
                     
                     this.EOF = lEOF;
                     this.XML = objDOM;  
                     this.Info = '';
                     this.Records = null
                     if (nTrace == 0) 
                     {
                         if (lEOF)
                         {
                            if(this.QueryType == 'SELECT')
                                this.Info = 'EOF'
                            else    
                                this.Info = 'OK';
                         }   
                         else
                         if (lOK)
                            this.Info = 'OK'
                         else   
                            this.Info = 'ERROR';
                         this.EOF = lEOF;
                     }
                     else       
                        this.Info = 'TRACE';
                     
                     return this.Info
                   } //ResultDOM
                   
                   
          this.ResultNode = function (sNode, nR)
          {
                //alert('ResultNode: ' + sNode)
				if(nR==null)
				   nR=0;
                if (this.XML == null)
                {
                    alert(this.XS_prmXml + ': ResultNode - No hay contenedor XML')
                    return '';
                }
                var aRows = this.XML.getElementsByTagName('ROWS')         
                if (aRows.length == 0)
                    return null;
    
                var oRow = aRows[0].childNodes[nR];
                
                var sResult = getNode(oRow, sNode.toUpperCase() )            	
            	
            	if ( sResult == null)
            		 sResult = '';
                return sResult;
            }	

        this.AddNode = function(sNode, sValue, nR)
            {
				if(nR==null)
				   nR=0;
                if (this.XML == null)
                {
                    alert(this.XS_prmXml + ': ResultNode - No hay contenedor XML')
                    return '';
                }
                var aRows = this.XML.getElementsByTagName('ROWS')         
                if (aRows.length == 0)
                    return null;
    
                var oRow = aRows[0].childNodes[nR];
                var nodProveedor = this.XML.createElement(sNode.toUpperCase());
                    nodProveedor.appendChild( this.XML.createTextNode(sValue) );
                    oRow.appendChild(nodProveedor);
            }
            
        this.FieldValue = function (sField, oRow)
           {
           var sValue = null;
           var aNodos = oRow.getElementsByTagName(sField)
           if (aNodos.length>0)
           {
            if (aNodos[0].firstChild != null)
                sValue = aNodos[0].firstChild.nodeValue
            else
             sValue = '';
           }
           return sValue   
           }
 
         //Retorna un arreglo con los nodos ROW
         this.rowList = function()
         		{
                    if (this.XML == null)
                    {
                        alert(this.XS_prmXml + ': rowList - No hay contenedor XML')
                        return null;
                    }         			
                    var aRows = this.XML.getElementsByTagName('ROWS')         
                    if (aRows.length == 0)
                    {
                        return null;
    			    }
                   this.Records = aRows[0].childNodes;
         		   return this.Records;
         		}   

         this.rowCount= function()
         		{
                        if (this.XML == null)
                        {
                            alert(this.XS_prmXml + ': rowCount - No hay contenedor XML')
                            return null;
                        }         			
                        var aRows = this.XML.getElementsByTagName('ROWS')         
         			  return aRows[0].childNodes.length;
         		}   
         		
         this.ResultJSON = function()
              {
                    if (this.XML == null)
                    {
                        alert(this.XS_prmXml + ': ResultJSON - No hay contenedor XML')
                        return null;
                    }         			
                    var aRows = this.XML.getElementsByTagName('ROWS')         
                    if (aRows.length == 0)
                    {
                        return null;
    			    }
                    var aRows = this.rowList();
                    var aRecords = new Array();
                    var oFields = null;
                    this.numRecords = 0;
                    if (aRows.length>0)
                    {
                        var aCols = null;
                        for(var nR=0;nR<aRows.length;nR++)
                        {
                            var jFields = '{';
                            aCols = aRows[nR].childNodes;
                            for(var nC=0;nC<aCols.length;nC++)
                            {
                                if (nC>0)
                                    jFields += ", ";
                                jFields += aCols[nC].nodeName.toLowerCase() + ":'";

                                 if(aCols[nC].firstChild != null)
                                {
                                    var sTempCol=Replace(aCols[nC].firstChild.nodeValue,String.fromCharCode(10),'<br/>');
                                    
                                   sTempCol=Replace(sTempCol,'\'','');
                                   sTempCol=Replace(sTempCol,'\"','');
//                                    sTempCol=sTempCol.escapeHTML();
                                    jFields += sTempCol + "'";
                                }
                                else
                                    jFields += "'";
                            }
                            jFields += "}";
                            oFields = jFields.evalJSON();
                            aRecords[nR] = oFields;
                        }
                        this.numRecords = this.Records.length;
                    }
                    
                    return aRecords;           
              }  
         		

        //Retorna lista items en forma Item 1=Valor 1, Item 2=Valor 2, ....
		this.ResultItems = function(sNodeItem,sNodeValue) 
             {
                    if (this.XML == null)
                    {
                        alert(this.XS_prmXml + ': ResultItems - No hay contenedor XML')
                        return null;
                    }         			
                    sNodeItem = sNodeItem.toLowerCase()
                    sNodeValue = sNodeValue.toLowerCase()
                    var sList = '';
                    var aRows = this.rowList();
                    var aCols = null;
                    var sItem = null;
                    var sValue = null;
                    for(var nR=0;nR<aRows.length;nR++)
                    {
                        aCols = aRows[nR].childNodes;
                        sItem = '';
                        sValue = '';
                        for(var nC=0;nC<aCols.length;nC++)
                        {
                            if(aCols[nC].nodeName.toLowerCase() == sNodeItem)
                                sItem = aCols[nC].firstChild.nodeValue;
                            if(aCols[nC].nodeName.toLowerCase() == sNodeValue)
                                sValue = aCols[nC].firstChild.nodeValue;
                        }
                        if(sList == '')
                            sList += sItem + '=' + sValue;
                        else
                            sList += ',' + sItem + '=' + sValue;
                    }   
                    return sList;           
             }
             
		this.ResultList = function(sNombreLista, sNombreValue, sNombreDesc,sClass,sPrimerVal,sPrimerDesc,sFunc,sTarget) 
             {
					if (sTarget!=null)
                    {
					    var divTarget = $(sTarget);
					    if (divTarget + '' == 'undefined')
				   	    {
						    alert('No se identificó el Target ' + XS_Target);
						    return false;
					    } 
					}
					else
					    var divTarget = null;

                      if (this.Conexion.Server == '')
                      {
                        alert('Parámetros del Servidor no definidos');
                        return false;
                      }
					  if (sNombreLista=='')
					  {
					    alert('Parámetros de nombre de lista no definido');
                        return false;
					  }
						
                      if ( this.XS_FormatParam == 'XML')
                      {
                           this.xParam  = this.prmHead + this.xParam + '</PARAMETROS></CONSULTA>';
                      }
                      var prm = 'XS_prmXml=' + this.XS_prmXml + '.xml&XS_Param=' + this.xParam + '&XS_FormatParam=' + this.XS_FormatParam + '&XS_NombreLista=' + sNombreLista + '&XS_NombreValue=' + sNombreValue  
                      prm=prm + '&XS_NombreDesc=' + sNombreDesc + '&XS_Class=' + sClass + '&XS_PrimerVal=' + sPrimerVal + '&XS_PrimerDesc=' + sPrimerDesc
                      prm=prm + '&XS_Fx=' + sFunc + '&XS_Repository=' + this.XS_Repository + '&XS_Trace=' + this.Trace + '&XS_Debug=' + this.Debug;
                      
                      var url = this.Conexion.Server + '?nocache=' + Math.random();
                      var nTrace =  this.Trace 
                      this.HTML = '';
                      var sResult = ''; 
                      var lEOF = true;
                      var lOK = false;

                      var peticion =  new Ajax.Request
                         (     
                         url,   
                         {     
		                     method:'post', encoding:'iso-8859-1', postBody: prm , asynchronous: false,
		                     onComplete: function(transport)
 			                    {   
 				                     if (transport.statusText == 'OK')
 				                     {		 
			                     									
					                    if (nTrace == 1)
					                      {		 
					                      	alert(prm)
					  	                     sResult = transport.responseText;
			 		                         $('divDebug').innerHTML = sResult;	
					                      }
				                        else
					                    if (nTrace == 2 || nTrace == 3)
					                      {				 						  
					                      		alert(prm)
					  	                      sResult = transport.responseText;
				  		                      alert(sResult);	
			 			                      $('divDebug').innerHTML = sResult;	
					                      }
				                        else
					                      {				 						  		
			  	 		                      var sText = transport.responseText;
                                              var nPos = sText.indexOf("|::")
		                                      var sRetorno = sText.substr(0,nPos+3)
		                                      var aRetorno = sRetorno.split('|')  		                                      
		                                      if ( aRetorno[0] == 'ERROR' )
		                                      {
		                                        var sMsg = 'ERROR XS_DataServer\n'
		                                        sMsg = sMsg + 'Codigo: ' + aRetorno[1] + '\n'
		                                        sMsg = sMsg + 'Servicio: ' + aRetorno[2] + '\n'
		                                        sMsg = sMsg + 'Info: ' + aRetorno[3] + '\n'
		                                        alert(sMsg)
		                                        return false
		                                      }
                                              lOK =  ( aRetorno[0] == 'OK'  )
                                              lEOF = ( aRetorno[0] == 'EOF' )                                              
			  	 		                      if ( lOK )
	                                              sResult = sText.substr(nPos+3)
                                              else
                                                  sResult = ''       
						                    if (divTarget != null) 
						                        divTarget.innerHTML = sResult;
						                    return sResult;
					                      }							 	  	
				                     }						 
				                     else
				                     {
                                            alert(transport.statusText)		
				                     }	
 			                    },     
 			                    onFailure: function()
 			                    { 
	 			                    alert('Error de conexión...') 
	 			                    alert(prm) 
	 			                    return null;
 			                    }   
 	                        }	 
 	                     );   //peticion 
 	                  this.EOF = lEOF;
                      this.HTML = sResult;   
				      return sResult;

        } //GetList
    
            
         		        
}	

function __ShowParams(sMsg, sPrm)
{
  var aPrm = sPrm.split('&');
  var sAux = ''
  for(var nP=0;nP<aPrm.length;nP++)
  {
     sAux = sAux + aPrm[nP]+ '\n';
  }
  sAux = Replace(sAux,'|_',' ')
  sAux = Replace(sAux,'><','>\n<')
  sAux = Replace(sAux,'>\n</','></')
  
  sAux = sMsg + '\n' + sAux
  return sAux;
}



/*
Crea un objeto SendRequest
Status = Contiene información del status del Request
Trace  = Contiene la descripción de la operación solicitada
TraceHTML = Retorna la descripcion de la operacion en HTML   
TraceXML = Retorna la descripcion de la operacion en XML tag: <status></status><trace></trace> 

*/

function XS_AjaxRequest(sAjaxUrl, sAjaxMethod)
{
  this.Status = 'NOK';
  this.Result = '';  
  this.Sep = '&';
  this.ContentType='application/x-www-form-urlencoded';
  this.Encoding = 'ISO-8859-1';
  this.Parameters='';
  this.AjaxUrl = sAjaxUrl;
  this.AjaxMethod = sAjaxMethod;

  var sResult = '';  
  var aResult = null;
  var bRet=false;
  
  this.addParam = function(sParam, sValue)
  {
    if (this.Parameters=='')
    {
       this.Parameters=sParam+'='+sValue;
    }
    else
    {
       this.Parameters+='&'+sParam+'='+sValue
    }
  }  

  this.SetParam = function(sParam)
  {
        if (sParam==null)
        {
          this.Parameters=''
          
        }
        else
          this.Parameters=sParam;
  }
        
  this.Execute = function(sAction, sParams)
    {
        var sUrl='';
		
        if (this.AjaxUrl == null){
            sResult = 'NOK'  + this.Sep + 'No ha especificado la URL de destino de la petición'
        }
        else{
            var sUrl = this.AjaxUrl + '?nocache=' + Math.random();}
      
        if (this.AjaxMethod == null)  
            var sMethod = 'post';
        else
            var sMethod = this.AjaxMethod;
         
        if (sParams != null)  
           this.Parameters = sParams;

        var peticion =  new Ajax.Request(sUrl,   
		  {     
				 method: sMethod, 
				 postBody: 'XS_Action=' + sAction + '&'+ this.Parameters ,
				 encoding: this.Encoding,
				 contentType: this.ContentType,
				 asynchronous: false,
				 onComplete: function(transport)
		 		 {   
 	 				 if (transport.statusText == 'OK')
	 				 {		 		
					      sResult = transport.responseText;
					      transport=null;
					      return 'OK';
					 }						 
					 else
					 {
					      sResult = 'NOK' + this.Sep + transport.responseText;
					      transport.abort()
					      transport=null;
         		 	      return 'NOK';
					 }
		 		 },     
		 		 onFailure: function()
		 		 { 
					sResult = 'NOK' + this.Sep + 'Error en la conexión. no se ha podido obtener el status';
					return 'NOK';
		 		 }   
		  }
	    )
	    aResult = sResult.split(this.Sep);
	    this.Status = aResult[0];
		this.Result = aResult[1];
		return this.Status;
	  }
}

function XS_SetSP(jOptions) {
    if ((typeof jOptions.Connection == "undefined") || (!jOptions.NameSP || jOptions.NameSP.empty()) ||
    (!jOptions.NameBD || jOptions.NameBD.empty()) || (typeof jOptions == "undefined" || !Object.isArray(jOptions.Param)))
        return;
    var objDat = new XS_DataAccess(jOptions.Connection);
    objDat.OpenSession(jOptions.NameSP, jOptions.NameBD);
    for (var i = 0; i < jOptions.Param.length; i++) {
        var Param = jOptions.Param[i];
        if (Param.type == "adVarchar") {
            objDat.addParam(Param.field, Param.value, Param.type, Param.length);
        } else {
            objDat.addParam(Param.field, Param.value, Param.type);
        }
    }
    return objDat;
}

