

//general functions for control of price recalculation
//copyright e-Business Systems Limited - www.e-businesssystems.co.uk

//version 3 - 26/03/04

function ebsl_Control_setup2()
{
	ebsl_setup_complete = true;
}






function Bands_control()
{
	this.list_price = 0;
	this.quantity = 0;
	this.price_band_count = 0;
	this.bands = new Array();
	this.Add_band = Add_new_band;
	this.highest_band_start = 0;
	this.is_on_special_offer = false;
	this.special_price = 0;
	this.band_message = "";
	this.Recalculate = Recalculate_prices;
	this.best_price = 0;
}


function Add_new_band(band_no,min_qty,max_qty,price,saving_val,saving_percent,discount_percent)
{
	this.bands[band_no] = new Band(band_no,min_qty,max_qty,price,saving_val,saving_percent,discount_percent);
	
}





function Recalculate_prices()
{
	
	var i;
	var price_band_bgcolor;
	
	this.quantity = document.form1.quantity.value;
	
	for (i = 0; i < this.price_band_count; i++)
	{
		price_band_bgcolor = default_bgcolor;
		
		if ((this.quantity >= this.bands[i].min_quantity) && (this.quantity <= this.bands[i].max_quantity)) 
		{
			price_band_bgcolor = highlight_bgcolor;
			
			if ((this.is_on_special_offer)&&(this.special_price < this.bands[i].price_ex))
			{
				price_ex = this.special_price;	
				if (modern_browser)
				{
					document.getElementById("special_offer_pic").style.visibility = "visible";
					document.getElementById("your_price").innerHTML = '<b><font size="3" color="#FF0000">' + Convert_ntc_fc(Round2((price_ex) + 0.000001)) + '</font></b>';
					document.getElementById("saving_val").innerHTML = '<font color="#666666">' + special_offer_saving_val + '</font>';
					document.getElementById("saving_percent").innerHTML = '<font color="#666666">' + special_offer_saving_percent + '</font>';
					product_discount_percent = 0;
				}
				
			}
			
			if ((this.is_on_special_offer)&&(this.special_price >= this.bands[i].price_ex))
			{
				price_ex = this.bands[i].price_ex;	
				
				if (modern_browser)
				{
					document.getElementById("special_offer_pic").style.visibility = "hidden";
					document.getElementById("your_price").innerHTML = '<b><font size="3" color="#FF0000">' + Convert_ntc_fc(Round2((price_ex) + 0.000001)) + '</font></b>';
					document.getElementById("saving_val").innerHTML = '<font color="#666666">' + this.bands[i].saving_val + '</font>';
					document.getElementById("saving_percent").innerHTML = '<font color="#666666">' + this.bands[i].saving_percent + '</font>';
					product_discount_percent = this.bands[i].discount_percent;

					
				}
			}
			
			if (!(this.is_on_special_offer))
			{
				price_ex = this.bands[i].price_ex;	
				
				if (modern_browser)
				{
					document.getElementById("your_price").innerHTML = '<b><font size="3" color="#FF0000">' + Convert_ntc_fc(Round2((price_ex) + 0.000001)) + '</font></b>';
					document.getElementById("saving_val").innerHTML = '<font color="#666666">' + this.bands[i].saving_val + '</font>';
					document.getElementById("saving_percent").innerHTML = '<font color="#666666">' + this.bands[i].saving_percent + '</font>';
					product_discount_percent = this.bands[i].discount_percent;

				}
			}
			
			
		}
		
		if (modern_browser)
		{
			document.getElementById("band" + i + "_min_quantity").style.backgroundColor = price_band_bgcolor;
			document.getElementById("band" + i + "_price_ex").style.backgroundColor = price_band_bgcolor;
			document.getElementById("band" + i + "_saving_val").style.backgroundColor = price_band_bgcolor;
			document.getElementById("band" + i + "_saving_percent").style.backgroundColor = price_band_bgcolor;
		}
		
	}
	
	
	if (modern_browser)
	{
		this.band_message = ""
		if (this.quantity < this.highest_band_start)
		{
			if ((this.is_on_special_offer)&&(this.special_price > this.best_price) )
			{
				this.band_message = '<font color="#FF0000"><b>' + 'You can get a better price if you increase the order quantity' + '</b></font>'
			}
			
			if (!this.is_on_special_offer) 
			{
			
				this.band_message = '<font color="#FF0000"><b>' + 'You can get a better price if you increase the order quantity' + '</b></font>'
			}
		}
		
		document.getElementById("price_band_message").innerHTML = this.band_message;
	}

}








function Band(band_no_x, min_qty_x, max_qty_x, price_x, saving_val_x, saving_percent_x, discount_percent_x)
{
	this.min_quantity = min_qty_x;
	this.max_quantity = max_qty_x;
	this.band_no = band_no_x;
	this.price_ex = price_x;
	this.saving_val = saving_val_x;
	this.saving_percent = saving_percent_x;
	this.discount_percent = discount_percent_x;
	if (this.discount_percent == "")
	{
		this.discount_percent = 0;
	}
}



function Convert_ntc_fc(num){
	var curr = String.fromCharCode(163) + num.toString();
	var len = curr.length;
	var x = curr.indexOf(".");
	if (x < 0) curr = curr + ".00";
	else curr += (((len-x) == 2) ? "0" : "");
	x = curr.length
	if (x>7) curr = curr.substring(0,(x-6)) + "," + curr.substring((x-6),x);
	if (x>10) curr = curr.substring(0,(x-9)) + "," + curr.substring((x-9),(x+1));
	
	curr = currency_display_text + curr.substring(1,(curr.length));
	
	return curr;
}

function Round2(num){
	return Math.round((num+0.0001)*100)/100;
}








function Calculate_extended_line_price(quantity,list_price,discount_percent, price_ex)
{
	var extended_price;
	if (ebsl_1059 == "Lakeview") 
	{	
		if (discount_percent > 0) 
		{			
			extended_price = quantity * list_price;
			return Round2(extended_price - Round2((extended_price *  (discount_percent + online_discount_percent) * 0.01) + 0.000001)+ 0.000001);
		}
		else
		{
			extended_price = quantity * price_ex;
			return Round2(extended_price - Round2((extended_price *  (online_discount_percent) * 0.01) + 0.000001)+ 0.000001);
		}
	}
		
	
	return Round2((quantity * price_ex)+ 0.000001);
		
	

}






function Recalculate(){
	
	var quantity = document.form1.quantity.value;
	//window.alert("qty " + quantity + "   price ex:" + product.details.price_ex + "  product vat rate:" + product.details.product_vat_rate);
	if (quantity.length < 1){
		return false;
	}
	
	var numeric = /\d+/;
	var result = numeric.exec(quantity);
	if (result == null){
		return false;
	}
	
	if (result[0]!= quantity){
		return false;
	}
	
	
	if (there_are_price_bands)
	{
		prices.Recalculate();
	}
	
	
	
	
	
	//if ((modern_browser)&&(customer_currency == "GBP")){
	if ((modern_browser)){
		document.getElementById("total_ex").innerHTML = "<b>" + Convert_ntc_fc(Calculate_extended_line_price(quantity,list_price,product_discount_percent, price_ex)
) + "</b>";
		
		document.getElementById("total_inc").innerHTML = Convert_ntc_fc(Round2(Calculate_extended_line_price(quantity,list_price,product_discount_percent, price_ex)
 * (1.0000001 + (product_vat_rate*0.01))));
		
	}
	
	
}




