
MaxCartItems = 15;

function getCookieVal (offset) 
{
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}//End of getCookieVal()

function GetCookie (name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
	   if (document.cookie.substring(i, j) == arg)
     return getCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break; 
  }
  return null;
}//End of GetCookie()

function SetCookie (name, value) 
{
  var argv = SetCookie.arguments;
  var argc = SetCookie.arguments.length;
  var expires = (argc > 2) ? argv[2] : null;
  var path = (argc > 3) ? argv[3] : null;
  var domain = (argc > 4) ? argv[4] : null;
  var secure = (argc > 5) ? argv[5] : false;
  document.cookie = name + "=" + escape (value) +
    ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
    ((path == null) ? "" : ("; path=" + path)) +
    ((domain == null) ? "" : ("; domain=" + domain)) +
    ((secure == true) ? "; secure" : "");
}//End of SetCookie()

function DeleteCookie (name) 
{
  var exp = new Date();
  exp.setTime (exp.getTime() - 1);  // This cookie is history
  var cval = GetCookie (name);
  if (cval != null)
    document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}//End of DeleteCookie()

function AddItem(ID,Sdesc,Price,Qty,Weight,WeightUnit,City,State,Size,Color)
{
	if (isNaN(Qty))
	{
		alert("Please Enter Proper Quantity. It can Contain only Numbers");
		self.location=self.location;
		//return false;
	}
	else
	{
		if (Math.round(Qty) <= 0) 
		{
			rc = alert('The Quantity must be greater than 0');
			return false;
		}

		for(NumItems = 0; NumItems<=MaxCartItems; NumItems++)
		{
			if(NumItems == MaxCartItems)
			{
				alert('Maximum of '+MaxCartItems+' different items can fit in the shopping cart, please go to the Checkout Counter and submit your order.  Then empty your cart and shop some more later.');
			}
			else
			{
				CookieName = 'Cart' +NumItems;
				CookieValue = GetCookie(CookieName);
				//If the Product with this ID is already presents, Increase the Quantity of that Item.
				itemnum=isItemThere(ID,Size,Color);
				if (itemnum==MaxCartItems)
				{
					if(CookieValue == null)
					{
						// Alt + 0192,0193,0194,0195,0196,0197,0198,0199,0200
						CookieValue = "["+ID+"À"+Sdesc+"Á"+Price+"Â"+Math.round(Qty)+"Ã"+Weight+"Ä"+WeightUnit+"Å"+City+"Æ"+State+"Ç"+Size+"È"+Color+"]";
						SetCookie(CookieName,CookieValue);
						break;
					}
				}
				else
				{
					increaseQty(itemnum,Qty);
					break;
				}
			}
		}	
	}
	return true;
}//End of AddItem()

function isItemThere(IdGiven,SizeGiven,ColorGiven)//Function to find out whether the Item has already been added to the Cart or Not.
{
	equalFlag=false;
	for(NumItems1 = 0; NumItems1<MaxCartItems; NumItems1++)
	{
		CookieName1 = "Cart" +NumItems1;
		CookieValue1 = GetCookie(CookieName1);
		
		if(CookieValue1 != null)
		{
			for(var i = 0;i <= CookieValue1.length; i++)
			{
				// using 0192,0193,0194,0195,0196
				if (CookieValue1.substring(i,i+1) == '[') 
					itemstart = i+1;
				else if (CookieValue1.substring(i,i+1) == 'À') 
				{
					IDCookie = CookieValue1.substring(itemstart, i);
					if (IDCookie==IdGiven) equalFlag=true;
					itemstart = i+1;
				} 
				else if (CookieValue1.substring(i,i+1) == 'Á') 
					itemstart = i+1;
				else if (CookieValue1.substring(i,i+1) == 'Â') 
					itemstart = i+1;
				else if (CookieValue1.substring(i,i+1) == 'Ã') 
					itemstart = i+1;
				else if (CookieValue1.substring(i,i+1) == 'Ä') 
					itemstart = i+1;
				else if (CookieValue1.substring(i,i+1) == 'Å') 
					itemstart = i+1;
				else if (CookieValue1.substring(i,i+1) == 'Æ')
					itemstart = i+1;
				else if (CookieValue1.substring(i,i+1) == 'Ç')
					itemstart = i+1;
				else if (CookieValue1.substring(i,i+1) == 'È')
				{
					if (equalFlag==true)
					{
						SizeCookie=CookieValue1.substring(itemstart, i);
						if (SizeGiven==SizeCookie) equalFlag=true;
						else equalFlag=false;
					}
					itemstart = i+1;
				}
				else if (CookieValue1.substring(i,i+1) == ']')
				{
					if (equalFlag==true)
					{
						ColorCookie=CookieValue1.substring(itemstart, i);
						if (ColorGiven==ColorCookie) equalFlag=true;
						else equalFlag=false;
					}
					itemstart = i+1;
				}
			}
		}
	if (equalFlag==true) return NumItems1;
	}
	return NumItems1;
}//End of isItemThere(Id).

