var XmlHttpObj;

var Utf8 = {

    //Convierte de UTF-8 a ISO
    decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

}

function CreateXmlHttpObj()
{
	try
	{
		XmlHttpObj = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			XmlHttpObj = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(oc)
		{
			XmlHttpObj = null;
		}
	}
	
	if (!XmlHttpObj && typeof XMLHttpRequest != "undefined") 
	{
		XmlHttpObj = new XMLHttpRequest();
	}
}

function cambioOperador()
{
	var obj = document.forms[0];
	if (obj.cmbOperadores.selectedIndex == 0)
	{
		obj.cmbLineas.options.length = 0;
	}
	else
	{
		var to = document.getElementById("advice");
		to.innerHTML="<img src='/recursos/imagenes/loading.gif' align='absmiddle'>";
		
		var selectedOperador = obj.cmbOperadores.options[obj.cmbOperadores.selectedIndex].value;
		var cadena = selectedOperador.split("$");
		var selectedCodOperador = cadena[0];

		var requestUrl;
		requestUrl = "/paginas/getlineas.php" + "?operador=" + encodeURIComponent(selectedCodOperador);
		
		CreateXmlHttpObj();
		
		if (XmlHttpObj)
		{
			XmlHttpObj.onreadystatechange = StateChangeHandler;
			XmlHttpObj.open("POST", requestUrl, true);
			XmlHttpObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
			XmlHttpObj.send('');
		}		
	}
}

function StateChangeHandler()
{
	if (XmlHttpObj.readyState == 4)
	{
		if (XmlHttpObj.status == 200)
		{
			PopulateLineasList(XmlHttpObj.responseXML.documentElement);
		}
		else
		{
			alert("Código de error: "  + XmlHttpObj.status);
		}
	}
}

function PopulateLineasList(lineaNode)
{
    var lineaList = document.getElementById("cmbLineas");
	for (var count = lineaList.options.length-1; count >-1; count--)
	{
		lineaList.options[count] = null;
	}
	
	//alert(lineaNode.childNodes[0].childNodes[0].nodeValue);
	var lineasNodes = lineaNode.childNodes;
	var textValue, codLinea, nomLinea; 
	var optionItem;

	for (var count = 0; count < lineasNodes.length; count++)
	{
		if (lineasNodes[count].nodeName == "linea")
		{
			var lineasHijosNodes = lineasNodes[count].childNodes;

			for (var count2 = 0; count2 < lineasHijosNodes.length; count2++)
			{
				if (lineasHijosNodes[count2].nodeName == "codigo")
					codLinea = Utf8.decode(GetInnerText(lineasHijosNodes[count2]));
				else if (lineasHijosNodes[count2].nodeName == "descripcion")
					nomLinea = Utf8.decode(GetInnerText(lineasHijosNodes[count2]));
			}
			textValue = codLinea + " - " + nomLinea;
			
			idValue=count;		
			optionItem = new Option(textValue, textValue, false, false);
			lineaList.options[lineaList.length] = optionItem;
		}
	}
	
	var to = document.getElementById("advice");
	to.innerHTML = "";
}

function GetInnerText(node)
{
	 return (node.textContent || node.innerText || node.text);
}

