var Indirizzo = function(id_nazione,id_regione,id_provincia,id_comune,id_frazione,indirizzo,civico,id_div,show_indirizzo,doc){
	if(id_nazione==""){id_nazione="IT";}
	this._id_nazione = id_nazione; 
	this._id_regione = id_regione; 
	this._id_provincia = id_provincia; 
	this._id_comune = id_comune; 
	this._id_frazione = id_frazione; 
	this._indirizzo = indirizzo; 
	this._civico = civico ; 
	this._document = doc; 
	this._id_div = id_div; 
	this.show_indirizzo = show_indirizzo==1; 
	this.mappa= $("map"+id_div); 
	this.imgmappa= $("imagemap"+id_div); 
	this.img_dir = "template/Html/indirizzo/" ; 
	this.img_ext = "gif"; 
	this.img_now="-";
	this.coordinate_torna_indietro = "1,6,8,14,19,14,19,2,7,2"; 
	this.ajax_page = "Root/JavaScript/Ajax/ajaxdb-indirizzo.php";
	this.divjs= "";
	this.hNazioni = true; 
	this.hRegione = true; 
	
 };
 
Indirizzo.prototype = {
	nascondiNazione : function(){
		this.hNazioni=false;
	}, 
	nascondiRegione : function(){
		this.hRegione=false;
	}, 
	refreshAll : function(){
		this.refreshSelect("Regione");
		this.refreshSelect("Provincia");
		this.refreshSelect("Comune");
		this.refreshSelect("Frazione");
		this.refreshText("Indirizzo");
		this.refreshText("Civico");
	},
	refreshSelect : function (selectname){
		var select = $(selectname+this._id_div);
		select.parentNode.parentNode.className+= " hidden"; 
		select.selectedIndex = 0; 
	},
	refreshText : function (textname){
		var text = document.getElementById(textname+this._id_div); 
		if(this.show_indirizzo&&text){
			text.parentNode.parentNode.className+= " hidden"; 
			text.value= ""; 
		}
		if(!text)this.getText(textname,"");
	},
	getXmlValue :  function(padre,xmlTag){
		if(padre.getElementsByTagName(xmlTag)[0].firstChild!=null){
			return 	padre.getElementsByTagName(xmlTag)[0].firstChild.nodeValue;
		}return "" ; 
	},
	creaSelectMap :	function(select,rows,valore,id_padre){
		var map = $("map"+this._id_div);
		var prova = map.innerHTML!="";
		var imgmappa = $("imgmap"+this._id_div); 
		var id = id_padre/*this.getXmlValue(rows[0],"PADRE"); */;
		var flag= true; 
		if((id=="-") && (this._id_nazione!="") && prova){
			flag = false; 
		}
		if(flag){
			map.innerHTML = ""; 
			this.img_now = this.img_dir+ id + "."+ this.img_ext;
			imgmappa.src = this.img_now;
			this.creaTornaIndietroMappa(map,id,select); 
		}
		this.creaOption(select,"-1","--",false);
	
		if(rows.length>0){
			for (i = 0; i < rows.length; i++){
				var row = rows[i];
				var id = this.getXmlValue(row,"ID");
				var nome = this.getXmlValue(row,"NOME");
				var coordinate = this.getXmlValue(row,"COORDINATE");
				this.creaOption(select,id,nome,id==valore); 
				if(coordinate){
					this.creaMappa(map,id,nome,coordinate,select); 
					}
				}
		}else{
			select.parentNode.parentNode.className+= " hidden";	
		}
	},
	creaOption :  	function (select,id,nome,selected){
		var option = new Element("option"); 
		option.value=id;
		try{
			select.add(option,null); // standards compliant
		}catch(ex){
			select.add(option); // IE only
		}
		option.innerHTML=nome;
		option.selected = selected; 
	
	},
	creaTornaIndietroMappa:  	function(mappa,id,select){
		var tipo = select.name.replace(this._id_div,""); 
		var padre = "";
		if(tipo=="Regione"){
			padre="Nazione@@-1"; 
		}else if(tipo=="Provincia"){
			padre="Nazione@@"+this._id_nazione+"@@Regione"; 
		}else if(tipo=="Comune"){
			padre="Regione@@"+this._id_regione+"@@Provincia"; 
		}else if(tipo=="Frazione"){
			padre="Provincia@@"+this._id_provincia+"@@Comune"; 
		}
		
		if(tipo!="Regione"&&tipo!="Nazione"&&tipo!="Provincia"){
			var area= new Element("area",{
				'title': "Torna indietro",
				'alt': padre
			});
			area.set('shape' ,"poly");
			area.set('coords',this.coordinate_torna_indietro); 
			area.addEvent('click',function(event){this.selectByMap(event);}.bind(this));
			area.inject(mappa);		
		}
		
	},
	mouseover: function (nome,tipo,id){
		if(tipo!=""&&tipo!=""&&id!="")
		this.getFigli(tipo,id,nome);		
		//alert(id);
		var elem = $("imgname"+this._id_div);
		elem.innerHTML = "<b>"+ nome + "</b>";
	},
	getFigli: function (tipo,id_padre,nome){
	var elem = $("imgname"+this._id_div);
	var ajax = new Ajax();
	elem.innerHTML = id_padre;
	ajax.setFunction ( function() {
			if(ajax._connection && ajax._connection.readyState==4){	
				//alert(ajax._connection.responseText);
				if(ajax._connection.responseXML){
					var rows = ajax._connection.responseXML.getElementsByTagName('row');	
					var figli = ""; 
					elem.innerHTML = "<b>"+ nome + "</b>"; 
					if(rows.length>0){
						for (i = 0; i < rows.length; i++){
							var row = rows[i];
							var nome_zona = this.getXmlValue(row,"NOME");
							figli += nome_zona + ", "; 
						}
						figli = figli+"-"; 
						figli = figli.replace(", ,","");
						figli = figli.replace(", -","");
						elem.innerHTML = "<b>"+nome + "</b> - " + figli;  
					}
				}}
			//ajax._connection.responseXML = ""; 
		}.bind(this) );
		ajax.appendParamRequest("FUNCTION","getFigli"+tipo+"("+id_padre+")"); 
		ajax.processa(this.ajax_page);


	},
	creaMappa :  function (mappa,id,nome,coordinate,select){
		var tipo = select.name.replace(this._id_div,""); 
		var area= new Element("area",{
					'title': nome,
					'alt': tipo+"@@"+ id
					});
		area.set('shape' ,"poly");
		area.set('coords',coordinate); 

		area.addEvent('click',function(event){this.selectByMap(event);}.bind(this));
		area.addEvent('mouseover', function(event) {
				event = new Event(event); 
				target = event.target; 
				alt = target.get("alt"); 
				alt_a = alt.split("@@");
				var id = alt_a[1]; 
				$("imgmap"+this._id_div).src = this.img_dir+id+"-selected."+this.img_ext;
				//$("imgname"+this._id_div).innerHTML = target.title;
				this.mouseover(target.title , alt_a[0] , alt_a[1])
			}.bind(this));
		area.addEvent('mouseleave', function(event) {
				event = new Event(event); 
				target = event.target; 
				var id = alt_a[1]; 
				$("imgmap"+this._id_div).src = this.img_now;
				$("imgname"+this._id_div).innerHTML = "";
			}.bind(this));
		area.inject(mappa);
	},
	riempiSelect: function (ajax,select,query,valore,id_padre){
		ajax.setFunction ( function() {
			if(ajax._connection && ajax._connection.readyState==4){	
				//alert(ajax._connection.responseText);
				if(ajax._connection.responseXML){
					var rows = ajax._connection.responseXML.getElementsByTagName('row');	
					this.creaSelectMap(select,rows,valore,id_padre);			
					//$('query').innerHTML += "<br>"+query+"<br>";	
				}}
			//ajax._connection.responseXML = ""; 
		}.bind(this) );
		ajax.appendParamRequest("FUNCTION",query); 
		ajax.processa(this.ajax_page);


	},
	getSelect : function(selectname,value){
		var select = $(selectname+this._id_div); 
		var div_input_field = new Element("div");
		var div_label = new Element("div");	
		var label = new Element("label");
		var div_input = new Element("div");
		if(select!=null){
			if(select.nodeName!="SELECT"){
				select=null;
			}
		}
		if(select!=null){
			div_input_field = 	select.getParent().getParent() ;
			div_label = 		div_input_field.getElementsByTagName("div")[0];	
			label = 			div_label.getElementsByTagName("label")[0];
			div_input = 		select.getParent();	
			div_input_field.className="input_field" ;
			select.dispose();
	
		}else{
			var principale = document.getElementById(this._id_div); 
		 	div_label.className="label"; 
			div_input.className="input"; 
			div_input_field.className="input_field hidden" ;
			label.innerHTML=selectname;
			/**************************todel only for ab*****************/
			if(selectname=="Provincia"){label.innerHTML="Zona";}	
			if(selectname=="Frazione"){label.innerHTML="Localit&agrave;";}	
			/**************************************************************/
			principale.appendChild(div_input_field);
			div_input_field.appendChild(div_label);
			div_input_field.appendChild(div_input);
			div_label.appendChild(label);		
		}
		select = document.createElement("select");
		select.id= selectname+this._id_div;
		select.name=selectname+this._id_div ;
		div_input.appendChild(select);
		if(selectname=="Nazione"&&!this.hNazioni){
			div_input_field.className += " hidden";
		}
		if(selectname=="Regione"&&!this.hRegione){
			div_input_field.className += " hidden";
		}
		
		return select; 
	} , 

	assegnaSelect :  function (select, value) {
		for(i=0;i<select.options.length ; i++ ){
			if(select.options[i].value == value){
				select.selectedIndex = i ; 
				select.options[i].selected = true;  
			}			
		}
		
	}, 

	getText : function(selectname,valore){
		var text = $(selectname+this._id_div); 
		var div_input_field = new Element("div");
		var div_label = new Element("div");	
		var label = new Element("label");
		var div_input = new Element("div");
		if(text!=null){
			if(text.type!="text"){
				text=null;
			}
		}

		if(text!=null){
			div_input_field = 	text.getParent().getParent() ;
			div_label = 		div_input_field.getElementsByTagName("div")[0];	
			label = 			div_label.getElementsByTagName("label")[0];
			div_input = 		text.getParent();	
			div_input_field.className="input_field" ;
			text.dispose();
		}else{
			var principale = document.getElementById(this._id_div); 
			div_label.className="label"; 
			div_input.className="input"; 
			div_input_field.className="input_field hidden" ;
			label.innerHTML=selectname;
			principale.appendChild(div_input_field);
			div_input_field.appendChild(div_label);
			div_input_field.appendChild(div_input);
			div_label.appendChild(label);		
		}
		text = document.createElement("input");
		text.value = valore; 
		text.id= selectname+this._id_div;
		text.name=selectname+this._id_div ;
		text.type = "text";
		text.onblur = function(){ 
				FormController.controllaTesto(selectname+this._id_div,50,30,0);
			}.bind(this);
		div_input.appendChild(text);
		return text; 
	} , 
	selectByMap : function(event){
		event = new Event(event); 
		var target = event.target; 
		var alt = target.get("alt"); 
		var alt_a = alt.split("@@"); 
		var select  = $(alt_a[0]+this._id_div);
		for(i=0;i<select.options.length ; i++ ){
			if(select.options[i].value == alt_a[1]){
				select.selectedIndex = i ; 
				select.options[i].selected = true;  
			}			
		}
		if(alt_a.length>2){
			this["_id_"+alt_a[2].toLowerCase()] = ""; 
		}
		select.onchange(event);
	},
	disegnaPadre 	: function(etichetta) {
		var padre = new Element("div"); 
		padre.set("class", "h_sx ricerca_con_mappa input_field_special"); 
		this.divjs = padre; 
		var div_label = new Element("div");
		div_label.set("class", "label"); 
		var label = new Element("label");
		label.set("foe", this._id_div); 
		label.innerHTML= etichetta; 
		label.inject(div_label);
		
		var mappa = new Element("div"); 
		mappa.set("id", "mappa"+this._id_div); 
		var map = new Element("map",{"id" : "map"+this._id_div,"name" : "map"+this._id_div });
		var imgmap = new Element("img",{
				"id" : "imgmap"+this._id_div ,
				"usemap" : "#"+  "map"+this._id_div
				}
				);
		var imgname = new Element("div",{
					"class" :  "nome_area_geografica" ,  
					"id" : "imgname"+this._id_div
					
					});
		imgname.setStyles({
					"width" : "300px",
					"height" : "80px"/*, 
					"font-weight" : "bold"*/
					});
//		var p = new Element("p",{
//					"id" : "imgname"+this._id_div
//					}); 
//		p.setStyles({
//					"border-size" : "4px",
//					"border-style" : "solid",
//					"border-color" : "#fff000"
//					});
		
		imgmap.inject(mappa);
		map.inject(mappa);
		imgname.inject(mappa);
		div_label.inject(padre);
		mappa.inject(padre)
//		p.inject(imgname);
		return padre; 
	},
	
	disegna : function(){
		var Phpdiv = $("indirizzoPhp"+this._id_div);
		 
		var js = Phpdiv.getParent().getParent().onblur;
		var etichetta =Phpdiv.getParent().getParent().getChildren("div")[0].getChildren("label")[0].innerHTML; 
	
		
		var padre = this.disegnaPadre(etichetta);
		padre.onblur = js;
		/*var padre = new Element("div"); 
		padre.set("class", "h_sx ricerca_con_mappa input_field_special"); 
		var mappa = new Element("div"); 
		mappa.set("id", "mappa"+this._id_div); 
		var map = new Element("map",{"id" : "map"+this._id_div,"name" : "map"+this._id_div });
		var imgmap = new Element("img",{
				"id" : "imgmap"+this._id_div ,
				"usemap" : "#"+  "map"+this._id_div
				}
				);
		var imgname = new Element("div",{
					"id" : "imgname"+this._id_div ,
					"class" :  "nome_area_geografica"
					});
		imgmap.inject(mappa);
		map.inject(mappa);
		imgname.inject(mappa);
		mappa.inject(padre)*/
		$(this._id_div).parentNode.appendChild(padre);



		var test = new Element("div",{"id" : "test"});
		var query = new Element("div",{"id" : "query"});
		query.inject(Phpdiv.parentNode);
		test.inject(Phpdiv.parentNode);
		Phpdiv.getParent().getParent().dispose(); 
		
		
		
		var hidden =($(this._id_div)).getChildren("input");
		for(var i = 0 ; i< hidden.length; i++){
			if(hidden[i].type=="hidden")
				hidden[i].dispose();
		}
		//alert(($(this._id_div)).innerHTML);
		this.getSelect("Nazione",this._id_nazione);
		this.getSelect("Regione",this._id_regione);
		this.getSelect("Provincia",this._id_provincia);
		this.getSelect("Comune",this._id_comune);
		this.getSelect("Frazione",this._id_frazione);
		this.getText("Indirizzo",this._indirizzo);
		this.getText("Civico",this._civico);	
		
		this.drawNazione();
	},
	drawNazione : function(){
		var document = this._document;
		var padre = document.getElementById("Regione"+this._id_div) 
		var select = this.getSelect("Nazione"); 
		select.onchange = function(event){
					event = new Event(event); 
					var target = event.target; 
					if(!target.selectedIndex){
						target = $("Nazione"+this._id_div);
					}
					if(target.selectedIndex==0){
						this._id_nazione ="";
						this.refreshAll();
						this.drawNazione(true);
					}else{
						this.drawRegione(true);
				 		this.refreshSelect("Provincia"); 
						this.refreshSelect("Comune"); 
						this.refreshSelect("Frazione"); 
						this.refreshText("Indirizzo"); 
						this.refreshText("Civico"); 
					}}.bind(this);
		var ajax = new Ajax();
		var query ="getAllNation()"; /* "SELECT '-' AS PADRE, COORDINATE_NAZIONE AS COORDINATE , ID_NAZIONE as ID, NOME_NAZIONE as NOME FROM GEO_NAZIONE  ORDER BY NOME"*/;  
		this.riempiSelect(ajax,select,query,this._id_nazione,"-"); 
		if(this._id_nazione!=null && this._id_nazione!=""){
			this.drawRegione(false);}
	},
	drawRegione : function(forza){
		var document = this._document; 
		var nazione = document.getElementById("Nazione"+this._id_div); 
		if(this._id_nazione==null|| this._id_nazione==""||forza){
			this._id_nazione = nazione.options[nazione.selectedIndex].value;
			$("imgname"+this._id_div).innerHTML = nazione.options[nazione.selectedIndex].text ;
		}
		var select = this.getSelect("Regione");
		select.onchange = function(){this.drawProvincia(true);}.bind(this);
		var ajax = new Ajax();
		var query = "getRegioniOf("+this._id_nazione+")" /*"SELECT ID_NAZIONE AS PADRE, COORDINATE_REGIONE AS COORDINATE , ID_REGIONE as ID, NOME_REGIONE as NOME FROM GEO_REGIONE WHERE ID_NAZIONE ='" + this._id_nazione+ "'  ORDER BY NOME"*/; 
		this.riempiSelect(ajax,select,query,this._id_regione,this._id_nazione);  
		
		this.refreshSelect("Provincia"); 
		this.refreshSelect("Comune"); 
		this.refreshSelect("Frazione"); 
		this.refreshText("Indirizzo"); 
		this.refreshText("Civico"); 
		if(this._id_regione!=null&& this._id_regione!="" && !forza){
			this.drawProvincia(false);
		}
	}, 
	drawProvincia : function(forza){
		var document = this._document; 
		var padre = document.getElementById("Regione"+this._id_div)
		if(this._id_regione==null|| this._id_regione==""||forza){
			this._id_regione = padre.options[padre.selectedIndex].value;
			$("imgname"+this._id_div).innerHTML = padre.options[padre.selectedIndex].text ;

		}
		var select = this.getSelect("Provincia"); 
		select.onchange = function(){ this.drawComune(true);}.bind(this);
		var ajax = new Ajax();
		var query = "getProvinciaOf("+this._id_regione+")";
		
		/*"SELECT ID_REGIONE AS PADRE,  COORDINATE_PROVINCIA AS COORDINATE , ID_PROVINCIA as ID, NOME_PROVINCIA as NOME FROM GEO_PROVINCIA WHERE ID_REGIONE = '"+this._id_regione+"'" ;*/ 
		
		this.riempiSelect(ajax,select,query,this._id_provincia,this._id_regione);
		this.refreshSelect("Comune");  
		this.refreshSelect("Frazione"); 
		this.refreshText("Indirizzo"); 
		this.refreshText("Civico"); 
 		if(this._id_provincia!=null&& this._id_provincia!=""&& !forza){this.drawComune(false);}
	},
	drawComune : function(forza){

//			alert("drawComune");
		var document = this._document; 
		var padre = document.getElementById("Provincia"+this._id_div)
		if(this._id_provincia==null|| this._id_provincia==""||forza){
			this._id_provincia = padre.options[padre.selectedIndex].value;
			$("imgname"+this._id_div).innerHTML = padre.options[padre.selectedIndex].text ;
		}
		var select = this.getSelect("Comune"); 
		var n_indirizzo = this; 
		select.onchange = function(){
			n_indirizzo.drawFrazione(false);
			var frazioni  = document.getElementById("Frazione"+n_indirizzo._id_div);
			if(frazioni.options.length<1){
				n_indirizzo.drawIndirizzo();			
			}
		};
		var ajax = new Ajax();
		var query = "getComuneOf("+this._id_provincia+")";
		
		/*"SELECT ID_REGIONE AS PADRE, COORDINATE_COMUNE AS COORDINATE , ID_COMUNE as ID, NOME_COMUNE as NOME FROM GEO_COMUNE NATURAL JOIN GEO_PROVINCIA WHERE ID_PROVINCIA = '"+this._id_provincia+"'  ORDER BY NOME" ;*/


 
		this.riempiSelect(ajax,select,query,this._id_comune,this._id_provincia); 
		this.refreshSelect("Frazione"); 
		this.refreshText("Indirizzo"); 
		this.refreshText("Civico"); 
		if(this._id_comune!=null&& this._id_comune!=""&& !forza){this.drawFrazione(false);}
 
	},
	drawFrazione : function(forza){

		//alert("drawFrazione");
		var document = this._document; 
		var padre = document.getElementById("Comune"+this._id_div);
		if(this._id_comune==null|| this._id_comune==""||this._id_comune=="-1"||forza){
			this._id_comune = padre.options[padre.selectedIndex].value;
			$("imgname"+this._id_div).innerHTML = padre.options[padre.selectedIndex].text ;
		}
		var select = this.getSelect("Frazione"); 
		var n_indirizzo = this; 
 		select.onchange = function(){ 
			n_indirizzo.drawIndirizzo();
		};
		var ajax = new Ajax();
		var query = "getFrazioneOf("+this._id_comune+")";
		
		/*"SELECT  ID_REGIONE AS PADRE, COORDINATE_FRAZIONE AS COORDINATE , ID_FRAZIONE as ID, NOME_FRAZIONE as NOME FROM GEO_FRAZIONE NATURAL JOIN GEO_COMUNE NATURAL JOIN GEO_PROVINCIA WHERE ID_COMUNE = '"+this._id_comune+"' ORDER BY NOME" ; */

		this.riempiSelect(ajax,select,query,this._id_frazione,this._id_comune); 
		this.refreshText("Indirizzo"); 
		this.refreshText("Civico"); 
		if(this._id_frazione!=null&& this._id_frazione!=""&& !forza){
			this.drawIndirizzo();
			}
	},
	drawIndirizzo : function(){
		if(this.show_indirizzo){
			var document = this._document; 
			var textI = this.getText("Indirizzo",this._indirizzo); 
			var textC = this.getText("Civico",this._civico); 
			
			}
		}


};

