 // JavaScript Document


//AJAX
function carica_window(what) {
 var ajax = assegnaXMLHttpRequest();
   if(ajax) {
	ajax.open("post", "/content/ajax/popup.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("dialog_content").innerHTML = ajax.responseText;
		//	reset_pagesize() ;
		} else
			alert("Operazione fallita, errore numero " + ajax.status);
			//close_dialog();
		}
	}
	ajax.send(what);
  }
}

function close_dialog() {
	document.getElementById("dialog_body").style.display = "none";
	
	 if (navigator.appVersion.indexOf("MSIE") != -1) {
		if (document.getElementById("TextArea_2")) var i = 8;
		else if (document.getElementById("TextArea_1")) var i = 1;
		if (i) {	
			for (i=1; i<=8; i++) {
 				if (document.getElementById("box" + i)) document.getElementById("box" + i).style.display = "block";
			}
		}
	 }
	
	
}
function open_dialog(what, title) {
 	document.getElementById('dialog_body').style.height = ('300px');
	var arrayPageSize = getPageSize();
	var height = arrayPageSize[1] + 50;
 	document.getElementById('dialog_body').style.height = (height + 'px');
 
 	document.getElementById('dialog_window').style.top = ('100px');
 	
	
	 
	 if (navigator.appVersion.indexOf("MSIE") == -1) {
 	 	document.getElementById("dialog_window").style.position = "fixed";
 	 } else {
 	 	//document.getElementById("dialog_body").style.background = "url(/blank.gif)";
		
 		if (document.getElementById("TextArea_2")) var i = 8;
		else if (document.getElementById("TextArea_1")) var i = 1;
		if (i) {	
			for (i=1; i<=8; i++) {
 				if (document.getElementById("box" + i)) document.getElementById("box" + i).style.display = "none";
			}
 		 }
	 }
	 
	document.getElementById("dialog_body").style.display = "block";
	document.getElementById("dialog_titletext").innerHTML = title;
	document.getElementById("dialog_content").innerHTML = '<p align="center"><img src="/style/img_grafica/loading.gif"></p>';
	carica_window(what);
 	
}



function reset_pagesize() {
 	document.getElementById('dialog_body').style.height = ('300px');
	var arrayPageSize = getPageSize();
	var height = arrayPageSize[1] + 100;
 	document.getElementById('dialog_body').style.height = (height + 'px');
 	
		
}



//VISUALIZZA ESEMPI

function vedi_esempio(what, title) {
	window.scroll(0, 0);
	document.getElementById('dialog_window').style.width = "550px";
 	document.getElementById('dialog_window').style.marginLeft = "-270px";	
	open_dialog(what, title);
	document.getElementById('dialog_window').style.position = "absolute";
 	document.getElementById('dialog_window').style.top = "10%";
}


//inserisci link --> modifica pagina

//CAMBIO STATO PER IMMAGINI e LINK
function cambia_stato_r1(type) { 
	if (document.getElementById("align_sx")) { if (document.getElementById("align_sx").checked) var align = "sx"; else var align = "dx"; }
	if (type == "image")  {
		var testo_titolo = "Inserisci immagine";
		var agg_lang = "";
	} else {
		var testo_titolo = "Inserisci collegamento";
		var agg_lang = "&lang=" + document.getElementById('lang_box').value;
	}
  	tb_show(testo_titolo, "/content/ajax/popup.php?width=430&autosize=true&type=" + type + "&editn=" + document.getElementById("editn").value + "&ramo1=" + document.getElementById("ramo1").value + "&align=" + align + agg_lang);   
}
function cambia_stato_r2(type) { 
	if (document.getElementById("align_sx")) { if (document.getElementById("align_sx").checked) var align = "sx"; else var align = "dx"; }
	if (type == "image")  {
		var testo_titolo = "Inserisci immagine";
		var agg_lang = "";
	} else {
		var testo_titolo = "Inserisci collegamento";
		var agg_lang = "&lang=" + document.getElementById('lang_box').value;
	}
	
 	tb_show(testo_titolo, "/content/ajax/popup.php?width=430&autosize=true&type=" + type + "&editn=" + document.getElementById("editn").value + "&ramo1=" + document.getElementById("ramo1").value + "&ramo2=" + document.getElementById("ramo2").value + "&align=" + align + agg_lang);  
}
function cambia_stato_r3(type) { 
	if (document.getElementById("align_sx")) { if (document.getElementById("align_sx").checked) var align = "sx"; else var align = "dx"; }	
	if (type == "image")  {
		var testo_titolo = "Inserisci immagine";
		var agg_lang = "";
	} else {
		var testo_titolo = "Inserisci collegamento";
		var agg_lang = "&lang=" + document.getElementById('lang_box').value;
	}
 	tb_show(testo_titolo, "/content/ajax/popup.php?width=430&autosize=true&type=" + type + "&editn=" + document.getElementById("editn").value + "&ramo1=" + document.getElementById("ramo1").value + "&ramo2=" + document.getElementById("ramo2").value + "&ramo3=" + document.getElementById("ramo3").value + "&align=" + align + agg_lang); 
}

