// JavaScript Document

function mostra_nascondi(obj) {
  if (document.getElementById) { 
	thisobject = document.getElementById(obj).style
	if (thisobject.display == "block")  { thisobject.display = "none" }
	else { thisobject.display = "block" } 
  }
}
//VALIDAZIONE FORM
function form_inserisci() { 	 
 if (document.modulo.testo_titolo.value == "") {
  alert("Inserisci un titolo!");
  document.modulo.testo_titolo.focus();
  return false;
 }
 return true;
}
function form_registra() {
 	 if (document.modulo.nome.value == "") {
	  alert("Inserisci il tuo nome!");
	  document.modulo.nome.focus();
	  return false;
	 }
	 if (document.modulo.nome.value.length < 3) {
	  alert("Il nome deve avere una lunghezza minima di 3 caratteri");
	  document.modulo.nome.focus();
	  return false;
	 }
	 if (document.modulo.provenienza.value == "") {
	  alert("Inserisci una citta' di provenienza");
	  document.modulo.provenienza.focus();
	  return false;
	 }
	  if (document.modulo.username.value == "") {
	  alert("Inserisci una username");
	  document.modulo.username.focus();
	  return false;
	 }
	  if (document.modulo.username.value.length < 3) {
	  alert("La username deve avere una lunghezza minima di 3 caratteri");
	  document.modulo.username.focus();
	  return false;
	 }
	 if (document.modulo.password.value.length < 6) {
	  alert("La password deve avere una lunghezza minima di 6 caratteri");
	  document.modulo.password.focus();
	  return false;
	 }
	  if (document.modulo.password.value != document.modulo.password_2.value) {
	  alert("Le password inserite non corrispondono");
	  document.modulo.password_2.focus();
	  return false;
	 }
	 var email_reg_exp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;
	 if (!email_reg_exp.test(document.modulo.email.value) || (document.modulo.email.value == "") || (document.modulo.email.value == "undefined")) {
	  alert("Inserire un indirizzo email corretto!");
	  document.modulo.email.select();
	  return false; 
	 }
}
function form_galleria() {
	if (document.getElementById("galleria_titolo").value == "") {
	  alert("Inserisci un titolo!");
	  document.getElementById("galleria_titolo").focus();
	  return false;
	 }
	 return true;	 
 }

//INIZIALIZZAZIONE AJAX
function assegnaXMLHttpRequest() {
	var XHR = null,
  browserUtente = navigator.userAgent.toUpperCase();
 if(typeof(XMLHttpRequest) === "function" || typeof(XMLHttpRequest) === "object")
  XHR = new XMLHttpRequest();
 else if(
  window.ActiveXObject &&
  browserUtente.indexOf("MSIE 4") < 0
 ) {
  if(browserUtente.indexOf("MSIE 5") < 0)
   XHR = new ActiveXObject("Msxml2.XMLHTTP");
  else
   XHR = new ActiveXObject("Microsoft.XMLHTTP");
 }
 return XHR;
}

//NAVIGAZIONE OPERE
function visualizza_opere(what, anno, mese, ordina) {
	 
   if (anno != 0) {
	   document.getElementById("anno").value = anno;
	   document.getElementById("mese").value = 0;
   }
   if (mese != 0) document.getElementById("mese").value = mese;
   if (ordina != 0) document.getElementById("ordina").value = ordina;
  
	if (mese == 255) document.getElementById("mese").value = 0;
	if (anno == 255) { 
		document.getElementById("mese").value = 0;
		document.getElementById("anno").value = 0;
	}
  
  
   anno = document.getElementById("anno").value;
   mese = document.getElementById("mese").value;
   ordina = document.getElementById("ordina").value;
   
   var w = "&what=" + what + "&anno=" + anno + "&mese=" + mese + "&ordina=" + ordina;
    var ajax1 = assegnaXMLHttpRequest();
   if(ajax1) {
		ajax1.open("post", "/contenuti/ajax/visualizza/opere.php", true);
		ajax1.setRequestHeader("content-type", "application/x-www-form-urlencoded");
		ajax1.setRequestHeader("connection", "close");
		ajax1.onreadystatechange = function() {
		if(ajax1.readyState === 4) {
			if(ajax1.status == 200) {
				document.getElementById(what).innerHTML = ajax1.responseText
 				 $(function() {$("#vis_tb_" + what + " tbody tr").quicksearch({ position: 'before', attached: 'table#vis_tb_' + what, stripeRowClass: ['odd', 'even'], labelText: 'Rircerca rapida:' });});
			} else alert("Si e' verificato un errore. Codice errore: " + ajax1.readyState);
		}
	}
	ajax1.send(w);
 	}
}

