//esto es necesario porque dependiendo del navegador, el objeto swf en el documento html tendra un nombre distinto
	function thisMovie(movieName) {
		var isIE = navigator.appName.indexOf("Microsoft") != -1;
		return (isIE) ? window[movieName] : document[movieName];
	}

	function makeCall(id) {
		id= id.substring(3,6);      	  	
		thisMovie("script").colorearjs(id);
	}


////////////////////////////////////////////

/**
 * common.js must be included in order to use these functions.
 */

/**
 * Checks whether the next and previous buttons should be enabled or not
 */
function nextPrevButtons(){
	var selMun = document.getElementById("selectedMunicipalities");
	
	if (selMun.options[0] == null)
		document.getElementById("next").disabled=true;
	else
		document.getElementById("next").disabled=false;
}

/**
 * Selects or deselects a municipality depending on its current state.
 */
function swapMunicipalitySelection(id, name){
	if (document.getElementById(id).className == "municipality"){
		addMunicipality(id,name);}		
	else {//document.getElementById(id).className == "municipality_selected"
		removeMunicipality(id);}
		//makeCall(id);
	nextPrevButtons();
	
}  

/**
 * Adds a municipality to the selection
 */
function addMunicipality(id, name){
	var i,j;
	var exists=false;
	var selMun = document.getElementById("selectedMunicipalities");
	document.getElementById(id).className = "municipality_selected";
	for(i=0;i<selMun.length;i++){
		if(selMun.options[i].value == id)
			exists = true;
	}
	if(exists == false){
	    //makeCall(id);
		newOption = new Option(name,id);
		//selMun.options[selMun.length]=newOption;
		j=selMun.length-1;
		    while(j>=0 && newOption.text <selMun.options[j].text){
		        selMun.options[j+1]=new Option(selMun.options[j].text, selMun.options[j].value);
		        j--;
		    }
		    selMun.options[j+1]=new Option(newOption.text, newOption.value);
	}
}

/**
 * Removes a municipality from the selection
 */
function removeMunicipality(id){
	var i;
	var selMun = document.getElementById("selectedMunicipalities");
	for (i=0; i < selMun.length; i++) {
		if (selMun.options[i].value == id){
		    //makeCall(id);
			document.getElementById(selMun.options[i].value).className="municipality";
			selMun.options[i]=null;
			
		}
	}
}

/**
 * Removes all municipalities marked
 */
function removeMunicipalities(){
	var i;
	var selMun = document.getElementById("selectedMunicipalities");
	
	/*Deletion compresses the options array. If you delete 
	options[0], the existing options[1] becomes options[0]. 
	That's why I delete options at the end of the array first.*/
	for (i = selMun.length-1; i >= 0; i--) {
		if (selMun.options[i].selected){					
			//document.getElementById(selMun.options[i].value).className="municipality";
			//makeCall(selMun.options[i].value);
			//selMun.options[i]=null;
			removeMunicipality(selMun.options[i].value);
			
		}
	}	
	nextPrevButtons();	
}

/*
 * Add or removes all municipalities in a TerritorialEntity at once.
 * munIds and munNames arrays are created programaticaly.
 * IMPORTANT NOTE: for loops reach lenght-1 due to a null element at the end of each array.
 * This null element is there because of the way the arrays are filled from the xsl templates.
 */
function swapTerritorialEntity(id){
    var i;
    if (document.getElementById(id).className == "entity"){         
        for(i=0;i < munIds[id].length-1; i++) {
            addMunicipality(munIds[id][i], munNames[id][i]);
        }
        document.getElementById(id).className = "entity_selected";
   }
   else { //document.getElementById(id).className == "entity_selected"
        for(i=0;i < munIds[id].length-1; i++) {
            removeMunicipality(munIds[id][i], munNames[id][i]);
        }
        document.getElementById(id).className = "entity";
   }
   nextPrevButtons();
}

/*
 * Marks as selected every item on the selectedMunicipalities select list, so they can be sent to Step2 
 */          
function toStep2(){
    var i;
	var selMun = document.getElementById("selectedMunicipalities");
	
	for(i=0;i<selMun.length;i++)
	    selMun.options[i].selected=true;
}

/*
 * To be called from the "municipality group" pop up window, this funcion asyncronously refreshes
 * the municipality groups list
 */
 
function refreshMunGroups(){
    var xmlHttp;
    document.getElementById("munGroups").innerHTML="Refrescando lista de grupos...";
    
    xmlHttp = getXMLHttpObject();
    if(xmlHttp==null) {
        document.getElementById("munGroups").innerHTML="Lista no disponible. El navegador no soporta Ajax.";
        return;
    }
    xmlHttp.onreadystatechange=function(){
        if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
            document.getElementById("munGroups").innerHTML=xmlHttp.responseText;
    }
    xmlHttp.open("GET", "MunicipalityGroups.aspx?getMunGroups=true", true);
    xmlHttp.send(null);
}
