/**
 * 
 * @param preventivoIdSelected
 * @param AllCity - Carica tutte le cittą della nazione
 * @param HotelMare - Punta a hotel_mare_ricerca.php per ottenere le citta' mare
 * @return
 */
function CityLiveSearch(preventivoIdSelected, AllCity, HotelMare)
{
	/*Inserisco un loading all'interno dell'input text per la ricerca (aggiungendo una classe css)*/
	$('#nomeCity').addClass("loading");
//	loading.className='loading';
	var countryId = document.getElementById('selectCountry').value;
	if(!AllCity){
		var nomeCity = document.getElementById('nomeCity').value;
	} else {
		var nomeCity = "";
	}
	if(!HotelMare){
		action = "hotel_ricerca.php";
	} else {
		action = "hotel_mare_ricerca.php";
	}
	var strReq = action+'?action=CITY_LIVE_SEARCH&preventivoIdSelected='+preventivoIdSelected+'&nomeCity='+nomeCity+'&countryId='+countryId;
	LiveSearchList(strReq, 'listaCities');
}

function SelectCity(cityId, nomeCity)
{
	var item = document.getElementById('listaCities');
	item.style.display = 'none';
	document.getElementById('cityId').value = cityId;
	document.getElementById('nomeCity').value = nomeCity;  
}

function RicercaHotels(preventivoIdSelected)
{
	if(!Validation()){
		return;
	}

	ShowDiv('loading');
	var cityId = document.getElementById('cityId').value;
	var checkin = document.getElementById('dataDal').value;
	var numNotti = document.getElementById('numNotti').value;
	var ordine = 'nome';
	if(document.getElementById('ordinaPrezzo').checked){
		ordine = 'prezzo';      
	}
	document.getElementById('ordinamento').value = ordine;
	var strConfig = GetConfig();
	var objXmlHttp = new XMLHttp();
		objXmlHttp.SendAsync('hotel_ricerca.php?action=RICERCA_HOTELS&preventivoIdSelected=' + preventivoIdSelected + '&cityId=' + cityId + '&checkin=' + checkin + '&numNotti=' + numNotti + strConfig, callBackRicercaHotels, null, null);
	}

function callBackRicercaHotels(xmlHttp)
{
	HideDiv('loading');
	var stream = xmlHttp.responseText;
	var objRet = eval("(" + stream + ")");  
	if(objRet.mess == 'SUCCESS'){    
		document.getElementById('nomeCityHidden').value = document.getElementById('nomeCity').value;
		document.getElementById('selectCountryHidden').value = document.getElementById('selectCountry').value;
		//document.form_hotel_ricerca_risultati.submit(); 
		document.forms[0].submit();
		//document.getElementById('form_hotel_ricerca_risultati').submit();
	}else{
		alert('Ricerca hotels non andata a buon fine, contattare l\'amministratore di sistema!');
	}
}

function Validation()
{ 
	var item = document.getElementById('cityId');
	if (item != null){
		var location = item.value;
		if (location == ''){
			alert("inserire una localit" + String.fromCharCode(224) + " valida!");
			return false;
		}
	}

	var data = document.getElementById('dataDal').value;  
	pattern = new RegExp("[0-9]{1,2}[-][0-9]{1,2}[-][0-9]{4}");    
	if (!pattern.test(data)){
		alert('inserire una data valida (gg-mm-aaaa)');
		return false;
	}
	var dd = data.substr(0,2);
	var mm = data.substr(3,2);
	var yy = data.substr(6,4);
	var mm = mm - 1; // javascript month range 0 - 11
	var tempDate = new Date(yy,mm,dd);
	if ((yy == tempDate.getFullYear()) && (mm == tempDate.getMonth()) && (dd == tempDate.getDate())){
		var message = "La data deve essere successiva alla data odierna!";
		var dateNow = new Date();        
		if (Math.floor(yy) < Math.floor(dateNow.getFullYear())){           
			alert(message);
			return false;
		}    
		if (Math.floor(yy) == Math.floor(dateNow.getFullYear())){      
			if (Math.floor(mm) < Math.floor(dateNow.getMonth())){    
				alert(message);
				return false;
			}
			if (Math.floor(mm) == Math.floor(dateNow.getMonth())){
				if (Math.floor(dd) < Math.floor(dateNow.getDate())){ 
					alert(message);
					return false;
				}
			}
		}
		return true;    
	}else{
		alert('inserire una data valida (gg-mm-aaaa)');
		return false;
	}
}