// begin cloaking from non-JavaScript browsers. 
// Written by: Paul DeBrino of Infinity Research and Development.
// Email:   IRandD@aol.com-->

// Check whether element exists on form
function elementIsInString(element, num) {
	if (element == "" || num == "") return false;
	num = 'Aantal' + num;
	return (element == num);
}

//Check which Textbox is selected 
function checkChoice(whichbox) {
	with (whichbox.form)
	{
	whichbox.priorval = 0;	

	for(i=1 ; i < document.myform.elements.length ; i++){
		if (elementIsInString(whichbox.name, i)) break ;
		}
	// Combine correct string 
	Bedrag = 'Bedrag' + i;
	Prijs = 'Prijs' + i;

	// Check whether value is a number and no decimal
	if (isNaN(whichbox.value)) 
		{ 
		whichbox.value=whichbox.priorval;
		whichbox.focus();
		}
	whichbox.value=Math.abs(whichbox.value);

	var dec = whichbox.value.indexOf('.', 1)
	if (dec > 0)
		{ 
		alert('Geen decimalen toegestaan voor \"' +whichbox.name +'\" !');
		whichbox.value=whichbox.priorval;
		whichbox.focus();
		}

	// Calculate Total per Row
	document.myform.elements[Bedrag].value = 0;
	document.myform.elements[Bedrag].value = eval(document.myform.elements[Bedrag].value) - eval(document.myform.elements[Prijs].value * whichbox.priorval);
	whichbox.priorval=whichbox.value;
	document.myform.elements[Bedrag].value = eval(document.myform.elements[Bedrag].value) + eval(document.myform.elements[Prijs].value * whichbox.value);
	return formatCurrency(document.myform.elements[Bedrag].value);

	}
}

// Format number into correct Currency only use if changing to Euro
function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num)) num = "0";
	cents = Math.floor((num*100+0.5)%100);
	num = Math.floor((num*100+0.5)/100).toString();
	if(cents < 10) cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++) 
		{	num = num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3)); }
	return(num +'.'+cents);

}

// Calculate total value of all rows
function MakeFinalTotal() {

	document.myform.Totaal.value = 0;
	for (xx=1; xx < document.myform.elements.length ; xx++){ 
		Str = 'Bedrag' + xx ;
		if(document.myform.elements[Str])
			document.myform.Totaal.value = formatCurrency(eval(document.myform.Totaal.value) + eval(document.myform.elements[Str].value));
		}
}

// Initialize values on form
function InitForm() {

	document.myform.Totaal.value=0;

	for (xx=1; xx < document.myform.elements.length ; xx++)
		{
		Str = 'Bedrag' + xx ;
		if ((document.myform.elements[Str]) && (document.myform.elements[Str].name == Str) && (document.myform.elements[Str].type == 'text'))
			{
			document.myform.elements[Str].value = 0;
			}
		else
			break;
	   }
}