// Basic Script for WEB pages
function MM_swapImgRestore() { //v3.0
var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
function MM_findObj(n, d) { //v4.01
var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
var i,j=0,x,a=MM_swapImage.arguments; 
document.MM_sr=new Array; 
for(i=0;i<(a.length-2);i+=3)
	if ((x=MM_findObj(a[i]))!=null)
	{
		document.MM_sr[j++]=x; 
		if(!x.oSrc) 
			x.oSrc=x.src; 
		x.src=a[i+2];
	}
}

function exploreChildNodes(e, nId)
{
	var i;
	if(!e) return null;
	
	for(i=0;i<e.childNodes.length;i++){
	if(e.childNodes[i].nodeName!=""){
	if(e.childNodes[i].id==nId){return e.childNodes[i];}
	else{ob= exploreChildNodes(e.childNodes[i], nId); if(ob) return ob;}}}
	return null;
}


function findFieldById(nm)
{
	return exploreChildNodes(document.getElementById("inputBody"), nm);
}

function validate_required(frm_field,alerttxt) {
if (frm_field.value==null||frm_field.value=="") {
alert(alerttxt);
return false}
else {
return true}
}

function validate_required2(frm_field,alerttxt) {
if (frm_field.value==null||frm_field.value==""||frm_field.value==0) {
alert(alerttxt);
return false}
else {
return true}
}

var lastOpened1 = null;
var lastOpened2 = null;
var closeMenu = null;

function menuClose(){
if(lastOpened1) lastOpened1.style.visibility="hidden";
lastOpened1=null;
if(lastOpened2) lastOpened2.style.visibility="hidden";
lastOpened2=null;
}

function mcloseT() {
closeMenu = window.setTimeout(menuClose, 150);
}

function calcOffset(ptr, top)
{
	o = 0;
	while(p=ptr.offsetParent)
	{
		o += top ? p.offsetTop : p.offsetLeft;
		ptr=p;
	}
	return o;
}

function openMenu(e){
p=e.parentNode.parentNode;
if(p.id=="menuTop") level=1;
else{
p=p.parentNode.parentNode;
if(p.id=="menuTop") level=2;
else{
p=p.parentNode.parentNode;
if(p.id=="menuTop") level=3;}}
if(level==1 && lastOpened1){
lastOpened1.style.visibility="hidden";
lastOpened1=null;
if(lastOpened2){
lastOpened2.style.visibility="hidden";
lastOpened2=null;
}}
if(closeMenu){
window.clearTimeout(closeMenu)
closeMenu=null;
}
if(level==2 && lastOpened2){
lastOpened2.style.visibility="hidden";
lastOpened2=null;
}
for(i=0; i<e.parentNode.childNodes.length; i++){
if(e.parentNode.childNodes[i].nodeName=="UL"){
if(level==1){
lastOpened1 = e.parentNode.childNodes[i];
mx=(e.offsetLeft+calcOffset(e, false))+"px";
my=(e.offsetTop+e.offsetHeight+calcOffset(e, true))+"px";
//alert(e.offsetLeft+'...'+e.offsetTop+'...'+e.offsetHeight);
//alert(e.offsetLeft+','+calcOffset(e, false)+'...'+e.offsetTop+','+calcOffset(e, true));
lastOpened1.style.left=(e.offsetLeft ? e.offsetLeft+calcOffset(e, false) : 0)+"px";
lastOpened1.style.top=((e.offsetTop ? e.offsetTop+calcOffset(e, true) : 0)+e.offsetHeight)+"px";

//alert(lastOpened1.offsetLeft+'...'+lastOpened1.offsetTop);

lastOpened1.style.visibility="visible";
}
else{
lastOpened2 = e.parentNode.childNodes[i];
lastOpened2.style.marginLeft=e.parentNode.offsetWidth+"px";
//lastOpened2.style.marginTop=-e.parentNode.offsetHeight+"px";
lastOpened2.style.visibility="visible";
}
}
}
}

var opened=null;
var menuSel=null;
var openedli=null;

function findMenuUl(e)
{
for(i=0;i<e.childNodes.length;i++){
if(e.childNodes[i].nodeName=="UL")
	return e.childNodes[i];
}
return null;
}

function hMenu(e)
{
var same=false;
m=findMenuUl(e.parentNode);
if(m){
if(opened){
if(opened==m) same=true;
//opened.style.visibility="hidden";
//opened.style.position="absolute";
opened.style.display="none";
if(openedli) openedli.style.height="34px";
findMenuImg(findMenuTable(opened.parentNode)).src="Imgs/menuClosed.gif";
opened=null;
openedli=null;
}
if(!same)
{
opened=m;
openedli=e.parentNode;
//opened.style.visibility="visible";
//opened.style.position="relative";
opened.style.display="block";
openedli.style.height="auto";
findMenuImg(findMenuTable(e.parentNode)).src="Imgs/menuOpened.gif";
}
}
}

var skipValidate=false;

function openCurrent()
{
if(checkNavigator()) document.getElementById("navtop").style.position="fixed";
skipValidate=false;
var mopen=document.getElementById("currentAdmin");
if(mopen){
opened=mopen.parentNode.parentNode;
//opened.style.position="relative";
//opened.style.visibility="visible";
opened.style.display="block";
openedli=opened.parentNode;
openedli.style.height="auto";
mt=findMenuTable(opened.parentNode);
if(mt) findMenuImg(mt).src="Imgs/menuOpened.gif";
}
}

function findMenuTable(m)
{
if(!m || !m.childNodes) return null;
for(i=0;i<m.childNodes.length;i++)
	if(m.childNodes[i].nodeName=="TABLE")
		return m.childNodes[i];
return null;
}

function findMenuImg(n)
{
for(k=0;k<n.childNodes.length;k++){
if(n.childNodes[k].nodeName=="TBODY"){
o=n.childNodes[k];
for(j=0;j<o.childNodes.length;j++){
if(o.childNodes[j].nodeName=="TR"){
p=o.childNodes[j];
for(w=0;w<p.childNodes.length;w++){
if(p.childNodes[w].nodeName=="TD"){
q=p.childNodes[w];
for(z=0;z<q.childNodes.length;z++){
if(q.childNodes[z].nodeName=="IMG"){
return q.childNodes[z];
}}}}}}}}
}

function selectMenu(m)
{
menuSel=findMenuImg(m)
if(opened && m==findMenuTable(opened.parentNode))
menuSel.src="Imgs/menuOpened0.gif"
else
menuSel.src="Imgs/menuClosed0.gif"
}

function outMenu(m)
{
menuSel=findMenuImg(m)
if(opened && opened.parentNode && m==findMenuTable(opened.parentNode))
menuSel.src="Imgs/menuOpened.gif"
else
menuSel.src="Imgs/menuClosed.gif"
}

function fnCancel(msg, loc){
if((msg!="") && !confirm(msg)) return false;
location.href=loc;
return true;
}

function MG_go(f, bs_val){
document.getElementById("extnd").name = "TB_ACTION";
document.getElementById("extnd").value = bs_val;
document.getElementById("saveBtn").disabled=true;
f.submit();
}

function pnClick(prev, f, valid){
skipValidate=prev;
if(valid && !valid(f)) {return false;}
document.getElementById("extnd").name = "TB_ACTION";
document.getElementById("extnd").value = prev ? "TB_PREV" : "TB_NEXT";
document.getElementById("saveBtn").disabled=true;
f.submit();
}

function getScroll() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

function getSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return[myWidth, myHeight];
}

