
/**
 * Spinner
 */
function rePositionSpinner() {			
	var spinner = document.getElementById('a4jstatus');
	var position = document.documentElement.scrollTop + Math.floor( document.documentElement.clientHeight / 2 );
	//alert('position: '+ document.documentElement.scrollTop );	// DEBUG
	spinner.style.top = position + 'px';
}

function swapText(src,dest) {			
	var temp = document.getElementById(dest).innerHTML;
	document.getElementById(dest).innerHTML = document.getElementById(src).innerHTML;
	document.getElementById(src).innerHTML = temp;
}

function swapLabel( element , src , dest ) { element.innerHTML == src ? element.innerHTML = dest : element.innerHTML = src; }

/**
* Apre la finestra di Gmap
*/
function openGmapWindow( url ) {
	var w = 750, h = 550; // default sizes
	var	xPos = 250, yPos = 250;

	if (window.screen) {
		w = window.screen.availWidth * 90 / 100;
		h = window.screen.availHeight * 70 / 100;
		xPos = w/15;
		yPos = h/15;
	}
	hmap = h - 100;

	window.open(url+'&h='+hmap, 'mapWindow', 'scrollbars=yes, location=no,toolbar=no,resizable=1,width='+w+',height='+h+',top='+xPos+',left='+yPos);
}

function anchorTo( anchor ) {
	var urllocation = location.href; //find url parameter
	if(urllocation.indexOf("#") == -1 || urllocation.indexOf("#") > -1) {
		window.location.hash=anchor; 
	}
}

/**
 * Prende la data di inizio e setta la data
 * fine al giorno successivo di una certa form.
 * ID input data inizio: inputDataInizio
 * ID input data fine: inputDataFine
 */
function setDataFineADomani( formID ) { 	

	// Estrazione e conversione input
	var input = document.getElementById(formID+':inputDataInizioInputDate').value;  			
	var anno = parseInt(input.substring(6,10),10);
	var mese = parseInt(input.substring(3,5),10)-1;
	var giorno = parseInt(input.substring(0,2),10);

	if ( giorno > 0 && giorno < 32 && mese >= 0 && mese < 13 && anno > 1000 ) {

		var annoFine = (giorno==31 && mese==11) ? anno+1 : anno; 

		// Creazione della data fine
		var dataFine = new Date();	
		dataFine.setFullYear( anno , mese , giorno+1 );	

		// Conversione e scrittura nel campo form
		var giornoFine = dataFine.getDate();
		var meseFine = dataFine.getMonth()+1;
		var output = (giornoFine<10?'0'+giornoFine:giornoFine)+'/'+((meseFine)<10?'0'+meseFine:meseFine)+'/'+(annoFine);	
		document.getElementById(formID+':inputDataFineInputDate').value = output;

		//alert( 'Input: '+input+ '\nOutput: '+output );
	}

}

/**
* Esegue il controllo sulle date di checkIn e checkOut
*/
function checkDate( formID ) {

	var checkIn = document.getElementById(formID+':inputDataInizioInputDate').value;
	var checkOut = document.getElementById(formID+':inputDataFineInputDate').value; 

	//if ( checkIn == '' || checkOut == '' ) {
	//	alert('ATTENZIONE: Inserire le date di arrivo e partenza in modo corretto.');
	//	return false;
	//}

	// Estrazione e conversione input checkIn		
	var annoIn = parseInt(checkIn.substring(6,10),10);
	var meseIn = parseInt(checkIn.substring(3,5),10)-1;
	var giornoIn = parseInt(checkIn.substring(0,2),10);

	// Creazione della data fine
	var dataInizio = new Date();	
	dataInizio.setFullYear( annoIn , meseIn , giornoIn );	

	// Estrazione e conversione input checkOut 			
	var annoOut = parseInt(checkOut.substring(6,10),10);
	var meseOut = parseInt(checkOut.substring(3,5),10)-1;
	var giornoOut = parseInt(checkOut.substring(0,2),10);

	// Creazione della data fine
	var dataFine = new Date();	
	dataFine.setFullYear( annoOut , meseOut , giornoOut );	

	if ( dataInizio.getTime() < new Date().getTime() ) {
		alert('ATTENZIONE: La data di arrivo non puo essere precedente alla data odierna.');
		return false;
	}

	if ( dataFine.getTime() <= dataInizio.getTime()  ) {
		alert('ATTENZIONE: La data di partenza deve essere successiva alla data di arrivo.');
		return false;
	}

	if ( (dataFine.getTime()-dataInizio.getTime()) > 1000*60*60*24*30  ) {
		alert('ATTENZIONE: La data di partenza non puo essere superiore di 30 giorni alla data di arrivo.');
		return false;
	}

	return true; 

}