// JavaScript Document
// Written by Ike Okoro
// 11 Nov 2005
// Modified by Peter Havranek
// 22 Nov 2005
// (c) Copyright 2005.  All rights reserved.
//

//*****************************************************
//addCookie : Adds an item to the cookie
//*****************************************************
function addItem(description, price, img){
  
  //get cookie from document
  var current_cookie = document.cookie;
  
  //get cookie length
  var cookie_length = current_cookie.length; 
  
  //find beginning of cookie 
  var beginning = current_cookie.indexOf("cart"); 
  
  //cookie item to be added
  var updatedCookie="cart="+description+"^"+price+"^"+img+"^";
  
  //initialize left part of cookie
  var cookieLeft="";  
  
  //initialize right part of cookie
  var cookieRight="";  
  
  //no items have been placed in the cart yet
  var itemPlaced = false;
  
  //if the cookie doesn't exist
  if (beginning == -1)
  {
    //set the right side of the cookie equal to the  whole cookie
    cookieRight=current_cookie; 
    
    //an item has been placed in the shopping cart
    itemPlaced = true;  
  }
  else  //cookie exists
  {
  
    //cart cookie isn't the first item in the cookie string
    if(beginning!=0)
      cookieLeft=document.cookie.substring(0, beginning);
  
    var index = document.cookie.indexOf("=", beginning);
  
    //if cookie not empty
    if(index!=(cookie_length-1))
      cookieRight = document.cookie.substring(index + 1, cookie_length);
  
    var itemLocation = document.cookie.indexOf(img, index);
    var descLocation = document.cookie.indexOf(description, index);
 
    //item is in cart, now checks BOTH itm and description variables
    if ((itemLocation != -1) && (descLocation != -1))
    {
      updatedCookie="cart=";
      alert("This item is already in your shopping bag.");
    }
    else  //item isn't in cart
      itemPlaced = true;  //item will be placed in the cart

  }
    
    //set the cookie value
    document.cookie = cookieLeft + updatedCookie + cookieRight;
    
    //if an item was placed in the cart then output a message saying so
    if(itemPlaced)
      alert("This item has been placed in your shopping bag.  To complete purchase, click on the 'View Shopping Bag' link at the top of the page.");
      
      //ORIGINAL MESSAGE
      //alert("This item has been placed in your shopping bag.  To complete purchase, click on the 'View Shopping Bag' link at the top of the page.");

}




//*****************************************************
//deleteCookie : Takes an item out of the cookie
//*****************************************************
function deleteItem(description){

var checkyn = confirm("Are you sure you want to remove this item from your shopping bag?");

if (checkyn) {

  var cookieLocation = document.cookie.indexOf(description);  //location of cookie
  var cookieRight = "";
  
  //location of first delimiter
  var delimiter = document.cookie.indexOf("^", cookieLocation);  
  
  //location of second delimiter
  delimiter = document.cookie.indexOf("^", delimiter + 1);  
  
  //location of third delimiter
  delimiter = document.cookie.indexOf("^", delimiter + 1);
  
  var cookieLeft = document.cookie.substring(0, cookieLocation);

  //if there are anymore items after this current one
  if(document.cookie.length > (delimiter + 2 )) {

  //if it's not the end of the cookie
    if(document.cookie.charAt(delimiter + 1) != "" &&     document.cookie.charAt(delimiter + 1) != ";")
        cookieRight=document.cookie.substring(delimiter+1,       document.cookie.length);
  }
  
  //if this is the last item in the cookie then make it expire
  if(cookieLeft.charAt(cookieLeft.length - 1) == "=" && (cookieRight == "" || cookieRight ==";"))
    document.cookie = "cart=empty; expires=Fri, 14 Oct 2005 12:00:00 UTC;";
  else
    document.cookie=cookieLeft+cookieRight;
    
  //reload the window
  window.location.reload();
}

}




//*****************************************************
//displayCart : Outputs the cart to the web page
//*****************************************************

