var Y = YAHOO;

Controls = function(oConfigs){
	this._objProvincia = document.getElementById(oConfigs.provincia);
	this._objComune = document.getElementById(oConfigs.comune);
	this._objTipologia = document.getElementById(oConfigs.tipologia);
	this._objSubTipologia = document.getElementById(oConfigs.subtipologia);
	
	this._objComune.disabled=true;
	this._objSubTipologia.disabled=true;
		
	Y.util.Event.addListener(this._objProvincia, 'change', this._changeProvincia,null,this);
	Y.util.Event.addListener(this._objTipologia, 'change', this._changeTipologia,null,this);
	this._ajaxURL = (oConfigs.ajaxURL)?oConfigs.ajaxURL:'';
	this._lang = (oConfigs.lang)?oConfigs.lang:'it';
}

Controls.prototype = {
	_changeProvincia :function(e){
		var target = Y.util.Event.getTarget(e);
		var id = this._objProvincia.options[target.selectedIndex].value;
		if(id==0)
			return;
		Y.util.Connect.asyncRequest('POST', this._ajaxURL+'ajax/comuni.php', { success: this._successComuni, failure: this._error, scope:this} , 'id='+id);  
	},
	
	_successComuni :function(o){
		if (o && o.responseXML) {
			this._hideOption(this._objProvincia);
			this._clearSelect(this._objComune);
			var root=o.responseXML.documentElement;
			var s=null;
			if(root.attributes['selected'])
				s=root.attributes['selected'].value;
			for(var i=0;i<root.childNodes.length;i++){
				this._objComune.options[i] = new Option();
				this._objComune.options[i].value = root.childNodes[i].attributes[0].value;
				this._objComune.options[i].innerHTML = root.childNodes[i].attributes[1].value;
				if(s==root.childNodes[i].attributes[0].value){
					this._objComune.selectedIndex=i;
				}
			}
			if(this._ajaxURL==''){
				this._objComune.options[i] = new Option();
				this._objComune.options[i].value = 0;
				if(this._lang=="en")
					this._objComune.options[i].innerHTML = '... All cities';
				else
					this._objComune.options[i].innerHTML = '... Tutti i comuni';
			}
			this._objComune.disabled=false;
		}
	},
	
	_changeTipologia :function(e){
		var target = Y.util.Event.getTarget(e);
		var id = this._objTipologia.options[target.selectedIndex].value;
		if(id==0)
			return;
		Y.util.Connect.asyncRequest('POST', this._ajaxURL+'ajax/subtipologie.php', { success: this._successTipologia, failure: this._error, scope:this} , 'id='+id+'&lang='+this._lang);  
	},
	
	_successTipologia :function(o){
		if (o && o.responseXML) {
			this._hideOption(this._objTipologia);
			this._objSubTipologia.disabled=true;
			var root=o.responseXML.documentElement;
			this._clearSelect(this._objSubTipologia);
			if(root.childNodes.length>0){
				var s=null;
				if(root.attributes['selected'])
					s=root.attributes['selected'].value;
				for(var i=0;i<root.childNodes.length;i++){
					this._objSubTipologia.options[i] = new Option();
					this._objSubTipologia.options[i].value = root.childNodes[i].attributes[0].value;
					this._objSubTipologia.options[i].innerHTML =root.childNodes[i].attributes[1].value;
					if(s==root.childNodes[i].attributes[0].value)
						this._objSubTipologia.selectedIndex=i;
				}		
				if(this._ajaxURL==''){
					this._objSubTipologia.options[i] = new Option();
					this._objSubTipologia.options[i].value = 0;
					if(this._lang=="en")
						this._objSubTipologia.options[i].innerHTML = '... All subtypology';
					else
						this._objSubTipologia.options[i].innerHTML = '... Tutte le sottotipologie';
				}
				this._objSubTipologia.disabled=false;
			}else{
				this._objSubTipologia.options[0] = new Option();
				this._objSubTipologia.options[0].value = 0;
				if(this._lang=="en")
					this._objSubTipologia.options[0].innerHTML = 'Subtypology not present';
				else
					this._objSubTipologia.options[0].innerHTML = 'sotto tipologie non presenti';
			}
		}
	},
	
	_error :function(){
		alert('Errore Chiamata Ajax!');
	},
	
	init :function(idp,idc,idt,ids){
		Y.util.Connect.asyncRequest('POST', this._ajaxURL+'ajax/comuni.php', { success: this._successComuni, failure: this._error, scope:this} , 'id='+idp+'&idc='+idc); 
		if(ids)
			Y.util.Connect.asyncRequest('POST', this._ajaxURL+'ajax/subtipologie.php', { success: this._successTipologia, failure: this._error, scope:this} , 'id='+idt+'&ids='+ids+'&lang='+this._lang); 
	},
	
	_clearSelect :function(el){
		for(var a = el.options.length; a>=0; a--)
			el.options[a]=null;
	},

	_hideOption :function(el){
		if(el.options[0].value==0)
			el.options[0]=null;
	}
}
