
/*
 *  10/21/10 - TB-101020-99 - jp - checkCartItem() should return false if cart.addItem() returns false to avoid a "cart is full" alert loop
 *  6/4/11 - TB-110604-78 - bd - addItem() will store the value in a hidden field rather than storing it to a cookie
 *  6/13/11 - TB-110613-66 - bd - submitToCart() will set the div overlay and Y positions

*/ 

function $(element) {
	return document.getElementById(element);
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";	
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

String.prototype.trim = function() {
a = this.replace(/^\s+/, '');
return a.replace(/\s+$/, '');
};

function Item()
{
	this.sku;
	this.qty;
	this.uom;
	this.price;
	this.prodname;
	
	this.setSku = function(sku)
	{
		this.sku = sku;	
	}
	
	this.setQty = function(qty)
	{
		this.qty = qty;	
	}
	
	this.setUom = function(uom)
	{
		this.uom = uom;	
	}
	
	this.setPrice = function(price)
	{
		this.price = price;	
	}

	this.setProdname = function(prodname)
	{
		this.prodname = prodname;	
	}
	
}	

function ShoppingCart(options)
{	
	this.items = new Array();
	this.options = options;
	// TB-110604-78 begin
	var delim = "^";
	var itemDelim = "**";
	// TB-110604-78 end

	this.addItem = function(sku, qty, uom, price, prodname, skip)
	{
		// TB-110604-78 begin
		var cartList = document.cartForm.cartItem.value;
		if (isNaN(qty) || qty.toString().trim()=="") { return false; }
        var itemToAdd = sku + delim + qty + delim + uom + delim + price + delim + prodname;
		document.cartForm.cartItem.value = (cartList.length<1)?itemToAdd:cartList+itemDelim+itemToAdd;

/*		
		
		
		
		try {
			if ((document.cookie.length+sku.length+qty.length+uom.length+7)>4000) {
				alert("Your cart is full!");
				return false;
			}
		} catch (e) { }
		
		if (!skip) {
			this.items = new Array();
			this.loadItems();
		}
		
		var newItem = new Item();
		newItem.setSku(sku);
		newItem.setQty(qty);
		newItem.setUom(uom);
		newItem.setPrice(price);
		newItem.setProdname("");
		var foundItem = this.findItem(newItem)
		
		if (!foundItem) {
			this.items.push(newItem);
		} else {
			this.updateItem(foundItem, qty);
		}
		
		this.save();
*/
		// TB-110604-78 end
		
		checkForNameTags(sku);

        return true;
	}
	
	this.save = function()
	{
		var cookieString = '';
		
		for (var i=0; i < this.items.length; i++) {
			cookieString += this.items[i].sku + this.options.delimiters[0];
			cookieString += this.items[i].qty + this.options.delimiters[0];
			cookieString += this.items[i].uom + this.options.delimiters[0];
			cookieString += this.items[i].price + this.options.delimiters[0];
			cookieString += this.items[i].prodname;
			if(i != this.items.length - 1) 
				cookieString += this.options.delimiters[1];
		}
		
		createCookie(this.options.cookie, cookieString, this.options.days);
		
	}
	
	this.loadItems = function()
	{
		var cookie = readCookie(this.options.cookie);
		
		if (cookie) {
			var items = cookie.split(this.options.delimiters[1]);
		
			for (var i=0; i < items.length; i++) {
				var splitItem = items[i].split(this.options.delimiters[0]);
				this.loadItemsAdd(splitItem[0], splitItem[1], splitItem[2], splitItem[3], splitItem[4]);
			}
			
			this.save();
		}
	}
	
	this.loadItemsAdd = function(sku, qty, uom, price, prodname)
	{
		sku = (sku.substring(0,1)=='"')?sku.substring(1):sku;
		var newItem = new Item();
		newItem.setSku(sku);
		newItem.setQty(qty);
		newItem.setUom(uom);
		newItem.setPrice(price);
		newItem.setProdname("");
		var foundItem = this.findItem(newItem)
		
		if (!foundItem) {
			this.items.push(newItem);
		} else {
			this.updateItem(foundItem, qty);
		}
	
	}
	
	this.findItem = function(item)
	{
		for (var i=0; i < this.items.length; i++) {
			if (this.items[i].sku == item.sku && this.items[i].uom == item.uom) {
				return this.items[i];
			}
		}
		
		return false;
	}
	
	this.updateItem = function(item, qty)
	{

		item.qty = parseInt(qty) + parseInt(item.qty);
		
    }
	
	this.destroy = function()
	{
		eraseCookie(this.options.cookie);	
	}
}


var options = {
	cookie : 'shoppingCartCookie',
	days : 30,
	delimiters : ['^', '**']
};
var cart = new ShoppingCart(options);
cart.loadItems(); 


function ReadCookie(cookieName) {
 var theCookie=""+document.cookie;
 var ind=theCookie.indexOf(cookieName);
 if (ind==-1 || cookieName=="") return ""; 
 var ind1=theCookie.indexOf(';',ind);
 if (ind1==-1) ind1=theCookie.length; 
 return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}


function checkCartItem(prod, qty, uom, prodname, idx) {
// need to verify the unit of measure that is selected on the page...also read the price html in case it has chanaged.
// alert("prod: " + prod + " qty: " + qty + " uom: " + uom + " prodname: " + prodname + " idx: " + idx);

  var price = document.getElementsByName("price_span_"+idx)[0];
  if (uom.indexOf(":")>-1) {
	uom = document.getElementById("UM_for_"+idx).value;
  }
  
  if (qty>0) {
    try { 
	  return cart.addItem(prod, qty, uom, price, prodname, idx); //TB-101020-99 added 'return'
	} catch (Err) {
		alert(Err);
	  return false;
	}
  }
  return true;
}

function submitToCart() {
document.cartForm.yPos.value = document.body.scrollTop;
window.scroll(0,0);
document.body.style.overflow = "hidden"; 
document.getElementById("coverdiv").style.display = "inline";
document.getElementById("centercover").style.display = "inline";
document.getElementById("centerbox").style.display = "inline";
progressImg = document.getElementById('processImage');
self.setTimeout("progressImg.src=progressImg.src",100);
document.cartForm.submit();
}
