var cookieName = "DiamondLustreCart";

var ie;
if(document.all)
{
	ie=true;
}else
{
	ie=false;
}



function displayCart(cartDivID)
{
	
	var displayDiv = document.getElementById(cartDivID);
	var cookieArray= splitToArrayOfArrays(GetCookie(cookieName));
	
	if(cookieArray.length>0)
	{
		var totalPrice = 0;

		var theForm = document.createElement("form");
		theForm.setAttribute("method","post");
		theForm.setAttribute("action","checkOut.asp");

		if(ie)
		{
			theForm.attachEvent("onsubmit",function (){return checkSubmitFields();});

		}else
		{
			theForm.setAttribute("onsubmit","return checkSubmitFields();");
		}
		


		var displayTable = document.createElement("table");
		displayTable.setAttribute("border","1");
		displayTable.setAttribute("cellpadding","2");
		
		var tbody=document.createElement("tbody");	/////////////////////////////////////////////////////////////////

		var firstRow = document.createElement("tr");
		firstRow.setAttribute("style","font-weight:bold;background-color:#999999;");


		var topCell1 = document.createElement("td");
		var topCell2 = document.createElement("td");
		var topCell3 = document.createElement("td");
		var topCell4 = document.createElement("td");
		var topCell5 = document.createElement("td");

		topCell1.setAttribute("align","center");
		topCell2.setAttribute("align","center");
		topCell3.setAttribute("align","center");
		topCell4.setAttribute("align","center");
		topCell5.setAttribute("align","center");
		
		
		topCell2.appendChild(document.createTextNode("Qty"));
		topCell3.appendChild(document.createTextNode("Description"));
		topCell4.appendChild(document.createTextNode("Price"));
		topCell5.appendChild(document.createTextNode("Total"));
		
		firstRow.appendChild(topCell1);
		firstRow.appendChild(topCell2);
		firstRow.appendChild(topCell3);
		firstRow.appendChild(topCell4);
		firstRow.appendChild(topCell5);
		
		tbody.appendChild(firstRow);		/////////////////////////////////////////////////////////////////
		//displayTable.appendChild(tbody);	/////////////////////////////////////////////////////////////////

		

		for(var i=0;i<cookieArray.length;i++)
		{
			displayRow = document.createElement("tr");


			var productArray = eval("ARRAY_"+cookieArray[i][0]);
			var holderPrice = new Number(productArray[2]);

			var priceHolder = cookieArray[i][1]*productArray[2];
			var quickHolder=new Number(priceHolder);
			totalPrice = Number(totalPrice) + Number(priceHolder);

			//delete marker checkbox
			var displayCell = document.createElement("td");
			var checkBox = document.createElement("input");
			checkBox.setAttribute("type","checkbox");
			checkBox.setAttribute("name","productLine"+i);
			checkBox.setAttribute("id",cookieArray[i][0]);
			checkBox.setAttribute("value",cookieArray[i][0]+"--"+cookieArray[i][1]+"--"+productArray[0]+"--$"+holderPrice.toFixed(2)+"--$"+quickHolder.toFixed(2) );
			
			displayCell.appendChild(checkBox);
			displayRow.appendChild(displayCell);

			//quantity indicator
			displayCell = document.createElement("td");
			displayCell.setAttribute("align","center");
			var prodQuantity = document.createTextNode(cookieArray[i][1]);
			displayCell.appendChild(prodQuantity);
			displayRow.appendChild(displayCell);


			//description
			displayCell = document.createElement("td");
			var prodDesc = document.createTextNode(productArray[0]);
			displayCell.appendChild(prodDesc);
			displayRow.appendChild(displayCell);


			//Price for this line item
			displayCell = document.createElement("td");
			displayCell.setAttribute("align","right");
			var prodSinglePrice = document.createTextNode("$ "+holderPrice.toFixed(2));
			displayCell.appendChild(prodSinglePrice);
			displayRow.appendChild(displayCell);


			//total cost for this line item
			displayCell = document.createElement("td");
			displayCell.setAttribute("align","right");
			var prodPrice = document.createTextNode("$ "+quickHolder.toFixed(2));
			displayCell.appendChild(prodPrice);
			displayRow.appendChild(displayCell);
			
			tbody.appendChild(displayRow);	/////////////////////////////////////////////////////////////////

			//displayTable.appendChild(tbody);	/////////////////////////////////////////////////////////////////
			
		}
		var howMany = document.createElement("input");
		howMany.setAttribute("type","hidden");
		howMany.setAttribute("name","numberOfRows");
		howMany.setAttribute("value",cookieArray.length+"");

		var total2 = document.createElement("input");
		total2.setAttribute("type","hidden");
		total2.setAttribute("name","totalCost");
		total2.setAttribute("value",totalPrice+"");
		
		var lastRow = document.createElement("tr");
		
		var bottomCell1 = document.createElement("td");
		bottomCell1.appendChild(howMany);
		bottomCell1.appendChild(total2);
		var bottomCell2 = document.createElement("td");
		var bottomCell3 = document.createElement("td");
		var bottomCell4 = document.createElement("td");
		bottomCell4.setAttribute("align","right");
		var bottomCell5 = document.createElement("td");

		bottomCell4.appendChild(document.createTextNode("Total:"));
		totalPrice = new Number(totalPrice);
		bottomCell5.appendChild(document.createTextNode("$ "+totalPrice.toFixed(2)));
		
		lastRow.appendChild(bottomCell1);
		lastRow.appendChild(bottomCell2);
		lastRow.appendChild(bottomCell3);
		lastRow.appendChild(bottomCell4);
		lastRow.appendChild(bottomCell5);
		
		tbody.appendChild(lastRow);		/////////////////////////////////////////////////////////////////
		//displayTable.appendChild(lastRow);
		displayTable.appendChild(tbody);	/////////////////////////////////////////////////////////////////


		//alert(cookieArray);
		theForm.appendChild(displayTable);


		var deleteButton = document.createElement("input");
		deleteButton.setAttribute("type","button");
		deleteButton.setAttribute("value","Remove Selected Items");

		
		if(ie)
		{
			deleteButton.attachEvent("onclick",function (){deleteFromCart();});

		}else
		{
			deleteButton.setAttribute("onclick","deleteFromCart()");
		}
				
		theForm.appendChild(deleteButton);

		var emptyButton = document.createElement("input");
		emptyButton.setAttribute("type","button");
		emptyButton.setAttribute("value","Empty Cart");
		if(ie)
		{
			emptyButton.attachEvent("onclick",function (){removeFromCart(' ',true);});

		}else
		{
			emptyButton.setAttribute("onclick","removeFromCart(' ',true)");
		}
		
		theForm.appendChild(emptyButton);
		theForm.appendChild(document.createElement("br"));
		theForm.appendChild(document.createElement("br"));
		theForm.appendChild(document.createElement("br"));

		var inputTableTable=document.createElement("table");	/////////////////////////////////////////////////////////////////
		var inputTable = document.createElement("tbody");
		var genericRow = document.createElement("tr");
		var firstTD = document.createElement("td");
		firstTD.setAttribute("align","right");
		var secondTD = document.createElement("td");


		firstTD.appendChild(document.createTextNode("First Name: "));
		genericRow.appendChild(firstTD);
		var nameField = document.createElement("input");
		nameField.setAttribute("type","text");
		nameField.setAttribute("size","25");
		nameField.setAttribute("name","firstName");
		nameField.setAttribute("id","firstName");
		secondTD.appendChild(nameField);
		genericRow.appendChild(secondTD);
		inputTable.appendChild(genericRow);

		genericRow = document.createElement("tr");
		firstTD = document.createElement("td");
		firstTD.setAttribute("align","right");
		secondTD = document.createElement("td");

		firstTD.appendChild(document.createTextNode("Last Name: "));
		genericRow.appendChild(firstTD);
		var nameField2 = document.createElement("input");
		nameField2.setAttribute("type","text");
		nameField2.setAttribute("size","25");
		nameField2.setAttribute("name","lastName");
		nameField2.setAttribute("id","lastName");
		secondTD.appendChild(nameField2);
		genericRow.appendChild(secondTD);
		inputTable.appendChild(genericRow);

		genericRow = document.createElement("tr");
		firstTD = document.createElement("td");
		firstTD.setAttribute("align","right");
		secondTD = document.createElement("td");

		firstTD.appendChild(document.createTextNode("Company : "));
		genericRow.appendChild(firstTD);
		var nameField3 = document.createElement("input");
		nameField3.setAttribute("type","text");
		nameField3.setAttribute("size","25");
		nameField3.setAttribute("name","companyName");
		secondTD.appendChild(nameField3);
		genericRow.appendChild(secondTD);
		inputTable.appendChild(genericRow);

		genericRow = document.createElement("tr");
		firstTD = document.createElement("td");
		firstTD.setAttribute("align","right");
		secondTD = document.createElement("td");

		firstTD.appendChild(document.createTextNode("Email: "));
		genericRow.appendChild(firstTD);
		var emailField = document.createElement("input");
		emailField.setAttribute("type","text");
		emailField.setAttribute("size","25");
		emailField.setAttribute("name","email");
		emailField.setAttribute("id","email");
		secondTD.appendChild(emailField);
		genericRow.appendChild(secondTD);
		inputTable.appendChild(genericRow);

		genericRow = document.createElement("tr");
		firstTD = document.createElement("td");
		firstTD.setAttribute("align","right");
		secondTD = document.createElement("td");

		firstTD.appendChild(document.createTextNode("Phone Number: "));
		genericRow.appendChild(firstTD);
		var phoneField = document.createElement("input");
		phoneField.setAttribute("type","text");
		phoneField.setAttribute("size","25");
		phoneField.setAttribute("name","phone");
		phoneField.setAttribute("id","phone");
		secondTD.appendChild(phoneField);
		genericRow.appendChild(secondTD);
		inputTable.appendChild(genericRow);

		inputTableTable.appendChild(inputTable);
		theForm.appendChild(inputTableTable);
		theForm.appendChild(document.createElement("br"));
		theForm.appendChild(document.createElement("br"));



		var submitButton = document.createElement("input");
		submitButton.setAttribute("type","submit");
		submitButton.setAttribute("value","Submit Order");
		theForm.appendChild(submitButton);


		
		document.getElementById(cartDivID).appendChild(theForm);
	}
	else
	{
		var noDice = document.createElement("h2");
		noDice.appendChild(document.createTextNode("Your cart is currently empty."));
		document.getElementById(cartDivID).appendChild(noDice);
	}

}


