/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
function addOption(lista,text,value )
{
	var listaNewItem = document.createElement('option');
	listaNewItem.text = text;
	listaNewItem.value = value;

	try {
		 lista.add(listaNewItem, null); // standards compliant; doesn't work in IE
	}
	catch(ex) {
		 lista.add(listaNewItem); // IE only
	}
}

function limpaDDlist(lista) {
	if(lista !== null) {
		lista.length = 0;
		addOption(lista,"Todos","");
	}
}

function mudaDistrito(distrito,concelho)
{
	if (distrito.value) {
  jQuery.ajax({
        type: "POST",
        url: "/cinemas/?id=42",
        data: ({
            Distrito : distrito.value
            }),
        dataType: "xml",
        success: function(xml){

            //This simple XPath XML function looks through the returned XML data
            //All tags can now be accessed within the loop
            var _numeroConcelho = "";
            var _concelho= "";

            var elSel = concelho;
            elSel.disabled = false;
				limpaDDlist(elSel);

            //This function will loop for each match on addresses/address
            var cinema = $(xml).find('Concelhos');
            jQuery(cinema).find('Row').each(function(){

                _numeroConcelho = $("Numero", this).text();
                _concelho = $("Concelho", this).text();

                var elSel = concelho;
					addOption(elSel, _concelho, _numeroConcelho);

            })
        }
    })
	} else {
		var elSel = concelho;
		limpaDDlist(elSel);
		elSel.disabled = true;
	}

/*
                jQuery.ajax({ type: "POST", url: "/cinemas/?id=121", data: ({Distrito : distrito.value, Concelho : "", Genero : "", Classificacao : "", DiasProgramacao : ""}), dataType: "xml", success: function(xml){

                    //This simple XPath XML function looks through the returned XML data
                    //All tags can now be accessed within the loop
                    var _numeroCinema = "";
                    var _cinema= "";

                    var elSel = document.getElementById('Cinemas');
                    var i;
                    for (i = elSel.length - 1; i>=0; i--) {
                      elSel.remove(i);
                    }
                    //Add option Todos
                    var elOptNew = document.createElement('option');
                    elOptNew.text = "Todos";
                    elOptNew.value = "";

                    try {
                      elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
                    }
                    catch(ex) {
                      elSel.add(elOptNew); // IE only
                    }

                    //This function will loop for each match on addresses/address
                    var cinema = $(xml).find('Cinema');
                    jQuery(cinema).find('Row').each(function(){

                    _numeroCinema = $("NumeroCinema", this).text();
                    _cinema = $("Cinema", this).text();

                    var elOptNew = document.createElement('option');
                    elOptNew.text = _cinema;
                    elOptNew.value = _numeroCinema;
                    var elSel = document.getElementById('Cinemas');

                    try {
                      elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
                    }
                    catch(ex) {
                      elSel.add(elOptNew); // IE only
                    }

                    })
                }
                })

                jQuery.ajax({ type: "POST", url: "/cinemas/?id=141", data: ({Distrito : distrito.value, Concelho : "", Genero : "", Classificacao : "", DiasProgramacao : ""}), dataType: "xml", success: function(xml){

                    //This simple XPath XML function looks through the returned XML data
                    //All tags can now be accessed within the loop
                    var _numeroFilme = "";
                    var _filme= "";

                    var elSel = document.getElementById('Filmes');
                    var i;
                    for (i = elSel.length - 1; i>=0; i--) {
                      elSel.remove(i);
                    }
                    //Add option Todos
                    var elOptNew = document.createElement('option');
                    elOptNew.text = "Todos";
                    elOptNew.value = "";

                    try {
                      elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
                    }
                    catch(ex) {
                      elSel.add(elOptNew); // IE only
                    }

                    //This function will loop for each match on addresses/address
                    var cinema = $(xml).find('Cinema');
                    jQuery(cinema).find('Row').each(function(){

                    _numeroFilme = $("NumeroFilme", this).text();
                    _filme = $("Filme", this).text();

                    var elOptNew = document.createElement('option');
                    elOptNew.text = _filme;
                    elOptNew.value = _numeroFilme;
                    var elSel = document.getElementById('Filmes');

                    try {
                      elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
                    }
                    catch(ex) {
                      elSel.add(elOptNew); // IE only
                    }

                    })
                }
                })

*/
            }


