
	var blank = new Image();
	blank.src = 'images/blank.gif';

function fixPng(png) {
	var src = png.src;
	if (!png.style.width) { png.style.width = $(png).width(); }
	if (!png.style.height) { png.style.height = $(png).height(); }
	png.onload = function() { };
	png.src = blank.src;
	png.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
}
 
$(document).ready(function(){ 
			
	if (setPage!=""){
		$("#vm"+setPage).find("img").css('visibility','visible');
		$("#vm"+setPage).children(".topMenu0").removeClass("topMenu0").addClass("topMenu1");
	}
	
    Shadowbox.init({
		overlayOpacity: .8
	});
	
	var badBrowser = (/MSIE ((5\.5)|6)/.test(navigator.userAgent) && navigator.platform == "Win32");
	if (badBrowser) {
		$("img[src$='.png']").each(function() {
			if (!this.complete) {
				this.onload = function() { fixPng(this) };
			} else {
				fixPng(this);
			}
		});
	}
 
	$(".roundTxt").bind('focus', function() {$(this).css('background-color','#FFF');});
	$(".roundTxt").bind('blur',  function() {$(this).css('background-color','#F5F5F5');});
	$("#txtSearch").bind('focus', function() {$(this).val('');});
	$("#txtSearch").bind('blur', function() {
										   		if ($(this).val()=='') {
													$(this).val('ricerca...');	
												}
										  	});

	$('#slider').nivoSlider();

	$(".nomeCategoria").click(function() {
		if ($(this).parent().children(".gruppoFamiglie").css("display") == 'none') {
			$(".gruppoFamiglie").slideUp(500);
			$(this).parent().children(".gruppoFamiglie").slideDown(500);
		} else {
			$(this).parent().children(".gruppoFamiglie").slideUp(500);
		}
	});	

	$("#btnLogIn").click(function() {
	  $("#divLogIn").fadeToggle("slow", "linear");
	});						   

	$("#btnMyPanel").click(function() {
	  $("#divLogIn").fadeToggle("slow", "linear");
	});						   

	var busy = false;
	
	$("#basketItemsWrap li:first").hide();

	$(".productPriceWrapRight a").click(function(e) {
												 
		if(busy) {
				e.preventDefault();
				return;
		}
		
		busy = true;
		var qta = 1;
				
		var productIDValSplitter 	= (this.id).split("_");
		var productIDVal 			= productIDValSplitter[1];
		
		var productX 		= $("#productImageWrapID_" + productIDVal).offset().left;
		var productY 		= $("#productImageWrapID_" + productIDVal).offset().top;
		
		if( $("#setQta_" + productIDVal).length > 0){
			qta = $("#setQta_" + productIDVal).val();
		}
					
		if( $("#productID_" + productIDVal).length > 0){
			var basketX 		= $("#productID_" + productIDVal).offset().left;
			var basketY 		= $("#productID_" + productIDVal).offset().top;			
		} else {
			var basketX 		= $("#basketTitleWrap").offset().left;
			var basketY 		= $("#basketTitleWrap").offset().top;
		}
		
		var gotoX 			= basketX - productX;
		var gotoY 			= basketY - productY;
				
		var newImageWidth 	= $("#productImageWrapID_" + productIDVal).width() / 3;
		var newImageHeight	= $("#productImageWrapID_" + productIDVal).height() / 3;
		
		$("#productImageWrapID_" + productIDVal + " img")
		.clone()
		.prependTo("#productImageWrapID_" + productIDVal)
		.css({'position' : 'absolute'})
		.animate({opacity: 0.4}, 100 )
		.animate({opacity: 0.1, marginLeft: gotoX, marginTop: gotoY, width: newImageWidth, height: newImageHeight}, 1200, function() {
																																																																										  			$(this).remove();
			$("#notificationsLoader").html('<img src="images/loader.gif" class="noBorder vAlignMiddle">');

			$.ajax({  
				type: "POST",  
				url: "functions.asp",  
				data: { productID: productIDVal, valoreQta: qta, action: "addToBasket"},  
				success: function(theResponse) {
					
					busy = false;

					if( $("#productID_" + productIDVal).length > 0){
						$("#productID_" + productIDVal).animate({ opacity: 0 }, 500);
						$("#productID_" + productIDVal).before(theResponse).remove();
						$("#productID_" + productIDVal).animate({ opacity: 0 }, 500);
						$("#productID_" + productIDVal).animate({ opacity: 1 }, 500);
						$("#notificationsLoader").empty();
						
					} else {
						$("#basketItemsWrap li:first").before(theResponse);
						$("#basketItemsWrap li:first").hide();
						$("#basketItemsWrap li:first").show("slow");  
						$("#notificationsLoader").empty();		
					}
					
				}  
			});  
		
		});
		
	});
	
	
	$("#basketItemsWrap li div a img").live("click", function(e) { 
		var productIDValSplitter 	= (this.id).split("_");
		var productIDVal 			= productIDValSplitter[1];	

		$("#notificationsLoader").html('<img src="images/loader.gif" style="vertical-align:middle;">');
	
		$.ajax({  
			type: "POST",  
			url: "functions.asp",  
			data: { productID: productIDVal, action: "deleteFromBasket"},  
			success: function(theResponse) {
				
				$("#productID_" + productIDVal).hide("slow",  function() {$(this).remove();});
				$("#basketItemsWrap li:first").before(theResponse);
				$("#notificationsLoader").empty();
			}  
		});  
		
	});
	
	$.ajax({  
		type: "POST",  
		url: "inc_stats.asp",  
		cache: false,
		success: function(theResponse) {
			$("#statsDiv").html(theResponse);
		}  
	});  
	

});

