function toggleappear(id){
  new Effect.toggle(id, 'appear', { duration: .5 });
}

function chemclose(id,idno){
	var sections = document.getElementsByClassName(id);
	$A(sections).each(function(section) {
		var sectionID = section.id;
    if(idno=='all' || sectionID!=idno){ /* chiudo tutti tranne il div attivo */
		//new Effect.Highlight(sectionID);
		//sectionID.update(''); 
      new Effect.BlindUp(sectionID, { duration: .6 });
    }
	});  
}
function alertSound(){
	Sound.play('/sounds/alert.mp3');  
  //Sound.play('alert.mp3');
  // --> plays sound immediately in 'global' track
  //Sound.play('alert.mp3',{replace:true});
  // --> stop playing all sounds in 'global' track, and play new sound
  //Sound.play('alert.mp3',{track:'mytrack'});
  // --> places the sound in the 'mytrack' track
  //Sound.play('alert.mp3',{track:'mytrack',replace:true});
  // --> stop playing all sounds in 'mytrack' track, and play new sound	
}
function chem_cat(id,liv){
  livello = liv-1;
  idclose = 'bc'+livello;
  if($(id+'_content').style.display!='none'){
    chemclose(idclose,'all');
  }else{
  //new Effect.ScrollTo('overlay', {offset: -24, afterFinish: function(){
  
  //alert(idclose);
  //chemclose(idclose);
	var pars = 'id='+id+'&liv='+liv;
  	new Ajax.Updater(
  		id+'_content',
  		'/chiedoemangio/chiedoemangio_liv.php', {
		  parameters: pars,      
  		onLoading:function(){
        //$(id+'_content').update('attendere');
        $(id+'_content').hide(),
        $('link'+id).className = 'chemload';
          chemclose(idclose,id+'_content');
  		},   
  		onComplete:function(){
      new Effect.BlindDown(id+'_content', { duration: .5, afterFinish: function(){
          $('link'+id).className = $('link'+id).className.replace(/\bchemload\b/,'');
          //new Effect.ScrollTo('CHEM_'+id, {offset: -24});
        }
      });
  		},	
  		asynchronous:true, evalScripts:true
  		}
  	);
        //}});

  }
}
                                
function closewin(win){
  initScrollX = $('scrollx').value;
  initScrollY = $('scrolly').value;
    new Effect.Fade(win, { duration: .6, afterFinish: function(){
      $('overlay').hide();
      prepareIE("",""); // Preparing IE 6 for showing modalbox		
      }
    });  
}    

function prepareIE(height, overflow){
		//if(Prototype.Browser.IE && !navigator.appVersion.match(/\b7.0\b/)) { // Preparing IE 6 for showing modalbox
		if(Prototype.Browser.IE && navigator.appVersion.match(/\b6.0\b/)) { // Preparing IE 6 for showing modalbox
			window.scrollTo(0,0);
		  //$$('html, body').invoke('setStyle', {width: height, height: height, overflow: overflow}); // IE requires width and height set to 100% and overflow hidden
		}
	}
