﻿
function SearchItem(source, eventArgs) {
     if (eventArgs.get_value() != null)
         getCanSearchInventoryItem(eventArgs.get_value());

     var e = document.getElementById("processing");
     e.style.visibility = 'hidden';
 }

 function resetSearch() {
     document.getElementById("txtSearch").value = "";
 }

 function ShowIcon() {
     var e = document.getElementById("processing");
     e.style.visibility = (e.style.visibility == 'visible') ? 'hidden' : 'visible';
 }

 function cartShow() {
     $find('ModelCartBehavior').show();

 }

 function closeCart() {
     $find('ModelCartBehavior').hide();
 }

 function LoadEmailDialog(inventorykey) {
     $find('ModalSendEmailBehaviour').show();
 }
 function CloseEmailDialog() {
     $find('ModalSendEmailBehaviour').show();
     alert('Thank you.  The product has been emailed to your friend.');
 }
 
 function addItem(imageUrl, inventoryKey, description) {
     var api = $("div.scrollable").scrollable();
     api.getItemWrap().append("<div><img src='" + imageUrl + "' height='95px' width='95px' border='0' alt='" + description + "' onclick='GetDetails(" + inventoryKey + ")' /></div>");

 }

 function search() {
     var lgn = document.getElementById("txtSearch").value.length;
     if (document.getElementById("txtSearch").value != "" && lgn >= 2) {
         searchProducts(document.getElementById("txtSearch").value, 1);
     }
 }

 function LoadProduct(ck, product) {
     price = 0;
     getBestValue(ck);
 }
 function LoadSearchProduct(ck, product) {
     price = 0;
     $get("thumbsdest").innerHTML = "";
     getProductByCategory(ck, true);
 }

 function LoadSubProduct(sk) {
     price = 0;
     getBestValueSubCategory(sk);
     $get("thumbsdest").innerHTML = "";
     getProductBySubCategory(sk, true);
 }

 function LoadSubProductType(stk) {
     price = 0;
     getBestValueSubCategoryType(stk);
     $get("thumbsdest").innerHTML = "";
     getProductBySubCategoryType(stk, true);
 }


 function getCanSearchInventoryItem(ik) {
     productsWS.GetRegionInventoryItem(ik,1,1, BuildCanInventorySearchDetail, of);
 }

 function getUSSearchInventoryItem(ik) {
     productsWS.GetRegionInventoryItem(ik,1,1, BuildUSInventorySearchDetail, of);
 }

 function BuildCanInventorySearchDetail(msg) {
     if (msg.IsPromo) {
         BuildPromoItemDetailNew(msg);
         LoadProduct(msg.CategoryKey, msg.RSSSModel);
         return;
     }
     if (msg.IsFeatureItem) {
         BuildFeaturedItemDetailNew(msg);
         LoadProduct(msg.CategoryKey, msg.RSSSModel);
         return;
     }
     if (msg.IsBestValueItem) {
         BuildBestValueDetailNew(msg);
         LoadProduct(msg.CategoryKey, msg.RSSSModel);
         return;
     }
     if (msg.IsTopSeller) {
         BuildInventoryDetailNew(msg);
         LoadProduct(msg.CategoryKey, msg.RSSSModel);
         return;
     }
     

     BuildInventoryDetailNew(msg);

     LoadSearchProduct(msg.CategoryKey, msg.RSSSModel);

 }

 function getActiveInventoryImages(ck) {
     productsWS.GetActiveInventoryImages(ck, preloadImages, of);
 }

 function loadInventoryItem(ik) {
     productsWS.GetRegionInventoryItem(ik,1,1, LoadCategoryItem, of);
 }

function getInventoryItem(ik) {
    productsWS.GetRegionInventoryItem(ik,1,1, BuildCategoryItem, of);
}
function getBestValueItem(ik) {
    productsWS.GetRegionInventoryItem(ik,1,1, BuildBestValueDetailNew, of);
}
function getFeaturedItem(ik) {
    productsWS.GetRegionInventoryItem(ik,1,1, BuildFeaturedItemDetailNew, of);
}
function GetMoreInfo(ik) {
    
}

function getBestValue(ck) {
    productsWS.GetBestValueItemByCategory(ck, 1, getBestValueResult);
}

