var xmlhttp;
var orderpriceprevious = -1;
var orderpriceoriginal = -1;	// update this in template
var HW =  0;					// update this in template
var ABO = 0;					// update this in template
var attentionmessage = ' <small>(met bundelkorting!)</small>';
var attentionblinkcolor = '#f4f4f4';
var doattentionmessage = false;
var doattentionblink = false;

// setradiovalue: used in bundelupsell.tpl (MSIE has timing troubles with the <label for="name"> and getting radio value afterwards)
function setradiovalue(name,value){
	var objs = document.getElementsByName(name);
	for(var i=0;i<objs.length;i++){
		if(objs[i].value == value){
			objs[i].checked = true;
			return true;
		}
	}
	return false;
}

function getradiovalue(name){
	var objs = document.getElementsByName(name);
	for(var i=0;i<objs.length;i++){
		if(objs[0].checked) return objs[0].value;
	}
	return null;
}

//  bundelonchange() called from:
// <input onchange="bundelonchange()"> and from 
// <tr onmouseup="...;bundelonchange()">
// wait a little while to only process latest click and give radio buttons a chance to update before requesting their values:
var bundelonchangetimeout;
function bundelonchange(){
	if(bundelonchangetimeout) clearTimeout(bundelonchangetimeout);
	bundelonchangetimeout = setTimeout('bundelonchange2()',100);
}

function bundelonchange2(){
	// HW  = ... (set in template)
	// ABO = ... (set in template)
	// find selected bundels by checking form:
	var BUNcnt = 0;
	var BUNsstr = '';
	var BUNS = [];
	for(var i=0;i<9;i++){
		BUNS[i] = document.getElementsByName("BUN_"+i);
		if(BUNS[i] && BUNS[i].length > 0){
			BUNcnt = (i+1);	// BUNcnt= Math.max(i,BUNcnt);
			for(var j=0;j<BUNS[i].length;j++){
				if(BUNS[i][j].checked && BUNS[i][j].value > 0){
					BUNsstr += '&BUN_' + i + '=' + BUNS[i][j].value;
				}
			}
		}
	}

	// build xmlhttp url string:
	var AXurlpath = '/logic/';
	var AXurl = AXurlpath + 'AX_getOrderPrice.php?hw='+HW+'&abo='+ABO+'&BUN_CNT='+BUNcnt+''+BUNsstr;	// comma safe

	// execute AJAX:
	try {
		xmlhttp = window.XMLHttpRequest?new XMLHttpRequest():
		new ActiveXObject("Microsoft.XMLHTTP");
	}
	catch (e) { /* do nothing */ }

	xmlhttp.onreadystatechange = bundelajaxchange;
	xmlhttp.open("GET", AXurl);
	xmlhttp.send(null);
	return true;
}

// bundelajaxchange() called from xmlhttprequest.onreadystatechange
function bundelajaxchange(){
	if(xmlhttp.readyState != 4) return;
	
	var orderprice = xmlhttp.responseText;
	
	// failsafe:
	// if AX_(...).php not found might return 404 source or even entire index.php source
	// don't try to stuff all that into the tiny span
	if(orderprice.length > 100) return;

	orderpriceupdate(orderprice);
}

function orderpriceupdate(orderprice){
	var spanprijs = document.getElementById('spanprijs');
	if(!spanprijs) return;

//	emphasize changes:
	var changestr = '';
	if(doattentionmessage && orderprice != orderpriceoriginal) changestr = attentionmessage;
	if(doattentionblink   && orderprice != orderpriceprevious && orderprice != orderpriceoriginal){
		bundelchangeloopstart(true);			// start (annoying) attention blink loop if value changes
	}
	orderpriceprevious = orderprice;
	
	// actually update price label:
	try{orderprice = priceToString(orderprice, '', '');}catch(e){;}	// try-catch in case priceToString is missing
	spanprijs.innerHTML = ' ' + orderprice + changestr;
}

var bundelchangeloopcnt = 0;
var bundelchangeinterval = null;
function bundelchangeloopstart(startstop){
	bundelchangeloopcnt = 0;
	if(bundelchangeinterval){
		clearInterval(bundelchangeinterval);
		bundelchangeinterval = null;
		if(startstop == false){
			return;
		}
	}
	bundelchangeinterval = setInterval('bundelchangeloop()', 200);
}

function bundelchangeloop(){
	var spanprijs = document.getElementById('spanprijs');
	if(!spanprijs) return;

	bundelchangeloopcnt++;
	if(bundelchangeloopcnt > 6){
		bundelchangeloopstart(false);
		spanprijs.style.border = '';
		spanprijs.style.backgroundColor = '';
	}
//	spanprijs.style.border = (bundelchangeloopcnt % 2 == 0)?'':'1px solid ' + attentionblinkcolor;
	spanprijs.style.backgroundColor = (bundelchangeloopcnt % 2 == 0)?'':attentionblinkcolor;
}