function chem_risp(id){
		var scrollx = window.pageXOffset || document.body.scrollLeft || document.documentElement.scrollLeft;
		var scrolly = window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop;	
    var my_width  = 0;
    var my_height = 0;

		prepareIE("100%", "hidden"); // Preparing IE 6 for showing modalbox  	

	if (window.innerHeight && window.scrollMaxY) {// Firefox
		my_height = window.innerHeight + window.scrollMaxY;
		my_width = window.innerWidth + window.scrollMaxX;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		my_height = document.body.scrollHeight;
		my_width = document.body.scrollWidth;
	} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
		my_height = document.body.offsetHeight;
		my_width = document.body.offsetWidth;
  	}    
    halfheight=(my_height/2);
    
  $('overlay').setStyle({
    height: my_height+ 'px',
    width: my_width+ 'px'
  });
  //$('popup').setStyle({
  //top: halfheight+ 'px'
  //});  
  //$('overlay').show();
  toggleappear('overlay');
  toggleappear('popup');
  $('popup').update(''); 
	var pars = 'id='+id;
  	new Ajax.Updater(
  		'popup',
  		'/chiedoemangio/chiedoemangio_risp.php', {
		  parameters: pars,      
  		//onLoading:function(){
      //  $('popup').update('attendere');
  		//}, 
  		onComplete:function(){
    		$('scrollx').value=scrollx;
    		$('scrolly').value=scrolly; 
        },        
  		asynchronous:true, evalScripts:true
  		}
  	);
   	
}
/* ************ modulo per richieste chiedoemangio **************** */
/*
function CHEMValid(form) {
  var nome=$('nome').value;
  var cognome=$('cognome').value;
  var email=$('email').value;
  var messaggio=$('messaggio').value;

  var fadetime='4';
  var valid=true;
  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;

  if(nome == "") {
    inlineMsg('nome','Qual\'e\' il tuo nome?',fadetime);
    return false;
  }
  if(cognome == "") {
    inlineMsg('cognome','Qual\'e\' il tuo cognome?',fadetime);
    return false;
  }
  if(email == "") {
    inlineMsg('email','Inserisci la tua email.',fadetime);
    return false;
  }else{
    if(!email.match(emailRegex)) {
      inlineMsg('email','Formato email non valido.',fadetime);
      return false;
    }
  }
  if(messaggio == "") {
    inlineMsg('messaggio','Non hai specificato alcun messaggio',fadetime);
    return false;
  }

   return true;
}

*/
/************************* SOMANGIARE? **********************************/
function SMvalidate() {
  var altezza = $('altezza').value;
  var peso = $('peso').value;
  var anno = $('anno').value;
  var fadetime='3';
  
  var data = new Date();
  var questanno = data.getFullYear();

  var numRegex = /^[0-9]$/;

  if(anno == "" || anno ==0) {
    inlineMsg('anno','qual\'&eacute; il tuo anno di nascita?',fadetime);
    return false;
  }
  if(anno < 1900 || anno > questanno) {
    inlineMsg('anno','verifica il corretto inserimento del valore (es. 1980)',fadetime);
    return false;
  }
  if (isNaN(parseInt(anno))) {
      inlineMsg('anno','non &eacute; un valore numerico.',fadetime);
      return false;
  }  
  if(peso == "" || peso ==0) {
    inlineMsg('peso','quanto pesi?',fadetime);
    return false;
  }
  if(peso < 20 || peso > 500) {
    inlineMsg('peso','sei sicuro?',fadetime);
    return false;
  }
  if (isNaN(parseInt(peso))) {
      inlineMsg('peso','non &eacute; un valore numerico.',fadetime);
      return false;
  }   
  if(altezza == "" || altezza ==0) {
    inlineMsg('altezza','qual\'&eacute; la tua altezza?',fadetime);
    return false;
  }
  if(altezza < 50 || altezza > 300) {
    inlineMsg('altezza','sei sicuro?',fadetime);
    return false;
  }
  if (isNaN(parseInt(altezza))) {
      inlineMsg('altezza','non &eacute; un valore numerico.',fadetime);
      return false;
  }  
  return true;
  //SB_responso();
  //return false;
}
function SO_attfis(liv){
    //alertSound();
		var scrollx = window.pageXOffset || document.body.scrollLeft || document.documentElement.scrollLeft;
		var scrolly = window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop;	
    var my_width  = 0;
    var my_height = 0;

		prepareIE("100%", "hidden"); // Preparing IE 6 for showing modalbox  	

	if (window.innerHeight && window.scrollMaxY) {// Firefox
		my_height = window.innerHeight + window.scrollMaxY;
		my_width = window.innerWidth + window.scrollMaxX;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		my_height = document.body.scrollHeight;
		my_width = document.body.scrollWidth;
	} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
		my_height = document.body.offsetHeight;
		my_width = document.body.offsetWidth;
  	}    
    halfheight=(my_height/2);
    
    if(my_height==0 || my_height==''){my_height='1200';}
  $('overlay').setStyle({
    height: my_height+ 'px',
    width: my_width+ 'px'
  });
  //$('popup').setStyle({
  //top: halfheight+ 'px'
  //});  
  //$('overlay').show();
  toggleappear('overlay');
  toggleappear('popup');
  $('popup').update(''); 
  	new Ajax.Updater(
  		'popup',
  		'/somangiare/somangiare_attfisicapop'+liv+'.php', {
  		//onLoading:function(){
      //  $('popup').update('attendere');
  		//}, 
  		onComplete:function(){
    		$('scrollx').value=scrollx;
    		$('scrolly').value=scrolly; 
        },        
  		asynchronous:true, evalScripts:true
  		}
  	);  	
}

/************************** CONTOEMANGIO **********************************************************/

function cem_add(idvalue,tipo,pasto,quant){
  //if(!$('check'+idvalue)){
  	var pars = 'idvalue='+idvalue+'&tipo='+tipo+'&pasto='+pasto;
  	 var url = '/contoemangio/cem_aggiungi_alim.php';
  	new Ajax.Updater(
  		'cem_contenitore', 
  		url, {
  		 onComplete:function(){
  		  cem_esito();
        new Effect.Fade('cem_contenuto_ricerca', { duration: .6});
  		 },
  		parameters: pars,
  		insertion: Insertion.Top,
  		asynchronous:true, evalScripts:true
  		}
  	);
	//}else{
    //alert('alimento gia\' presente');
  //}
}

function faderemove(id){
  new Effect.Fade(id, { duration: .5, afterFinish: function(){
      $(id).remove(); //verifica explorer
      cem_esito();
    }  
  });
}  

function cercacem(){
	var params = Form.serialize($('cercacem')); 
	new Ajax.Updater(
		'cem_contenuto_ricerca', 
		'/contoemangio/cercacontoemangio.php?ajax=s', {
		parameters: params,
  		//onLoading:function(){
         //new Effect.Fade('xxx');
  		//}, 	
  		 onComplete:function(){
  		  cem_esito();
        new Effect.Appear('cem_contenuto_ricerca', { duration: .8});
  		 },	
		asynchronous:true, evalScripts:true
		}
	);	
	//new Effect.Appear('panel');
}

function cem_calcolafabbisogno(){
  var anno = $('anno').value;
  var altezza = $('altezza').value;
  var data = new Date();
  var questanno = data.getFullYear();
  var fadetime='3';

  if(anno == "" || anno ==0) {
    inlineMsg('anno','qual\'&eacute; il tuo anno di nascita?',fadetime);
    return false;
  }
  if(anno < 1900 || anno > questanno) {
    inlineMsg('anno','verifica il corretto inserimento del valore (es. 1980)',fadetime);
    return false;
  }
  if(altezza == "" || altezza == 0) {
    inlineMsg('altezza','Quanto sei alto?',fadetime);
    return false;
  }
  if(altezza < 50 || altezza > 300 ) {
    inlineMsg('altezza','Sei sicuro?',fadetime);
    return false;
  }
  if (isNaN(parseInt(altezza))) {
      inlineMsg('altezza','non &eacute; un valore numerico.',fadetime);
      return false;
  }
	var params = Form.serialize($('CEMform'));
	new Ajax.Updater(
		'CEM_responso_fabbisogno', 
		'/contoemangio/cem_calcolafabbisogno.php', {
		parameters: params,
		method: 'get',
  		//onLoading:function(){
         //new Effect.Fade('xxx');
  		//}, 
  		 onComplete:function(){
        new Effect.Appear('CEM_responso_fabbisogno');
  		 },			
		asynchronous:true, evalScripts:true
		}
	);	
	//new Effect.Appear('panel');
}

