function parseToFloat(number)
{
   var output, str;
   
   // parsujemy na float
   output = parseFloat(number)
   str = String(output);
   
   // jesli zwrocony string jest bez kropki
   if(str.lastIndexOf('.') == -1)
      str += '.00';
      
   return(str);
}

// JavaScript Document
Math.decimal = function(n, k) {
    var factor = Math.pow(10, k+1);
    n = Math.round(Math.round(n*factor)/10);

    return parseToFloat(n/(factor/10));
}




$(document).ready(
	function()
	{
	
	$("#testjs").hide();
  $("#dokoszyka").show();
  $(".add-to-basket").show();
  
  $('#dokoszyka').bind('submit', function(){
  var valid = true;
  if($("#productoverprint").val()=='wybor'){
  alert('Musisz wybrac typ nadruku.');
  return false;
  }
  });
  
  	 
		$("#productoverprint").change(
			function(){
			  $("#productoverprint").removeOption("wybor");
			   $("#productquantity").removeOption("wybor");
			   
  			for( indeks in myprices[this.value]){
      
      	$("#productquantity").addOption(indeks,myprices[this.value][indeks][0], false);
        }
      	for (i=0;i<=colors[this.value];i++){
       	$("#colorsquantity").addOption(i,i, false);
         }
         
        var nadruk=myprices[$('#productoverprint').val()][$("#productquantity").val()][1]+myprices[$('#productoverprint').val()][$("#productquantity").val()][2]*($('#colorsquantity').val()-1);
        nadruk=0;
        var cena=myprices[$('#productoverprint').val()][$("#productquantity").val()][0]*nadruk;
        var cenakoncowa= cena+myprices[$('#productoverprint').val()][$("#productquantity").val()][0]*$('#productprice').val();    
        
        
        var cenaproduktu=parseFloat($('#productprice').val())+nadruk;
        
      	$("#overprintproductprice").val(Math.decimal(cenaproduktu,2));
      	$("#overprintprice").val(Math.decimal(nadruk,2));
      	
        $("#total").val(Math.decimal(cenakoncowa,2));
				
			}
		);
		$("#productquantity").change(
			function(){
		   if ( $("#colorsquantity").val() >0){
		   var nadruk=myprices[$('#productoverprint').val()][$("#productquantity").val()][1]+myprices[$('#productoverprint').val()][$("#productquantity").val()][2]*($('#colorsquantity').val()-1);
       var cena=myprices[$('#productoverprint').val()][$("#productquantity").val()][0]*nadruk;
    
       }
       else {var cena=0; nadruk=0;}
       
       var cenakoncowa= cena+myprices[$('#productoverprint').val()][$("#productquantity").val()][0]*$('#productprice').val();    
       var cenaproduktu=parseFloat($('#productprice').val())+nadruk;
        
      	$("#overprintproductprice").val(Math.decimal(cenaproduktu,2));
      	$("#overprintprice").val(Math.decimal(nadruk,2));
      	$("#total").val(Math.decimal(cenakoncowa,2));
      }
    );
    $("#colorsquantity").change(
			function(){
			if ($("#colorsquantity").val() >0) {
		   var nadruk=myprices[$('#productoverprint').val()][$("#productquantity").val()][1]+myprices[$('#productoverprint').val()][$("#productquantity").val()][2]*($('#colorsquantity').val()-1);
       var cena=myprices[$('#productoverprint').val()][$("#productquantity").val()][0]*nadruk;
     }
      else {var cena=0; nadruk=0;}
       var cenakoncowa= cena+myprices[$('#productoverprint').val()][$("#productquantity").val()][0]*$('#productprice').val();    
       var cenaproduktu=parseFloat($('#productprice').val())+nadruk;
        
      	$("#overprintproductprice").val(Math.decimal(cenaproduktu,2));
      	$("#overprintprice").val(Math.decimal(nadruk,2));
      	$("#total").val(Math.decimal(cenakoncowa,2));
    }
    );	
		
	}
);