﻿
function alertando() {
    var estado = document.getElementById("idestado");

    // Obtener el índice de la opción que se ha seleccionado
    var indiceSeleccionado = estado.selectedIndex;
    // Con el índice y el array "options", obtener la opción seleccionada
    var opcionSeleccionada = estado.options[indiceSeleccionado];
    // Obtener el valor y el texto de la opción seleccionada
    var textoSeleccionado = opcionSeleccionada.text;
    var valorSeleccionado = opcionSeleccionada.value;
    alert("Opción seleccionada: " + textoSeleccionado + "\n Valor de la opción: " + valorSeleccionado);
  //alert("entra en select " +idestado.value);
}

function objetoAjax() {
    var xmlhttp = false;
    try {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (E) {
            xmlhttp = false;
        }
    }
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
        xmlhttp = new XMLHttpRequest();
    }
    return xmlhttp;
}
  
function cambia() {
    if (ajax.readyState == 4) {
        //mostrar resultados en esta capa
        divcont.innerHTML = ajax.responseText;
        //alert (ajax.responseText);
    }
}
function cambia1() {
    if (ajax1.readyState == 4) {
        //mostrar resultados en esta capa
        divcontd.innerHTML = ajax1.responseText;
        //alert (ajax.responseText);
    }
}


function crearGrupo() {
    var estado = document.getElementById("idestado");

    // Obtener el índice de la opción que se ha seleccionado
    var indiceSeleccionado = estado.selectedIndex;
    // Con el índice y el array "options", obtener la opción seleccionada
    var opcionSeleccionada = estado.options[indiceSeleccionado];

    // Obtener el valor y el texto de la opción seleccionada
    var textoSeleccionado = opcionSeleccionada.text;
    var valorSeleccionado = opcionSeleccionada.value;
    //alert(" Valor de la opción: " + valorSeleccionado);
    
    new Ajax.Request('procesar.aspx', {
        method: 'post',
        postBody: "ids=" + valorSeleccionado,
        onSuccess: procesar
    } 
    );
}


function procesar(transport) {
    var res = transport.responseXML;

    var equipos = res.getElementsByTagName('ciudad');
    var sel = $('idciudad');
   //lert(sel);
    sel.innerHTML = "";
    for (i = 0; i < equipos.length; i++) {
        op = document.createElement('option');
        op.text = equipos[i].childNodes[1].firstChild.nodeValue
        op.value = equipos[i].childNodes[0].firstChild.nodeValue;


        try {
            sel.add(op, null); // standards compliant
        }
        catch (ex) {
            sel.add(op); // IE only
        }

    }
     
}
/*************/
function inicial() {
    divcont = document.getElementById('contenedor');
    ajax = objetoAjax();
    ajax.onreadystatechange = cambia;
    ajax.open("GET", "inicial.aspx", true);
    ajax.send(null);
    

}

/*************************/
function inicializa() {
    divcontd = document.getElementById('contenedord');
    ajax1 = objetoAjax();
    ajax1.onreadystatechange = cambia1;
    ajax1.open("GET", "inicializa.aspx", true);
    ajax1.send(null);

}

/***************************************************/
function creardireccion() {
    var ciudad = document.getElementById("idciudad");

    // Obtener el índice de la opción que se ha seleccionado
    var indiceSeleccionado = ciudad.selectedIndex;
    //alert(indiceSeleccionado);
    // Con el índice y el array "options", obtener la opción seleccionada
    var opcionSeleccionada = ciudad.options[indiceSeleccionado];

    // Obtener el valor y el texto de la opción seleccionada
    var textoSeleccionado = opcionSeleccionada.text;
    var valorSeleccionado = opcionSeleccionada.value;
    //alert(" Valor de la opción: " + valorSeleccionado);

    new Ajax.Request('procesardi.aspx', {
        method: 'post',
        postBody: "idci=" + valorSeleccionado,
        onSuccess: procesar1
    }
    );
}


function procesar1(transport) {
    var res = transport.responseXML;

    var equipos = res.getElementsByTagName('distribuidor');
    //alert(equipos);
    var sel = $('contenedor');
    //alert(equipos.length);
    sel.innerHTML = "";
    for (i = 0; i < equipos.length; i++) {

        //num++;
        fi = document.getElementById('contenedor');
        contenedor = document.createElement('p');
        contenedor.id = 'p' + i;
        //alert(contenedor.id);
        fi.appendChild(contenedor);


        text = equipos[i].childNodes[1].firstChild.nodeValue;
        value = equipos[i].childNodes[0].firstChild.nodeValue;
        opt = document.createElement('p');
        contenedor.appendChild(opt);

        var newLink = document.createElement('a');
        newLink.setAttribute('onclick', '"direccion(' + value + ')"');
       

        var linkText = document.createTextNode(text);
        newLink.appendChild(linkText);
        contenedor.appendChild(newLink);

        try {
            sel.add(contenedor, null); // standards compliant
        }
        catch (ex) {
            sel.add(contenedor); // IE only
        }
        //sel.insert(opt);
    }
    
}

/**********************************************************************************/


function distribidores() {
    var ciudad = document.getElementById("idciudad");
    var indiceSeleccionado = ciudad.selectedIndex;
    var opcionSeleccionada = ciudad.options[indiceSeleccionado];
    var textoSeleccionado = opcionSeleccionada.text;
    var valorSeleccionado = opcionSeleccionada.value;
    //alert(valorSeleccionado);

    divcont = document.getElementById('contenedor');
    ajax=objetoAjax();
    ajax.onreadystatechange= cambia;
    ajax.open("GET", "distribuidores.aspx?id="+valorSeleccionado,true);

    ajax.send(null)
}




function intento() {
    alert("Hello, buen dìa :)");
}

function direccion(idd) {
  
    divcontd = document.getElementById('contenedord');
    ajax = objetoAjax();
    ajax.onreadystatechange = cambiad;
    ajax.open("GET", "direccion.aspx?idd=" + idd, true);

    ajax.send(null)
}


function cambiad() {
    if (ajax.readyState == 4) {
        //mostrar resultados en esta capa
        divcontd.innerHTML = ajax.responseText;
        //alert (ajax.responseText);
    }
}

function cargar() {
    crearGrupo();
    inicial(); 
    inicializa();
}
