/**
function lnkRedirect(strLink){
	location.href = strLink;
	//window.open(strLink);
}

function lnkRedirectNew(strLink){
	nuova=window.open(strLink);
} 
**/
function getBrowserWidth(){
    if (window.innerWidth){
        return window.innerWidth;}  
    else if (document.documentElement && document.documentElement.clientWidth != 0){
        return document.documentElement.clientWidth;    }
    else if (document.body){return document.body.clientWidth;}      
        return 0;
}

  function getArticoli(categoria,sezione,start,cid) {
    var item = '';
	switch (categoria) {
		case "News":
		case "Eventi":
			item = '#l' + categoria;
			break;
		default:
			item = '#listaArticoli';
			break;
	}
	$(item).load('/dynamic/get_articoli.php?categoria=' + escape(categoria) + "&sezione=" + escape(sezione) + "&start=" + start + "&cid=" + cid);
  }
  
  function saveemail() {
    var email = $('#email').val();
    if (!testEmail(email,true)) {
      alert("Email non valida.");
    } else {      
      txtCont = "/stubs/galbuseraframe.php?k=" + escape(email);
      Shadowbox.open({
        player:     'iframe',
        title:      'Newsletter',
        content:    txtCont,
        height:     288,
        width:      595
      });
    }
  }
  
  function testEmail(oggTextfield, boolObbligatorio) {
    oggTextfield = oggTextfield.replace(/\s+$|^\s+/g,"")
    if (oggTextfield == "")
      if (boolObbligatorio)
        return false
      else
        return true
      var rex = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
      var risultato = rex.test(oggTextfield);
      return risultato
  }
  
function isEmail(s) {
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if(reg.test(s) == false) {
	  return false;
	}
	return true;
}
 
/*
 * Voti agli articoli
 */
 
function enableStars() {
	$('a.starvote').hover(
		function() {
			var $title = $(this).attr('title');
			for (var i=1;i<6;i++) {
				if (i <= $title) {
					$('#s' + i + " img").attr('src', '/images/vivere/star_on.png');
				} else {
					$('#s' + i + " img").attr('src', '/images/vivere/star_off.png');
				}
			}
			switch ($title) {
				case "1":
					$('#votedescription').html('Scarso');
					break;
				case "2":
					$('#votedescription').html('Niente di speciale');
					break;
				case "3":
					$('#votedescription').html('Degno di nota');
					break;
				case "4":
					$('#votedescription').html('Bello');
					break;
				case "5":
					$('#votedescription').html('Strepitoso!');
					break;
				default:
					$('#votedescription').html('???');
					break;
			}
		},
		function () {
			void(0);
		}
	);
	$('a.starvote').click(function() {
		disableStars();
		var $id = $('#id').val();
		var $vote = $(this).attr('title');
		var $data = {
			id : $id,
			vote : $vote
		};
		$('#votedescription').load('/vivereinarmonia/ajax-sendvote.php',$data);
	});
}

function disableStars() {
	$('.starvote').unbind('hover');
	if ($('#votedescription').html() != 'Il tuo voto è stato salvato, grazie!' && $('#votedescription').html() != 'Hai già votato questo articolo.' && $('#votedescription').html() != 'Impossibile salvare il voto.') {
		$('#votedescription').html('');
	}
}

function setupVoti() {
	var votestatus = $('#votearea').html();
	$('#votearea').hover(
		function() {
			enableStars();
		},
		function() {
			disableStars();
			$('#votearea').html(votestatus);
		}
	);
} 
 