function checkSubmitFields()
{
	
	if(!document.getElementById("firstName").value.length>0)
	{
		alert("Please enter a first name.");
		return false;
	}
	if(!document.getElementById("lastName").value.length>0)
	{
		alert("Please enter a last name.");
		return false;
	}
	if(!document.getElementById("email").value.length>0)
	{
		alert("Please enter an email address.");
		return false;
	}
	if(!document.getElementById("phone").value.length>0)
	{
		alert("Please enter a phone number");
		return false;
	}

	if( document.getElementById("email").value.search("@")<0 || document.getElementById("email").value.search(".")<0 )
	{
		alert("Please enter a valid email address.");
		return false;
	}
	
	var elemArray = document.getElementsByTagName("input");

	for(var i=0;i<elemArray.length;i++)
	{
		if(elemArray[i].type = "checkbox")
		{
			elemArray[i].checked=true;
			//alert("just checked it");
		}
	}

	//return true;

}

//loops through all the checkboxes with same name and looks to see if they are checked..if they are it removes from the cookie;
function deleteFromCart ()
{
	var checkBoxArray=document.getElementsByTagName("input");
	
	for(var i=0;i<checkBoxArray.length;i++)
	{
		if(checkBoxArray[i].type=="checkbox")
		{
			if(checkBoxArray[i].checked)
			{
				removeFromCart(checkBoxArray[i].id,false);
			}
		}
	}
	window.location.reload(true);
}

