// JavaScript Document

	  
	  
	  if (!$.browser.msie) {
         
	  
	  }
	  
	  
	  
	  
	  $(document).ready(function(){
								 
								 
								 
					var prc_id_selected = $("#prc_id_selected").val();	
					if(prc_id_selected>0) OpenMenuCatalogue(prc_id_selected);
								 
				
							
							/*
					$("#MenuCatalogue ul > li").each(function(){
						$("a",this).attr("href","#");		
						$("a",this).click(function(){
							$(this).parent().find("ul").show();		   
						});
					});
								*/ 
	 
	 
	
								 
					function OpenMenuCatalogue(prc_id_selected){
					   	$("#MenuCatalogue #li_"+prc_id_selected).find("ul").eq(0).show();
						$("#MenuCatalogue #li_"+prc_id_selected).parent().show();
					}
								 
								 
//////////////////////////////// FORM RICERCA PRODOTTI


				  $("#FormRicercaProdotti #ul_lista_colori li").each(function(index){
				  
					 $("span", this).hide();
					 $("input", this).hide();
					 
					 $("a", this).click(function(){
						 var checked = $(this).parent().find("input").attr("checked");
						 if(!checked){
							$(this).parent().find("input").attr("checked","true");
							$(this).addClass("select");
						 }else{
							$(this).parent().find("input").attr("checked","false");
							$(this).parent().find("input").removeAttr("checked");
							$(this).removeClass("select");
						 }
					 });
				  
				  });
				  
				  
				  
				  
					$("#FormRicercaProdotti #ul_lista_keywords li").each(function(index){
				  
					 $("input", this).hide();
					 
					 $("a", this).click(function(){
						 var checked = $(this).parent().find("input").attr("checked");
						 if(!checked){
							$(this).parent().find("input").attr("checked","true");
							$(this).addClass("select");
						 }else{
							$(this).parent().find("input").attr("checked","false");
							$(this).parent().find("input").removeAttr("checked");
							$(this).removeClass("select");
						 }
					 });
				  
				   });
					
					
					
					
					$("#FormRicercaProdotti #button_submit").click(function(){
						    
							
							
							
							
							var theForm = $("#FormRicercaProdotti");
							var rewriting = $("input[name=rewriting]").val();
							var testo_ricerca = $("input[name=testo_ricerca]").val();
							var prc_id = $("select[name=prc_id] option:selected").val();
							var da_ricerca = $("input[name=da_ricerca]").val();
							var a_ricerca = $("input[name=a_ricerca]").val();
							var atteggiamenti_id = "";
							var colori_id = "";
							var keywords_id = "";
							
							$("#ul_lista_atteggiamenti input:checked", theForm).each(function(){
							   atteggiamenti_id += $(this).val()+"|";
							});
							
							$("#ul_lista_colori input:checked", theForm).each(function(){
							   colori_id += $(this).val()+"|";
							});
							
							$("#ul_lista_keywords input:checked", theForm).each(function(){
							   keywords_id += $(this).val()+"|";
							});
							
							atteggiamenti_id = atteggiamenti_id.substr(0, atteggiamenti_id.lastIndexOf("|"));
							colori_id = colori_id.substr(0, colori_id.lastIndexOf("|"));
							keywords_id = keywords_id.substr(0, keywords_id.lastIndexOf("|"));
							
							
							
							
							
							var geturlstring = "";
							if(testo_ricerca=="") testo_ricerca="tutti";
							if(prc_id=="") prc_id="tutti";
							if(atteggiamenti_id=="") atteggiamenti_id="tutti";
							if(colori_id=="") colori_id="tutti";
							if(keywords_id=="") keywords_id="tutti";
							if(da_ricerca=="") da_ricerca="tutti";
							if(a_ricerca=="") a_ricerca="tutti";
							
							geturlstring = "ricerca/"+testo_ricerca+"/"+prc_id+"/"+atteggiamenti_id+"/"+colori_id+"/"+keywords_id+"/"+da_ricerca+"/"+a_ricerca;
							
							
							
							if(rewriting=="Y"){
							//$(theForm).attr("action","http://www.dmpromo.it/"+geturlstring);
							//$(theForm).get(0).setAttribute("action", "http://www.dmpromo.it/"+geturlstring); //this works
							window.location.href = "http://www.dmpromo.it/"+geturlstring+"/1.html";
							return false;
							}
									
								
					});
					
					
					
					function ClearForm(theForm){
					 $('input', theForm)
					 .not(':button, :submit, :reset, :hidden')
					 .val('')
					 .removeAttr('checked')
					 .removeAttr('selected');

                     $('ul li a.select', theForm).removeClass("select");
					}
					
					
					
					
					
					$("#FormRicercaProdotti #button_reset").click(function(){						   
					   
					   ClearForm($("#FormRicercaProdotti"));

																		   
					});
					
								 

					
					var hform = $("#BoxFormSearchOverlay").height();
					$("#BoxFormSearchOverlay").hide();
					$("#BoxFormSearchOverlay").addClass("closed");
					
						$("#BoxFormSearch #open, #BoxFormSearch #close").click(function(){
																						
									if($("#BoxFormSearchOverlay").hasClass("closed")){
										
									$("#BoxFormSearchOverlay").removeClass("closed");	
									   
										$("#BoxFormSearchOverlay").show().css("height","1px").animate({
										height: hform+'px'}, 1000, function() { 
										
										$("#BoxFormSearchOverlay").addClass("open");
										
										});
									
									
									}else{
										
										
										$("#BoxFormSearchOverlay").removeClass("open");	
										
									
										$("#BoxFormSearchOverlay").show().animate({
										height:'1px'}, 1000, function() { 
										
										$("#BoxFormSearchOverlay").addClass("closed");
										$("#BoxFormSearchOverlay").hide();
										
										});
										
									
									}
						
						});
						
						
						
						////////////////////////////////////////////////////////
						
						
 // put all your jQuery goodness in here.
      $("#MenuProdottoTab a").each(function(index){
	      
		  $(this).click(function(){ 
		  $("#MenuProdottoTab a").removeClass("open");
		  $(this).addClass("open");
		  $(".prodotti2tab").css("display","none");
		  $(".prodotti2tab").eq(index).css("display","block");
		  
		  });
		  
		  
	  });
	  
	  
	  
	  
	  ////////////////////// design tabella costi per prodotto pagina articolo.php /////////////
	  
	  
	  $("#BoxTableCostsProduct table td.num").each(function(){
		
		var text = $(this).text();
		if((text!="")  && (text!="-") && (text!="INFO")){
		   $(this).html("&euro; "+text);	
		}
		
		var id_prodotto = $("#BoxCartProduct form input[name=pro_id]").val();
		var url_prodotto = $("#BoxCartProduct form input[name=url]").val();
		var nome_prodotto = $("#BoxCartProduct form input[name=nome]").val();
		var quantita_minima_prodotto = $("#BoxCartProduct form input[name=quantita_minima]").val();
		
		
		
		if(text=="INFO"){
		  
		  var index = $(this).parent('tr').children().index(this);
		  var quantita_stampa = $("#BoxTableCostsProduct table thead th").eq(index).attr("class");
		  var lavorazione = $(this).parent().find("td").eq(0).text();
			
		  $(this).text("");
		  var button_request = $('<a>');
		  $(button_request).addClass("info_table_costs")
		  $(button_request).html("<span>Richiedi informazioni</span>");
		  $(button_request).attr("title","Richiedi maggiori informazioni");
		  $(button_request).click(function(){
		  OpenRequestInfo(id_prodotto, url_prodotto, nome_prodotto,quantita_stampa,lavorazione);
		  });
		  $(this).append(button_request);
		  //$(this).html('<a class="info_table_costs"></div>');
		  
		 
	     }
		
															
	  });
	  
	  
	  
	  function OpenRequestInfo(id_prodotto, url_prodotto, nome_prodotto, quantita_stampa, lavorazione){
		  		window.open('http://www.dmpromo.it/form_richiesta.php?id_prodotto='+id_prodotto+'&url_prodotto='+url_prodotto+'&nome_prodotto='+nome_prodotto+'&quantita_stampa='+quantita_stampa+'&lavorazione='+lavorazione,'richiesta_info_prodotto','resizable=yes, scrollbars=yes, width=560,height=600,top=20,left=20');
	  }
	  
	  
	  
	  
	  //////////////////////////// submit form carrello (carrello.php) ////////////////////////
	  
	  
	  $("#FormChart input[type=submit]").click(function(){
					
					var theForm = $("#FormChart");
					var messagge = "";
					
					$("table tr", theForm).each(function(){
														 
							var art_id = $("input[name=art_id[]]",this).val();	
							var pro_id = $("input[name=pro_id[]]",this).val();	
							var pro_nome = $("input[name=pro_nome[]]",this).val();	
							var quantita_minima = $("input[name=pro_quantita_minima[]]",this).val();	
							
							if(pro_id && quantita_minima){
								var quantita_richiesta = 0;
								$("input.quantita_richiesta_"+pro_id, theForm).each(function(){
							       quantita_richiesta = Number(quantita_richiesta)+Number($(this).val());
								})
								
								if(quantita_richiesta<quantita_minima){
								  messagge += "La quantità minima ordinabile per il prodotto "+pro_nome.toString().toUpperCase()+" è di "+quantita_minima+"pezzi.\n";	
								}
							}
							
															
					})
					
					
					if(messagge!=""){
					messagge = "Attenzione:\n"+messagge;
					alert(messagge);
					return false;	
					
					}
	
	  });
	  
	  
	  
	  
	  
	  
	  
	  /////////////////////// submit form carrello pagina articolo.php ///////////////////////////
	  
	  
	  
	  $("#FormCartProduct input[type=submit]").click(function(){

              var name_submit = $(this).attr("name");

              

	          var theForm = $("#FormCartProduct");
			  var host = $("#FormCartProduct input[name=host]").val();
		      var quantita_minima = parseFloat($("#FormCartProduct input[name=quantita_minima]").val());
			  var quantita_ordinata = parseFloat($("#FormCartProduct input[name=quantita_ordinata]").val());
		      var input_quantita = $("#FormCartProduct input[name=quantita[]]");
			  var url_preventivo = $("#FormCartProduct input[name=url_preventivo]");
			  var url_carrello = $("#FormCartProduct input[name=url_carrello]");
			  var quantita_totale = 0;
			  
			  
			  
			  var quantita = parseFloat(0);
			  
			  for(var i=0; i<input_quantita.length; i++){
			      var input = $(input_quantita[i]);
			      quantita += Number(input.val());
			  }
			  
			  
			  if(name_submit=="submit_preventivo"){
				  quantita_totale = Number(quantita);
			  }else if(name_submit=="submit_carrello"){
				  quantita_totale = Number(quantita+quantita_ordinata);
			  }

			  
			  if(quantita_minima>quantita_totale){
			  alert("Attenzione, la quantita' minima ordinabile per questo prodotto è di "+quantita_minima+" pezzi.");
			  return false;
			  }

			  
			  
			  if(name_submit=="submit_preventivo"){
			     $("#FormCartProduct").attr("action",host+"/preventivo.php");
			  }else if(name_submit=="submit_carrello"){
			     $("#FormCartProduct").attr("action",host+"/carrello.php");
			  }

		   
	  });
	  
	  
	  
	  
	  $("#FormCartProduct table td.button").each(function(index){
	  
	     var aplus = $("a:eq(0)",this);
		 var aminus = $("a:eq(1)",this);
		 aplus.removeAttr("href");
		 aminus.removeAttr("href");
		 
		 
		 
		 
		 $("#FormCartProduct table td.button a").live("click", function(event) {
           
		   var icon = $(this).text();
		   var parent = $(this).parent().parent();
		   var index = $("#FormCartProduct table tr").index(parent);
		   var quantita_ordinata_riga =  $("input[name=quantita[]]", parent).val();
		   var index_variante_stampa = $(".select_stampa", parent).attr("selectedIndex");
		   var quantita_minima_stampa = parseFloat(50);
		   var minimo_di_stampa = $("#BoxTableCostsProduct table tr").eq(index_variante_stampa).find("td").eq(1).text();
		   
		   
	       if(icon=="+"){
			   
			 if((quantita_ordinata_riga<=0) || (quantita_ordinata_riga=="")){
				 
				 alert("Attenzione, indicare una quantità maggiore a 0.");
				 
			 }else if((quantita_ordinata_riga>0) && (minimo_di_stampa=="-")){
				 
				 alert("Attenzione, non è possibile ordinare il quantitativo di merce indicato in quanto non è possibile effettuare la lavorazione di stampa selezionata.");
				 $("input[name=quantita[]]", parent).val("");
				 
			 }else{
			   
		     var tr = $("#FormCartProduct table tr:eq("+index+")").html();
			 $("#FormCartProduct table tr:eq("+index+")").after("<tr>"+tr+"</tr>");
			 
			 }
			 
			 
			 
		   }else{
		     $("#FormCartProduct table tr:eq("+index+")").remove();
		   }
		   
		 });
	  
	  
	  });
						
						

					
					
								 
	  });