//PROFILO
function index_profilo() {
   var autore = document.getElementById("autore").value;
   var ajax0 = assegnaXMLHttpRequest();
   var what = "autore=" + autore;
    if(ajax0) {
		ajax0.open("post", "/contenuti/ajax/profilo/profilo.php", true);
		ajax0.setRequestHeader("content-type", "application/x-www-form-urlencoded");
		ajax0.setRequestHeader("connection", "close");
		ajax0.onreadystatechange = function() {
		if(ajax0.readyState === 4) {
			if(ajax0.status == 200) {
				document.getElementById("profilo").innerHTML = ajax0.responseText				 
			} else alert("Si e' verificato un errore. Codice errore: " + ajax0.readyState);
		}
	}
	ajax0.send(what);
 	}
}
function opere_profilo(type, anno, mese, ordina) {
	
   var autore = document.getElementById("autore").value;
   if (type != 0) document.getElementById("type").value = type;
   if (type == 255) document.getElementById("type").value = 0;
   
   if (anno != 0) {
	   document.getElementById("anno").value = anno;
	   document.getElementById("mese").value = 0;
   }
   if (mese != 0) document.getElementById("mese").value = mese;
   if (ordina != 0) document.getElementById("ordina").value = ordina;
  
	if (mese == 255) document.getElementById("mese").value = 0;
	if (anno == 255) { 
		document.getElementById("mese").value = 0;
		document.getElementById("anno").value = 0;
	}
  
  
   type = document.getElementById("type").value;
   anno = document.getElementById("anno").value;
   mese = document.getElementById("mese").value;
   ordina = document.getElementById("ordina").value;
   
   var what = "autore=" + autore + "&type=" + type + "&anno=" + anno + "&mese=" + mese + "&ordina=" + ordina;
    
   var ajax1 = assegnaXMLHttpRequest();
   if(ajax1) {
		ajax1.open("post", "/contenuti/ajax/profilo/opere.php", true);
		ajax1.setRequestHeader("content-type", "application/x-www-form-urlencoded");
		ajax1.setRequestHeader("connection", "close");
		ajax1.onreadystatechange = function() {
		if(ajax1.readyState === 4) {
			if(ajax1.status == 200) {
				document.getElementById("opere").innerHTML = ajax1.responseText
				 $(function() {$("#vis_tb tbody tr").quicksearch({ position: 'before', attached: 'table#vis_tb', stripeRowClass: ['odd', 'even'], labelText: 'Rircerca rapida:' });});
			} else alert("Si e' verificato un errore. Codice errore: " + ajax1.readyState);
		}
	}
	ajax1.send(what);
 	}
}
function cartelle_profilo(ref) {
   var autore = document.getElementById("autore").value;
   var what = "autore=" + autore + "&ref=" + ref;
   var ajax2 = assegnaXMLHttpRequest();
   if(ajax2) {
		ajax2.open("post", "/contenuti/ajax/profilo/cartelle.php", true);
		ajax2.setRequestHeader("connection", "close");
		ajax2.setRequestHeader("content-type", "application/x-www-form-urlencoded");
		ajax2.onreadystatechange = function() {
		if(ajax2.readyState === 4) {
			if(ajax2.status == 200) {
				document.getElementById("cartelle").innerHTML = ajax2.responseText
 				 $(function() {$("#cart_tb tbody tr").quicksearch({ position: 'before', attached: 'table#cart_tb', stripeRowClass: ['odd', 'even'], labelText: 'Rircerca rapida:' });});
				 
			} else alert("Si e' verificato un errore. Codice errore: " + ajax2.readyState);
		}
	}
 	ajax2.send(what);
 	}
}
function cartella_rimuovi(what, codice, ref) {
	var autore = document.getElementById("autore").value;
   var what = "codice=" + codice + "&autore=" + autore + "&what=" + what;
   
   var ajax5 = assegnaXMLHttpRequest();
   if(ajax5) {
		ajax5.open("post", "/contenuti/ajax/profilo/cartella_rimuovi.php", true);
		ajax5.setRequestHeader("connection", "close");
		ajax5.setRequestHeader("content-type", "application/x-www-form-urlencoded");
		ajax5.onreadystatechange = function() {
		if(ajax5.readyState === 4) {
			if(ajax5.status == 200) {
				if (ajax5.responseText == 1) cartelle_profilo(ref);
				else alert("Si è verificato un errore");
			} else alert("Si e' verificato un errore. Codice errore: " + ajax2.readyState);
		}
	}
 	ajax5.send(what);
 	}	
}
function bozze_profilo() {
   var autore = document.getElementById("autore").value;
   var ajax3 = assegnaXMLHttpRequest();
   var what = "autore=" + autore;
    if(ajax3) {
		ajax3.open("post", "/contenuti/ajax/profilo/bozze.php", true);
		ajax3.setRequestHeader("content-type", "application/x-www-form-urlencoded");
		ajax3.setRequestHeader("connection", "close");
		ajax3.onreadystatechange = function() {
		if(ajax3.readyState === 4) {
			if(ajax3.status == 200) {
				document.getElementById("bozze").innerHTML = ajax3.responseText
				 $(function() {$("#boz_tb tbody tr").quicksearch({ position: 'before', attached: 'table#boz_tb', stripeRowClass: ['odd', 'even'], labelText: 'Rircerca rapida:' });});
				 
			} else alert("Si e' verificato un errore. Codice errore: " + ajax3.readyState);
		}
	}
	ajax3.send(what);
 	}
}
function modifica_profilo() {
   var autore = document.getElementById("autore").value;
   var ajax4 = assegnaXMLHttpRequest();
   var what = "autore=" + autore;
    if(ajax4) {
		ajax4.open("post", "/contenuti/ajax/profilo/modifica.php", true);
		ajax4.setRequestHeader("content-type", "application/x-www-form-urlencoded");
		ajax4.setRequestHeader("connection", "close");
		ajax4.onreadystatechange = function() {
		if(ajax4.readyState === 4) {
			if(ajax4.status == 200) {
				document.getElementById("modifica").innerHTML = ajax4.responseText
 			} else alert("Si e' verificato un errore. Codice errore: " + ajax4.readyState);
		}
	}
	ajax4.send(what);
 	}
}
function contenuto_cartella(ref) {
	tb_show('Aggiungi contenuto a cartella', '/contenuti/ajax/profilo/dialog/cartella_contenuto.php?width=500&height=400&ref=' + ref);
	$(function() {$("#tb_bozze").tablesorter({debug: false, widgets: ["zebra"]});});
	$(function() {$("#tb_bozze tbody tr").quicksearch({ position: 'before', attached: 'table#tb_bozze', stripeRowClass: ['odd', 'even'], labelText: 'Rircerca rapida:' });});
}

