﻿// ShoppingListComponentLib.js
// Ryan van der Kooy
// 02/27/08
// eComSystems, Inc.

// Classes:
// ListItem

ShoppingList = new Array();

ShoppingList.imprintLocation = "";

ShoppingList.shoppingListText = "";
ShoppingList.slTextBuilder = new Array;
ShoppingList.addListItem = function(listItem) {
    //ShoppingList.push(listItem);
    ShoppingList[ShoppingList.length] = listItem;
    
}



ShoppingList.addMyListItem = function() {
    var qty = document.getElementById("QtyInput").value;
    var title = document.getElementById("DescriptionInput").value;
    
    if ((qty != "") && (title != "")) {
    var myID = Math.floor(Math.random()*99999)

    var listTable = document.getElementById("listTable");
    
    var newrow= listTable.insertRow(-1) //add new row to end of table
            newrow.style.width= parseInt(listTable.style.width.toString().replace("px", "") + 20);        
            newrow.id = myID.toString();
            
        var newQuantityCell=newrow.insertCell(0);
            newQuantityCell.style.width = "35px";    
            newQuantityCell.className = "slItem";
         //insert new cell to row
            newQuantityCell.innerHTML=qty;
        var newItemCell = newrow.insertCell(1);
            newItemCell.style.width = "285px";
            newItemCell.className = "slItem";
            
            var maxCharLength = 25;
       
        var sLTitle = title;
        if(sLTitle.length > maxCharLength) { sLTitle = Left(sLTitle, maxCharLength) + "..."; }
            
            newItemCell.innerHTML = "<br/>" + sLTitle;
            newItemCell.innerHTML +="<br><div style='font-size:6pt; text-align:right;'><a href='javascript:ShoppingList.removeListItem(" + "\"" + myID.toString() + "\"" + ");'>Remove</a></div>";
            

            document.getElementById("QtyInput").value = "1";
            document.getElementById("DescriptionInput").value = "";
            
            var myItem = new ListItem(myID);
            myItem.title = title;
            myItem.brand = "";
            myItem.quantity = qty;
            myItem.imageLocation = "assets/filler.png";
            myItem.description = "";
            myItem.size = "";
            myItem.unit = "";
            myItem.department = "My Added Items";
            myItem.price = "";
            
            ShoppingList.addListItem(myItem);
            } else {
                alert("please enter values for quantity and description");
            }
            
}

ShoppingList.removeListItem = function(myID) {

    

    //remove from table
    var rowToDelete = document.getElementById(myID);
    
    var listTable = document.getElementById("listTable");
    listTable.removeChild(rowToDelete);
    
    //remove from shoppinglist.
    for (i=0;i<ShoppingList.length;i++) {
        if (ShoppingList[i].id == myID) {
            ShoppingList.splice(i, 1);
        }
    }
}

ShoppingList.showReport = function() {
    ShoppingList.shoppingListText = "";
    ShoppingList.slTextBuilder = [];
    var numberOfColumns = 5;

    //document.getElementById("SilverlightControl").height = "0%";
    hideHTMLElements();
    document.getElementById("AdViewer").style.height = "0px"; //03/14/08
    document.getElementById("SilverlightControl").style.height = "0px";
    //document.getElementById("AdViewer").style.height = "0px";  
    //document.getElementById("closeShoppingListReport").innerHTML = "Close";
    document.getElementById("shoppingListReport").style.visibility = "visible";
    ShoppingList.shoppingListText = "<table><tr>";
    ShoppingList.shoppingListText += "<td colspan='" + numberOfColumns + "' style='text-align:center';><img src='" + ShoppingList.imprintLocation + "' width='350'/>";
    //ShoppingList.shoppingListText += "<br>" + Store.name + "<br>" + Store.address1 + Store.address2 + "<br>" + Store.city + ", " + Store.state + " " + Store.zip + "<br>" + Store.phone + "</td>"; 
    ShoppingList.shoppingListText += "</tr><tr><td colspan='" + numberOfColumns + "' >Shopping list generated on " + Date() + "<br>" + gblGoodThru + "</td>";
    ShoppingList.shoppingListText += "</tr></table><br> ";

    ShoppingList.shoppingListText += "<table style='padding:20px; border-spacing:10px; background-color:White; border:Solid 1 Black'> ";
    ShoppingList.shoppingListText += "<tr style='font-weight:bold;font-size:16;text-align:right;'><td colspan='" + numberOfColumns + "'><img src='assets/close.jpg' alt='close' style='cursor:pointer;' onclick='ShoppingList.closeReport();'/><br/></td></tr>";
    ShoppingList.shoppingListText += "<tr style='font-weight:bold;'><td colspan=2>Product</td><td>Item Description</td><td>Price/Unit&nbsp&nbsp</td><td>Qty</td></tr> ";


    var sortedShoppingList = new Array();
    sortedShoppingList = ShoppingList;
    sortedShoppingList.sort(compareDepartments);






    //for (s=0;s<ShoppingList.length;s++) {
    //   ShoppingList.AddProductToTableString(ShoppingList[s]);
    // }


    ShoppingList.shoppingListText += ShoppingList.GenerateTableString(sortedShoppingList);
    document.getElementById("shoppingListReport").innerHTML = ShoppingList.shoppingListText;
    //document.execCommand('print',false,null)
    document.getElementById("shoppingListReport").focus();

    document.getElementById("AGNotInstalled").style.display = "none";
    document.getElementById("AdViewer").style.display = "none";
    document.getElementById("listDiv").style.display = "none";
    document.getElementById("addMyOwn").style.display = "none";
    document.getElementById("emailControls").style.display = "none";

    for (i = 0; i < ShoppingList.length; i++) {
        var currImage = new Image();
        currImage = document.getElementById(ShoppingList[i].id + "_thumbImage");
        while (currImage == NaN) {
            if (currImage.height < currImage.width) {
                currImage.width = 100;
            } else {
                currImage.height = 100;
            }
            currImage = null;
        }
    }
    window.print();

}

