function addProduct(idProduct,countOfElements)
{
	
	$('#removeToMuch').css('display','none');
	 $.ajax({
    	type: 'POST',
   	 	url: 'index.ajax.php?mod=main&scr=ajaxcart&evt=addMoreProducts&id_product='+idProduct,
  		dataType: 'json',
    	success: function(data){ 
			var wholePrice =  data['wholePrice'];
			var privatePrice = data['privatePrice'];
			var countOfElements = data['countOfElements'];
			var toMuch = data['toMuch'];
			
			$('#wholePrice').html(wholePrice+" PLN");
			$('#privatePrice'+idProduct).html(privatePrice);
			$('#elements'+idProduct).val(countOfElements);
			$('#toMuch').html(toMuch);
			
			if($('#giveOrder').html() == '')
			{
				$('#giveOrder').html('<td id="giveOrder"><input type="button" value="'+$zlozZamowienie+'" onclick="javascript:giveOrder()" /></td>');
				$('#confirmation').html('');
			}
			if(toMuch != "")
				$('#removeToMuch').css('display','');
			priceAfterDiscount();
		}
	});
	  
//	  $.post("index.ajax.php?mod=main&scr=ajaxcart&evt=addMoreProducts&id_product="+idProduct,function(data)
//	 {
//	 	eval("var json_obiekt = ("+data+")");//wyciąganie z biektu zwróconego z php odpowiednich wartośći z tablicy 
//   		
//		var wholePrice =  json_obiekt['wholePrice'];
//		var privatePrice = json_obiekt['privatePrice'];
//		var countOfElements = json_obiekt['countOfElements'];
//		var toMuch = json_obiekt['toMuch'];
////	alert(toMuch);
////wstawainie do htmla wartości
//		$('#wholePrice').html(wholePrice+" PLN");
//		$('#privatePrice'+idProduct).html(privatePrice);
//		$('#elements'+idProduct).val(countOfElements);
//		$('#toMuch').html(toMuch);
//		
//		if($('#giveOrder').html() == '')
//		{
//			$('#giveOrder').html('<td id="giveOrder"><input type="button" value="złóż zamówienie" onclick="javascript:giveOrder()" /></td>');
//			$('#confirmation').html('');
//		}
//		if(toMuch != "")
//			$('#removeToMuch').css('display','');
//		priceAfterDiscount();
////koniec wstawiania
//	 }) 
}
function priceAfterDiscount()
{
	$.post("index.ajax.php?mod=main&scr=ajaxcart&evt=priceAfterDiscount",function(data)
	{
		$('#priceAfterDiscount').html('<td class=" rightAlg" colspan="4"><span class="blueItalicTxt">'+$doZaplatyPoZnizkach+'</span> <span id="wholePrice" class="greyTxt">  '+data+' '+$PLN+'</span></td>');
		
	});
}
function subProduct(idProduct,wholePrice,countOfElements)
{
//	alert('dshjafd');
		
	$('#removeToMuch').css('display','none');
	 $.ajax({
    	type: 'POST',
   	 	url: 'index.ajax.php?mod=main&scr=ajaxcart&evt=subMoreProducts&id_product='+idProduct,
  		dataType: 'json',
    	success: function(data){ 
		
			var wholePrice =  data['wholePrice'];
			var privatePrice = data['privatePrice'];
			var countOfElements = data['countOfElements'];
	
	//wstawainie do htmla wartości usun_z_koszyka
			if(countOfElements!=0) {	
				$('#wholePrice').html(wholePrice+" PLN");
				$('#privatePrice'+idProduct).html(privatePrice);
			}
			if(countOfElements == 0)
			{
				document.location='usun_z_koszyka,'+idProduct;
			}
			else
			{
				$('#elements'+idProduct).val(countOfElements);
			}
			if($('#giveOrder').html() == '')
			{
				$('#giveOrder').html('<td id="giveOrder"><input type="button" value="'+$zlozZamowienie+'" onclick="javascript:giveOrder()" /></td>');
				$('#confirmation').html('');
			}
			$('#toMuch').html('');
			$('#removeToMuch').css('display','none');
			priceAfterDiscount();
		}
	});	
		
	
//	  $.post("index.ajax.php?mod=main&scr=ajaxcart&evt=subMoreProducts&id_product="+idProduct,function(data)
//	 {
//	 	eval("var json_obiekt = ("+data+")");//wyciąganie z biektu zwróconego z php odpowiednich wartośći z tablicy 
//   		
//		var wholePrice =  json_obiekt['wholePrice'];
//		var privatePrice = json_obiekt['privatePrice'];
//		var countOfElements = json_obiekt['countOfElements'];
//
////wstawainie do htmla wartości usun_z_koszyka
//		if(countOfElements!=0) {	
//			$('#wholePrice').html(wholePrice+" PLN");
//			$('#privatePrice'+idProduct).html(privatePrice);
//		}
//		if(countOfElements == 0)
//		{
//			document.location='usun_z_koszyka,'+idProduct;
//		}
//		else
//		{
//			$('#elements'+idProduct).val(countOfElements);
//		}
//		if($('#giveOrder').html() == '')
//		{
//			$('#giveOrder').html('<td id="giveOrder"><input type="button" value="złóż zamówienie" onclick="javascript:giveOrder()" /></td>');
//			$('#confirmation').html('');
//		}
//		$('#toMuch').html('');
//		$('#removeToMuch').css('display','none');
//		priceAfterDiscount();
//koniec wstawiania
//	 }); 
}