/*
 * Form commenti
 */
  
 function commentoCallback(data) {
	if (data.result) {
		$('.commenti-lista ul li:last').after(data.html);
		$('#commento').val('');
		$('.nocomment').hide();
	} else {
		$('#errorMessage').html(data.error);
	}
	$('#formCommenti').show();
	$('#saveMessage').hide();
 }
  
 function setupFormCommenti() {
	$('#formCommenti').submit(
		function(e) {
			e.preventDefault();
			var $error = false;
			var $id = $('#id').val();
			var $nome = $('#nome').val();
			var $email = $('#email').val();
			var $url = $('#url').val();
			var $commento = $('#commento').val();
			var $thispage = $('#thispage').val();
			if ($nome == '') {
				$('#nome').addClass('error');
				$error = true;
			}
			if (!isEmail($email)) {
				$('#email').addClass('error');
				$error = true;
			}
			if ($commento == '') {
				$('#commento').addClass('error');
				$error = true;
			}
			if ($error) {
				$('#errorMessage').html('<p>Impossibile salvare il tuo commento verifica i campi evidenziati.</p>');
			} else {
				$('#formCommenti').hide();
				$('#saveMessage').show();
				var $data = {
					action: 'save',
					id: $id,
					nome: $nome,
					email: $email,
					url: $url,
					commento: $commento,
					thispage: $thispage
				}
				$.post('/vivereinarmonia/ajax-comment.php', $data, commentoCallback, 'json');				
			}
		}
	);
 }
 
 
 function checkNostraDomanda(num) {
	var error = "";
	if (num==1) {
		var err1 = false, err2 = false;

		if ( $('input[name=domanda-1-1]:checked').val() == undefined) {
			error += '<li>&Egrave; necessario rispondere alla domanda 1.</li>';
			$('#domanda1').addClass('error');
			err1 = true;			
		}
		if (error=="") {
			showNostraDomanda(num+1);
		} else {
			$('#error1').html("<ul>" + error + "</ul>");
			$('#error1').show('fast');
			if (err1==true) {
				$('#domanda1').addClass('error');
			} else {
				$('#domanda1').removeClass('error');
			}
		}
	} else if (num==2) {
		var err1 = false, err2 = false, err3 = false;
		if ( $('input[name=domanda-2-1]:checked').val() == undefined) {
			error += '<li>&Egrave; necessario rispondere alla domanda 2.</li>';
			err2 = true;
		}
		
		if (err1==false) {
			if ( $('input[name=domanda-3-1]:checked').val() == undefined) {
				error += '<li>&Egrave; necessario rispondere alla domanda 3.</li>';
				err2 = true;
			}
		}
		
		if ( $('input[name=domanda-4-1]:checked').val() == undefined) {
			error += '<li>&Egrave; necessario rispondere alla domanda 4.</li>';
			err2 = true;
		}
		
		if ( $('input[name=domanda-5-1]:checked').val() == undefined) {
			error += '<li>&Egrave; necessario rispondere alla domanda 5.</li>';
			err2 = true;
		}
		if (error=="") {
			$val = $('input[name=domanda-5-1]:checked').val();
			if($val=='attenzione ai grassi') 
				showNostraDomanda(3);
			if($val=='senze zuccheri aggiunti') 
				showNostraDomanda(4);
			if($val=='senza glutine') 
				showNostraDomanda(5);
			if($val=='equilibrio e bonta') 
				showNostraDomanda(6);
			if($val=='tra') 
				showNostraDomanda(7);
		} else {
			$('#error2').html("<ul>" + error + "</ul>");
			$('#error2').show('fast');
			if (err1==true) {
				$('#domanda3').addClass('error');
			} else {
				$('#domanda3').removeClass('error');
			}
			if (err2==true) {
				$('#domanda4').addClass('error');
			} else {
				$('#domanda4').removeClass('error');
			}
			if (err3==true) {
				$('#domanda5').addClass('error');
			} else {
				$('#domanda5').removeClass('error');
			}
		}
	} else if (num==3){
		var err1 = false;
		if ( $('input[name=domanda-6-1]:checked').val() == undefined) {
			error += '<li>&Egrave; necessario rispondere alla domanda 6.</li>';
			err1 = true;
		}
		if ( $('input[name=domanda-7-1]:checked').val() == undefined) {
			error += '<li>&Egrave; necessario rispondere alla domanda 7.</li>';
			err1 = true;
		}
		if ( $('input[name=domanda-8-1]:checked').val() == undefined) {
			error += '<li>&Egrave; necessario rispondere alla domanda 8.</li>';
			err1 = true;
		}
		if (error=="") {
			submitDomanda();
		} else {
			$('#error3').html("<ul>" + error + "</ul>");
			$('#error3').show('fast');
			if (err1==true) {
				$('#domanda6').addClass('error');
			} else {
				$('#domanda6').removeClass('error');
			}
		}
	}else if (num==4){
		var err1 = false;
		if ( $('input[name=domanda-6-2]:checked').val() == undefined) {
			error += '<li>&Egrave; necessario rispondere alla domanda 6.</li>';
			err1 = true;
		}
		if ( $('input[name=domanda-7-2]:checked').val() == undefined) {
			error += '<li>&Egrave; necessario rispondere alla domanda 7.</li>';
			err1 = true;
		}
		if ( $('input[name=domanda-8-2]:checked').val() == undefined) {
			error += '<li>&Egrave; necessario rispondere alla domanda 8.</li>';
			err1 = true;
		}
		if (error=="") {
			submitDomanda();
		} else {
			$('#error4').html("<ul>" + error + "</ul>");
			$('#error4').show('fast');
			if (err1==true) {
				$('#domanda6').addClass('error');
			} else {
				$('#domanda6').removeClass('error');
			}
		}
	}else if (num==5){
		var err1 = false;
		if ( $('input[name=domanda-6-3]:checked').val() == undefined) {
			error += '<li>&Egrave; necessario rispondere alla domanda 6.</li>';
			err1 = true;
		}
		if ( $('input[name=domanda-7-3]:checked').val() == undefined) {
			error += '<li>&Egrave; necessario rispondere alla domanda 7.</li>';
			err1 = true;
		}
		if ( $('input[name=domanda-8-3]:checked').val() == undefined) {
			error += '<li>&Egrave; necessario rispondere alla domanda 8.</li>';
			err1 = true;
		}
		if (error=="") {
			submitDomanda();
		} else {
			$('#error5').html("<ul>" + error + "</ul>");
			$('#error5').show('fast');
			if (err1==true) {
				$('#domanda6').addClass('error');
			} else {
				$('#domanda6').removeClass('error');
			}
		}
	}else if (num==6){
		var err1 = false;
		if ( $('input[name=domanda-6-4]:checked').val() == undefined) {
			error += '<li>&Egrave; necessario rispondere alla domanda 6.</li>';
			err1 = true;
		}
		if ( $('input[name=domanda-7-4]:checked').val() == undefined) {
			error += '<li>&Egrave; necessario rispondere alla domanda 7.</li>';
			err1 = true;
		}
		if (error=="") {
			submitDomanda();
		} else {
			$('#error6').html("<ul>" + error + "</ul>");
			$('#error6').show('fast');
			if (err1==true) {
				$('#domanda6').addClass('error');
			} else {
				$('#domanda6').removeClass('error');
			}
		}
	}else if (num==7){
		var err1 = false;
		if ( $('input[name=domanda-6-5]:checked').val() == undefined) {
			error += '<li>&Egrave; necessario rispondere alla domanda 6.</li>';
			err1 = true;
		}
		if ( $('input[name=domanda-7-5]:checked').val() == undefined) {
			error += '<li>&Egrave; necessario rispondere alla domanda 7.</li>';
			err1 = true;
		}
		if (error=="") {
			submitDomanda();
		} else {
			$('#error7').html("<ul>" + error + "</ul>");
			$('#error7').show('fast');
			if (err1==true) {
				$('#domanda6').addClass('error');
			} else {
				$('#domanda6').removeClass('error');
			}
		}
	}
}
 
