// Variable Global para XmlHttp Request Object
var xmlhttp;

// Variable para el combo hijo
var cboHijo;

/* Llama al Action y metodo de un struts para obtener el XmlData usando XmlHttpObject */
function getDropDownChild(id, cboP, cboH, action, metodo){

	var paramID = cboP.value;
	var iD = id.value;
    cboHijo = cboH;

    xmlhttp = null;

    // Inicializacion de XmlHttpRequest Object en Mozilla, etc.
    if (window.XMLHttpRequest){
    	xmlhttp = new XMLHttpRequest();
    } else if (window.ActiveXObject) { // Inicializacion de XmlHttpRequest Object en IE
 		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

	if (xmlhttp != null){
    	//Seteo del Action y el metodo que resulve la consulta
        url = action + ".do?method="+ metodo + "&paramID=" + paramID + "&iD=" + iD;

		//Luego de ejecutarse el action sigue por aca
		xmlhttp.onreadystatechange = getResponseAction;

		// Se abre el request
        xmlhttp.open("GET",url,true); // true porque es asincrono
        // Se envia la URL
        xmlhttp.send(null);
	} else {
    	alert("Su navegador no soporta XMLHTTP.")
	}
}

/* Se utiliza para verficar el ReadyState & Status of XmlHttpRequest Object */
function verifyReadyState(obj){
	// XmlHttp.ReadyState == 4 invalid response
    if(obj.readyState == 4){
    // XmlHttp.status == 200 invalid response
    	if(obj.status == 200){
        	return true;
        } else {
        	alert("Problemas con el XML de Datos");
            return false;
        }
	}
}

/* Action that has to take place after getting reponse */
function getResponseAction(){
	// Verificando el estado y el estatus
    if(verifyReadyState(xmlhttp) == true){
    	// Construyendo el DOM parser desde el Response Object
        var response = xmlhttp.responseXML.documentElement

        // Eliminado todos los elementos del drop down hijo
        cboHijo.options.length = 0;

        // Checkeando el Root node
        var x = response.getElementsByTagName("option");
        var val;
        var tex;
        var optn;

		// Cargando la nueva coleccion en el drop down hijo
        for(var i = 0;i < x.length; i++){
        	optn = document.createElement("OPTION")
	        var er;

    	    // Obteniendo los valores del nodo
        	val = x[i].getElementsByTagName("optionvalue");
	        tex = x[i].getElementsByTagName("optionlabel");
    	    try{
        		// Asignando el valor y el label al drop down hijo
            	optn.value = val[0].firstChild.data;
	            optn.text = tex[0].firstChild.data;
    	    } catch(er){
        		alert("Error al asignar valores al combo");
	        }

    	    // Agregando el html option al drop down hijo
            cboHijo.options.add(optn);
		}
	}
}