var webshopuri = 'webshop';


$j(document).ready(
function()
{

    $j('.updateclick').click(function() {
        $j.post('/webshopaction/savecustomer', {
							nameOfField : $j(this).attr('name'),
							valueOfField : $j(this).is(":checked")
						}, function(data) {  });
    });
	
	var fncEditMe = function()
	{
		var error_td = $j(this);
		error_td.unbind('click');
		var error_id = error_td.attr('id');
		var data_id = error_id.substr(0,error_id.length - 5) + 'data';
		var data_input = error_id.substr(0,error_id.length - 5) + 'input';
		var data_td = $j('#' + data_id);
		var current_data = data_td.html();
		var fieldname = error_id.substr(14,error_id.length - 20);
		var blnEdit = true;
		
		
		
		error_td.html('');
//		error_td.html('<img src="/images/icons/web/grey/PNG/action_delete.png" alt="Annuleer bewerking" />');
		data_td.html('<input type="text" id="' + data_input + '" value="' + current_data + '" style="width: 245px; border: 0px; font-family: Verdana; font-size: 12px; margin: 0px; margin-left: -1px; padding: 0px;" />');


		$j('#' + data_input).blur(function(){
			$j('#' + data_input).unbind('blur');
			
			var fncRestoreErrorTd = function()
			{
				error_td.html('<img src="/images/icons/web/grey/PNG/reply.png" alt="Annuleer bewerking" style="cursor:pointer" />');
				error_td.click(fncEditMe);
			};
			
			var fncSaveField = function(data)
			{
				if(data.error == 1)
				{
					alert(data.message);
				}
				error_td.html('<img src="/images/icons/web/grey/PNG/reply.png" alt="Annuleer bewerking" style="cursor:pointer" />');
				error_td.click(fncEditMe);
				if(fieldname == 'postalzip')
				{
					$j.post(document.location.href, 
							{ 'getpaymethod' : 'true' }, function(data) { $j('#register_form_company_data').html(data); });
				}
			};
			
			var fncSave = function()
			{
				
				if(blnEdit)
				{
					data_td.html($j('#' + data_input).val());
					error_td.unbind('click');
					if(current_data != data_td.html())
					{
						error_td.html('<img src="/images/icons/ajax-loader.gif" alt="Aan het opslaan..." />');
						
						$j.post('/webshopaction/savecustomer', {
							nameOfField : fieldname,
							valueOfField : data_td.html()
						}, fncSaveField, "json");
						
					}
					else
					{
						fncRestoreErrorTd();
					}
				}
			}
			fncSave();
//			setTimeout(fncSave, 500);
		});
		
//		error_td.click(function() {
//			
//			blnEdit = false;
//			$j('#' + data_input).unbind('blur');
//			error_td.unbind('click');
//			error_td.html('<img src="/images/icons/web/grey/PNG/reply.png" alt="Annuleer bewerking" />');
//			data_td.html(current_data);
//			error_td.click(fncEditMe);
//		});
		
		$j('#' + data_input).focus();
	}
	
	
	$j('.step2').click(function() {
		window.location.href = "/" + webshopuri + "/klantgegevens";
		
	});
	
	$j('.step1').click(function() {
		window.location.href = "/" + webshopuri + "/afrekenen";
		
	});
	
	$j('.print').click(function() {
		window.print();
		return false;
		
	});
	
	$j('.step3').click(function() {
		window.location.href = "/" + webshopuri + "/betaling-en-levering";
		
	});
	
	$j('.registerError').show();
	
	$j('.registerError').click(fncEditMe);
	
	var fncSlideProduct = function() {
		$j(this).blur();
		$j('tr.edit_form_' + $j(this).attr('productid')).toggle();
		
		return false;
	};
		
	$j('a.slideProduct').click(fncSlideProduct);
	
	
	$j('.payment_costs').click(function() {
		$j('#payment_costs_td').html('&nbsp;&euro; ' + $j(this).attr('costs'));
	});

        var myCounter = 0;
        var fncUpdateTotals = function() {
            
            var value = 0;
            var totalvalue = 0;
            var cents = 0;
            if($j('.base_product').val()) {
                totalvalue += parseInt($j('.base_product').val() * $j('.base_product').attr('price'));
                value = parseInt(parseInt($j('.base_product').val() * $j('.base_product').attr('price')) / 100);
                cents = parseInt(parseInt($j('.base_product').val() * $j('.base_product').attr('price')) % 100);
                if(cents < 10) {
                    cents = '0' + cents;
                }
                $j('.product_totals .product_price').text(value + ',' + cents);
            }
            else {
                $j('.product_totals .product_price').text('-,--');
            }
            
            $j('.item-title-selected').each(function() {
                if(!$j(this).attr('id')) return true;
               var container = $j('#' + $j(this).attr('id').replace('title', 'container'));
               var value = 0;
               var value2 = 0;
               var cents = 0;
               var key = container.find('.removeProduct').attr('mytype');
               
               container.find('.product_sale_count').each(function() {
                   
                   value += parseInt($j(this).val()) * parseInt($j(this).attr('price'));

               });
               
               totalvalue += value;
               value2 = parseInt(parseInt(value) / 100);
                cents = parseInt(parseInt(value) % 100);
                if(cents < 10) {
                    cents = '0' + cents;
                }
               $j('.product_totals .cat_' + key).text(value2 + ',' + cents);
            });

            $j('.item-title').each(function() {
                var key = $j(this).attr('id').substr(6);
                $j('.product_totals .cat_' + key).text('-,--');
            });
//            alert(totalvalue);
            if(totalvalue == 0) {

                $j('.product_totals .total_price').text('-,--');
            }
            else {
                value = parseInt(parseInt(totalvalue) / 100);
                cents = parseInt(parseInt(totalvalue) % 100);
                if(cents < 10) {
                        cents = '0' + cents;
                    }
                $j('.product_totals .total_price').text(value + ',' + cents);
            }

        }
	
	
	function bindCounter()
	{
		$j('.product_sale_count').unbind('keyup');
		$j('.product_sale_totalcount').unbind('keyup');
		$j('.product_sale_totalcount').unbind('blur');
		
		$j('.product_sale_count').keyup(function() {
			if(parseInt($j(this).val()))
			{
				$j('#product_sale_totalcount_' + $j(this).attr('pro_id')).val(parseInt(parseInt($j(this).val()) * $j(this).attr('factor') * 100) / 100);
			}
			else
			{
				$j('#product_sale_totalcount_' + $j(this).attr('pro_id')).val('0');
			}
                        fncUpdateTotals();
		});
	
		$j('.product_sale_totalcount').keyup(function() {
			if(parseFloat($j(this).val()))
			{
				$j('#product_sale_count_' + $j(this).attr('pro_id')).val(parseInt((parseFloat($j(this).val()) - .01) / $j('#product_sale_count_' + $j(this).attr('pro_id')).attr('factor')) + 1);
			}
			else
			{
				$j('#product_sale_count_' + $j(this).attr('pro_id')).val("0");
			}
                        fncUpdateTotals();
		});
		
		$j('.product_sale_totalcount').blur(function() {
			$j('#product_sale_totalcount_' + $j(this).attr('pro_id')).val(parseInt($j('#product_sale_count_' + $j(this).attr('pro_id')).val() * $j('#product_sale_count_' + $j(this).attr('pro_id')).attr('factor') * 100) / 100);
//			$j('#product_sale_totalcount_' + $j(this).attr('pro_id')).val(parseInt(parseInt($j(this).val()) * $j(this).attr('factor') * 100) / 100);
                        fncUpdateTotals();
		});
		
		$j('.removeProduct').click(function() {
                        var container = $j(this).parent().parent();
			$j(this).parent().remove();
			if($j('#container_' + $j(this).attr('myType')).html() == '')
			{
				$j('#title_' + $j(this).attr('myType')).attr('class', 'item-title');
			}
                        if(!container.find('.item').html()) {
                            container.html('');
                        }
                        
                        fncUpdateTotals();
			return false;
		});

                fncUpdateTotals();
	}
	
	bindCounter();
	
	 $j(document).bind('reveal.facebox', function() {
		 
		 if($j('#facebox .face_new_footer').html())
	 	{
	 		$j('#facebox .footer').html($j('#facebox .face_new_footer').html());
	 		$j('#facebox .face_new_footer').remove();
	 	}
		 
		 
		 $j('#facebox .addtoList:first').css('font-weight', 'bold');
	
		 $j('.addtoList').click(function(event) {
			 	event.preventDefault();
			 	$j('.product_add').attr('product_class', $j(this).attr('id'))
		
		 		$j('#facebox .product_image').attr('src', $j(this).attr('product_image'));
		 		$j('#facebox .product_text').html('<strong>' + $j(this).text() + '</strong><br />' + $j(this).attr('product_text'));
		 		
		 		$j('.addtoList').each(function() {
		 			$j(this).css('font-weight', 'normal');
		 		});
		 		
		 		$j(this).css('font-weight', 'bold');
		 		fncUpdateTotals();
		 	});
		 
		 
		 $j('a.close').click(function(e) {
			 e.preventDefault();
			 $j(document).trigger('close.facebox');
		 })
		 
	 	$j('.product_add').click(function(e) {
                        e.preventDefault();
	 		
	 		
	 		$j(document).trigger('close.facebox');
	 		if($j('#product_orderer_' + $j('#' + $j(this).attr('product_class')).attr('pro_id')).attr('id') != 'product_orderer_' + $j('#' + $j(this).attr('product_class')).attr('pro_id'))
	 		{
		 		var type = $j('#' + $j(this).attr('product_class')).attr('type');
		 		
		 		if($j('#' + $j(this).attr('product_class')).attr('total_unit') == $j('.base_product').attr('units'))
		 		{
		 			var intCount = parseInt($j('.base_product').val() * $j('.base_product').attr('factor') / $j('#' + $j(this).attr('product_class')).attr('factor')) + 1;
		 			var intTotalCount = intCount * $j('#' + $j(this).attr('product_class')).attr('factor');
		 		}
		 		else
		 		{
		 			var intCount = 1;
		 			var intTotalCount = 1;
		 		}	
		 		
		 		
		 		//alert(type);
		 		//alert($j('container_' + type).attr('id'));
                                
		 		var strPricesaleunits;
		 		var strPricerelative;
		 		if($j('#' + $j(this).attr('product_class')).attr('price').length)
		 		{ 
		 			strPricesaleunits  = '(&euro; ' + number_format($j('#' + $j(this).attr('product_class')).attr('price') / 100, 2, ',') + ' per ' + $j('#' + $j(this).attr('product_class')).attr('unit_single') + ')';
		 			strPricerelative  = '(&euro; ' + number_format($j('#' + $j(this).attr('product_class')).attr('price') / $j('#' + $j(this).attr('product_class')).attr('factor') / 100, 2, ',') + ' per ' + $j('#' + $j(this).attr('product_class')).attr('total_unit_single') + ')';
		 		}
                                var price = parseInt($j('#' + $j(this).attr('product_class')).attr('price'));
                                
		 		var strFirst = '<div id="product_orderer_' + $j('#' + $j(this).attr('product_class')).attr('pro_id') + '" class="item"><b>' + $j('#' + $j(this).attr('product_class')).html() +  '</b>(<a href="#" class="removeProduct" myType=' + type + ' pro_id=' + $j('#' + $j(this).attr('product_class')).attr('pro_id') + '>verwijder</a>)<br /><table><tr><td><input type="text" factor="' + $j('#' + $j(this).attr('product_class')).attr('factor') + '" name="count[' + $j('#' + $j(this).attr('product_class')).attr('pro_id') + ']"  price="' + price + '" id="product_sale_count_' + $j('#' + $j(this).attr('product_class')).attr('pro_id') + '" pro_id="' + $j('#' + $j(this).attr('product_class')).attr('pro_id') + '" class="product_sale_count" autocomplete="off" value="' + intCount + '" /></td><th style="line-height: 130%;"><strong>' + $j('#' + $j(this).attr('product_class')).attr('unit') + '</strong> <br />' + strPricesaleunits + '</th>';
		 		var strSecond = '<tr><td><input type="text" id="product_sale_totalcount_' + $j('#' + $j(this).attr('product_class')).attr('pro_id') + '" pro_id="' + $j('#' + $j(this).attr('product_class')).attr('pro_id') + '" class="product_sale_totalcount" autocomplete="off"  name="totalcount" value="' + intTotalCount + '" /></td><th style="line-height: 130%"><strong>' + $j('#' + $j(this).attr('product_class')).attr('total_unit') + '</strong> <br />' + strPricerelative + '</th></tr>';
				var strEnd = '</table></div>'
				if($j('#' + $j(this).attr('product_class')).attr('factor') == '1')
				{
					$j('#container_' + type).append(strFirst + strEnd);
				}
				else
				{
					$j('#container_' + type).append(strFirst + strSecond + strEnd);
				}
                                $j('.more_' + type).remove();
                                $j('#container_' + type).append('<a href="#" class="more_' + type + '" style="margin-left: 89px; margin-bottom: 5px; margin-top: -10px; display: block;">&raquo; meer ' + $j('#title_' + type + ' a').text().substr(10) + '</a>');
                                $j('.more_' + type).click(function(e) {
                                    e.preventDefault();
                                    $j('#title_' + type + ' a').click();
                                });

				bindCounter();
				$j('product_select_' + $j('#' + $j(this).attr('product_class')).attr('pro_id')).attr('active', 'true');
				$j('#title_' + type).attr('class', 'item-title-selected');
                                fncUpdateTotals();
				
			}
	 		
	 		
	 		 
		});
	 	
	 	
	  	
	 });
	
	$j('form#order_product').submit(function(event)
	{		
		$j.post(document.location.href, $j(this).serialize(), function() {});
		if($j('form#order_product #gotourl').val() == '')
		{
			event.preventDefault();
			if($j('.base_product').val() != undefined) // Product with multiple packages?
			{
				// Product with a single packages
				if($j('.base_product').val() && parseFloat($j('.base_product').val().replace(',', '.')) != $j('.base_product').val().replace(',', '.') && $j('.base_product').val() != '')
				{
					return false;
				}
				else if(!$j('.base_product').val())
				{
					return false;
				}
			}
			else
			{
				// Product with multiple packages
				var emptyForm = true;
				$j('.product_sale_totalcount').each(function() {
					if($j(this).val().replace(',', '.') != parseFloat($j(this).val().replace(',', '.')) && $j(this).val() != '')
					{
						return false; // Not numeric
					}
					if($j(this).val() != '')
					{
						emptyForm = false;
					}
				});
				if(emptyForm) return false;
			}
			
			$j.facebox($j('#facebox_order').html());
			
			
			$j('#facebox').find('#goshopping').click(function() {
				
				$j('form#order_product').find('#gotourl').val('goshopping');
				$j('form#order_product').submit();
			});
			
			$j('#facebox').find('#webshopcart').click(function() {
				
				$j('form#order_product').find('#gotourl').val('webshopcart');
				$j('form#order_product').submit();
			});
			return false;
		}
		else
		{
			
		}

	});

        $j('.cartEnlarge').mouseover(function() {
            el = $j(this);
            el.before('<img src="' + el.attr('src') + '" style="position: absolute; margin-left: -30px; border: 1px solid #000; padding: 3px; background: #FFF;" class="activeEnlargement" />');

            $j('.activeEnlargement').mouseout(function() {
                $j(this).remove();
            });


        });


    $j('#input_action_code').keyup(function() {
       $j.post(document.location.href, { action_code: $j(this).val(), request: 'ajax_action_code_update' }, function(data) {
           $j('#action_perc').html(data.actionperc);
           $j('#action_discount').html(data.actiondisc);
           $j('#shopping_total').html(data.shoppingtotal);
       }, "json");
    });

	$j('.facebox').facebox();
	
});
