/********************************
 * global sale items array...
 ********************************/
var saleItems = new Object();


/***************************************************************
 * OBJECT saleItem
 *
 *   Creates a notecard saleItem object (same as a regular saleItem,
 *   w/ addition of "desc" and "numInSet" properties)
 *   NOTE: Most of the "regular saleItem" properties are not used
 *         for a notecard, however to keep the code for working w/
 *         the cookies simpler, I included them here too.
 *
 *   returns:
 *     nothing
 *
 *   params:
 *     objArray   = array to add this item to
 *     imageID    = ID for this sale item
 *     title      = title of this sale item
 *     price      = price of this sale item
 *     theDesc    = description for this notecard
 *
 ***************************************************************/



function saleItem(objArray, imageID, title, sm_price, lg_price,theSize)
{
  
  //alert("saleItem:"+imageID + title+ price+ theSize);
  
  
  // properties...
  this.imageID      = imageID;
  this.title        = (title) ? title : "Untitled";
  this.sm_price     = sm_price;
  this.lg_price     = lg_price;
  this.size         = theSize;
  //this.desc         = theDesc;
  //this.numInSet     = num
  //this.mat_num      = 0;
  
  //this.theOption    = 0; 
  //this.sm_mat_chrg  = 0;
  //this.lg_mat_chrg  = 0;

  // methods...
   this.printDesc = printItemDescription;

  // store in product array...
  objArray[imageID] = this;
 //alert(theDesc);
} // end NCsaleItem()





/***************************************************************
 * METHOD saleItem::printItemDescription
 *
 *   Outputs the HTML table item "small image and Add to cart button" to 
 *   the left DIV of the page.
 *
 *   returns:
 *     nothing
 *
 *   params:
 *     TDstr = optional string to include in the TD definition.
 ***************************************************************/
function printItemDescription(TDstr)
{
  document.writeln("    <TD " + TDstr + ">");
  if (this.lg_price == 0){
     document.writeln("      <A HREF=\"javascript:productView('products/" + this.imageID + ".jpg', 'Description: " + this.title + "', 'Size: " + this.size + " ','Price: $" + this.sm_price + " ');  toggleBox('large',1); toggleBox('introImage',0); toggleBox('introDesc',0); \"><IMG SRC=\"products/" + this.imageID + "_sm.jpg\" BORDER=0 width='150'  ALT=\"" + this.title + "\"></A>");
    }
  else if (this.imageID =="381" || this.imageID =="386" || this.imageID =="389")
    {   
	 document.writeln("      <A HREF=\"javascript:dualProductView('products/" + this.imageID + ".jpg', 'Description: " + this.title + "', 'Size: " + this.size + " ', 'Sterling Price: $" + this.sm_price + " ','14K Goldfill Price: $" + this.lg_price + " ');  toggleBox('large',1); toggleBox('introImage',0); toggleBox('introDesc',0); \"><IMG SRC=\"products/" + this.imageID + "_sm.jpg\" BORDER=0 width='150'  ALT=\"" + this.title + "\"></A>");  
    }
  else 
    {
	 document.writeln("      <A HREF=\"javascript:dualProductView('products/" + this.imageID + ".jpg', 'Description: " + this.title + "', 'Size: " + this.size + " ', 'Sterling Price: $" + this.sm_price + " ','14K Gold Price: $" + this.lg_price + " ');  toggleBox('large',1); toggleBox('introImage',0); toggleBox('introDesc',0); \"><IMG SRC=\"products/" + this.imageID + "_sm.jpg\" BORDER=0 width='150'  ALT=\"" + this.title + "\"></A>");  
  } //end if
  
  //document.writeln("      <BR>");
  //document.writeln("      \"" + this.title + "\"");
  //document.writeln("      <BR>");
  document.writeln("      <A HREF=\"javascript:addToCart('" + this.imageID + "');\"><IMG SRC=\"images/addtocart.gif\" BORDER=0 ALT=\"Add to Cart\"></A>");
  document.writeln("   <BR><BR> </TD>");
  

} // end printItemDescription()




/***************************************************************
 * OBJECT orderedItem
 *
 *   Creates an orderedItem object
 *
 *   returns:
 *     nothing
 *
 *   params:
 *     id      = ID of item ordered
 *     qty     = number of these items ordered
 *     b_mat   = whether a mat is requested (0=no, 1=yes)
 *     mat_num = specific mat chosen
 *     size    = size of image selected (0=small, 1=large);
 ***************************************************************/