function displayCart(){

var current_cookie = document.cookie;
var beginning = -1

if(current_cookie!="")
  var beginning = current_cookie.indexOf("cart=");
	if( beginning != -1 && (current_cookie.charAt(beginning + 5)!=";") && (current_cookie.charAt(beginning + 5)) != "")
	{
	  var index = beginning + 5;
	  var total = 0;
	  var numItems = 0;
	  var price;
	  var description;
	  document.write("<table width='500' border=0 cellpadding=0 cellspacing=0>");
	  document.write("<td colspan=3><br><br><span class='body'><b>Items in shopping bag:</b><br><br><br></span></td></tr>");
    document.write("<tr><td align=\"center\" valign=\"top\"><span class='body'>[Remove]&nbsp;&nbsp;&nbsp;</span></td>");
	  document.write("<td valign=\"top\"><span class='body'>Item Description (click for image)</span></td>");
	  document.write("<td align=\"right\" valign=\"top\"><span class='body'>Price&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></td></tr>");
    document.write("<tr><td bgcolor=\"#018c4c\" colspan=3><img src=\"images/green.gif\" width=\"1\" height=\"2\"></td></tr>");

    //while it hasn't reached the end of the cookie
		while(current_cookie.charAt(index) != ";" && current_cookie.charAt(index) != "")
		{
		  //first delimiter
		  var delimiter = current_cookie.indexOf("^", index);
		  description = current_cookie.substring(index, delimiter);
		  index = delimiter + 1;
		  
		  //second delimiter
		  delimiter = current_cookie.indexOf("^", index);
		  price = current_cookie.substring(index, delimiter);
		  index = delimiter + 1;
		  
		  //increment the total
		  total = total + parseFloat(price);
		  
		  //third delimiter
		  delimiter = current_cookie.indexOf("^", index);
		  var img = current_cookie.substring(index, delimiter);
		  index = delimiter + 1;
		  
		  //increment the number of items in the cart
		  numItems++;
		  document.write("<tr><td align=\"center\" valign=\"top\"><br><span class='body'><a href=\"javascript:deleteItem('" + description + "')\"><b>X</b></a></span></td>");
		  document.write("<td valign=\"top\"><br><span class='body'><a href=\"javascript:PopUpWindow('" + img + "')\">" + description + "</a></span></td>");
      var displayprice = formatTwoDecimals(price);
      document.write("<td align=\"right\" valign=\"top\"><br><span class='body'>$ " + displayprice + "&nbsp;&nbsp;&nbsp;&nbsp;<br></span></td></tr>");
      document.write("<input type='hidden' name='item_name_" + numItems + "' value='" + description + "'>");
      document.write("<input type='hidden' name='amount_" + numItems + "' value='" + price + "'>");
    }
    document.write("<tr><td><br></td></tr>");
    document.write("<tr><td bgcolor=\"#018c4c\" colspan=3><img src=\"images/green.gif\" width=\"1\" height=\"2\"></td></tr>");
    var displaytotal = formatTwoDecimals(total);
    document.write("<tr><td colspan='2'></td><td align=\"right\"><span class='body'><br><br> Total:&nbsp;&nbsp;&nbsp;$" + displaytotal + "&nbsp;&nbsp;&nbsp;&nbsp;</span><br><br></td></tr>");
    document.write("<tr><td bgcolor=\"#018c4c\" colspan=3><img src=\"images/green.gif\" width=\"1\" height=\"2\"></td></tr>");
    document.write("<tr><td bgcolor=\"#000000\" colspan=3><span class='copyright'><br>All orders are placed through the Paypal system, a 100% secure server. We ship out orders within 3 business days from when they are received. We do not ship on the weekends. So please allow 2 weeks for items to arrive after the jewelry has been shipped. Currently Jill Bush Contemporary Jewelry only ships to addresses within the United States. Satisfaction is of course guaranteed. You may return your purchase for any reason with three business days, in its original condition and packaging. Your payment, minus shipping, will be mailed promptly. In the unlikely event that an item is unavailable, we reserve the right to provide a substitute or refund whichever the customer prefers. Unless otherwise noted, all information, designs and content on this Web Site are ©Copyright 2003-2005 by Jill Bush Contemporary Jewelry.   All rights reserved.  Digital images or text may not be copied by electronic or other means without the express written permission of Jill Bush Contemporary Jewelry. Any type of commercial use or reproduction in whole or part in any form without prior permission from Jill Bush Contemporary Jewelry is expressly prohibited.<br><br></span></td></tr>");
    document.write("<tr><td bgcolor=\"#018c4c\" colspan=3><img src=\"images/green.gif\" width=\"1\" height=\"2\"></td></tr>");
    document.write("<tr><td colspan='3' align=\"center\"><br><input type='submit' value='Complete my Purchase at PayPal'>");
    document.writeln("<span class='body'><br><br>Or continue browsing through the jewelry catalog:<br><br><a href=\"necklaces.html\">Necklaces</a><br><a href=\"bracelets.html\">Bracelets</a><br><a href=\"earrings.html\">Earrings</a><br><a href=\"rings.html\">Rings</a><br><br>When you see an item you'd like to purchase, click the \"buy\" link next to it.</span>");
    document.write("</td></tr></table>");
  }
  else{
    document.writeln("<span class='body'><br>Cart is empty!<br><br>Please browse through the jewelry catalog:<br><br><a href=\"necklaces.html\">Necklaces</a><br><a href=\"bracelets.html\">Bracelets</a><br><a href=\"earrings.html\">Earrings</a><br><a href=\"rings.html\">Rings</a><br><br>When you see an item you'd like to purchase, click the \"buy\" link next to it.</span>");
  }

}


function removeCookie()
{
  document.cookie = "cart=Quackit JavaScript cookie experiment; expires=Fri, 14   Oct 2005 12:00:00 UTC;";
}

function formatTwoDecimals (numtoformat)   // Written by Peter Havranek, 22 Nov 2005
{
  var numtrunc = Math.floor(numtoformat);
  var nummod = (numtoformat * 100) % 100;
  
  var numresult = numtrunc + "." + nummod;
  if (nummod == 0) numresult = numresult + "0";
  
  return numresult;
}