//INSERISCI TESTO
function testo_inserisci(bozza, modifica, codice) {
   var autore = document.getElementById("autore").value;
   var ajax0 = assegnaXMLHttpRequest();
   var what = "autore=" + autore;
   
   
   if ((bozza != "false") && (bozza)) {
		what = what + "&bozza=" + bozza + "&codice=" + codice;   	
		setInterval(function() {$('#tabs').tabs( 'option', 'disabled', [1, 2] ) }, 100);			
   } else if ((modifica != "false") && (modifica)) {
		what = what + "&modifica=" + modifica + "&codice=" + codice;   
		setInterval(function() {$('#tabs').tabs( 'option', 'disabled', [1, 2] ) }, 100);		
   } 
 
       if(ajax0) {
		ajax0.open("post", "/contenuti/ajax/inserisci/testo.php", true);
		ajax0.setRequestHeader("content-type", "application/x-www-form-urlencoded");
		ajax0.setRequestHeader("connection", "close");
		ajax0.onreadystatechange = function() {
		if(ajax0.readyState === 4) {
			if(ajax0.status == 200) {
				document.getElementById("testo").innerHTML = ajax0.responseText	
				//inizializzazione editor
				CKEDITOR.replace("testo_editor",
					{
						filebrowserImageBrowseUrl : "/ckfinder/ckfinder.html?Type=Images",
						filebrowserImageUploadUrl : "/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Images",
						uiColor : "#DCE4DE",
						width: "610",
						resize_maxWidth : "610",
						resize_minWidth: "610",
						toolbar : [["Source", "Cut","Copy","Paste","PasteText","PasteFromWord","-","Print", "Scayt"],["Undo","Redo","-","Find","Replace","-","SelectAll","RemoveFormat"],["Image","Table","HorizontalRule","SpecialChar","PageBreak"], "/",["Subscript","Superscript"],["NumberedList","BulletedList","-","Outdent","Indent","Blockquote"],["JustifyLeft","JustifyCenter","JustifyRight","JustifyBlock"],["Link","Unlink"],"/",["Bold","Italic","Underline","Strike"],["Font","FontSize"],["TextColor","BGColor"],["Maximize"]]
					});
					var time_salvataggio = 30000;
					setInterval(function() { autosalvataggio(false) }, time_salvataggio);			 
			} else alert("Si e' verificato un errore. Codice errore: " + ajax0.status);
		}
	}
	ajax0.send(what);
 	}
}