function showNostraDomanda(num) {
	for (i=1;i<8;i++) {
		if (i!=num) {
			$('#nostreDomandeStep'+i).hide();
		} else {
			$('#nostreDomandeStep'+num).show('fast');
		}
	}
	
	
}
function checkSondaggioAbilitato() {
	$val = $('input[name=domanda-1-1]:checked').val();
	var err1 = false;
	
	if($val=='si') {
		showNostraDomanda(2);
	} else if ($val=='no') {
		submitDomanda();
	} else {
		$error = '<li>&Egrave; necessario rispondere alla domanda 1.</li>';
		err1 = true;
		if (err1==true) {
			$('#error1').addClass('error');
			$('#error1').html("<ul>" + $error + "</ul>");
			$('#error1').show('fast');
		} else {
			$('#error1').removeClass('error');
		}
	}
}


function submitDomanda(){
	$('#nostreDomandeForm').submit();
}

function siteSearch() {
	$('#frm-search').submit();
}

function condividiCallback($result) {
	if ($result.status) {
		$('#condividiConfirm').show();
		$('#condividiError').hide();		
		$('#nomesuo').val('');
		$('#emailsua').val('');
	} else {
		$('#condividiConfirm').hide();
		$('#condividiError').show();
	}
}

function setupCondividi() {
	$('#formCondividi').submit(
		function (e) {
			e.preventDefault();
			var $error = false;
			var $nome1 = $('#nomemio').val();
			var $email1 = $('#emailmia').val();
			var $nome2 = $('#nomesuo').val();
			var $email2 = $('#emailsua').val();
			var $thispage = $('#condividi-thispage').val();
			var $thistitle = $('#condividi-thistitle').val();
			
			if ($nome1 == '') {
				$('#nomemio').addClass('error');
				$error = true;
			}
			if (!isEmail($email1)) {
				$('#emailmia').addClass('error');
				$error = true;
			}
			if ($nome2 == '') {
				$('#nomesuo').addClass('error');
				$error = true;
			}
			if (!isEmail($email2)) {
				$('#emailsua').addClass('error');
				$error = true;
			}
			
			if ($error) {
				$('#condividiConfirm').hide();
				$('#condividiError').show();
			} else {
				var $data = {
					nome1: $nome1,
					email1: $email1,
					nome2: $nome2,
					email2: $email2,
					thispage: $thispage,
					thistitle: $thistitle
				};
				$.post('/vivereinarmonia/ajax-condividi.php', $data, condividiCallback, 'json');	
			}
			
		}
	);
}