//CAMBIO STATO PER INSERIMENTO SEZIONE PER NUOVA PAGINA
function cambia_stato_n(type) {
	tb_show("Seleziona sezione", "/content/ajax/popup.php?width=430&autosize=true&type=" + type + "&ramo1=" + document.getElementById("ramo1").value);   
}


function cambia_upload() {
	if (document.getElementById("dim_1").checked) var dimensioni = "normale";
	else if (document.getElementById("dim_2").checked) var dimensioni = "grande";
	else var dimensioni = "piccola";
	tb_show("Carica immagine", "/content/ajax/popup.php?width=430&autosize=true&type=upload&editn=" + document.getElementById("editn").value + "&ramo1=" + document.getElementById("ramo1").value + "&dim=" + dimensioni + "&img_name=" + document.getElementById("img_name").value); 
}

function check_name() {
	if 	(!document.getElementById("img_name").value) { alert("Scegli un nome breve per l'immagine"); document.getElementById("img_name").focus; }
	else { submit_upload(); }
} 




function check_n() { //abilita-disabilita bottone "Salva" --> sezione nuova pagina
	if (document.getElementById("ramo1").value == "italy") {
		document.getElementById("insert_button").disabled = false;
	}
	else {
		if (document.getElementById("ramo2")) document.getElementById("insert_button").disabled = false;
		else document.getElementById("insert_button").disabled = true;
	}
	
}

function check_b(what) { //disabilita bottone "Inserisci" quando non è selezionato il link
 
	if ((document.getElementById("ramo1").value == "italy") || (document.getElementById("ramo1").value == "main_links")) {
		if (document.getElementById("ramo2")) {
			if (document.getElementById("ramo2").value == 'articoli') {
				if (document.getElementById("ramo3")) {
 					if (document.getElementById("ramo3").style.display == "none") cambia_stato_r2(what);
 					else if (document.getElementById("ramo3").value) document.getElementById("insert_button").disabled = false;
					else document.getElementById("insert_button").disabled = true;
				} else cambia_stato_r2(what);
			} else {
				if (document.getElementById("ramo3")) {
					document.getElementById("ramo3").style.display = "none";
					document.getElementById("articoli_label").style.display = "none";
				}
				if (document.getElementById("ramo2").value) { document.getElementById("insert_button").disabled = false; }
				else { document.getElementById("insert_button").disabled = true; }
			}
		} else { document.getElementById("insert_button").disabled = true; }
	} else {
		
		
		if (document.getElementById("ramo2") && document.getElementById("ramo3")) {
			
			if (document.getElementById("ramo3").value == "articoli") {
				if (document.getElementById("ramo4")) {
					if (document.getElementById("ramo4").style.display == "none") cambia_stato_r3(what);
					else if (document.getElementById("ramo4").value) { document.getElementById("insert_button").disabled = false; }
					else { document.getElementById("insert_button").disabled = true; }
					
				} else {
					cambia_stato_r3(what);
				}
			} else {
				if (document.getElementById("ramo4")) {
					document.getElementById("ramo4").style.display = "none";
					document.getElementById("articoli_label").style.display = "none";
				}
				if (document.getElementById("ramo3").value) { document.getElementById("insert_button").disabled = false; }
				else { document.getElementById("insert_button").disabled = true; }
			}
		} else { document.getElementById("insert_button").disabled = true; }
	}
	
	
}

function check_i() { //disabilita l'input file quando non è selezionata la sezione
	 if (document.getElementById("ramo1").value == "italy") document.getElementById("inputfile").disabled = false;
	 else {
		if (document.getElementById("ramo2")) {
			if (document.getElementById("ramo2").value) { document.getElementById("inputfile").disabled = false; }
		}
	 }
}

function check_c() { //abilita il bottone "Carica" quando viene selezionato un file
	var input_value = document.getElementById("inputfile").value;
	
	if (input_value) {
		if (check_ext(input_value)) {
			document.getElementById("carica_b").disabled = false;
		}
		else document.getElementById("inputfile").value = "";
	}
}



function submit_link(what) {//amministrazione menu???!?!?!
 
 	var oField=document.createElement("input"); 
	oField.setAttribute("value","true");
	oField.setAttribute("name",what);
	oField.setAttribute("type","hidden");
	document.getElementById("admin_form").appendChild(oField);
	
	document.admin_form.submit();
}
 
function dimensioni_box(what) {
		alert("ciao");
	if (what == "normale") {
		document.getElementById('dialog_window').style.width = "430px";
		document.getElementById('dialog_window').style.marginLeft = "-215px";
	} else if (what == "allargato") {
		document.getElementById('dialog_window').style.width = "550px";
		document.getElementById('dialog_window').style.marginLeft = "-270px";
	}
}

/*
function c_percorso(ramo1, ramo2, ramo3) {
	if (ramo1) { 
		if (ramo2) {  percorso = ramo1 + " > " + ramo2 + " > " + document.getElementById("ramo3").value; }
		else { percorso = ramo1 + " > " + document.getElementById("ramo2").value; }
	}
	document.getElementById("dialog_percorso").innerHTML = percorso;
	
}
*/



//pagesize

function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

