/*
	## LOGIN ## ---------------------------------------------------------------------------------------
*/

/*	Fix de links op de homepage (links zitten alleen op de pijlen, en moeten op het hele block. <-- */
	$(document).ready(function() {
		   
		$("#loginButton").bind('click', function(i) {		
			var vgebruikersnaam  = $("#gebruikersnaamTop").val();

			var vwachtwoord  = $("#wachtwoordTop").val();
			
			$.post("http://" + window.location.hostname + "/includes/login.php", { gebruikersnaamTop: vgebruikersnaam, wachtwoordTop : vwachtwoord }, function(data){
				if (trim(data) == 'redirect')
				{
					document.location = 'http://www.afzuiging.nl/index.php';
				}
				else if(trim(data) == 'onjuist')
				{
					document.location = 'http://www.afzuiging.nl/index.php?login=onjuist';	
				}
				else if(trim(data) == 'redirect2')
				{
					document.location = 'http://www.afzuiging.nl/partnerbestellingen.php';
				}
				else
				{
					document.location = 'http://www.afzuiging.nl/index.php';
					$(".loginBox").replaceWith(data);
				}
			});
		});
	});
	
/*	end -- */

/* submit form als je op enter drukt bij inloggen <-- */

$(function(){
    $("#wachtwoordTop").keydown(function(e){
        if (e.keyCode == 13) {
			var vgebruikersnaam  = $("#gebruikersnaamTop").val();

			var vwachtwoord  = $("#wachtwoordTop").val();
			
			$.post("http://" + window.location.hostname + "/includes/login.php", { gebruikersnaamTop: vgebruikersnaam, wachtwoordTop : vwachtwoord }, function(data){
				if (trim(data) == 'redirect')
				{
					document.location = 'http://www.afzuiging.nl/index.php';
				}
				else if(trim(data) == 'onjuist')
				{
					document.location = 'http://www.afzuiging.nl/index.php?login=onjuist';	
				}
				else if(trim(data) == 'redirect2')
				{
					document.location = 'http://www.varioparts.nl/partnerbestellingen.php';
				}
				else
				{
					document.location = 'http://www.afzuiging.nl/index.php';
					$(".loginBox").replaceWith(data);
				}
			});
            return false;
        }
    });
});

/* end -- */


/*
	## WINKELWAGEN ## ---------------------------------------------------------------------------------------
*/

/*	Fix de links op de homepage (links zitten alleen op de pijlen, en moeten op het hele block. <-- */
	$(document).ready(function() {
		$("#cartVoegToe").bind('click', function(i) {
												 
			/* De onderstaande if/else is tijdelijk uit omdat alle voorraad op 0 staat 
			if ($("#vooraad").val() > 0)
			{*/
				var aantalvanproduct = $("#aantalVanProduct").val();
				if ($(this).attr('rel') > '')
				{
					var product_id = $(this).attr('rel');
				}
				else
				{
					var product_id = $("#gekozenid").val();
				}
				
				if ( IsNumeric(aantalvanproduct) )
				{
					$.post("/webshop/cart.php", { productid: product_id, aantal: aantalvanproduct }, function(data) {
						$("#pushCartBox").replaceWith(data);
					});
					$("#aantalVanProduct").css('border', 'solid 1px #ACBECC');	
				}
				else
				{
					$("#aantalVanProduct").css('border', 'solid 1px red');	
				}
				
			/*}
			else
			{
				jAlert('Dit artikel is helaas niet op voorraad', 'Voorraad');
			}*/
		});
		
		$(".bestelbt").each(function(e) { 
			$(this).bind('click', function(i) {
				i.stopPropagation();
				var product_id = $(this).attr('rel');
				$.post("/webshop/cart.php", { productid: product_id, aantal: 1}, function(data) {
					$("#pushCartBox").replaceWith(data);
				});
			});
		});
	});
	
	
	function clearCart()
	{
		$.post("/webshop/cart.php", { clearcart: 1 }, function(data) {
			$("#pushCartBox").replaceWith(data);
		});
	}
	
	
	function deleteFromCart(id)
	{
		$.post("/webshop/cart.php", { productid: id, remove: 1 }, function(data) {
			$("#pushCartBox").replaceWith(data);
		});
	}
	
/*	end -- */


/*
	## MONTAGEHULP KEUZE ## ---------------------------------------------------------------------------------------
*/

/*	Fix de links op de homepage (links zitten alleen op de pijlen, en moeten op het hele block. <-- */
	$montagehulp = 'no';
	$(document).ready(function() {
		
		if($("#montageNee img").attr('src') == 'images/yes_off.jpg')
		{
			$montagehulp = 'yes';
			$("#volgendestap").attr('href', 'montagehulpuitleg.php');
		}
		
		$("#montagehulp").bind('click', function(i) {
			if ($montagehulp == 'no')
			{
				$("#montageNee img").attr('src', 'images/no.jpg');
				$("#montageJa img").attr('src', 'images/yes.jpg');
				$.post("/webshop/cart.php", { montagepartner: 'yes'}, function(data) {
					$montagehulp = 'yes';
					$("#volgendestap").attr('href', 'montagehulpuitleg.php');
				});
			}
			else
			{
				$("#montageNee img").attr('src', 'images/no_off.jpg');
				$("#montageJa img").attr('src', 'images/yes_off.jpg');
				$.post("/webshop/cart.php", { montagepartner: 'no'}, function(data) {
					$montagehulp = 'no';
					$("#volgendestap").attr('href', 'afrekenen2.php');
				});
			}
		});
	});
	
/*	end -- */




function trim(value) {
  value = value.replace(/^\s+/,'');
  value = value.replace(/\s+$/,'');
  return value;
}


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;
}