function checkNavigator() {
if(navigator.appName=="Microsoft Internet Explorer"){
var start = navigator.appVersion.search("MSIE")
start = navigator.appVersion.indexOf(" ", start) + 1
var stop = navigator.appVersion.indexOf(".", start)
var version = navigator.appVersion.substring(start, stop)
if(version < 7) {return false;}
}
return true;
}

function resizeImgX(g, xMax)
{
x=g.offsetWidth;
y=g.offsetHeight;
if(x>xMax){
g.style.width=xMax+"px";
g.style.height=(xMax*y/x)+"px";
}
}

function styleUpdate()
{
f=document.getElementById("style_update");
if(f.style.visibility=="hidden") f.style.visibility="visible";
else f.style.visibility="hidden";
}

function openNewBrowser(addr, msg){
if(msg && !confirm(msg)) return false;
window.open(addr,'_blank','toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,copyhistory=yes,resizable=yes,toolbar=yes');
return false;
}

/*(C)Andrea Giammarchi*/(function(a,n,y,I,E,A,c,t,i,v,e,X){var l,O,G="getAttribute",P="parentNode",T="type",p=function(i,s,a){while(i<a[I])s+=e[t](a[i++].nodeName)?a[i-1][A]:"";return s};if(!window.opera&&/msie (6|7)/i[t](navigator.userAgent)){i.write("<"+c+" defer src=//:></"+c+">");c=i[y](c);c[c[I]-1].onreadystatechange=function(){if((c=this).readyState=="complete"){for(l=i[y](E);n<l[I];n++)if(v[t]((O=l[n])[G](T)))a(O);c[P].removeChild(c)}};window.attachEvent("onload",function(){for(l=i[y](E),n=0;n<l[I];n++)if(v[t]((O=l[n])[G](T))){a(O,1);O[A]=O[A].split(">")[0]+">"+p(0,"",O.childNodes)+O.innerHTML.replace(X,"")+"</"+E+">"}})}else if(i[a="addEventListener"])i[a]("DOMContentLoaded",function(){for(l=i[y](E);n<l[I];n++)if(v[t]((O=l[n])[G](T)))O[P].replaceChild(O.cloneNode(true),O);},false);})(function(O,v){O.style.visibility=v?"visible":"hidden"},0,"getElementsByTagName","length","object","outerHTML","script","test",document,/^application\/x-shockwave-flash$/i,/^param$/i,/^\s+|\s+$/g);