function deleteBasket() {
	$.ajax({  
		type: "POST",  
		url: "functions.asp",  
		data: {action: "deleteBasket"},  
		success: function(theResponse) {
			
			if (setPage=="8") {
				//Update carrello grande
				$.ajax({  
					type: "POST",  
					url: "inc_carrellopage.asp",  
					success: function(carelloFull) {
						$("#tabDx").hide("slow")
						$("#tabDx").html(carelloFull);
						$("#tabDx").show("slow");  
					}  
				});  
			} else {
				//Update carrello piccolo
				$("#basketUl").hide("slow")
				$("#basketUl").html("<li></li>");
				$("#basketUl").show("slow");  
				$("#notificationsLoader").html(theResponse);
				$("#basketItemsWrap li:first").hide();
			}
		}  
	});  
}

function logIn() {
	$("#divLogInInt1").fadeOut( function() {
		$("#divLogInInt2").fadeIn("slow", function() {
			var usr = $("#usr").val();
			var pwd = $("#pwd").val();
			$.ajax({  
				type: "POST",  
				url: "functions.asp",  
				data: { userName: usr, password: pwd, action: "logInToWebsite"},  
				success: function(theResponse) {
					if (theResponse!="no"){
						$.ajax({  
							type: "POST",  
							url: theResponse,  
							success: function(logOk) {
								$("#divLogInInt2").fadeOut( function() {
																	 
									//User Button
									$("#usrBtn1").hide();
									$("#usrBtn2").hide();
									$("#usrBtn0").show();										
									
									//Info ControlCenter
									$("#divLogInInt3").fadeIn("slow");										
									$("#divLogInInt3").html(logOk);
									
									//label
									$("#lblLogIn").fadeOut( function() {
										$("#lblLogIn").html("CONTROL CENTER");
										$("#lblLogIn").fadeIn("slow");										
									});
									
									//bottone
									$("#btnLogIn1").fadeOut( function() {
										$("#btnLogIn2").fadeIn("slow");										
									});
									
									//Sono nella pagina Carrello?
									if (setPage!="8") {
										//Update carrello piccolo
										$.ajax({  
											type: "POST",  
											url: "functions.asp",  
											data: {action: "refreshLittleBasket"},  
											success: function(carrelloLista) {
												$("#basketUl").hide()
												$("#basketUl").html(carrelloLista);
												$("#basketUl").show("slow");  
											}  
										});  
									} else {
										//Update carrello grande
										$.ajax({  
											type: "POST",  
											url: "inc_carrellopage.asp",  
											success: function(carelloFull) {
												$("#tabDx").hide()
												$("#tabDx").html(carelloFull);
												$("#tabDx").show("slow");  
											}  
										});  
									}
									
								});
							}  
						});  
					} else {
						$("#divLogInInt2").fadeOut( function() {
							$("#divLogInInt1").fadeIn("slow");
							$("#divLogInInt1").children("span").css('display','inline-block');
						});
					}
				}  
			});  
		});
	});
}

