﻿/*
*Autor: Flying Peach
*Copyright: 2009 Contenidos Digitales Interactivos FP S.C.
*Versión: 1.0
*/

FlyingPeach.WebBrowsers.namespace("FlyingPeach.WebBrowsers.Ajax");

function instanciarHttpRequest() {
    var httpRequest = null; 
    try {
        httpRequest = new XMLHttpRequest();
    }
    catch (e) {
        alert(e);
        try {
            httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            try {
                httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e) {
                alert("El browser utilizado no soporta AJAX. \n" + e);
            }
        }
    }

    return httpRequest;
}

FlyingPeach.WebBrowsers.Ajax.obtenerUrlSincronamente = function(url) {
    var httpRequest = instanciarHttpRequest();
    httpRequest.open("GET", url, false);
    httpRequest.send(null);
    
    return httpRequest.responseText;
}

FlyingPeach.WebBrowsers.Ajax.obtenerUrlAsincronamente = function(url, funcionCallback) {
    var httpRequest = instanciarHttpRequest();
    httpRequest.onreadystatechange = funcionCallback;
    httpRequest.open("GET", url, true);
    httpRequest.send(null);   
}

FlyingPeach.WebBrowsers.Ajax.poblarInnerHtml = function poblarInnerHtml(idElemento, url, invocarAsincronamente) {
    invocarAsincronamente = invocarAsincronamente == null ? true : invocarAsincronamente;
    if (invocarAsincronamente) {
        FlyingPeach.WebBrowsers.Ajax.obtenerUrlAsincronamente(url, function() {
        if (this.readyState == 4)
            document.getElementById(idElemento).innerHTML = this.responseText; 
        });
    }
    else
        document.getElementById(idElemento).innerHTML = FlyingPeach.WebBrowsers.Ajax.obtenerUrlSincronamente(url);
}

FlyingPeach.WebBrowsers.Ajax.poblarOverlayFullScreen = function(url, textoBotonOcultarOverlay, invocarAsincronamente) {
    invocarAsincronamente = invocarAsincronamente == null ? true : invocarAsincronamente;
    if (invocarAsincronamente) {
        FlyingPeach.WebBrowsers.Ajax.obtenerUrlAsincronamente(url, function() {
        if (this.readyState == 4)
            FlyingPeach.WebBrowsers.mostrarOverlayFullScreen(this.responseText, textoBotonOcultarOverlay);
        });
    }
    else
        FlyingPeach.WebBrowsers.mostrarOverlayFullScreen(FlyingPeach.WebBrowsers.Ajax.obtenerUrlSincronamente(url), textoBotonOcultarOverlay);
}