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 discotecaListOnChange() {
	var fechaList = document.getElementById("fechaList");
	if(fechaList.selectedIndex!=0){
		var to=document.getElementById("advice");
		to.innerHTML="<img src='imagenes/loading.gif' align='absmiddle'>";
		var selectedDia = fechaList.options[fechaList.selectedIndex].value;
		var requestUrl = "discoteca_provider.php?filter=" + (fechaList.selectedIndex-1);
		CreateXmlHttpObj();
		if(XmlHttpObj)
		{
			XmlHttpObj.onreadystatechange = StateChangeHandler;
			XmlHttpObj.open("POST", requestUrl, true );
			XmlHttpObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
			XmlHttpObj.send('');		
		}
	}else{
		var discotecaList = document.getElementById("discotecaList");
		for (var count = discotecaList.options.length-1; count >-1; count--)
		{
			discotecaList.options[count] = null;
		}
	}
}
function CreateXmlHttpObj()
{
	if (window.ActiveXObject) {
		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();
		}
	}else if (window.XMLHttpRequest) { // Mozilla, Safari,...
		XmlHttpObj = new XMLHttpRequest();
	}
	if (XmlHttpObj.overrideMimeType) {
		XmlHttpObj.overrideMimeType('text/xml');
	}
}
function StateChangeHandler()
{
	if(XmlHttpObj.readyState == 4)
	{
		if(XmlHttpObj.status == 200)
		{			
			try //Internet Explorer
			{
				xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
				xmlDoc.async="false";
				xmlDoc.loadXML(XmlHttpObj.responseText);
				PopulateDiscotecaList(xmlDoc);
			}catch(e){
				PopulateDiscotecaList(XmlHttpObj.responseXML);
			}
		}
		else
		{
			alert("Código de error: "  + XmlHttpObj.status);
		}
	}
}
function PopulateDiscotecaList(discotecaNode)
{	
    var discotecaList = document.getElementById("discotecaList");
	for (var count = discotecaList.options.length-1; count >-1; count--)
	{
		discotecaList.options[count] = null;
	}
	var discotecaNodes = discotecaNode.getElementsByTagName('discoteca');
	var textValue; 
	var optionItem;
	for (var count = 0; count < discotecaNodes.length; count++)
	{ 
   		textValue = Utf8.decode(GetInnerText(discotecaNodes[count]));
		idValue=count;		
		optionItem = new Option( textValue, textValue,  false, false);
		discotecaList.options[discotecaList.length] = optionItem;
	}
	var to=document.getElementById("advice");
	to.innerHTML="";
}
function GetInnerText (node)
{
	 return (node.textContent || node.innerText || node.text) ;
}