
var submitted = false;
var ricarica = false;

function preLoadImages(urls) {
	var img = new Array();
	for (i=0; i<urls.length; i++) {
		img[img.length] = new Image();
		img[img.length-1].src = urls[i];
	}
}

function attiva(gallery, n, tot, color) {
	mask = /(^.*)([0-9]{1,3}$)/;
	path = document.images[gallery].src.replace(mask, "$1");
	oldN = document.images[gallery].src.replace(mask, "$2");
	document.images[gallery].src = path+n;

	cursorDefault(gallery, n);

	document.images[gallery+oldN].parentNode.style.backgroundColor = "transparent";
	document.images[gallery+oldN].onclick = (function(gallery, n, tot, color){return function(){attiva(gallery, n, tot, color)}})(gallery, oldN, tot, color);
	document.images[gallery+oldN].onmouseover = (function(gallery, n){return function(){cursorActive(gallery, n)}})(gallery, oldN);
	document.images[gallery+oldN].onmouseout = (function(gallery, n){return function(){cursorDefault(gallery, n)}})(gallery, oldN);

	document.images[gallery+n].parentNode.style.backgroundColor = color;
	document.images[gallery+n].onclick = null;
	document.images[gallery+n].onmouseover = function() {cursorDefault(gallery, n)};
	document.images[gallery+n].onmouseout = function() {cursorDefault(gallery, n)};

	if (tot == 1) {next=1; prec=1;}
	else if (n==tot) {next=1; prec=n-1;}
	else if (n==1) {next=parseInt(n)+1; prec=tot;}
	else {next=parseInt(n)+1; prec=n-1;}
	document.images[gallery].onclick = (function(gallery, n, tot, color){return function(){attiva(gallery, n, tot, color)}})(gallery, next, tot, color);
	document.getElementById(gallery+"Next").getElementsByTagName("img")[0].onclick = (function(gallery, n, tot, color){return function(){attiva(gallery, n, tot, color)}})(gallery, next, tot, color);
	document.getElementById(gallery+"Prec").getElementsByTagName("img")[0].onclick = (function(gallery, n, tot, color){return function(){attiva(gallery, n, tot, color)}})(gallery, prec, tot, color);

}
function cursorActive(gallery, n) {
	//if (window.event) {document.images[gallery+n].style.cursor="hand"}
	//else {document.images[gallery+n].style.cursor="pointer"}
	document.images[gallery+n].style.cursor="pointer";
}
function cursorDefault(gallery, n) {
	//if (window.event) {document.images[gallery+n].style.cursor="default"}
	//else {document.images[gallery+n].style.cursor="default"}
	document.images[gallery+n].style.cursor="default";
}



function addLoading() {
	go = false;
//alert(document.getElementById("inpFile1"));
	//if (document.getElementById("inpFile1") == null) {return false;}
	//document.getElementById("go").setAttribute('type', 'hidden');
	//document.getElementById("go").onclick = null;
	for (var i=1; i<=8; i++) {
		if (document.getElementById("inpFile"+i).value != "") {go = true; break;}
	}
	if (document.getElementById("loading") || !go) {return false;}
	var sub = document.getElementById("insSub");
	document.getElementById("back").setAttribute('href', 'javascript:void(0)');
	var div = document.createElement("div");
	div.setAttribute('id', 'loading');
	//p = document.createElement("p");
	var p = document.createTextNode("Attendi ... ");
	var p2 = document.createTextNode("Caricamento in corso!");
	var br1 = document.createElement("br");
	var p3 = document.createTextNode("Il tempo inpiegato dipende dalla dimensione delle immagini e dalla velocità di connessione.");
	var br2 = document.createElement("br");
	var img1 = document.createElement("img");
	img1.setAttribute("src","/IMAGES/waitOn.png");
	img1.setAttribute("class","on");
	img1.setAttribute("id","img1");
	var img2 = document.createElement("img");
	img2.setAttribute("src","/IMAGES/waitOff.png");
	img2.setAttribute("class","off");
	img2.setAttribute("id","img2");
	var img3 = document.createElement("img");
	img3.setAttribute("src","/IMAGES/waitOff.png");
	img3.setAttribute("class","off");
	img3.setAttribute("id","img3");
	div.appendChild(p);
	div.appendChild(img1);
	div.appendChild(img2);
	div.appendChild(img3);
	div.appendChild(br1);
	div.appendChild(p2);
	div.appendChild(br2);
	div.appendChild(p3);
	sub.appendChild(div);

	document.getElementById("footer").style.bottom = "0px";

	load = window.setInterval("loading()", 300);
}
function loading() {
	for (var i=1; i<4; i++) {
		if (document.getElementById("img"+i).getAttribute("class") == "on") {
			document.getElementById("img"+i).src = "/IMAGES/waitOff.png";
			document.getElementById("img"+i).setAttribute("class", "off");
			if (i==3) {document.getElementById("img1").src = "/IMAGES/waitOn.png"; document.getElementById("img1").setAttribute("class", "on"); return;}
			else {document.getElementById("img"+(i+1)).src = "/IMAGES/waitOn.png"; document.getElementById("img"+(i+1)).setAttribute("class", "on"); return;}
		}
	}
}
function checkForm(action, method) { //per funzionare form deve avere name=modulo e submit id=go
	if (submitted) {return false;}
	else {
		submitted = true;
		form = document.modulo;
		sub = document.getElementById('go');
		sub.disabled = true;
		form.method = method;
		form.action = action;
		subH = document.createElement('input');
		subH.setAttribute('type', 'hidden');
		subH.name = sub.name;
		subH.value = sub.value;
		form.appendChild(subH);
		form.submit();

		return true;
	}
}

function ricaricaIframe(obj) {
	if (ricarica) {return false;}
	else {
		ricarica = true;
		obj.style.height = obj.contentWindow.document.lastChild.lastChild.lastChild.style.height;
		obj.style.width = obj.contentWindow.document.lastChild.lastChild.lastChild.style.width;
		var foot = document.getElementById('footer');
		var page = document.getElementById('page');
		var padre = obj.parentNode;
		page.removeChild(foot);
		//padre.removeChild(obj);
		//padre.appendChild(obj);
		page.appendChild(foot);
		foot.style.bottom = '0px';
		ricarica = false;
	}
}

