function calcHeight(){
//  var the_height=document.getElementById('center').contentWindow.document.body.scrollHeight;
  var thedoc = document.getElementById('center').contentWindow.document;
  var the_height=getDocHeight(thedoc);
  
  the_height = the_height + 12;
//change the height of the iframe

  if (the_height >= 412){
    document.getElementById('center').style.height = the_height + 'px';
    document.getElementById('nav').style.height = (the_height +11) + 'px';
  }else{
    document.getElementById('center').style.height = '400px';
    document.getElementById('nav').style.height = '411px';
  }
}

function getDocHeight(doc) {
  var docHt = 0, sh, oh;
  if (doc.height){
  	docHt = doc.height;
  } 
  else if (doc.body) {
    if (doc.body.scrollHeight) docHt = sh = doc.body.scrollHeight;
	if (doc.body.offsetHeight) docHt = oh = doc.body.offsetHeight;
    if (sh && oh) docHt = Math.max(sh, oh);
  }
  return docHt;
}


var urlobj;

function BrowseServer(obj)
{
	urlobj = obj;
	
	OpenServerBrowser(
		'FCKeditor/editor/filemanager/browser/default/browser.html?Type=Image&Connector=connectors/php/connector.php',
		screen.width * 0.5,
		screen.height * 0.5 ) ;
}

function OpenServerBrowser( url, width, height )
{
	var iLeft = (screen.width  - width) / 2 ;
	var iTop  = (screen.height - height) / 2 ;

	var sOptions = "toolbar=no,status=no,resizable=yes,dependent=yes" ;
	sOptions += ",width=" + width ;
	sOptions += ",height=" + height ;
	sOptions += ",left=" + iLeft ;
	sOptions += ",top=" + iTop ;

	var oWindow = window.open( url, "BrowseWindow", sOptions ) ;
}

function SetUrl( url, width, height, alt )
{
	document.getElementById(urlobj).value = url ;
	oWindow = null;
}


function openDeleteConfirmDialog(id,label) {
	Dialog.confirm("<center>Confermare la cancellazione della pagina<br /> <b> " + label +"</b><br/>e delle pagine ad essa associate?.</center>", 
	{windowParameters: {width:300, height:100, className:"alphacube"}, okLabel:'Elimina', cancelLabel:'Annulla',
	cancel:function(win) {void(0)},
	ok:function(win){location.href='submitactions.php?id=' + id + '&action=DELETE';}
	});
}

function openDeleteConfirmDialogUser(id,label) {
	Dialog.confirm("<center>Confermare la cancellazione dell'utente<br /><b> " + label +"</b>?</center>", 
	{windowParameters: {width:300, height:100, className:"alphacube"}, okLabel:'Elimina', cancelLabel:'Annulla',
	cancel:function(win) {void(0)},
	ok:function(win){location.href='submitactions.php?userid=' + id + '&action=USERDELETE';}
	});
}

function openDeleteConfirmDialog(id,label) {
	Dialog.confirm("<center>Confermare la cancellazione della pagina<br /> <b> " + label +"</b><br/>e delle pagine ad essa associate?</center>", 
	{windowParameters: {width:300, height:100, className:"alphacube"}, okLabel:'Elimina', cancelLabel:'Annulla',
	cancel:function(win) {void(0)},
	ok:function(win){location.href='submitactions.php?id=' + id + '&action=DELETE';}
	});
}

// crea l'oggetto per la comunicazione AJAX con il server
// compatibile con tutti i browser che supportano AJAX
function crea_http_req() {
	var req = false;
	if (typeof XMLHttpRequest != "undefined")
		req = new XMLHttpRequest();
	if (!req && typeof ActiveXObject != "undefined") {
		try {
			req=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e1) {
			try {
				req=new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e2) {
				try {
					req=new ActiveXObject("Msxml2.XMLHTTP.4.0");
				} catch (e3) {
					req=null;
				}
			}
		}
	}

	if(!req && window.createRequest)
		req = window.createRequest();

	if (!req) alert("Il browser non supporta AJAX");

	return req;
}