function sendSearchVal(form){
	msgSend("SRC="+document.getElementById("searchVal").value, xmlSearchWait, "search.php");
	return false;}
	
function xmlSearchWait(){
	if(xmlHttp.readyState==4){
		if(xmlHttp.status==200){
			try{handleSearchResponse();}
			catch(e){alert("Errore di lettura risposta dal server "+ e.toString());}}
		else{alert("Problema di accesso al server\nError accessing server\n"+xmlHttp.statusText);}}
	}
	
function doSearch(elem, page)
{
var srcStr=document.getElementById(elem);
if(srcStr && srcStr.nodeName=="INPUT") location.href=page+"?SRC="+encodeURIComponent(srcStr.value);
}
	
var mg_validXML = "No valid XML: ";
	
function handleSearchResponse(){
	xmlResponse = xmlHttp.responseXML;
	if(!xmlResponse || !xmlResponse.documentElement)
		throw(mg_validXML + xmlHttp.responseText);
	xmlDocumentElement = xmlResponse.documentElement;
	if(xmlDocumentElement.nodeName=="parseerror")
		throw(mg_validXML + xmlHttp.responseText);
		
	str = "<h1>Risultati ricerca<\/h1>";
	titles = xmlDocumentElement.getElementsByTagName("title");
	descs = xmlDocumentElement.getElementsByTagName("description");
	scores = xmlDocumentElement.getElementsByTagName("score");
	links = xmlDocumentElement.getElementsByTagName("link");
	for(var i =0; i<titles.length;i++)
		str += "<h2><a href="+links.item(i).firstChild.data+">"+titles.item(i).firstChild.data+"<\/a><\/h2>"+
			   "<h3>"+descs.item(i).firstChild.data+"<\/h3>"+
			   "<h4>Score = "+scores.item(i).firstChild.data+"<\/h4>";
	document.getElementById("searchResults").innerHTML = str;
}

function checked_selectedItem(e, tb)
{
	var bs_state = e.checked;
	e.checked = bs_state ? false : true;
	msgSend("SRC="+e.id.match(/\d+/)+"&STATE="+(bs_state ? "1" : "0")+"&TABLE="+tb, hndchk_xmlCheckWait, "hndcheck.php");
}

function checked_selectAllItems(e, tb)
{
	var bs_state = e.checked;
	e.checked = bs_state ? false : true;
	msgSend("SRC=ALL&STATE="+(bs_state ? "1" : "0")+"&TABLE="+tb, hndchk_xmlCheckWait, "hndcheck.php");
}

function hndchk_xmlCheckWait()
{
if(xmlHttp.readyState==4){if(xmlHttp.status==200){try{hndchk_handle();} catch(e){alert(ajax_txt_errorMsg2+ e.toString());}}
else{alert(ajax_txt_errorMsg3+xmlHttp.statusText);}}
}

function hndchk_handle()
{
	xmlResponse = xmlHttp.responseXML;
	if(!xmlResponse || !xmlResponse.documentElement) throw(mg_validXML + xmlHttp.responseText);
	xmlDocumentElement = xmlResponse.documentElement;
	if(xmlDocumentElement.nodeName=="parseerror") throw(mg_validXML + xmlHttp.responseText);
	var selected = getXmlVal(xmlDocumentElement.getElementsByTagName("selected"), 0);
	var id = getXmlVal(xmlDocumentElement.getElementsByTagName("id"), 0);
	var bs_state = getXmlVal(xmlDocumentElement.getElementsByTagName("state"), 0);
	document.getElementById("elements_selected").innerHTML = selected;
	if(id=="ALL")
	{
		var ck = document.getElementsByName("chk_sel");
		for(i=0; i<ck.length; i++)
			ck.item(i).checked = (bs_state == "1") ? true : false;
	}
	else document.getElementById("chk_sel_"+id).checked = (bs_state == "1") ? true : false;
}

