// Funzioni Javascript necessarie al funzionamento del sito
function trim(string)
{
	while (string.substring(0,1) == ' ')
	{
		string = string.substring(1, string.length);
	}
	while (string.substring(string.length-1, string.length) == ' ')
	{
		string = string.substring(0,string.length-1);
	}
	return string;
}

function contactFormSubmit (){
	
	if (!document.contactForm.privacy.checked){
		alert ("Accettare il consenso al trattamento dei dati personali per inviare il messaggio.");
		return;
	}
	
	var sName = document.contactForm.name.value = trim (document.contactForm.name.value);
	var sSurname = document.contactForm.surname.value = trim (document.contactForm.surname.value);
	var sEmail = document.contactForm.email.value = trim (document.contactForm.email.value);
	var sMessage = document.contactForm.message.value = trim (document.contactForm.message.value);
	
	if (sName == "" ||
		sSurname == "" ||
		sEmail == "" ||
		sMessage == ""){
		alert ("Inserire tutti i campi richiesti per proseguire.");
		return
	}
	
	document.contactForm.submit ();
}

function checkAndAddReservation (){
	
	
	if (!document.reservationForm.privacy.checked){
		alert ("Accettare il consenso al trattamento dei dati personali per inviare il messaggio.");
		return;
	}
	
	var sName = document.reservationForm.name.value = trim (document.reservationForm.name.value);
	var sSurname = document.reservationForm.surname.value = trim (document.reservationForm.surname.value);
	var sTel = document.reservationForm.tel.value = trim (document.reservationForm.tel.value);
	var sNotes = document.reservationForm.notes.value = trim (document.reservationForm.notes.value);
	var sEmail = document.reservationForm.email.value = trim (document.reservationForm.email.value);
	
	if (sName == "" ||
		sSurname == "" ||
		sTel == "" ||
		sNotes == "" ||
		sEmail == ""){
		alert ("Inserire tutti i campi richiesti per proseguire.");
		return
	}
	
	
	document.reservationForm.submit ();
}

function validateAndPost (){

	var sName = document.postForm.name.value = trim (document.postForm.name.value);
	var sMessage = document.postForm.message.value = trim (document.postForm.message.value);
	
	if (sName == "" ||
		sMessage == ""){
		alert ("Completare entrambi i campi richiesti per pubblicare un messaggio.");
		return
	}
	
	document.postForm.submit ();
}

function resetHeaderPhoto (index){
	if (index == 11)
		name = "Foto intro";
	else
		name = "Foto "+(index+1);
	if (confirm ("Eliminare la "+name+"?\nLe modifiche avranno effetto dopo averle confermate con il pulsante 'Applica modifiche'.")){
		var parent = document.getElementById('container'+index);
		newFileInput = document.createElement('input');
		
		parent.removeChild(document.getElementById('file'+index));
	
		newFileInput.setAttribute('type', 'file');
		newFileInput.setAttribute('id', 'file'+index);
		newFileInput.setAttribute('name', 'photo'+index);
		newFileInput.setAttribute('onchange', 'this.form.fact'+index+'.value="replace"');
		
		parent.appendChild(newFileInput);
		document.getElementById('img'+index).src="files/dot.gif";
		document.getElementById('fact'+index).value='remove';
	}
}

function resetGalleryElement (index){
	var parent = document.getElementById('container'+index);
	newFileInput = document.createElement('input');
	
	parent.removeChild(document.getElementById('photo'+index));

	newFileInput.setAttribute('type', 'file');
	newFileInput.setAttribute('id', 'photo'+index);
	newFileInput.setAttribute('name', 'photo'+index);
	newFileInput.setAttribute('onchange', 'this.form.fact'+index+'.value="add"');
	
	parent.appendChild(newFileInput);
	document.getElementById('fact'+index).value='none';
}

function validateAndAddGallery (){
	if (trim(document.galleryForm.gname.value) != ""){
		document.galleryForm.gname.value = trim(document.galleryForm.gname.value);
		document.galleryForm.act.value='gadd';
		document.galleryForm.submit();
	}
	else 
		alert('Inserisci una didascalia per la nuova galleria.');	
}

function validateAndEditGallery (){

	if (trim(document.galleryForm.gtitle.value) == "")
		alert('Inserire una didascalia per la galleria da modificare'); 
	else {
		document.galleryForm.gtitle.value = trim (document.galleryForm.gtitle.value);
		document.galleryForm.act.value = "gedit";
		document.galleryForm.submit();
	}
}

function validateAndAddEvent (){
	if (trim(document.eventForm.etitle.value) == "")
		alert ('Inserire almeno il titolo per l\'evento');
	else{
		document.eventForm.etitle.value = trim (document.eventForm.etitle.value);
		document.eventForm.act.value = "eadd";
		document.eventForm.submit();
	}
}

function validateAndEditEvent (){
	if (trim(document.eventForm.etitle.value) == "")
		alert ('Inserire almeno il titolo per l\'evento');
	else{
		document.eventForm.etitle.value = trim(document.eventForm.etitle.value);
		document.eventForm.edescription.value = trim(document.eventForm.edescription.value);
		document.eventForm.act.value = "eedit";
		document.eventForm.submit();
	}
}