function logOut() {
	$("#divLogInInt3").fadeOut( function() {
		$("#divLogInInt2").fadeIn("slow", function() {
			$.ajax({  
				type: "POST",  
				url: "functions.asp",  
				data: {action: "logOutToWebsite"},  
				success: function(theResponse) {
					$("#divLogInInt2").fadeOut( function() {
																 
						//User Button
						$("#usrBtn1").show();
						$("#usrBtn2").show();
						$("#usrBtn0").hide();										
						
						//Info ControlCenter
						$("#divLogInInt1").fadeIn("slow");										
						
						//label
						$("#lblLogIn").fadeOut( function() {
							$("#lblLogIn").html("ACCESSO");
							$("#lblLogIn").fadeIn("slow");										
						});
						
						//bottone
						$("#btnLogIn2").fadeOut( function() {
							$("#btnLogIn1").fadeIn("slow");										
						});
						
						//Sono nella pagina Carrello?
						if (setPage!="8") {
							//Update carrello piccolo
							$.ajax({  
								type: "POST",  
								url: "functions.asp",  
								data: {action: "emptyLittleBasket"},  
								success: function(carrelloLista) {
									$("#basketUl").hide()
									$("#basketUl").html(carrelloLista);
									$("#basketUl").show("slow");  
								}  
							});  
						} else {
							//Update carrello grande
							$.ajax({  
								type: "POST",  
								url: "inc_carrellopage.asp",  
								success: function(carelloFull) {
									$("#tabDx").hide()
									$("#tabDx").html(carelloFull);
									$("#tabDx").show("slow");  
								}  
							});  
						}
								
					});  
				}  
			});  
		});
	});
}

function deleteRow(id){
	$("#rowCar" + id).hide("slow",  function() {$(this).remove();});
	$.ajax({  
		type: "POST",  
		url: "functions.asp",  
		data: { productID: id, action: "deleteFromBasketCAR"},  
		success: function(theResponse) {
			$("#tot1Car").hide();
			$("#tot2Car").hide();
			$("#tot3Car").hide();
			$("#tot4Car").hide();
			$("#tot5Car").hide();
			$("#responseHTML").before(theResponse);
			$("#tot1Car").show("slow");  			
			$("#tot2Car").show("slow");  			
			$("#tot3Car").show("slow");  	
			$("#tot4Car").show("slow");  	
			$("#tot5Car").show("slow");  	
		}
   });
}

function setQtaValue(id,vQta){
	if (isNaN(parseInt($("#qtaCar" + id).val()))) {
		vQta = 1
		$("#qtaCar" + id).val("0");
	} else {
		if (parseInt($("#qtaCar" + id).val())=='1' && (vQta==-1)) {
			return;
		}
	}
	vQta = (parseInt($("#qtaCar" + id).val()) + vQta);
	$("#qtaCar" + id).val(vQta);
	$.ajax({  
		type: "POST",  
		url: "functions.asp",  
		data: { productID: id, valoreQta: vQta, action: "refreshBasket"},  
		success: function(theResponse) {
			$("#tot1Car").hide();
			$("#tot2Car").hide();
			$("#tot3Car").hide();
			$("#tot4Car").hide();
			$("#tot5Car").hide();
			$("#prezzoRigaCar" + id).hide();
			$("#responseHTML").before(theResponse);
			$("#prezzoRigaCar" + id).show("slow");					
			$("#tot1Car").show("slow");  			
			$("#tot2Car").show("slow");  			
			$("#tot3Car").show("slow");  	
			$("#tot4Car").show("slow");  	
			$("#tot5Car").show("slow");  	
		}
   });
}