function cem_esito(){
  cem_attendi('calcola','on');
	var params = Form.serialize($('cem_contenuto_form')); 
	new Ajax.Updater(
		'CEM_esito',
		'/contoemangio/calcola.php', {
		parameters: params,
  		//onLoading:function(){
      //   cem_attendi('calcola','on');
  		//}, 	
  		onComplete:function(){
         cem_attendi('calcola','off');
  		},
		asynchronous:true, evalScripts:true
		}
	);	
	//new Effect.Appear('panel');
}

function endisable(id){
  if($(id).disabled == true){
    $(id).disabled = false;
  }else{
    $(id).disabled = true;
  }
}
function cem_attendi(div,onoff){
  if(onoff=='on'){
    $('CEM_'+div+'_attendi').update('<img src="/struttura/mini_orange_loader.gif" align="absmiddle" title="attendi" alt="attendi">');
  }else{
    $('CEM_'+div+'_attendi').update('');
  }
}

function cem_avanti(pasto){
  $('pasto').value=pasto; /* cambio il valore del tipo di pasto nella form di ricerca */
	var params = Form.serialize($('cem_contenuto_form')); 
	new Ajax.Updater(
		'cem_contenitore',
		'/contoemangio/cem_dieta_update.php?pasto='+pasto, {
		parameters: params,
  		onLoading:function(){
         cem_attendi('search','on');
  		}, 	
  		onComplete:function(){
         cem_attendi('search','off');
         new Effect.ScrollTo('CEM_traccia_blocco', {offset: -100});
  		}, 	
		asynchronous:true, evalScripts:true
		}
	);	
	//new Effect.Appear('panel');

  if(pasto=='colazione'){
    var avanti = 'spuntino';
    var indietro = '';
  }else if(pasto=='spuntino'){
    var avanti = 'pranzo';
    var indietro = 'colazione';
  }else if(pasto=='pranzo'){
    var avanti = 'merenda';
    var indietro = 'spuntino';
  }else if(pasto=='merenda'){
    var avanti = 'cena';
    var indietro = 'pranzo'; 
  }else if(pasto=='cena'){
    var avanti = '';
    var indietro = 'merenda';
  }
  
  if(indietro!=''){
    $('cem_indietro').update('<a href="#" onclick="cem_avanti(\''+indietro+'\'); return false"><img src="/struttura/pul_indietro.jpg" align="absmiddle" title="indietro" alt="indietro"> Indietro</a>');
  }else{
    $('cem_indietro').update('<img src="/struttura/pul_indietro_spento.jpg" align="absmiddle" title="indietro" alt="indietro"> Indietro');
  }
  if(avanti!=''){
    $('cem_avanti').update('<a href="#" onclick="cem_avanti(\''+avanti+'\'); return false">Avanti</a> <img src="/struttura/pul_avanti.jpg" align="absmiddle" title="avanti" alt="avanti">');
  }else{
    $('cem_avanti').update('Avanti <img src="/struttura/pul_avanti_spento.jpg" align="absmiddle" title="indietro" alt="indietro">');
  }
  cem_dieta_briciole(pasto);
  $('cem_contenuto_ricerca').update(''); /* svuoto il box con i risultati della ricerca */
  $('searchtext').value=''; /* cancello il valore nel motore di ricerca */
}
function cem_dieta_briciole(pasto){
  var pars = 'pasto='+pasto;
	new Ajax.Updater(
		'CEM_traccia_contenitore',
		'/contoemangio/cem_briciole_dieta.php', {
		parameters: pars,
		asynchronous:true, evalScripts:true
		}
	);	
	//new Effect.Appear('panel');
}

/************************* SOBERE? **********************************/

function SBvalidate(form) {

  var anno = $('anno').value;
  var peso = $('peso').value;
  var bevanda1 = $('bevanda1').value;
  var bevanda2 = $('bevanda2').value;
  var bevanda3 = $('bevanda3').value;
  var quant1=$('quant1').value;
  var quant2=$('quant2').value;
  var quant3=$('quant3').value;
  var data = new Date();
  var questanno = data.getFullYear();
  var fadetime='3';
  //var email=$('email').value;
  //var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
  var numRegex = /^[0-9]$/;
  if(anno == "" || anno ==0) {
    inlineMsg('anno','qual\'&eacute; il tuo anno di nascita?',fadetime);
    return false;
  }
  if(anno < 1900 || anno > questanno) {
    inlineMsg('anno','verifica il corretto inserimento del valore (es. 1980)',fadetime);
    return false;
  }
  if (isNaN(parseInt(anno))) {
      inlineMsg('anno','non &eacute; un valore numerico.',fadetime);
      return false;
  } 
  if(peso == "" || peso ==0) {
    inlineMsg('peso','quanto pesi?',fadetime);
    return false;
  }
  if(peso < 20 || peso > 500) {
    inlineMsg('peso','sei sicuro?',fadetime);
    return false;
  }
  if (isNaN(parseInt(peso))) {
      inlineMsg('peso','non &eacute; un valore numerico.',fadetime);
      return false;
  }   
  if(bevanda1 == "" && bevanda2 == "" && bevanda3 == "") {
    inlineMsg('bevanda1','Cosa hai bevuto?',fadetime);
    return false;
  }
  if(bevanda1 != "" && quant1 == "0") {
    inlineMsg('quant1','quanto?',fadetime);
    return false;
  }
  if(bevanda1 != "" && isNaN(parseInt(quant1))) {
      inlineMsg('quant1','non &eacute; un valore numerico.',fadetime);
    return false;
  }
  if(bevanda2 != "" && quant2 == "0") {
    inlineMsg('quant2','quanto?',fadetime);
    return false;
  }
  if(bevanda3 != "" && quant3 == "0") {
    inlineMsg('quant3','quanto?',fadetime);
    return false;
  }
  //return true;
  SB_responso();
  return false;
}
function SB_responso(){
	var params = Form.serialize($('SBform')); 
	new Ajax.Updater(
		'SB_responso', 
		'/sobere/responsoSoBere.php', {
		parameters: params,
		method: 'get',
  		onLoading:function(){
        new Effect.Appear('SB_responso');
        $('SB_responso').update('<div class="orangeloader"></div>');
  		}, 
  		 onComplete:function(){
          //new Effect.Highlight('SB_responso');
          new Effect.ScrollTo('SB_responso', {offset: -24, duration:2.5});
  		 },			
		asynchronous:true, evalScripts:true
		}
	);	
	//new Effect.Appear('panel');
}