function increaseQty(itemnum,Qty)
{	
	CookieName2 = "Cart" +itemnum;
	CookieValue2 = GetCookie(CookieName2);
	for(var j = 0;j <= CookieValue2.length; j++)
	{
		// using 0192,0193,0194,0195,0196
		if (CookieValue2.substring(j,j+1) == '[') 
		{
			itemstart1 = j+1;
		} 
		else if (CookieValue2.substring(j,j+1) == 'À') 
		{
			ID1 = CookieValue2.substring(itemstart1, j);
			itemstart1 = j+1;
		} 
		else if (CookieValue2.substring(j,j+1) == 'Á') 
		{
			Sdesc1 = CookieValue2.substring(itemstart1, j);
			itemstart1 = j+1;
		}
		else if (CookieValue2.substring(j,j+1) == 'Â') 
		{
			Price1 = CookieValue2.substring(itemstart1, j);
			itemstart1 = j+1;
		}
		else if (CookieValue2.substring(j,j+1) == 'Ã') 
		{			
			OldQty1 = CookieValue2.substring(itemstart1, j);
			itemstart1 = j+1;
		} 
		else if (CookieValue2.substring(j,j+1) == 'Ä') 
		{
			Weight1 = CookieValue2.substring(itemstart1, j);
			itemstart1 = j+1;
		}
		else if (CookieValue2.substring(j,j+1) == 'Å') 
		{
			WeightUnit1 = CookieValue2.substring(itemstart1, j);
			itemstart1 = j+1;
		}
		else if (CookieValue2.substring(j,j+1) == 'Æ')
		{
			City1 = CookieValue2.substring(itemstart1,j);
			itemstart1 = j+1;
		}
		else if (CookieValue2.substring(j,j+1) == 'Ç')
		{
			State1 = CookieValue2.substring(itemstart1,j);
			itemstart1 = j+1;
		}
		else if (CookieValue2.substring(j,j+1) == 'È')
		{
			Size1 = CookieValue2.substring(itemstart1,j);
			itemstart1 = j+1;
		}
		else if (CookieValue2.substring(j,j+1) == ']')
		{
			Color1 = CookieValue2.substring(itemstart1,j);
			itemstart1 = j+1;
		}
	}
	NewQty=Math.round(OldQty1)+Math.round(Qty);
	NewCookieValue = "["+ID1+"À"+Sdesc1+"Á"+Price1+"Â"+NewQty+"Ã"+Weight1+"Ä"+WeightUnit1+"Å"+City1+"Æ"+State1+"Ç"+Size1+"È"+Color1+"]";
	if(Math.round(NewQty) <= 0)
		DeleteCookie(CookieName2);
	else
		SetCookie(CookieName2,NewCookieValue);
}//End of increaseQty()

function RemoveItem(itemnum)
{
	CookieName3 = "Cart" +itemnum;
	DeleteCookie(CookieName3);
	self.location = self.location;
}//End of RemoveItem()

