﻿var xmlHttp;

function CreateXmlHttp()
{   
    // Probamos con IE
    try
    {
        // Funcionará para JavaScript 5.0
        xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e)
    {
        try
        {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch(oc)
        {  
            xmlHttp = null;
        }
    }

    // Si no se trataba de un IE, probamos con esto
    if(!xmlHttp && typeof XMLHttpRequest != "undefined")
    {
        xmlHttp = new XMLHttpRequest();
    }

    return xmlHttp;
}

function ajaxPeticion(urlPeticion, delegadoFuncion)
{   
    CreateXmlHttp();      
    var ajaxRequest = urlPeticion;
    // Asignar funcion delegate para la respuesta
    xmlHttp.onreadystatechange = delegadoFuncion;  
    // Enviar
    xmlHttp.open("GET", ajaxRequest, true);
    xmlHttp.send("");
}

function ajaxPeticionPOST(urlPeticion, delegadoFuncion)
{   
     var Formulario = document.getElementById('Form');
     var longitudFormulario = Formulario.elements.length;
     var cadenaFormulario = "";
     var sepCampos;
     sepCampos = "";
     for (var i=0; i <= Formulario.elements.length-1;i++) 
     {     
        if (Formulario.elements[i].name != '__VIEWSTATE')
        {
            if (Formulario.elements[i].type == 'checkbox' && Formulario.elements[i].checked)
            {                
                cadenaFormulario += sepCampos+Formulario.elements[i].name+'='+encodeURI(Formulario.elements[i].value);
                sepCampos="&";
            }
            else if (Formulario.elements[i].type != 'submit' && Formulario.elements[i].type != 'button')
            {
                cadenaFormulario += sepCampos+Formulario.elements[i].name+'='+encodeURI(Formulario.elements[i].value);
                sepCampos="&";
            }            
        }
     }     

    CreateXmlHttp();      
    var ajaxRequest = urlPeticion;
    // Asignar funcion delegate para la respuesta
    xmlHttp.onreadystatechange = delegadoFuncion;  
    // Enviar
    xmlHttp.open("POST", ajaxRequest, true);
    xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF8');
    xmlHttp.send(cadenaFormulario);
}

