Subversion Repositories SmartDukaan

Rev

Blame | Last modification | View Log | RSS feed

/* 
     Example:
     addResearch(refdivid,reqtype) refdivid=pane1, reqtype=single/multi
     Multiple request: onclick="javascript:addResearch('pane2','multi');"
     Single request: onclick="javascript:addResearch('','single');"
     
     addToCart(refdivid,reqtype) refdivid=pane1, reqtype=single/multi
     Multiple request: onclick="javascript:addToCart('pane2','multi');"
     Single request: onclick="javascript:addToCart('','single');"
     
     NOTE: incase reqtype=list then refdivid should be product id
*/

function trackEventWithGA(eventCategory, actionName, label)     {
        if(typeof _gaq != "undefined" && _gaq != null)  {
                _gaq.push(['_trackEvent', eventCategory, actionName, label, 2]);
        }
}

function trackPageWithGA(page)  {
        if(typeof _gaq != "undefined" && _gaq != null)  {
                _gaq.push(['_trackPageview', page]);
        }
}

$(function() {
          $('.add-to-cart-icon').live('click', function() {
                clickedCatalogItem=$(this).closest('div.productDetails').find('a').html();
            var items = this.id;
            var itemsarray = items.split('-');
            addToCart(itemsarray[1], itemsarray[0]);
            trackEventWithGA('Home Page', 'Add to Cart Click', '');
            return false;
          });
          
          $('.best-deals .title a, .best-deals .productImg a').click(function(){
                  trackEventWithGA('Home Page', 'Product Link Click', 'Best Deals');
          });
          $('.latest-arrivals .title a, .latest-arrivals .productImg a').click(function(){
                  trackEventWithGA('Home Page', 'Product Link Click', 'Latest Arrivals');
          });
          $('.best-sellers .title a, .best-sellers .productImg a').click(function(){
                  trackEventWithGA('Home Page', 'Product Link Click', 'Best Sellers');
          });
        
        $("a.addtocart").live('click', function() {
                clickedCatalogItem=$(this).closest('div.productDetails').find('a').html();
                var catalogid = $(this).attr('catalogid');
                
                var radBtn = "";
                var param = "PARAMETERS_" + catalogid;
                radBtn += '<input type="hidden" id="selected_catalog_id" value=' + catalogid + ' />';
                $.each(eval(param), function(val, text) {
                        radBtn += '<label><input type="radio" name="radColorSel" value="' + val + '" /> <span>' + text + '</span></label>';
                });
                
                radBtn += '<div class="clearBoth"></div>';
                $(".colorSelLightBoxColor .content").html(radBtn);

                $.fn.colorbox({
                        inline:true, 
                        href:".colorSelLightBoxColor", 
                        open:true, 
                        onComplete:function(){
                                var posLeft = (document.documentElement.clientWidth / 2) - (parseInt($('#colorbox').css('width')) / 2);
                                var posTop = (document.documentElement.clientHeight / 2) - (parseInt($('#colorbox').css('height')) / 2);

                                $('#colorbox').css('top', posTop);
                                $('#colorbox').css('left', posLeft);
                            trackEventWithGA('Home Page', 'Add to Cart Click', '');
                        }
                });
        });
        
        
        $('#notify').live('click', function() {
                emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
                email=$('#emailId').val();
                if(!email.match(emailExp)){
                        alert("Please enter valid email.");
                        return false;
                }
                itemId=$('#itemId').val();
                emailId=$('#emailId').val();
                jQuery.ajax({
                 type: "POST",
                 url: "/product-notification",
                 data: "emailId="+emailId+"&itemId="+itemId,
                 success: function(msg){
                        $('#notificationBox').html("<h3>We will notify you once we get the stock.</h3>");        
                 }
                });
        });
        

    $("#query").autocomplete({  autoFocus: true, minLength: 3,
        source: "/auto-suggest",
        select: function( event, ui ) {
                this.value = ui.item.value;
                        $('#formSearch').submit();
                        return false;
                }
    });
    
});