function orderedItem(id, qty, title, size, theOption)
{
  // properties...
  //this.ID      = id;
 // this.qty     = qty;
 // this.b_mat   = b_mat;
  //this.mat_num = mat_num;
 // this.size    = Size

   this.ID        = id;
   this.qty       = 1;  //qty;
   this.title     = title;
   this.size      = size;
   this.theOption = theOption;  
   //this.desc      = desc;
   
   
} // end orderedItem()




/***************************************************************
 * OBJECT orderedItems
 *
 *   Creates an orderedItems object (array of orderedItem objects)
 *
 *   returns:
 *     nothing
 *
 *   params:
 ***************************************************************/
function orderedItems()
{
  // properties...
  
 
  
  this.items   = new Array();
 
  // constructor...
  this.init = initOrderedItems;


  // public methods...
  this.addItem       = addOrderedItem;
  this.findItem      = findOrderedItem;
  this.deleteItem    = deleteOrderedItem;
  this.toCookie      = toCookieOrderedItems;


  // init this object...
  this.init();

} // end orderedItems()



/***************************************************************
 * METHOD orderedItems::initOrderedItems
 *
 *   Constructor for orderItems object.  Reads in cookie data
 *   (if it exists) and initilizes this object w/ that data.
 *   The expected format of the Artful Artisan cookie:
 *     Cookie name: "ARTI"
 *     Each order's format:  "vvv.w.x.yyyy.z-" where
 *       vvv  = the sale item's ID
 *       w    = qty requested
 *       x    = whether a mat is requested (0=no, 1=yes)
 *       yyyy = mat color number
 *       z    = size of image requested (0=small, 1=large)
 *
 *   returns:
 *     nothing
 *
 *   params:
 *     none
 ***************************************************************/
function initOrderedItems()
{
  var orderStr = getCookieData("ARTI");

  if(!orderStr)  // no order data was found
    return;

  var orderArray = orderStr.split("-");
  var tempArray;

  for(var i=0; i < orderArray.length-1; i++)
  {
    tempArray = orderArray[i].split(".");

    // add this ordered item to items array
    this.items[this.items.length] = new orderedItem(tempArray[0], tempArray[1],
                                          tempArray[2], tempArray[3], tempArray[4]); 
	
  } // end for
  


} // end initOrderedItems()



/***************************************************************
 * METHOD orderedItems::toCookieOrderedItems
 *
 *   Constructs a string based representation of this orderItems object
 *   and writes it to a cookie in the following format:
 *     Cookie name: "ARTI"
 *     Each order's format:  "vvv.w.x.yyyy.z-" where
 *       vvv  = the sale item's ID
 *       w    = qty requested
 *       x    = whether a mat is requested (0=no, 1=yes)
 *       yyyy = mat color number
 *       z    = size of image requested (0=small, 1=large)
 *
 *   returns:
 *     nothing
 *
 *   params:
 *     none
 ***************************************************************/
function toCookieOrderedItems()
{
  var cookieStr = "";
  var numItems = this.items.length;
  for(var i=0; i < numItems; i++)
  {
    if(this.items[i].ID != "DELETED")  // if this item hasn't been deleted
    {
     
	 cookieStr += this.items[i].ID + "." +  
                     this.items[i].qty + "." +
					 this.items[i].title + "." +
                      this.items[i].size + "." +
					 this.items[i].theOption + "-";
    } // end if
  } // end for


  
 if(cookieStr != "")
    setCookie("ARTI", cookieStr);
  else
    deleteCookie("ARTI");

} // end toCookieOrderedItems()



/***************************************************************
 * METHOD orderedItems::addOrderedItem
 *
 *   Adds an orderedItem to the orderedItems array if it isn't
 *   already there
 *
 *   returns:
 *     boolean - true if successful add, false otherwise
 *
 *   params:
 *     id = ID of item to be added
 ***************************************************************/
