function updatePrices(){
	// get all quantities

	var quantities = document.getElementsByName("qty[]");
	var prices = document.getElementsByName("price[]");
	var discounts = document.getElementsByName("discountprice[]");
	var totalEls = [document.getElementById('price_element1') , document.getElementById('price_element2')];
	var discountEls = [document.getElementById('discount_element1') , document.getElementById('discount_element2')];
	var priceEls = [document.getElementById('pay_element1') , document.getElementById('pay_element2')];
	
	var qty;
	var price;
	var discount;
	var totalPrice = 0;
	var actualPrice = 0;
	var totalSaving;
	
	if(((prices.length + discounts.length + quantities.length) / 3) !== prices.length){
		return;
	}
	
	for(var a=0; a < quantities.length; a++){
		qty = isNaN(quantities[a].value) ? 0 : Math.round(Number(quantities[a].value));
		qty = qty > 0 ? Math.min(qty, 1000) : 0;
		
		price = isNaN(prices[a].value) ? 0 : Number(prices[a].value);
		discount = isNaN(discounts[a].value) ? 0 : Number(discounts[a].value);
		
		totalPrice += (price * qty);
		actualPrice += discount > 0 ? discount * qty : price * qty;
	}
	
	totalPrice = (totalPrice);
	actualPrice = (actualPrice );
	discountPrice = (totalPrice - actualPrice);

	for(var a = 0; a < totalEls.length; a++){
		totalEls[a].innerHTML = "&pound;" + formatNumber(totalPrice, 2);
	}
	
	for(var a = 0; a < priceEls.length; a++){
		priceEls[a].innerHTML = "&pound;" + formatNumber(actualPrice, 2);
	}
		
	for(var a = 0; a < priceEls.length; a++){
		discountEls[a].innerHTML = "&pound;" + formatNumber(discountPrice , 2);
	}
	
	
}

function setGrape(gvid,col){
	if(strstr($('ws_sel_grape').value,'|' + gvid + '|')){
		$('grape-'+gvid).className = col;
		var newval = $('ws_sel_grape').value;
		newval = newval.split('|' + gvid + '|').join('|');
		$('ws_sel_grape').value = newval;		
	}else{
		$('grape-'+gvid).className = col + '-selected';
		$('ws_sel_grape').value = $('ws_sel_grape').value + gvid + '|';
	}

	if($('ws_sel_grape').value != "|"){
		$('ws_bottles').style.display = 'block';
	}
}


function setBottles(botid){
	$('bot3').className = '';
	$('bot6').className = '';
	$('bot12').className = '';
	$('bot'+botid).className = 'selected';
	$('ws_sel_bottles').value = botid;
	$('ws_maxprice').style.display = 'block';
}


function setPrice(prid){
	$('pri5').className = '';
	$('pri10').className = '';
	$('pri15').className = '';
	$('pri'+prid).className = 'selected';
	$('ws_sel_price').value = prid;
	$('ws_findwine').style.display = 'block';
}


function findWine(){
	$('ws_find').className = 'ws_loading';
	$('ws_find').innerHTML = '<img src="/images/winederful/loading.gif" alt="Loading Wines" id="ws_load_gif" />';
	var pars = 'ajax=true';
	var qrystr = '?grapes=' + $('ws_sel_grape').value;
	qrystr += '&bottles=' + $('ws_sel_bottles').value;
	qrystr += '&price=' + $('ws_sel_price').value;
	
	var myAjax = new Ajax.Updater(
	  $('ws_results'),
	  'winderfulresults.php' + qrystr,
	  {
		method: 'get',
		onComplete: findWineDone()
	  });
}

function findWineDone(){
	setTimeout("$('ws_find').innerHTML = '';$('ws_find').className = '';",500);
}


function strstr(haystack,needle,bool) {
    var pos = 0;
    haystack += '';
    pos = haystack.indexOf(needle);
    if(pos == -1){
        return false;
    }else{
		if(bool){
            return haystack.substr(0, pos);
        }else{
            return haystack.slice(pos);
        }
    }
}