function compareDepartments(a,b) {
    if (a.department == b.department) { 
        return 0;
        }else {
            return 1;
        }
    

}

ShoppingList.AddProductToTableString = function(shoppingListItem) {
    //iterate through current strings - one line per table row
    //if shoppingListItem.department is in the list already, add it
    //if not, add a new department, then add it.
    var deptIsInList = false;
    
    var preDeptDecoration = "<tr><td colspan=5 style='border-width: 1px 1px 1px 1px; border-color: Black; border-style: solid;'>";
    var postDeptDecoration = "</td></tr>";
    
    for (b=0;b<=ShoppingList.slTextBuilder.length;b++) {
    var prodDept = preDeptDecoration + shoppingListItem.department + postDeptDecoration;
    var currText = ShoppingList.slTextBuilder[b];
            if (preDeptDecoration + shoppingListItem.department + postDeptDecoration == ShoppingList.slTextBuilder[b]) {
            deptIsInList = true;
            }
        }
           if (deptIsInList) {
             ShoppingList.slTextBuilder[ShoppingList.slTextBuilder.length] = "<tr  style='text-align:left;'><td><img src='" + shoppingListItem.imageLocation + "' width='50'/></td>";
             ShoppingList.slTextBuilder[ShoppingList.slTextBuilder.length - 1] += "<td style='font-style:italic;'>" + shoppingListItem.brand + "</td>";
             ShoppingList.slTextBuilder[ShoppingList.slTextBuilder.length - 1] += "<td>" + shoppingListItem.title + "&nbsp&nbsp&nbsp<br>" + shoppingListItem.size + "</td>";
             ShoppingList.slTextBuilder[ShoppingList.slTextBuilder.length - 1] += "<td style='width:50px'>" + shoppingListItem.price + "&nbsp" + shoppingListItem.unit + "&nbsp</td>";
             ShoppingList.slTextBuilder[ShoppingList.slTextBuilder.length - 1] += "<td>" + shoppingListItem.quantity + "</td>";
             ShoppingList.slTextBuilder[ShoppingList.slTextBuilder.length - 1] += "</tr>";
           }else{
             ShoppingList.slTextBuilder[ShoppingList.slTextBuilder.length] = preDeptDecoration + shoppingListItem.department + postDeptDecoration;
             ShoppingList.slTextBuilder[ShoppingList.slTextBuilder.length] = "<tr style='text-align:left;'><td><img src='" + shoppingListItem.imageLocation + "' width='60' height='60'/></td>";
             ShoppingList.slTextBuilder[ShoppingList.slTextBuilder.length - 1] += "<td style='font-style:italic;'>" + shoppingListItem.brand + "</td>";
             ShoppingList.slTextBuilder[ShoppingList.slTextBuilder.length - 1] += "<td>" + shoppingListItem.title + "&nbsp&nbsp&nbsp<br>" + shoppingListItem.size + "</td>";
             ShoppingList.slTextBuilder[ShoppingList.slTextBuilder.length - 1] += "<td style='width:50px'>" + shoppingListItem.price + "&nbsp" + shoppingListItem.unit + "&nbsp</td>";
             ShoppingList.slTextBuilder[ShoppingList.slTextBuilder.length - 1] += "<td>" + shoppingListItem.quantity + "</td>";
             ShoppingList.slTextBuilder[ShoppingList.slTextBuilder.length - 1] += "</tr>";
           }
           

}