function GetTotalCartItems()
{
	TotalCartItems=0;

	for(NumItems = 0; NumItems<MaxCartItems; NumItems++)
	{
		CookieName = "Cart" +NumItems;
		CookieValue = GetCookie(CookieName);

		if(CookieValue != null)
		{
			for(var i = 0;i <= CookieValue.length; i++)
			{
				// using 0192,0193,0194,0195,0196,0197,0198,0199,0200
				if (CookieValue.substring(i,i+1) == '[') 
					itemstart = i+1;
				else if (CookieValue.substring(i,i+1) == 'À') 
					itemstart = i+1;
				else if (CookieValue.substring(i,i+1) == 'Á') 
					itemstart = i+1;
				else if (CookieValue.substring(i,i+1) == 'Â') 
					itemstart = i+1;
				else if (CookieValue.substring(i,i+1) == 'Ã') 
				{
					Qty = CookieValue.substring(itemstart, i);
					TotalCartItems = parseInt(TotalCartItems) + parseInt(Qty);
				} 
				else if (CookieValue.substring(i,i+1) == 'Ä') 
					itemstart = i+1;
				else if (CookieValue.substring(i,i+1) == 'Å') 
					itemstart = i+1;
				else if (CookieValue.substring(i,i+1) == 'Æ')
					itemstart = i + 1;
				else if (CookieValue.substring(i,i+1) == 'Ç')
					itemstart = i + 1;
				else if (CookieValue.substring(i,i+1) == 'È')
					itemstart = i + 1;
				else if (CookieValue.substring(i,i+1) == ']')
					itemstart = i + 1;
			}
		}
	}
	return TotalCartItems;
}//End Of GetTotalItems().

function GetTotalCartValue()
{
	TotalCartValue = 0;

	for(NumItems = 0; NumItems<MaxCartItems; NumItems++)
	{
		CookieName = "Cart" +NumItems;
		CookieValue = GetCookie(CookieName);

		if(CookieValue != null)
		{
			for(var i = 0;i <= CookieValue.length; i++)
			{
				// using 0192,0193,0194,0195,0196,0197,0198,0199,0200
				if (CookieValue.substring(i,i+1) == '[') 
					itemstart = i+1;
				else if (CookieValue.substring(i,i+1) == 'À') 
					itemstart = i+1;
				else if (CookieValue.substring(i,i+1) == 'Á') 
					itemstart = i+1;
				else if (CookieValue.substring(i,i+1) == 'Â') 
				{
					Price = CookieValue.substring(itemstart, i);
					itemstart = i+1;
				}
				else if (CookieValue.substring(i,i+1) == 'Ã') 
				{
					Qty = CookieValue.substring(itemstart, i);
					ItemTotal = (eval(Price*Qty));
					TotalCartValue = TotalCartValue + ItemTotal;
				} 
				else if (CookieValue.substring(i,i+1) == 'Ä') 
					itemstart = i+1;
				else if (CookieValue.substring(i,i+1) == 'Å') 
					itemstart = i+1;
				else if (CookieValue.substring(i,i+1) == 'Æ')
					itemstart = i + 1;
				else if (CookieValue.substring(i,i+1) == 'Ç')
					itemstart = i + 1;
				else if (CookieValue.substring(i,i+1) == 'È')
					itemstart = i + 1;
				else if (CookieValue.substring(i,i+1) == ']')
					itemstart = i + 1;
			}
		}
	}
	return FloatFormat(TotalCartValue, 2);
}//End Of GetTotalCartValue().

function FloatFormat(expr,decplaces)
{
	var str = "" + Math.round(eval(expr) * Math.pow(10,decplaces));
	while(str.length <= decplaces)
	{
		str = "0" + str;
	}

	var decpoint = str.length - decplaces;
	return str.substring(0,decpoint) + "." + str.substring(decpoint,str.length);
}
