$(document).ready( function()
{
	/****************************************************
		Assortiment barcodes of artikelen toevoegen
	****************************************************/
	$( '.barcode_toevoegen' ).click( function() 
	{
		var barcode = $( this ).attr( 'id' );
		var artikelnummer = barcode.replace( 'barcode_', '' );
		
		$( this ).attr( 'src', '/images/barcode_added.png' );
					
		$.post('/ajax_barcode_toevoegen.php', { artikelnummer: artikelnummer }, function( data )
		{
			
		});
	});
	
	$( '.favoriet_toevoegen' ).click( function() 
	{
		var favoriet = $( this ).attr( 'id' );
		var artikelnummer = favoriet.replace( 'favoriet_', '' );
		$( this ).attr( 'src', '/images/star_added.png' );
		$.post('/ajax_favoriet_toevoegen.php', { artikelnummer: artikelnummer }, function( data )
		{
			
		});
	});
	
	$( '.favoriet_verwijderen' ).click( function() 
	{
		var favoriet = $( this ).attr( 'id' );
		var artikelnummer = favoriet.replace( 'verwijder_favo_', '' );
		
		$.post('/ajax_favoriet_verwijderen.php', { artikelnummer: artikelnummer }, function( data )
		{
			$( '#favo_row_'+artikelnummer ).hide();
		});
	});
	
	$( '.winkelwagen_toevoegen' ).click( function()
	{
		var winkelwagen = $( this ).attr( 'id' );
		var artikelnummer = winkelwagen.replace( 'winkelwagen_', '' );
		$( this ).attr( 'src', '/images/winkelwagen_added.png' );	
		$( this ).attr( 'class', 'winkelwagen_toevoegen' );	
		$( this ).attr( 'id', winkelwagen );
		
		$.get('/ajax_artikel_toevoegen.php', { artikelnummer: artikelnummer }, function( data )
		{
			$( '#menu_winkel_aantal' ).html( data );
			
		});
	});
	
	$('.winkelwagen_toevoegen').cluetip({
		attribute: 'rel',
		titleAttribute: 'rel',
		splitTitle: '|', // use the invoking element's title attribute to populate the clueTip...
						 // ...and split the contents into separate divs where there is a "|"
		showTitle: false, // hide the clueTip's heading
		activation: "click",
		delayedClose: 1500
	});
	
	/****************************************************
			Barcodes select alles of niets
	****************************************************/
	$( '#select' ).click( function()
	{
		 $("input[type='checkbox']").attr('checked', 'checked');   
	});
	
	$( '#unselect' ).click( function()
	{
		 $("input[type='checkbox']").attr('checked', '');   
	});
	
	/****************************************************
			Bestellen Catalogus artikelen
	****************************************************/
	$( '.bestel_cat_art' ).keyup( function( event ) 
	{
		if( event.keyCode != 9 && event.keyCode != 13 )
		{
			var bestel_regel = $( this ).attr( 'id' );
			var regelnummer = bestel_regel.replace( '_bestel_cat_art', '' );
			delay( function()
			{
				var artikelnummer = $( '#'+bestel_regel ).val();
				$.get( '/ajax_artikel_omschrijving.php', { artikelnummer: artikelnummer }, function( data )
				{
					var tekst = '<product niet gevonden of bestelnummer ongeldig>';
					if( data != '' )
					{
						$( '#'+ regelnummer +'_bestel_cat_oms' ).val( data );
						$( '#'+ regelnummer +'_bestel_cat_aantal' ).val( 1 );
					}
					else
					{
						$( '#'+ regelnummer +'_bestel_cat_oms' ).val( tekst );
						$( '#'+ regelnummer +'_bestel_cat_aantal' ).val( 1 );
					}
				});
			}, 500 );
		}
	});
	
	$( '#form_bestel_uit_catalogus' ).keydown( function( event )
	{
		if( event.keyCode == 13 )
		{
			event.preventDefault();
			return false;
		}
	});
	
	/****************************************************
				Winkelwagen functies
	****************************************************/
	
	$( '.winkelwagen_aantal' ).keyup( function() {
		var td = $( this ).parent();
		var row = td.parent().attr( 'id' );
		var artikelnummer = row.replace( 'winkelwagen_rij_', '' );
		var aantal = $( this ).val();
				
		$.getJSON( '/ajax_artikel_wijzigen.php', { artikelnummer: artikelnummer, aantal: aantal }, function( data )
		{
			$( '#bedrag_' + artikelnummer ).html( '&euro; ' + data.prijs );
			$( '#winkelwagen_totaal_excl' ).html( '&euro; ' + data.totaal_excl );
			$( '#winkelwagen_btw' ).html( '&euro; ' + data.btw );
			$( '#winkelwagen_totaal_incl' ).html( '&euro; ' + data.totaal_incl );
			
			// check of orderkosten erbij/eraf moeten
			if( data.orderkosten == 'false' )
			{
				if( $( '#winkewagen_rij_088888' ) )
				{
					$( '#winkelwagen_rij_088888' ).hide();
				}
			}
			else if( data.orderkosten == 'true' )
			{
				if( $( '#winkelwagen_rij_088888' ).length == 0 )
					location.reload();
				else
					$( '#winkelwagen_rij_088888' ).show();
			}
			
			if( data.punten.punten > 0 )
			{
				if( data.punten.verschil_volgende > 0 && data.punten.verschil_volgende <= 50 )
				{
					$( '#punten_rij' ).html( 'Let op! Als u nog voor &euro; '+ number_format( data.punten.verschil_volgende,2, ",","." ) + ' besteld, krijgt u geen '+ data.punten.punten +' punten, maar '+ data.punten.volgend_punten +' punten!' );
				}
				else
					$( '#punten_rij' ).html( '' );
			}
			else
			{
				$( '#punten_rij' ).html( '' );
			}
		});
	});
		
	$( '#opmerking_winkelwagen' ).keyup( function( event ) 
	{
		var opmerking = $( this ).val();
		delay( function()
		{
			$.post( '/ajax_winkelwagen_opmerking.php', { opmerking: opmerking }, function( data )
			{
				
			});
		}, 250 );
	});
	
	
	$( '#bestelling_afronden' ).click( function()
	{
		
		if( $( "#bestelling_afronden" ).hasClass( 'leveringsvoorwaarden' ) )
		{
			show_popup_voorwaarden();
		}
		else
		{
			$( "#bestelling_afronden" ).unbind( "click" ) ;
			$( "#bestelling_afronden" ).trigger( "click" );
		}
		
		return false;
	});
	/*
	$( '#opmerking_winkelwagen' ).change( function()
	{
		var opmerking = $( this ).val();
		$.post( '/ajax_winkelwagen_opmerking.php', { opmerking: opmerking }, function( data )
		{
			
		});
	});*/
	
	/****************************************************
					Artikel weergave
	****************************************************/
	$( '#productdetail_plaatje' ).lightBox({
		txtImage: ''
	});

	/*
	$( '.artikel' ).click( function() 
	{
		var id = $( this ).attr( 'id' );
		var artikelnummer = id.replace( 'artikel_', '' );
		
		$( '#prod_detail_img' ).attr( "src", "" );
		
		$( '#prod_kalender img' ).attr( "src", "" );
		$( '#prod_kalender img' ).attr( "src", "/product_kalender.php?artikelnummer="+artikelnummer );
		
		$.getJSON( '/ajax_artikel_informatie.php', { artikelnummer: artikelnummer }, function( data )
		{
			$( '#prod_detail_omschrijving' ).html( data.lange_omschrijving );
			$( '#prod_detail_artikelnummer' ).html( data.artikelnummer );
			$( '#prod_detail_fabrikant' ).html( data.merk.omschrijving );
			$( '#prod_detail_art_leverancier' ).html( data.artikel_leverancier );
			$( '#prod_detail_voorraad' ).html( data.voorraad );
			$( '#prod_detail_prijs' ).html( '&euro; '+ number_format( data.verkoopprijs_weergave.verkoopprijs,2, ',','.' ) );
			$( '#prod_detail_extra' ).html( data.extra );
			//$( '#prod_detail_img' ).attr( "src", "/CorimProductImages/resized/"+data.artikelnummer+".jpg" );
			$( '#prod_detail_img' ).attr( "src", "/watermerk.php?path="+data.artikelnummer+".jpg" );
			
			var show_staffel = false;
			if( data.actiestaffel_1_aantal > 0)
			{
				show_staffel = true;
				var staffel_html = '<ul>';
				staffel_html += '<li>'+ data.actiestaffel_1_aantal +' stuks - &euro; '+ number_format( data.actiestaffel_1_prijs,2, ',','.' ) +' per stuk</li>';
				
				if( data.actiestaffel_2_aantal > 0 )
					staffel_html += '<li>'+ data.actiestaffel_2_aantal +' stuks - &euro; '+ number_format( data.actiestaffel_2_prijs,2, ',','.' ) +' per stuk</li>';
				if( data.actiestaffel_3_aantal > 0 )
					staffel_html += '<li>'+ data.actiestaffel_3_aantal +' stuks - &euro; '+ number_format( data.actiestaffel_3_prijs,2, ',','.' ) +' per stuk</li>';
				if( data.actiestaffel_4_aantal > 0 )
					staffel_html += '<li>'+ data.actiestaffel_4_aantal +' stuks - &euro; '+ number_format( data.actiestaffel_4_prijs,2, ',','.' ) +' per stuk</li>';
				staffel_html += '</ul>';
			}
			else if( data.staffel_1_aantal > 0 )
			{
				show_staffel = true;
				var staffel_html = '<ul>';
				staffel_html += '<li>'+ data.staffel_1_aantal +' stuks - &euro; '+ number_format( data.staffel_1_prijs,2, ',','.' ) +' per stuk</li>';
				
				if( data.staffel_2_aantal > 0 )
					staffel_html += '<li>'+ data.staffel_2_aantal +' stuks - &euro; '+ number_format( data.staffel_2_prijs,2, ',','.' ) +' per stuk</li>';
				if( data.staffel_3_aantal > 0 )
					staffel_html += '<li>'+ data.staffel_3_aantal +' stuks - &euro; '+ number_format( data.staffel_3_prijs,2, ',','.' ) +' per stuk</li>';
				if( data.staffel_4_aantal > 0 )
					staffel_html += '<li>'+ data.staffel_4_aantal +' stuks - &euro; '+ number_format( data.staffel_4_prijs,2, ',','.' ) +' per stuk</li>';
				staffel_html += '</ul>';
			}
			
			if( show_staffel && staffel_html > '' )
			{
				$( '#prod_detail_staffel' ).html( staffel_html );
			}
			
			$( '#popup_product_info' ).dialog(
			{
				position: ['center',110],
				width: 700,
				draggable: false,
				resizable: false,
				closeOnEscape: false,
				modal: false,
				title: data.productnaam.omschrijving +': '+ data.lange_omschrijving,
				open: function(event, ui) 
					{
						$(".ui-dialog-titlebar-close").click( function ()
						{ 
							$('#popup_product_info').dialog( "destroy" ); 
							return false;
						}); 
					}	
			});
		});
	});
	*/
	
	/****************************************************
					Banner admin
	****************************************************/
	var check = $( "input[name='type']:checked").val();
	$( '#admin_banner_tekst' ).hide();
	$( '.admin_banner_pdf' ).hide();
	
	if( check == 'tekst' )
	{
		$( '#admin_banner_tekst' ).show();
		$( '#admin_banner_tekst textarea' ).ckeditor();
	}
	else if( check == 'pdf' )
	{
		$( '.admin_banner_pdf' ).show();
	}
	
	$( '.admin_banner_type' ).change( function() 
	{
		var check = $( "input[name='type']:checked").val();
		
		if( check == 'tekst' )
		{
			$( '.admin_banner_pdf' ).hide();
			$( '#admin_banner_tekst' ).show();
			
			$( '#admin_banner_tekst textarea' ).ckeditor();
		}
		else if( check == 'pdf' )
		{
			$( '#admin_banner_tekst' ).hide();
			$( '.admin_banner_pdf' ).show();
		}

	});
	/****************************************************
					Klantnummer vergeten
	****************************************************/
	$( '#klantnummer_vergeten_button' ).click( function()
	{
		var doorgaan = true;
		$( 'input[type=text], textarea' ).each( function()
		{
			if( $( this ).hasClass( 'verplicht_veld' ) )
			{
				if( $( this ).val() == '' )
				{
					doorgaan = false;
					$( this ).css( 'border', '1px solid red' );
				}
				else
					$( this ).css( 'border', 'none' );
			}
		});
		return doorgaan;
	});
	
	/****************************************************
					Klantnummer vergeten
	****************************************************/
	$( '.belmij' ).click( function()
	{
		$( '#div_bel_mij_terug' ).show();
	});
	
	/****************************************************
					Barcode pagina
	****************************************************/
	$( '#barcodes_verwijderen' ).click( function()
	{
		return confirm( 'Weet u zeker dat u de geselecteerde barcodes wilt verwijderen?' );
	});
	
	$( '#barcode_printen_a4' ).click( function()
	{
		var html = '';
		$( 'input[type=checkbox]:checked' ).each( function()
		{
			var artikelnummer = $( this ).attr( 'name' );
			html += '<li><img border="0" align="middle" src="/barcodes/'+ artikelnummer +'.jpg"></li>'
		});
		$( '#barcode_print_list' ).html( html );
		window.print();
	});
	
	
	/****************************************************
					Helpbutton
	****************************************************/
	var showOrHide = true;
	$( '.helpbutton' ).click( function()
	{
		var pagina = $( this ).attr( 'rel' );
		var tekst_id = pagina.replace( 'help_', '' );
				
		if( showOrHide == true )
		{
			$( '#div_help_pagina' ).html( '' );
			$.getJSON( '/ajax_help_informatie.php', { pagina: tekst_id }, function( data )
			{
				var tekst = data.tekst;
				var sluit_knop = '<span id="help_sluiten">Sluit venster <img src="/images/CloseButton.gif"></span>';
				
				$( '#div_help_pagina' ).html( sluit_knop + tekst );
				$( '#div_help_pagina' ).show();
				showOrHide = false;
				
				$( '#div_help_pagina #help_sluiten' ).click( function()
				{
					$( '#div_help_pagina' ).hide();
						showOrHide = true;
				});
			});
		}
		else if( showOrHide == false )
		{
			$( '#div_help_pagina' ).hide();
			showOrHide = true;
		}
	});
	
	$("#tabel_orderhistorie").tablesorter(
	{
		dateFormat: "uk",
		sortList: [[0,0],[2,0]] 
	}); 	
});