function SB_win(page){
    //alertSound();
		var scrollx = window.pageXOffset || document.body.scrollLeft || document.documentElement.scrollLeft;
		var scrolly = window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop;	
    var my_width  = 0;
    var my_height = 0;
		prepareIE("100%", "hidden"); // Preparing IE 6 for showing modalbox	
	if (window.innerHeight && window.scrollMaxY) {// Firefox
		my_height = window.innerHeight + window.scrollMaxY;
		my_width = window.innerWidth + window.scrollMaxX;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		my_height = document.body.scrollHeight;
		my_width = document.body.scrollWidth;
	} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
		my_height = document.body.offsetHeight;
		my_width = document.body.offsetWidth;
  	}    
    halfheight=(my_height/2);
    
    if(my_height==0 || my_height==''){my_height='1200';}
  $('overlay').setStyle({
    height: my_height+ 'px',
    width: my_width+ 'px'
  });
  $('popup').setStyle({
  top: '0',
  marginTop: '20px'
  });
  //$('overlay').show();
  toggleappear('overlay');
  toggleappear('popup');
  $('popup').update(''); 
  	new Ajax.Updater(
  		'popup',
  		'/sobere/'+page+'.php', {
  		//onLoading:function(){
      //  $('popup').update('attendere');
  		//}, 
  		onComplete:function(){
    		$('scrollx').value=scrollx;
    		$('scrolly').value=scrolly; 
        },        
  		asynchronous:true, evalScripts:true
  		}
  	);  	
}


/** TEST **/
/*
function test_domanda(codice){
  $('domanda').update('<div class="orangeloader"></div>');
  var pars = 'codice='+codice;
  	new Ajax.Updater(
  		'domanda',
  		'/sobere/test_domanda.php', {
		  parameters: pars,
		  method: 'get',
  		//onLoading:function(){
      //  $('domanda').update('<div class="orangeloader"></div>');
  		//}, 
  		//onComplete:function(){
      //  },        
  		asynchronous:true, evalScripts:true
  		}
  	); 
}
function test_risposta(codice){
  $('risposta').update('<div class="orangeloader"></div>');
  var pars = 'codice='+codice;
  	new Ajax.Updater(
  		'risposta',
  		'/sobere/test_risposta.php', {
		  parameters: pars,
		  method: 'get',
  		//onLoading:function(){
      //  $('risposta').update('<div class="orangeloader"></div>');
  		//}, 
  		//onComplete:function(){
      //  },        
  		asynchronous:true, evalScripts:true
  		}
  	); 
}
*/

function test_prosegui(SoN){
  if(SoN=='s' || SoN=='s1'){
    $('risposta').show();
  }else{
    $('risposta').hide();
  }
  if(SoN=='s1'){$('SBtest_continua_button').value = "Continua il test per scoprire quanto puņ bere un adulto";}
  if(SoN=='h1'){$('SBtest_continua_button').value = "Continua il test";}
}

function test_valid(){
  var check1=$('check1');
  var check2=$('check2');
  if(check1.checked==false && check2.checked==false ){
    inlineMsg('check2','Seleziona una risposta per proseguire.',2);
    return false;
  }  
  return true; 
}
/************************* MANGIOMEGLIO **********************************/

