gd = function(objid){
	return document.getElementById(objid);
}

t = function(objid1, objid2){
	with (gd(objid1).style)
		display = (display != 'none') ? 'none' : 'block';
	with (gd(objid2).style)
		display = (display != 'none') ? 'none' : 'block';
}


toggle = function(objid){
	with (gd(objid).style){
		display = (display != 'none') ? 'none' : 'block';
	}	
}


function fpos(obj) {
	if (obj){
		var curleft = curtop = 0;
		if (obj.offsetParent) {
			do {
				curleft += obj.offsetLeft;
				curtop += obj.offsetTop;
			} while (obj = obj.offsetParent);
			return [curleft,curtop];
		}
	}
	return false;
}

function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}

function check_editmember(){
	xform = document.forms.editmember;
	if (xform.lastname.value == '') { alert('Attenzione: Campo `Cognome/Ditta` obbligatorio'); return(0); }
	xform.submit();	
}
function check_newmember(){
	xform = document.forms.newmember;
	if (xform.lastname.value == '') { alert('Attenzione: Campo `Cognome/Ditta` obbligatorio'); return(0); }
	xform.submit();
}
function confirm_delete(xtodelete, xlink){
	if (confirm('Eliminare definitivamente ' + xtodelete + '?')){
		window.location.href = xlink;
	}
}

function confirm_delete_section(xname, x_sectionid){
	if (confirm('Eliminare definitivamente la sezione "' + xname + '"?\n\nAttenzione: verranno eliminati anche i contenuti e le categorie\nappartenenti a questa sezione.')){
		window.location.href = 'index.php?delete_section='+x_sectionid;
	} else {
		return;
	}
}

function confirm_delete_category(xname, x_categoryid,x_sectionid){
	if (confirm('Eliminare definitivamente la categoria "' + xname + '"?\n\nAttenzione: verranno eliminati anche i contenuti\nappartenenti a questa categoria.')){
		window.location.href = 'index.php?delete_category='+x_categoryid+'&sectionid='+x_sectionid;
	} else {
		return;
	}
}
var currentgallery = new Array();

function nextimg(){
	
	if (!gd('gallery_img')){
		return false;
	} else {
		var currentimg = gd('gallery_img').src;
	}
		
	nexti = 0;
	for (var i = 0; i < currentgallery.length; i++){
		if (currentimg.indexOf('/'+currentgallery[i]) >= 0) {
			if (i == currentgallery.length - 1){
				nexti = 0;
			} else {
				nexti = i+1;
			}
		}
	}
	gd('gallery_img').src = '/files/'+currentgallery[nexti];
	deselect();
}

function previmg(){
	
	if (!gd('gallery_img')){
		return false;
	} else {
		var currentimg = gd('gallery_img').src;
	}
		
	nexti = 0;
	for (var i = 0; i < currentgallery.length; i++){
		if (currentimg.indexOf('/'+currentgallery[i]) >= 0) {
			if (i == 0){
				nexti = currentgallery.length - 1;
			} else {
				nexti = i-1;
			}
			break;
		}
	}
	gd('gallery_img').src = '/files/'+currentgallery[nexti];
	deselect();
}

function openGallery(cid){
	
	if (!cid){
		return false;
	} else {
		currentgallery = cid;
	}
	
	div_height = 540;
	div_width = 520;
	div_top = Math.round((f_clientHeight() / 2) - (div_height / 2) + f_scrollTop());
	div_left = Math.round((f_clientWidth() / 2) - (div_width / 2) + f_scrollLeft());
	if (!gd('float_gallery')){
		gallery = document.createElement('div');
		gallery.id = 'float_gallery';
		document.body.appendChild(gallery);
	} else {
		gallery = gd('float_gallery');
	}

	with (gallery.style){
		display = 'block';
		position = 'absolute';
		overflow = 'hidden';
		float = 'left';
		zIndex = 99;
		left = div_left+'px';
		top = div_top+'px';
		width = div_width+'px';
		minHeight = div_height+'px';
		backgroundColor = '#000000';
		border = '1px solid #7E7A6F';
	}
	
	var galleryhtml = '<div style="padding:2px;background-image:url(/img/gb_lo.jpg);width:100%;overflow:auto;">';
	galleryhtml += '<a style="float:left;text-decoration:none;color:#B40000;font-weight:bold;padding:2px;" onclick="previmg();" href="javascript:void(0)">&lt; indietro</a>';
	galleryhtml += '<a style="float:left;margin-left:16px;text-decoration:none;color:#B40000;font-weight:bold;padding:2px;" onclick="nextimg();" href="javascript:void(0)">avanti &gt;</a>';
	galleryhtml += '<a style="float:right;text-decoration:none;color:#B40000;font-weight:bold;padding:2px;margin-right:4px;" onclick="closeGallery()" href="javascript:void(0)">X</a></div>';
	galleryhtml += '</div>';
	//galleryhtml+= '<div onclick="previmg();" style="cursor:pointer;float:left;display:block;height:350px;width:12px;background-color:#B40000"><!-- // --></div>';
	galleryhtml+= '<img style="float:left;cursor:pointer;" id="gallery_img" onclick="closeGallery()" alt="" width="520" height="520" src="/files/'+cid[0]+'" />';
	//galleryhtml+= '<div onclick="nextimg();" style="cursor:pointer;float:left;display:block;height:350px;width:12px;background-color:#B40000"><!-- // --></div>';
	
	gallery.innerHTML = galleryhtml;
	
	return gallery;

}

