// trovo la risoluzione video per richiamare il relativo css
function resolution() {
  if (screen.height < 600) {
      screenSize = "600";
  }
  if (screen.height >= 600) {
      screenSize = "600";
  }
  if (screen.height >= 720) {
      screenSize = "720";
  }
  if (screen.height >= 768) {
      screenSize = "768";
  }
  if (screen.height >= 800) {
      screenSize = "800";
  }
  if (screen.height >= 864) {
      screenSize = "864";
  }
  if (screen.height >= 900) {
      screenSize = "900";
  }
  if (screen.height >= 960) {
      screenSize = "960";
  }
  if (screen.height >= 1024) {
      screenSize = "1024";
  }
  if (screen.height >= 1050) {
      screenSize = "1050";
  }
  if (screen.height >= 1200) {
      screenSize = "1200";
  }
  if (screen.height >= 1440) {
      screenSize = "1440";
  }

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

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);
	var dim = getPageSize();
  var wWidth = dim[0];
  var wHeight = dim[1];

  var xScroll, yScroll; 
  xScroll = document.body.scrollLeft; 
  yScroll = document.body.scrollTop; 
  //alert(xScroll+'-'+yScroll);

  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="+(wWidth-280-xScroll)+"px,height="+(wHeight-80-yScroll)+"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';
}

function getPageSize(){ 
    // Returns array with page width, height and window width, height 
    var xScroll, yScroll; 
     
    if (window.innerHeight && window.scrollMaxY) {     
        xScroll = document.body.scrollWidth; 
        yScroll = window.innerHeight + window.scrollMaxY; 
    } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac 
        xScroll = document.body.scrollWidth; 
        yScroll = document.body.scrollHeight; 
    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari 
        xScroll = document.body.offsetWidth; 
        yScroll = document.body.offsetHeight; 
    } 
     
    var windowWidth, windowHeight; 
    if (self.innerHeight) {    // all except Explorer 
        windowWidth = self.innerWidth; 
        windowHeight = self.innerHeight; 
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode 
        windowWidth = document.documentElement.clientWidth; 
        windowHeight = document.documentElement.clientHeight; 
    } else if (document.body) { // other Explorers 
        windowWidth = document.body.clientWidth; 
        windowHeight = document.body.clientHeight; 
    }     
     
    // for small pages with total height less then height of the viewport 
    if(yScroll < windowHeight){ 
        pageHeight = windowHeight; 
    } else { 
        pageHeight = yScroll; 
    } 

    // for small pages with total width less then width of the viewport 
    if(xScroll < windowWidth){     
        pageWidth = windowWidth; 
    } else { 
        pageWidth = xScroll; 
    } 
    arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
    return arrayPageSize;
}

