// JavaScript Document
var send = 0;
var required_one = 0;

$(document).ready(function() {
	$("form").submit(function(){
			
			if(send == 0){
				
				send = 1;					
				var mesg = '';	
				
				//validazione campi obbligatori
				$.each($(".required"), function() {
												
				  if(this.name == 'email'){	
				  	if( !this.value.match(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i)){
							mesg = mesg+'Inserire il campo obbligatorio '+this.title+' o controllarne la sintassi.\n';
							$(this).addClass("error");
						}
						else
							$(this).removeClass("error");
					}
					else if(this.type == 'select-one'){
						if(this.value == '0'){
							mesg = mesg+'Inserire il campo obbligatorio '+this.title+'\n';
							$(this).addClass("error");
						}
						else
							$(this).removeClass("error");
					}
					else{
						if( jQuery.trim(this.value) == ''){
							mesg = mesg+'Inserire il campo obbligatorio '+this.title+'\n';	
							$(this).addClass("error");
						}
						else
							$(this).removeClass("error");
					}				  					  
				});
				
				//radiobutton
				if($("input[name='finitura']:checked").val() == null){
					mesg = mesg+'Scegliere un valore nel gruppo Finitura desiderata\n';
				}
				
				//almeno un campo obbligatorio nel gruppo			
				$.each($(".required_one"), function() {				  
						if( jQuery.trim(this.value) != ''){
							required_one = 1;							
						}												  					  
				});				
				if(required_one == 0){	
					var name = '';
					$.each($(".required_one"), function() {				  
						name =	this.title;
						$(this).addClass("error");
					});
					mesg = mesg+'Inserire almeno un valore nel gruppo '+name+'\n';
				}
				else{
					$.each($(".required_one"), function() {						
						$(this).removeClass("error");
					});
				}
				
				
				if(mesg != ''){
					alert(mesg);
					send = 0;
					return false;
				}
				else{
					$("#buttons").html("Invio in corso ...");
					this.submit();	
				}
				
			}
			return false;
	});
	
	$("#annulla").click(function(){
			send = 0;
			required_one = 0;
			$.each($(".required"), function() {
				$(this).removeClass("error");
			});
			$.each($(".required_one"), function() {
				$(this).removeClass("error");
			});
	});
	
	/* tooltip */
		  /* precaricamento */	  
		  for(var i = 1;i<=$("input[type^='radio']").length;i++)
		  	$(document.createElement("img")).attr({ src: "finiture_tooltip/fin_"+i+".jpg"}).appendTo($("#preload"));
	
	$("input[type^='radio']").tooltip({         
        tip: '.tooltip',           
        effect: 'slide',
		position:'center right',
		offset: [0, 180],
		onBeforeShow: function() { 
			$("#tip_img").attr("src","finiture_tooltip/"+this.getTrigger().context.id+".jpg");						
		} 
    });
	
	/* unit switch */
	$(".unit").click(function (){
		$(this).parent().find("a").removeClass("unit_selected");
		$(this).addClass("unit_selected");
		$(this).parent().find("input[type=hidden]").val(this.rel);
		return false;
	});
	
	/* same address */
	$("#address_clear").click(function(){
		$("#indirizzo2").val("");
		$("#citta2").val("");
		$("#cap2").val("");
		$("#prov2").val("");
		$("#stato2 option:selected").removeAttr("selected","selected");
		$("#stato2 option:first").attr("selected","selected");
		return false;								   
	});
	
	$("#address_copy").click(function(){
		$("#indirizzo2").val($("#indirizzo").val());
		$("#citta2").val($("#citta").val());
		$("#cap2").val($("#cap").val());
		$("#prov2").val($("#prov").val());
		$("#stato2 option:eq("+$("#stato option").index($("#stato option:selected"))+")").attr("selected","selected");
		return false;								   
	});
});
