﻿//Super clase js
function scAjax() {
    this.xmlDoc;
    this._xh;

    scAjax.prototype.error = function(mentodo, msg) {
        if (msg.description == null) {
            alert(mentodo + ': ' + msg.message);
        }
        else {
            alert(mentodo + ': ' + msg.description);
        }
    }

    scAjax.prototype.alertError = function(mentodo, msg) {
        alert(mentodo + ': ' + msg.message);
    }

    scAjax.prototype.iniciar = function() {
        try {
            // Mozilla / Safari
            this._xh = new XMLHttpRequest();
        } catch (e) {
            // Explorer
            var _ieModelos = new Array(
			'MSXML2.XMLHTTP.5.0',
			'MSXML2.XMLHTTP.4.0',
			'MSXML2.XMLHTTP.3.0',
			'MSXML2.XMLHTTP',
			'Microsoft.XMLHTTP'
			);
            var success = false;
            for (var i = 0; i < _ieModelos.length && !success; i++) {
                try {
                    this._xh = new ActiveXObject(_ieModelos[i]);
                    success = true;
                } catch (e) {
                    // Implementar manejo de excepciones
                    //this.error('scAjax.prototype.iniciar', e);
                }
            }
            if (!success) {
                // Implementar manejo de excepciones, mientras alerta.
                return false;
            }
            return true;
        }
    }

    scAjax.prototype.iniciarXML = function(xml) {
        try {
            if (window.ActiveXObject) {
                this.xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
                this.xmlDoc.async = false;
                this.xmlDoc.loadXML(xml);
            } else {
                var parser = new DOMParser();
                this.xmlDoc = parser.parseFromString(xml, "text/xml");
            }
            return this.xmlDoc;
        } catch (err) {
            this.error('scAjax.prototype.iniciarXML', err);
        }
    }

    scAjax.prototype.enviar = function(urlget) {
        try {
            var Digital = new Date();
            var seconds = Digital.getSeconds();
            if (urlget.indexOf('?') == -1) {
                urlget += '?';
            } else {
                urlget += '&';
            }
            urlget += 'sec=' + seconds;
            if (!this._xh) {
                this.iniciar();
            }
            this._xh.open("GET", urlget, false);
            this._xh.send(null);

            if (this._xh.readyState == 4 && this._xh.status == 200) {
                return this._xh.responseText;
            }
        }
        catch (err) {
            this.error('scAjax.prototype.enviar', err);
        }
    }

}

//crea una instancia de spAjax para permitir manejo de mensajes de errores
var spErr = new scAjax()

//Clase Ajax que retorna XML
function ajax(ArrayId, proceso) {
    ajax.prototype.ArrayId = ArrayId;
    ajax.prototype.proceso = proceso;

    ajax.prototype.getData = function() {
        try {
            var arr = this.ArrayId;
            var strParam = "";
            for (var i = 0; i < arr.length; ++i) {
                strParam += arr[i][0] + '=' + arr[i][1] + '&'
            }
            var url = encodeURI("funcionesGenerales.aspx?" + strParam + "proceso=" + this.proceso);
            var xml = this.enviar(url)
            if (xml != undefined) {
                return xml;
            }
            else {
                this.alertError('ajax.prototype.getData', 'Existe un problema para realizar la solicitud');
            }
        } catch (e) {
            this.error('ajax.prototype.getData', e);
        }
    }
}

//hereda de scAjax
ajax.prototype = new scAjax();
