// JavaScript Document


$.validator.addMethod(
    "dateIT",
    function(value, element) {
        // put your own logic here, this is just a (crappy) example
        return value.match(/^\d\d?\/\d\d?\/\d\d\d\d$/);
    },
    "Data non corretta gg/mm/aaaa"
);

		


    $(document).ready(function() {
		
		$("#infosoc").colorbox({inline:true, href:"#divaint"});
		$("#condVendita").colorbox({iframe:true, innerWidth:825, innerHeight:444});
		$("#regconcorso").colorbox({iframe:true, innerWidth:825, innerHeight:444});
		$("#pvtotal").colorbox({iframe:true, innerWidth:780, innerHeight:565});

		
	  	$("#postMessage").submit(function(){
										 
			if ($("#commento").val()=="") { alert("Devi inserire un commento"); } else {
			$.ajax({
				type: "GET",
				url: "ajax/postComment.php?utenteID=" +$("#utenteID").val()+ "&nomebo=" +$("#nomebo").val()+ "&emailbo=" +$("#emailbo").val()+ "&prodottoID=" +$("#prodottoID").val()+ "&lingua=" +$("#lingua").val()+ "&rate=" +$("#rate").val()+ "&commento=" +$("#commento").val(),
				success: function(msg){
					$("#line1").html(msg);
				}
			});
			}
			return false;
		});		   

							   
      
		if (!$.browser.opera) {
			$('select.select').each(function(){
				var title = $(this).attr('title');
				if( $('option:selected', this).val() != ''  ) title = $('option:selected',this).text();
				$(this)
					.css({'z-index':10,'opacity':0,'-khtml-appearance':'none'})
					.after('<span class="select">' + title + '</span>')
					.change(function(){
						val = $('option:selected',this).text();
						$(this).next().text(val);
						})
			});

		};
		if (!$.browser.opera) {
			$('select.select2').each(function(){
				var title = $(this).attr('title');
				if( $('option:selected', this).val() != ''  ) title = $('option:selected',this).text();
				$(this)
					.css({'z-index':10,'opacity':0,'-khtml-appearance':'none'})
					.after('<span class="select2">' + title + '</span>')
					.change(function(){
						val = $('option:selected',this).text();
						$(this).next().text(val);
						})
			});

		};
		$("#emailProd").each(function() {
			var default_value = this.value;
			$(this).focus(function() {
				if(this.value == default_value) {
					this.value = '';
				}
			});
			$(this).blur(function() {
				if(this.value == '') {
					this.value = default_value;
				}
			});
		});
		
		$("#nomeNewsletter").each(function() {
			var default_value = this.value;
			$(this).focus(function() {
				if(this.value == default_value) {
					this.value = '';
				}
			});
			$(this).blur(function() {
				if(this.value == '') {
					this.value = default_value;
				}
			});
		});		
		$("#cognomeNewsletter").each(function() {
			var default_value = this.value;
			$(this).focus(function() {
				if(this.value == default_value) {
					this.value = '';
				}
			});
			$(this).blur(function() {
				if(this.value == '') {
					this.value = default_value;
				}
			});
		});		
		$("#emailNewsletter").each(function() {
			var default_value = this.value;
			$(this).focus(function() {
				if(this.value == default_value) {
					this.value = '';
				}
			});
			$(this).blur(function() {
				if(this.value == '') {
					this.value = default_value;
				}
			});
		});			
		
		$("#provincia").change(function(){
			$("#loadingComuni").hide();
			$("#loadingComuni").show();
			$.ajax({
				type: "GET",
				url: "ajax/comuni.php?idprovincia=" +$("#provincia").val(),
				success: function(msg){
					$('#comuni').html(msg);
					$("#loadingComuni").show();
					$("#loadingComuni").hide();
					return false;
				}
			});
		});
		/*-------------- FORM LINKS.PHP ------*/
		$("#provincia2").change(function(){
			$("#loadingComuni2").hide();
			$("#loadingComuni2").show();
			$.ajax({
				type: "GET",
				url: "ajax/comuni.php?idprovincia=" +$("#provincia2").val(),
				success: function(msg){
					$('#comuni2').html(msg);
					$("#loadingComuni2").show();
					$("#loadingComuni2").hide();
					$("#error2").hide();
					return false;
				}
			});
		});
		$("#comuni2").change(function(){
			$("#error3").hide();
		});
		$("#prodottoID").change(function(){
			$("#error1").hide();
		});
		
     $("#ricercaShop").submit(function(){
	 	var prodottoID=$("#prodottoID").val();
	 	var provincia2=$("#provincia2").val();
	 	var comuni2=$("#comuni2").val();
	 	var emailProd=$("#emailProd").val();
		var lang=$("#lang").val();
		var errori=0;
		if (prodottoID<1) { $("#error1").fadeIn(); errori++; } else {$("#error1").hide();}
		if (provincia2<1) { $("#error2").fadeIn(); errori++; } else {$("#error2").hide();}
		if (comuni2<1) { $("#error3").fadeIn(); errori++; } else {$("#error3").hide();}
		if (emailProd=='') { $("#error4").fadeIn(); errori++; } else {$("#error4").hide();}
		if (emailProd=='Indirizzo E-mail') { $("#error4").fadeIn(); errori++; } else {$("#error4").hide();}
		if (errori==0) {
			$("#ricercaShop").hide();
			$("#formLoader").show();
			$.ajax({
				type: "GET",
				url: "ajax/ricercanegozio.php?prodottoID=" +prodottoID+ "&provincia2=" +provincia2+ "&comuni2=" +comuni2+ "&emailProd=" +emailProd+ "&lang=" +lang,
				success: function(msg){
					$("#formRisultato").html(msg);
					$("#formLoader").hide();
					$("#formRisultato").show();
					return false;
				}
			});
		}
		
		return false;
	 });
	 
	 
	 
	 $("#newsletterForm").submit(function(){
		 var lang=$("#lang").val();
		 var nome=$("#nomeNewsletter").val();
		 var cognome=$("#cognomeNewsletter").val();
		 var email=$("#emailNewsletter").val();
		 if (nome>'' && cognome>'' && email>'') {
			 $.ajax({
				type: "GET",
				url: "ajax/newsletter.php?nome=" +nome+ "&cognome=" +cognome+ "&email=" +email+ "&lang=" +lang,
				success: function(msg){
					alert(msg);
					return false;
				}
			});
		 } else {
			alert("Attenzione tutti i campi sono obbligatori"); 
			return false;
		 }
		 return false;
	 });
	 
	 //------- FILTRO SECONDO PRODOTTO REG. FRIA FRIENDS
	 
		
		
			$("#regProvincia").change(function(){
			$.ajax({
				type: "GET",
				url: "ajax/comuni.php?idprovincia=" +$("#regProvincia").val(),
				success: function(msg){
					$('#regComune').html(msg);
					return false;
				}
			});
		});	
		
		
		
    });
	
	function chiudiRisultato(){
		$("#formRisultato").hide();
		$("#ricercaShop").show();
		return false;
	}
	
	

	
	