function MMvalidate(form) {
  var email=$('email').value;
  var cell=$('cell').value;
  var nocell=$('nocell').value;
  var anno=$('anno').value;
  var peso = $('peso').value;
  var altezza=$('altezza').value;
  //var user=$('user').value;
  var password=$('password').value;
  var password2=$('password2').value;
  var accetta=$('accetta');

  var fadetime='4';
  var valid=true;
  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
  var numRegex = /^[0-9 +()]+$/; //accetta solo numeri, + e spazi, ad es: (+39) 123 456 789
  var alphanumRegex = /^[a-zA-Z0-9]+$/;
  email = email.trim();

  if(email == "") {
    inlineMsg('email','Inserisci la tua email, ti servir&agrave; per completare la registrazione e per ricevere feedback ed informazioni!',fadetime);
    return false;
  }else{
    if(!email.match(emailRegex)) {
      inlineMsg('email','Formato email non valido.',fadetime);
      return false;
    }else{  
      new Ajax.Request(
          '/mangiomeglio/checkuser.php', {
          method: 'get',
          parameters: { email: $('email').value },
          onSuccess: function(transport) { 
          var response = transport.responseText;
            if(response == 'unavailable'){
              inlineMsg('email','Questa email risulta gi&agrave; registrata, indicane un\'altra',fadetime);
              valid=false;
            }
          },	
    		asynchronous:false, evalScripts:false
      });
    }
  }
  if(cell == "" && nocell == "") {
    inlineMsg('cell','Anche se non &egrave; obbligatorio, indicando il numero di tel cellulare potrai ricevere feedback ed informazioni via sms!',fadetime);
    $('nocell').value = 'no';
    return false;
  }else{
    if(cell != "" && !cell.match(numRegex)) {
      inlineMsg('cell','Formato non valido: i caratteri consentiti sono valori numerici, spazi, + e ().',fadetime);
      return false;
    }else{  
      new Ajax.Request(
          '/mangiomeglio/checkuser.php', {
          method: 'get',
          parameters: { cell: $('cell').value },
          onSuccess: function(transport) { 
          var response = transport.responseText;
            if(response == 'unavailable'){
              inlineMsg('cell','Questo numero risulta gi&agrave; registrato',fadetime);
              valid=false;
            }
          },	
    		asynchronous:false, evalScripts:false
      });
    }
  } 
  //if (isNaN(parseInt(cell))) {
  //    inlineMsg('cell','non &eacute; un valore numerico.',fadetime);
  //    return false;
  //} 
  if(anno == "") {
    inlineMsg('anno','In quale anno sei nato?',fadetime);
    return false;
  }
  if (isNaN(parseInt(anno))) {
      inlineMsg('anno','non &eacute; un valore numerico.',fadetime);
      return false;
  }  
  if(anno < 1900 || anno > 2010) {
    inlineMsg('anno','Sei sicuro?',fadetime);
    return false;
  }    
  if(peso == "" || peso ==0) {
    inlineMsg('peso','quanto pesi?',fadetime);
    return false;
  }
  if(peso < 20 || peso > 500) {
    inlineMsg('peso','sei sicuro?',fadetime);
    return false;
  }
  if (isNaN(parseInt(peso))) {
      inlineMsg('peso','non &eacute; un valore numerico.',fadetime);
      return false;
  } 
  if(altezza == "" || altezza == 0) {
    inlineMsg('altezza','Quanto sei alto?',fadetime);
    return false;
  }      
  if(altezza < 50 || altezza > 300 ) {
    inlineMsg('altezza','Sei sicuro?',fadetime);
    return false;
  } 
  if (isNaN(parseInt(altezza))) {
      inlineMsg('altezza','non &eacute; un valore numerico.',fadetime);
      return false;
  }
  /*
  if(user == "") {
    inlineMsg('user','Specifica una username',fadetime);
    return false;
  }else{
    if(!user.match(alphanumRegex)) {
      inlineMsg('user','Formato user non valido: utilizza solo caratteri alfanumerici.',fadetime);
      return false;
    }else{ 
      new Ajax.Request(
          '/mangiomeglio/checkuser.php', {
          method: 'get',
          parameters: { username: $('user').value },
          onSuccess: function(transport) { 
          var response = transport.responseText;
            if(response == 'unavailable'){
              inlineMsg('user','Questa username risulta gi&agrave; registrata, prova a sceglierne un\'altra',fadetime);
              valid=false;
            }
          },	
    		asynchronous:false, evalScripts:false
      });
    }
  }
  if(user.length < 5) {
    inlineMsg('user','La username deve contenere almeno 5 caratteri',fadetime);
    return false;
  }
  */
  if(password == "") {
    inlineMsg('password','Non hai indicato la password',fadetime);
    return false;
  }
  if(password.length < 5) {
    inlineMsg('password','La password deve contenere almeno 5 caratteri',fadetime);
    return false;
  }
    if(!password.match(alphanumRegex)) {
      inlineMsg('password','Formato password non valido: utilizza solo caratteri alfanumerici.',fadetime);
      return false;
    }  
  if(password2 == "") {
    inlineMsg('password2','Non hai confermato la password',fadetime);
    return false;
  }
  if(password != password2) {
    inlineMsg('password2','Le password non coincidono.',5);
    return false;
  } 
  if (accetta.checked==false) {
    inlineMsg('accetta','E\' necessario acconsentire al trattamento dei dati personali.',5);
    return false;
  }   
   return valid;
  //MM_responso();
}