function getTimeStamp() {
	return Number(new Date());
}

/* JQuery ready */
    
$(document).ready(function() {
	if ($('.commenti-container').length > 0) {
		setupFormCommenti();
	}
	if ($('.voti-container').length > 0) {
		setupVoti();
	}
	if ($('.condividi-container').length > 0) {
		setupCondividi();
	}
	if ($('#submitNewsletterWidget').length > 0) {
		$('#submitNewsletterWidget').click(
			function (e) {
				e.preventDefault();
				$('#widgetNewsletterForm').submit();
			}
		);
		$('#widgetNewsletterForm').submit(
			function (e) {
				var $privacy = $('input[name=accett]:checked').val();
				var $email = $('#e').val();
				if ($privacy == 0 || !isEmail($email)) {
					// alert ('Per scaricare Buongiorno Armonia è necessario dare il consenso alla privacy e indicare un indirizzo email valido.');
					Shadowbox.open({
						content:    '<div id="widget-error">Per scaricare Buongiorno Armonia è necessario dare il consenso alla privacy e indicare un indirizzo email valido.</div>',
						player:     "html",
						title:      "",
						height:     288,
						width:      595
					});
					e.preventDefault();
				} 
			}
		);
	}
	if ($('#downloadWidget0').length > 0) {
		$('#downloadWidget0').click(
			function () {}
		);
	}
	if ($('#downloadWidget1').length > 0) {
		$('#downloadWidget1').click(
			function () {}
		);
	}
	
});