//GESTIONE BOZZE
function autosalvataggio(chiudi) {
   document.getElementById("autosave_failed").style.display = "none";
   $("#autosave_load").fadeIn();
   
   var testo = encodeURIComponent(CKEDITOR.instances.testo_editor.getData());
   var titolo = encodeURIComponent(document.getElementById("testo_titolo").value);
      
   var what = document.getElementById("testo_what").value;
   var codice = document.getElementById("testo_codice").value;
   var w = "testo=" + testo + "&titolo=" + titolo + "&codice=" + codice + "&what=" + what;
  
	var ajax = assegnaXMLHttpRequest();
   if(ajax) {
		ajax.open("post", "/contenuti/ajax/inserisci/autosalvataggio.php", true);
		ajax.setRequestHeader("content-type", "application/x-www-form-urlencoded");
		ajax.setRequestHeader("connection", "close");
		ajax.onreadystatechange = function() {
		if(ajax.readyState === 4) {
			if(ajax.status == 200) {
    				if (ajax.responseText == 1) {
					if (chiudi == true) {
						if (form_inserisci(document.modulo)) { document.modulo.submit();}
						
					} else { setTimeout(function() { $("#autosave_load").fadeOut() }, 2000); }
				} else {
					alert('Error code: ' + ajax.responseText);
					document.getElementById("autosave_load").style.display = "none";
					document.getElementById("autosave_failed").style.display = "block";
				}
			} else {
				alert('Ajax Response: ' + ajax.status);
 				document.getElementById("autosave_load").style.display = "none";
				document.getElementById("autosave_failed").style.display = "block";
			}
		}
	}
	ajax.send(w);
   }	
}
function no_pubbliazione() {
	document.modulo.testo_pubblica.value = "false"; 
}

