//---------------------------------------------------------
//	GESTIONE PRENOTAZIONI
//---------------------------------------------------------
 function movePrevMonth(){
	var month = parseInt(document.getElementById('hddCurMonth').value,10);
	var year = parseInt(document.getElementById('divCurYear').innerHTML,10);
	if(month==1){
		month = 12;
		year--;
	}
	else month--;	
	var risp = ajaxRequestPost('core.php','mode=book_1&month='+month+'&year='+year);
	document.getElementById('divBooking').innerHTML = risp;
 }

 function moveNextMonth(){
	var month = parseInt(document.getElementById('hddCurMonth').value,10);
	var year = parseInt(document.getElementById('divCurYear').innerHTML,10);
	if(month==12){
		month = 1;
		year++;
	}
	else month++;	
	var risp = ajaxRequestPost('core.php','mode=book_1&month='+month+'&year='+year);
	document.getElementById('divBooking').innerHTML = risp;
 }
 
 function nextWeek(tempo){
	var risp = ajaxRequestPost('core.php','mode=book_4&time='+tempo+'&week=next');
	document.getElementById('divWeekBooking').innerHTML = risp;
 }

 function prevWeek(tempo){
	var risp = ajaxRequestPost('core.php','mode=book_4&time='+tempo+'&week=prev');
	document.getElementById('divWeekBooking').innerHTML = risp;
 }
 
 function dayBooking(time,reload){
	var risp = ajaxRequestPost('core.php','mode=book_2&time='+time);
	document.getElementById('divDayBooking').innerHTML = risp;
	if(!reload)openDiv('divDayBooking');
 }

 function addRoom(admin){
	if(document.getElementById('txtFrom').value!=""&&document.getElementById('txtTo').value!=""){
		var from = document.getElementById('txtFrom').value;
		var to = document.getElementById('txtTo').value;
		if(compareDate(from,to)==1||compareDate(from,to)==0){
			alert('La data di partenza è precedente alla data di arrivo');
			return false;
		}
		from = document.getElementById('txtFrom').value.split('/');
		to = document.getElementById('txtTo').value.split('/');
		//controllo che la stanza sia libera in quell'intervallo di tempo solo se sono l'amministratore
		if(admin){
			var risp = ajaxRequestPost('core.php','mode=book_5&room='+document.getElementById('selRoom').value+'&from='+from+'&to='+to);
			if(risp=='occuped'){
				alert('Attenzione. La camera risulta già prenotata in questo periodo.')
				return false;
			}
		}
		var tipo = document.getElementById('selRoom').options[document.getElementById('selRoom').selectedIndex].text;
		var room = document.createElement('DIV');
		room.className = 'room_voice';
		room.innerHTML = '<input type="hidden" value="'+document.getElementById('selRoom').value+'|'+from+'|'+to+'"/>';
		room.innerHTML = room.innerHTML + '<div class="d1">'+tipo+'</div>';	
		room.innerHTML = room.innerHTML + '<div class="d2">'+document.getElementById('txtFrom').value+'<br>'+document.getElementById('txtTo').value+'</div>';
		room.innerHTML = room.innerHTML + '<div class="d3" onclick="deleteRoom(this);"><img style="cursor:pointer;" src="images/t_del.png"/></div>';	
		document.getElementById('divRoomList').appendChild(room);
	}
 }
 
 function deleteRoom(obj){
 	if(obj.parentNode.className=='room_voice'){
		document.getElementById('divRoomList').removeChild(obj.parentNode);
	}
 }
 
 function deleteRoomBooking(id,book){
	var risp = ajaxRequestPost('core.php','mode=book_8&availability='+id+'&book='+book);
	document.getElementById('divDetails').innerHTML = risp;
	reloadWeek();
	reloadMonth();
 }
 
 function summaryBooking(){
	var name = escape(document.getElementById('txtName').value);
	var mail = escape(document.getElementById('txtMail').value);
	var phone = document.getElementById('txtPhone').value;
	var fax = document.getElementById('txtFax').value;
	var note = escape(document.getElementById('txtNote').value);
	//prendo tutte le camere richieste
	var rooms = document.getElementById('divRoomList').getElementsByTagName('INPUT');
	var strRoom = '';
	for (var i = 0; i < rooms.length; i++) {
		var app = rooms[i].value.replace(/\,/gi,'/');
		strRoom = strRoom + '' + app + '_';
	}
	if(strRoom==""){
		alert('Attenzione. Non è stata scelta nessuna camera da prenotare.');
	}else if(name&&(mail||phone)){
		var risp = ajaxRequestPost('core.php','mode=book_14&name='+name+'&mail='+mail+'&phone='+phone+'&fax='+fax+'&note='+note+'&book='+strRoom);
		document.getElementById('divSummary').innerHTML = risp;
		openDiv('divSummary');
	}else{
		alert('Attenzione. Non sono stati compilati tutti i campi obbligatori');
	}
 }
 
 function requestBooking(admin,edit){
	var name = escape(document.getElementById('txtName').value);
	var mail = escape(document.getElementById('txtMail').value);
	var phone = document.getElementById('txtPhone').value;
	var fax = document.getElementById('txtFax').value;
	var note = escape(document.getElementById('txtNote').value);
	//prendo tutte le camere richieste
	var rooms = document.getElementById('divRoomList').getElementsByTagName('INPUT');
	var strRoom = '';
	for (var i = 0; i < rooms.length; i++) {
		var app = rooms[i].value.replace(/\,/gi,'/');
		strRoom = strRoom + '' + app + '_';
	}
	if(strRoom==""){
		alert('Attenzione. Non è stata scelta nessuna camera da prenotare.');
	}else if(name&&(mail||phone)){
		if(admin){
			if(edit){
				var id_book = document.getElementById('hddBook').value;
				//prima elimino le camere prenotate precedentemente
				var risp = ajaxRequestPost('core.php','mode=book_10&booking='+id_book);
				//poi le ricreo con quelle che trovo nel form
				var risp = ajaxRequestPost('core.php','mode=book_11&booking='+id_book+'&name='+name+'&mail='+mail+'&phone='+phone+'&fax='+fax+'&note='+note+'&book='+strRoom);
			}else{
				var risp = ajaxRequestPost('core.php','mode=book_6&name='+name+'&mail='+mail+'&phone='+phone+'&fax='+fax+'&note='+note+'&book='+strRoom);
			}
			reloadWeek();
			reloadMonth();
			reloadEditBooking(1);
			if(risp=='ok')alert('Prenotazione creata correttamente.');
		}else{
			var risp = ajaxRequestPost('core.php','mode=book_3&name='+name+'&mail='+mail+'&phone='+phone+'&fax='+fax+'&note='+note+'&book='+strRoom);
			reloadEditBooking();
			if(risp=='ok')alert('Prenotazione inviata correttamente.');
			else alert("Si è verificato un errore riguardante il vostro indirizzo email. Controllare che sia digitato correttamente.")
		}
	}else{
		alert('Attenzione. Non sono stati compilati tutti i campi obbligatori');
	}
 }
 
 function deleteBooking(id){
	if(confirm('Si vuole veramente cancellare questa prenotazione?')){
		var risp = ajaxRequestPost('core.php','mode=book_12&booking='+id);
		closeDiv();
		reloadEditBooking(1);
		reloadWeek();
		reloadMonth();
	}
 }
 
 function searchBooking(){
	var key = document.getElementById('txtSearch').value;
	if(key){
		var risp = ajaxRequestPost('core.php','mode=book_13&key='+key);
		document.getElementById('divSearchResult').innerHTML = risp;
	}
 }
 
 function reloadWeek(){
	//ricarico la vista settimanale
	var risp2 = ajaxRequestPost('core.php','mode=book_4&week=current');
	document.getElementById('divWeekBooking').innerHTML = risp2;
 }
 
 function reloadMonth(){
	//ricarico il calendario
	var risp2 = ajaxRequestPost('core.php','mode=book_1');
	document.getElementById('divBooking').innerHTML = risp2;
 }
 
 function reloadEditBooking(admin){
	//ricarico il form di inserimento prenotazioni
	if(!admin)admin = '';
	var risp2 = ajaxRequestPost('core.php','mode=book_9&admin='+admin);
	document.getElementById('divEditBooking').innerHTML = risp2;
 }
 
 function bookingDetails(id){
	var risp = ajaxRequestPost('core.php','mode=book_7&book='+id);
	document.getElementById('divDetails').innerHTML = risp;
	openDiv('divDetails');
 }
 
 function openEditBooking(id){
	closeDiv();
	if(document.getElementById('divMask').style.display=='block')closeDiv();
	var risp = ajaxRequestPost('core.php','mode=book_9&admin=1&book='+id);
	document.getElementById('divEditBooking').innerHTML = risp;
 }
 