/* validazione modifica utente */
function MMvalidate_edit(form) {
  var edituser=$('edituser').value;
  var email=$('email').value;
  var cell=$('cell').value;
  var nocell=$('nocell').value;
  var anno=$('anno').value;
  var peso = $('peso').value;
  var altezza=$('altezza').value;
  var fadetime='4';
  var valid=true;
  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
  var numRegex = /^[0-9 +()]+$/; //accetta solo numeri, + e spazi, ad es: (+39) 123 456 789
  var alphanumRegex = /^[a-zA-Z0-9]+$/;

  if(email == "") {
    inlineMsg('email','Inserisci la tua email, ti servir&agrave; per completare la registrazione e per ricevere feedback ed informazioni!',fadetime);
    return false;
  }else{
    if(!email.match(emailRegex)) {
      inlineMsg('email','Formato email non valido.',fadetime);
      return false;
    }else{  
	    var pars = 'email='+email+'&edituser='+edituser;
      new Ajax.Request(   
          '/mangiomeglio/checkuser.php', {
          method: 'get',
		      parameters: pars,   
          //parameters: { email: $('email').value },
          onSuccess: function(transport) { 
          var response = transport.responseText;
            if(response == 'unavailable'){
              inlineMsg('email','Questa email risulta gi&agrave; registrata, indicane un\'altra',fadetime);
              valid=false;
            }
          },	
    		asynchronous:false, evalScripts:false
      });
    }
  }
  if(cell == "" && nocell == "") {
    inlineMsg('cell','Anche se non &egrave; obbligatorio, indicando il numero di tel cellulare potrai ricevere feedback ed informazioni via sms!',fadetime);
    $('nocell').value = 'no';
    return false;
  }else{
    if(cell != "" && !cell.match(numRegex)) {
      inlineMsg('cell','Formato non valido: i caratteri consentiti sono valori numerici, spazi, + e ().',fadetime);
      return false;
    }else{  
	    var pars = 'cell='+cell+'&edituser='+edituser;
      new Ajax.Request(
          '/mangiomeglio/checkuser.php', {
          method: 'get',
          parameters: pars,   
          //parameters: { cell: $('cell').value, edituser: $('edituser').value },
          onSuccess: function(transport) { 
          var response = transport.responseText;
            if(response == 'unavailable'){
              inlineMsg('cell','Questo numero risulta gi&agrave; registrato',fadetime);
              valid=false;
            }
          },	
    		asynchronous:false, evalScripts:false
      });
    }
  } 
  //if (isNaN(parseInt(cell))) {
  //    inlineMsg('cell','non &eacute; un valore numerico.',fadetime);
  //    return false;
  //} 
  if(anno == "") {
    inlineMsg('anno','In quale anno sei nato?',fadetime);
    return false;
  }
  if (isNaN(parseInt(anno))) {
      inlineMsg('anno','non &eacute; un valore numerico.',fadetime);
      return false;
  }  
  if(anno < 1900 || anno > 2010) {
    inlineMsg('anno','Sei sicuro?',fadetime);
    return false;
  }    
  if(peso == "" || peso ==0) {
    inlineMsg('peso','quanto pesi?',fadetime);
    return false;
  }
  if(peso < 20 || peso > 500) {
    inlineMsg('peso','sei sicuro?',fadetime);
    return false;
  }
  if (isNaN(parseInt(peso))) {
      inlineMsg('peso','non &eacute; un valore numerico.',fadetime);
      return false;
  } 
  if(altezza == "" || altezza == 0) {
    inlineMsg('altezza','Quanto sei alto?',fadetime);
    return false;
  }      
  if(altezza < 50 || altezza > 300 ) {
    inlineMsg('altezza','Sei sicuro?',fadetime);
    return false;
  } 
  if (isNaN(parseInt(altezza))) {
      inlineMsg('altezza','non &eacute; un valore numerico.',fadetime);
      return false;
  }  
   return valid;
}

/* validazione modifica password */
function MMvalidate_newpasswd(form) {
  var password=$('password').value;
  var password2=$('password2').value;

  var fadetime='3';
  var valid=true;
  var alphanumRegex = /^[a-zA-Z0-9]+$/;

  if(password == "") {
    inlineMsg('password','Non hai indicato la password',fadetime);
    return false;
  }
  if(password.length < 5) {
    inlineMsg('password','La password deve contenere almeno 5 caratteri',fadetime);
    return false;
  }
    if(!password.match(alphanumRegex)) {
      inlineMsg('password','Formato password non valido: utilizza solo caratteri alfanumerici.',fadetime);
      return false;
    }  
  if(password2 == "") {
    inlineMsg('password2','Non hai confermato la password',fadetime);
    return false;
  }
  if(password != password2) {
    inlineMsg('password2','Le password non coincidono.',5);
    return false;
  } 
   return valid;
}
/* form per il recupero della password dimenticata */
function MMvalidate_email(form) {
  var email=$('email').value;
  email = email.trim();
  var fadetime='3';
  var valid=true;
  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;

  if(email == "") {
    inlineMsg('email','Inserisci la tua email, ti servir&agrave; per ricevere la password!',fadetime);
    return false;
  }else{
    if(!email.match(emailRegex)) {
      inlineMsg('email','Formato email non valido.',fadetime);
      return false;
    }else{
      new Ajax.Request(
          '/mangiomeglio/checkuser.php', {
          method: 'get',
          parameters: { email: $('email').value },
          onSuccess: function(transport) {
          var response = transport.responseText;
            if(response == 'available'){
              inlineMsg('email','L\'email indicata non &egrave; stata trovata, controlla che sia corretta.',fadetime);
              valid=false;
            }
          },
    		asynchronous:false, evalScripts:false
      });
    }
  }
   return valid;
}
/* form di accesso ai servizi */
function MMvalidate_access() {
  var email=$('email').value;
  var password=$('password').value;
  var fadetime='3';
  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
  var valid=true;
  email = email.trim();
  if(email == "") {
    inlineMsg('email','Inserisci la tua email.',fadetime);
    return false;
  }else{
    if(!email.match(emailRegex)) {
      inlineMsg('email','Formato email non valido.',fadetime);
      return false;
    }else{  
      new Ajax.Request(
          '/mangiomeglio/checkuser.php', {
          method: 'get',
          parameters: { email: $('email').value },
          onSuccess: function(transport) { 
          var response = transport.responseText;
            if(response == 'available'){
              inlineMsg('email','L\'email indicata non &egrave; stata trovata, controlla che sia corretta.',fadetime);
              valid=false;
            }
          },	
    		asynchronous:false, evalScripts:false
      });
    }
  }
  if(password == "") {
    inlineMsg('password','Non hai indicato la password',fadetime);
    return false;
  }
   return valid;
}

