/*
	Winkelman PC Samenstellen - javascript functies

	Copyright (C) 2004, Chemistry Interactive Media VOF
	http://www.chemistry-interactive.nl
*/

/* Array met product info (prijzen */
var regel_prijzen        = new Array();   // key = id, val = prijs
var regel_product        = new Array();   // key = regelnr, val = productid
var regel_aantal         = new Array();   // key = regelnr, val = aantal

var regel_optie_os       = 0.00;
var regel_optie_garantie = 0.00;
regel_prijzen[0]     = 0.00;

/* setPrijs */
function regel_setPrijs(product_id, prijs)
{
	regel_prijzen[product_id] = prijs;
}

/* setAantal */
function regel_setAantal(regel_nr, aantal)
{
	regel_aantal[regel_nr] = aantal;

	if (regel_aantal[regel_nr] == undefined) regel_aantal[regel_nr] = 1;
	if (regel_product[regel_nr] == undefined) regel_product[regel_nr] = 0;
}

/* setProduct */
function regel_setProduct(regel_nr, product_id)
{
	regel_product[regel_nr] = product_id;
	
	if (regel_aantal[regel_nr] == undefined) regel_aantal[regel_nr] = 1;
	if (regel_product[regel_nr] == undefined) regel_product[regel_nr] = 0;
}

/* getSubtotaal */
function regel_getSubtotaal(regel_nr)
{
	if (isNaN(regel_aantal[regel_nr]))
	{
		regel_aantal[regel_nr] = 1;
	}

	return regel_aantal[regel_nr] * regel_prijzen[regel_product[regel_nr]];
}

/* getTotaal */
function regel_getTotaal()
{
	var totaal = 0;

	/* doorloop alle product keuzes */
	for (regelnr in regel_product)
	{
		totaal += regel_getSubtotaal(regelnr);
	}
	
	return totaal + regel_optie_os + regel_optie_garantie;
}

/*
	Overige functies
*/

/* Update productkeuze */
function pss_updateoptie(oSelect, optienum)
{
	var oForm = oSelect.form;									    // form object
	var iProductID = oSelect.options[oSelect.selectedIndex].value;	// product id uit select

	regel_setProduct(optienum, iProductID);
	pss_tekstupdate_regel(oForm, optienum);
}


/* Update aantal */
function pss_updateaantal(oInput, optienum)
{
	var oForm = oInput.form;
	var nieuwAantal = parseInt(oInput.value);

	/* controleer invoer */
	if (isNaN(nieuwAantal))
	{
		nieuwAantal = 1;
		oForm.elements['optie['+ regelnr +'][aantal]'].value = 1;
	}
	
	regel_setAantal(optienum, nieuwAantal);
	
	pss_tekstupdate_regel(oForm, optienum);
}

/* Regel tekst Update */
function pss_tekstupdate_regel(oForm, regelnr)
{
	/* update regel totaal */
	oForm.elements['optie['+ regelnr +'][totaal_string]'].value = float2string(regel_getSubtotaal(regelnr));
	
	/* update grant total */
	pss_tekstupdate_grant(oForm);
}

/* Grant update */
function pss_tekstupdate_grant(oForm)
{
	oForm.elements['granttotal_string'].value = float2string(regel_getTotaal());
}


/* Installatie besturingssysteem */
function pss_installatiebs(oSelect)
{
	var oForm = oSelect.form;									// form object
	var oVal = oSelect.options[oSelect.selectedIndex].value;	// waarde geselecteerde optie
	var totaaloud = parseFloat(oForm.elements['granttotal'].value);
	var prijsoud = parseFloat(oForm.elements['extra[installatiebs][totaal]'].value);

	/* prijs berekening */
	regel_optie_os = 0;
	if (oVal == 1) { regel_optie_os = 25; }
	
	pss_tekstupdate_grant(oForm);
	
//	oForm.elements['extra[installatiebs][totaal]'].value = optiePrijs;
//	oForm.elements['granttotal'].value = totaaloud - prijsoud + optiePrijs
//	
//	/* string weergave */
//	oForm.elements['granttotal_string'].value = float2string(oForm.elements['granttotal'].value);
//	oForm.elements['extra[installatiebs][totaal_string]'].value = float2string(oForm.elements['extra[installatiebs][totaal]'].value);
}


/* Uitbreiding garantieperiode */
function pss_uitbreidinggarantie(oSelect)
{
	var oForm = oSelect.form;									// form object
	var oVal = oSelect.options[oSelect.selectedIndex].value;	// waarde geselecteerde optie
	var totaaloud = parseFloat(oForm.elements['granttotal'].value);
	var prijsoud = parseFloat(oForm.elements['extra[uitbreidinggarantie][totaal]'].value);

	/* prijs berekening */
	regel_optie_garantie = 0;
	if (oVal == 1) { regel_optie_garantie = 35; }

	pss_tekstupdate_grant(oForm);

//	oForm.elements['extra[uitbreidinggarantie][totaal]'].value = optiePrijs;
//	oForm.elements['granttotal'].value = totaaloud - prijsoud + optiePrijs
//	
//	/* string weergave */
//	oForm.elements['granttotal_string'].value = float2string(oForm.elements['granttotal'].value);
//	oForm.elements['extra[uitbreidinggarantie][totaal_string]'].value = float2string(oForm.elements['extra[uitbreidinggarantie][totaal]'].value);
}

function float2string(getal)
{
	if (getal == 0) return '0,00';
	
	var intround = parseInt(parseFloat(getal) * 100);
	var strround = intround.toString();
	return strround.substring(0, strround.length-2) + "," + strround.substring(strround.length-2);
}