function addToCart(refdivid,reqtype){
     var seldata="";
     var tot=0;
     var saprt="";
     if(reqtype == "multi"){
          var containerdiv = "#" + refdivid;
          var par= containerdiv + " input[type=checkbox]:checked";
          var isMulti = false;
          $(par).each(function(){
                  
                   var totalcolors = $(this).attr('totalcolors');
                   saprt = (tot>0)? "_":"";
               seldata += saprt+$(this).attr('itemid');

                   if(totalcolors != 1) {
                           isMulti = true;
                   }
               tot++;
          });
          if(isMulti == true){
                  alert("Some items are available in multiple colors. Please go to product page and select color you want.");
                  return;
          } else        {
                  var items = seldata.split('_');
                  for(var i in items)   {
                          trackEventWithGA('Order', 'Add to Cart', items[i]);
                  }
          }
     }else if(reqtype == "single"){
          seldata = $("#item_id").val();
                  trackEventWithGA('Order', 'Add to Cart', seldata + '');
          tot=1;
     }else if(reqtype == "list"){
          var seldata=refdivid;
                  trackEventWithGA('Order', 'Add to Cart', seldata + '');
          tot=1;
     }

     if(tot > 0)        {
         jQuery.ajax({
                 type: "POST",
                 url: "/addtocart",
                 data: "productid=" + seldata,
                 success: function(msg){
                         if(msg==""){
                                 window.location = "/cart";
                         }else{
                                 trackEventWithGA('Order', 'OOS Lightbox', clickedCatalogItem);
                                 displayRelatedProducts(msg);
                         }
                 }
         });
     } else     {
         alert("Please select atleast one product");
     }
}


function selectColor(){
        var selected = $(".colorSelLightBoxColor .content input[name='radColorSel']:checked");
        var itemid = selected.val();
        
        trackEventWithGA('Order', 'Select Product Color', itemid);
        
        if(itemid == undefined) {
                alert('Please select a color.');
        } else  {
                addToCart(itemid,'list');
        }
}


function displayRelatedProducts(message){
        $.colorbox({
                html:message,
                maxWidth:"400px",
                maxHeight:"410px",
                width:"400px",
                
                onComplete: function(){
                        $('#cartRecommendationBox').show().find('.widgetChkBox').hide();
                },
                onCleanup: function(){
                        $('#cartRecommendationBox').hide();
                }
        });

}



function addResearch(refdivid, reqtype){
     var research_tot = $("#research_total").val();
     var seldata = [];
     var tot = 0;
     var fromsrc = "";
     
     if(reqtype == "multi")     {
          var containerdiv = "#" + refdivid;     
          var par = containerdiv + " input[type=checkbox]:checked";
          $(par).each(function(){
               seldata.unshift($(this).val());
               tot ++;
          });
     }else if(reqtype == "single")      {
          seldata.unshift($("#product_id").val());
          tot = 1;
          fromsrc = "product_page";
     }else if(reqtype == "list"){
          seldata.unshift(refdivid);
          tot = 1;
          fromsrc = "other_pages";
     }

     if(tot > 0)        {
          jQuery.ajax({
               type: "POST",
                      url: "/addtoresearch",
                      data: "productid=[" + seldata +  "]&fromsrc=" + fromsrc,
                      success: function(msg){
                           var postSuccessDOMProcessing = function(objValue)    {
                                   trackEventWithGA('Research', 'Add to Research', objValue);
                                   add_to_storage_set("resitems", objValue);
                                   //Updating the count
                                   var truncFields=function(){
                                           var msgDom = $(msg);
                                           $("#pane1").prepend(msgDom);
                                           $(msgDom).find("a.truncate").truncate({addtitle: true});
                                   $(msgDom).find("div.price").truncate({addtitle: true});
                                   $(msgDom).find("div.text").truncate({addtitle: true});
                                   $(msgDom).animate({ backgroundColor: "#fcffb3" }, 'slow');
                                   $(msgDom).animate({ backgroundColor: "transparent" }, 'slow');
                                   $('#research_compare_count').html($('#myresearch input[type="checkbox"]:checked').length);
                                   };
                                   if($('.common-widget-top-bar').data('is_colapsed')){
                                           $("#myresearch").children(".common-widget-top-bar").trigger('click',[truncFields]);
                                   }else {
                                           truncFields();
                                   }
                           };
                           
                           if(msg == 0){
                                alert( "Product is already in compare list" );
                           } else       {
                                var t = (research_tot * 1) + (tot * 1);
                                $("#research_total").val(t);
                                if(t > 0)       {
                                     $("#research_default").css("display","none");
                                }     
                                if(reqtype == "multi")  {
                                     jQuery.each(seldata,function(intIndex, objValue)   {
                                         postSuccessDOMProcessing(objValue);
                                     });
                                     
                                } else if(reqtype == "single")  {
                                        postSuccessDOMProcessing(seldata);
                                        
                                } else if(reqtype == "list")    {
                                        postSuccessDOMProcessing(seldata);
                                }
                           }
                      }
          });
     } else     {
          alert("Please select atleast one product");
     }
}