var tableNameReturn = '';
var tableFileOpened = '';
// Apertura file manager non come dialog
function openFM2_0(){
	var iframe=document.getElementById('fmgr2_0');
	if(iframe) openFMIframe(iframe, 'filemanager2_0.php');
}
// Apertura file manager come dialog per aggiornamento campi tabelle
function openFMD2_0(file, name)
{
	var iframe=document.getElementById('fmgr2_0');
	if(iframe) 
	{
		tableFileOpened = file;
		tableNameReturn = name;
		openFMIframe(iframe, 'filemanager2_0.php?slave&file='+file+'&step=table')
	}
}
// Apertura file manager come dialog per aggiornamento immagini articoli
function openFMA2_0(file)
{
	var iframe=document.getElementById('fmgr2_0');
	if(iframe) 
	{
		tableNameReturn = name;
		openFMIframe(iframe, 'filemanager2_0.php?slave&file='+file+'&step=imgArt')
	}
}
function openFMW2_0(file)
{
	var iframe=document.getElementById('fmgr2_0');
	if(iframe) 
	{
		tableNameReturn = name;
		openFMIframe(iframe, 'filemanager2_0.php?slave&file='+file+'&step=imgDownload')
	}
}
function openFMIframe(iframe, src)
{
	iframe.src=src;
	wSize=getSize();
	iframe.style.width=wSize[0]+'px';
	iframe.style.height=wSize[1]+'px';
	iframe.style.display= 'block';
}