/* inserimento alimenti */
function MM_insert1(){
  var alim=$('alim').value;
  var porz_alim=$('porz_alim').value;
  var pasto=$('pasto').value;
  var giorno=$('giorno').value;
  var data=$('data').value;
  //var fadetime='3';

  if(alim == "") {
    //inlineMsg('alim','Seleziona un alimento',fadetime);
    alert('Seleziona un alimento');
    return false;
  }
  if(porz_alim == "") {
    //inlineMsg('porz_alim','Seleziona la porzione',fadetime);
    alert('Seleziona la porzione');
    return false;
  }
  var pars = 'alim='+alim+'&porz_alim='+porz_alim+'&pasto='+pasto+'&data='+data+'&giorno='+giorno;
	new Ajax.Updater(
		'tr_alim', 
		'/mangiomeglio/aggiungi.php', {
		parameters: pars,
		method: 'get',
  		//onLoading:function(){
        //new Effect.Appear('SB_responso');
        //$('SB_responso').update('<div class="orangeloader"></div>');
          //Modalbox.resizeToInclude('td_sel');
  		//}, 
/*
          onComplete: function(transport) { 
          var response = transport.responseText;
            if(response == '0'){
              //new Element.remove(trID);
            }
            if(response != 0){
              $('p'+response).update('xxx'); 
            }
          },
*/  		
  		 onComplete:function(){
          //Modalbox.resizeToContent();
        //$('sp4').setStyle({backgroundColor: '#000000'});
        $(pasto+''+data).className = 'si_mangiomeglio';
        $('a'+pasto+''+data).update('');   
        reportSettLink();
        if($('MM_nessunpasto').style.display!='none'){
          new Effect.Fade('MM_nessunpasto');
        }
  		 },			
  	insertion: Insertion.Before,
		asynchronous:true, evalScripts:true
		}
	);	
	//new Effect.Appear('panel');
}

/* inserimento pasto nullo */
function MM_insert_null(){
  var pasto=$('pasto').value;
  var giorno=$('giorno').value;
  var data=$('data').value;
  var pars = 'pasto='+pasto+'&data='+data+'&giorno='+giorno;
	new Ajax.Updater(
		'tr_alim', 
		'/mangiomeglio/digiuno.php', {
		parameters: pars,
		method: 'get', 		
  		 onComplete:function(){
  		  if(pasto=='co' || pasto=='pr' || pasto=='ce' ){
          $(pasto+''+data).className = 'null_mangiomeglio';
        }else{
          $(pasto+''+data).className = 'no_mangiomeglio';
        }          
        reportSettLink();
        $('a'+pasto+''+data).update('Non ho<br />mangiato nulla'),
        $('MM_nessunpasto1').hide(),
        $('MM_nessunpasto2').show();
  		 },			
  	insertion: Insertion.Before,
		asynchronous:true, evalScripts:true
		}
	);	
	//new Effect.Appear('panel');
}




/* inserimento bevande */
function MM_insert2(){
  var bev=$('bev').value;
  var porz_bev=$('porz_bev').value;
  var pasto=$('pasto').value;
  var giorno=$('giorno').value;
  var data=$('data').value;
  //var fadetime='3';
  if(bev == "") {
    //inlineMsg('bev','Seleziona una bevanda',fadetime);
    alert('Seleziona una bevanda');
    return false;
  }
  if(porz_bev == "") {
    //inlineMsg('porz_bev','Seleziona la porzione',fadetime);
    alert('Seleziona la porzione');
    return false;
  }
  var pars = 'bev='+bev+'&porz_bev='+porz_bev+'&pasto='+pasto+'&data='+data+'&giorno='+giorno;
	new Ajax.Updater(
		'tr_bev', 
		'/mangiomeglio/aggiungi.php', {
		parameters: pars,
		method: 'get',
  		 onComplete:function(){
          //Modalbox.resizeToContent();
        //$('sp4').setStyle({backgroundColor: '#000000'});
        $(pasto+''+data).className = 'si_mangiomeglio';
        $('a'+pasto+''+data).update(''); 
        reportSettLink();
        if($('MM_nessunpasto').style.display!='none'){
          new Effect.Fade('MM_nessunpasto');
        }
  		 },				
  	insertion: Insertion.Before,asynchronous:true, evalScripts:true
		}
	);	
}



