﻿var url_local = 'http://www.adrianoaoli.com/chat/';

function AJAX(url, metodo, params, processa, modo, esconde_processando) { // Definição da Classe AJAX

    this.url = url;

    this.metodo = (metodo) ? metodo : 'GET';
    this.params = (metodo == 'GET') ? null : params;
    this.processaresultado = processa; // Nome da função que será executada quando obtiver a resposta do site
    this.Header = new Array();
    this.modo = (modo) ? modo : 'T';



    if (this.modo != 'T' && this.modo != 'X' && this.modo != 'R') { this.modo = 'T'; }

    this.addHeader = addHeader;
    this.delHeader = delHeader;
    this.setHeader = setHeader;
    this.conectar = conectar;
    this.processaretorno = processaretorno;
    this.processaerro = processaerro;


    this.conectar();


    function addHeader(h, v) { this.Header[h] = v; }
    function delHeader(h) {
        //   delete (this.Header[h]); 
    }
    function setHeader() {
        //     if (this.httprequest == null) { return; } for (h in this.Header) { this.httprequest.setRequestHeader(h, this.Header[h]); }
    }

    function conectar() {


        document.getElementById("chat_icone").innerHTML = '<img src="' + url_local + 'img/loading.gif">';
        if (this.url == undefined || this.url == '') { return; }

        this.httprequest = null;

        if (window.XMLHttpRequest) { // Mozilla, Safari,...
            this.httprequest = new XMLHttpRequest();
        } else if (window.ActiveXObject) { // IE
            try {
                this.httprequest = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    this.httprequest = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {
                }
            }
        }

        //alert("sadfasdf");

        /////////////////////////////////////////////////////////////
        // Verificando se o objeto XMLHTTP foi criado corretamente //
        /////////////////////////////////////////////////////////////
        if (this.httprequest != null && this.httprequest != undefined) {
            var obj = this;
            this.httprequest.onreadystatechange = function () { obj.processaretorno.call(obj); }
            if (this.metodo == undefined || this.metodo == '') { this.metodo = 'GET'; }

            var url = this.url;

            if (this.metodo == 'POST') {
                this.addHeader('Content-Type', "application/x-www-form-urlencoded; charset=iso-8859-1");
                this.addHeader("Cache-Control", "no-store, no-cache, must-revalidate");
                this.addHeader("Cache-Control", "post-check=0, pre-check=0");
                this.addHeader("Pragma", "no-cache");
            } else {
                var rnd = Math.random();
                url += (this.url.indexOf('?') != -1) ? '&rnd=' + rnd : '?rnd=' + rnd;
            }

            this.httprequest.open(this.metodo, url, true);
            //this.setHeader();
            this.httprequest.send(this.params);

        } else {
            alert('Seu navegador é incompatível com este site.');
        }
    }

    function processaretorno() {

        if (this.httprequest.readyState == 4) {


            //document.getElementById('d1').style.display = 'none';


            if (this.httprequest.status == 200) {
                var resp;
                if (this.modo == 'T') { resp = this.httprequest.responseText; }
                else if (this.modo == 'R') { resp = new XMLtoRS(this.httprequest.responseXML); }
                else { resp = this.httprequest.responseXML; }
                //ok(resp);

                if (this.processaresultado != null) {
                    this.processaresultado(resp);
                } else {
                    //document.write(resp);
                }

            } else {
                this.processaerro();
            }
        }
    }

    function processaerro() { alert('Erro processando a requisição \n' + this.httprequest.status + '-' + this.httprequest.statusText + ' :-> ' + this.url); }
}


function XMLtoRS(xmlDOC) { }

function g(id) { return document.getElementById(id)}


var empip;

function getUrl() {
    var wl = document.location;
    //alert(wl);
    //wl = replaceAll(wl, '&', '...');
    //wl = replaceAll(wl, '&', '...');
    //wl = replaceAll(wl, '&', '...');
    //wl = replaceAll(wl, '&', '...');
    //wl = replaceAll(wl, '&', '...');
    //wl = replaceAll(wl, '&', '...');
    //wl = replaceAll(wl, '/', '___');
    //wl = replaceAll(wl, '/', '___');
    //wl = replaceAll(wl, '/', '___');
    //wl = replaceAll(wl, '/', '___');
    //wl = replaceAll(wl, '/', '___');
    //wl = replaceAll(wl, '/', '___');
    return wl;
}

function replaceAll(string, token, newtoken) {
    try {

        return string.replace(token, newtoken);
    }
    catch (err) {
        return string;
    }


}

function make(id) { document.write('<iframe src="' + url_local + 'script.aspx?id=' + id + '&or=' + getUrl() + '" width=140 height=48 frameborder=0 scrolling=no ></iframe>') }
function getchat(id) {
    empip = id;
    document.write('<div id="chat_icone" ></div>')
    var url = 'http://adrianoaoli.com/chat/chatstatus.aspx?id=' + id;
    var ajaxCliente0 = AJAX(url, 'POST', '', ok, 'T', false);
}

function ok(asf) {
    if (asf.indexOf("online") > 0) {
        g("chat_icone").innerHTML = '<a href="javascript:void(0)" onclick="abrjam()"><img src=' + url_local + asf + ' /></a>';
    } else if (asf.indexOf("offline") > 0) {
        g("chat_icone").innerHTML = '<img src=' + url_local + asf + ' />';
    } else {
        g("chat_icone").innerHTML = "---";
    }
}