//---------------------------------------------------------
//	GESTIONE POST
//---------------------------------------------------------
 
 //termina l'upload dei file
 function uploadPostImage(filename){
	document.getElementById('imgUp').src=filename;
	document.getElementById('hddImgPost').value=filename;
 }
 
 //inserimento di un nuovo post
 function openNewPost(){
	//creo il nuovo post nel db e prendo l'id
	var risp = ajaxRequestPost('core.php','mode=post_112');
	var app = parseInt(risp,10);
	//carico la finestra di edit
	var risp2 = ajaxRequestPost('core.php','mode=post_113&post='+app);
	document.getElementById('divPostEdit').innerHTML = risp2;
	//carico il contenuto nell'editor
	var risp3 = ajaxRequestPost('core.php','mode=post_111&post='+app);
	openEditor(risp3);
 }
 
 //carica il post selezionato
 function loadCurrentPost(obj){
	var id = obj.id.split('p_')[1];
	//carico la finestra di edit
	var risp2 = ajaxRequestPost('core.php','mode=post_113&post='+id);
	document.getElementById('divPostEdit').innerHTML = risp2;
	//carico il contenuto nell'editor
	var risp3 = ajaxRequestPost('core.php','mode=post_111&post='+id);
	openEditor(risp3);
 }
 
 //salva il post 
 function savePost(){
	var idPost = document.getElementById('hddPostId').value;
	//salvataggio di un post titolo + testo + immagine
	var titolo = escape(document.getElementById('txtPostTitle').value);
	var testo = escape(CKEDITOR.instances.txtPostEditor.getData());
	if(titolo!=""&&testo!=''){
		var risp = ajaxRequestPost('core.php','mode=post_114&title='+titolo+'&content='+testo+'&post='+idPost);
		//aggiorno l'interfaccia
		document.getElementById('divPostEdit').innerHTML = "";
		document.getElementById('divPosts').innerHTML = risp;
	}else{
		alert('Attenzione. Alcuni campi sono stati lasciati vuoti.')
	}	
 }
 
 //elimina il post selezionato
 function deletePost(){
	if(document.getElementById('hddPostId').value!=0){
		if(confirm("Si vuole procedere alla cancellazione di questo menù?")){
			var risp = ajaxRequestPost('core.php','mode=post_115&post='+document.getElementById('hddPostId').value);
			//aggiorno l'interfaccia
			document.getElementById('divPostEdit').innerHTML = "";
			document.getElementById('divPosts').innerHTML = risp;
		}
	}else{
		alert('Attenzione. Nessuna notizia selezionata.');
	}
 }
 
 //completa l'upload di un'immagine su una news
 function addPostImage(imgSrc,id){
	document.getElementById('imgLoading').style.visibility = "hidden";
	var photo = document.createElement('SPAN');
	photo.id = 'sp_'+id;
	photo.className = 'albumImage';
	var photoImg = document.createElement('IMG');
	photoImg.src = imgSrc;
	photoImg.id = 'img_'+id;
	photo.appendChild(photoImg);
	document.getElementById('divImageList').appendChild(photo);
 }