ShoppingList.GenerateTableString = function(sortedShoppingList) {
    var listString = "";
    var oldDept = "";
    for (i = 0; i < sortedShoppingList.length; i++) {
        if (oldDept != sortedShoppingList[i].department) {
            listString += "<tr><td colspan=5 style='border-width: 1px 1px 1px 1px; border-color: Black; border-style: solid;'>";
            listString += sortedShoppingList[i].department;
            listString += "</td></tr>";
            oldDept = sortedShoppingList[i].department;
        }
        listString += "<tr style='text-align:left;'><td>";
        if (sortedShoppingList[i].imageLocation != '') {
            listString += "<img id='" + sortedShoppingList[i].id + "_thumbImage' src='" + sortedShoppingList[i].imageLocation + "' />";
        }
        listString += "       </td>";
        listString += "<td style='font-style:italic;'>" + sortedShoppingList[i].brand + "</td>";
        listString += "<td>" + sortedShoppingList[i].title + "&nbsp&nbsp&nbsp<br>" + sortedShoppingList[i].size + "</td>";
        listString += "<td style='width:50px'>" + sortedShoppingList[i].price + "&nbsp" + sortedShoppingList[i].unit + "&nbsp</td>";
        listString += "<td>" + sortedShoppingList[i].quantity + "</td>";
        listString += "</tr>";





    }
    listString += "</table>";
    return listString;
}

ShoppingList.closeReport = function() {
    showHTMLElements();
    document.getElementById("AGNotInstalled").style.display = "";
    document.getElementById("AdViewer").style.display = "";
    document.getElementById("listDiv").style.display = "";
    document.getElementById("addMyOwn").style.display = "";
    document.getElementById("emailControls").style.display = "";
    
    
    document.getElementById("shoppingListReport").innerHTML = "";
    document.getElementById("closeShoppingListReport").innerHTML = "";
 
 //document.getElementById("AdViewer").style.height = "1024px";//03/14/08
 document.getElementById("SilverlightControl").style.height = gblPlugInHeight + "px";
 
 }


 ShoppingList.emailReport = function() {


     var emailTo = document.getElementById("emailToTextBox").value;

     var shoppingListText = "";

     for (i = 0; i < ShoppingList.length; i++) {

         shoppingListText += "{b}" + ShoppingList[i].brand + "{}";
         if (ShoppingList[i].title != "") { shoppingListText += "#@"; }
         shoppingListText += "{t}" + ShoppingList[i].title + "{}";
         if (ShoppingList[i].unit != "") { shoppingListText += "#@"; }
         shoppingListText += "{u}" + ShoppingList[i].unit + "{}";
         if (ShoppingList[i].quantity != "") { shoppingListText += "#@"; }
         shoppingListText += "{q}Quantity: " + ShoppingList[i].quantity + "{}";
         if (ShoppingList[i].price!= "") { shoppingListText += "#@"; }
         shoppingListText += "{p}" + ShoppingList[i].price + "{}";
         shoppingListText += "#@";
         shoppingListText += "#@";
         

     }





     xmlhttp = null;
     if (window.XMLHttpRequest) {// code for Firefox, Opera, IE7, etc.
         xmlhttp = new XMLHttpRequest();
     }
     else if (window.ActiveXObject) {// code for IE6, IE5
         xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
     }
     if (xmlhttp != null) {
         xmlhttp.onreadystatechange = state_Change;
         try {

             xmlhttp.open("POST", "shop.aspx?to=" + emailTo + "&cl=" + gblDist + "&adid=" + gblAdId + "&list=" + escape(shoppingListText), true);
             xmlhttp.send();
         } catch (err) {
             //two dots (..) means the list was probably too long ( > approx 16 items) to be put into the query string
             //another method of sending the email will be developed in a future release.
             //alert(err.description);
             document.getElementById("emailStatus").innerText = "Unable to generate email..";

         }
     }
     else {
         alert("unsupported browser");
     }
 }

function state_Change()
{
if (xmlhttp.readyState==4)
  {// 4 = "loaded"
  if (xmlhttp.status==200)
    {// 200 = "OK"
    document.getElementById("emailStatus").innerText = "Email Sent Succesfully!";
    //alert(xmlhttp.responseText);
    }
  else
    {
    //alert("Problem retrieving data:" + xmlhttp.statusText);
    //one dot (.) means probably an invalid email address.
    document.getElementById("emailStatus").innerHTML = "Unable to generate email.<br>Please enter a valid email address. ";
    }
  }

}



ShoppingList.showResult = function(result) {
        alert(result);
}


ListItem = function(id) {
    
    this.id;
    this.brand;
    this.title;
    this.imageLocation;
    this.price;
    this.quantity;
    this.size;
    this.unit;
    this.department;
    this.setID(id);
    this.myItem = false;
    
}

ListItem.prototype.setID = function(id) {
    this.id = id;
}

ListItem.prototype.setBrand = function(brand) {
    if (brand == null) { brand=""};
    this.brand = brand.replace("'", "").replace("&", "");
}

ListItem.prototype.setTitle = function(title) {
    this.title = title.replace("'", "").replace("&", "");
}

ListItem.prototype.setImageLocation = function(imageLocation) {
    this.imageLocation = imageLocation;
}
ListItem.prototype.setPrice = function(price) {
    this.price = price.replace("'", "").replace("&", "");
}
ListItem.prototype.setQuantity = function(quantity) {
    this.quantity = quantity;
}
ListItem.prototype.setSize = function(size) {
    this.size = size;
}  

ListItem.prototype.setUnit = function(unit) {
    this.unit = unit;
} 
ListItem.prototype.setDepartment = function(department) {
    this.department = department;
}  
 

