function refreshTreeviewAdmin() {
  var status = AjaxRequest.get(
    {
      'url':'/admin/treeview.php'
      ,'onSuccess':function(req){ eval(req.responseText); }
      ,'onError':function(req){ alert('Error!\nStatusText='+req.statusText+'\nContents='+req.responseText);}
    }
  );
  return status;
}

function submitForm(theform) {
  var msgObj = document.getElementById('formAttesa');
  var tastiObj = document.getElementById('formTasti');
  var azione = theform.azione.value;
  //eseguo il submit via AJAX
  var status = AjaxRequest.submit(
    theform
    ,{
      'onLoading':function() {
        tastiObj.style.display = 'none';
        msgObj.style.display = 'block';
      }
      /*,'onLoaded':function() { msgObj.style.display='none'; }*/
      ,'onSuccess':function(req){
        var split_result = req.responseText.split('|');
        //alert(split_result.toString());
        if (split_result[0] == '-1') {
          alert(split_result[1]);
          try {
            theform.elements[split_result[2]].focus();
          }
          catch(err) {}
          msgObj.style.display = 'none';
          tastiObj.style.display = 'block';
        }
        else {
          //esito positivo LOGIN
          if (azione == 'login') {
            var referer = theform.referer.value;
            msgObj.style.display = 'none';
            //alert(split_result[1]);
            self.location = referer;
          }
          //esito positivo altri casi
          else {
            msgObj.style.display = 'none';
            alert(split_result[1]);
            self.location = '/';
          }
        }
      }
      ,'onError':function(req){ alert('Error!\nStatusText='+req.statusText);}
    }
  );
  return status;
}

function init_field(obj, text) {
  if (obj.value == text) obj.value='';
  return true;
}

function logout() {
  var status = AjaxRequest.get(
    {
      'url':'/elabora.ajax.php?azione=logout'
      ,'onSuccess':function(req){ 
        //alert(req.responseText);
        self.location = '/';
      }
      ,'onError':function(req){ alert('Error!\nStatusText='+req.statusText+'\nContents='+req.responseText);}
    }
  );
  return status;
}

function mouseoverToolTip(id) {
	var id = (id == null) ? '' : id;
  ttObj = document.getElementsByName('tooltip');
  for (i=0; i<ttObj.length; i++) {
    ttObj[i].onmouseover=function(){
      showToolTip(this, id);
    }
    ttObj[i].onmouseout=function(){
      hideToolTip(id);
    }
  }
}

function showToolTip(obj, id) {
  tooltipObj = document.getElementById('tooltip'+id);
  //alert(document.getElementById('tooltip'+id).offsetHeight);
  //alert(document.getElementById('tooltip'+id).offsetWidth);
  pos = findPos(obj);
  tooltipHeight = tooltipObj.offsetHeight;
  //alert(window.pageYOffset);
  if ((pos[1] - 0) > (tooltipHeight + 20))
    newX = pos[1] - (tooltipHeight + 20);
  else
    newX = pos[1] + 20;
  document.getElementById('tooltip'+id).style.top=newX+'px';
  document.getElementById('tooltip'+id).style.visibility='visible';
}

function hideToolTip(id) {
  document.getElementById('tooltip'+id).style.visibility='hidden';
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

//apre la pagina dei crediti
function openCredits(w, h)
{
	var w = (w == null) ? '320' : w;
	var h = (h == null) ? '240' : h;
	var left = ( (screen.width-w) / 2);
	left = (left > 0) ? left : 0;
	var top = ( (screen.height-h) / 2);
	top = (top > 0) ? top : 0;
  var res = window.open('/credits.php', 'credits_popup', 'left='+left+', top='+top+', width='+w+', height='+h+' ,toolbar=no, location=no, status=yes, menubar=no, scrollbars=no, resizable=no');
  res.focus();
  return res;
}

//funzione per controllare la lunghezza di una textarea
function maxChars(ogg,maxchars)
{
	if (ogg.value.length > maxchars){
	  ogg.value=ogg.value.substr(0,maxchars);
	}
}

//funzione per controllare il valore con una regexp
function campoRegExp(campo,exp,messaggio)
{
	var reg = exp
	if(!reg.test(campo.value)){
		alert(messaggio);
		campo.focus();
		return (0);
	}
}

function campoBol(campo,val,messaggio){
	if(! val){
		alert(messaggio)
		campo.focus();
		return (0);
	}
}								

function campoNotBol(campo,val,messaggio){
	if(val){
		alert(messaggio)
		campo.focus();
		return (0);
	}
}								

function campoBlank(campo,val,messaggio){
	if(val.length == 0){
		alert(messaggio)
		return (0);
	}
}					
		
//funzione per il redirect temporizzato
function redirect(url,secondi)
{
	if(url!=''){
		if(secondi==''||isNaN(secondi)) secondi = 5;
		window.setTimeout("window.location='"+url+"'",secondi*1000);}
}

//funzioni per iscrizione newsletter infomail
function setDefaultValues(){
	var fname = document.subscribe.firstName.value;
	var lname = document.subscribe.lastName.value;
	if(!checkEmptyString(fname)) document.subscribe.firstName.value=' ';
	if(!checkEmptyString(lname)) document.subscribe.lastName.value=' ';
	document.subscribe.submit();
}
function checkEmptyString(str) {
	for(var i=0; i<str.length; i++){
		if(str.charAt(i)!=' '){
			return 1;		// exits as soon as it finds non spaces
		}
	}
	return 0;		// if it gets as far as here it was all spaces
}

/* ----------------------------* ***** *------------------------------------- */
/* ----------------------------* ADMIN *------------------------------------- */
/* ----------------------------* ***** *------------------------------------- */

function showTtipAdmin(ref) {
  document.getElementById('extra').innerHTML = ttipAdmin[ref];
  return true;
}

function hideTtipAdmin(ref) {
  document.getElementById('extra').innerHTML = '...';
  return true;
}

function closeWin(id) {
  window.parent.win[id].close();
}

function openWin(id, url, titolo) {
	var k = id;
	var nome = unescape(titolo);
  results = url.search(/\?/gi);
  if (results == -1)
    url += '?idwin='+id
  else
    url += '&idwin='+id
	if (win[k]) {
    win[k].show();
	} else {
		win[k]=dhtmlwindow.open(id, "iframe", url, nome, "width="+((screen.width)-(278))+"px,height="+((screen.height)-(328))+"px,resize=1,scrolling=yes,center=0", "recal");
		win[k].moveTo(254,34);
		win[k].onclose=function(){
		  win[k]=null;
      return true;
    }
	}
}

function showToolTipAdmin(parentObj, titolo, testo) {
  tooltipObj = document.getElementById('tooltip');
  pos = findPos(parentObj);
  tooltipObj.style.left=(pos[0]-150)+'px';
  tooltipObj.style.top=pos[1]+'px';
  document.getElementById('tooltip_titolo').innerHTML = titolo;
  document.getElementById('tooltip_testo').innerHTML = testo;
  tooltipObj.style.visibility='visible';
}

function hideToolTipAdmin() {
  document.getElementById('tooltip').style.visibility='hidden';
}