function setCoupon(codeCoupon){
	$.ajax({  
		type: "POST",  
		url: "functions.asp",  
		data: {codeCoupon: codeCoupon, action: "addCoupon"},  
		success: function(theResponse) {
			$("#tot4Car").hide();
			$("#tot5Car").hide();
			$("#responseHTML").before(theResponse);
			$("#tot4Car").show("slow");  	
			$("#tot5Car").show("slow");  	
		}
   });
}

function controlloObbligatori (testo_messaggio, testo_min_char) {
	
	// Recupero campi obbligatori (separati da uno spazio)
	var ArrayReq = new Array();
	ArrayReq = $("#obbligatori").val().split(" ");
	
	// Recupero descrizione del messaggio a video dei campi obbligatori (separati da virgola spazio)
	var ArrayAlias = new Array();
	var appo = "";
	if (typeof($("#obbligatori_alias")) != 'undefined') {
		appo = $("#obbligatori_alias").val();
		if (appo != '') ArrayAlias = appo.split(", ");
	}

	// Recupero messaggio video (carattere jolly: #%)
	var txt_mess = 'I campi contrassegnati da un asterisco (*) sono obligatori! (#%)';
	if (testo_messaggio != null) txt_mess = testo_messaggio;
	
	var txt_mess_min_char = 'Il campo (#1) prevede almeno #2 caratteri';
	if (testo_min_char != null) txt_mess_min_char = testo_min_char;
	
	// Ciclo di controllo
	var iErrore = 0;
	for (var i=0; i < ArrayReq.length; i++) {
		var appoArrayReq = ArrayReq[i].split("#");
		if ($("#" + appoArrayReq[0]).val()  == "") {
			iErrore = 1;	// campo vuoto
			break;
		
		} else if (appoArrayReq[1] == "EMAIL") {
			if (!isEmail($("#" + appoArrayReq[0]).val())) {
				alert ('Email non valida');
				$("#" + appoArrayReq[0]).val().focus();
				iErrore = 3;
				break;
			}
		} else {
			if (appoArrayReq.length > 1) {
				var strAppo = $("#" + appoArrayReq[0]).val();
				if (strAppo.length < parseInt(appoArrayReq[1],10)) {
					iErrore = 2;	// pochi caratteri
					break;
				}
			}
		}
	}
	
	// campo non valorizzato
	if (iErrore == 1) {
		var t = txt_mess.replace('#%', (((ArrayAlias.length > 0) && (i <= ArrayAlias.length)) ? ArrayAlias[i] : appoArrayReq[0]));
		alert ( t );
		var appo = $("#" + appoArrayReq[0]);
		if (appo.type != "hidden")
			appo.focus();
		return false;
	
	// numero di caratteri del campo non sufficienti
	} else if (iErrore == 2) {
		var t = txt_mess_min_char.replace('#1', (((ArrayAlias.length > 0) && (i <= ArrayAlias.length)) ? ArrayAlias[i] : appoArrayReq[0]));
		t = t.replace('#2', appoArrayReq[1]);
		alert ( t );
		var appo = $("#" + appoArrayReq[0]);
		if (appo.type != "hidden")
			appo.focus();
		return false;
	
	} else if (iErrore > 0) {
		// a fronte di un errore non definito, ritorno comunque l'impossibilità di proseguire
		return false;
	}

	// tutto ok
	return true;
}

function isCodiceFiscale(string) {
	if (string!=""){
		if (string.search(/^[A-Za-z]{6}[0-9]{2}[A-Za-z]{1}[0-9]{2}[A-Za-z]{1}[0-9]{3}[A-Za-z]{1}$/) != -1)
			return true;
		else
			return false;
	} else {
		return true;
	}
}

function isPartitaIva(string) {
	if (string!=""){
		if (string.search(/^[0-9]{11}$/) != -1)
			return true;
		else
			return false;
	} else {
		return true;
	}
}

