
var ajax = new Ajax();


 
function addPunten(nStr) {
	nStr += '';
	x = nStr.split(',');
	x1 = x[0];
	x2 = x.length > 1 ? ',' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + '.' + '$2');
	}
	return x1 + x2;
}


	   
function changeToNumber(input){
	if(input.value.length > 0 && !input.value.match(/^[0-9]+$/g)){
    input.value = input.value.replace(/[^0-9]*/g,'');
	}
}
 
function berekenPrijs(id,prijs) {
	var input  = document.getElementById('num-'+id);
	var output = document.getElementById('total-'+id);
	changeToNumber(input);

 	var value = input.value;
 	var total = (value*prijs);
 	total = total.toFixed(2);
 	total = total.replace(/\./,",");
 	total = addPunten(total);
	output.innerHTML = "&euro; "+total;
 }

function getBaseHref()	{
	var baseTag = document.getElementsByTagName("base");
  return baseTag[0].href;
}

function bestel(productId) {
	
	
	document.getElementById('b-'+productId).className="buttonShop ghost";
	document.getElementById('b-'+productId).value="wordt besteld...";
	document.getElementById('b-'+productId).disabled=true;
		
		
	var aantal = document.getElementById('aantal-'+productId).value;
	var baseHref = getBaseHref();
	ajax.requestURI = baseHref+"mc/1/ShopProduct/"+productId+"/ShopProductAjaxBestelAction/webwinkel/bestel.html?aantal="+aantal;
	ajax.onCompletion = refreshCartFromBestelling;
	ajax.run();
}


function pasAan(productId) {
	document.getElementById('b-'+productId).className="buttonAdd ghost";
	document.getElementById('b-'+productId).value="wordt aangepast...";
	document.getElementById('b-'+productId).disabled=true;
	
	
		
	var aantal = document.getElementById('num-'+productId).value;
	var baseHref = getBaseHref();
	ajax.requestURI = baseHref+"mc/1/ShopProduct/"+productId+"/ShopProductAjaxWijzigAantalAction/webwinkel/wijzig.html?aantal="+aantal;
	ajax.onCompletion = refreshCartFromCart;
	ajax.run();
}


function refreshCartFromCart() 
{
	var newInfo = ajax.response.split(";");
	var prodId  = newInfo[0];
	var prijs   = newInfo[2];
	refreshCartSummary(newInfo);
	
	document.getElementById('b-'+prodId).className="buttonAdd ok";
	document.getElementById('b-'+prodId).value = "aangepast!";
		
	setTimeout("labelButton("+prodId+", 'buttonAdd', 'aanpassen')",2000);
	
	document.getElementById('cartTotaalPrijs').innerHTML = prijs;
	
}


function refreshCartSummary(newInfo) {
		var prodId  = newInfo[0];
		var aantal  = newInfo[1];
		var prijs   = newInfo[2];
		
		document.getElementById('totaalartikelen').innerHTML = aantal;
		document.getElementById('totaalbedrag').innerHTML    = prijs;
		document.getElementById('cartSummary').style.visibility = 'visible';
		blinkIt('cartSummary', 16);
}



function refreshCartFromBestelling()	
	{
		var newInfo = ajax.response.split(";");
		var prodId  = newInfo[0];
		
		refreshCartSummary(newInfo);
	
		document.getElementById('b-'+prodId).className="buttonShop ok";
		document.getElementById('b-'+prodId).value = "besteld!";
		
		setTimeout("labelButton("+prodId+", 'buttonShop', 'toevoegen')",2000);
		blinkIt('winkelmandKnop', 16);
	}
	
	function blinkIt(obj, aantal) 
	{		
		flip(document.getElementById(obj));
		aantal--;
		if (aantal==0) return;
		setTimeout("blinkIt('"+obj+"',"+aantal+")",130);
	}
	
	function labelButton(prodId, klasse, val) {
		document.getElementById('b-'+prodId).className=klasse;
		document.getElementById('b-'+prodId).value = val;
		document.getElementById('b-'+prodId).disabled = false;	
		document.getElementById('besteld-'+prodId).innerHTML = 'product aan uw bestelling toegevoegd'; 
	}
	
	function flip(obj) {
		if (!obj) return;
		var cname = obj.className;
		var lijst = cname.split(" "); 
		var active = false;
		var strClass = new String();
		
		for (i=0;i<lijst.length;i++) {
			if (lijst[i] == 'active') {
				active = true;
				lijst[i] = null;
				break;	
			}
			strClass += lijst[i]+" ";
		}	
		if (!active) {
			strClass += "active";
		}	
		obj.className = strClass;
}

function toggleAdres(obj) {
		if (!obj.checked) {
			document.getElementById('afleverAdres').style.display="block";
		} else {
			document.detailsForm.afleverAdres.value = "";
			document.detailsForm.afleverHuisnummer.value = "";
			document.detailsForm.afleverPostcode.value = "";
			document.detailsForm.afleverPlaats.value = "";
			document.detailsForm.afleverContactpersoon.value = "";
			document.detailsForm.afleverTelefoon.value = "";
			document.detailsForm.afleverEmail.value = "";
			document.getElementById('afleverAdres').style.display="none";
		}
	}
	
	function checkPasswordsAndSubmit() {
		if(document.detailsForm.wachtwoord.value == document.detailsForm.wachtwoord2.value) {
			document.detailsForm.submit();
		} else {
			alert("De door u ingevoerde wachtwoorden komen niet overeen.");
			return false;
		}
	}