function getBestValueSubCategory(sck) {
    productsWS.GetBestValueItemBySubCategory(sck, 1, getBestValueSubCategoryResult);
}

function getBestValueSubCategoryType(stk) {
    productsWS.GetBestValueItemBySubCategoryType(stk, 1, getBestValueSubCategoryTypeResult);
}

function getBestValueResult(msg) {
    if (msg != null) {
        productsWS.GetRegionInventoryItem(msg.InventoryKey,1,1, BuildSearchItem, of);
    }
}

function getBestValueSubCategoryResult(msg) {
    if (msg != null) {
        productsWS.GetRegionInventoryItem(msg.InventoryKey, 1,1, BuildCategoryItem, of);
    }
}

function getBestValueSubCategoryTypeResult(msg) {
    if (msg != null) {
        productsWS.GetRegionInventoryItem(msg.InventoryKey,1,1, BuildCategoryItem, of);
    }
}

function preloadImages(msg) {
   
    $get("dvPreload").innerHTML = msg;
}

function LoadCategoryItem(msg) {

    if (msg.IsPromo)
    { BuildPromoItemDetailNew(msg); loadProductByCategory(msg, 1); return; }
   
    if (msg.IsFeatureItem) {
        BuildFeaturedItemDetailNew(msg);
        loadProductByCategory(msg, 1); return;
    }
    if (msg.IsBestValueItem)
    { BuildBestValueDetailNew(msg); loadProductByCategory(msg, 1); return; }
    if (msg.IsTopSeller)
    { BuildInventoryDetailNew(msg); loadProductByCategory(msg, 1); return; }
   
    BuildInventoryDetailNew(msg);
    loadProductByCategory(msg, 1);
}

function BuildCategoryItem(msg) {
    if (msg.IsPromo)
    { BuildPromoItemDetailNew(msg); return; }
     if (msg.IsFeatureItem)
    { BuildFeaturedItemDetailNew(msg); return; }
    if (msg.IsBestValueItem)
    { BuildBestValueDetailNew(msg); return; }
    if (msg.IsTopSeller)
    { BuildInventoryDetailNew(msg); return; }
    

    BuildInventoryDetailNew(msg);
    }

    function BuildSearchItem(msg) {
        if (msg.IsPromo) {
            BuildPromoItemDetailNew(msg); $get("thumbsdest").innerHTML = "";
            //if (msg.SubCategoryKey == 0)
                getProductByCategory(msg.CategoryKey, 1);
            //else
            //    getProductBySubCategory(msg.SubCategoryKey, 1);
            return;
        }
    if (msg.IsFeatureItem)
    { BuildFeaturedItemDetailNew(msg); 
    $get("thumbsdest").innerHTML = "";
    //if(msg.SubCategoryKey == 0)
        getProductByCategory(msg.CategoryKey, 1);
    //else
    //    getProductBySubCategory(msg.SubCategoryKey, 1);        
     return; }
    if (msg.IsBestValueItem)
    { BuildBestValueDetailNew(msg);
    $get("thumbsdest").innerHTML = "";
    //if(msg.SubCategoryKey == 0)
        getProductByCategory(msg.CategoryKey, 1);
    //else
    //    getProductBySubCategory(msg.SubCategoryKey, 1);        
      return; }
    if (msg.IsTopSeller)
    { BuildInventoryDetailNew(msg); $get("thumbsdest").innerHTML = "";
    //if(msg.SubCategoryKey == 0)
        getProductByCategory(msg.CategoryKey, 1);
    //else
    //    getProductBySubCategory(msg.SubCategoryKey, 1);        
     return; }
    

    BuildInventoryDetailNew(msg);
    $get("thumbsdest").innerHTML = "";
    //if (msg.SubCategoryKey == 0)
        getProductByCategory(msg.CategoryKey, 1);
    //else
    //    getProductBySubCategory(msg.SubCategoryKey, 1);        
     
    }