function isEmail(string) {
	if (string!=""){
		if (string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
			return true;
		else
			return false;
	} else {
		return true;
	}
}

function registrazione() {
	if (controlloObbligatori()) {
		if ($("#r_password").val()=='') {
			alert('Reinserisci correttamente la tua password in entrambi i campi!');
			$("#r_password").focus();
			return;
		}
		if (isEmail($("#r_email").val())==false) {
			alert('Inserire una Email valida');
			$("#r_email").focus();
			return;
		}

		if ((($("#r_codicefiscale").val()=='') && ($("#r_partitaiva").val()==''))) {
			alert('Codice Fiscale o Partita IVA obbligatori!');
			$("#r_codicefiscale").focus();
			return;
		}
		if (isCodiceFiscale($("#r_codicefiscale").val()) == false) {
			alert('Inserire un Codice Fiscale valido');
			$("#r_codicefiscale").focus();
			return;
		}
		if (isPartitaIva($("#r_partitaiva").val()) == false) {
			alert('Inserire una Partita IVA valida');
			$("#r_partitaiva").focus();
			return;
		}
		if (!($('#r_consenso').attr('checked'))) {
			alert('Impossibile proseguire se non si accetta il consenso al trattamento dei propri dati personali forniti');
			$("#r_consenso").focus();
			return;
		}
		$('#frmRegistrazione').submit();
	}
}

function registrazioneConferma() {
	$('#frmRegistrazione').submit();
}

function fornitore() {
	if (controlloObbligatori()) {
		if ($("#r_password").val()=='') {
			alert('Reinserisci correttamente la tua password in entrambi i campi!');
			$("#r_password").focus();
			return;
		}
		if (isEmail($("#r_email").val())==false) {
			alert('Inserire una Email valida');
			$("#r_email").focus();
			return;
		}

		if (isPartitaIva($("#r_partitaiva").val()) == false) {
			alert('Inserire una Partita IVA valida');
			$("#r_partitaiva").focus();
			return;
		}
		$('#frmFornitore').submit();
	}
}

function fornitoreConferma() {
	$('#frmFornitore').submit();
}

function showinfo(id) {
	if (id=='1') {	
		$("#pagDiv2").slideUp(500);
		$("#pagDiv3").slideUp(500);
		$("#pagDiv6").slideUp(500);
		$("#pagDiv5").slideUp(500);
		$("#divTelefonata").slideDown(500);
	}
	
	if (id=='2') {	
		$("#pagDiv2").slideDown(500);
		$("#pagDiv3").slideUp(500);
		$("#pagDiv6").slideUp(500);
		$("#pagDiv5").slideUp(500);
		$("#divTelefonata").slideDown(500);
	}
	
	if (id=='3') {	
		$("#pagDiv2").slideUp(500);
		$("#pagDiv3").slideDown(500);
		$("#pagDiv6").slideUp(500);
		$("#pagDiv5").slideUp(500);
		$("#divTelefonata").slideDown(500);
	}
	
	if (id=='4') {	
		$("#pagDiv2").slideUp(500);
		$("#pagDiv3").slideUp(500);
		$("#pagDiv6").slideUp(500);
		$("#pagDiv5").slideUp(500);
		$("#divTelefonata").slideUp(500);
		$("#telefonata").attr('checked',false);
	}
	
	if (id=='5') {	
		$("#pagDiv2").slideUp(500);
		$("#pagDiv3").slideUp(500);
		$("#pagDiv6").slideUp(500);
		$("#pagDiv5").slideDown(500);
		$("#divTelefonata").slideDown(500);
	}

	if (id=='6') {	
		$("#pagDiv2").slideUp(500);
		$("#pagDiv3").slideUp(500);
		$("#pagDiv5").slideUp(500);
		$("#pagDiv6").slideDown(500);
		$("#divTelefonata").slideDown(500);
	}
}

function confermaAcquisto(totaleC) {
	frm = document.carrello;
	tras = '0';
	for (i=0; i<frm.trasporto.length; i++) { 
		if (frm.trasporto[i].checked==true) {
			tras=frm.trasporto[i].value;
		}
	}
	if (tras=='0') {
		alert('E\' necessario specificare un sistema di Trasporto e Pagamento');
		return;
	} 
	if ((tras=='1') && (parseFloat(totaleC)>50)) {
		alert('Importo superiore ai 50€, impossibile confermare l\'ordine con questo metodo di Trasporto e Pagamento');
		return;
	}
	if (confirm('Si desidera confermare l\'ordine?')) {
		frm.submit();
	}
}

function startSearch() {
	document.location = 'ricerca.asp?ricerca=' + $("#txtSearch").val()
}