function addOrderedItem(id)
{
  var itemIdx = this.findItem(id);
  if(itemIdx != -1)  // item was found
    return(false);

  // get default mat number for this item 
  // (and check to make sure this item even exits)...
  var theOption = (saleItems[id]) ? saleItems[id].theOption : -1;
  /////////var qty = saleItems[id].qty;
  /////////var title = saleItems[id].title;
  /////////var size =  saleItems[id].size;

 if(theOption == -1)  // oops, saleItem doesn't exist...
  {
    alert('Oops!  Item "' + id + '" does not exist in our sales dB.\n' +
          'Please contact us and let us know about this error.');
    return(false);
  }

  // determine if this item has a mat associated w/ it...
 //////////// var qty = saleItems[id].qty;
 //////////// var title = saleItems[id].title;
  var theOption = (theOption==0) ? 0 : 1;
 ///////////// var size =  saleItems[id].size;
  // add new ordered item to items array...
  
  this.items[this.items.length] = new orderedItem(id, 77, 88, theOption, 0);

  // write order info back to cookie...
  this.toCookie();

  return(true);

} // end addOrderedItem()




/***************************************************************
 * METHOD orderedItems::deleteOrderedItem
 *
 *   Deletes an orderedItem from the orderedItems array (if it's there)
 *
 *   returns:
 *     boolean - true if successful delete, false otherwise
 *
 *   params:
 *     id = ID of item to be deleted
 ***************************************************************/
function deleteOrderedItem(id)
{
  var itemIdx = this.findItem(id);
  if(itemIdx == -1)  // item wasn't found
    return(false);

  // set this item's ID to "DELETED" so that it won't be written back to the cookie
  this.items[itemIdx].ID = "DELETED";

  // write order info back to cookie...
  this.toCookie();

  return(true);

} // end deleteOrderedItem()




/***************************************************************
 * METHOD orderedItems::findOrderedItem
 *
 *   Finds an orderedItem in the orderedItems array (if it exists)
 *
 *   returns:
 *     int - index of requested orderItem if found, otherwise -1
 *
 *   params:
 *     id      = ID of item to be found
 ***************************************************************/
function findOrderedItem(id)
{
  for(var i=0; i<this.items.length; i++)
  {
    if(this.items[i].ID == id)
      return(i);
  } // end for
  
  // didn't find item, so return -1
  return(-1);

} // end findOrderedItem()




/***************************************************************
 * GLOBAL FUNCTION getCookieData
 *
 *   Gets the cookie data associated w/ a cookie name.
 *
 *   returns:
 *     string - unescaped cookie data if cookie name is found, otherwise
 *              null is returned
 *
 *   params:
 *     label  = label of cookie whose data you want to retrieve
 ***************************************************************/
function getCookieData(label)
{
  var labelLen = label.length;
  var cLen = document.cookie.length;
  var i = 0;
  var cEnd;

  while(i < cLen)
  {
    var j = i + labelLen;
    if(document.cookie.substring(i,j) == label)  // found cookie name
    {
      cEnd = document.cookie.indexOf(";", j);
      if(cEnd == -1)
        cEnd = document.cookie.length;
      return( unescape(document.cookie.substring(j+1,cEnd)) );
    } // end if

    i++;
  } // end while

  return( null );
} // end getCookieData()



/***************************************************************
 * GLOBAL FUNCTION setCookie
 *
 *   Sets the cookie data associated w/ a cookie name.
 *
 *   returns:
 *     nothing
 *
 *   params:
 *     name  = cookie's name
 *     value = cookie's value
 ***************************************************************/
function setCookie(label, value)
{
  // calculate expiration date (15 days from now)...
  var exp = new Date();
  var halfMonthFromNow = exp.getTime() + 1296000000;
  exp.setTime(halfMonthFromNow);


//alert(label+"value:"+value);


document.cookie = label + "=" + escape(value) +
    "; expires=" + exp.toGMTString();

} // end setCookie()



/***************************************************************
 * GLOBAL FUNCTION deleteCookie
 *
 *   Deletes the cookie associated w/ a cookie name.
 *
 *   returns:
 *     nothing
 *
 *   params:
 *     name  = name of cookie to delete
 ***************************************************************/
function deleteCookie(label)
{
  document.cookie = label + "=" +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
} // end deleteCookie() 



/***************************************************************
 * GLOBAL FUNCTION addToCart
 *
 *   Adds a sale item to the orderedItems object and calls
 *   "gotoPageWithFrom" to redirect to shopping cart page
 *
 *   returns:
 *     nothing
 *
 *   params:
 *     name  = name of cookie to delete
 ***************************************************************/
function addToCart(id)
{
  // create new orderedItems object (reading in all cookie data, if it exists)...
  
  
  
  var ordered = new orderedItems();



  // add this item to the orderedItems array (implicitly writing new cookie to disk)...
  ordered.addItem(id);

  // redirect to shopping cart page...
  gotoPageWithFrom("shoppingcart.html");

} // end addToCart()