function BuildInventoryDetailNew(msg) {

    var details = '<table style="width:800px;"><tr><td style="width:175px; vertical-align: middle; text-align:center;"><img src="images/living_made_easy.jpg" alt="easyhome" style=" width:auto;max-width:200px; max-height:225px; height:auto;" border="0"  /></td>';
    details += '<td style="width:325px; height:225px; vertical-align: middle; text-align:center;"><img src="' + msg.ThumbUrl + '"  border="0"  style="width:auto;max-width:325px; max-height:225px; height:auto;" alt="' + msg.Description + '" /></td>';
    details += '<td style="width:270px;  vertical-align: middle;" >';
    details += '<table><tr><td><img alt="vendor" src="' + msg.VendorUrl + '" style="height:25px;"  border="0"/></td></tr><tr><td style=" font-weight:bold;color:#000;" id="invDesc">' + msg.Description + '</td></tr><tr><td><span style="font-size:.9em; color:#808080;">' + msg.Features + '</span></td>';
    details += '<tr><td><table><tr><td style="font-size: 4em;font-weight: bold;	color: #51b848; vertical-align:top;">$' + msg.WebRate + '<span style="color:#808080; font-size:.5em; vertical-align:top;">*</span></td><td>';
    details += '<table><tr><td style="border: solid 1pt #ffffff;"><span>&nbsp;&nbsp;<a href="#" onclick="AddToCart(' + msg.InventoryKey + ');CartLog(' + msg.InventoryKey + ');"><img border="0" alt="Add to cart" src="images/products/btnCart.gif" style="width:1.7em;" /></a>&nbsp;&nbsp;<a href="#" onclick="LoadEmailDialog(' + msg.InventoryKey + ')"><img border="0" alt="Email to a friend" src="images/products/btnEmail.gif" style="width:1.7em;" /></a></span></td></tr><tr><td><span style="font-size: .6em;color: #808080;">per week</span><br><span style="color:#ff0000; font-weight:bold;">' + msg.ExpiryTag + '</span></td></tr></table></td></tr><tr><td colspan="2"><span style="font-size: .6em;color: #808080;">Cost of Product:' + msg.CostOfProduct + ' Cost of TPC:' + msg.CostOfTPC + '</span></td></tr><tr><td colspan="2"><span style="font-size:.8em;">' + msg.RSSSModel + '</span></table></td></tr></table>';
    details += '</td></tr></table>';
    $get("DetailItem").innerHTML = details;

    scroll(0, 0);
}

function BuildBestValueDetailNew(msg) {

    var details = '<table style="width:800px;"><tr><td style="width:175px; vertical-align: middle; text-align:center;"><img src="images/products/bestvalue.gif" alt="easyhome best value" style=" width:auto;max-width:200px; max-height:225px; height:auto;" border="0"  /></td>';
    details += '<td style="width:325px; height:225px; vertical-align: middle; text-align:center;"><img src="' + msg.ThumbUrl + '"  border="0"  style="width:auto;max-width:325px; max-height:225px; height:auto;" alt="' + msg.Description + '" /></td>';
    details += '<td style="width:270px;  vertical-align: middle;" >';
    details += '<table><tr><td><img alt="vendor" src="' + msg.VendorUrl + '" style="height:25px;"  border="0"/></td></tr><tr><td style=" font-weight:bold;color:#000;" id="invDesc">' + msg.Description + '</td></tr><tr><td><span style="font-size:.9em; color:#808080;">' + msg.Features + '</span></td>';
    details += '<tr><td><table><tr><td style="font-size: 4em;font-weight: bold;	color: #51b848; vertical-align:top;">$' + msg.WebRate + '<span style="color:#808080; font-size:.5em; vertical-align:top;">*</span></td><td>';
    details += '<table><tr><td style="border: solid 1pt #ffffff;"><span>&nbsp;&nbsp;<a href="#" onclick="AddToCart(' + msg.InventoryKey + ');CartLog(' + msg.InventoryKey + ');"><img border="0" alt="Add to cart" src="images/products/btnCart.gif" style="width:1.7em;" /></a>&nbsp;&nbsp;<a href="#" onclick="LoadEmailDialog(' + msg.InventoryKey + ')"><img border="0" alt="Email to a friend" src="images/products/btnEmail.gif" style="width:1.7em;" /></a></span></td></tr><tr><td><span style="font-size: .6em;color: #808080;">per week<n><br><span style="color:#ff0000; font-weight:bold;">' + msg.ExpiryTag + '</span></td></tr></table></td></tr><tr><td colspan="2"><span style="font-size: .6em;color: #808080;">Cost of Product:' + msg.CostOfProduct + ' Cost of TPC:' + msg.CostOfTPC + '</span></td></tr><tr><td colspan="2"><span style="font-size:.8em;">' + msg.RSSSModel + '</span></table></td></tr></table>';
    
    details += '</td></tr></table>';

    $get("DetailItem").innerHTML = details;
}

