//Vaughn Micciche
//Net Impact www.net-impact.us
//All code, accept bottom cookie code, property of Net Impact.
//

var cookieName = "DiamondLustreCart";
var ie;
if(document.all)
{
	ie=true;
}else
{
	ie=false;
}


function addToCart(stringProductID,quantity)
{
	document.cookie="cookieTest=test";
	(document.cookie.indexOf("cookieTest")!=-1)?cookieEnabled=true:cookieEnabled=false;

	if(cookieEnabled)
	{
		if(GetCookie(cookieName)==null)
		{
			SetCookie(cookieName,stringProductID+"|"+quantity+",");
		}else
		{

			var cookieArray= splitToArrayOfArrays(GetCookie(cookieName));

			var holderVal = true;

			for (var i=0;i<cookieArray.length ;i++ )
			{

				if(cookieArray[i][0] == stringProductID)
				{
					cookieArray[i][1] = Number(cookieArray[i][1]) + Number(quantity);
					holderVal=false;
				}
			}

			if(holderVal)
			{
				//alert("in push,"+stringProductID+"~"+quantity);
				cookieArray.push([stringProductID,quantity]);
				//alert(cookieArray);
			}

			SetCookie(cookieName,buildCookieStringFromArrays(cookieArray));		
		}

		alert("Item added to cart.");
	}
	else
	{
		alert("Please allow cookies to be written to your computer.");
	}
}


function removeFromCart(stringProductID,booleanRemoveAllProducts)
{

	if(!booleanRemoveAllProducts)
	{
		if(GetCookie(cookieName)==null)
		{
			alert("You do not currently have anything in your shopping cart");
		}else
		{

			var cookieArray= splitToArrayOfArrays(GetCookie(cookieName));

			for (var i=0;i<cookieArray.length ;i++ )
			{

				if(cookieArray[i][0] == stringProductID)
				{
					cookieArray.splice(i,1);
				}
			}

			SetCookie(cookieName,buildCookieStringFromArrays(cookieArray));
			//alert("Item removed from cart.");
		}
		
	}
	else
	{
		if(confirm("Are you sure you want to delete your entire shopping cart?"))
		{
			DeleteCookie(cookieName);
			//alert("All Items Deleted.");
			window.location.reload(true);
		}
	}
}

//function to split apart a string that is seperated by | and , 
function splitToArrayOfArrays(stringCookieText)
{
	stringCookieText += " ";
	var cookieArray = stringCookieText.split(",");
	cookieArray.pop();
	//alert(cookieArray);
	for(var i = 0;i<cookieArray.length;i++)
	{
		cookieArray[i]=cookieArray[i].split("|");
	}
	//alert(cookieArray);
	return cookieArray;
}

//function to build a string from an array of arrays
function buildCookieStringFromArrays(arrayCookie)
{
	var cookieString="";

	for(var i=0;i<arrayCookie.length;i++)
	{
		var holderString="";

		for (var b=0;b<arrayCookie[i].length;b++ )
		{
			holderString += arrayCookie[i][b]+"|";
		}
		
		holderString = holderString.substring(0,holderString.length-1);

		cookieString += holderString+",";
	}

	//alert(cookieString);
	return cookieString;

}

//check to see if quantity field is a number and add to cart.
//the id of the submit to cart picture must be the product ID and the ID of the quantity text field must be the 
//product ID and the letters 'QTY'
function checkAndAddToCart(which)
{
	var productID = which.id;
	var quantity = document.getElementById(which.id+"QTY").selectedIndex;
	quantity+=1;

	var numHolder = new Number(quantity);

	if(numHolder == Number.NaN)
	{
		alert("Please enter a numeric quantity.");
	}
	else
	{
		addToCart(productID,quantity);
	}
}




// BEGIN Derived from the Bill Dortch code at http://www.hidaho.com/cookies/cookie.txt  *************************************

var today = new Date();
//expires in a year....
var expiry = new Date(today.getTime() + 365 * 24 * 60 * 60 * 1000);

function getCookieVal (offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1) { endstr = document.cookie.length; }
	return unescape(document.cookie.substring(offset, endstr));
}

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;
}

function DeleteCookie (name,path,domain) {
	if (GetCookie(name)) {
		document.cookie = name + "=" +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		"; expires=Thu, 01-Jan-70 00:00:01 GMT";
		}
}

function SetCookie (name,value,expires,path,domain,secure) {
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}


// END Derived from the Bill Dortch code at http://www.hidaho.com/cookies/cookie.txt  *************************************