function giveSomeProducts(id_product)
{    

	 countOfElements = $('#elements'+id_product).val();
	 
	 
	 $('#removeToMuch').css('display','none');
	 $.ajax({
    	type: 'POST',
   	 	url: 'index.ajax.php?mod=main&scr=ajaxcart&evt=giveSomeProducts&id_product='+id_product+'&countOfElements='+countOfElements,
  		dataType: 'json',
    	success: function(data){ 
		
		var wholePrice =  data['wholePrice'];
		var privatePrice = data['privatePrice'];
		var countOfElements = data['countOfElements'];
		var toMuch = data['toMuch'];
		
		if(countOfElements<=0)
		{
			document.location='usun_z_koszyka,'+id_product;
		}
		else
		{
			$('#wholePrice').html(wholePrice+ "PLN");
			$('#privatePrice'+id_product).html(privatePrice);
			$('#elements'+id_product).val(countOfElements);
			if (toMuch != '') {
				$('#toMuch').html(toMuch);
				$('#removeToMuch').css('display', '');
			}
		}
		
//	 	alert(elements);
		priceAfterDiscount();
	}
	});
	 
//	 $.post("index.ajax.php?mod=main&scr=ajaxcart&evt=giveSomeProducts&id_product="+id_product+"&countOfElements="+countOfElements,function(data)
//	 {
//	 	eval("var json_obiekt = ("+data+")");//wyciąganie z biektu zwróconego z php odpowiednich wartośći z tablicy 
//	 	
//		
//		wholePrice =  json_obiekt['wholePrice'];
//		privatePrice = json_obiekt['privatePrice'];
//		countOfElements = json_obiekt['countOfElements'];
//		toMuch = json_obiekt['toMuch'];
//		
//		if(countOfElements<=0)
//		{
//			document.location='usun_z_koszyka,'+id_product;
//		}
//		else
//		{
//			$('#wholePrice').html(wholePrice);
//			$('#privatePrice'+id_product).html(privatePrice);
//			$('#elements'+id_product).val(countOfElements);
//			if (toMuch != '')
//				$('#toMuch').html(toMuch);
//			$('#removeToMuch').css('display','');
//		}
//		
////	 	alert(elements);
//		priceAfterDiscount();
//	 });
}