function BuildFeaturedItemDetailNew(msg) {

    var details = '<table style="width:800px;"><tr><td style="width:175px; vertical-align: middle; text-align:center;"><img src="images/products/featuredItem_logo.jpg" alt="easyhome featured item" style=" width:auto;max-width:200px; max-height:225px; height:auto;" border="0"  /></td>';
    details += '<td style="width:325px; height:225px; vertical-align: middle; text-align:center;"><img src="' + msg.ThumbUrl + '"  border="0"  style="width:auto;max-width:325px; max-height:225px; height:auto;" alt="' + msg.Description + '" /></td>';
    details += '<td style="width:270px;  vertical-align: middle;" >';
    details += '<table><tr><td><img alt="vendor" src="' + msg.VendorUrl + '" style="height:25px;"  border="0"/></td></tr><tr><td style=" font-weight:bold;color:#000;" id="invDesc">' + msg.Description + '</td></tr><tr><td><span style="font-size:.9em; color:#808080;">' + msg.Features + '</span></td>';
    details += '<tr><td><table><tr><td style="font-size: 4em;font-weight: bold;	color: #51b848; vertical-align:top;">$' + msg.WebRate + '<span style="color:#808080; font-size:.5em; vertical-align:top;">*</span></td><td>';
    details += '<table><tr><td style="border: solid 1pt #ffffff;"><span>&nbsp;&nbsp;<a href="#" onclick="AddToCart(' + msg.InventoryKey + ');CartLog(' + msg.InventoryKey + ');"><img border="0" alt="Add to cart" src="images/products/btnCart.gif" style="width:1.7em;" /></a>&nbsp;&nbsp;<a href="#" onclick="LoadEmailDialog(' + msg.InventoryKey + ')"><img border="0" alt="Email to a friend" src="images/products/btnEmail.gif" style="width:1.7em;" /></a></span></td></tr><tr><td><span style="font-size: .6em;color: #808080;">per week</span><br><span style="color:#ff0000; font-weight:bold;">' + msg.ExpiryTag + '</span></td></tr></table></td></tr><tr><td colspan="2"><span style="font-size: .6em;color: #808080;">Cost of Product:' + msg.CostOfProduct + ' Cost of TPC:' + msg.CostOfTPC + '</span></td></tr><tr><td colspan="2"><span style="font-size:.8em;">' + msg.RSSSModel + '</span></table></td></tr></table>';
    
    details += '</td></tr></table>';

    $get("DetailItem").innerHTML = details;
}

function BuildPromoItemDetailNew(msg) {

    var details = '<table style="width:800px;"><tr><td style="width:175px; vertical-align: middle; text-align:center;"><img src="images/12DaysOfChristmas/dotd.jpg" alt="easyhome featured item" style=" width:auto;max-width:200px; max-height:225px; height:auto;" border="0"  /></td>';
    details += '<td style="width:325px; height:225px; vertical-align: middle; text-align:center;"><img src="' + msg.ThumbUrl + '"  border="0"  style="width:auto;max-width:325px; max-height:225px; height:auto;" alt="' + msg.Description + '" /></td>';
    details += '<td style="width:270px;  vertical-align: middle;" >';
    details += '<table><tr><td><img alt="vendor" src="' + msg.VendorUrl + '" style="height:25px;"  border="0"/></td></tr><tr><td style=" font-weight:bold;color:#000;" id="invDesc">' + msg.Description + '</td></tr><tr><td><span style="font-size:.9em; color:#808080;">' + msg.Features + '</span></td>';
    details += '<tr><td><table><tr><td style="font-size: 4em;font-weight: bold;	color: #51b848; vertical-align:top;">$' + msg.WebRate + '<span style="color:#808080; font-size:.5em; vertical-align:top;">*</span></td><td>';
    details += '<table><tr><td style="border: solid 1pt #ffffff;"><span>&nbsp;&nbsp;<a href="#" onclick="AddToCart(' + msg.InventoryKey + ');CartLog(' + msg.InventoryKey + ');"><img border="0" alt="Add to cart" src="images/products/btnCart.gif" style="width:1.7em;" /></a>&nbsp;&nbsp;<a href="#" onclick="LoadEmailDialog(' + msg.InventoryKey + ')"><img border="0" alt="Email to a friend" src="images/products/btnEmail.gif" style="width:1.7em;" /></a></span></td></tr><tr><td><span style="font-size: .6em;color: #808080;">per week</span><br><span style="color:#ff0000; font-weight:bold;">' + msg.ExpiryTag + '</span></td></tr></table></td></tr><tr><td colspan="2"><span style="font-size: .6em;color: #808080;">Cost of Product:' + msg.CostOfProduct + ' Cost of TPC:' + msg.CostOfTPC + '</span></td></tr><tr><td colspan="2"><span style="font-size:.8em;">' + msg.RSSSModel + '</span></table></td></tr></table>';

    details += '</td></tr></table>';

    $get("DetailItem").innerHTML = details;
}