function closeGallery(){

	if (gd('float_gallery')){
		gd('float_gallery').parentNode.removeChild(gd('float_gallery'));
	}

}

tinyon = false;
loadTinyMCE = function(){
	if (!tinyon){
		tinyMCE.init({
			width : "100%",
			mode : "textareas",
			theme : "advanced",
			theme_advanced_buttons1 : "bold,italic,underline,separator,strikethrough,justifyleft,justifycenter,justifyright, justifyfull,bullist,numlist,undo,redo,link,unlink,forecolor,hr,table,cleanup,code,preview",
			theme_advanced_buttons2 : "",
			theme_advanced_buttons3 : "",
			theme_advanced_toolbar_location : "top",
			theme_advanced_toolbar_align : "left",
			theme_advanced_path_location : "bottom",
			invalid_elements : "em",
			editor_deselector : "mceNoEditor",
			extended_valid_elements : "a[name|href|target|title|onclick],img[class|src|border=0|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name],hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style]"
		});
		tinyon = true;
		return true;
	} else {
		return true;
	}
}

function applyEditor(id){
	if (loadTinyMCE()){
		tinyMCE.execCommand('mceAddControl', false, id);
	}
}

function html_entity_decode(str)
{
    try
	{
		var  tarea=document.createElement('textarea');
		tarea.innerHTML = str; return tarea.value;
		tarea.parentNode.removeChild(tarea);
	}
	catch(e)
	{
		//for IE add <div id="htmlconverter" style="display:none;"></div> to the page
		document.getElementById("htmlconverter").innerHTML = '<textarea id="innerConverter">' + str + '</textarea>';
		var content = document.getElementById("innerConverter").value;
		document.getElementById("htmlconverter").innerHTML = "";
		return content;
	}
}

function deselect() {
  if (document.selection)
    document.selection.empty();
  else if (window.getSelection)
    window.getSelection().removeAllRanges();
}

function toggleEditor(id) {
	if (gd('chk_html')){
		if (gd('chk_html').checked){
			if (loadTinyMCE()){
				var tx = gd(id).value;
				gd(id).value = tx.replace(/\n/ig,'<br />');
				tinyMCE.execCommand('mceAddControl', false, id);
			}
		} else {
			if (tinyon){
				if (confirm('Gli stili applicati verranno persi, continuare?')){
					var tx = tinyMCE.get(id).getContent();
					tx = html_entity_decode(tx);
					tx = tx.replace(/\n/ig,'');
					tx = tx.replace(/\<\/li\>/ig,'\n'); 
					tx = tx.replace(/\<hr/ig,'\n<hr');
					tx = tx.replace(/\<\/p\>/ig,'\n\n'); 
					tx = tx.replace(/\<br/ig,'\n<br');
					tx = tx.replace(/(<([^>]+)>)/ig,"");

					tinyMCE.execCommand('mceRemoveControl', false, id);

					gd(id).value = tx;
				} else {
					gd('chk_html').checked = true;
				}
			}
		}
	}
}

function xget(uri, div){

	var xmlhttp=null;

	if (window.XMLHttpRequest)
	  xmlhttp=new XMLHttpRequest();
	else if (window.ActiveXObject)
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		
	if (xmlhttp!=null){
		xmlhttp.open("GET",uri,true);
	  xmlhttp.onreadystatechange = function(){
	  	if (div){
	  		if (xmlhttp.readyState==4){
				  if (xmlhttp.status==200){
						gd(div).innerHTML = xmlhttp.responseText;
			  	}
				}
			}
	  }
	  
	  xmlhttp.send(null);
	}
	
}

function getParentByTagName(obj, tagName) {
	tagName = tagName.toLowerCase();
	while (obj!= null && obj.tagName!=null && obj.tagName.toLowerCase() != tagName) {
		obj=obj.parentNode;
	}
	return obj;
}

function text(obj, tagName) {
	tagName = tagName.toLowerCase();
	while (obj!= null && obj.tagName!=null && obj.tagName.toLowerCase() != tagName) {
		obj=obj.parentNode;
	}
	return obj;
}

function text_zoomin(){
	gd('zoomin').style.display = 'none';
	gd('zoomout').style.display = 'block';
	var divs = document.getElementsByTagName('div');
	for(var i = 0; i < divs.length; i++){
		if (divs[i].className == 'text_content'){
			divs[i].style.fontSize = '16px';
		}
	}
	xget('ajax.php?setzoom=1');
}

