// JScript source code
function CTQtyPriceBreak(Qty, IsPercent, Delta){
	this.Qty = Qty;
	this.IsPercent = IsPercent;
	this.Delta = Delta;
}

function CTQtyPriceBreak(){
	this.Breaks = new Array();
	this.Qtys = new Array();
}

CTQtyPriceBreak.prototype.addBreak = function(Qty, isPercent, Delta){
	this.Breaks[this.Breaks.length] = new QtyPriceBreak(Qty, isPercent, Delta);
	if(this.Qtys[Qty] == null){
		this.Qtys[Qty] = this.Breaks[this.Breaks.length - 1];
	}else{
		if(this.Qtys[Qty].Delta < Delta)
			this.Qtys[Qty] = this.Breaks[this.Breaks.length];
	}
	
}

CTQtyPriceBreak.prototype.toString = function(BasePrice){
	
	var out = '';
	var isEven = true;
	var sClass;
	this.Breaks.sort(sortQtyPriceBreaks);
	
	try{
		if(this.Qtys.length > 0)
		{
			out += '<table width="95%" border=0 cellpadding=3 cellspacing=0 class="TableWithBorder"><tbody class="Primary">';
			out += '<tr><td align=center colspan=2 class="SecondaryBoldText">Volume Pricing</td></tr>';
			out += '<tr><td class="Primary">Qty</td><td class="Primary">Price</td></tr>';
			for(var i=0; i<this.Breaks.length; i++){
				window.status += 'a' + this.Breaks.length;
				sClass = (isEven?'GridRowEven':'GridRowOdd')
				var next = i + 1;
				
				if (next != this.Breaks.length)
				{ 
				    var ceil = this.Breaks[next].Qty - 1
				    out += '<tr><td class="' + sClass + '">' + this.Breaks[i].Qty + '-' + ceil + '</td><td class="' + sClass + '">' + this.Breaks[i].toString(BasePrice) + '</td></tr>'; 
				}
				else
				{ out += '<tr><td class="' + sClass + '">' + this.Breaks[i].Qty + '+ </td><td class="' + sClass + '">' + this.Breaks[i].toString(BasePrice) + '</td></tr>'; }
				isEven = !isEven;
			}
			out += '</tbody></table>';
		}
	}
	catch(e){}
	return out;
}

CTQtyPriceBreak.prototype.getBreak = function(Qty){

	if(this.Breaks.length == 0) return null;
	var i = 0;
	//if(Qty == 1000) throw 'holla!';
	while((i < this.Breaks.length) && (this.Breaks[i].Qty <= Qty)){
		i++;
	}
	try
	{ if (this.Breaks[i-1].Delta == 100000) return this.Qtys[this.Breaks[i-2].Qty]; }
	catch (e) {return null}
	if(i == this.Breaks.length && this.Breaks[i-1].Qty <= Qty) return this.Qtys[this.Breaks[i-1].Qty];
	if ((i == 0)|| i == this.Breaks.length) return null; //no discount found
	i--;
	//alert(this.Breaks[i].Qty);
	return this.Qtys[this.Breaks[i].Qty];
}


CTQtyPriceBreak.prototype.handleBreaks = function(){
    for(var i=0;i!=this.Breaks.length;i++)
    {
	if(i>0)
	{
	    if(this.Breaks[i].Delta==this.Breaks[i-1].Delta && !this.Breaks[i].IsPercent && !this.Breaks[i-1].IsPercent)
    	    {
            	this.Breaks[i].Delta-=100000;
            }
	}
	if(this.Breaks.length==1)
	{
	    if(this.Breaks[i].Delta==0 && !this.Breaks[i].IsPercent)
	    {
            	this.Breaks[i].Delta-=100000;	    
	    }
	}
    }
    return false;
}


QtyPriceBreak.prototype.toString = function(BasePrice){
	if (BasePrice){
		
	}else{
		BasePrice = 0;
	}
	var tmpStr = "";

	
	
	if(!this.IsPercent) {
		if ((BasePrice - this.Delta).toFixed(2) > 99999)		
		{
		    tmpStr += "Call for Quote";
		}
		else
		{
		    tmpStr += "$";
		    tmpStr += (BasePrice - this.Delta).toFixed(2);
		    tmpStr += " ea.";
		}
	} else {
		tmpStr += (BasePrice - (BasePrice * (this.Delta * .01))).toFixed(2);
		tmpStr += " (" + this.Delta + "% off)";
	}
	
	return tmpStr;
}

QtyPriceBreak.prototype.getDiscountedPrice = function(Price){
	if(this.IsPercent){
		return Price - (Price * (this.Delta * .01));
	}else{
		return Price - this.Delta;
	}
}

function sortQtyPriceBreaks(a,b)
{
	if(parseInt(a.Qty) > parseInt(b.Qty)) return 1;
	else if(parseInt(a.Qty) < parseInt(b.Qty)) return -1;
	else return 0;
}

