var calculator = {
	basic : 0,
    price : 0,
    discounts_count : 0,
    discounts : new Array(),
    discount: 0,
	currentAction: '',
	
	get_discount : function()
	{
	    var temp_price        = $('current_price').innerHTML.split(' ');
	        temp_price        = temp_price[1];
	        temp_price        = temp_price.replace(',','');
	       
	    for(i=0;i<calculator.discounts_count;i++)
		{
		    if(calculator.discounts[i]['percentage'])
		    {
		        minus               = temp_price/100*calculator.discounts[i]['percentage'];
		        calculator.discount = temp_price-minus;
		    }
		    else
		    {
		        calculator.discount = temp_price-calculator.discounts[i]['price'];
		    }
		}
		
		if(calculator.discount != 0)
		{
		    $('current_discount').innerHTML = '&euro; ' + calculator.add_comma_to_number(Math.round(calculator.discount));
		}
	    
	    calculator.discount = 0;
	},
	
	calculate : function()
	{
		calculator.price = 0;
	    inputs = $('prices').getElementsByTagName('input');
	    for(i=0;i<inputs.length;i++)
	    {
	       if(inputs[i].checked == true)
	       {
			   	id               = inputs[i].id.replace('item_','price_');
				calculator.price = calculator.price + parseFloat($F(id));								 
	       }
	    }
	    
		calculator.price = calculator.price;
	    temp_price = '&euro; ' + calculator.add_comma_to_number(calculator.price);
	    $('current_price').innerHTML = temp_price;
		
		calculator.price = calculator.price-calculator.discount;
	    temp_price = '&euro; ' + calculator.add_comma_to_number(calculator.price);
		$('current_discount').innerHTML = temp_price;
	     
	    /* Reset price */
	    calculator.price = 0;
	    
	},
	
	
	set_discount : function(amount)
	{
	    calculator.discount = amount;
	},
	
	add_comma_to_number: function(number)
	{
		number = number.toString();
		strlength = number.length;
		return number.substr(0,strlength-2)+","+number.substr(strlength-2,2);

	},
	
	remove_product:function(id){
		
		if(document.getElementById('basketproduct_'+id) != undefined){
			new Effect.Fade('basketproduct_'+id,{duration:1});
		}	
		url = webhost+"prijscalculator/removeproduct?product_ID="+id;
		new Ajax.Request(url, {
		  method: 'get',
		  onSuccess: function(transport) {
			if(transport.responseText.indexOf('success') == -1){
				alert('Fout tijdens het verwijderen van het product\r\n'+transport.responseText+"?");	
			}
			else
			{
			    window.location = webhost + 'bestel?bekijk=true';
			}
		  }
		});
			
	},
	
	submit_form:function(action){
		calculator.currentAction = action;
		$('#priceForm').request({
			method: 'post',
			onComplete: function(transport){ 
						switch(calculator.currentAction){
							case 'offer':
								window.location = webhost + 'bestel/';
								break;
							default:
								window.location = webhost + 'bestel?bekijk=true';
								break;
						}
			}
		  });
	}
};