function getColors(product_id){

	//$('#errorProductCount').hide();
	var size = jQuery('#size').val();
	$('.borderRelativeDiv ').children('img').removeAttr('class');
	$('.borderRelativeDiv ').children('img').addClass('imgAbsoluteColor');
	$('.borderRelativeDiv ').children('img').show();
 	$.ajax({
  		type: 'POST',
  		url: 'pobierz_kolory,'+product_id,
		//url: 'index.php?mod=main&scr=ajaxproducts&evt=getColors&product_id='+product_id,
  		dataType: 'json',
  		data: {
			size: size
		},
  		success: function(data){         
	        var obiekts = '<option value="0" selected="selected">-- '+$wybierz+' --</option>';
	        i = 1;
	        for (key in data) {
	            obiekts += '<option value="' + data[key][0] + '" >' + data[key][1] + '</option>';
	            i++;
	        }		        
			
			opt = '<option value="0" > -- '+$wybierz+'-- </option>';
			$('.borderRelativeDiv ').children('img').removeClass('imgAbsoluteColor');
			$('.borderRelativeDiv ').children('img').hide();
		
		      jQuery('#color').empty();
		      jQuery('#color').append(obiekts);
			jQuery('#color').attr('disabled','');

			jQuery('#haft').empty();
			jQuery('#haft').append(opt);
			jQuery('#haft').attr('disabled','true');

			jQuery('#count').empty();
			jQuery('#count').append(opt);
			jQuery('#count').attr('disabled','true');
			
			if(size == 0) {
				
				//$('.addToCartLink').hide();
				$('.addToCartLink'+$lang).addClass('addTocartDisabled'+$lang);	
				$('.addToCartLink'+$lang).attr('href','javascript:void(0)');
			}
				
  		}
 	})
}

function getHafts(product_id){
	
	//$('#errorProductCount').hide();
	$('.borderRelativeDiv ').children('img').removeAttr('class');

	//jQuery('div.haft').hide();

	var size = jQuery('#size').val();
	var color = jQuery('#color').val();
	$('.borderRelativeDiv ').children('img').addClass('imgAbsoluteHaft');
	$('.borderRelativeDiv ').children('img').show();
 	$.ajax({
  		type: 'POST',
  		url: 'pobierz_hafty,'+product_id,
		//url: 'index.php?mod=main&scr=ajaxproducts&evt=getHafts&product_id='+product_id,
  		dataType: 'json',
  		data: {
			size: size,
			color: color
		},
  		success: function(data){   			  
	        obiekts = '<option value="0" selected="selected">-- '+$wybierz+' --</option>';
	        i = 1;
			var brak = false;
			
//			if (data.length == 1 && data.product_parameter_name == 'Nie dotyczy'){
//				brak = true;
//			}
			
			if (data.length > 0) {
				for (key in data) {
					if ((data.length == 1 && (data[key][1]).toLowerCase() == "nie dotyczy")) {
						brak = true;
					}
					obiekts += '<option value="' + data[key][0] + '" >' + data[key][1] + '</option>';
					i++;
					
				}
			}
			
			if(data.length == 0){
					brak = true;		
			}


			
			
			opt = '<option value="0" > -- '+$wybierz+' -- </option>';
			$('.borderRelativeDiv ').children('img').removeClass('imgAbsoluteHaft');
			$('.borderRelativeDiv ').children('img').hide();
			jQuery('#haft').empty();
			jQuery('#haft').append(obiekts);		
			jQuery('#haft').attr('disabled','');

			jQuery('#count').empty();
			jQuery('#count').append(opt);
			jQuery('#count').attr('disabled','true');

			if (brak ==true) {
//				jQuery('div.haft').hide();
//				jQuery('.hafty').hide();
				
//				jQuery('.ilosc').attr({
//					"style":"top:69px;left:24px"
//				});

//				jQuery('.addToCartLink').attr({
//					"style":"top:65px"
//				});

//				jQuery('.selectsContainer').attr({
//					"style":"height:92px"
//				});
			}
			else {
//				jQuery('div.haft').show();
//				jQuery('.hafty').show();

//				jQuery('.ilosc').attr({
//					"style":"top:100px;left:24px"
//				});

//				jQuery('.addToCartLink').attr({
//					"style":"top:96px"
//				});

//				jQuery('.selectsContainer').attr({
//					"style":"height:122px"
//				});
			}

			if(color == 0) {
				//$('.addToCartLink').hide();
				$('.addToCartLink'+$lang).addClass('addTocartDisabled'+$lang);	
				$('.addToCartLink'+$lang).attr('href','javascript:void(0)');
			}

			if(size == 0) {
				//$('.addToCartLink').hide();
				$('.addToCartLink'+$lang).addClass('addTocartDisabled'+$lang);	
				$('.addToCartLink'+$lang).attr('href','javascript:void(0)');
			}
			
			
			if (brak == true && data[0] != 'reset'){
				
				$('#haft').val(data[0].product_parameter_id);
				getCount(product_id);
				$('#count').removeAttr('disabled');
			}			
			else if (data[0] == 'reset'){
				$('#haft').attr('disabled',true);
				$('#count').children(':eq(0)').text(' -- '+$wybierz+' -- ');
			}
  		}
		
 	})
}