function jsonTest() {
    productsWS.GetProduct(os, of);
}


function os(res) {
    pushDetail(res);
}
function of(res) {
    alert('err');
    alert(res);
}

var typekey = 0;
var categorykey = 0;
var subcategorykey = 0;
var subcategorytypekey = 0;
var price = 0;

function filterPrice(pr) {
    $get("thumbsdest").innerHTML = "";
    price = pr;    
    if (typekey > 0)
        getProductByType(typekey, pr);

    if (categorykey > 0)
        getProductByCategory(categorykey, 1);

    if (subcategorykey > 0)
        getProductBySubCategory(subcategorykey, 1);

    if (subcategorytypekey > 0)
        getProductBySubCategoryType(subcategorytypekey, 1);

    //$("#thumbsdest").fadeTo("slow", 1.0);
    //$applications = $('#thumbsdest');
    //$data = $applications.clone();
    
}

function loadProductByCategory(msg, cnt) {
    
    typekey = 0;
    categorykey = msg.CategoryKey;
    subcategorykey = 0;
    subcategorytypekey = 0;
    productsWS.GetRegionProductByCategory(msg.CategoryKey, 1, 1, price, pbcos, pbcof);
}

function getProductByCategory(ck, cnt) {
    typekey = 0;
    categorykey = ck;
    subcategorykey = 0;
    subcategorytypekey = 0;
    productsWS.GetRegionProductByCategory(ck, 1, 1, price, pbcos, pbcof);
}

function searchProducts(stxt, cnt) {
    
    price = 0;
    $get("thumbsdest").innerHTML = "";
    //$("#thumbsdest").fadeTo("slow", 1.0);
    //$applications = $('#thumbsdest');
    //$data = $applications.clone();
    productsWS.GetProductBySearch(stxt, 1,1, 0, pbcos, pbcof);
    
}

function getProductBySubCategory(sk, cnt) {
    typekey = 0;
    categorykey = 0;
    subcategorykey = sk;
    subcategorytypekey = 0;
    productsWS.GetRegionProductBySubCategory(sk, 1, 1, price, pbcos, pbcof);
}

function getProductBySubCategoryType(stk, cnt) {
    typekey = 0;
    categorykey = 0;
    subcategorykey = 0;
    subcategorytypekey = stk;
    productsWS.GetRegionProductBySubCategoryType(stk, 1, 1, price, pbcos, pbcof);
}

function getProductByType(tk) {
    typekey = tk;
    categorykey = 0;
    subcategorykey = 0;
    subcategorytypekey = 0;
    productsWS.GetProductByType(tk, 1, price, pbtos, pbtof);
}

function getHomeMenu() {
    productsWS.GetHomeMenu(ghmos, ghmof);
}

function ghmos(res) {
    alert(res);
}

function ghmof(res) {
    alert(res);
}

