function doLogout (){
	location.href = "logout.php";
}

function doLogin (form){
	form.submit();
}

function quickSubmit(myfield,e)
{
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return true;

	if (keycode == 13)
   	{
   		myfield.form.submit();
   		return false;
   	}
	else
   		return true;
}

function gotoEvent (eventid,date){
	location.href = "event.php?id="+eventid+(date == null ? "" : "&did="+date);
}

function gotoAdmin (index){
	switch (index){
		case 0: location.href = "admin-settings.php";
				break;
		case 1: location.href = "admin-events.php";
				break;
		case 2: location.href = "admin-program.php";
				break;
		case 3: location.href = "admin-gallery.php";
				break;
		case 4: location.href = "admin-video.php";
				break;
		case 5: location.href = "admin-users.php";
				break;
		case 6: location.href = "admin-tables.php";
	}
}

function selectReservationArea (index){
	switch (index){
		case 0:	valueString = "Vicino il BANCONE";
				image="side";
				break;
				
		case 1:	valueString = "Nella sala ROSSA";
				image="red";
				break;
				
		case 2:	valueString = "Nella sala BLU";
				image="blue";
				break;
				
		case 3:	valueString = "Nella sala TV";
				image="tv";
				break;
				
		case 4:	valueString = "Nella sala HARLEY DAVIDSON";
				image="harley";
				break;
				
		case 5:	valueString = "Nella parte CENTRALE fronte PALCO";
				image="central";
				break;
				
		case 6:	valueString = "Nella sala WESTERN";
				image="western";
				break;
				
		case 7:	valueString = "Ovunque";
				image="all";
				break;
				
	}
	
	document.getElementById('reservationArea').value = valueString;
	document.getElementById('imgArea').src = "files/reserv-"+image+".jpg";
}

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 checkReservationForm(){
	if (document.getElementById('privacyBox').checked){
		
		if (document.getElementById('reservName').value != "" &&
			document.getElementById('reservMail').value !== "" &&
			document.getElementById('reservPlaces').value != "" &&
			document.getElementById('reservHour').value != "" &&
			document.getElementById('reservMobile').value != "" &&
			document.getElementById('day').value != ""){
			
			return true;
		}
		else
			alert ("Riempire tutti i campi.");
	}
	else
		alert("Spuntare la casella della legge sulla privacy per continuare.");
		
	return false;
}

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();
	}
}
