"use strict";
//autocart.js   objects to automatically create a cart that is updatable by javascript calls

function AutoCart(qtyID,priceID,extPriceID,subTotalID,currencySymbol,currency)
{
	//each ID is the starting part of the id tag of each element, which is to be followed with a cartitemid to make it unique
	this.qtyID = qtyID;
	this.priceID = priceID;
	this.extPriceID = extPriceID;
	this.subTotalID = subTotalID;
	this.currencySymbol = currencySymbol;
	this.currency = currency;
	this.items = new Array();
}

AutoCart.prototype.qtyID = null;
AutoCart.prototype.priceID = null;
AutoCart.prototype.extPriceID = null;
AutoCart.prototype.subtotalID = null;
AutoCart.prototype.currencySymbol = null;
AutoCart.prototype.currency = null;
AutoCart.prototype.items = null;
AutoCart.prototype.format = function AutoCart_format(price,includeCurrency)
{
		var m,result = Math.round(Number(price)*100)/100;
		result = String(result).replace(/(\.[0-9]{2})[0-9]*$/g,'$1');
		var count = 0;
		if (!(m=result.match(/\.[0-9]*$/)) || (m.length == 0))
			result += '.';
		while ((!(m = result.match(/\.[0-9]{2}$/)) || (m.length <= 0)) && (count < 10))
		{
			result += '0';
			count++;
		}
		return this.currencySymbol+result+(includeCurrency?' '+this.currency:'');
}
AutoCart.prototype.add = function AutoCart_add(cartItem)
{
	this.items.push(cartItem);
}
AutoCart.prototype.getItem = function AutoCart_getItem(cartItemID)
{
	for (var count = 0;count < this.items.length;count++)
		if (this.items[count].cartItemID == cartItemID)	
			return this.items[count];
	return null;
}

AutoCart.prototype.updateQuantity = function AutoCart_updateQuantity(cartItemID,quantity)
{
	var item = this.getItem(cartItemID);
	if (item)
	{
		item.quantity = quantity;
		return true;
	}
	else if ((console) && (console.warn))
	{
		console.warn('couldn\'t update quantity for '+cartItemID);
	}
	return false;
}
AutoCart.prototype.adjustPrice = function(cartItemID,newPrice,discounts,quantity)
{
	var item = this.getItem(cartItemID);
	if (item)
	{
		if (discounts) {
			for (var i = 0;i < item.discounts.length;++i){
				for (var j = 0;j < discounts.length;++j) {
					if (item.discounts[i].discountID == discounts[j].discountID) {
						item.discounts[i].unitPrice = discounts[j].unitPrice;
					}
				}
			}
		}
		quantity = Number(quantity);
		if (quantity) {
			item.quantity = quantity;
			var qtyElement = document.getElementById(this.qtyID+item.cartItemID);
			if (qtyElement) {
				qtyElement.value = quantity;
				if (qtyElement.lp) {//labelPricing.js::LabelPricing
						qtyElement.lp.setQuantity(quantity);
				}
			}
		}
		newPrice = Number(newPrice);//only do something if newPrice is a valid number;
		if (newPrice)
		{
			
			item.unitPrice = newPrice;
			var priceElement = document.getElementById(this.priceID+item.cartItemID);
			if (priceElement)
			{
				while (priceElement.firstChild) priceElement.removeChild(priceElement.firstChild);
				priceElement.appendChild(document.createTextNode(this.format(newPrice)));
			}
			else if ((console) && (console.warn))
				console.warn('couldn\'t adjust price for ',cartItemID,' to ',newPrice);
		}
		else if ((console) && (console.warn))
			console.warn('newPrice is not a number? ',newPrice);
	}
	else if ((console) && (console.warn))
	{
		console.warn('couldn\'t adjust price for '+cartItemID);
	}
}
AutoCart.prototype.updatePrices = function AutoCart_updatePrices()
{
	var subTotal = 0;
	for (var count = 0;count < this.items.length;count++)
	{
		var extPriceElement = document.getElementById(this.extPriceID+this.items[count].cartItemID);
		if (extPriceElement)
		{
			if (String(extPriceElement.tagName).toLowerCase == 'input')
				extPriceElement.value = this.format(this.items[count].getExtPrice());
			else 
			{
				while (extPriceElement.firstChild)
					extPriceElement.removeChild(extPriceElement.firstChild);
				extPriceElement.appendChild(document.createTextNode(this.format(this.items[count].getExtPrice())));
			}
		}
		else
		{
			if ((console) && (console.warn))
				console.warn('couldn\'t find element '+this.extPriceID+this.items[count].cartItemID);
		}
		
		subTotal += this.items[count].getExtPrice()-this.items[count].getDiscountPrice();
	}
	var stElement = document.getElementById(this.subTotalID);
	if (stElement)
	{
		while (stElement.firstChild)
			stElement.removeChild(stElement.firstChild);
		stElement.appendChild(document.createTextNode(this.format(subTotal,true)));
	}
	$('.'+this.subTotalID).text(this.format(subTotal,true));
	
}
AutoCart.prototype.addItemDiscount = function Autocart_addItemDiscount(cartItemID,discount) {
	for(var i = 0;i < this.items.length;++i) {
		if (this.items[i].cartItemID == cartItemID) {
			this.items[i].discounts.push(discount);
		}
	}
}
function AutoCartItemDiscount(discountID,unitPrice,useQty) {
	this.discountID = discountID;
	this.unitPrice = unitPrice;
	this.useQty = useQty;
}

AutoCartItemDiscount.prototype.discountID = '';
AutoCartItemDiscount.prototype.unitPrice=0;
AutoCartItemDiscount.prototype.useqty = false;

AutoCartItemDiscount.prototype.toString = function AutoCartItemDiscount_toString() {
	return 'id='+this.discountID+' price='+this.unitPrice+' useqty='+this.useQty;
}

function AutoCartItem(cartItemID,unitPrice,setupPrice,quantity)
{
	this.cartItemID = cartItemID;
	if (unitPrice)
		this.unitPrice = unitPrice;
	else this.unitPrice =0;
	if (setupPrice)
		this.setupPrice = setupPrice;
	else this.setupPrice = 0;
	if (quantity)
		this.quantity = quantity;
	else this.quantity = 0;
	this.discounts = new Array();
}

AutoCartItem.prototype.cartItemID = null;
AutoCartItem.prototype.unitPrice = null;
AutoCartItem.prototype.setupPrice = null;
AutoCartItem.prototype.quantity = null;
AutoCartItem.prototype.discounts = null;
AutoCartItem.prototype.getExtPrice = function AutoCartItem_getExtPrice()
{
	var result = (this.unitPrice * this.quantity)+this.setupPrice;
	if (result) return result;
	else return 0;
}
AutoCartItem.prototype.getDiscountPrice = function AutoCartItem_getDiscountPrice() {
	var result = 0
	for(var i = 0;i < this.discounts.length;++i) { 
		if (this.discounts[i]) {
			result += this.discounts[i].unitPrice*(this.discounts[i].useQty?this.quantity:1);
		}
	}
	if (result) return result;
	else return 0;
}