function text_zoomout(){
	gd('zoomout').style.display = 'none';
	gd('zoomin').style.display = 'block';
	var divs = document.getElementsByTagName('div');
	for(var i = 0; i < divs.length; i++){
		if (divs[i].className == 'text_content'){
			divs[i].style.fontSize = '14px';
		}
	}
	xget('ajax.php?setzoom=0');
}


function arraymerge(a1, a2){
	var a3 = [];
	for (var i=0; i < a1.length; i++)
		a3[i] = a1[i];
  for (var c=0; c < a2.length; c++)
    a3[i+c] = a2[c];
  return a3;
}

function homelang(cid){
	
	gd('home_content_13').style.display = 'none';
	gd('home_content_150').style.display = 'none';
	gd('home_content_145').style.display = 'none';
	gd('home_content_154').style.display = 'none';
	with(gd('home_link_13').style){border = '';color = '';background = '';}
	with(gd('home_link_150').style){border = '';color = '';background = '';}
	with(gd('home_link_145').style){border = '';color = '';background = '';}
	with(gd('home_link_154').style){border = '';color = '';background = '';}
	
	gd('home_content_'+cid).style.display = 'block';
	with(gd('home_link_'+cid).style){
		border = '1px solid #7E716F';
		borderBottom = '1px solid #FFFFFF';
		color = '#000000';
	}	
}

window.onload = function(){
	
	/*document.onmouseup = function(evt){
		deselect();
	}*/
	
	var hrefs = document.getElementsByTagName('a');
	for(var i = 0; i < hrefs.length; i++){
		
		if (hrefs[i].href.indexOf('del_') >= 0){
			hrefs[i].onclick = function(){
				this.parentNode.parentNode.style.background = '#FFEDAE';
				if (confirm('Eliminare definitivamente questo elemento?')){
					xget(this.href);
					tr = getParentByTagName(this, 'tr');
					tr.parentNode.removeChild(tr);
					return false;
				} else {
					this.parentNode.parentNode.style.background = '';
					return false;
				}
			}
		}
		
		if (hrefs[i].id.indexOf('menu_') >= 0){

			hrefs[i].onmouseover = function(){
				oid = this.id;
				this.className = 'headmenu_hover';
				if (gd('float'+oid)){
					gd('float'+oid).style.display = 'block';
					gd('float'+oid).style.left = (fpos(this)[0] -1) +'px';
					gd('float'+oid).style.top = (fpos(this)[1] + 29) + 'px';
					gd('float'+oid).onmouseover = function(){
						gd(oid).className = 'headmenu_hover';
						this.style.display = 'block';
					}
				}
			}
			hrefs[i].onfocus = function(){
				this.onmouseover();
			}
			hrefs[i].onblur = function(){
				this.onmouseout();
			}
			
			hrefs[i].onmouseout = function(){
				oid = this.id;
				this.className = '';
				if (gd('float'+oid)){
					gd('float'+oid).style.display = 'none';
					gd('float'+oid).onmouseout = function(){
						this.style.display = 'none';
						gd(oid).className = '';
					}
				}
			}
		}
	}
	
	var tables = document.getElementsByTagName('table');
	for(var i = 0; i < tables.length; i++){
		if (tables[i].className == 'tbl' ){
			var tbody = tables[i].getElementsByTagName('tbody');
			var trs = tbody[0].getElementsByTagName('tr');
			for(var t = 0; t < trs.length; t++){
				trs[t].onmouseover = function(){
					this.style.background = '#F0F4EC';
				}
				trs[t].onmouseout = function(){
					with (this.style){
						if (backgroundColor.toLowerCase() == '#f0f4ec' || backgroundColor.toLowerCase() == 'rgb(240, 244, 236)'){
							backgroundColor = '';
						}
					}
				}
			}
		}
	}
	var allselect = document.getElementsByTagName('select');
	var allinputs = document.getElementsByTagName('input');
	var allinput = arraymerge(allinputs,allselect);
	for(var i = 0; i < allinput.length; i++){
		if (allinput[i].className == 'button' ){
			allinput[i].onmouseover = function(){
				this.style.color = '#FF2E17';
			}
			allinput[i].onmousedown = function(){
				this.className = 'buttondown';
				this.style.color = '#FF2E17';
			}
			allinput[i].onmouseup = function(){
				this.className = 'button';
			}
			allinput[i].onmouseout = function(){
				this.className = 'button';
				this.style.color = '';
			}
		} else {
			allinput[i].onfocus = function(){
				this.style.borderColor = '#FF2E17';
			}
			allinput[i].onblur = function(){
				this.style.borderColor = '';
			}
			if (allinput[i].id.indexOf('autofocus') >= 0){
				allinput[i].focus();
				allinput[i].select();
			}
		}
		
	}
	
	var alltextareas = document.getElementsByTagName('textarea');
	for(var i = 0; i < alltextareas.length; i++){
		alltextareas[i].onfocus = function(){
			this.style.borderColor = '#FF2E17';
		}
		alltextareas[i].onblur = function(){
			this.style.borderColor = '';
		}
	}


}