function scrollWinToTop(){
    $('html, body').animate({
      scrollTop: $("body").offset().top
    }, 0);
}

function changeShipAddress(id){
  addressId = id.substring(20);
   jQuery.ajax({
      type: "POST",
      url: "/shipping",
      data: "action=change&addressid="+addressId,
      success: function(msg){
         $("#addressid").val(msg);
      }
   });
  document.getElementById('shipAddressContainer').innerHTML = document.getElementById(id).innerHTML;
        $(".proceed-bttn .yellow-image").removeClass('imgDisableButton').addClass('imgEnableButton');
        $(".proceed-bttn input[disabled='']").removeAttr('disabled');

        $('#newShipAdr').hide();
        scrollWinToTop();

}

function merge_history_items(){
        params = $.Storage.get("histitems");
        
        jQuery.ajax({
                type: "POST",
                url: "/browse-history",
                data: "id="+params,
                cache: false,
                success: function(json) {
                    $.Storage.set("histitems", json);
                    load_history_widget();
            }
        });
}

function load_history_widget() {
        
        var browseHistoryWidgetContainer = $("#browsehistory");
        
        if($(browseHistoryWidgetContainer).length == 0) {
                return;
        }
        var histitems = $.Storage.get("histitems");
        if (histitems == "[]") {
                $(browseHistoryWidgetContainer).find(".common-widget-content-area").html("");
                $(browseHistoryWidgetContainer).children('.common-widget-top-bar').trigger('click');
                return;
        }
        var params = "/" + histitems;
        
        jQuery.ajax({
                type: "GET",
                url: "/browse-history" + params,
                cache: true,
                success: function(html){
                    $("#browsehistory").html(html);

                    // Product Title
                    $("#browsehistory table td div a").each(function(){
                            $(this).truncate({addtitle: true}); 
                    });

                // Product Price
                $("#browsehistory table td div div.price").each(function(){
                        $(this).truncate({addtitle: true});
                    });

                    // Product Details
                    $("#browsehistory table td div div.text").each(function(){
                            $(this).truncate({addtitle: true});
                    });
            }
        });
}

function add_to_storage_set(key, id) {
        var set = eval($.Storage.get(key));
        if (set) {
                $.each(set, function(index, value) {
                        if (value == id) {
                                set.splice(index, 1);
                        }
                });
        }
        set.unshift(id);
        if (set.length>10)
        {
                set.splice(10, set.length-10);
        }
        $.Storage.set(key, "[" + set.toString() + "]");
}

function delete_from_storage_set(key, id) {
        var set = eval($.Storage.get(key));
        if (set) {
                $.each(set, function(index, value) {
                        if (value == id) {
                                set.splice(index, 1);
                        }
                });
        }
        $.Storage.set(key, "[" + set.toString() + "]");
}

function add_track_log(event, url, data) {
        if ($.cookie("uafc")) {
                jQuery.ajax( {
                        url : "/tracking",
                        type : "POST",
                        data : {
                                "event" : event,
                                "url" : url,
                                "data" : data
                        },
                        cache : false
                });
        }
}

function load_accessories_widget(){
        var accessoriesContainer = $("#accessories");
        
        if(accessoriesContainer.length == 0) {
                return;
        }

        jQuery.ajax({
                type : "GET",
                url : "/related-accessories/" + $("#product_id").val(),
                cache: true,
                success : function(html) {
                        
                        if($.trim(html).length > 0)     {
                            $(accessoriesContainer).html(html);
                            
                            var accessoryInfoContainer = $(accessoriesContainer).find("table td div");
                            // Product Title
                            $(accessoryInfoContainer).children("a").each(function() {
                                $(this).truncate({addtitle : true});
                            });

                            // Product Price
                            $(accessoryInfoContainer).children("div.price").each(function() {
                                $(this).truncate({addtitle : true});
                                });

                            // Product Details
                            $(accessoryInfoContainer).children("div.text").each(function() {
                                $(this).truncate( {addtitle : true});
                                });
                        } else  {
                                $(accessoriesContainer).hide();
                        }
            }
        });
}