//INSERISCI GALLERIA
function galleria_inserisci(bozza, modifica, codice) {
   var autore = document.getElementById("autore").value;
   
   	var what = "autore=" + autore;   
    if ((bozza != "false") && (bozza)) {
		setInterval(function() {$('#tabs').tabs( 'option', 'disabled', [1, 1] ) }, 100);			 
		what = what + "&bozza=" + bozza + "&codice=" + codice;  
		src_open =   "/contenuti/ajax/inserisci/galleria/commenta.php"
   } else if ((modifica != "false") && (modifica)) {
		setInterval(function() {$('#tabs').tabs( 'option', 'disabled', [1, 1] ) }, 100);			 
		what = what + "&modifica=" + modifica + "&codice=" + codice;   
		src_open =   "/contenuti/ajax/inserisci/galleria/commenta.php"
   } else {
	src_open =   "/contenuti/ajax/inserisci/galleria/page_1.php"
   }

   var ajax1 = assegnaXMLHttpRequest();
   if(ajax1) {
		ajax1.open("post", src_open, true);
		ajax1.setRequestHeader("content-type", "application/x-www-form-urlencoded");
		ajax1.setRequestHeader("connection", "close");
		ajax1.onreadystatechange = function() {
		if(ajax1.readyState === 4) {
			if(ajax1.status == 200) {
				document.getElementById("galleria").innerHTML = ajax1.responseText
 				if (((bozza != "false") && (bozza)) || ((modifica != "false") && (modifica))) {
 					$('#slider').easySlider({ numeric: true  })					
				}
 			} else alert("Si e' verificato un errore. Codice errore: " + ajax1.readyState);
		}
	}
	ajax1.send(what);
 	}
}
function inserisci_galleria_add() {
	var k = parseFloat(document.getElementById("n_img").value);
	k = k+1;
	document.getElementById("n_img").value = k;
	var tbody = document.getElementById("tbl_immagini").getElementsByTagName("TBODY")[0];
	 var row = document.createElement("TR")
	var td1 = document.createElement("TD")
	td1.appendChild(document.createTextNode("Immagine " + k + ":"))
	td1.style.fontWeight = "bold"
	td1.align = "right"
	var td2 = document.createElement("TD")
	td2.appendChild (document.createTextNode(""))
	row.appendChild(td1);
	row.appendChild(td2);
	tbody.appendChild(row);
	var sel = document.createElement('input');
	sel.type = "file";
	sel.name = "pictures[]";
	td2.appendChild(sel);
}
function startUpload(){
 	if (form_galleria()) {
		tb_show('Caricamento immagini...', '#TB_inline?height=60&width=400&inlineId=upload_img_msg&modal=true')	
		return true;
	}
}
function carica_immagini() {
	if (form_galleria()) {
	   var titolo = document.getElementById("galleria_titolo").value;
	   var descrizione = document.getElementById("galleria_descrizione").value;
	   
	   var what = "titolo=" + titolo  + "&descrizione=" + descrizione;
	   var ajax = assegnaXMLHttpRequest();
	   if(ajax) {
			ajax.open("post", "/contenuti/ajax/inserisci/galleria/page_2.php", true);
			ajax.setRequestHeader("content-type", "application/x-www-form-urlencoded");
			ajax.setRequestHeader("connection", "close");
			ajax.onreadystatechange = function() {
			if(ajax.readyState === 4) {
				if(ajax.status == 200) {
					document.getElementById("galleria").innerHTML = ajax.responseText
				} else alert("Si e' verificato un errore. Codice errore: " + ajax.readyState);
			}
		}
		ajax.send(what);
	  }	
	}
}  
function stopUpload(result, codice){
 	document.getElementById("page_2").style.display = "block";
 	document.getElementById("page_1").style.display = "none";
	set_iframe_height('upload_target', 30);
	tb_remove();
} 
function set_iframe_height(id, height) {
	var myiframe=document.getElementById(id)
	if (myiframe.contentDocument)  {
		contenthg = myiframe.contentDocument.body.offsetHeight;
		myiframe.height = contenthg + height; //aggiungo 50 pixel
 	}
	else if (myiframe.Document)  {
		contenthg = myiframe.Document.body.offsetHeight;
		myiframe.height = contenthg + height; //aggiungo 50 pixel
	} 
}
function upload_image_step3() { //commenti e titoli foto
   var codice = document.getElementById("codice").value;
   if (codice) {   
	   var what = "codice="+codice;
	   var ajax = assegnaXMLHttpRequest();
	   if(ajax) {
			ajax.open("post", "/contenuti/ajax/nuova_galleria/page_2.php", true);
			ajax.setRequestHeader("content-type", "application/x-www-form-urlencoded");
			ajax.setRequestHeader("connection", "close");
			ajax.onreadystatechange = function() {
			if(ajax.readyState === 4) {
				if(ajax.status == 200) {
					document.getElementById("box_galleria").innerHTML = ajax.responseText
				} else alert("Si e' verificato un errore. Codice errore: " + ajax.readyState);
			}
		}
		ajax.send(what);
	  }	
   } else alert("Impossibile continuare. Si è verificato un errore");
}

//ELIMINA CONTENUTI
function elimina_opera(codice, what) {
	var answer = confirm("Sicuro di voler eliminare l'opera selezionata?")
	if (answer){
 		window.location = "/contenuti/post/elimina/" + what + ".php?codice=" + codice;
	}
} 
function elimina_commento(codice, opera) {
	var answer = confirm("Eliminerai anche tutte le risposte al tuo commento. Continuare?")
	if (answer){
 		window.location = "/contenuti/post/elimina/commento.php?codice=" + codice + "&opera=" + opera;
	}
} 

//COMMENTI
function inserisci_commento(codice) { tb_show('Inserisci nuovo commento', '/contenuti/ajax/commenti/inserisci.php?width=500&height=360&codice=' + codice) }
function  risposta_commento(codice, risposta) { tb_show('Inserisci nuovo commento', '/contenuti/ajax/commenti/risposta.php?width=500&height=370&codice=' + codice + '&risposta=' + risposta) }


