﻿var READY_STATE_UNINITIALIZED=0;
var READY_STATE_LOADING=1;
var READY_STATE_LOADED=2;
var READY_STATE_INTERACTIVE=3;
var READY_STATE_COMPLETE=4;
/*Funcion que inicializa el Objeto AJAX*/
function inicializa_xhr()
{if (window.XMLHttpRequest)
	{
            return new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
            return new ActiveXObject("Microsoft.XMLHTTP");
	}
}

/*Funcion que recorre el form y trae el valor de todos los elementos como si fuera un submit*/
function ArmaQuery(formid)
{
	var Formulario = document.forms[formid];
    //var Formulario = document.getElementById(formid);
   // alert(Formulario);
    //var form = document.forms[0];
	var longitudFormulario = Formulario.elements.length;   
    var cadenaFormulario = ""
    var sepCampos
    sepCampos = ""
    for (var i=0; i <= Formulario.elements.length-1;i++)
	{//Los radio button NO chequeados no se agregan al query
		var ban=true;
		if (Formulario.elements[i].type=="radio")
		{
			if(Formulario.elements[i].checked==false)
				ban=false;
		}
		if (Formulario.elements[i].type=="checkbox")
		{
			if(Formulario.elements[i].checked==false)
				ban=false;
		}
    	if (ban==true)
   		{
   		cadenaFormulario += sepCampos+Formulario.elements[i].name+'='+encodeURI(Formulario.elements[i].value);
        	sepCampos="&";

     	}
	}
	return	cadenaFormulario;
}

function get_content(_pagina,capa,tipo)
{
    var ajax;
    ajax = inicializa_xhr();
    if(ajax)
    {
        ajax.onreadystatechange = function()
        {
      
            if (ajax.readyState == 4)
            {
           if (ajax.status == 200)
            {            
                 if(tipo=='1')
                    {                       
                      
                      document.getElementById(capa).innerHTML =  ajax.responseText;   
                      runJS(ajax.responseText);        
                    
                     }else
                     { 
                            
                       document.getElementById(capa).innerHTML =  ajax.responseText;
                     }
              }
                
         
        }else{
        document.getElementById(capa).innerHTML="<img src='images/bigrotation2.gif'/>";

    }}
    
ajax.open("POST", _pagina, true);
ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
ajax.send(null);}
}
                        
function rescatar_script(texto)
{

    var p1=texto.split("<script",2);
	if (p1[1]) {
		var p2=p1[1].split(">",2);
		var p3=p2[1].split("</script",2);
		if (p3[0] && p3[0]!=''){
			eval(p3[0]);
			texto=p1[0]+p3[1];
			texto=rescatar_script(texto);}
	}
	return texto;

}

function easytabs(menunr, active) {
                            menunr = menunr-1;
                            for (i=1; i <= tabcount[menunr]; i++)
                            {
                                document.getElementById(tablink_idname[menunr]+i).className='tab'+i;
                                document.getElementById(tabcontent_idname[menunr]+i).style.display = 'none';
                            }
                                document.getElementById(tablink_idname[menunr]+active).className='tab'+active+' tabactive';
                                document.getElementById(tabcontent_idname[menunr]+active).style.display = 'block';
                            
 }
 
function runJS(search) 
{ 
 
    var css;
    var script;
    var contenido=search;
 
    while( script = search.match(/(<script[^>]+javascript[^>]+>\s*(<!--)?)/i)) 
    { 
      search = search.substr(search.indexOf(RegExp.$1) + RegExp.$1.length); 
 
      if (!(endscript = search.match(/((-->)?\s*<\/script>)/))) break; 
 
      block = search.substr(0, search.indexOf(RegExp.$1)); 
      search = search.substring(block.length + RegExp.$1.length); 
 
      var oScript = document.createElement('script'); 
      oScript.text = block; 
      document.getElementsByTagName("head").item(0).appendChild(oScript); 
    } 
    while( css= contenido.match(/(<style[^>]+css[^>]+>\s*(<!--)?)/i)) 
    { 
      contenido = contenido.substr(contenido.indexOf(RegExp.$1) + RegExp.$1.length); 
 
      if (!(endcss = contenido.match(/((-->)?\s*<\/style>)/))) break; 
 
      block1 = contenido.substr(0, contenido.indexOf(RegExp.$1)); 
      contenido = contenido.substring(block1.length + RegExp.$1.length); 
 
      var oCss = document.createElement('style'); 
      oCss.text = block1; 
      document.getElementsByTagName("head").item(0).appendChild(oCss); 
    } 
}
function getCodigo(campo) {
 return "&codigo=" + campo.value+ "&nocache=" + Math.random();

}
function cargarDatosCombo(url,campo,div_id,tipo){
peticion_http = inicializa_xhr();  
   if(peticion_http)
   {
            peticion_http.onreadystatechange= function (){
            if(peticion_http.readyState == READY_STATE_COMPLETE)
                { 
                if (peticion_http.status == 200)
                {
                   document.getElementById(div_id).innerHTML =peticion_http.responseText;
                
                }
                 else{document.getElementById(div_id).innerHTML = "<font size=1>Error: servidor NO encontrado.</font>";}
               }
              }
     
   }
   var query_string = getQuery(campo);     
   peticion_http.open("POST",url+'?'+query_string,true);
    peticion_http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=ISO-8859-1");      
    peticion_http.send(query_string);   
 }
 
 function getQuery(campo){
   var elemento;
   elemento=campo.options[campo.selectedIndex].text;
   return "codigo=" + encodeURIComponent(elemento)+ "&nocache=" + Math.random();
  }


function pasaDatos(url,contenedor,form){
    var datos_formulario="";
    
    peticion_http = inicializa_xhr();
   	if(peticion_http)
	{	           
		peticion_http.onreadystatechange = function(){
        if(peticion_http.readyState==4){  
        
                if(peticion_http.readyState==200){              
                    document.getElementById(contenedor).innerHTML = peticion_http.responseText;                  
                    runJS(peticion_http.responseText);                    
                  }
                } else{
                    document.getElementById(contenedor).innerHTML="<img src='imagenes/cargando.gif'/>";
                }
        } ;      
    }
    
    datos_formulario=ArmaQuery(form);   
    peticion_http.open("POST",url+'?'+datos_formulario, true);   
    peticion_http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');                                 
    peticion_http.send(datos_formulario);
  
               
}

function loadContentsOnDemand(divname,fillingUrl){
   var xmlHttp = XmlHttp.create();
   xmlHttp.open("POST", fillingUrl, true);   // async
   xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=ISO-8859-1');
   xmlHttp.onreadystatechange = function () {
      if (xmlHttp.readyState == 4) {
         document.getElementById(divname).innerHTML = xmlHttp.responseText;
      }
   };
   document.getElementById(divname).innerHTML = '<span class="txtrojo10"><b>Cargando...</b></span>';
   // llamada en otro hilo para permitir actualizar el interfaz
   window.setTimeout(function () {
      xmlHttp.send('nid='+nid);
   }, 10);
}

function get_content1(_pagina,capa,form)
{
    var ajax;
    ajax = inicializa_xhr();
    if(ajax)
    {
        ajax.onreadystatechange = function()
        {
      
            if (ajax.readyState == 4)
            {
                                 
                document.getElementById(capa).innerHTML = "Sus datos han sido enviados exitosamente";                               
          
                
         
        }else{
        document.getElementById(capa).innerHTML="<img src='images/cargando.gif'/>";

    }}
datos_formulario=ArmaQuery(form); 
   
ajax.open("POST",_pagina+'?'+datos_formulario, true);
ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
ajax.send(null);}
}
