﻿// UI_ShoppingListDiv.js
// Ryan van der Kooy
// 02/27/08
// eComSystems, Inc.

// Classes:
// ShoppingListDiv

ShoppingListDiv = function() {


    this.top = 0;
    this.left = 0;
    this.width = 0;
    this.height = 0;
    this.whiteCell = true;
    
    this.listDiv = document.getElementById("listDiv");
   
   
    
    
    this.listTable = document.getElementById("listTable");
    this.listTable.style.width = this.width;
    
    this.addMyOwn = document.getElementById("addMyOwn");
    //this.addMyOwn.style.width = this.width;
    
   // this.renderBackground();
   
   
    
}

ShoppingListDiv.prototype.setTop = function(top) {
    this.top = top;
    this.listDiv.style.top = top;
    
    this.addMyOwn.style.top = parseInt(this.listDiv.style.height.replace("px", "")) + top;
  
    
   
}

ShoppingListDiv.prototype.setLeft = function(left) {
    this.listDiv.style.left = left;
    this.left = left;
    this.addMyOwn.style.left = left;
}

ShoppingListDiv.prototype.setHeight = function(height) {
    this.listDiv.style.height = height;
    this.height = height;
    try {
        this.addMyOwn.style.top = this.listDiv.style.height + this.listDiv.style.top;
        } catch(err) {
        
        
        }
}

ShoppingListDiv.prototype.setWidth = function(width) {
    this.listDiv.style.width = width;
    this.width = width;
    this.addMyOwn.style.width = width;
}

ShoppingListDiv.prototype.addProduct = function(listItem) {
    
  

        var newrow=this.listTable.insertRow(-1) //add new row to end of table  
            newrow.style.width=this.width + 20;
            newrow.id = listItem.id.toString();
            
        var newQuantityCell=newrow.insertCell(0);
            newQuantityCell.style.width = "35px";
            newQuantityCell.className = "slItem";
         //insert new cell to row
            newQuantityCell.innerHTML=listItem.quantity;
        var newItemCell = newrow.insertCell(1);
            newItemCell.style.width = "285px";
            newItemCell.className = "slItem";
            
            var maxCharLength = 25;
        var sLBrand = listItem.brand;
        if(sLBrand.length > maxCharLength) { sLBrand = Left(sLBrand, maxCharLength) + "..."; }
        var sLTitle = listItem.title;
        if(sLTitle.length > maxCharLength) { sLTitle = Left(sLTitle, maxCharLength) + "..."; }
           
            newItemCell.innerHTML = sLBrand + "<br/>" + sLTitle;
            newItemCell.innerHTML +="<br><div style='font-size:6pt; text-align:right;'><a href='javascript:ShoppingList.removeListItem(" + "\"" + listItem.id.toString() + "\"" + ");'>Remove</a></div>";
         
    //}

}

ShoppingListDiv.prototype.removeProduct = function(listItemID) {
   
    var rowToDelete = document.getElementById(listItemID);
    this.listTable.removeChild(rowToDelete);
}




function Left(str, n) {
                if (n <= 0)     // Invalid bound, return blank string
                        return "";
                else if (n > String(str).length)   // Invalid bound, return
                        return str;                // entire string
                else // Valid bound, return appropriate substring
                        return String(str).substring(0,n);
        }