function setCount(product_id){
	
	var count = $('#count').val();
	var size = $('#size').val();
	var color = $('#color').val();
	var haft = $('#haft').val();
	var count = $('#count').val();

	$('.addToCartLink'+$lang).attr('href','szybkie_dodawanie,'+product_id+','+size+','+color+','+haft+','+count);
	$('.addToCartLink'+$lang).removeClass('addTocartDisabled'+$lang);
	$('.addToCartLink'+$lang).removeAttr('disabled');
}

function getCount(product_id){

	var size = jQuery('#size').val();
	var color = jQuery('#color').val();
	var haft = jQuery('#haft').val();
	//$('#errorProductCount').hide();
	if (jQuery('.haft').css('display')!="none") {
		$('.borderRelativeDiv ').children('img').addClass('imgAbsoluteCount');
		$('.borderRelativeDiv ').children('img').show();
	}
//	alert(size+' '+color+' '+haft);
 	$.ajax({
  		type: 'POST',
  		url: 'pobierz_ilosc,'+product_id,
		//url: 'index.php?mod=main&scr=ajaxproducts&evt=getCount&product_id='+product_id,
  		dataType: 'json',
  		data: {
			size: size,
			color: color,
			haft: haft
		},
  		success: function(data)
		{   
				$('.borderRelativeDiv ').children('img').removeClass('imgAbsoluteAddToCartLink');
				$('.borderRelativeDiv ').children('img').hide();      
		//alert(data)
			$('.addToCartLink'+$lang).attr('href',"szybkie_dodawanie,"+product_id+","+size+","+color+","+haft);
			//$('.addToCartLink').show();
			$('.addToCartLink'+$lang).removeClass('addTocartDisabled'+$lang);			
			if(size == 0) {
				//$('.addToCartLink').hide();
				$('.addToCartLink'+$lang).addClass('addTocartDisabled'+$lang);	
				$('.addToCartLink'+$lang).attr('href','javascript:void(0)');
			}
			if(color == 0) {
				//$('.addToCartLink').hide();
				$('.addToCartLink'+$lang).addClass('addTocartDisabled'+$lang);	
				$('.addToCartLink'+$lang).attr('href','javascript:void(0)');
			}
			if(haft == 0) {
				//$('.addToCartLink').hide();
				$('.addToCartLink'+$lang).addClass('addTocartDisabled'+$lang);	
				$('.addToCartLink'+$lang).attr('href','javascript:void(0)');
			}
			if(data == 0) {
				//$('.addToCartLink').hide();
				$('.addToCartLink'+$lang).addClass('addTocartDisabled'+$lang);	
				$('.addToCartLink'+$lang).attr('href','javascript:void(0)');
				//$('#errorProductCount').attr('style','position:absolute;right:34px;top:20px;');
				//$('#errorProductCount').show();
			}

			$('#count').empty();
			//$('#count').append('<option value="0">-- wybierz --</option>');			
			if (parseInt(data) > 0){
				for (i = 0; i < parseInt(data); i++){
					if(i<10)
						$('#count').append('<option value="'+(i+1)+'">'+(i+1)+'</option>');
				}
				$('#count').removeAttr('disabled');	
				setCount(product_id);			
			}
			else if (parseInt(data) == 0 && haft != 0){
				$('#count').append('<option value="0">'+$brakProduktow+'</option>');
				//$('.addToCartLink').attr('disabled','disabled');
				$('.addToCartLink'+$lang).addClass('addTocartDisabled'+$lang);	
				$('.addToCartLink'+$lang).attr('href','javascript:void(0)');
			}
			else if (parseInt(data) == 0 && haft == 0){
				$('#count').append('<option value="0">-- '+$wybierz+' --</option>');
				$('#count').attr('disabled','disabled');
				//$('.addToCartLink').attr('disabled','disabled');
				$('.addToCartLink'+$lang).addClass('addTocartDisabled'+$lang);	
				$('.addToCartLink'+$lang).attr('href','javascript:void(0)');
			}			
			
			
			

			
  		}
 	})
}
