// JavaScript Document

function addUitvoering(intId){
	
	intAantal = $("#uitv_aantal_"+intId).val()
	if(!IsNumeric(intAantal)){
		$("#uitvoering_"+intId).after('<tr class="uitvoering-fout"><td colspan="4">Het ingevoerde aantal is geen nummer</td></tr>');
		$("#uitv_aantal_"+intId).val(1)
		return;
	}
	$.get("/lib/ajax_winkelwagen.php", {id:intId, action:'addUitvoering', aantal:intAantal}, function(data){verwerk(intId, data)});
		
	function verwerk(intId, data){
		//$("#content").html('test');
		
		// code 200 staat voor in orde
		if(data == '200'){
			$("#uitvoering_"+intId).after('<tr class="uitvoering-toegevoegd"><td colspan="4">Uitvoering toegevoegd aan winkelwagen</td></tr>');
			refreshWinkelmand();
		}else{
			$("#uitvoering_"+intId).after('<tr class="uitvoering-fout"><td colspan="4">Uitvoering is wegens een fout niet toegevoegd</td></tr>');	
		}
		
	}
}

function refreshWinkelmand(){
	$.get("/lib/ajax_winkelwagen.php", {id:0, action:'refreshWinkelmand'}, function(data){verwerk(data)});
	$("#winkelmand").attr("style").display = 'block';	
	function verwerk(data){
		//$("#content").html('test');
		
		// code 200 staat voor in orde
		if(data != ''){
			$("#winkelmand").html(data);
		}else{
			$("#winkelmand").html('De winkelmand is niet goed herladen, klik op de refreshknop of F5.');
		}
	}
	refreshWinkelWagen();
}

function refreshWinkelWagen(){
	
	if($("#winkelwagen-content")){
		$.get("/lib/ajax_winkelwagen.php", {id:0, action:'refreshWinkelwagen'}, function(data){verwerk(data)});
		$("#winkelmand").attr("style").display = 'block';
		function verwerk(data){
			//$("#content").html('test');
			
			// code 200 staat voor in orde
			if(data != ''){
				$("#winkelwagen-content").html(data);
			}else{
				$("#winkelwagen-content").html('De winkelmand is niet goed herladen, klik op de refreshknop of F5.');
			}
			getWinkelWagenTotaal();
		}
	}
}

function changeWinkelmandAantal(intId){
	intAantal = $("#aantal_mand_"+intId).val();
	if(!IsNumeric(intAantal)){
		$("#winkelmand_"+intId).after('<tr class="uitvoering-fout"><td colspan="5">Het ingevoerde aantal is geen nummer</td></tr>');
		$("#aantal_mand_"+intId).val(1)
		return
	}
	
	$.get("/lib/ajax_winkelwagen.php", {id:intId, action:'changeWinkelwagenAantal', aantal:intAantal}, function(data){verwerk(data)});
		
	function verwerk(data){
		// code 200 staat voor in orde
		if(data == '200'){
			refreshWinkelmand();
			
		}else{
			$("#winkelmand_"+intId).after('<tr class="uitvoering-fout"><td colspan="5">Het aantal kon niet worden aangepast</td></tr>');
		}
		/*$("#content").html(data);*/
	}
}

function changeWinkelwagenAantal(intId){
	intAantal = $("#aantal_wagen_"+intId).val();
	if(!IsNumeric(intAantal)){
		$("#winkelwagen_"+intId).after('<tr class="uitvoering-fout"><td colspan="5">Het ingevoerde aantal is geen nummer</td></tr>');
		$("#aantal_wagen_"+intId).val(1)
		return
	}
	
	$.get("/lib/ajax_winkelwagen.php", {id:intId, action:'changeWinkelwagenAantal', aantal:intAantal}, function(data){verwerk(data)});
		
	function verwerk(data){
		// code 200 staat voor in orde
		if(data == '200'){
			refreshWinkelmand();
		}else{
			$("#winkelwagen_"+intId).after('<tr class="uitvoering-fout"><td colspan="5">Het aantal kon niet worden aangepast</td></tr>'+data);
		}
		/*$("#content").html(data);*/
	}
}

function changeDelivery(intVal){
	$.get("/lib/ajax_winkelwagen.php", {id:0, action:'changeDelivery', waarde:intVal}, function(data){verwerk(data)});
		
	function verwerk(data){
		// code 200 staat voor in orde
		if(data == '200'){
			refreshWinkelmand();
		}else{
			$("#aflevering-afhalen-li").after('<li class="uitvoering-fout">De aflevering keuze kan niet worden gewijzigd. Probeer opnieuw of meld dit bij uw opmerkingen.</li>'+data);
		}
		/*$("#content").html(data);*/
	}
}

function deleteWinkelWagenItem(intId){
	$.get("/lib/ajax_winkelwagen.php", {id:intId, action:'deleteWinkelWagenItem'}, function(data){verwerk(data)});
		
	function verwerk(data){
		// code 200 staat voor in orde
		if(data == '200'){
			refreshWinkelmand();
		}else{
			alert(data);	
		}
		/*$("#content").html(data);*/
	}
}

function getWinkelWagenTotaal(){
	$.get("/lib/ajax_winkelwagen.php", {id:0, action:'getWinkelWagenTotaal'}, function(data){verwerk(data)});
	
	function verwerk(data){
		// code 200 staat voor in orde
		if(data){
			$("#winkelwagen-totaal-group").html(data);
		}
		/*$("#content").html(data);*/
	}
}

function addOmschrijving(intId){
	$.get("/lib/ajax_winkelwagen.php", {id:intId, action:'addOmschrijving', opmerkingen:$("#"+intId).val()}, function(data){verwerk(intId, data)});
		
	function verwerk(intId, data){
		//$("#content").html('test');
		
		// code 200 staat voor in orde
		if(data == '200'){
			
		}else{
			$("#"+intId).val(data);	
		}
		
	}
}

function IsNumeric(sText)

{
	var ValidChars = "0123456789.";
	var IsNumber=true;
	var Char;
	
	
	for (i = 0; i < sText.length && IsNumber == true; i++) 
	  { 
	  Char = sText.charAt(i); 
	  if (ValidChars.indexOf(Char) == -1) 
		 {
		 IsNumber = false;
		 }
	  }
	return IsNumber;

}