function show_popup_voorwaarden()
{
	$( '#popup_voorwaarden' ).dialog(
	{
		position: ['center',110],
		width: 630,
		draggable: false,
		resizable: false,
		closeOnEscape: false,
		modal: true,
		title: 'Algemene Verkoop- en leveringsvoorwaarden',
		buttons: { "I Agree": function()
					{
						var voorwaarde = false;
						
						$("input:checked").each( function()
						{
							if( $( this ).attr( 'id' ) == 'algemene_voorwaarden' )
								voorwaarde = true;
						});
						
						if( voorwaarde )
						{
							$.get( "/ajax_accepteer_voorwaarden.php" );
							
							$(this).dialog( "destroy" );
							$( "#bestelling_afronden" ).unbind( "click" ) ;
							$( "#bestelling_afronden" ).trigger( "click" );
							return false;
						}
						else
						{
							alert( 'De voorwaarde moeten worden geaccepteerd alvorens er besteld kan worden.' );
						}
					}
				}	
	});
}
function number_format (number, decimals, dec_point, thousands_sep) {
    // Formats a number with grouped thousands  
    // 
    // version: 1102.614
    // discuss at: http://phpjs.org/functions/number_format    // +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     bugfix by: Michael White (http://getsprink.com)
    // +     bugfix by: Benjamin Lupton
    // +     bugfix by: Allan Jensen (http://www.winternet.no)    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +     bugfix by: Howard Yeend
    // +    revised by: Luke Smith (http://lucassmith.name)
    // +     bugfix by: Diogo Resende
    // +     bugfix by: Rival    // +      input by: Kheang Hok Chin (http://www.distantia.ca/)
    // +   improved by: davook
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +      input by: Jay Klehr
    // +   improved by: Brett Zamir (http://brett-zamir.me)    // +      input by: Amir Habibi (http://www.residence-mixte.com/)
    // +     bugfix by: Brett Zamir (http://brett-zamir.me)
    // +   improved by: Theriault
    // +      input by: Amirouche
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)    // *     example 1: number_format(1234.56);
    // *     returns 1: '1,235'
    // *     example 2: number_format(1234.56, 2, ',', ' ');
    // *     returns 2: '1 234,56'
    // *     example 3: number_format(1234.5678, 2, '.', '');    // *     returns 3: '1234.57'
    // *     example 4: number_format(67, 2, ',', '.');
    // *     returns 4: '67,00'
    // *     example 5: number_format(1000);
    // *     returns 5: '1,000'    // *     example 6: number_format(67.311, 2);
    // *     returns 6: '67.31'
    // *     example 7: number_format(1000.55, 1);
    // *     returns 7: '1,000.6'
    // *     example 8: number_format(67000, 5, ',', '.');    // *     returns 8: '67.000,00000'
    // *     example 9: number_format(0.9, 0);
    // *     returns 9: '1'
    // *    example 10: number_format('1.20', 2);
    // *    returns 10: '1.20'    // *    example 11: number_format('1.20', 4);
    // *    returns 11: '1.2000'
    // *    example 12: number_format('1.2000', 3);
    // *    returns 12: '1.200'
    // *    example 13: number_format('1 000,50', 2, '.', ' ');    // *    returns 13: '100 050.00'
    number = (number + '').replace(',', '').replace(' ', '');
    var n = !isFinite(+number) ? 0 : +number,
        prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
        sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,        dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
        s = '',
        toFixedFix = function (n, prec) {
            var k = Math.pow(10, prec);
            return '' + Math.round(n * k) / k;        };
    // Fix for IE parseFloat(0.55).toFixed(0) = 0;
    s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
    if (s[0].length > 3) {
        s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);    }
    if ((s[1] || '').length < prec) {
        s[1] = s[1] || '';
        s[1] += new Array(prec - s[1].length + 1).join('0');
    }    return s.join(dec);
}

var delay = (function(){
  var timer = 0;
  return function(callback, ms){
    clearTimeout (timer);
    timer = setTimeout(callback, ms);
  };
})();