function pbcos(res) {
    var lBox = $('ul[id$=thumbsdest]');
    var inv = res;
    var cnt = 1;
    if (inv.length > 0) {
        for (var key in inv) {

            if (cnt == 4)
            { cnt = 1; }
            
            if(inv[key].WebRate!="undefined")
                $(lBox).append('<li data-id="test" data-type="A"><a href="#" onclick="getInventoryItem(' + inv[key].InventoryKey + ');ClickLog(' + inv[key].InventoryKey + ',1);" ><table cellpadding="0" cellspacing="0" style="width:100%;height:150px;"><tr><td style="width:150px;border: solid 1pt #ffffff;"><img border="0" alt="' + inv[key].Description + '"  src="' + inv[key].ThumbUrl + '" style="width:auto;max-width:150px;height:auto;max-height:150px;"></td></tr></table><table cellpadding="0" cellspacing="0"  style="width:100%;height:50px;"><tr><td style="border:solid 1pt #ffffff;"><img alt="vendor" src="' + inv[key].VendorUrl + '" style="height:25px;"  border="0"/></td><td style="border:solid 1pt #ffffff;"><span style="font-size: 2em;font-weight: bold;	color: #51b848; border: solid 1pt #ffffff;">$' + inv[key].WebRate + '</span>&nbsp;&nbsp;<img border="0"  alt="Add to cart" src="images/products/btnCart.gif" style="width:1.7em;" onclick="AddToCart(' + inv[key].InventoryKey + ')" /></td></tr><tr><td style="border:solid 1pt #ffffff; text-align:center;" colspan="2"><span style="font-size:.8em;">' + inv[key].Description + '</span></td></tr></table></a></li>');
            
            cnt++;
        }
    }
    else {
        alert("No product found");
    }
}

function pbtos(res) {
    var lBox = $('ul[id$=thumbs]');
    var inv = res;
    var cnt = 1;
    if (inv.length > 0) {
        for (var key in inv) {
            if (cnt == 4)
            { cnt = 1; }
            if (inv[key].WebRate != "undefined")
                $(lBox).append('<li data-id="test" data-type="A"><a href="#" onclick="getInventoryItem(' + inv[key].InventoryKey + ');ClickLog(' + inv[key].InventoryKey + ',1);" ><img border="0" alt="' + inv[key].Description + '"  src="' + inv[key].ThumbUrl + '" style="width:auto;max-width:150px;height:auto;max-height:150px;"><br><img alt="vendor" src="' + inv[key].VendorUrl + '" style="height:25px;"  border="0"/><span style="font-size: 2em;font-weight: bold;	color: #51b848;">$' + inv[key].WebRate + '</span><br><span style="font-size:.8em;">' + inv[key].Description + '</span></a></li>');
            cnt++;
        }
    }
    else {
        alert("No product found");
    }
}

function pbcof(res) {
    alert(res);
}

function pbtof(res) {
    alert(res);
}

function setMenuItems() {
    var li = "<li><a href='#'>Home</a><ul><li><a href='#'>Furniture</a></li><li><a href='#'>Appliances</a></li><li><a href='#'>Electronics</a></li><li><a href='#'>Computers</a></li></ul></li><li class='current'><a href='#'>Furniture Accessories</a></li>";
    $get("breadcrumbs").innerHTML = "";
    $get("breadcrumbs").innerHTML = li;
    $('#breadcrumbs').xBreadcrumbs();
}

function setFurnitureItems() {
    $get("breadcrumbs").innerHTML = "";
    $get("breadcrumbs").innerHTML = li;
}


function ClickLog(key, element) {
    //var randomnumber = Math.floor(Math.random() * 11)
    $.ajax({
        url: 'ClickLog.ashx?key=' + key + '&Element='+ element+'&View=1&r=' + Math.random(),
        success: function(data) {
            //alert('Click logged');
        }
    });
}

function CartLog(key) {
    //var randomnumber = Math.floor(Math.random() * 11)

    $.ajax({
        url: 'ClickLog.ashx?key=' + key + '&Cart=1&r=' + Math.random(),
        success: function(data) {
            //alert('Cart logged');
        }
    });
}


function CheckOut(key) {
    //var randomnumber = Math.floor(Math.random() * 11)

    $.ajax({
        url: 'ClickLog.ashx?key=' + key + '&CheckOut=1&r=' + Math.random(),
        success: function(data) {
            //alert('CheckOut logged');
        }
    });
}

