function toggledescription(id){
	var d1 = document.getElementById('bundel_shortdescription_'+id);
	var d2 = document.getElementById('bundel_description_'+id);
	if(!d1 || !d2) return;
	if(d1.style.display != 'none'){
		d1.style.display = 'none';
		d2.style.display = '';
	}else{
		d1.style.display = '';
		d2.style.display = 'none';
	}
}

var bundelselecttimeout;
function bundelselect(){
	if(bundelselecttimeout){
		clearTimeout(bundelselecttimeout);
	}
	bundelselecttimeout = setTimeout('bundelselect2()',100);
}
function bundelselect2(){
	// gather selected bundel ids:
	var bunids = [];
	for(var i=0;i<bundeltypes.length;i++){
		var id = getradiovaluebyname('BUN_' + bundeltypes[i]);
		if(id>0){
			bunids.push(id);
		}
		if(bundeltypes[i] == 'Blackberry'){
			if(id > 0){
//				$("#divbbnotice").hide();
			}else{
				$("#divbbnotice").show();
			}
		}
	}
	// show matching top bundel boxes in bestellingoverzicht:
	showtopbundels(bunids);
	
	// show bundelacties:
	showacties(bunids);
	
	// update eenmalige kosten:
	updatekosten(bunids);
	
	// gather selected bundel shortnames:
	var bundelsurl = '';
	for(var i=0;i<bunids.length;i++){
		bundelsurl += '/' + bundelnames[bunids[i]];
	}
	
	// build kies url (bestelknop):
	var url = '';
//	url += baseurl;		// using this will return a good url, but setting a.href = "http://www.bs.nl/" will result in "http://www.bs.nl/www.bs.nl/" in some browsers
	url += '/'+pageurl+'/';
	url += provider + '/' + prijsplan + '/' + abo + '/' + gsm + bundelsurl;
	url += '/winkelwagen';

	// strip double slashes (possible when no phone in sim_only):
	url = url.replace('//','/');
	
	var b0 = document.getElementById('bestelknop0');
	var b1 = document.getElementById('bestelknop1');
	var b2 = document.getElementById('bestelknop2');
	if(b0) b1.href = url;
	if(b1) b1.href = url;
	if(b2) b2.href = url;
}

var topbundels;
function showtopbundels(bunids){
	if(!topbundels){
		topbundels = [];
		var elements = document.getElementsByTagName('*');		// all types? (no worky MSIE 5.5?)
		for(var i=0;i<elements.length;i++){
			if(elements[i] && elements[i].id && elements[i].id.substring(0,11)=='topbundelid'){
				var n = elements[i].id.substring(12);
				topbundels[n] = elements[i];
			}
		}
	}
	// hide all topbundels:
	for(var i in topbundels){
		topbundels[i].style.display = 'none';
	}
	// show selected topbundels:
	for(var i=0;i<bunids.length;i++){
		if(topbundels[bunids[i]]){
			topbundels[bunids[i]].style.display = '';
		}
	}
}

// abo/actie show/hide functions:
var bunacties;	// store elements so we don't have to search the very large DOM on every click
function showacties(bunids){
	if(!bunids) bunids = [];
	if(!bunacties){
		bunacties = [];
		var elements = document.getElementsByTagName('*');		// (no worky MSIE 5.5? if this is a problem use TR or DIV instead (depending on template))
		for(var i=0;i<elements.length;i++){
			if(elements[i] && elements[i].id && elements[i].id.substring(0,8)=='bunactie'){
				var n = elements[i].id.substring(9);
				bunacties[n] = elements[i];
			}
		}
	}
	for(var i in bunacties){
		bunacties[i].style.display = 'none';
		for(var j in bunids){
			if(i==bunids[j]){
				bunacties[i].style.display = '';
				break;
			}
		}
	}
	$('table.bundeltabel tr').removeClass("selectedRow");
	for(var j in bunids){
		$('table.bundeltabel tr#bundel_' + bunids[j]).addClass("selectedRow");
	}
}


function updatekosten(bunids){
	var bundelidsstring = '';
	for(var i=0;i<bunids.length;i++){
		if(i>0) bundelidsstring += ',';
		bundelidsstring += bunids[i];
	}
	var getbtw = 1;
	try{
		if(zakelijk == 1 || zakelijk == '1' || zakelijk == 'true'){		//	zakelijk : '' of '0' of '1'
			getbtw = 0;
		}
	}catch(e){;}
	$.ajax({
		url: baseurl + "/logic/AX_getOrderPrice.php",
		type: "GET",
		data: ({ hw : gsmid, abo : aboid , bundels : bundelidsstring, btw: getbtw}),
		dataType : "text",
		success : function(price) {
			if(price==0){
				price = 'GRATIS';
			}else{
				try{price = priceToString(price, '', '');}catch(e){;}	// try-catch in case priceToString is missing
				price = '&euro;' + price;
			}
			$('#spanprijs').html(price);
		}
	});

}

function getradiovaluebyname(name){
	var value;
	var radios = document.getElementsByName(name);
	for( var j = 0; j < radios.length; j++ ){
		if(radios[j].checked == true){
			value = radios[j].value;
			return value;
		}
	}
}
function setradiovaluebyname(name, value){
	if(!name) return false;
	var value;
	var radios = document.getElementsByName(name);
	for( var j = 0; j < radios.length; j++ ){
		if(radios[j].value == value){
			radios[j].checked = true;
			return true;
		}
	}
	return false;
}

var initdone;
function init(){
	if(initdone) return;
	initdone = true;
	bundelselect();
}

$().ready(function(){
	init();
});