function getFileTable(file, sizeVal, dateVal, width, height)
{
	var fName = document.getElementById('chn_'+tableNameReturn);
	var path = document.getElementById('chp_'+tableNameReturn);
	var flName = document.getElementById('chs_'+tableNameReturn);
	var fInput = document.getElementById(tableNameReturn);
	var image = document.getElementById('chi_'+tableNameReturn);
	var button = document.getElementById('chd_'+tableNameReturn);
	var size =  document.getElementById('chz_'+tableNameReturn);
	var date =  document.getElementById('cht_'+tableNameReturn);
	var w = document.getElementById('chw_'+tableNameReturn);
	var h = document.getElementById('chh_'+tableNameReturn);
	var infoFile = document.getElementById('chv_'+tableNameReturn);
	var infoMsg = document.getElementById('chg_'+tableNameReturn);
	var imgInfo = document.getElementById('chj_'+tableNameReturn);
	var cancBtn = document.getElementById('chx_'+tableNameReturn);
	var download = document.getElementById('chy_'+tableNameReturn);
	var allowedExt = document.getElementById('chb_'+tableNameReturn);
	
	if(!fName || !path || !flName || ! fInput)
	{
		alert('Errore inaspettato 1');
		return false;
	}
	
	var patt = new RegExp('^galleries\/([^\/]*)', 'i');
	if((b=patt.exec(tableFileOpened)) && (c=patt.exec(file)))
	{
		if(b[1] != c[1])
		{
			alert('Le immagini devono risiedere nella cartella predefinita per questa galleria');
			return false;
		}
	}
	
	var patt = new RegExp('^([^\/]*)\/(.*?)([^\/]*\.(.*))$');
	if(!(a=patt.exec(file)))
	{
		alert('Errore inaspettato 2');
		return false;
	}
	

	regex='('+allowedExt.value+')$'
	patt = new RegExp(regex, 'i');
	if(!(patt.exec(file)))
	{
		alert('L\'Estensione del file selezionato non è permessa per questo campo.\nScegli un altro file con l\'estensione corretta.');
		return false;
	}
	
	fName.innerHTML = a[3];
	path.innerHTML = a[2] ? a[2] : 'Cartella principale';
	flName.value = file;
	fInput.value = '';
	button.setAttribute("onclick", "openFMD2_0('"+file+"', '"+tableNameReturn+"')");
	size.innerHTML=sizeVal;
	date.innerHTML=dateVal;
	download.href='download.php?fName='+file+'&amp;direct';
	if(width) 
	{
		image.src='getimage.php?IMG='+file+'&X_MAX=100&Y_MAX=100';
		w.innerHTML = width.innerHTML;
		h.innerHTML = height.innerHTML;
		imgInfo.style.display="block";
	}
	else
		imgInfo.style.display="none";
	infoMsg.style.display="none";
	infoFile.style.display="block";	
	cancBtn.disabled=false;
	return true;
}
function fileInputChange(elem, field)
{
	var fName = document.getElementById('chn_'+field);
	var path = document.getElementById('chp_'+field);
	var flName = document.getElementById('chs_'+field);
	var fInput = document.getElementById(field);
	var button = document.getElementById('chd_'+field);
	var resBtn = document.getElementById('chr_'+field);
	var cancBtn = document.getElementById('chx_'+field);
	var infoFile = document.getElementById('chv_'+field);
	var infoMsg = document.getElementById('chg_'+field);
	var pathBase = document.getElementById('cha_'+field);
	var allowedExt = document.getElementById('chb_'+field);
	
	if(elem.value)
	{
		regex='('+allowedExt.value+')$'
		var patt = new RegExp(regex, 'i');
		if(!(patt.exec(elem.value)))
		{
			alert('L\'Estensione del file selezionato non è permessa per questo campo.\nScegli un altro file con l\'estensione corretta.');
			elem.value='';
			infoFile.style.display="none";
			infoMsg.innerHTML='<span class="fileName red">Nessun file selezionato.</span><span class="fileLabel">Per associare un file al presente campo puoi aprire la gestione file oppure selezionare un file dal tuo PC ed inviarlo nella cartella di destinazione che desideri.</span>';
			infoMsg.style.display="block";
			cancBtn.disabled=true;
			button.setAttribute("onclick", "openFMD2_0('"+pathBase.value+"', '"+field+"')");
			flName.value='';
			path.innerHTML='';
			fName.innerHTML='Nessun file selezionato';
			resBtn.disabled=true;
			return false;
		}
	
		resBtn.disabled=false;
		fName.innerHTML='Nessun file selezionato';
		path.innerHTML='';
		flName.value='';
		button.setAttribute("onclick", "openFMD2_0('"+pathBase.value+"', '"+field+"')");
		cancBtn.disabled=true;
		infoFile.style.display="none";
		infoMsg.innerHTML='<span class="fileName red">File selezionato per l\'upload.</span><span class="fileLabel">Il file del tuo computer: <b>'+elem.value+'</b> è stato selezionato per essere trasferito sul server.</span>';
		infoMsg.style.display="block";
	}
}
function clearFileInput(io, id)
{
	document.getElementById(id).value = '';
	io.disabled=true;
	
}
function removeFileInput(field)
{
	var fName = document.getElementById('chn_'+field);
	var path = document.getElementById('chp_'+field);
	var flName = document.getElementById('chs_'+field);
	var fInput = document.getElementById(field);
	var button = document.getElementById('chd_'+field);
	var resBtn = document.getElementById('chr_'+field);
	var cancBtn = document.getElementById('chx_'+field);
	var infoFile = document.getElementById('chv_'+field);
	var infoMsg = document.getElementById('chg_'+field);
	var pathBase = document.getElementById('cha_'+field);
	
	if(!fName || !path || !flName || ! fInput)
	{
		alert('Errore inaspettato 3');
		return;
	}
	
	fName.innerHTML='Nessun file selezionato';
	path.innerHTML='';
	flName.value='';
	fInput.value='';
	button.setAttribute("onclick", "openFMD2_0('"+pathBase.value+"', '"+field+"')");
	resBtn.disabled=true;
	cancBtn.disabled=true;
	infoFile.style.display="none";
	infoMsg.innerHTML='<span class="fileName red">Nessun file selezionato.</span><span class="fileLabel">Per associare un file al presente campo puoi aprire la gestione file oppure selezionare un file dal tuo PC ed inviarlo nella cartella di destinazione che desideri.</span>';
	infoMsg.style.display="block";
}
function credits(x, y, file, posx, posy)
{
	if(!x) x=300;
	if(!y) y=300;
	if(!file) file = 'CSS/credits.html';
	if(!posx) posx=400;
	if(!posy) posy=200;
	win=window.open(file, "credits", "directories=no, scrollbars=yes, resizable=no, status=no, height="+y+", left="+posx+", location=no, menubar=no, toolbar=no, top="+posy+", width="+x);
	win.focus();
}