// l'oggetto per comunicare con il server
var http_req = crea_http_req();

function reload(){
	if(http_req.readyState == 4) {	
//		location.reload();
		location.href='index.php';
	}
}

function logout() {
	var dati_post = "Submit=Esci";
	http_req.onreadystatechange = reload;
	http_req.open('POST', 'components/logincheck.php', true);
	http_req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	http_req.setRequestHeader("Content-length", dati_post.length);
	http_req.setRequestHeader("Connection", "close");
	http_req.send(dati_post);
}

function invia_dati() {
	var dati_post = "user=" + encodeURIComponent( document.getElementById("username").value ) +
					"&pass=" + encodeURIComponent( document.getElementById("password").value ) + 
					"&partner=" + encodeURIComponent( document.getElementById("partner").value ) + 
					"&Submit=Accedi";
	http_req.onreadystatechange = gestisci_risposta;
	http_req.open('POST', 'components/logincheck.php', true);
	http_req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	http_req.setRequestHeader("Content-length", dati_post.length);
	http_req.setRequestHeader("Connection", "close");
	http_req.send(dati_post);
}

function gestisci_risposta() {
	if(http_req.readyState == 4) {
		var esito = http_req.responseText;
		switch (esito) {
		  case '1':
			response = 'Username non presente nel sistema!!!';
		  break;

		  case '2':
			response = 'Password errata!!!';
		  break;

		  case '3':
		  	response = 'Username o password non inserite!!!';
		  break;
		  
		  case '4':
//			document.location.reload();
//			self.location.reload();
//			location.href='index.php?partner='+thepartner;
//			location.href='index.php';
			break;

		  default:
			response = 'Risposta del server non riconosciuta!!!';
//			response = esito;
		}
//		$('login_error_msg').innerHTML = "<img src=\"img/exlamation24.png\">" + response;
		if (esito != '4'){
			$('login_error_msg').innerHTML = response;
			$('login_error_msg').show();
			Windows.focusedWindow.updateHeight();
			return false;
		}else{
			location.href='index.php';
			return true;
		}
	}
}

function openLoginDialog() {
	Dialog.confirm($('login').innerHTML, 
		{windowParameters: {className:"alphacube", width:250}, 
			okLabel: "Accedi", 
			cancelLabel: "Annulla", 
			ok:function(win){
				invia_dati();
			}
		}
	);
}
//GESTIONE ORDINAMENTO pagelist.php
var tosort = 0;

function serializelist(){
	varserialized = Sortable.serialize('thelist');
	document.getElementById('serialized').value = varserialized;
	if (tosort == 0){
		Effect.SlideDown('saveorder',{duration:0.3});
		tosort = 1;
	}
}

function getpagetree(pageid) {
//	document.pagineForm.linkint.value=pageid;
	var dati_post = "pageid=" + encodeURIComponent(pageid) +
					"&action=PAGETREE";
					/*
					 + encodeURIComponent( document.getElementById("password").value ) + 
					"&partner=" + encodeURIComponent( document.getElementById("partner").value ) + 
					"&Submit=Accedi";*/
	http_req.onreadystatechange = printtree;
//	http_req.open('POST', 'components/logincheck.php', true);
	http_req.open('POST', 'submitactions.php', true);
	http_req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	http_req.setRequestHeader("Content-length", dati_post.length);
	http_req.setRequestHeader("Connection", "close");
	http_req.send(dati_post);
	
}

function printtree() {
	if(http_req.readyState == 4) {
		var esito = http_req.responseText;
		$('linktree').innerHTML = esito;
		$('linktree').show();
	}
}

var popUpWin = 0;
function popUpWindow(URLStr, left, top, width, height)
{ if(popUpWin) { if(!popUpWin.closed) popUpWin.close(); } popUpWin = open(URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menub ar=no,scrollbar=no,resizable=no,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+''); } 