/* finestra di popup inserimenti giornalieri */
function MMdaily(pasto,giorno,data){
		var scrollx = window.pageXOffset || document.body.scrollLeft || document.documentElement.scrollLeft;
		var scrolly = window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop;	
    var my_width  = 0;
    var my_height = 0;

		prepareIE("100%", "hidden"); // Preparing IE 6 for showing modalbox

	if (window.innerHeight && window.scrollMaxY) {// Firefox
		my_height = window.innerHeight + window.scrollMaxY;
		my_width = window.innerWidth + window.scrollMaxX;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		my_height = document.body.scrollHeight;
		my_width = document.body.scrollWidth;
	} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
		my_height = document.body.offsetHeight;
		my_width = document.body.offsetWidth;
  	}    
    halfheight=(my_height/2);
    
    if(my_height==0 || my_height==''){my_height='1200';}
  $('overlay').setStyle({
    height: my_height+ 'px',
    width: my_width+ 'px'
  });
  $('popup').setStyle({
  top: '0',
  marginTop: '20px'
  });
  //$('overlay').show();
  toggleappear('overlay');
  toggleappear('popup');
  //new Effect.Appear('popup');
  $('popup').update(''); 
 	MMdailyupdate(pasto,giorno,data);
}
function MMdailyupdate(pasto,giorno,data){
  var pars = 'pasto='+pasto+'&data='+data+'&giorno='+giorno;
  	new Ajax.Updater(
  		'popup',
  		'/mangiomeglio/giornaliero.php', {
		  parameters: pars,
		  method: 'get',
  		onLoading:function(){
      //  $('popup').update('attendere');
        $('box3D').startWaiting('waitingLayer');
  		}, 
  		onComplete:function(){
        checkDisabledOptions();
    		$('scrollx').value=scrollx;
    		$('scrolly').value=scrolly; 
        $('box3D').stopWaiting(); 
        },        
  		asynchronous:true, evalScripts:true
  		}
  	); 
}
function MMdailyGo(pasto,giorno,data){
  //$('box3D').update('Attendere');
 // $('box3').morph('height:500px;');
 
 
 	MMdailyupdate(pasto,giorno,data);
}
function MMdailyDelete(trID){
  if (confirm('Sei sicuro?')) {
	new Effect.Pulsate(trID,{duration:1.2, from:0.5, pulses:3});
  var pars = 'trID='+trID;
  var splitted=trID.split('_');
  var pasto = splitted[1];
  var data = splitted[2];   
  //alert('pasto: '+pasto+' data: '+data);
      new Ajax.Request(   
          '/mangiomeglio/delete.php', {
          method: 'get',
		      parameters: pars,   
          onComplete: function(transport) { 
          var response = transport.responseText;
            if(response == 'deleted'){
              new Element.remove(trID);
            }
            if(response == 'deletedAll'){
              new Element.remove(trID);
              //new Effect.Fade(trID);
              $(pasto+''+data).className = 'no_mangiomeglio';
              $('a'+pasto+''+data).update('Non<br />compilato');
              $('MM_nessunpasto1').show(),
              $('MM_nessunpasto2').hide();              
              new Effect.Appear('MM_nessunpasto');
            }
          reportSettLink();
          },
    		asynchronous:true, evalScripts:true
      });
  } 
}
function checkDisabledOptions(){
  if (Prototype.Browser.IE){
    var sels = document.getElementsByTagName('select');
    for(var i=0; i < sels.length; i++){
    if(sels[i].options[sels[i].selectedIndex].disabled){
      sels[i].onchange();
    } 
    for(var j=0; j < sels[i].options.length; j++){ //colocando o estilo
      if(sels[i].options[j].disabled){
        sels[i].options[j].style.color = '#999';
      }
    }
    sels[i].onchange= function(){
      if(this.options[this.selectedIndex].disabled){
        if(this.options.length<=1){
          this.selectedIndex = -1;
        }else if(this.selectedIndex < this.options.length - 1){
        do {
          //this.selectedIndex++;
          this.selectedIndex--;
        } while(this.options[this.selectedIndex].disabled && this.selectedIndex < (this.options.length-1));
        if(this.options[this.selectedIndex].disabled) {
          do {
            this.selectedIndex--;
          } while(this.options[this.selectedIndex].disabled && this.selectedIndex > -2);
        }
        }else{
          do {
            this.selectedIndex--;
          } while(this.options[this.selectedIndex].disabled && this.selectedIndex > -2);
        }
      }
    }
    }
  }
}
function MMreportSett(){
  //var pars = 'pasto='+pasto+'&data='+data+'&giorno='+giorno;
  	new Ajax.Updater(
  		'reportSett',
  		'/mangiomeglio/reportSett.php', {
		  //parameters: pars,
		  method: 'get',
  		onLoading:function(){
        $('reportSett').update('<div class="reportSett">Generazione del report in corso...</div>');
  		}, 
  		//onComplete:function(){
      //  },        
  		asynchronous:true, evalScripts:true
  		}
  	); 
}

/* link per la generazione delreport settimanale */
function reportSettLink(){
  	new Ajax.Updater(
  		'reportSettLink',
  		'/mangiomeglio/reportSettLink.php', {
		  method: 'get',      
  		asynchronous:true, evalScripts:true
  		}
  	); 
}
function MMdailyreset(){
  if (confirm('Sei sicuro di voler annullare gli inserimenti di questa settimana?')) {
	 return true;
	}else{
    return false;
  }
}
/*************** VIDEO ***********************/
function video(video){
  var pars = 'video='+video;
  	new Ajax.Updater(
  		'SMvideo',
  		'/videobox.php', {
		  parameters: pars,
		  method: 'get',
  		//onLoading:function(){
      //  $('SMvideo').update('<div class="video_font">SAPERMANGIARE IN VIDEO </div>');
  		//}, 
  		//onComplete:function(){
      //  },        
  		asynchronous:true, evalScripts:true
  		}
  	); 
}
/*************** Glossario ***********************/
function glossario(lettera){
  var pars = 'lettera='+lettera;
  	new Ajax.Updater(
  		'glossario',
  		'/glossario_termini.php', {
		  parameters: pars,
		  method: 'get',
  		onLoading:function(){
        $('glossario').update('<div class="orangeloader"></div>');
  		}, 
  		//onComplete:function(){
      //  },        
  		asynchronous:true, evalScripts:true
  		}
  	); 
}


/* ************ Modulo Contatti **************** */
function ContactValid(form) {
  var nome=$('nome').value;
  var cognome=$('cognome').value;
  var citta=$('citta').value;
  var email=$('email').value;
  var messaggio=$('messaggio').value;

  var fadetime='4';
  var valid=true;
  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
  email = email.trim();
  
  if(nome == "") {
    inlineMsg('nome','Qual\'e\' il tuo nome?',fadetime);
    return false;
  }
  if(cognome == "") {
    inlineMsg('cognome','Qual\'e\' il tuo cognome?',fadetime);
    return false;
  }
  if(citta == "") {
    inlineMsg('citta','Qual\'e\' la tua citta\'?',fadetime);
    return false;
  }
  if(email == "") {
    inlineMsg('email','Inserisci la tua email.',fadetime);
    return false;
  }else{
    if(!email.match(emailRegex)) {
      inlineMsg('email','Formato email non valido.',fadetime);
      return false;
    }
  }
  if(messaggio == "") {
    inlineMsg('messaggio','Non hai specificato alcun messaggio',fadetime);
    return false;
  }

   return true;
}