//---------------------------------------------------------
//	GESTIONE PREZZI
//---------------------------------------------------------
 function editPrice(){
	var testo = escape(CKEDITOR.instances.txtEditor.getData());
	var risp = ajaxRequestPost('core.php','mode=price_2&content='+testo);
	alert('Modifiche salvate.');
 }
 
//---------------------------------------------------------
//	GESTIONE PROMOZIONI
//---------------------------------------------------------
 
 //termina l'upload dei file
 function uploadOfferImage(filename){
	document.getElementById('imgUp').src=filename;
	document.getElementById('hddImgOffer').value=filename;
 }
 
 //inserimento di un nuovo post
 function openNewOffer(){
	//creo il nuovo post nel db e prendo l'id
	var risp = ajaxRequestPost('core.php','mode=offer_112');
	var app = parseInt(risp,10);
	//carico la finestra di edit
	var risp2 = ajaxRequestPost('core.php','mode=offer_113&offer='+app);
	document.getElementById('divOfferEdit').innerHTML = risp2;
	//carico il contenuto nell'editor
	var risp3 = ajaxRequestPost('core.php','mode=offer_111&offer='+app);
	openEditor(risp3);
 }
 
 //carica il post selezionato
 function loadCurrentOffer(obj){
	var id = obj.id.split('o_')[1];
	//carico la finestra di edit
	var risp2 = ajaxRequestPost('core.php','mode=offer_113&offer='+id);
	document.getElementById('divOfferEdit').innerHTML = risp2;
	//carico il contenuto nell'editor
	var risp3 = ajaxRequestPost('core.php','mode=offer_111&offer='+id);
	openEditor(risp3);
 }
 
 //salva il post 
 function saveOffer(){
	var idOffer = document.getElementById('hddOfferId').value;
	//salvataggio di un post titolo + testo + immagine
	var titolo = escape(document.getElementById('txtOfferTitle').value);
	var testo = escape(CKEDITOR.instances.txtOfferEditor.getData());
	if(titolo!=""&&testo!=''){
		var risp = ajaxRequestPost('core.php','mode=offer_114&title='+titolo+'&content='+testo+'&offer='+idOffer);
		//aggiorno l'interfaccia
		document.getElementById('divOfferEdit').innerHTML = "";
		document.getElementById('divOffers').innerHTML = risp;
	}else{
		alert('Attenzione. Alcuni campi sono stati lasciati vuoti.')
	}	
 }
 
 //elimina il post selezionato
 function deleteOffer(){
	if(document.getElementById('hddOfferId').value!=0){
		if(confirm("Si vuole procedere alla cancellazione di questa offerta?")){
			var risp = ajaxRequestPost('core.php','mode=offer_115&offer='+document.getElementById('hddOfferId').value);
			//aggiorno l'interfaccia
			document.getElementById('divOfferEdit').innerHTML = "";
			document.getElementById('divOffers').innerHTML = risp;
		}
	}else{
		alert('Attenzione. Nessuna notizia selezionata.');
	}
 }
 
 //completa l'upload di un'immagine su una news
 function addOfferImage(imgSrc,id){
	document.getElementById('imgLoading').style.visibility = "hidden";
	var photo = document.createElement('SPAN');
	photo.id = 'sp_'+id;
	photo.className = 'albumImage';
	var photoImg = document.createElement('IMG');
	photoImg.src = imgSrc;
	photoImg.id = 'img_'+id;
	photo.appendChild(photoImg);
	document.getElementById('divImageList').appendChild(photo);
 } 
 
