var AuCap = null;
var AuComune = null;
var AuProvincia = null;

function enableAutocompleter(){
	var cap = $('cap_spedizione');
	var comune = $('comune_spedizione');
	var provincia = $('provincia_spedizione');
	
	if(!($chk(comune) && $chk(provincia) && $chk(cap))){return;}
	
	if(AuCap == null){
		AuCap = new Autocompleter.Request.HTML(cap, getCap, 'utils/normalizeAddress.php', {
			'setResourceRange':function(value){var str = value.substring(1); $('comune_cap').set('value',str);},
			'indicatorClass': 'autocompleter-loading',
			'postData': {
				'extended': 'cap' // send additional POST data, check the PHP code
			},
			'injectChoice': function(choice) {
			// choice is one <li> element
			var text = choice.getFirst();
			// the first element in this <li> is the <span> with the text
			var value = text.innerHTML;
			// inputValue saves value of the element for later selection
			choice.inputValue = value;
			// overrides the html with the marked query value (wrapped in a <span>)
			text.set('html', this.markQueryValue(value));
			// add the mouse events to the <li> element
			this.addChoiceEvents(choice);
		}

		});
	}
	
	if(AuComune == null){
		AuComune = new Autocompleter.Request.JSON(comune, getCodeComune, 'utils/normalizeAddress.php', {
			'postData': {
				'extended': 'comune' // send additional POST data, check the PHP code
			},
			'indicatorClass': 'autocompleter-loading'
		});
	}
	
	
	if(AuProvincia == null){
		AuProvincia = new Autocompleter.Request.JSON(provincia, getCodeProvincia, 'utils/normalizeAddress.php', {
			'postData': {
				'extended': 'provincia' // send additional POST data, check the PHP code
			},
			'indicatorClass': 'autocompleter-loading'
		});
	}
}


function getCap(){
	var cap = encode($('cap_spedizione').value);
	var comune_cap = encode($('comune_cap').value);

	if(cap == ""){return;}

	var data  = "cap="+cap;
	data += "&comune_cap="+comune_cap;
	data += "&op=checkCap&args=cap,comune_cap";
	
	var ajax = new Request.HTML({
		url:'utils/normalizeAddress.php',
		method:'post',
		data: data,
		onSuccess : function(responseTree, responseElements, responseHTML, responseJavaScript) {
			var xml = responseTree.item(0);
			var comune = xml.getElement('comune').get('text');
			var cod_comune = xml.getElement('cod_comune').get('text');
			var cod_provincia = xml.getElement('cod_provincia').get('text');
			var provincia = xml.getElement('provincia').get('text');
			
			if($('nazione_spedizione').value == "ITA"){
				$('comune_spedizione').set('value',comune);
				$('comune_istat').set('value',cod_comune);
				$('provincia_istat').set('value',cod_provincia);
				$('provincia_spedizione').set('value',provincia);
			}else{
				//$('comune_istat').set('value','0');
			}
			
			//console.log("cod comune: %s", cod_comune);
		}
	});
	
	ajax.send();
}


function checkCodeComune(el){
	if($('nazione_spedizione').value == "ITA" && $('comune_istat').get('value') == ""){
		var errorMsg = "Comune non riconosciuto.";
		el.errors.push( errorMsg );
		return false;
	}
	return true;
}

function getCodeComune(){
	var nome_comune = encode($('comune_spedizione').value);

	var data  = "comune="+nome_comune;
	data += "&op=checkComune&args=comune";
	
	var ajax = new Request.HTML({
		url:'utils/normalizeAddress.php',
		method:'post',
		data: data,
		onSuccess : function(responseTree, responseElements, responseHTML, responseJavaScript) {
			var xml = responseTree.item(0);
			var cap = xml.getElement('cap').get('text');
			var cod_comune = xml.getElement('cod_comune').get('text');
			var cod_provincia = xml.getElement('cod_provincia').get('text');
			var provincia = xml.getElement('provincia').get('text');
			
			if($('nazione_spedizione').value == "ITA"){
				//$('cap_spedizione').value == "" ? $('cap_spedizione').set('value',cap) : null ;
				$('comune_istat').set('value',cod_comune);
				$('provincia_istat').set('value',cod_provincia);
				$('provincia_spedizione').set('value',provincia);
			}else{
				$('comune_istat').set('value','0');
			}
			
			//console.log("cod comune: %s", cod_comune);
		}
	});
	
	ajax.send();
}

function checkCodeProvincia(el){
	if($('nazione_spedizione').value == "ITA" && $('provincia_istat').get('value') == ""){
		var errorMsg = "Provincia non riconosciuta.";
		el.errors.push( errorMsg );
		return false;
	}
	return true;
}

function getCodeProvincia(){
	var nome_provincia = this.value;
	
	var data  = "provincia="+nome_provincia;
	data += "&op=checkProvincia&args=provincia";
	
	var ajax = new Request.HTML({
		url:'utils/normalizeAddress.php',
		method:'post',
		data: data,
		onSuccess : function(responseTree, responseElements, responseHTML, responseJavaScript) {
			var xml = responseTree.item(0);
			var istat = xml.getElement('cod_istat').get('text');
			if($('nazione_spedizione').value == "ITA"){
				$('provincia_istat').set('value',istat);
			}else{
				$('provincia_istat').set('value','0');
			}
			//console.log("cod provincia: %s", istat);
		}
	});
	
	ajax.send();
}

function checkNazione(el){
	if(el.value=="ITA"){
		if($('provincia_istat').value=='0'){
			$('provincia_istat').set('value','');
			$('provincia_spedizione').set('value','');
		}
		if($('comune_istat').value=='0'){
			$('comune_istat').set('value','');
			$('comune_spedizione').set('value','');
		}
	}else{
		$('provincia_istat').set('value','0');
		$('comune_istat').set('value','0');
		
		$('comune_spedizione').set('value','');
		$('provincia_spedizione').set('value','');
	}
}