//---------------------------------------------------------
//	GESTIONE IMMAGINI
//---------------------------------------------------------
 
 //completa l'upload di un'immagine su un album
 function addAlbumImage(imgSrc,id){
	document.getElementById('imgLoading').style.visibility = "hidden";
	var album = document.getElementById('hddAlbum').value;
	var photo = document.createElement('SPAN');
	photo.id = 'sp_'+id;
	photo.className = 'albumImage';
	var photoImg = document.createElement('IMG');
	photoImg.src = imgSrc;
	photoImg.id = 'img_'+id;
	photo.appendChild(photoImg);
	document.getElementById('divImageList').appendChild(photo);
 }
 
 //crea un nuovo album
 function addAlbum(){
	var risp = ajaxRequestPost('core.php','mode=img_115');
	document.getElementById('divCurAlbum').innerHTML = risp;
 }
 
 //edita l'album attuale e ricarica la lista
 function editAlbum(){
	var id = document.getElementById('hddAlbum').value;
	var name = escape(document.getElementById('txtAName').value);
	var desc = escape(document.getElementById('txtADesc').value);
	var risp = ajaxRequestPost('core.php','mode=img_116&album='+id+'&name='+name+'&desc='+desc);
	document.getElementById('divAlbumGrid').innerHTML = risp;
	document.getElementById('divCurAlbum').innerHTML = "";
 }
 
 //carica l'album selezionato
 function loadAlbumEdit(id){
	var risp = ajaxRequestPost('core.php','mode=img_117&album='+id);
	document.getElementById('divCurAlbum').innerHTML = risp;
 }
 
 //elimina l'album selezionato e tutte le sue foto
 function deleteAlbum(id){
 	if(confirm("Si vuole veramente eliminare quest'album?")){
 		var risp = ajaxRequestPost('core.php','mode=img_118&album='+id);
 		document.getElementById('divCurAlbum').innerHTML = "";
		//ricarico la lista degli album
 		document.getElementById('divAlbumGrid').innerHTML = risp;
 	}
 }
 
 //elimina l'immagine selezionata
 function deleteImage(evt){
	var tar = getTarget(evt);
	if(tar.tagName=='IMG'){
		var id = tar.id.split('_')[1];
		var risp = ajaxRequestPost('core.php','mode=img_119&image='+id);
		document.getElementById('divImageList').removeChild(document.getElementById('sp_'+id));
	}
 }
 
